1 /* vi: set sw=4 ts=4: */
12 #include <sys/resource.h>
15 #include <sys/types.h>
23 /* The shell to run if none is given in the user's passwd entry. */
24 #define DEFAULT_SHELL "/bin/sh"
25 #define DEFAULT_USER "root"
27 //#define SYSLOG_SUCCESS
28 #define SYSLOG_FAILURE
31 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
32 /* Log the fact that someone has run su */
34 # if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
35 static void log_su (const char *successful, const char *old_user, const char *tty)
37 syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
39 # define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
40 # define log_su_failure(cu, u, tty) if(!cu) log_su("FAILED SU ", u, tty)
43 # if !defined( SYSLOG_SUCESS )
44 # define log_su_successful(cu, u, tty)
45 # define log_su_failure(cu, u, t) if(!cu) \
46 syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
48 # define log_su_successful(cu, u, t) if(!cu) \
49 syslog(LOG_NOTICE, "%s on %s", u, t)
50 # define log_su_failure(cu, u, tty)
54 /* logging not used */
55 # define log_su_successful(cu, u, tty)
56 # define log_su_failure(cu, u, tty)
60 int su_main ( int argc, char **argv )
66 char *opt_command = 0;
67 char *opt_username = DEFAULT_USER;
70 uid_t cur_uid = getuid();
72 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
77 flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
78 &opt_command, &opt_shell);
79 opt_preserve = flags & 3;
80 opt_loginshell = (flags & 4 ? 1 : 0);
82 if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
87 /* get user if specified */
89 opt_username = argv [optind++];
92 opt_args = argv + optind;
94 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
95 #ifdef CONFIG_FEATURE_U_W_TMP
96 /* The utmp entry (via getlogin) is probably the best way to identify
97 the user, especially if someone su's from a su-shell. */
98 old_user = getlogin ( );
102 /* getlogin can fail -- usually due to lack of utmp entry. Resort to getpwuid. */
103 pw = getpwuid ( cur_uid );
104 old_user = ( pw ? pw->pw_name : "" );
110 openlog ( bb_applet_name, 0, LOG_AUTH );
113 pw = getpwnam ( opt_username );
115 bb_error_msg_and_die ( "user %s does not exist", opt_username );
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), but 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 ((( cur_uid == 0 ) || correct_password ( pw ))) {
124 log_su_successful(pw->pw_uid, old_user, tty );
126 log_su_failure (pw->pw_uid, old_user, tty );
127 bb_error_msg_and_die ( "incorrect password" );
130 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
134 if ( !opt_shell && opt_preserve )
135 opt_shell = getenv ( "SHELL" );
137 if ( opt_shell && cur_uid && restricted_shell ( pw-> pw_shell )) {
138 /* The user being su'd to has a nonstandard shell, and so is
139 probably a uucp account or has restricted access. Don't
140 compromise the account by allowing access with a standard
142 fputs ( "using restricted shell\n", stderr );
147 opt_shell = pw->pw_shell;
149 change_identity ( pw );
150 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
151 run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args
152 #ifdef CONFIG_SELINUX