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