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.
14 int su_main(int argc, char **argv)
18 char *opt_command = 0;
19 char *opt_username = "root";
21 uid_t cur_uid = getuid();
25 flags = getopt32(argc, argv, "mplc:s:", &opt_command, &opt_shell);
29 if (argc && LONE_DASH(argv[0])) {
35 /* get user if specified */
37 opt_username = argv[0];
42 if (ENABLE_FEATURE_SU_SYSLOG) {
43 /* The utmp entry (via getlogin) is probably the best way to identify
44 the user, especially if someone su's from a su-shell.
45 But getlogin can fail -- usually due to lack of utmp entry.
46 in this case resort to getpwuid. */
47 old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
48 tty = ttyname(2) ? : "none";
49 openlog(applet_name, 0, LOG_AUTH);
52 pw = getpwnam(opt_username);
54 bb_error_msg_and_die("unknown id: %s", opt_username);
56 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
57 is a username that is retrieved via NIS (YP), but that doesn't have
58 a default shell listed. */
59 if (!pw->pw_shell || !pw->pw_shell[0])
60 pw->pw_shell = (char *)DEFAULT_SHELL;
62 if ((cur_uid == 0) || correct_password(pw)) {
63 if (ENABLE_FEATURE_SU_SYSLOG)
64 syslog(LOG_NOTICE, "%c %s %s:%s",
65 '+', tty, old_user, opt_username);
67 if (ENABLE_FEATURE_SU_SYSLOG)
68 syslog(LOG_NOTICE, "%c %s %s:%s",
69 '-', tty, old_user, opt_username);
70 bb_error_msg_and_die("incorrect password");
73 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
78 if (!opt_shell && (flags & SU_OPT_mp))
79 opt_shell = getenv("SHELL");
81 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
82 if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
83 /* The user being su'd to has a nonstandard shell, and so is
84 probably a uucp account or has restricted access. Don't
85 compromise the account by allowing access with a standard
87 bb_error_msg("using restricted shell");
92 opt_shell = pw->pw_shell;
95 setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
96 USE_SELINUX(set_current_security_context(NULL);)
99 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);