2002-06-05 02:15:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-19 23:34:15 +05:30
|
|
|
/*
|
2006-07-10 08:35:46 +05:30
|
|
|
* Mini su implementation for busybox
|
|
|
|
*
|
|
|
|
* Licensed under the GPL v2, see the file LICENSE in this tarball.
|
|
|
|
*/
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
#include "busybox.h"
|
2002-06-05 02:15:46 +05:30
|
|
|
#include <syslog.h>
|
|
|
|
|
2006-09-07 21:50:03 +05:30
|
|
|
int su_main(int argc, char **argv)
|
2002-06-05 02:15:46 +05:30
|
|
|
{
|
2003-08-29 13:08:56 +05:30
|
|
|
unsigned long flags;
|
2002-06-05 02:15:46 +05:30
|
|
|
char *opt_shell = 0;
|
|
|
|
char *opt_command = 0;
|
2006-07-10 08:35:46 +05:30
|
|
|
char *opt_username = "root";
|
2002-06-05 02:15:46 +05:30
|
|
|
char **opt_args = 0;
|
2003-08-29 13:08:56 +05:30
|
|
|
struct passwd *pw;
|
|
|
|
uid_t cur_uid = getuid();
|
|
|
|
const char *tty;
|
2006-07-10 08:35:46 +05:30
|
|
|
char *old_user;
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
flags = bb_getopt_ulflags(argc, argv, "mplc:s:", &opt_command, &opt_shell);
|
|
|
|
#define SU_OPT_mp (3)
|
2006-06-14 22:06:45 +05:30
|
|
|
#define SU_OPT_l (4)
|
2003-08-29 13:08:56 +05:30
|
|
|
|
|
|
|
if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
|
2006-06-14 22:06:45 +05:30
|
|
|
flags |= SU_OPT_l;
|
2002-06-05 02:15:46 +05:30
|
|
|
++optind;
|
2006-09-07 21:50:03 +05:30
|
|
|
}
|
2002-06-05 02:15:46 +05:30
|
|
|
|
|
|
|
/* get user if specified */
|
2006-07-10 08:35:46 +05:30
|
|
|
if (optind < argc) opt_username = argv [optind++];
|
|
|
|
|
|
|
|
if (optind < argc) opt_args = argv + optind;
|
|
|
|
|
|
|
|
if (ENABLE_SU_SYSLOG) {
|
|
|
|
/* The utmp entry (via getlogin) is probably the best way to identify
|
|
|
|
the user, especially if someone su's from a su-shell.
|
|
|
|
But getlogin can fail -- usually due to lack of utmp entry.
|
|
|
|
in this case resort to getpwuid. */
|
2006-08-03 21:11:12 +05:30
|
|
|
old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
|
2006-07-10 08:35:46 +05:30
|
|
|
tty = ttyname(2) ? : "none";
|
|
|
|
openlog(bb_applet_name, 0, LOG_AUTH);
|
2003-08-29 13:08:56 +05:30
|
|
|
}
|
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
pw = getpwnam(opt_username);
|
|
|
|
if (!pw) bb_error_msg_and_die("Unknown id: %s", opt_username);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-06-05 02:15:46 +05:30
|
|
|
/* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
|
|
|
|
is a username that is retrieved via NIS (YP), but that doesn't have
|
|
|
|
a default shell listed. */
|
2006-07-10 08:35:46 +05:30
|
|
|
if (!pw->pw_shell || !pw->pw_shell[0]) pw->pw_shell = (char *)DEFAULT_SHELL;
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
if ((cur_uid == 0) || correct_password(pw)) {
|
|
|
|
if (ENABLE_SU_SYSLOG)
|
|
|
|
syslog(LOG_NOTICE, "+ %s %s:%s", tty, old_user, opt_username);
|
2003-08-29 13:08:56 +05:30
|
|
|
} else {
|
2006-07-10 08:35:46 +05:30
|
|
|
if (ENABLE_SU_SYSLOG)
|
|
|
|
syslog(LOG_NOTICE, "- %s %s:%s", tty, old_user, opt_username);
|
|
|
|
bb_error_msg_and_die("incorrect password");
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP && ENABLE_SU_SYSLOG) {
|
|
|
|
closelog();
|
|
|
|
free(old_user);
|
|
|
|
}
|
2003-08-29 13:08:56 +05:30
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
if (!opt_shell && (flags & SU_OPT_mp)) opt_shell = getenv("SHELL");
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
|
2002-06-05 02:15:46 +05:30
|
|
|
/* The user being su'd to has a nonstandard shell, and so is
|
|
|
|
probably a uucp account or has restricted access. Don't
|
|
|
|
compromise the account by allowing access with a standard
|
|
|
|
shell. */
|
2006-07-10 08:35:46 +05:30
|
|
|
bb_error_msg("using restricted shell");
|
2002-06-05 02:15:46 +05:30
|
|
|
opt_shell = 0;
|
|
|
|
}
|
|
|
|
|
2006-07-10 08:35:46 +05:30
|
|
|
if (!opt_shell) opt_shell = pw->pw_shell;
|
|
|
|
|
|
|
|
change_identity(pw);
|
|
|
|
setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
|
2006-09-07 21:50:03 +05:30
|
|
|
USE_SELINUX(set_current_security_context(NULL);)
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2006-07-16 01:16:46 +05:30
|
|
|
/* Never returns */
|
2006-06-14 22:06:45 +05:30
|
|
|
run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-06-05 02:15:46 +05:30
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|