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>
18 #include <sys/types.h>
24 /* The shell to run if none is given in the user's passwd entry. */
26 #define DEFAULT_SHELL "/bin/sh"
30 #define DEFAULT_USER "root"
32 /* #define SYSLOG_SUCCESS */
33 #define SYSLOG_FAILURE
36 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
37 /* Log the fact that someone has run su */
39 # if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
40 static void log_su (const char *successful, const char *old_user,
43 syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
45 # define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
46 # define log_su_failure(cu, u, tty) if(!cu) log_su("FAILED SU ", u, tty)
49 # if !defined( SYSLOG_SUCESS )
50 # define log_su_successful(cu, u, tty)
51 # define log_su_failure(cu, u, t) if(!cu) \
52 syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
54 # define log_su_successful(cu, u, t) if(!cu) \
55 syslog(LOG_NOTICE, "%s on %s", u, t)
56 # define log_su_failure(cu, u, tty)
60 /* logging not used */
61 # define log_su_successful(cu, u, tty)
62 # define log_su_failure(cu, u, tty)
66 int su_main ( int argc, char **argv )
72 char *opt_command = 0;
73 char *opt_username = DEFAULT_USER;
76 uid_t cur_uid = getuid();
78 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
83 flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
84 &opt_command, &opt_shell);
85 opt_preserve = flags & 3;
86 opt_loginshell = (flags & 4 ? 1 : 0);
88 if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
93 /* get user if specified */
95 opt_username = argv [optind++];
98 opt_args = argv + optind;
100 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
101 #ifdef CONFIG_FEATURE_UTMP
102 /* The utmp entry (via getlogin) is probably the best way to identify
103 the user, especially if someone su's from a su-shell. */
104 old_user = getlogin ( );
108 /* getlogin can fail -- usually due to lack of utmp entry.
109 Resort to getpwuid. */
110 pw = getpwuid ( cur_uid );
111 old_user = ( pw ? pw->pw_name : "" );
117 openlog ( bb_applet_name, 0, LOG_AUTH );
120 pw = getpwnam ( opt_username );
122 bb_error_msg_and_die ( "user %s does not exist", opt_username );
124 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
125 is a username that is retrieved via NIS (YP), but that doesn't have
126 a default shell listed. */
127 if ( !pw->pw_shell || !pw->pw_shell [0] )
128 pw->pw_shell = (char *) DEFAULT_SHELL;
130 if ((( cur_uid == 0 ) || correct_password ( pw ))) {
131 log_su_successful(pw->pw_uid, old_user, tty );
133 log_su_failure (pw->pw_uid, old_user, tty );
134 bb_error_msg_and_die ( "incorrect password" );
137 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
141 if ( !opt_shell && opt_preserve )
142 opt_shell = getenv ( "SHELL" );
144 if ( opt_shell && cur_uid && restricted_shell ( pw->pw_shell )) {
145 /* The user being su'd to has a nonstandard shell, and so is
146 probably a uucp account or has restricted access. Don't
147 compromise the account by allowing access with a standard
149 fputs ( "using restricted shell\n", stderr );
154 opt_shell = pw->pw_shell;
156 change_identity ( pw );
157 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
159 set_current_security_context(NULL);
161 run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args);