660ec6f515c172a321ac195ffe4d0b9b8321f540
[oweals/busybox.git] / loginutils / su.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Mini su implementation for busybox
4  *
5  *  Licensed under the GPL v2, see the file LICENSE in this tarball.
6  */
7
8 #include "busybox.h"
9 #include <signal.h>
10 #include <syslog.h>
11 #include <sys/resource.h>
12 #include <time.h>
13
14
15 int su_main ( int argc, char **argv )
16 {
17         unsigned long flags;
18         char *opt_shell = 0;
19         char *opt_command = 0;
20         char *opt_username = "root";
21         char **opt_args = 0;
22         struct passwd *pw;
23         uid_t cur_uid = getuid();
24         const char *tty;
25         char *old_user;
26
27         flags = bb_getopt_ulflags(argc, argv, "mplc:s:", &opt_command, &opt_shell);
28 #define SU_OPT_mp (3)
29 #define SU_OPT_l (4)
30
31         if (optind < argc  && argv[optind][0] == '-' && argv[optind][1] == 0) {
32                 flags |= SU_OPT_l;
33                 ++optind;
34     }
35
36         /* get user if specified */
37         if (optind < argc) opt_username = argv [optind++];
38
39         if (optind < argc) opt_args = argv + optind;
40
41         if (ENABLE_SU_SYSLOG) {
42                 /* The utmp entry (via getlogin) is probably the best way to identify
43                 the user, especially if someone su's from a su-shell.
44                 But getlogin can fail -- usually due to lack of utmp entry.
45                 in this case resort to getpwuid.  */
46                 old_user = bb_xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
47                 tty = ttyname(2) ? : "none";
48                 openlog(bb_applet_name, 0, LOG_AUTH);
49         }
50
51         pw = getpwnam(opt_username);
52         if (!pw) bb_error_msg_and_die("Unknown id: %s", opt_username);
53
54         /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
55            is a username that is retrieved via NIS (YP), but that doesn't have
56            a default shell listed.  */
57         if (!pw->pw_shell || !pw->pw_shell[0]) pw->pw_shell = (char *)DEFAULT_SHELL;
58
59         if ((cur_uid == 0) || correct_password(pw)) {
60                 if (ENABLE_SU_SYSLOG)
61                         syslog(LOG_NOTICE, "+ %s %s:%s", tty, old_user, opt_username);
62         } else {
63                 if (ENABLE_SU_SYSLOG)
64                         syslog(LOG_NOTICE, "- %s %s:%s", tty, old_user, opt_username);
65                 bb_error_msg_and_die("incorrect password");
66         }
67
68         if (ENABLE_FEATURE_CLEAN_UP && ENABLE_SU_SYSLOG) {
69                 closelog();
70                 free(old_user);
71         }
72
73         if (!opt_shell && (flags & SU_OPT_mp)) opt_shell = getenv("SHELL");
74
75         if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
76                 /* The user being su'd to has a nonstandard shell, and so is
77                    probably a uucp account or has restricted access.  Don't
78                    compromise the account by allowing access with a standard
79                    shell.  */
80                 bb_error_msg("using restricted shell");
81                 opt_shell = 0;
82         }
83
84         if (!opt_shell) opt_shell = pw->pw_shell;
85
86         change_identity(pw);
87         setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
88     USE_SELINUX(set_current_security_context(NULL);)
89
90         /* Never returns */
91         run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args);
92
93         return EXIT_FAILURE;
94 }