* src/su.c: Use a bool when possible instead of int integers.

* src/su.c: Add brackets and parenthesis.
	* src/su.c: Avoid implicit conversion of pointers / integers
	/ chars to booleans.
	* src/su.c: Ignore the return value of pam_end() before
	exiting.
	* src/su.c: Avoid assignments in comparisons.
	* src/su.c: Avoid multi-statements lines.
This commit is contained in:
nekral-guest 2008-06-10 22:07:51 +00:00
parent c573f432fe
commit d7ffaf94b1
2 changed files with 158 additions and 103 deletions

View File

@ -1,3 +1,14 @@
2008-06-10 Nicolas François <nicolas.francois@centraliens.net>
* src/su.c: Use a bool when possible instead of int integers.
* src/su.c: Add brackets and parenthesis.
* src/su.c: Avoid implicit conversion of pointers / integers
/ chars to booleans.
* src/su.c: Ignore the return value of pam_end() before
exiting.
* src/su.c: Avoid assignments in comparisons.
* src/su.c: Avoid multi-statements lines.
2008-06-10 Nicolas François <nicolas.francois@centraliens.net> 2008-06-10 Nicolas François <nicolas.francois@centraliens.net>
* lib/prototypes.h, libmisc/valid.c: Change the prototype of * lib/prototypes.h, libmisc/valid.c: Change the prototype of

250
src/su.c
View File

@ -80,11 +80,11 @@ static char name[BUFSIZ];
static char oldname[BUFSIZ]; static char oldname[BUFSIZ];
/* If nonzero, change some environment vars to indicate the user su'd to. */ /* If nonzero, change some environment vars to indicate the user su'd to. */
static int change_environment; static bool change_environment;
#ifdef USE_PAM #ifdef USE_PAM
static pam_handle_t *pamh = NULL; static pam_handle_t *pamh = NULL;
static int caught = 0; static bool caught = false;
#endif #endif
static char *Prog; static char *Prog;
@ -139,19 +139,19 @@ static int iswheel (const char *username)
#endif /* !USE_PAM */ #endif /* !USE_PAM */
/* borrowed from GNU sh-utils' "su.c" */ /* borrowed from GNU sh-utils' "su.c" */
static int restricted_shell (const char *shellstr) static bool restricted_shell (const char *shellstr)
{ {
char *line; char *line;
setusershell (); setusershell ();
while ((line = getusershell ()) != NULL) { while ((line = getusershell ()) != NULL) {
if (*line != '#' && strcmp (line, shellstr) == 0) { if (('#' != *line) && (strcmp (line, shellstr) == 0)) {
endusershell (); endusershell ();
return 0; return false;
} }
} }
endusershell (); endusershell ();
return 1; return true;
} }
static void su_failure (const char *tty) static void su_failure (const char *tty)
@ -159,9 +159,10 @@ static void su_failure (const char *tty)
sulog (tty, 0, oldname, name); /* log failed attempt */ sulog (tty, 0, oldname, name); /* log failed attempt */
#ifdef USE_SYSLOG #ifdef USE_SYSLOG
if (getdef_bool ("SYSLOG_SU_ENAB")) if (getdef_bool ("SYSLOG_SU_ENAB"))
SYSLOG ((pwent.pw_uid ? LOG_INFO : LOG_NOTICE, SYSLOG (((0 != pwent.pw_uid) ? LOG_INFO : LOG_NOTICE,
"- %s %s:%s", tty, "- %s %s:%s", tty,
oldname[0] ? oldname : "???", name[0] ? name : "???")); ('\0' != oldname[0]) ? oldname : "???",
('\0' != name[0]) ? name : "???"));
closelog (); closelog ();
#endif #endif
exit (1); exit (1);
@ -172,14 +173,14 @@ static void su_failure (const char *tty)
/* Signal handler for parent process later */ /* Signal handler for parent process later */
static void catch_signals (unused int sig) static void catch_signals (unused int sig)
{ {
++caught; caught = true;
} }
/* This I ripped out of su.c from sh-utils after the Mandrake pam patch /* This I ripped out of su.c from sh-utils after the Mandrake pam patch
* have been applied. Some work was needed to get it integrated into * have been applied. Some work was needed to get it integrated into
* su.c from shadow. * su.c from shadow.
*/ */
static void run_shell (const char *shellstr, char *args[], int doshell, static void run_shell (const char *shellstr, char *args[], bool doshell,
char *const envp[]) char *const envp[])
{ {
int child; int child;
@ -209,9 +210,9 @@ static void run_shell (const char *shellstr, char *args[], int doshell,
} }
/* parent only */ /* parent only */
sigfillset (&ourset); sigfillset (&ourset);
if (sigprocmask (SIG_BLOCK, &ourset, NULL)) { if (sigprocmask (SIG_BLOCK, &ourset, NULL) != 0) {
(void) fprintf (stderr, "%s: signal malfunction\n", Prog); (void) fprintf (stderr, "%s: signal malfunction\n", Prog);
caught = 1; caught = true;
} }
if (!caught) { if (!caught) {
struct sigaction action; struct sigaction action;
@ -221,14 +222,14 @@ static void run_shell (const char *shellstr, char *args[], int doshell,
action.sa_flags = 0; action.sa_flags = 0;
sigemptyset (&ourset); sigemptyset (&ourset);
if (sigaddset (&ourset, SIGTERM) if ( (sigaddset (&ourset, SIGTERM) != 0)
|| sigaddset (&ourset, SIGALRM) || (sigaddset (&ourset, SIGALRM) != 0)
|| sigaction (SIGTERM, &action, NULL) || (sigaction (SIGTERM, &action, NULL) != 0)
|| sigprocmask (SIG_UNBLOCK, &ourset, NULL) || (sigprocmask (SIG_UNBLOCK, &ourset, NULL) != 0)
) { ) {
fprintf (stderr, fprintf (stderr,
"%s: signal masking malfunction\n", Prog); "%s: signal masking malfunction\n", Prog);
caught = 1; caught = true;
} }
} }
@ -238,14 +239,14 @@ static void run_shell (const char *shellstr, char *args[], int doshell,
pid = waitpid (-1, &status, WUNTRACED); pid = waitpid (-1, &status, WUNTRACED);
if ((pid != -1) && WIFSTOPPED (status)) { if ((-1 != pid) && (0 != WIFSTOPPED (status))) {
/* The child (shell) was suspended. /* The child (shell) was suspended.
* Suspend su. */ * Suspend su. */
kill (getpid (), WSTOPSIG(status)); kill (getpid (), WSTOPSIG(status));
/* wake child when resumed */ /* wake child when resumed */
kill (pid, SIGCONT); kill (pid, SIGCONT);
} }
} while (WIFSTOPPED (status)); } while (0 != WIFSTOPPED (status));
} }
if (caught) { if (caught) {
@ -254,11 +255,11 @@ static void run_shell (const char *shellstr, char *args[], int doshell,
} }
ret = pam_close_session (pamh, 0); ret = pam_close_session (pamh, 0);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_close_session: %s", SYSLOG ((LOG_ERR, "pam_close_session: %s",
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret)); fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
pam_end (pamh, ret); (void) pam_end (pamh, ret);
exit (1); exit (1);
} }
@ -271,9 +272,8 @@ static void run_shell (const char *shellstr, char *args[], int doshell,
exit (-1); exit (-1);
} }
exit (WIFEXITED (status) exit ((0 != WIFEXITED (status)) ? WEXITSTATUS (status)
? WEXITSTATUS (status) : WTERMSIG (status) + 128);
: WTERMSIG (status) + 128);
} }
#endif #endif
@ -309,14 +309,15 @@ static void usage (void)
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
char *cp; char *cp;
const char *tty = 0; /* Name of tty SU is run from */ const char *tty = NULL; /* Name of tty SU is run from */
int doshell = 0; bool doshell = false;
int fakelogin = 0; bool fakelogin = false;
int amroot = 0; bool amroot = false;
uid_t my_uid; uid_t my_uid;
struct passwd *pw = 0; struct passwd *pw = NULL;
char **envp = environ; char **envp = environ;
char *shellstr = 0, *command = 0; char *shellstr = NULL;
char *command = NULL;
#ifdef USE_PAM #ifdef USE_PAM
char **envcp; char **envcp;
@ -340,7 +341,7 @@ int main (int argc, char **argv)
(void) bindtextdomain (PACKAGE, LOCALEDIR); (void) bindtextdomain (PACKAGE, LOCALEDIR);
(void) textdomain (PACKAGE); (void) textdomain (PACKAGE);
change_environment = 1; change_environment = true;
/* /*
* Get the program name. The program name is used as a prefix to * Get the program name. The program name is used as a prefix to
@ -380,7 +381,7 @@ int main (int argc, char **argv)
usage (); usage ();
break; break;
case 'l': case 'l':
fakelogin = 1; fakelogin = true;
break; break;
case 'm': case 'm':
case 'p': case 'p':
@ -388,7 +389,7 @@ int main (int argc, char **argv)
* user do not have a restricted shell, or if * user do not have a restricted shell, or if
* su is called by root. * su is called by root.
*/ */
change_environment = 0; change_environment = false;
break; break;
case 's': case 's':
shellstr = optarg; shellstr = optarg;
@ -398,11 +399,13 @@ int main (int argc, char **argv)
} }
} }
if (optind < argc && !strcmp (argv[optind], "-")) { if ((optind < argc) && (strcmp (argv[optind], "-") == 0)) {
fakelogin = 1; fakelogin = true;
optind++; optind++;
if (optind < argc && !strcmp (argv[optind], "--")) if ( (optind < argc)
&& (strcmp (argv[optind], "--") == 0)) {
optind++; optind++;
}
} }
} }
@ -415,11 +418,13 @@ int main (int argc, char **argv)
* Get the tty name. Entries will be logged indicating that the user * Get the tty name. Entries will be logged indicating that the user
* tried to change to the named new user from the current terminal. * tried to change to the named new user from the current terminal.
*/ */
if (isatty (0) && (cp = ttyname (0))) { cp = ttyname (0);
if (strncmp (cp, "/dev/", 5) == 0) if ((isatty (0) != 0) && (NULL != cp)) {
if (strncmp (cp, "/dev/", 5) == 0) {
tty = cp + 5; tty = cp + 5;
else } else {
tty = cp; tty = cp;
}
#ifndef USE_PAM #ifndef USE_PAM
is_console = console (tty); is_console = console (tty);
#endif #endif
@ -441,24 +446,27 @@ int main (int argc, char **argv)
* doesn't start with a "-" unless you specify the new user name. * doesn't start with a "-" unless you specify the new user name.
* Any remaining arguments will be passed to the user's login shell. * Any remaining arguments will be passed to the user's login shell.
*/ */
if (optind < argc && argv[optind][0] != '-') { if ((optind < argc) && ('-' != argv[optind][0])) {
STRFCPY (name, argv[optind++]); /* use this login id */ STRFCPY (name, argv[optind++]); /* use this login id */
if (optind < argc && !strcmp (argv[optind], "--")) if ((optind < argc) && (strcmp (argv[optind], "--") == 0)) {
optind++; optind++;
}
} }
if (!name[0]) /* use default user ID */ if ('\0' == name[0]) { /* use default user ID */
(void) strcpy (name, "root"); (void) strcpy (name, "root");
}
doshell = argc == optind; /* any arguments remaining? */ doshell = (argc == optind); /* any arguments remaining? */
if (command) if (NULL != command) {
doshell = 0; doshell = false;
}
/* /*
* Get the user's real name. The current UID is used to determine * Get the user's real name. The current UID is used to determine
* who has executed su. That user ID must exist. * who has executed su. That user ID must exist.
*/ */
pw = get_my_pwent (); pw = get_my_pwent ();
if (!pw) { if (NULL == pw) {
SYSLOG ((LOG_CRIT, "Unknown UID: %u", my_uid)); SYSLOG ((LOG_CRIT, "Unknown UID: %u", my_uid));
su_failure (tty); su_failure (tty);
} }
@ -470,14 +478,16 @@ int main (int argc, char **argv)
* Sort out the password of user calling su, in case needed later * Sort out the password of user calling su, in case needed later
* -- chris * -- chris
*/ */
if ((spwd = getspnam (oldname))) /* !USE_PAM, no need for xgetspnam */ spwd = getspnam (oldname); /* !USE_PAM, no need for xgetspnam */
if (NULL != spwd) {
pw->pw_passwd = spwd->sp_pwdp; pw->pw_passwd = spwd->sp_pwdp;
}
oldpass = xstrdup (pw->pw_passwd); oldpass = xstrdup (pw->pw_passwd);
#endif /* SU_ACCESS */ #endif /* SU_ACCESS */
#else /* USE_PAM */ #else /* USE_PAM */
ret = pam_start ("su", name, &conv, &pamh); ret = pam_start ("su", name, &conv, &pamh);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_start: error %d", ret); SYSLOG ((LOG_ERR, "pam_start: error %d", ret);
fprintf (stderr, _("%s: pam_start: error %d\n"), fprintf (stderr, _("%s: pam_start: error %d\n"),
Prog, ret)); Prog, ret));
@ -485,9 +495,10 @@ int main (int argc, char **argv)
} }
ret = pam_set_item (pamh, PAM_TTY, (const void *) tty); ret = pam_set_item (pamh, PAM_TTY, (const void *) tty);
if (ret == PAM_SUCCESS) if (PAM_SUCCESS == ret) {
ret = pam_set_item (pamh, PAM_RUSER, (const void *) oldname); ret = pam_set_item (pamh, PAM_RUSER, (const void *) oldname);
if (ret != PAM_SUCCESS) { }
if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_set_item: %s", SYSLOG ((LOG_ERR, "pam_set_item: %s",
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret)); fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
@ -505,16 +516,20 @@ int main (int argc, char **argv)
* The password file entries for the user is gotten and the account * The password file entries for the user is gotten and the account
* validated. * validated.
*/ */
if (!(pw = xgetpwnam (name))) { pw = xgetpwnam (name);
if (NULL == pw) {
(void) fprintf (stderr, _("Unknown id: %s\n"), name); (void) fprintf (stderr, _("Unknown id: %s\n"), name);
closelog (); closelog ();
exit (1); exit (1);
} }
#ifndef USE_PAM #ifndef USE_PAM
spwd = NULL; spwd = NULL;
if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0 if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
&& (spwd = getspnam (name))) /* !USE_PAM, no need for xgetspnam */ spwd = getspnam (name); /* !USE_PAM, no need for xgetspnam */
pw->pw_passwd = spwd->sp_pwdp; if (NULL != spwd) {
pw->pw_passwd = spwd->sp_pwdp;
}
}
#endif /* !USE_PAM */ #endif /* !USE_PAM */
pwent = *pw; pwent = *pw;
@ -534,30 +549,42 @@ int main (int argc, char **argv)
* The terminal type will be left alone if it is present in * The terminal type will be left alone if it is present in
* the environment already. * the environment already.
*/ */
if ((cp = getenv ("TERM"))) cp = getenv ("TERM");
if (NULL != cp) {
addenv ("TERM", cp); addenv ("TERM", cp);
}
#ifndef USE_PAM #ifndef USE_PAM
if ((cp = getdef_str ("ENV_TZ"))) cp = getdef_str ("ENV_TZ");
addenv (*cp == '/' ? tz (cp) : cp, NULL); if (NULL != cp) {
addenv (('/' == *cp) ? tz (cp) : cp, NULL);
}
/* /*
* The clock frequency will be reset to the login value if required * The clock frequency will be reset to the login value if required
*/ */
if ((cp = getdef_str ("ENV_HZ"))) cp = getdef_str ("ENV_HZ");
if (NULL != cp) {
addenv (cp, NULL); /* set the default $HZ, if one */ addenv (cp, NULL); /* set the default $HZ, if one */
}
/* /*
* Also leave DISPLAY and XAUTHORITY if present, else * Also leave DISPLAY and XAUTHORITY if present, else
* pam_xauth will not work. * pam_xauth will not work.
*/ */
if ((cp = getenv ("DISPLAY"))) cp = getenv ("DISPLAY");
if (NULL != cp) {
addenv ("DISPLAY", cp); addenv ("DISPLAY", cp);
if ((cp = getenv ("XAUTHORITY"))) }
cp = getenv ("XAUTHORITY");
if (NULL != cp) {
addenv ("XAUTHORITY", cp); addenv ("XAUTHORITY", cp);
}
#endif /* !USE_PAM */ #endif /* !USE_PAM */
} else { } else {
while (*envp) while (NULL != *envp) {
addenv (*envp++, NULL); addenv (*envp, NULL);
envp++;
}
} }
#ifndef USE_PAM #ifndef USE_PAM
@ -580,7 +607,8 @@ int main (int argc, char **argv)
*/ */
if (!amroot) { if (!amroot) {
if (pwent.pw_uid == 0 && getdef_bool ("SU_WHEEL_ONLY") if ( (0 == pwent.pw_uid)
&& getdef_bool ("SU_WHEEL_ONLY")
&& !iswheel (oldname)) { && !iswheel (oldname)) {
fprintf (stderr, fprintf (stderr,
_("You are not authorized to su %s\n"), name); _("You are not authorized to su %s\n"), name);
@ -610,50 +638,56 @@ int main (int argc, char **argv)
* use the current SHELL. * use the current SHELL.
* (unless another shell is required by the command line) * (unless another shell is required by the command line)
*/ */
if (shellstr == NULL && change_environment == 0) if ((NULL == shellstr) && !change_environment) {
shellstr = getenv ("SHELL"); shellstr = getenv ("SHELL");
}
/* For users with non null UID, if this user has a restricted /* For users with non null UID, if this user has a restricted
* shell, the shell must be the one specified in /etc/passwd * shell, the shell must be the one specified in /etc/passwd
*/ */
if (shellstr != NULL && !amroot && restricted_shell (pwent.pw_shell)) if ( (NULL != shellstr)
&& !amroot
&& restricted_shell (pwent.pw_shell)) {
shellstr = NULL; shellstr = NULL;
}
/* If the shell is not set at this time, use the shell specified /* If the shell is not set at this time, use the shell specified
* in /etc/passwd. * in /etc/passwd.
*/ */
if (shellstr == NULL) if (NULL == shellstr) {
shellstr = (char *) strdup (pwent.pw_shell); shellstr = (char *) strdup (pwent.pw_shell);
}
/* /*
* Set the default shell. * Set the default shell.
*/ */
if (shellstr == NULL || shellstr[0] == '\0') if ((NULL == shellstr) || ('\0' == shellstr[0])) {
shellstr = "/bin/sh"; shellstr = "/bin/sh";
}
signal (SIGINT, SIG_IGN); signal (SIGINT, SIG_IGN);
signal (SIGQUIT, SIG_IGN); signal (SIGQUIT, SIG_IGN);
#ifdef USE_PAM #ifdef USE_PAM
ret = pam_authenticate (pamh, 0); ret = pam_authenticate (pamh, 0);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_authenticate: %s", SYSLOG ((LOG_ERR, "pam_authenticate: %s",
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret)); fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
pam_end (pamh, ret); (void) pam_end (pamh, ret);
su_failure (tty); su_failure (tty);
} }
ret = pam_acct_mgmt (pamh, 0); ret = pam_acct_mgmt (pamh, 0);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
if (amroot) { if (amroot) {
fprintf (stderr, _("%s: %s\n(Ignored)\n"), Prog, fprintf (stderr, _("%s: %s\n(Ignored)\n"), Prog,
pam_strerror (pamh, ret)); pam_strerror (pamh, ret));
} else if (ret == PAM_NEW_AUTHTOK_REQD) { } else if (PAM_NEW_AUTHTOK_REQD == ret) {
ret = pam_chauthtok (pamh, PAM_CHANGE_EXPIRED_AUTHTOK); ret = pam_chauthtok (pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_chauthtok: %s", SYSLOG ((LOG_ERR, "pam_chauthtok: %s",
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, fprintf (stderr, _("%s: %s\n"), Prog,
pam_strerror (pamh, ret)); pam_strerror (pamh, ret));
pam_end (pamh, ret); (void) pam_end (pamh, ret);
su_failure (tty); su_failure (tty);
} }
} else { } else {
@ -661,7 +695,7 @@ int main (int argc, char **argv)
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, fprintf (stderr, _("%s: %s\n"), Prog,
pam_strerror (pamh, ret)); pam_strerror (pamh, ret));
pam_end (pamh, ret); (void) pam_end (pamh, ret);
su_failure (tty); su_failure (tty);
} }
} }
@ -690,8 +724,9 @@ int main (int argc, char **argv)
* expired password. * expired password.
*/ */
if (!amroot) { if (!amroot) {
if (!spwd) if (NULL == spwd) {
spwd = pwd_to_spwd (&pwent); spwd = pwd_to_spwd (&pwent);
}
if (expire (&pwent, spwd)) { if (expire (&pwent, spwd)) {
/* !USE_PAM, no need for xgetpwnam */ /* !USE_PAM, no need for xgetpwnam */
@ -699,8 +734,9 @@ int main (int argc, char **argv)
/* !USE_PAM, no need for xgetspnam */ /* !USE_PAM, no need for xgetspnam */
spwd = getspnam (name); spwd = getspnam (name);
if (pwd) if (NULL != pwd) {
pwent = *pwd; pwent = *pwd;
}
} }
} }
@ -712,7 +748,7 @@ int main (int argc, char **argv)
*/ */
if (!amroot) { if (!amroot) {
if (!isttytime (pwent.pw_name, "SU", time ((time_t *) 0))) { if (!isttytime (pwent.pw_name, "SU", time ((time_t *) 0))) {
SYSLOG ((pwent.pw_uid ? LOG_WARN : LOG_CRIT, SYSLOG (((0 != pwent.pw_uid) ? LOG_WARN : LOG_CRIT,
"SU by %s to restricted account %s", "SU by %s to restricted account %s",
oldname, name)); oldname, name));
su_failure (tty); su_failure (tty);
@ -724,23 +760,24 @@ int main (int argc, char **argv)
signal (SIGQUIT, SIG_DFL); signal (SIGQUIT, SIG_DFL);
cp = getdef_str ((pwent.pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH"); cp = getdef_str ((pwent.pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
if (!cp) { if (NULL == cp) {
addenv ("PATH=/bin:/usr/bin", NULL); addenv ("PATH=/bin:/usr/bin", NULL);
} else if (strchr (cp, '=')) { } else if (strchr (cp, '=') != NULL) {
addenv (cp, NULL); addenv (cp, NULL);
} else { } else {
addenv ("PATH", cp); addenv ("PATH", cp);
} }
if (getenv ("IFS")) /* don't export user IFS ... */ if (getenv ("IFS") != NULL) { /* don't export user IFS ... */
addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */ addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */
}
/* /*
* Even if --shell is specified, the subsystem login test is based on * Even if --shell is specified, the subsystem login test is based on
* the shell specified in /etc/passwd (not the one specified with * the shell specified in /etc/passwd (not the one specified with
* --shell, which will be the one executed in the chroot later). * --shell, which will be the one executed in the chroot later).
*/ */
if (pwent.pw_shell[0] == '*') { /* subsystem root required */ if ('*' == pwent.pw_shell[0]) { /* subsystem root required */
pwent.pw_shell++; /* skip the '*' */ pwent.pw_shell++; /* skip the '*' */
subsystem (&pwent); /* figure out what to execute */ subsystem (&pwent); /* figure out what to execute */
endpwent (); endpwent ();
@ -748,18 +785,20 @@ int main (int argc, char **argv)
goto top; goto top;
} }
sulog (tty, 1, oldname, name); /* save SU information */ sulog (tty, true, oldname, name); /* save SU information */
endpwent (); endpwent ();
endspent (); endspent ();
#ifdef USE_SYSLOG #ifdef USE_SYSLOG
if (getdef_bool ("SYSLOG_SU_ENAB")) if (getdef_bool ("SYSLOG_SU_ENAB")) {
SYSLOG ((LOG_INFO, "+ %s %s:%s", tty, SYSLOG ((LOG_INFO, "+ %s %s:%s", tty,
oldname[0] ? oldname : "???", name[0] ? name : "???")); ('\0' != oldname[0]) ? oldname : "???",
('\0' != name[0]) ? name : "???"));
}
#endif #endif
#ifdef USE_PAM #ifdef USE_PAM
/* set primary group id and supplementary groups */ /* set primary group id and supplementary groups */
if (setup_groups (&pwent)) { if (setup_groups (&pwent) != 0) {
pam_end (pamh, PAM_ABORT); pam_end (pamh, PAM_ABORT);
exit (1); exit (1);
} }
@ -769,20 +808,20 @@ int main (int argc, char **argv)
* and much more, depending on the configured modules * and much more, depending on the configured modules
*/ */
ret = pam_setcred (pamh, PAM_ESTABLISH_CRED); ret = pam_setcred (pamh, PAM_ESTABLISH_CRED);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, ret))); SYSLOG ((LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret)); fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
pam_end (pamh, ret); (void) pam_end (pamh, ret);
exit (1); exit (1);
} }
ret = pam_open_session (pamh, 0); ret = pam_open_session (pamh, 0);
if (ret != PAM_SUCCESS) { if (PAM_SUCCESS != ret) {
SYSLOG ((LOG_ERR, "pam_open_session: %s", SYSLOG ((LOG_ERR, "pam_open_session: %s",
pam_strerror (pamh, ret))); pam_strerror (pamh, ret)));
fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret)); fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
pam_setcred (pamh, PAM_DELETE_CRED); pam_setcred (pamh, PAM_DELETE_CRED);
pam_end (pamh, ret); (void) pam_end (pamh, ret);
exit (1); exit (1);
} }
@ -795,8 +834,8 @@ int main (int argc, char **argv)
/* update environment with all pam set variables */ /* update environment with all pam set variables */
envcp = pam_getenvlist (pamh); envcp = pam_getenvlist (pamh);
if (envcp) { if (NULL != envcp) {
while (*envcp) { while (NULL != *envcp) {
addenv (*envcp, NULL); addenv (*envcp, NULL);
envcp++; envcp++;
} }
@ -804,21 +843,23 @@ int main (int argc, char **argv)
} }
/* become the new user */ /* become the new user */
if (change_uid (&pwent)) { if (change_uid (&pwent) != 0) {
pam_close_session (pamh, 0); pam_close_session (pamh, 0);
pam_setcred (pamh, PAM_DELETE_CRED); pam_setcred (pamh, PAM_DELETE_CRED);
pam_end (pamh, PAM_ABORT); (void) pam_end (pamh, PAM_ABORT);
exit (1); exit (1);
} }
#else /* !USE_PAM */ #else /* !USE_PAM */
environ = newenvp; /* make new environment active */ environ = newenvp; /* make new environment active */
/* no limits if su from root (unless su must fake login's behavior) */ /* no limits if su from root (unless su must fake login's behavior) */
if (!amroot || fakelogin) if (!amroot || fakelogin) {
setup_limits (&pwent); setup_limits (&pwent);
}
if (setup_uid_gid (&pwent, is_console)) if (setup_uid_gid (&pwent, is_console) != 0) {
exit (1); exit (1);
}
#endif /* !USE_PAM */ #endif /* !USE_PAM */
if (change_environment) { if (change_environment) {
@ -850,20 +891,22 @@ int main (int argc, char **argv)
char *arg0; char *arg0;
cp = getdef_str ("SU_NAME"); cp = getdef_str ("SU_NAME");
if (!cp) if (NULL == cp) {
cp = Basename (shellstr); cp = Basename (shellstr);
}
arg0 = xmalloc (strlen (cp) + 2); arg0 = xmalloc (strlen (cp) + 2);
arg0[0] = '-'; arg0[0] = '-';
strcpy (arg0 + 1, cp); strcpy (arg0 + 1, cp);
cp = arg0; cp = arg0;
} else } else {
cp = Basename (shellstr); cp = Basename (shellstr);
}
if (!doshell) { if (!doshell) {
/* Position argv to the remaining arguments */ /* Position argv to the remaining arguments */
argv += optind; argv += optind;
if (command) { if (NULL != command) {
argv -= 2; argv -= 2;
argv[0] = "-c"; argv[0] = "-c";
argv[1] = command; argv[1] = command;
@ -879,17 +922,18 @@ int main (int argc, char **argv)
(void) fputs (_("No shell\n"), stderr); (void) fputs (_("No shell\n"), stderr);
SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr)); SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
closelog (); closelog ();
exit (err == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC); exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
#else #else
run_shell (shellstr, &argv[-1], 0, environ); /* no return */ run_shell (shellstr, &argv[-1], false, environ); /* no return */
#endif #endif
} }
#ifndef USE_PAM #ifndef USE_PAM
err = shell (shellstr, cp, environ); err = shell (shellstr, cp, environ);
exit (err == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC); exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
#else #else
run_shell (shellstr, &cp, 1, environ); run_shell (shellstr, &cp, true, environ);
#endif #endif
/* NOT REACHED */ /* NOT REACHED */
exit (1); exit (1);
} }