2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2011, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* Some parts substantially derived from an ancestor of:
34 su for GNU. Run a shell with substitute user and group IDs.
36 Copyright (C) 1992-2003 Free Software Foundation, Inc.
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2, or (at your option)
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 51 Franklin Street, Fifth Floor,
51 Boston, MA 02110-1301, USA. */
63 #include <sys/types.h>
64 #include "prototypes.h"
72 #include "exitcodes.h"
75 * Assorted #defines to control su's behavior
82 /* not needed by sulog.c anymore */
83 static char name[BUFSIZ];
84 static char oldname[BUFSIZ];
86 /* If nonzero, change some environment vars to indicate the user su'd to. */
87 static bool change_environment;
90 static pam_handle_t *pamh = NULL;
91 static int caught = 0;
92 /* PID of the child, in case it needs to be killed */
93 static pid_t pid_child = 0;
96 extern struct passwd pwent;
99 * External identifiers
102 extern char **newenvp;
103 extern char **environ;
104 extern size_t newenvc;
106 /* local function prototypes */
108 static void execve_shell (const char *shellstr,
112 static RETSIGTYPE kill_child (int unused(s));
114 static RETSIGTYPE die (int);
115 static bool iswheel (const char *);
116 #endif /* !USE_PAM */
120 * die - set or reset termio modes.
122 * die() is called before processing begins. signal() is then called
123 * with die() as the signal handler. If signal later calls die() with a
124 * signal number, the terminal modes are then reset.
126 static RETSIGTYPE die (int killed)
142 static bool iswheel (const char *username)
146 grp = getgrnam ("wheel"); /* !USE_PAM, no need for xgetgrnam */
148 || (NULL == grp->gr_mem)) {
151 return is_on_list (grp->gr_mem, username);
154 static RETSIGTYPE kill_child (int unused(s))
156 if (0 != pid_child) {
157 (void) kill (pid_child, SIGKILL);
158 (void) fputs (_(" ...killed.\n"), stderr);
160 (void) fputs (_(" ...waiting for child to terminate.\n"),
167 /* borrowed from GNU sh-utils' "su.c" */
168 static bool restricted_shell (const char *shellstr)
173 while ((line = getusershell ()) != NULL) {
174 if (('#' != *line) && (strcmp (line, shellstr) == 0)) {
183 static void su_failure (const char *tty)
185 sulog (tty, false, oldname, name); /* log failed attempt */
187 if (getdef_bool ("SYSLOG_SU_ENAB")) {
188 SYSLOG (((0 != pwent.pw_uid) ? LOG_INFO : LOG_NOTICE,
190 ('\0' != oldname[0]) ? oldname : "???",
191 ('\0' != name[0]) ? name : "???"));
199 * execve_shell - Execute a shell with execve, or interpret it with
202 static void execve_shell (const char *shellstr,
207 (void) execve (shellstr, (char **) args, envp);
210 if (access (shellstr, R_OK|X_OK) == 0) {
212 * Assume this is a shell script (with no shebang).
213 * Interpret it with /bin/sh
217 while (NULL != args[n_args]) {
220 targs = (char **) xmalloc ((n_args + 3) * sizeof (args[0]));
223 targs[2] = xstrdup (shellstr);
224 targs[n_args+2] = NULL;
225 while (1 != n_args) {
226 targs[n_args+1] = args[n_args - 1];
230 (void) execve (SHELL, targs, envp);
237 /* Signal handler for parent process later */
238 static void catch_signals (int sig)
243 /* This I ripped out of su.c from sh-utils after the Mandrake pam patch
244 * have been applied. Some work was needed to get it integrated into
247 static void run_shell (const char *shellstr, char *args[], bool doshell,
256 if (child == 0) { /* child shell */
258 * PAM_DATA_SILENT is not supported by some modules, and
259 * there is no strong need to clean up the process space's
260 * memory since we will either call exec or exit.
261 pam_end (pamh, PAM_SUCCESS | PAM_DATA_SILENT);
265 (void) shell (shellstr, (char *) args[0], envp);
267 /* There is no need for a controlling terminal.
268 * This avoids the callee to inject commands on
269 * the caller's tty. */
272 execve_shell (shellstr, (char **) args, envp);
275 exit (errno == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
276 } else if ((pid_t)-1 == child) {
277 (void) fprintf (stderr,
278 _("%s: Cannot fork user shell\n"),
280 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
286 sigfillset (&ourset);
287 if (sigprocmask (SIG_BLOCK, &ourset, NULL) != 0) {
288 (void) fprintf (stderr,
289 _("%s: signal malfunction\n"),
294 struct sigaction action;
296 action.sa_handler = catch_signals;
297 sigemptyset (&action.sa_mask);
299 sigemptyset (&ourset);
301 if ( (sigaddset (&ourset, SIGTERM) != 0)
302 || (sigaddset (&ourset, SIGALRM) != 0)
303 || (sigaction (SIGTERM, &action, NULL) != 0)
304 || ( !doshell /* handle SIGINT (Ctrl-C), SIGQUIT
305 * (Ctrl-\), and SIGTSTP (Ctrl-Z)
306 * since the child does not control
309 && ( (sigaddset (&ourset, SIGINT) != 0)
310 || (sigaddset (&ourset, SIGQUIT) != 0)
311 || (sigaddset (&ourset, SIGTSTP) != 0)
312 || (sigaction (SIGINT, &action, NULL) != 0)
313 || (sigaction (SIGQUIT, &action, NULL) != 0))
314 || (sigaction (SIGTSTP, &action, NULL) != 0))
315 || (sigprocmask (SIG_UNBLOCK, &ourset, NULL) != 0)
318 _("%s: signal masking malfunction\n"),
331 pid = waitpid (-1, &status, WUNTRACED);
333 /* When interrupted by signal, the signal will be
334 * forwarded to the child, and termination will be
337 if ( ((pid_t)-1 == pid)
339 && (SIGTSTP == caught)) {
340 /* Except for SIGTSTP, which request to
342 * We will SIGSTOP ourself on the next
345 kill (child, SIGSTOP);
347 } else if ( ((pid_t)-1 != pid)
348 && (0 != WIFSTOPPED (status))) {
349 /* The child (shell) was suspended.
351 kill (getpid (), SIGSTOP);
352 /* wake child when resumed */
360 (void) fputs ("\n", stderr);
361 (void) fputs (_("Session terminated, terminating shell..."),
363 (void) kill (child, caught);
366 ret = pam_close_session (pamh, 0);
367 if (PAM_SUCCESS != ret) {
368 SYSLOG ((LOG_ERR, "pam_close_session: %s",
369 pam_strerror (pamh, ret)));
370 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
371 (void) pam_end (pamh, ret);
375 ret = pam_end (pamh, PAM_SUCCESS);
378 (void) signal (SIGALRM, kill_child);
381 (void) wait (&status);
382 (void) fputs (_(" ...terminated.\n"), stderr);
385 exit ((0 != WIFEXITED (status)) ? WEXITSTATUS (status)
386 : WTERMSIG (status) + 128);
391 * usage - print command line syntax and exit
393 static void usage (int status)
395 fputs (_("Usage: su [options] [LOGIN]\n"
398 " -c, --command COMMAND pass COMMAND to the invoked shell\n"
399 " -h, --help display this help message and exit\n"
400 " -, -l, --login make the shell a login shell\n"
402 " --preserve-environment do not reset environment variables, and\n"
403 " keep the same shell\n"
404 " -s, --shell SHELL use SHELL instead of the default in passwd\n"
405 "\n"), (E_SUCCESS != status) ? stderr : stdout);
410 * su - switch user id
412 * su changes the user's ids to the values for the specified user. if
413 * no new user name is specified, "root" or UID 0 is used by default.
415 * Any additional arguments are passed to the user's shell. In
416 * particular, the argument "-c" will cause the next argument to be
417 * interpreted as a command by the common shell programs.
419 int main (int argc, char **argv)
422 const char *tty = NULL; /* Name of tty SU is run from */
423 bool doshell = false;
424 bool fakelogin = false;
427 struct passwd *pw = NULL;
428 char **envp = environ;
429 char *shellstr = NULL;
430 char *command = NULL;
438 RETSIGTYPE (*oldsig) (int);
441 struct spwd *spwd = 0;
446 #endif /* !USE_PAM */
448 (void) setlocale (LC_ALL, "");
449 (void) bindtextdomain (PACKAGE, LOCALEDIR);
450 (void) textdomain (PACKAGE);
452 change_environment = true;
455 * Get the program name. The program name is used as a prefix to
456 * most error messages.
458 Prog = Basename (argv[0]);
463 * Process the command line arguments.
468 * Parse the command line options.
470 int option_index = 0;
472 static struct option long_options[] = {
473 {"command", required_argument, NULL, 'c'},
474 {"help", no_argument, NULL, 'h'},
475 {"login", no_argument, NULL, 'l'},
476 {"preserve-environment", no_argument, NULL, 'p'},
477 {"shell", required_argument, NULL, 's'},
478 {NULL, 0, NULL, '\0'}
482 getopt_long (argc, argv, "c:hlmps:", long_options,
483 &option_index)) != -1) {
496 /* This will only have an effect if the target
497 * user do not have a restricted shell, or if
498 * su is called by root.
500 change_environment = false;
506 usage (E_USAGE); /* NOT REACHED */
510 if ((optind < argc) && (strcmp (argv[optind], "-") == 0)) {
514 && (strcmp (argv[optind], "--") == 0)) {
523 amroot = (my_uid == 0);
526 * Get the tty name. Entries will be logged indicating that the user
527 * tried to change to the named new user from the current terminal.
530 if ((isatty (0) != 0) && (NULL != tty)) {
532 is_console = console (tty);
536 * Be more paranoid, like su from SimplePAMApps. --marekm
540 _("%s: must be run from a terminal\n"),
548 * The next argument must be either a user ID, or some flag to a
549 * subshell. Pretty sticky since you can't have an argument which
550 * doesn't start with a "-" unless you specify the new user name.
551 * Any remaining arguments will be passed to the user's login shell.
553 if ((optind < argc) && ('-' != argv[optind][0])) {
554 STRFCPY (name, argv[optind++]); /* use this login id */
555 if ((optind < argc) && (strcmp (argv[optind], "--") == 0)) {
559 if ('\0' == name[0]) { /* use default user */
560 struct passwd *root_pw = getpwnam ("root");
561 if ((NULL != root_pw) && (0 == root_pw->pw_uid)) {
562 (void) strcpy (name, "root");
564 root_pw = getpwuid (0);
565 if (NULL == root_pw) {
566 SYSLOG ((LOG_CRIT, "There is no UID 0 user."));
569 (void) strcpy (name, root_pw->pw_name);
573 doshell = (argc == optind); /* any arguments remaining? */
574 if (NULL != command) {
579 * Get the user's real name. The current UID is used to determine
580 * who has executed su. That user ID must exist.
582 pw = get_my_pwent ();
585 _("%s: Cannot determine your user name.\n"),
587 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
588 (unsigned long) my_uid));
591 STRFCPY (oldname, pw->pw_name);
596 * Sort out the password of user calling su, in case needed later
599 spwd = getspnam (oldname); /* !USE_PAM, no need for xgetspnam */
601 pw->pw_passwd = spwd->sp_pwdp;
603 oldpass = xstrdup (pw->pw_passwd);
604 #endif /* SU_ACCESS */
607 ret = pam_start ("su", name, &conv, &pamh);
608 if (PAM_SUCCESS != ret) {
609 SYSLOG ((LOG_ERR, "pam_start: error %d", ret);
611 _("%s: pam_start: error %d\n"),
616 ret = pam_set_item (pamh, PAM_TTY, (const void *) tty);
617 if (PAM_SUCCESS == ret) {
618 ret = pam_set_item (pamh, PAM_RUSER, (const void *) oldname);
620 if (PAM_SUCCESS != ret) {
621 SYSLOG ((LOG_ERR, "pam_set_item: %s",
622 pam_strerror (pamh, ret)));
623 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
631 * This is the common point for validating a user whose name is
632 * known. It will be reached either by normal processing, or if the
633 * user is to be logged into a subsystem root.
635 * The password file entries for the user is gotten and the account
638 pw = xgetpwnam (name);
640 (void) fprintf (stderr, _("Unknown id: %s\n"), name);
646 if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
647 spwd = getspnam (name); /* !USE_PAM, no need for xgetspnam */
649 pw->pw_passwd = spwd->sp_pwdp;
652 #endif /* !USE_PAM */
657 * BSD systems only allow "wheel" to SU to root. USG systems don't,
658 * so we make this a configurable option.
661 /* The original Shadow 3.3.2 did this differently. Do it like BSD:
663 * - check for UID 0 instead of name "root" - there are systems with
664 * several root accounts under different names,
666 * - check the contents of /etc/group instead of the current group
667 * set (you must be listed as a member, GID 0 is not sufficient).
669 * In addition to this traditional feature, we now have complete su
670 * access control (allow, deny, no password, own password). Thanks
671 * to Chris Evans <lady0110@sable.ox.ac.uk>.
675 if ( (0 == pwent.pw_uid)
676 && getdef_bool ("SU_WHEEL_ONLY")
677 && !iswheel (oldname)) {
679 _("You are not authorized to su %s\n"),
684 switch (check_su_auth (oldname, name)) {
685 case 0: /* normal su, require target user's password */
687 case 1: /* require no password */
688 pwent.pw_passwd = ""; /* XXX warning: const */
690 case 2: /* require own password */
691 puts (_("(Enter your own password)"));
692 pwent.pw_passwd = oldpass;
694 default: /* access denied (-1) or unexpected value */
696 _("You are not authorized to su %s\n"),
700 #endif /* SU_ACCESS */
702 #endif /* !USE_PAM */
704 (void) signal (SIGINT, SIG_IGN);
705 (void) signal (SIGQUIT, SIG_IGN);
707 ret = pam_authenticate (pamh, 0);
708 if (PAM_SUCCESS != ret) {
709 SYSLOG ((LOG_ERR, "pam_authenticate: %s",
710 pam_strerror (pamh, ret)));
711 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
712 (void) pam_end (pamh, ret);
716 ret = pam_acct_mgmt (pamh, 0);
717 if (PAM_SUCCESS != ret) {
720 _("%s: %s\n(Ignored)\n"),
721 Prog, pam_strerror (pamh, ret));
722 } else if (PAM_NEW_AUTHTOK_REQD == ret) {
723 ret = pam_chauthtok (pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
724 if (PAM_SUCCESS != ret) {
725 SYSLOG ((LOG_ERR, "pam_chauthtok: %s",
726 pam_strerror (pamh, ret)));
729 Prog, pam_strerror (pamh, ret));
730 (void) pam_end (pamh, ret);
734 SYSLOG ((LOG_ERR, "pam_acct_mgmt: %s",
735 pam_strerror (pamh, ret)));
738 Prog, pam_strerror (pamh, ret));
739 (void) pam_end (pamh, ret);
745 * Set up a signal handler in case the user types QUIT.
748 oldsig = signal (SIGQUIT, die);
751 * See if the system defined authentication method is being used.
752 * The first character of an administrator defined method is an '@'
756 && (pw_auth (pwent.pw_passwd, name, PW_SU, (char *) 0) != 0)) {
757 SYSLOG (((pwent.pw_uid != 0)? LOG_NOTICE : LOG_WARN,
758 "Authentication failed for %s", name));
759 fprintf(stderr, _("%s: Authentication failure\n"), Prog);
762 (void) signal (SIGQUIT, oldsig);
765 * Check to see if the account is expired. root gets to ignore any
766 * expired accounts, but normal users can't become a user with an
769 if ((!amroot) && (NULL != spwd)) {
770 (void) expire (&pwent, spwd);
774 * Check to see if the account permits "su". root gets to ignore any
775 * restricted accounts, but normal users can't become a user if
776 * there is a "SU" entry in the /etc/porttime file denying access to
780 if (!isttytime (pwent.pw_name, "SU", time ((time_t *) 0))) {
781 SYSLOG (((0 != pwent.pw_uid) ? LOG_WARN : LOG_CRIT,
782 "SU by %s to restricted account %s",
785 _("%s: You are not authorized to su at that time\n"),
790 #endif /* !USE_PAM */
792 (void) signal (SIGINT, SIG_DFL);
793 (void) signal (SIGQUIT, SIG_DFL);
796 * Even if --shell is specified, the subsystem login test is based on
797 * the shell specified in /etc/passwd (not the one specified with
798 * --shell, which will be the one executed in the chroot later).
800 if ('*' == pwent.pw_shell[0]) { /* subsystem root required */
801 subsystem (&pwent); /* change to the subsystem root */
802 endpwent (); /* close the old password databases */
804 goto top; /* authenticate in the subsystem */
807 /* If su is not called by root, and the target user has a restricted
808 * shell, the environment must be changed.
810 change_environment |= (restricted_shell (pwent.pw_shell) && !amroot);
813 * If a new login is being set up, the old environment will be
814 * ignored and a new one created later on.
816 if (change_environment && fakelogin) {
818 * The terminal type will be left alone if it is present in
819 * the environment already.
821 cp = getenv ("TERM");
827 * For some terminals COLORTERM seems to be the only way
828 * for checking for that specific terminal. For instance,
829 * gnome-terminal sets its TERM as "xterm" but its
830 * COLORTERM as "gnome-terminal". The COLORTERM variable
831 * is also of use when running GNU screen since it sets
832 * TERM to "screen" but doesn't touch COLORTERM.
834 cp = getenv ("COLORTERM");
836 addenv ("COLORTERM", cp);
840 cp = getdef_str ("ENV_TZ");
842 addenv (('/' == *cp) ? tz (cp) : cp, NULL);
846 * The clock frequency will be reset to the login value if required
848 cp = getdef_str ("ENV_HZ");
850 addenv (cp, NULL); /* set the default $HZ, if one */
852 #endif /* !USE_PAM */
855 * Also leave DISPLAY and XAUTHORITY if present, else
856 * pam_xauth will not work.
858 cp = getenv ("DISPLAY");
860 addenv ("DISPLAY", cp);
862 cp = getenv ("XAUTHORITY");
864 addenv ("XAUTHORITY", cp);
867 while (NULL != *envp) {
868 addenv (*envp, NULL);
873 /* If the user do not want to change the environment,
874 * use the current SHELL.
875 * (unless another shell is required by the command line)
877 if ((NULL == shellstr) && !change_environment) {
878 shellstr = getenv ("SHELL");
880 /* For users with non null UID, if this user has a restricted
881 * shell, the shell must be the one specified in /etc/passwd
883 if ( (NULL != shellstr)
885 && restricted_shell (pwent.pw_shell)) {
888 /* If the shell is not set at this time, use the shell specified
891 if (NULL == shellstr) {
892 shellstr = (char *) strdup (pwent.pw_shell);
896 * Set the default shell.
898 if ((NULL == shellstr) || ('\0' == shellstr[0])) {
902 cp = getdef_str ((pwent.pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
904 addenv ((pwent.pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
905 } else if (strchr (cp, '=') != NULL) {
911 if (getenv ("IFS") != NULL) { /* don't export user IFS ... */
912 addenv ("IFS= \t\n", NULL); /* ... instead, set a safe IFS */
915 sulog (tty, true, oldname, name); /* save SU information */
919 if (getdef_bool ("SYSLOG_SU_ENAB")) {
920 SYSLOG ((LOG_INFO, "+ %s %s:%s", tty,
921 ('\0' != oldname[0]) ? oldname : "???",
922 ('\0' != name[0]) ? name : "???"));
927 /* set primary group id and supplementary groups */
928 if (setup_groups (&pwent) != 0) {
929 pam_end (pamh, PAM_ABORT);
934 * pam_setcred() may do things like resource limits, console groups,
935 * and much more, depending on the configured modules
937 ret = pam_setcred (pamh, PAM_ESTABLISH_CRED);
938 if (PAM_SUCCESS != ret) {
939 SYSLOG ((LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, ret)));
940 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
941 (void) pam_end (pamh, ret);
945 ret = pam_open_session (pamh, 0);
946 if (PAM_SUCCESS != ret) {
947 SYSLOG ((LOG_ERR, "pam_open_session: %s",
948 pam_strerror (pamh, ret)));
949 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
950 pam_setcred (pamh, PAM_DELETE_CRED);
951 (void) pam_end (pamh, ret);
955 /* we need to setup the environment *after* pam_open_session(),
956 * else the UID is changed before stuff like pam_xauth could
957 * run, and we cannot access /etc/shadow and co
959 environ = newenvp; /* make new environment active */
961 if (change_environment) {
962 /* update environment with all pam set variables */
963 envcp = pam_getenvlist (pamh);
965 while (NULL != *envcp) {
966 addenv (*envcp, NULL);
972 /* become the new user */
973 if (change_uid (&pwent) != 0) {
974 pam_close_session (pamh, 0);
975 pam_setcred (pamh, PAM_DELETE_CRED);
976 (void) pam_end (pamh, PAM_ABORT);
980 environ = newenvp; /* make new environment active */
982 /* no limits if su from root (unless su must fake login's behavior) */
983 if (!amroot || fakelogin) {
984 setup_limits (&pwent);
987 if (setup_uid_gid (&pwent, is_console) != 0) {
990 #endif /* !USE_PAM */
992 if (change_environment) {
994 pwent.pw_shell = shellstr;
997 addenv ("HOME", pwent.pw_dir);
998 addenv ("USER", pwent.pw_name);
999 addenv ("LOGNAME", pwent.pw_name);
1000 addenv ("SHELL", shellstr);
1005 * This is a workaround for Linux libc bug/feature (?) - the
1006 * /dev/log file descriptor is open without the close-on-exec flag
1007 * and used to be passed to the new shell. There is "fcntl(LogFile,
1008 * F_SETFD, 1)" in libc/misc/syslog.c, but it is commented out (at
1009 * least in 5.4.33). Why? --marekm
1014 * See if the user has extra arguments on the command line. In that
1015 * case they will be provided to the new user's shell as arguments.
1020 cp = getdef_str ("SU_NAME");
1022 cp = Basename (shellstr);
1025 arg0 = xmalloc (strlen (cp) + 2);
1027 strcpy (arg0 + 1, cp);
1030 cp = Basename (shellstr);
1034 /* Position argv to the remaining arguments */
1036 if (NULL != command) {
1042 * Use the shell and create an argv
1043 * with the rest of the command line included.
1047 execve_shell (shellstr, &argv[-1], environ);
1049 (void) fputs (_("No shell\n"), stderr);
1050 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
1052 exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
1054 run_shell (shellstr, &argv[-1], false, environ); /* no return */
1058 err = shell (shellstr, cp, environ);
1059 exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
1061 run_shell (shellstr, &cp, true, environ);