2002-06-04 20:45:46 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-19 18:04:15 +00:00
|
|
|
/*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Mini su implementation for busybox
|
2006-07-10 03:05:46 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2006-07-10 03:05:46 +00:00
|
|
|
*/
|
2002-06-04 20:45:46 +00:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2002-06-04 20:45:46 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
2010-12-31 01:52:35 +00:00
|
|
|
//usage:#define su_trivial_usage
|
|
|
|
//usage: "[OPTIONS] [-] [USER]"
|
|
|
|
//usage:#define su_full_usage "\n\n"
|
|
|
|
//usage: "Run shell under USER (by default, root)\n"
|
|
|
|
//usage: "\n -,-l Clear environment, run shell as login shell"
|
|
|
|
//usage: "\n -p,-m Do not set new $HOME, $SHELL, $USER, $LOGNAME"
|
|
|
|
//usage: "\n -c CMD Command to pass to 'sh -c'"
|
|
|
|
//usage: "\n -s SH Shell to use instead of user's default"
|
|
|
|
|
2010-02-26 09:01:18 +00:00
|
|
|
#if ENABLE_FEATURE_SU_CHECKS_SHELLS
|
|
|
|
/* Return 1 if SHELL is a restricted shell (one not returned by
|
2010-06-27 01:23:31 +00:00
|
|
|
* getusershell), else 0, meaning it is a standard shell. */
|
2010-02-26 09:01:18 +00:00
|
|
|
static int restricted_shell(const char *shell)
|
|
|
|
{
|
|
|
|
char *line;
|
2010-06-27 01:23:31 +00:00
|
|
|
int result = 1;
|
2010-02-26 09:01:18 +00:00
|
|
|
|
|
|
|
/*setusershell(); - getusershell does it itself*/
|
|
|
|
while ((line = getusershell()) != NULL) {
|
2010-06-27 01:23:31 +00:00
|
|
|
if (/* *line != '#' && */ strcmp(line, shell) == 0) {
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-26 09:01:18 +00:00
|
|
|
}
|
2010-06-27 01:23:31 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
endusershell();
|
|
|
|
return result;
|
2010-02-26 09:01:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-19 08:55:38 +00:00
|
|
|
#define SU_OPT_mp (3)
|
2010-06-27 01:23:31 +00:00
|
|
|
#define SU_OPT_l (4)
|
2006-12-19 08:55:38 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int su_main(int argc UNUSED_PARAM, char **argv)
|
2002-06-04 20:45:46 +00:00
|
|
|
{
|
2006-12-19 00:20:20 +00:00
|
|
|
unsigned flags;
|
2007-01-29 22:51:44 +00:00
|
|
|
char *opt_shell = NULL;
|
|
|
|
char *opt_command = NULL;
|
|
|
|
const char *opt_username = "root";
|
2003-08-29 07:38:56 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
uid_t cur_uid = getuid();
|
|
|
|
const char *tty;
|
2010-11-30 08:47:56 +00:00
|
|
|
#if ENABLE_FEATURE_UTMP
|
2010-06-27 01:23:31 +00:00
|
|
|
char user_buf[64];
|
2010-11-30 08:47:56 +00:00
|
|
|
#endif
|
2010-06-27 01:23:31 +00:00
|
|
|
const char *old_user;
|
2002-06-04 20:45:46 +00:00
|
|
|
|
2007-08-18 15:32:12 +00:00
|
|
|
flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
|
2008-03-17 09:07:36 +00:00
|
|
|
//argc -= optind;
|
2006-12-23 02:59:06 +00:00
|
|
|
argv += optind;
|
2003-08-29 07:38:56 +00:00
|
|
|
|
2008-03-17 09:07:36 +00:00
|
|
|
if (argv[0] && LONE_DASH(argv[0])) {
|
2006-06-14 16:36:45 +00:00
|
|
|
flags |= SU_OPT_l;
|
2006-12-16 23:49:13 +00:00
|
|
|
argv++;
|
2006-09-07 16:20:03 +00:00
|
|
|
}
|
2002-06-04 20:45:46 +00:00
|
|
|
|
|
|
|
/* get user if specified */
|
2008-03-17 09:07:36 +00:00
|
|
|
if (argv[0]) {
|
2006-12-16 23:49:13 +00:00
|
|
|
opt_username = argv[0];
|
|
|
|
argv++;
|
|
|
|
}
|
2006-07-10 03:05:46 +00:00
|
|
|
|
2006-12-19 08:55:38 +00:00
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG) {
|
2010-06-27 01:23:31 +00:00
|
|
|
/* The utmp entry (via getlogin) is probably the best way to
|
|
|
|
* identify the user, especially if someone su's from a su-shell.
|
2009-09-09 21:12:10 +00:00
|
|
|
* But getlogin can fail -- usually due to lack of utmp entry.
|
|
|
|
* in this case resort to getpwuid. */
|
|
|
|
#if ENABLE_FEATURE_UTMP
|
2010-06-27 01:23:31 +00:00
|
|
|
old_user = user_buf;
|
2009-09-09 21:12:10 +00:00
|
|
|
if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
pw = getpwuid(cur_uid);
|
2010-06-27 01:23:31 +00:00
|
|
|
old_user = pw ? xstrdup(pw->pw_name) : "";
|
2009-09-09 21:12:10 +00:00
|
|
|
}
|
|
|
|
tty = xmalloc_ttyname(2);
|
|
|
|
if (!tty) {
|
|
|
|
tty = "none";
|
|
|
|
}
|
2006-10-03 21:00:43 +00:00
|
|
|
openlog(applet_name, 0, LOG_AUTH);
|
2003-08-29 07:38:56 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 19:05:55 +00:00
|
|
|
pw = xgetpwnam(opt_username);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2013-11-19 12:09:06 +00:00
|
|
|
if (cur_uid == 0 || ask_and_check_password(pw) > 0) {
|
2006-12-19 08:55:38 +00:00
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG)
|
2006-12-19 00:20:20 +00:00
|
|
|
syslog(LOG_NOTICE, "%c %s %s:%s",
|
|
|
|
'+', tty, old_user, opt_username);
|
2003-08-29 07:38:56 +00:00
|
|
|
} else {
|
2006-12-19 08:55:38 +00:00
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG)
|
2006-12-19 00:20:20 +00:00
|
|
|
syslog(LOG_NOTICE, "%c %s %s:%s",
|
|
|
|
'-', tty, old_user, opt_username);
|
2014-03-16 10:18:19 +00:00
|
|
|
bb_do_delay(LOGIN_FAIL_DELAY);
|
2006-07-10 03:05:46 +00:00
|
|
|
bb_error_msg_and_die("incorrect password");
|
2002-06-04 20:45:46 +00:00
|
|
|
}
|
|
|
|
|
2006-12-19 08:55:38 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
|
2006-07-10 03:05:46 +00:00
|
|
|
closelog();
|
|
|
|
}
|
2003-08-29 07:38:56 +00:00
|
|
|
|
2010-06-27 01:23:31 +00:00
|
|
|
if (!opt_shell && (flags & SU_OPT_mp)) {
|
|
|
|
/* -s SHELL is not given, but "preserve env" opt is */
|
2006-12-19 00:20:20 +00:00
|
|
|
opt_shell = getenv("SHELL");
|
2010-06-27 01:23:31 +00:00
|
|
|
}
|
|
|
|
|
2006-12-19 00:20:20 +00:00
|
|
|
#if ENABLE_FEATURE_SU_CHECKS_SHELLS
|
2011-03-06 17:49:40 +00:00
|
|
|
if (opt_shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
|
2002-06-04 20:45:46 +00:00
|
|
|
/* The user being su'd to has a nonstandard shell, and so is
|
2010-06-27 01:23:31 +00:00
|
|
|
* probably a uucp account or has restricted access. Don't
|
|
|
|
* compromise the account by allowing access with a standard
|
|
|
|
* shell. */
|
2006-07-10 03:05:46 +00:00
|
|
|
bb_error_msg("using restricted shell");
|
2011-03-06 17:49:40 +00:00
|
|
|
opt_shell = NULL; /* ignore -s PROG */
|
2002-06-04 20:45:46 +00:00
|
|
|
}
|
2010-06-27 01:23:31 +00:00
|
|
|
/* else: user can run whatever he wants via "su -s PROG USER".
|
|
|
|
* This is safe since PROG is run under user's uid/gid. */
|
2006-12-19 00:20:20 +00:00
|
|
|
#endif
|
|
|
|
if (!opt_shell)
|
|
|
|
opt_shell = pw->pw_shell;
|
2006-07-10 03:05:46 +00:00
|
|
|
|
|
|
|
change_identity(pw);
|
2010-02-26 08:52:45 +00:00
|
|
|
setup_environment(opt_shell,
|
|
|
|
((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
|
2012-06-12 11:21:02 +00:00
|
|
|
+ (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
|
|
|
|
+ (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
|
2010-02-26 08:52:45 +00:00
|
|
|
pw);
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_SELINUX(set_current_security_context(NULL);)
|
2002-06-04 20:45:46 +00:00
|
|
|
|
2006-07-15 19:46:46 +00:00
|
|
|
/* Never returns */
|
2006-12-16 23:49:13 +00:00
|
|
|
run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-09-10 13:15:28 +00:00
|
|
|
/* return EXIT_FAILURE; - not reached */
|
2002-06-04 20:45:46 +00:00
|
|
|
}
|