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_USER "root"
26 //#define SYSLOG_SUCCESS
27 #define SYSLOG_FAILURE
30 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
31 /* Log the fact that someone has run su */
33 # if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
34 static void log_su (const char *successful, const char *old_user, const char *tty)
36 syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
38 # define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
39 # define log_su_failure(cu, u, tty) if(!cu) log_su("FAILED SU ", u, tty)
42 # if !defined( SYSLOG_SUCESS )
43 # define log_su_successful(cu, u, tty)
44 # define log_su_failure(cu, u, t) if(!cu) \
45 syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
47 # define log_su_successful(cu, u, t) if(!cu) \
48 syslog(LOG_NOTICE, "%s on %s", u, t)
49 # define log_su_failure(cu, u, tty)
53 /* logging not used */
54 # define log_su_successful(cu, u, tty)
55 # define log_su_failure(cu, u, tty)
59 int su_main ( int argc, char **argv )
65 char *opt_command = 0;
66 char *opt_username = DEFAULT_USER;
69 uid_t cur_uid = getuid();
71 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
76 flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
77 &opt_command, &opt_shell);
78 opt_preserve = flags & 3;
79 opt_loginshell = (flags & 4 ? 1 : 0);
81 if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
86 /* get user if specified */
88 opt_username = argv [optind++];
91 opt_args = argv + optind;
93 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
94 #ifdef CONFIG_FEATURE_U_W_TMP
95 /* The utmp entry (via getlogin) is probably the best way to identify
96 the user, especially if someone su's from a su-shell. */
97 old_user = getlogin ( );
101 /* getlogin can fail -- usually due to lack of utmp entry. Resort to getpwuid. */
102 pw = getpwuid ( cur_uid );
103 old_user = ( pw ? pw->pw_name : "" );
109 openlog ( bb_applet_name, 0, LOG_AUTH );
112 pw = getpwnam ( opt_username );
114 bb_error_msg_and_die ( "user %s does not exist", opt_username );
116 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
117 is a username that is retrieved via NIS (YP), but that doesn't have
118 a default shell listed. */
119 if ( !pw-> pw_shell || !pw->pw_shell [0] )
120 pw-> pw_shell = (char *) DEFAULT_SHELL;
122 if ((( cur_uid == 0 ) || correct_password ( pw ))) {
123 log_su_successful(pw->pw_uid, old_user, tty );
125 log_su_failure (pw->pw_uid, old_user, tty );
126 bb_error_msg_and_die ( "incorrect password" );
129 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
133 if ( !opt_shell && opt_preserve )
134 opt_shell = getenv ( "SHELL" );
136 if ( opt_shell && cur_uid && restricted_shell ( pw-> pw_shell )) {
137 /* The user being su'd to has a nonstandard shell, and so is
138 probably a uucp account or has restricted access. Don't
139 compromise the account by allowing access with a standard
141 fputs ( "using restricted shell\n", stderr );
146 opt_shell = pw->pw_shell;
148 change_identity ( pw );
149 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
150 run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args
151 #ifdef CONFIG_SELINUX