mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-31 22:45:45 +00:00
clean up
This commit is contained in:
+58
-58
@@ -68,83 +68,83 @@ char *itoa(int i)
|
||||
int run_as(const char *user)
|
||||
{
|
||||
#ifndef __MINGW32__
|
||||
if (user[0])
|
||||
if (user[0])
|
||||
{
|
||||
#ifdef HAVE_GETPWNAM_R
|
||||
struct passwd pwdbuf, *pwd;
|
||||
size_t buflen;
|
||||
int err;
|
||||
struct passwd pwdbuf, *pwd;
|
||||
size_t buflen;
|
||||
int err;
|
||||
|
||||
for (buflen = 128;; buflen *= 2)
|
||||
for (buflen = 128;; buflen *= 2)
|
||||
{
|
||||
char buf[buflen]; /* variable length array */
|
||||
char buf[buflen]; /* variable length array */
|
||||
|
||||
/* Note that we use getpwnam_r() instead of getpwnam(),
|
||||
which returns its result in a statically allocated buffer and
|
||||
cannot be considered thread safe. */
|
||||
err = getpwnam_r(user, &pwdbuf, buf, buflen, &pwd);
|
||||
if(err == 0 && pwd)
|
||||
/* Note that we use getpwnam_r() instead of getpwnam(),
|
||||
which returns its result in a statically allocated buffer and
|
||||
cannot be considered thread safe. */
|
||||
err = getpwnam_r(user, &pwdbuf, buf, buflen, &pwd);
|
||||
if(err == 0 && pwd)
|
||||
{
|
||||
/* setgid first, because we may not be allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
/* setgid first, because we may not be allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
{
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
{
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (err != ERANGE)
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (err != ERANGE)
|
||||
{
|
||||
if(err)
|
||||
LOGE("run_as user '%s' could not be found: %s",user,strerror(err));
|
||||
else
|
||||
LOGE("run_as user '%s' could not be found.",user);
|
||||
return 0;
|
||||
}
|
||||
else if (buflen >= 16*1024)
|
||||
if(err)
|
||||
LOGE("run_as user '%s' could not be found: %s",user,strerror(err));
|
||||
else
|
||||
LOGE("run_as user '%s' could not be found.",user);
|
||||
return 0;
|
||||
}
|
||||
else if (buflen >= 16*1024)
|
||||
{
|
||||
/* If getpwnam_r() seems defective, call it quits rather than
|
||||
keep on allocating ever larger buffers until we crash. */
|
||||
LOGE("getpwnam_r() requires more than %u bytes of buffer space.",(unsigned)buflen);
|
||||
return 0;
|
||||
}
|
||||
/* Else try again with larger buffer. */
|
||||
}
|
||||
/* If getpwnam_r() seems defective, call it quits rather than
|
||||
keep on allocating ever larger buffers until we crash. */
|
||||
LOGE("getpwnam_r() requires more than %u bytes of buffer space.",(unsigned)buflen);
|
||||
return 0;
|
||||
}
|
||||
/* Else try again with larger buffer. */
|
||||
}
|
||||
#else
|
||||
/* No getpwnam_r() :-( We'll use getpwnam() and hope for the best. */
|
||||
struct passwd *pwd;
|
||||
/* No getpwnam_r() :-( We'll use getpwnam() and hope for the best. */
|
||||
struct passwd *pwd;
|
||||
|
||||
if (!(pwd=getpwnam(user)))
|
||||
if (!(pwd=getpwnam(user)))
|
||||
{
|
||||
LOGE("run_as user %s could not be found.",user);
|
||||
return 0;
|
||||
}
|
||||
/* setgid first, because we may not allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
LOGE("run_as user %s could not be found.",user);
|
||||
return 0;
|
||||
}
|
||||
/* setgid first, because we may not allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
{
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
{
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif //__MINGW32__
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user