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