Initial commit for Tizen
[profile/extras/shadow-utils.git] / src / su.c
1 /*
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 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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.
31  */
32
33 /* Some parts substantially derived from an ancestor of:
34    su for GNU.  Run a shell with substitute user and group IDs.
35
36    Copyright (C) 1992-2003 Free Software Foundation, Inc.
37
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)
41    any later version.
42
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.
47
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.  */
52
53
54 #include <config.h>
55
56 #ident "$Id: su.c 3035 2009-07-22 13:35:57Z nekral-guest $"
57
58 #include <getopt.h>
59 #include <grp.h>
60 #include <pwd.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <sys/types.h>
64 #include "prototypes.h"
65 #include "defines.h"
66 #include "pwauth.h"
67 #include "getdef.h"
68 #ifdef USE_PAM
69 #include "pam_defs.h"
70 #endif                          /* USE_PAM */
71 /*@-exitarg@*/
72 #include "exitcodes.h"
73
74 /*
75  * Assorted #defines to control su's behavior
76  */
77 /*
78  * Global variables
79  */
80 char *Prog;
81
82 /* not needed by sulog.c anymore */
83 static char name[BUFSIZ];
84 static char oldname[BUFSIZ];
85
86 /* If nonzero, change some environment vars to indicate the user su'd to. */
87 static bool change_environment;
88
89 #ifdef USE_PAM
90 static pam_handle_t *pamh = NULL;
91 static bool caught = false;
92 #endif
93
94 extern struct passwd pwent;
95
96 /*
97  * External identifiers
98  */
99
100 extern char **newenvp;
101 extern char **environ;
102 extern size_t newenvc;
103
104 /* local function prototypes */
105
106 #ifndef USE_PAM
107
108 static RETSIGTYPE die (int);
109 static int iswheel (const char *);
110
111 /*
112  * die - set or reset termio modes.
113  *
114  *      die() is called before processing begins. signal() is then called
115  *      with die() as the signal handler. If signal later calls die() with a
116  *      signal number, the terminal modes are then reset.
117  */
118 static RETSIGTYPE die (int killed)
119 {
120         static TERMIO sgtty;
121
122         if (killed)
123                 STTY (0, &sgtty);
124         else
125                 GTTY (0, &sgtty);
126
127         if (killed) {
128                 closelog ();
129                 exit (killed);
130         }
131 }
132
133 static int iswheel (const char *username)
134 {
135         struct group *grp;
136
137         grp = getgrnam ("wheel"); /* !USE_PAM, no need for xgetgrnam */
138         if (   (NULL ==grp)
139             || (NULL == grp->gr_mem)) {
140                 return 0;
141         }
142         return is_on_list (grp->gr_mem, username);
143 }
144 #endif                          /* !USE_PAM */
145
146 /* borrowed from GNU sh-utils' "su.c" */
147 static bool restricted_shell (const char *shellstr)
148 {
149         char *line;
150
151         setusershell ();
152         while ((line = getusershell ()) != NULL) {
153                 if (('#' != *line) && (strcmp (line, shellstr) == 0)) {
154                         endusershell ();
155                         return false;
156                 }
157         }
158         endusershell ();
159         return true;
160 }
161
162 static void su_failure (const char *tty)
163 {
164         sulog (tty, false, oldname, name);      /* log failed attempt */
165 #ifdef USE_SYSLOG
166         if (getdef_bool ("SYSLOG_SU_ENAB")) {
167                 SYSLOG (((0 != pwent.pw_uid) ? LOG_INFO : LOG_NOTICE,
168                          "- %s %s:%s", tty,
169                          ('\0' != oldname[0]) ? oldname : "???",
170                          ('\0' != name[0]) ? name : "???"));
171         }
172         closelog ();
173 #endif
174         exit (1);
175 }
176
177 /*
178  * execve_shell - Execute a shell with execve, or interpret it with
179  * /bin/sh
180  */
181 void execve_shell (const char *shellstr, char *args[], char *const envp[])
182 {
183         int err;
184         (void) execve (shellstr, (char **) args, envp);
185         err = errno;
186
187         if (access (shellstr, R_OK|X_OK) == 0) {
188                 /*
189                  * Assume this is a shell script (with no shebang).
190                  * Interpret it with /bin/sh
191                  */
192                 size_t n_args = 0;
193                 char **targs;
194                 while (NULL != args[n_args]) {
195                         n_args++;
196                 }
197                 targs = (char **) xmalloc ((n_args + 3) * sizeof (args[0]));
198                 targs[0] = "sh";
199                 targs[1] = "-";
200                 targs[2] = xstrdup (shellstr);
201                 targs[n_args+2] = NULL;
202                 while (1 != n_args) {
203                         targs[n_args+1] = args[n_args - 1];
204                         n_args--;
205                 }
206
207                 (void) execve (SHELL, targs, envp);
208         } else {
209                 errno = err;
210         }
211 }
212
213 #ifdef USE_PAM
214 /* Signal handler for parent process later */
215 static void catch_signals (unused int sig)
216 {
217         caught = true;
218 }
219
220 /* This I ripped out of su.c from sh-utils after the Mandrake pam patch
221  * have been applied.  Some work was needed to get it integrated into
222  * su.c from shadow.
223  */
224 static void run_shell (const char *shellstr, char *args[], bool doshell,
225                        char *const envp[])
226 {
227         pid_t child;
228         sigset_t ourset;
229         int status;
230         int ret;
231
232         child = fork ();
233         if (child == 0) {       /* child shell */
234                 /*
235                  * PAM_DATA_SILENT is not supported by some modules, and
236                  * there is no strong need to clean up the process space's
237                  * memory since we will either call exec or exit.
238                 pam_end (pamh, PAM_SUCCESS | PAM_DATA_SILENT);
239                  */
240
241                 if (doshell) {
242                         (void) shell (shellstr, (char *) args[0], envp);
243                 } else {
244                         execve_shell (shellstr, (char **) args, envp);
245                 }
246
247                 exit (errno == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
248         } else if ((pid_t)-1 == child) {
249                 (void) fprintf (stderr, "%s: Cannot fork user shell\n", Prog);
250                 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
251                 closelog ();
252                 exit (1);
253         }
254         /* parent only */
255         sigfillset (&ourset);
256         if (sigprocmask (SIG_BLOCK, &ourset, NULL) != 0) {
257                 (void) fprintf (stderr, "%s: signal malfunction\n", Prog);
258                 caught = true;
259         }
260         if (!caught) {
261                 struct sigaction action;
262
263                 action.sa_handler = catch_signals;
264                 sigemptyset (&action.sa_mask);
265                 action.sa_flags = 0;
266                 sigemptyset (&ourset);
267
268                 if (   (sigaddset (&ourset, SIGTERM) != 0)
269                     || (sigaddset (&ourset, SIGALRM) != 0)
270                     || (sigaction (SIGTERM, &action, NULL) != 0)
271                     || (sigprocmask (SIG_UNBLOCK, &ourset, NULL) != 0)
272                     ) {
273                         fprintf (stderr,
274                                  "%s: signal masking malfunction\n", Prog);
275                         caught = true;
276                 }
277         }
278
279         if (!caught) {
280                 do {
281                         pid_t pid;
282
283                         pid = waitpid (-1, &status, WUNTRACED);
284
285                         if (((pid_t)-1 != pid) && (0 != WIFSTOPPED (status))) {
286                                 /* The child (shell) was suspended.
287                                  * Suspend su. */
288                                 kill (getpid (), WSTOPSIG(status));
289                                 /* wake child when resumed */
290                                 kill (pid, SIGCONT);
291                         }
292                 } while (0 != WIFSTOPPED (status));
293         }
294
295         if (caught) {
296                 fprintf (stderr, "\nSession terminated, killing shell...");
297                 kill (child, SIGTERM);
298         }
299
300         ret = pam_close_session (pamh, 0);
301         if (PAM_SUCCESS != ret) {
302                 SYSLOG ((LOG_ERR, "pam_close_session: %s",
303                          pam_strerror (pamh, ret)));
304                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
305                 (void) pam_end (pamh, ret);
306                 exit (1);
307         }
308
309         ret = pam_end (pamh, PAM_SUCCESS);
310
311         if (caught) {
312                 sleep (2);
313                 kill (child, SIGKILL);
314                 fprintf (stderr, " ...killed.\n");
315                 exit (-1);
316         }
317
318         exit ((0 != WIFEXITED (status)) ? WEXITSTATUS (status)
319                                         : WTERMSIG (status) + 128);
320 }
321 #endif
322
323 /*
324  * usage - print command line syntax and exit
325   */
326 static void usage (void)
327 {
328         fputs (_("Usage: su [options] [LOGIN]\n"
329                  "\n"
330                  "Options:\n"
331                  "  -c, --command COMMAND         pass COMMAND to the invoked shell\n"
332                  "  -h, --help                    display this help message and exit\n"
333                  "  -, -l, --login                make the shell a login shell\n"
334                  "  -m, -p,\n"
335                  "  --preserve-environment        do not reset environment variables, and\n"
336                  "                                keep the same shell\n"
337                  "  -s, --shell SHELL             use SHELL instead of the default in passwd\n"
338                  "\n"), stderr);
339         exit (E_USAGE);
340 }
341
342 /*
343  * su - switch user id
344  *
345  *      su changes the user's ids to the values for the specified user.  if
346  *      no new user name is specified, "root" or UID 0 is used by default.
347  *
348  *      Any additional arguments are passed to the user's shell. In
349  *      particular, the argument "-c" will cause the next argument to be
350  *      interpreted as a command by the common shell programs.
351  */
352 int main (int argc, char **argv)
353 {
354         const char *cp;
355         const char *tty = NULL; /* Name of tty SU is run from        */
356         bool doshell = false;
357         bool fakelogin = false;
358         bool amroot = false;
359         uid_t my_uid;
360         struct passwd *pw = NULL;
361         char **envp = environ;
362         char *shellstr = NULL;
363         char *command = NULL;
364
365 #ifdef USE_PAM
366         char **envcp;
367         int ret;
368 #else                           /* !USE_PAM */
369         int err = 0;
370
371         RETSIGTYPE (*oldsig) (int);
372         int is_console = 0;
373
374         struct spwd *spwd = 0;
375
376 #ifdef SU_ACCESS
377         char *oldpass;
378 #endif
379 #endif                          /* !USE_PAM */
380
381         sanitize_env ();
382
383         (void) setlocale (LC_ALL, "");
384         (void) bindtextdomain (PACKAGE, LOCALEDIR);
385         (void) textdomain (PACKAGE);
386
387         change_environment = true;
388
389         /*
390          * Get the program name. The program name is used as a prefix to
391          * most error messages.
392          */
393         Prog = Basename (argv[0]);
394
395         OPENLOG ("su");
396
397         /*
398          * Process the command line arguments. 
399          */
400
401         {
402                 /*
403                  * Parse the command line options.
404                  */
405                 int option_index = 0;
406                 int c;
407                 static struct option long_options[] = {
408                         {"command", required_argument, NULL, 'c'},
409                         {"help", no_argument, NULL, 'h'},
410                         {"login", no_argument, NULL, 'l'},
411                         {"preserve-environment", no_argument, NULL, 'p'},
412                         {"shell", required_argument, NULL, 's'},
413                         {NULL, 0, NULL, '\0'}
414                 };
415
416                 while ((c =
417                         getopt_long (argc, argv, "c:hlmps:", long_options,
418                                      &option_index)) != -1) {
419                         switch (c) {
420                         case 'c':
421                                 command = optarg;
422                                 break;
423                         case 'h':
424                                 usage ();
425                                 break;
426                         case 'l':
427                                 fakelogin = true;
428                                 break;
429                         case 'm':
430                         case 'p':
431                                 /* This will only have an effect if the target
432                                  * user do not have a restricted shell, or if
433                                  * su is called by root.
434                                  */
435                                 change_environment = false;
436                                 break;
437                         case 's':
438                                 shellstr = optarg;
439                                 break;
440                         default:
441                                 usage ();       /* NOT REACHED */
442                         }
443                 }
444
445                 if ((optind < argc) && (strcmp (argv[optind], "-") == 0)) {
446                         fakelogin = true;
447                         optind++;
448                         if (   (optind < argc)
449                             && (strcmp (argv[optind], "--") == 0)) {
450                                 optind++;
451                         }
452                 }
453         }
454
455         initenv ();
456
457         my_uid = getuid ();
458         amroot = (my_uid == 0);
459
460         /*
461          * Get the tty name. Entries will be logged indicating that the user
462          * tried to change to the named new user from the current terminal.
463          */
464         tty = ttyname (0);
465         if ((isatty (0) != 0) && (NULL != tty)) {
466 #ifndef USE_PAM
467                 is_console = console (tty);
468 #endif
469         } else {
470                 /*
471                  * Be more paranoid, like su from SimplePAMApps.  --marekm
472                  */
473                 if (!amroot) {
474                         fprintf (stderr,
475                                  _("%s: must be run from a terminal\n"), Prog);
476                         exit (1);
477                 }
478                 tty = "???";
479         }
480
481         /*
482          * The next argument must be either a user ID, or some flag to a
483          * subshell. Pretty sticky since you can't have an argument which
484          * doesn't start with a "-" unless you specify the new user name.
485          * Any remaining arguments will be passed to the user's login shell.
486          */
487         if ((optind < argc) && ('-' != argv[optind][0])) {
488                 STRFCPY (name, argv[optind++]); /* use this login id */
489                 if ((optind < argc) && (strcmp (argv[optind], "--") == 0)) {
490                         optind++;
491                 }
492         }
493         if ('\0' == name[0]) {          /* use default user */
494                 struct passwd *root_pw = getpwnam ("root");
495                 if ((NULL != root_pw) && (0 == root_pw->pw_uid)) {
496                         (void) strcpy (name, "root");
497                 } else {
498                         root_pw = getpwuid (0);
499                         if (NULL == root_pw) {
500                                 SYSLOG ((LOG_CRIT, "There is no UID 0 user."));
501                                 su_failure (tty);
502                         }
503                         (void) strcpy (name, root_pw->pw_name);
504                 }
505         }
506
507         doshell = (argc == optind);     /* any arguments remaining? */
508         if (NULL != command) {
509                 doshell = false;
510         }
511
512         /*
513          * Get the user's real name. The current UID is used to determine
514          * who has executed su. That user ID must exist.
515          */
516         pw = get_my_pwent ();
517         if (NULL == pw) {
518                 fprintf (stderr, _("%s: Cannot determine your user name.\n"),
519                          Prog);
520                 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
521                          (unsigned long) my_uid));
522                 su_failure (tty);
523         }
524         STRFCPY (oldname, pw->pw_name);
525
526 #ifndef USE_PAM
527 #ifdef SU_ACCESS
528         /*
529          * Sort out the password of user calling su, in case needed later
530          * -- chris
531          */
532         spwd = getspnam (oldname); /* !USE_PAM, no need for xgetspnam */
533         if (NULL != spwd) {
534                 pw->pw_passwd = spwd->sp_pwdp;
535         }
536         oldpass = xstrdup (pw->pw_passwd);
537 #endif                          /* SU_ACCESS */
538
539 #else                           /* USE_PAM */
540         ret = pam_start ("su", name, &conv, &pamh);
541         if (PAM_SUCCESS != ret) {
542                 SYSLOG ((LOG_ERR, "pam_start: error %d", ret);
543                         fprintf (stderr, _("%s: pam_start: error %d\n"),
544                                  Prog, ret));
545                 exit (1);
546         }
547
548         ret = pam_set_item (pamh, PAM_TTY, (const void *) tty);
549         if (PAM_SUCCESS == ret) {
550                 ret = pam_set_item (pamh, PAM_RUSER, (const void *) oldname);
551         }
552         if (PAM_SUCCESS != ret) {
553                 SYSLOG ((LOG_ERR, "pam_set_item: %s",
554                          pam_strerror (pamh, ret)));
555                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
556                 pam_end (pamh, ret);
557                 exit (1);
558         }
559 #endif                          /* USE_PAM */
560
561       top:
562         /*
563          * This is the common point for validating a user whose name is
564          * known. It will be reached either by normal processing, or if the
565          * user is to be logged into a subsystem root.
566          *
567          * The password file entries for the user is gotten and the account
568          * validated.
569          */
570         pw = xgetpwnam (name);
571         if (NULL == pw) {
572                 (void) fprintf (stderr, _("Unknown id: %s\n"), name);
573                 closelog ();
574                 exit (1);
575         }
576 #ifndef USE_PAM
577         spwd = NULL;
578         if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
579                 spwd = getspnam (name); /* !USE_PAM, no need for xgetspnam */
580                 if (NULL != spwd) {
581                         pw->pw_passwd = spwd->sp_pwdp;
582                 }
583         }
584 #endif                          /* !USE_PAM */
585         pwent = *pw;
586
587         /* If su is not called by root, and the target user has a restricted
588          * shell, the environment must be changed.
589          */
590         change_environment |= (restricted_shell (pwent.pw_shell) && !amroot);
591
592         /*
593          * If a new login is being set up, the old environment will be
594          * ignored and a new one created later on.
595          * (note: in the case of a subsystem, the shell will be restricted,
596          *        and this won't be executed on the first pass)
597          */
598         if (change_environment && fakelogin) {
599                 /*
600                  * The terminal type will be left alone if it is present in
601                  * the environment already.
602                  */
603                 cp = getenv ("TERM");
604                 if (NULL != cp) {
605                         addenv ("TERM", cp);
606                 }
607
608                 /*
609                  * For some terminals COLORTERM seems to be the only way
610                  * for checking for that specific terminal. For instance,
611                  * gnome-terminal sets its TERM as "xterm" but its
612                  * COLORTERM as "gnome-terminal". The COLORTERM variable
613                  * is also of use when running GNU screen since it sets
614                  * TERM to "screen" but doesn't touch COLORTERM.
615                  */
616                 cp = getenv ("COLORTERM");
617                 if (NULL != cp) {
618                         addenv ("COLORTERM", cp);
619                 }
620
621 #ifndef USE_PAM
622                 cp = getdef_str ("ENV_TZ");
623                 if (NULL != cp) {
624                         addenv (('/' == *cp) ? tz (cp) : cp, NULL);
625                 }
626
627                 /*
628                  * The clock frequency will be reset to the login value if required
629                  */
630                 cp = getdef_str ("ENV_HZ");
631                 if (NULL != cp) {
632                         addenv (cp, NULL);      /* set the default $HZ, if one */
633                 }
634 #endif                          /* !USE_PAM */
635
636                 /*
637                  * Also leave DISPLAY and XAUTHORITY if present, else
638                  * pam_xauth will not work.
639                  */
640                 cp = getenv ("DISPLAY");
641                 if (NULL != cp) {
642                         addenv ("DISPLAY", cp);
643                 }
644                 cp = getenv ("XAUTHORITY");
645                 if (NULL != cp) {
646                         addenv ("XAUTHORITY", cp);
647                 }
648         } else {
649                 while (NULL != *envp) {
650                         addenv (*envp, NULL);
651                         envp++;
652                 }
653         }
654
655 #ifndef USE_PAM
656         /*
657          * BSD systems only allow "wheel" to SU to root. USG systems don't,
658          * so we make this a configurable option.
659          */
660
661         /* The original Shadow 3.3.2 did this differently. Do it like BSD:
662          *
663          * - check for UID 0 instead of name "root" - there are systems with
664          *   several root accounts under different names,
665          *
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).
668          *
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>.
672          */
673
674         if (!amroot) {
675                 if (   (0 == pwent.pw_uid)
676                     && getdef_bool ("SU_WHEEL_ONLY")
677                     && !iswheel (oldname)) {
678                         fprintf (stderr,
679                                  _("You are not authorized to su %s\n"), name);
680                         exit (1);
681                 }
682 #ifdef SU_ACCESS
683                 switch (check_su_auth (oldname, name)) {
684                 case 0: /* normal su, require target user's password */
685                         break;
686                 case 1: /* require no password */
687                         pwent.pw_passwd = "";   /* XXX warning: const */
688                         break;
689                 case 2: /* require own password */
690                         puts (_("(Enter your own password)"));
691                         pwent.pw_passwd = oldpass;
692                         break;
693                 default:        /* access denied (-1) or unexpected value */
694                         fprintf (stderr,
695                                  _("You are not authorized to su %s\n"), name);
696                         exit (1);
697                 }
698 #endif                          /* SU_ACCESS */
699         }
700 #endif                          /* !USE_PAM */
701
702         /* If the user do not want to change the environment,
703          * use the current SHELL.
704          * (unless another shell is required by the command line)
705          */
706         if ((NULL == shellstr) && !change_environment) {
707                 shellstr = getenv ("SHELL");
708         }
709         /* For users with non null UID, if this user has a restricted
710          * shell, the shell must be the one specified in /etc/passwd
711          */
712         if (   (NULL != shellstr)
713             && !amroot
714             && restricted_shell (pwent.pw_shell)) {
715                 shellstr = NULL;
716         }
717         /* If the shell is not set at this time, use the shell specified
718          * in /etc/passwd.
719          */
720         if (NULL == shellstr) {
721                 shellstr = (char *) strdup (pwent.pw_shell);
722         }
723
724         /*
725          * Set the default shell.
726          */
727         if ((NULL == shellstr) || ('\0' == shellstr[0])) {
728                 shellstr = SHELL;
729         }
730
731         (void) signal (SIGINT, SIG_IGN);
732         (void) signal (SIGQUIT, SIG_IGN);
733 #ifdef USE_PAM
734         ret = pam_authenticate (pamh, 0);
735         if (PAM_SUCCESS != ret) {
736                 SYSLOG ((LOG_ERR, "pam_authenticate: %s",
737                          pam_strerror (pamh, ret)));
738                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
739                 (void) pam_end (pamh, ret);
740                 su_failure (tty);
741         }
742
743         ret = pam_acct_mgmt (pamh, 0);
744         if (PAM_SUCCESS != ret) {
745                 if (amroot) {
746                         fprintf (stderr, _("%s: %s\n(Ignored)\n"), Prog,
747                                  pam_strerror (pamh, ret));
748                 } else if (PAM_NEW_AUTHTOK_REQD == ret) {
749                         ret = pam_chauthtok (pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
750                         if (PAM_SUCCESS != ret) {
751                                 SYSLOG ((LOG_ERR, "pam_chauthtok: %s",
752                                          pam_strerror (pamh, ret)));
753                                 fprintf (stderr, _("%s: %s\n"), Prog,
754                                          pam_strerror (pamh, ret));
755                                 (void) pam_end (pamh, ret);
756                                 su_failure (tty);
757                         }
758                 } else {
759                         SYSLOG ((LOG_ERR, "pam_acct_mgmt: %s",
760                                  pam_strerror (pamh, ret)));
761                         fprintf (stderr, _("%s: %s\n"), Prog,
762                                  pam_strerror (pamh, ret));
763                         (void) pam_end (pamh, ret);
764                         su_failure (tty);
765                 }
766         }
767 #else                           /* !USE_PAM */
768         /*
769          * Set up a signal handler in case the user types QUIT.
770          */
771         die (0);
772         oldsig = signal (SIGQUIT, die);
773
774         /*
775          * See if the system defined authentication method is being used. 
776          * The first character of an administrator defined method is an '@'
777          * character.
778          */
779         if (!amroot && pw_auth (pwent.pw_passwd, name, PW_SU, (char *) 0)) {
780                 SYSLOG ((pwent.pw_uid ? LOG_NOTICE : LOG_WARN,
781                          "Authentication failed for %s", name));
782                 fprintf(stderr, _("%s: Authentication failure\n"), Prog);
783                 su_failure (tty);
784         }
785         (void) signal (SIGQUIT, oldsig);
786
787         /*
788          * Check to see if the account is expired. root gets to ignore any
789          * expired accounts, but normal users can't become a user with an
790          * expired password.
791          */
792         if (!amroot) {
793                 if (NULL == spwd) {
794                         spwd = pwd_to_spwd (&pwent);
795                 }
796
797                 if (expire (&pwent, spwd)) {
798                         /* !USE_PAM, no need for xgetpwnam */
799                         struct passwd *pwd = getpwnam (name);
800
801                         /* !USE_PAM, no need for xgetspnam */
802                         spwd = getspnam (name);
803                         if (NULL != pwd) {
804                                 pwent = *pwd;
805                         }
806                 }
807         }
808
809         /*
810          * Check to see if the account permits "su". root gets to ignore any
811          * restricted accounts, but normal users can't become a user if
812          * there is a "SU" entry in the /etc/porttime file denying access to
813          * the account.
814          */
815         if (!amroot) {
816                 if (!isttytime (pwent.pw_name, "SU", time ((time_t *) 0))) {
817                         SYSLOG (((0 != pwent.pw_uid) ? LOG_WARN : LOG_CRIT,
818                                  "SU by %s to restricted account %s",
819                                  oldname, name));
820                         fprintf(stderr,
821                                 _("%s: You are not authorized to su at that time\n"), Prog);
822                         su_failure (tty);
823                 }
824         }
825 #endif                          /* !USE_PAM */
826
827         (void) signal (SIGINT, SIG_DFL);
828         (void) signal (SIGQUIT, SIG_DFL);
829
830         cp = getdef_str ((pwent.pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
831         if (NULL == cp) {
832                 addenv ("PATH=/bin:/usr/bin", NULL);
833         } else if (strchr (cp, '=') != NULL) {
834                 addenv (cp, NULL);
835         } else {
836                 addenv ("PATH", cp);
837         }
838
839         if (getenv ("IFS") != NULL) {   /* don't export user IFS ... */
840                 addenv ("IFS= \t\n", NULL);     /* ... instead, set a safe IFS */
841         }
842
843         /*
844          * Even if --shell is specified, the subsystem login test is based on
845          * the shell specified in /etc/passwd (not the one specified with
846          * --shell, which will be the one executed in the chroot later).
847          */
848         if ('*' == pwent.pw_shell[0]) { /* subsystem root required */
849                 pwent.pw_shell++;       /* skip the '*' */
850                 subsystem (&pwent);     /* figure out what to execute */
851                 endpwent ();
852                 endspent ();
853                 goto top;
854         }
855
856         sulog (tty, true, oldname, name);       /* save SU information */
857         endpwent ();
858         endspent ();
859 #ifdef USE_SYSLOG
860         if (getdef_bool ("SYSLOG_SU_ENAB")) {
861                 SYSLOG ((LOG_INFO, "+ %s %s:%s", tty,
862                          ('\0' != oldname[0]) ? oldname : "???",
863                          ('\0' != name[0]) ? name : "???"));
864         }
865 #endif
866
867 #ifdef USE_PAM
868         /* set primary group id and supplementary groups */
869         if (setup_groups (&pwent) != 0) {
870                 pam_end (pamh, PAM_ABORT);
871                 exit (1);
872         }
873
874         /*
875          * pam_setcred() may do things like resource limits, console groups,
876          * and much more, depending on the configured modules
877          */
878         ret = pam_setcred (pamh, PAM_ESTABLISH_CRED);
879         if (PAM_SUCCESS != ret) {
880                 SYSLOG ((LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, ret)));
881                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
882                 (void) pam_end (pamh, ret);
883                 exit (1);
884         }
885
886         ret = pam_open_session (pamh, 0);
887         if (PAM_SUCCESS != ret) {
888                 SYSLOG ((LOG_ERR, "pam_open_session: %s",
889                          pam_strerror (pamh, ret)));
890                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
891                 pam_setcred (pamh, PAM_DELETE_CRED);
892                 (void) pam_end (pamh, ret);
893                 exit (1);
894         }
895
896         if (change_environment) {
897                 /* we need to setup the environment *after* pam_open_session(),
898                  * else the UID is changed before stuff like pam_xauth could
899                  * run, and we cannot access /etc/shadow and co
900                  */
901                 environ = newenvp;      /* make new environment active */
902
903                 /* update environment with all pam set variables */
904                 envcp = pam_getenvlist (pamh);
905                 if (NULL != envcp) {
906                         while (NULL != *envcp) {
907                                 addenv (*envcp, NULL);
908                                 envcp++;
909                         }
910                 }
911         }
912
913         /* become the new user */
914         if (change_uid (&pwent) != 0) {
915                 pam_close_session (pamh, 0);
916                 pam_setcred (pamh, PAM_DELETE_CRED);
917                 (void) pam_end (pamh, PAM_ABORT);
918                 exit (1);
919         }
920 #else                           /* !USE_PAM */
921         environ = newenvp;      /* make new environment active */
922
923         /* no limits if su from root (unless su must fake login's behavior) */
924         if (!amroot || fakelogin) {
925                 setup_limits (&pwent);
926         }
927
928         if (setup_uid_gid (&pwent, is_console) != 0) {
929                 exit (1);
930         }
931 #endif                          /* !USE_PAM */
932
933         if (change_environment) {
934                 if (fakelogin) {
935                         pwent.pw_shell = shellstr;
936                         setup_env (&pwent);
937                 } else {
938                         addenv ("HOME", pwent.pw_dir);
939                         addenv ("USER", pwent.pw_name);
940                         addenv ("LOGNAME", pwent.pw_name);
941                         addenv ("SHELL", shellstr);
942                 }
943         }
944
945         /*
946          * This is a workaround for Linux libc bug/feature (?) - the
947          * /dev/log file descriptor is open without the close-on-exec flag
948          * and used to be passed to the new shell. There is "fcntl(LogFile,
949          * F_SETFD, 1)" in libc/misc/syslog.c, but it is commented out (at
950          * least in 5.4.33). Why?  --marekm
951          */
952         closelog ();
953
954         /*
955          * See if the user has extra arguments on the command line. In that
956          * case they will be provided to the new user's shell as arguments.
957          */
958         if (fakelogin) {
959                 char *arg0;
960
961                 cp = getdef_str ("SU_NAME");
962                 if (NULL == cp) {
963                         cp = Basename (shellstr);
964                 }
965
966                 arg0 = xmalloc (strlen (cp) + 2);
967                 arg0[0] = '-';
968                 strcpy (arg0 + 1, cp);
969                 cp = arg0;
970         } else {
971                 cp = Basename (shellstr);
972         }
973
974         if (!doshell) {
975                 /* Position argv to the remaining arguments */
976                 argv += optind;
977                 if (NULL != command) {
978                         argv -= 2;
979                         argv[0] = "-c";
980                         argv[1] = command;
981                 }
982                 /*
983                  * Use the shell and create an argv
984                  * with the rest of the command line included.
985                  */
986                 argv[-1] = shellstr;
987 #ifndef USE_PAM
988                 execve_shell (shellstr, &argv[-1], environ);
989                 err = errno;
990                 (void) fputs (_("No shell\n"), stderr);
991                 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
992                 closelog ();
993                 exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
994 #else
995                 run_shell (shellstr, &argv[-1], false, environ); /* no return */
996 #endif
997         }
998 #ifndef USE_PAM
999         err = shell (shellstr, cp, environ);
1000         exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
1001 #else
1002         run_shell (shellstr, &cp, true, environ);
1003 #endif
1004         /* NOT REACHED */
1005         exit (1);
1006 }
1007