Big cleanup in config help and description
[oweals/busybox.git] / loginutils / su.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini su implementation for busybox
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 //config:config SU
8 //config:       bool "su"
9 //config:       default y
10 //config:       select FEATURE_SYSLOG
11 //config:       help
12 //config:         su is used to become another user during a login session.
13 //config:         Invoked without a username, su defaults to becoming the super user.
14 //config:         Note that busybox binary must be setuid root for this applet to
15 //config:         work properly.
16 //config:
17 //config:config FEATURE_SU_SYSLOG
18 //config:       bool "Log to syslog all attempts to use su"
19 //config:       default y
20 //config:       depends on SU
21 //config:
22 //config:config FEATURE_SU_CHECKS_SHELLS
23 //config:       bool "If user's shell is not in /etc/shells, disallow -s PROG"
24 //config:       default y
25 //config:       depends on SU
26
27 //applet:/* Needs to be run by root or be suid root - needs to change uid and gid: */
28 //applet:IF_SU(APPLET(su, BB_DIR_BIN, BB_SUID_REQUIRE))
29
30 //kbuild:lib-$(CONFIG_SU) += su.o
31
32 //usage:#define su_trivial_usage
33 //usage:       "[-lmp] [-] [-s SH] [USER [SCRIPT ARGS / -c 'CMD' ARG0 ARGS]]"
34 //usage:#define su_full_usage "\n\n"
35 //usage:       "Run shell under USER (by default, root)\n"
36 //usage:     "\n        -,-l    Clear environment, go to home dir, run shell as login shell"
37 //usage:     "\n        -p,-m   Do not set new $HOME, $SHELL, $USER, $LOGNAME"
38 //usage:     "\n        -c CMD  Command to pass to 'sh -c'"
39 //usage:     "\n        -s SH   Shell to use instead of user's default"
40
41 #include "libbb.h"
42 #include <syslog.h>
43
44 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
45 /* Return 1 if SHELL is a restricted shell (one not returned by
46  * getusershell), else 0, meaning it is a standard shell.  */
47 static int restricted_shell(const char *shell)
48 {
49         char *line;
50         int result = 1;
51
52         /*setusershell(); - getusershell does it itself*/
53         while ((line = getusershell()) != NULL) {
54                 if (/* *line != '#' && */ strcmp(line, shell) == 0) {
55                         result = 0;
56                         break;
57                 }
58         }
59         if (ENABLE_FEATURE_CLEAN_UP)
60                 endusershell();
61         return result;
62 }
63 #endif
64
65 #define SU_OPT_mp (3)
66 #define SU_OPT_l  (4)
67
68 int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69 int su_main(int argc UNUSED_PARAM, char **argv)
70 {
71         unsigned flags;
72         char *opt_shell = NULL;
73         char *opt_command = NULL;
74         const char *opt_username = "root";
75         struct passwd *pw;
76         uid_t cur_uid = getuid();
77         const char *tty;
78 #if ENABLE_FEATURE_UTMP
79         char user_buf[64];
80 #endif
81         const char *old_user;
82
83         /* Note: we don't use "'+': stop at first non-option" idiom here.
84          * For su, "SCRIPT ARGS" or "-c CMD ARGS" do not stop option parsing:
85          * ARGS starting with dash will be treated as su options,
86          * not passed to shell. (Tested on util-linux 2.28).
87          */
88         flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
89         argv += optind;
90
91         if (argv[0] && LONE_DASH(argv[0])) {
92                 flags |= SU_OPT_l;
93                 argv++;
94         }
95
96         /* get user if specified */
97         if (argv[0]) {
98                 opt_username = argv[0];
99                 argv++;
100         }
101
102         if (ENABLE_FEATURE_SU_SYSLOG) {
103                 /* The utmp entry (via getlogin) is probably the best way to
104                  * identify the user, especially if someone su's from a su-shell.
105                  * But getlogin can fail -- usually due to lack of utmp entry.
106                  * in this case resort to getpwuid.  */
107 #if ENABLE_FEATURE_UTMP
108                 old_user = user_buf;
109                 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
110 #endif
111                 {
112                         pw = getpwuid(cur_uid);
113                         old_user = pw ? xstrdup(pw->pw_name) : "";
114                 }
115                 tty = xmalloc_ttyname(2);
116                 if (!tty) {
117                         tty = "none";
118                 }
119                 openlog(applet_name, 0, LOG_AUTH);
120         }
121
122         pw = xgetpwnam(opt_username);
123
124         if (cur_uid == 0 || ask_and_check_password(pw) > 0) {
125                 if (ENABLE_FEATURE_SU_SYSLOG)
126                         syslog(LOG_NOTICE, "%c %s %s:%s",
127                                 '+', tty, old_user, opt_username);
128         } else {
129                 if (ENABLE_FEATURE_SU_SYSLOG)
130                         syslog(LOG_NOTICE, "%c %s %s:%s",
131                                 '-', tty, old_user, opt_username);
132                 bb_do_delay(LOGIN_FAIL_DELAY);
133                 bb_error_msg_and_die("incorrect password");
134         }
135
136         if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
137                 closelog();
138         }
139
140         if (!opt_shell && (flags & SU_OPT_mp)) {
141                 /* -s SHELL is not given, but "preserve env" opt is */
142                 opt_shell = getenv("SHELL");
143         }
144
145 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
146         if (opt_shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
147                 /* The user being su'd to has a nonstandard shell, and so is
148                  * probably a uucp account or has restricted access.  Don't
149                  * compromise the account by allowing access with a standard
150                  * shell.  */
151                 bb_error_msg("using restricted shell");
152                 opt_shell = NULL; /* ignore -s PROG */
153         }
154         /* else: user can run whatever he wants via "su -s PROG USER".
155          * This is safe since PROG is run under user's uid/gid. */
156 #endif
157         if (!opt_shell)
158                 opt_shell = pw->pw_shell;
159
160         change_identity(pw);
161         setup_environment(opt_shell,
162                         ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
163                         + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
164                         + (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
165                         pw);
166         IF_SELINUX(set_current_security_context(NULL);)
167
168         if (opt_command) {
169                 *--argv = opt_command;
170                 *--argv = (char*)"-c";
171         }
172
173         /* A nasty ioctl exists which can stuff data into input queue:
174          * #include <sys/ioctl.h>
175          * int main() {
176          *      const char *msg = "echo $UID\n";
177          *      while (*msg) ioctl(0, TIOCSTI, *msg++);
178          *      return 0;
179          * }
180          * With "su USER -c EXPLOIT" run by root, exploit can make root shell
181          * read as input and execute arbitrary command.
182          * It's debatable whether we need to protect against this
183          * (root may hesitate to run unknown scripts interactively).
184          *
185          * Some versions of su run -c CMD in a different session:
186          * ioctl(TIOCSTI) works only on the controlling tty.
187          */
188
189         /* Never returns */
190         run_shell(opt_shell, flags & SU_OPT_l, (const char**)argv);
191
192         /* return EXIT_FAILURE; - not reached */
193 }