]> granicus.if.org Git - shadow/blob - src/su.c
* src/su.c: Updating pwent after expire() is not useful. Only the
[shadow] / 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 - 2011, 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$"
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 const 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 int caught = 0;
92 /* PID of the child, in case it needs to be killed */
93 static pid_t pid_child = 0;
94 #endif
95
96 extern struct passwd pwent;
97
98 /*
99  * External identifiers
100  */
101
102 extern char **newenvp;
103 extern char **environ;
104 extern size_t newenvc;
105
106 /* local function prototypes */
107
108 static void execve_shell (const char *shellstr,
109                           char *args[],
110                           char *const envp[]);
111 #ifdef USE_PAM
112 static RETSIGTYPE kill_child (int unused(s));
113 #else                           /* !USE_PAM */
114 static RETSIGTYPE die (int);
115 static bool iswheel (const char *);
116 #endif                          /* !USE_PAM */
117
118 #ifndef USE_PAM
119 /*
120  * die - set or reset termio modes.
121  *
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.
125  */
126 static RETSIGTYPE die (int killed)
127 {
128         static TERMIO sgtty;
129
130         if (killed != 0) {
131                 STTY (0, &sgtty);
132         } else {
133                 GTTY (0, &sgtty);
134         }
135
136         if (killed != 0) {
137                 closelog ();
138                 exit (128+killed);
139         }
140 }
141
142 static bool iswheel (const char *username)
143 {
144         struct group *grp;
145
146         grp = getgrnam ("wheel"); /* !USE_PAM, no need for xgetgrnam */
147         if (   (NULL ==grp)
148             || (NULL == grp->gr_mem)) {
149                 return false;
150         }
151         return is_on_list (grp->gr_mem, username);
152 }
153 #else                           /* USE_PAM */
154 static RETSIGTYPE kill_child (int unused(s))
155 {
156         if (0 != pid_child) {
157                 (void) kill (pid_child, SIGKILL);
158                 (void) fputs (_(" ...killed.\n"), stderr);
159         } else {
160                 (void) fputs (_(" ...waiting for child to terminate.\n"),
161                               stderr);
162         }
163         exit (255);
164 }
165 #endif                          /* USE_PAM */
166
167 /* borrowed from GNU sh-utils' "su.c" */
168 static bool restricted_shell (const char *shellstr)
169 {
170         char *line;
171
172         setusershell ();
173         while ((line = getusershell ()) != NULL) {
174                 if (('#' != *line) && (strcmp (line, shellstr) == 0)) {
175                         endusershell ();
176                         return false;
177                 }
178         }
179         endusershell ();
180         return true;
181 }
182
183 static void su_failure (const char *tty)
184 {
185         sulog (tty, false, oldname, name);      /* log failed attempt */
186 #ifdef USE_SYSLOG
187         if (getdef_bool ("SYSLOG_SU_ENAB")) {
188                 SYSLOG (((0 != pwent.pw_uid) ? LOG_INFO : LOG_NOTICE,
189                          "- %s %s:%s", tty,
190                          ('\0' != oldname[0]) ? oldname : "???",
191                          ('\0' != name[0]) ? name : "???"));
192         }
193         closelog ();
194 #endif
195         exit (1);
196 }
197
198 /*
199  * execve_shell - Execute a shell with execve, or interpret it with
200  * /bin/sh
201  */
202 static void execve_shell (const char *shellstr,
203                           char *args[],
204                           char *const envp[])
205 {
206         int err;
207         (void) execve (shellstr, (char **) args, envp);
208         err = errno;
209
210         if (access (shellstr, R_OK|X_OK) == 0) {
211                 /*
212                  * Assume this is a shell script (with no shebang).
213                  * Interpret it with /bin/sh
214                  */
215                 size_t n_args = 0;
216                 char **targs;
217                 while (NULL != args[n_args]) {
218                         n_args++;
219                 }
220                 targs = (char **) xmalloc ((n_args + 3) * sizeof (args[0]));
221                 targs[0] = "sh";
222                 targs[1] = "-";
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];
227                         n_args--;
228                 }
229
230                 (void) execve (SHELL, targs, envp);
231         } else {
232                 errno = err;
233         }
234 }
235
236 #ifdef USE_PAM
237 /* Signal handler for parent process later */
238 static void catch_signals (int sig)
239 {
240         caught = sig;
241 }
242
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
245  * su.c from shadow.
246  */
247 static void run_shell (const char *shellstr, char *args[], bool doshell,
248                        char *const envp[])
249 {
250         pid_t child;
251         sigset_t ourset;
252         int status;
253         int ret;
254
255         child = fork ();
256         if (child == 0) {       /* child shell */
257                 /*
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);
262                  */
263
264                 if (doshell) {
265                         (void) shell (shellstr, (char *) args[0], envp);
266                 } else {
267                         /* There is no need for a controlling terminal.
268                          * This avoids the callee to inject commands on
269                          * the caller's tty. */
270                         (void) setsid ();
271
272                         execve_shell (shellstr, (char **) args, envp);
273                 }
274
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"),
279                                 Prog);
280                 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
281                 closelog ();
282                 exit (1);
283         }
284         /* parent only */
285         pid_child = child;
286         sigfillset (&ourset);
287         if (sigprocmask (SIG_BLOCK, &ourset, NULL) != 0) {
288                 (void) fprintf (stderr,
289                                 _("%s: signal malfunction\n"),
290                                 Prog);
291                 caught = SIGTERM;
292         }
293         if (0 == caught) {
294                 struct sigaction action;
295
296                 action.sa_handler = catch_signals;
297                 sigemptyset (&action.sa_mask);
298                 action.sa_flags = 0;
299                 sigemptyset (&ourset);
300
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
307                                      * the tty anymore.
308                                      */
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)
316                     ) {
317                         fprintf (stderr,
318                                  _("%s: signal masking malfunction\n"),
319                                  Prog);
320                         caught = SIGTERM;
321                 }
322         }
323
324         if (0 == caught) {
325                 bool stop = true;
326
327                 do {
328                         pid_t pid;
329                         stop = true;
330
331                         pid = waitpid (-1, &status, WUNTRACED);
332
333                         /* When interrupted by signal, the signal will be
334                          * forwarded to the child, and termination will be
335                          * forced later.
336                          */
337                         if (   ((pid_t)-1 == pid)
338                             && (EINTR == errno)
339                             && (SIGTSTP == caught)) {
340                                 /* Except for SIGTSTP, which request to
341                                  * stop the child.
342                                  * We will SIGSTOP ourself on the next
343                                  * waitpid round.
344                                  */
345                                 kill (child, SIGSTOP);
346                                 stop = false;
347                         } else if (   ((pid_t)-1 != pid)
348                                    && (0 != WIFSTOPPED (status))) {
349                                 /* The child (shell) was suspended.
350                                  * Suspend su. */
351                                 kill (getpid (), SIGSTOP);
352                                 /* wake child when resumed */
353                                 kill (pid, SIGCONT);
354                                 stop = false;
355                         }
356                 } while (!stop);
357         }
358
359         if (0 != caught) {
360                 (void) fputs ("\n", stderr);
361                 (void) fputs (_("Session terminated, terminating shell..."),
362                               stderr);
363                 (void) kill (child, caught);
364         }
365
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);
372                 exit (1);
373         }
374
375         ret = pam_end (pamh, PAM_SUCCESS);
376
377         if (0 != caught) {
378                 (void) signal (SIGALRM, kill_child);
379                 (void) alarm (2);
380
381                 (void) wait (&status);
382                 (void) fputs (_(" ...terminated.\n"), stderr);
383         }
384
385         exit ((0 != WIFEXITED (status)) ? WEXITSTATUS (status)
386                                         : WTERMSIG (status) + 128);
387 }
388 #endif
389
390 /*
391  * usage - print command line syntax and exit
392   */
393 static void usage (int status)
394 {
395         fputs (_("Usage: su [options] [LOGIN]\n"
396                  "\n"
397                  "Options:\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"
401                  "  -m, -p,\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);
406         exit (status);
407 }
408
409 /*
410  * su - switch user id
411  *
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.
414  *
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.
418  */
419 int main (int argc, char **argv)
420 {
421         const char *cp;
422         const char *tty = NULL; /* Name of tty SU is run from        */
423         bool doshell = false;
424         bool fakelogin = false;
425         bool amroot = false;
426         uid_t my_uid;
427         struct passwd *pw = NULL;
428         char **envp = environ;
429         char *shellstr = NULL;
430         char *command = NULL;
431
432 #ifdef USE_PAM
433         char **envcp;
434         int ret;
435 #else                           /* !USE_PAM */
436         int err = 0;
437
438         RETSIGTYPE (*oldsig) (int);
439         int is_console = 0;
440
441         struct spwd *spwd = 0;
442
443 #ifdef SU_ACCESS
444         char *oldpass;
445 #endif
446 #endif                          /* !USE_PAM */
447
448         (void) setlocale (LC_ALL, "");
449         (void) bindtextdomain (PACKAGE, LOCALEDIR);
450         (void) textdomain (PACKAGE);
451
452         change_environment = true;
453
454         /*
455          * Get the program name. The program name is used as a prefix to
456          * most error messages.
457          */
458         Prog = Basename (argv[0]);
459
460         OPENLOG ("su");
461
462         /*
463          * Process the command line arguments. 
464          */
465
466         {
467                 /*
468                  * Parse the command line options.
469                  */
470                 int option_index = 0;
471                 int c;
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'}
479                 };
480
481                 while ((c =
482                         getopt_long (argc, argv, "c:hlmps:", long_options,
483                                      &option_index)) != -1) {
484                         switch (c) {
485                         case 'c':
486                                 command = optarg;
487                                 break;
488                         case 'h':
489                                 usage (E_SUCCESS);
490                                 break;
491                         case 'l':
492                                 fakelogin = true;
493                                 break;
494                         case 'm':
495                         case 'p':
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.
499                                  */
500                                 change_environment = false;
501                                 break;
502                         case 's':
503                                 shellstr = optarg;
504                                 break;
505                         default:
506                                 usage (E_USAGE);        /* NOT REACHED */
507                         }
508                 }
509
510                 if ((optind < argc) && (strcmp (argv[optind], "-") == 0)) {
511                         fakelogin = true;
512                         optind++;
513                         if (   (optind < argc)
514                             && (strcmp (argv[optind], "--") == 0)) {
515                                 optind++;
516                         }
517                 }
518         }
519
520         initenv ();
521
522         my_uid = getuid ();
523         amroot = (my_uid == 0);
524
525         /*
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.
528          */
529         tty = ttyname (0);
530         if ((isatty (0) != 0) && (NULL != tty)) {
531 #ifndef USE_PAM
532                 is_console = console (tty);
533 #endif
534         } else {
535                 /*
536                  * Be more paranoid, like su from SimplePAMApps.  --marekm
537                  */
538                 if (!amroot) {
539                         fprintf (stderr,
540                                  _("%s: must be run from a terminal\n"),
541                                  Prog);
542                         exit (1);
543                 }
544                 tty = "???";
545         }
546
547         /*
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.
552          */
553         if ((optind < argc) && ('-' != argv[optind][0])) {
554                 STRFCPY (name, argv[optind++]); /* use this login id */
555                 if ((optind < argc) && (strcmp (argv[optind], "--") == 0)) {
556                         optind++;
557                 }
558         }
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");
563                 } else {
564                         root_pw = getpwuid (0);
565                         if (NULL == root_pw) {
566                                 SYSLOG ((LOG_CRIT, "There is no UID 0 user."));
567                                 su_failure (tty);
568                         }
569                         (void) strcpy (name, root_pw->pw_name);
570                 }
571         }
572
573         doshell = (argc == optind);     /* any arguments remaining? */
574         if (NULL != command) {
575                 doshell = false;
576         }
577
578         /*
579          * Get the user's real name. The current UID is used to determine
580          * who has executed su. That user ID must exist.
581          */
582         pw = get_my_pwent ();
583         if (NULL == pw) {
584                 fprintf (stderr,
585                          _("%s: Cannot determine your user name.\n"),
586                          Prog);
587                 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
588                          (unsigned long) my_uid));
589                 su_failure (tty);
590         }
591         STRFCPY (oldname, pw->pw_name);
592
593 #ifndef USE_PAM
594 #ifdef SU_ACCESS
595         /*
596          * Sort out the password of user calling su, in case needed later
597          * -- chris
598          */
599         spwd = getspnam (oldname); /* !USE_PAM, no need for xgetspnam */
600         if (NULL != spwd) {
601                 pw->pw_passwd = spwd->sp_pwdp;
602         }
603         oldpass = xstrdup (pw->pw_passwd);
604 #endif                          /* SU_ACCESS */
605
606 #else                           /* USE_PAM */
607         ret = pam_start ("su", name, &conv, &pamh);
608         if (PAM_SUCCESS != ret) {
609                 SYSLOG ((LOG_ERR, "pam_start: error %d", ret);
610                         fprintf (stderr,
611                                  _("%s: pam_start: error %d\n"),
612                                  Prog, ret));
613                 exit (1);
614         }
615
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);
619         }
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));
624                 pam_end (pamh, ret);
625                 exit (1);
626         }
627 #endif                          /* USE_PAM */
628
629       top:
630         /*
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.
634          *
635          * The password file entries for the user is gotten and the account
636          * validated.
637          */
638         pw = xgetpwnam (name);
639         if (NULL == pw) {
640                 (void) fprintf (stderr, _("Unknown id: %s\n"), name);
641                 closelog ();
642                 exit (1);
643         }
644 #ifndef USE_PAM
645         spwd = NULL;
646         if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
647                 spwd = getspnam (name); /* !USE_PAM, no need for xgetspnam */
648                 if (NULL != spwd) {
649                         pw->pw_passwd = spwd->sp_pwdp;
650                 }
651         }
652 #endif                          /* !USE_PAM */
653         pwent = *pw;
654
655         /* If su is not called by root, and the target user has a restricted
656          * shell, the environment must be changed.
657          */
658         change_environment |= (restricted_shell (pwent.pw_shell) && !amroot);
659
660         /*
661          * If a new login is being set up, the old environment will be
662          * ignored and a new one created later on.
663          * (note: in the case of a subsystem, the shell will be restricted,
664          *        and this won't be executed on the first pass)
665          */
666         if (change_environment && fakelogin) {
667                 /*
668                  * The terminal type will be left alone if it is present in
669                  * the environment already.
670                  */
671                 cp = getenv ("TERM");
672                 if (NULL != cp) {
673                         addenv ("TERM", cp);
674                 }
675
676                 /*
677                  * For some terminals COLORTERM seems to be the only way
678                  * for checking for that specific terminal. For instance,
679                  * gnome-terminal sets its TERM as "xterm" but its
680                  * COLORTERM as "gnome-terminal". The COLORTERM variable
681                  * is also of use when running GNU screen since it sets
682                  * TERM to "screen" but doesn't touch COLORTERM.
683                  */
684                 cp = getenv ("COLORTERM");
685                 if (NULL != cp) {
686                         addenv ("COLORTERM", cp);
687                 }
688
689 #ifndef USE_PAM
690                 cp = getdef_str ("ENV_TZ");
691                 if (NULL != cp) {
692                         addenv (('/' == *cp) ? tz (cp) : cp, NULL);
693                 }
694
695                 /*
696                  * The clock frequency will be reset to the login value if required
697                  */
698                 cp = getdef_str ("ENV_HZ");
699                 if (NULL != cp) {
700                         addenv (cp, NULL);      /* set the default $HZ, if one */
701                 }
702 #endif                          /* !USE_PAM */
703
704                 /*
705                  * Also leave DISPLAY and XAUTHORITY if present, else
706                  * pam_xauth will not work.
707                  */
708                 cp = getenv ("DISPLAY");
709                 if (NULL != cp) {
710                         addenv ("DISPLAY", cp);
711                 }
712                 cp = getenv ("XAUTHORITY");
713                 if (NULL != cp) {
714                         addenv ("XAUTHORITY", cp);
715                 }
716         } else {
717                 while (NULL != *envp) {
718                         addenv (*envp, NULL);
719                         envp++;
720                 }
721         }
722
723 #ifndef USE_PAM
724         /*
725          * BSD systems only allow "wheel" to SU to root. USG systems don't,
726          * so we make this a configurable option.
727          */
728
729         /* The original Shadow 3.3.2 did this differently. Do it like BSD:
730          *
731          * - check for UID 0 instead of name "root" - there are systems with
732          *   several root accounts under different names,
733          *
734          * - check the contents of /etc/group instead of the current group
735          *   set (you must be listed as a member, GID 0 is not sufficient).
736          *
737          * In addition to this traditional feature, we now have complete su
738          * access control (allow, deny, no password, own password).  Thanks
739          * to Chris Evans <lady0110@sable.ox.ac.uk>.
740          */
741
742         if (!amroot) {
743                 if (   (0 == pwent.pw_uid)
744                     && getdef_bool ("SU_WHEEL_ONLY")
745                     && !iswheel (oldname)) {
746                         fprintf (stderr,
747                                  _("You are not authorized to su %s\n"),
748                                  name);
749                         exit (1);
750                 }
751 #ifdef SU_ACCESS
752                 switch (check_su_auth (oldname, name)) {
753                 case 0: /* normal su, require target user's password */
754                         break;
755                 case 1: /* require no password */
756                         pwent.pw_passwd = "";   /* XXX warning: const */
757                         break;
758                 case 2: /* require own password */
759                         puts (_("(Enter your own password)"));
760                         pwent.pw_passwd = oldpass;
761                         break;
762                 default:        /* access denied (-1) or unexpected value */
763                         fprintf (stderr,
764                                  _("You are not authorized to su %s\n"),
765                                  name);
766                         exit (1);
767                 }
768 #endif                          /* SU_ACCESS */
769         }
770 #endif                          /* !USE_PAM */
771
772         /* If the user do not want to change the environment,
773          * use the current SHELL.
774          * (unless another shell is required by the command line)
775          */
776         if ((NULL == shellstr) && !change_environment) {
777                 shellstr = getenv ("SHELL");
778         }
779         /* For users with non null UID, if this user has a restricted
780          * shell, the shell must be the one specified in /etc/passwd
781          */
782         if (   (NULL != shellstr)
783             && !amroot
784             && restricted_shell (pwent.pw_shell)) {
785                 shellstr = NULL;
786         }
787         /* If the shell is not set at this time, use the shell specified
788          * in /etc/passwd.
789          */
790         if (NULL == shellstr) {
791                 shellstr = (char *) strdup (pwent.pw_shell);
792         }
793
794         /*
795          * Set the default shell.
796          */
797         if ((NULL == shellstr) || ('\0' == shellstr[0])) {
798                 shellstr = SHELL;
799         }
800
801         (void) signal (SIGINT, SIG_IGN);
802         (void) signal (SIGQUIT, SIG_IGN);
803 #ifdef USE_PAM
804         ret = pam_authenticate (pamh, 0);
805         if (PAM_SUCCESS != ret) {
806                 SYSLOG ((LOG_ERR, "pam_authenticate: %s",
807                          pam_strerror (pamh, ret)));
808                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
809                 (void) pam_end (pamh, ret);
810                 su_failure (tty);
811         }
812
813         ret = pam_acct_mgmt (pamh, 0);
814         if (PAM_SUCCESS != ret) {
815                 if (amroot) {
816                         fprintf (stderr,
817                                  _("%s: %s\n(Ignored)\n"),
818                                  Prog, pam_strerror (pamh, ret));
819                 } else if (PAM_NEW_AUTHTOK_REQD == ret) {
820                         ret = pam_chauthtok (pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
821                         if (PAM_SUCCESS != ret) {
822                                 SYSLOG ((LOG_ERR, "pam_chauthtok: %s",
823                                          pam_strerror (pamh, ret)));
824                                 fprintf (stderr,
825                                          _("%s: %s\n"),
826                                          Prog, pam_strerror (pamh, ret));
827                                 (void) pam_end (pamh, ret);
828                                 su_failure (tty);
829                         }
830                 } else {
831                         SYSLOG ((LOG_ERR, "pam_acct_mgmt: %s",
832                                  pam_strerror (pamh, ret)));
833                         fprintf (stderr,
834                                  _("%s: %s\n"),
835                                  Prog, pam_strerror (pamh, ret));
836                         (void) pam_end (pamh, ret);
837                         su_failure (tty);
838                 }
839         }
840 #else                           /* !USE_PAM */
841         /*
842          * Set up a signal handler in case the user types QUIT.
843          */
844         die (0);
845         oldsig = signal (SIGQUIT, die);
846
847         /*
848          * See if the system defined authentication method is being used. 
849          * The first character of an administrator defined method is an '@'
850          * character.
851          */
852         if (   !amroot
853             && (pw_auth (pwent.pw_passwd, name, PW_SU, (char *) 0) != 0)) {
854                 SYSLOG (((pwent.pw_uid != 0)? LOG_NOTICE : LOG_WARN,
855                          "Authentication failed for %s", name));
856                 fprintf(stderr, _("%s: Authentication failure\n"), Prog);
857                 su_failure (tty);
858         }
859         (void) signal (SIGQUIT, oldsig);
860
861         /*
862          * Check to see if the account is expired. root gets to ignore any
863          * expired accounts, but normal users can't become a user with an
864          * expired password.
865          */
866         if (!amroot) {
867                 if (NULL == spwd) {
868                         spwd = pwd_to_spwd (&pwent);
869                 }
870
871                 (void) expire (&pwent, spwd);
872         }
873
874         /*
875          * Check to see if the account permits "su". root gets to ignore any
876          * restricted accounts, but normal users can't become a user if
877          * there is a "SU" entry in the /etc/porttime file denying access to
878          * the account.
879          */
880         if (!amroot) {
881                 if (!isttytime (pwent.pw_name, "SU", time ((time_t *) 0))) {
882                         SYSLOG (((0 != pwent.pw_uid) ? LOG_WARN : LOG_CRIT,
883                                  "SU by %s to restricted account %s",
884                                  oldname, name));
885                         fprintf (stderr,
886                                  _("%s: You are not authorized to su at that time\n"),
887                                  Prog);
888                         su_failure (tty);
889                 }
890         }
891 #endif                          /* !USE_PAM */
892
893         (void) signal (SIGINT, SIG_DFL);
894         (void) signal (SIGQUIT, SIG_DFL);
895
896         cp = getdef_str ((pwent.pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
897         if (NULL == cp) {
898                 addenv ((pwent.pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
899         } else if (strchr (cp, '=') != NULL) {
900                 addenv (cp, NULL);
901         } else {
902                 addenv ("PATH", cp);
903         }
904
905         if (getenv ("IFS") != NULL) {   /* don't export user IFS ... */
906                 addenv ("IFS= \t\n", NULL);     /* ... instead, set a safe IFS */
907         }
908
909         /*
910          * Even if --shell is specified, the subsystem login test is based on
911          * the shell specified in /etc/passwd (not the one specified with
912          * --shell, which will be the one executed in the chroot later).
913          */
914         if ('*' == pwent.pw_shell[0]) { /* subsystem root required */
915                 pwent.pw_shell++;       /* skip the '*' */
916                 subsystem (&pwent);     /* figure out what to execute */
917                 endpwent ();
918                 endspent ();
919                 goto top;
920         }
921
922         sulog (tty, true, oldname, name);       /* save SU information */
923         endpwent ();
924         endspent ();
925 #ifdef USE_SYSLOG
926         if (getdef_bool ("SYSLOG_SU_ENAB")) {
927                 SYSLOG ((LOG_INFO, "+ %s %s:%s", tty,
928                          ('\0' != oldname[0]) ? oldname : "???",
929                          ('\0' != name[0]) ? name : "???"));
930         }
931 #endif
932
933 #ifdef USE_PAM
934         /* set primary group id and supplementary groups */
935         if (setup_groups (&pwent) != 0) {
936                 pam_end (pamh, PAM_ABORT);
937                 exit (1);
938         }
939
940         /*
941          * pam_setcred() may do things like resource limits, console groups,
942          * and much more, depending on the configured modules
943          */
944         ret = pam_setcred (pamh, PAM_ESTABLISH_CRED);
945         if (PAM_SUCCESS != ret) {
946                 SYSLOG ((LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, ret)));
947                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
948                 (void) pam_end (pamh, ret);
949                 exit (1);
950         }
951
952         ret = pam_open_session (pamh, 0);
953         if (PAM_SUCCESS != ret) {
954                 SYSLOG ((LOG_ERR, "pam_open_session: %s",
955                          pam_strerror (pamh, ret)));
956                 fprintf (stderr, _("%s: %s\n"), Prog, pam_strerror (pamh, ret));
957                 pam_setcred (pamh, PAM_DELETE_CRED);
958                 (void) pam_end (pamh, ret);
959                 exit (1);
960         }
961
962         /* we need to setup the environment *after* pam_open_session(),
963          * else the UID is changed before stuff like pam_xauth could
964          * run, and we cannot access /etc/shadow and co
965          */
966         environ = newenvp;      /* make new environment active */
967
968         if (change_environment) {
969                 /* update environment with all pam set variables */
970                 envcp = pam_getenvlist (pamh);
971                 if (NULL != envcp) {
972                         while (NULL != *envcp) {
973                                 addenv (*envcp, NULL);
974                                 envcp++;
975                         }
976                 }
977         }
978
979         /* become the new user */
980         if (change_uid (&pwent) != 0) {
981                 pam_close_session (pamh, 0);
982                 pam_setcred (pamh, PAM_DELETE_CRED);
983                 (void) pam_end (pamh, PAM_ABORT);
984                 exit (1);
985         }
986 #else                           /* !USE_PAM */
987         environ = newenvp;      /* make new environment active */
988
989         /* no limits if su from root (unless su must fake login's behavior) */
990         if (!amroot || fakelogin) {
991                 setup_limits (&pwent);
992         }
993
994         if (setup_uid_gid (&pwent, is_console) != 0) {
995                 exit (1);
996         }
997 #endif                          /* !USE_PAM */
998
999         if (change_environment) {
1000                 if (fakelogin) {
1001                         pwent.pw_shell = shellstr;
1002                         setup_env (&pwent);
1003                 } else {
1004                         addenv ("HOME", pwent.pw_dir);
1005                         addenv ("USER", pwent.pw_name);
1006                         addenv ("LOGNAME", pwent.pw_name);
1007                         addenv ("SHELL", shellstr);
1008                 }
1009         }
1010
1011         /*
1012          * This is a workaround for Linux libc bug/feature (?) - the
1013          * /dev/log file descriptor is open without the close-on-exec flag
1014          * and used to be passed to the new shell. There is "fcntl(LogFile,
1015          * F_SETFD, 1)" in libc/misc/syslog.c, but it is commented out (at
1016          * least in 5.4.33). Why?  --marekm
1017          */
1018         closelog ();
1019
1020         /*
1021          * See if the user has extra arguments on the command line. In that
1022          * case they will be provided to the new user's shell as arguments.
1023          */
1024         if (fakelogin) {
1025                 char *arg0;
1026
1027                 cp = getdef_str ("SU_NAME");
1028                 if (NULL == cp) {
1029                         cp = Basename (shellstr);
1030                 }
1031
1032                 arg0 = xmalloc (strlen (cp) + 2);
1033                 arg0[0] = '-';
1034                 strcpy (arg0 + 1, cp);
1035                 cp = arg0;
1036         } else {
1037                 cp = Basename (shellstr);
1038         }
1039
1040         if (!doshell) {
1041                 /* Position argv to the remaining arguments */
1042                 argv += optind;
1043                 if (NULL != command) {
1044                         argv -= 2;
1045                         argv[0] = "-c";
1046                         argv[1] = command;
1047                 }
1048                 /*
1049                  * Use the shell and create an argv
1050                  * with the rest of the command line included.
1051                  */
1052                 argv[-1] = cp;
1053 #ifndef USE_PAM
1054                 execve_shell (shellstr, &argv[-1], environ);
1055                 err = errno;
1056                 (void) fputs (_("No shell\n"), stderr);
1057                 SYSLOG ((LOG_WARN, "Cannot execute %s", shellstr));
1058                 closelog ();
1059                 exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
1060 #else
1061                 run_shell (shellstr, &argv[-1], false, environ); /* no return */
1062 #endif
1063         }
1064 #ifndef USE_PAM
1065         err = shell (shellstr, cp, environ);
1066         exit ((ENOENT == err) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
1067 #else
1068         run_shell (shellstr, &cp, true, environ);
1069 #endif
1070         /* NOT REACHED */
1071         exit (1);
1072 }
1073