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