1 /* vi: set sw=4 ts=4: */
3 * Mini su implementation for busybox
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //usage:#define su_trivial_usage
12 //usage: "[OPTIONS] [-] [USER]"
13 //usage:#define su_full_usage "\n\n"
14 //usage: "Run shell under USER (by default, root)\n"
16 //usage: "\n -,-l Clear environment, run shell as login shell"
17 //usage: "\n -p,-m Do not set new $HOME, $SHELL, $USER, $LOGNAME"
18 //usage: "\n -c CMD Command to pass to 'sh -c'"
19 //usage: "\n -s SH Shell to use instead of user's default"
21 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
22 /* Return 1 if SHELL is a restricted shell (one not returned by
23 * getusershell), else 0, meaning it is a standard shell. */
24 static int restricted_shell(const char *shell)
29 /*setusershell(); - getusershell does it itself*/
30 while ((line = getusershell()) != NULL) {
31 if (/* *line != '#' && */ strcmp(line, shell) == 0) {
36 if (ENABLE_FEATURE_CLEAN_UP)
45 int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46 int su_main(int argc UNUSED_PARAM, char **argv)
49 char *opt_shell = NULL;
50 char *opt_command = NULL;
51 const char *opt_username = "root";
53 uid_t cur_uid = getuid();
55 #if ENABLE_FEATURE_UTMP
60 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
64 if (argv[0] && LONE_DASH(argv[0])) {
69 /* get user if specified */
71 opt_username = argv[0];
75 if (ENABLE_FEATURE_SU_SYSLOG) {
76 /* The utmp entry (via getlogin) is probably the best way to
77 * identify the user, especially if someone su's from a su-shell.
78 * But getlogin can fail -- usually due to lack of utmp entry.
79 * in this case resort to getpwuid. */
80 #if ENABLE_FEATURE_UTMP
82 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
85 pw = getpwuid(cur_uid);
86 old_user = pw ? xstrdup(pw->pw_name) : "";
88 tty = xmalloc_ttyname(2);
92 openlog(applet_name, 0, LOG_AUTH);
95 pw = xgetpwnam(opt_username);
97 if (cur_uid == 0 || correct_password(pw)) {
98 if (ENABLE_FEATURE_SU_SYSLOG)
99 syslog(LOG_NOTICE, "%c %s %s:%s",
100 '+', tty, old_user, opt_username);
102 if (ENABLE_FEATURE_SU_SYSLOG)
103 syslog(LOG_NOTICE, "%c %s %s:%s",
104 '-', tty, old_user, opt_username);
105 bb_error_msg_and_die("incorrect password");
108 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
112 if (!opt_shell && (flags & SU_OPT_mp)) {
113 /* -s SHELL is not given, but "preserve env" opt is */
114 opt_shell = getenv("SHELL");
117 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
118 * is a username that is retrieved via NIS (YP), that doesn't have
119 * a default shell listed. */
120 if (!pw->pw_shell || !pw->pw_shell[0])
121 pw->pw_shell = (char *)DEFAULT_SHELL;
123 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
124 if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
125 /* The user being su'd to has a nonstandard shell, and so is
126 * probably a uucp account or has restricted access. Don't
127 * compromise the account by allowing access with a standard
129 bb_error_msg("using restricted shell");
132 /* else: user can run whatever he wants via "su -s PROG USER".
133 * This is safe since PROG is run under user's uid/gid. */
136 opt_shell = pw->pw_shell;
139 setup_environment(opt_shell,
140 ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
141 + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
143 IF_SELINUX(set_current_security_context(NULL);)
146 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
148 /* return EXIT_FAILURE; - not reached */