1 /* vi: set sw=4 ts=4: */
3 Licensed under the GPL v2, see the file LICENSE in this tarball.
15 #include <sys/resource.h>
17 #include <sys/types.h>
23 /* The shell to run if none is given in the user's passwd entry. */
25 #define DEFAULT_SHELL "/bin/sh"
29 #define DEFAULT_USER "root"
31 /* #define SYSLOG_SUCCESS */
32 #define SYSLOG_FAILURE
35 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
36 /* Log the fact that someone has run su */
38 # if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
39 static void log_su (const char *successful, const char *old_user,
42 syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
44 # define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
45 # define log_su_failure(cu, u, tty) if(!cu) log_su("FAILED SU ", u, tty)
48 # if !defined( SYSLOG_SUCESS )
49 # define log_su_successful(cu, u, tty)
50 # define log_su_failure(cu, u, t) if(!cu) \
51 syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
53 # define log_su_successful(cu, u, t) if(!cu) \
54 syslog(LOG_NOTICE, "%s on %s", u, t)
55 # define log_su_failure(cu, u, tty)
59 /* logging not used */
60 # define log_su_successful(cu, u, tty)
61 # define log_su_failure(cu, u, tty)
65 int su_main ( int argc, char **argv )
71 char *opt_command = 0;
72 char *opt_username = DEFAULT_USER;
75 uid_t cur_uid = getuid();
77 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
82 flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
83 &opt_command, &opt_shell);
84 opt_preserve = flags & 3;
85 opt_loginshell = (flags & 4 ? 1 : 0);
87 if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
92 /* get user if specified */
94 opt_username = argv [optind++];
97 opt_args = argv + optind;
99 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
100 #ifdef CONFIG_FEATURE_UTMP
101 /* The utmp entry (via getlogin) is probably the best way to identify
102 the user, especially if someone su's from a su-shell. */
103 old_user = getlogin ( );
107 /* getlogin can fail -- usually due to lack of utmp entry.
108 Resort to getpwuid. */
109 pw = getpwuid ( cur_uid );
110 old_user = ( pw ? pw->pw_name : "" );
116 openlog ( bb_applet_name, 0, LOG_AUTH );
119 pw = getpwnam ( opt_username );
121 bb_error_msg_and_die ( "user %s does not exist", opt_username );
123 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
124 is a username that is retrieved via NIS (YP), but that doesn't have
125 a default shell listed. */
126 if ( !pw->pw_shell || !pw->pw_shell [0] )
127 pw->pw_shell = (char *) DEFAULT_SHELL;
129 if ((( cur_uid == 0 ) || correct_password ( pw ))) {
130 log_su_successful(pw->pw_uid, old_user, tty );
132 log_su_failure (pw->pw_uid, old_user, tty );
133 bb_error_msg_and_die ( "incorrect password" );
136 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
140 if ( !opt_shell && opt_preserve )
141 opt_shell = getenv ( "SHELL" );
143 if ( opt_shell && cur_uid && restricted_shell ( pw->pw_shell )) {
144 /* The user being su'd to has a nonstandard shell, and so is
145 probably a uucp account or has restricted access. Don't
146 compromise the account by allowing access with a standard
148 fputs ( "using restricted shell\n", stderr );
153 opt_shell = pw->pw_shell;
155 change_identity ( pw );
156 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
158 set_current_security_context(NULL);
160 run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args);