1 /* vi: set sw=4 ts=4: */
3 * Mini su implementation for busybox
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
12 /* Return 1 if SHELL is a restricted shell (one not returned by
13 getusershell), else 0, meaning it is a standard shell. */
14 static int restricted_shell(const char *shell)
18 /*setusershell(); - getusershell does it itself*/
19 while ((line = getusershell()) != NULL) {
20 if (/* *line != '#' && */ strcmp(line, shell) == 0)
31 int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32 int su_main(int argc UNUSED_PARAM, char **argv)
35 char *opt_shell = NULL;
36 char *opt_command = NULL;
37 const char *opt_username = "root";
39 uid_t cur_uid = getuid();
43 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
47 if (argv[0] && LONE_DASH(argv[0])) {
52 /* get user if specified */
54 opt_username = argv[0];
58 if (ENABLE_FEATURE_SU_SYSLOG) {
59 /* The utmp entry (via getlogin) is probably the best way to identify
60 * the user, especially if someone su's from a su-shell.
61 * But getlogin can fail -- usually due to lack of utmp entry.
62 * in this case resort to getpwuid. */
64 #if ENABLE_FEATURE_UTMP
67 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
70 pw = getpwuid(cur_uid);
71 user = pw ? pw->pw_name : "";
73 old_user = xstrdup(user);
74 tty = xmalloc_ttyname(2);
78 openlog(applet_name, 0, LOG_AUTH);
81 pw = xgetpwnam(opt_username);
83 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
84 is a username that is retrieved via NIS (YP), but that doesn't have
85 a default shell listed. */
86 if (!pw->pw_shell || !pw->pw_shell[0])
87 pw->pw_shell = (char *)DEFAULT_SHELL;
89 if ((cur_uid == 0) || correct_password(pw)) {
90 if (ENABLE_FEATURE_SU_SYSLOG)
91 syslog(LOG_NOTICE, "%c %s %s:%s",
92 '+', tty, old_user, opt_username);
94 if (ENABLE_FEATURE_SU_SYSLOG)
95 syslog(LOG_NOTICE, "%c %s %s:%s",
96 '-', tty, old_user, opt_username);
97 bb_error_msg_and_die("incorrect password");
100 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
105 if (!opt_shell && (flags & SU_OPT_mp))
106 opt_shell = getenv("SHELL");
108 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
109 if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
110 /* The user being su'd to has a nonstandard shell, and so is
111 probably a uucp account or has restricted access. Don't
112 compromise the account by allowing access with a standard
114 bb_error_msg("using restricted shell");
119 opt_shell = pw->pw_shell;
122 setup_environment(opt_shell,
123 ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
124 + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
126 IF_SELINUX(set_current_security_context(NULL);)
129 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
131 /* return EXIT_FAILURE; - not reached */