2002-06-04 20:45:46 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-19 18:04:15 +00:00
|
|
|
/*
|
2006-07-10 03:05:46 +00:00
|
|
|
* Mini su implementation for busybox
|
|
|
|
*
|
2006-09-13 16:39:19 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
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>
|
|
|
|
|
2006-12-19 08:55:38 +00:00
|
|
|
#define SU_OPT_mp (3)
|
|
|
|
#define SU_OPT_l (4)
|
|
|
|
|
2007-02-03 17:28:39 +00:00
|
|
|
int su_main(int argc, char **argv);
|
2006-09-07 16:20:03 +00:00
|
|
|
int su_main(int argc, 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;
|
2006-07-10 03:05:46 +00:00
|
|
|
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);
|
2006-12-16 23:49:13 +00:00
|
|
|
argc -= optind;
|
2006-12-23 02:59:06 +00:00
|
|
|
argv += optind;
|
2003-08-29 07:38:56 +00:00
|
|
|
|
2006-12-16 23:49:13 +00:00
|
|
|
if (argc && LONE_DASH(argv[0])) {
|
2006-06-14 16:36:45 +00:00
|
|
|
flags |= SU_OPT_l;
|
2006-12-16 23:49:13 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2006-09-07 16:20:03 +00:00
|
|
|
}
|
2002-06-04 20:45:46 +00:00
|
|
|
|
|
|
|
/* get user if specified */
|
2006-12-16 23:49:13 +00:00
|
|
|
if (argc) {
|
|
|
|
opt_username = argv[0];
|
2007-09-10 13:15:28 +00:00
|
|
|
//argc--; - not used below anyway
|
2006-12-16 23:49:13 +00:00
|
|
|
argv++;
|
|
|
|
}
|
2006-07-10 03:05:46 +00:00
|
|
|
|
2006-12-19 08:55:38 +00:00
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG) {
|
2006-07-10 03:05:46 +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.
|
|
|
|
But getlogin can fail -- usually due to lack of utmp entry.
|
|
|
|
in this case resort to getpwuid. */
|
2006-08-03 15:41:12 +00:00
|
|
|
old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
|
2006-07-10 03:05:46 +00:00
|
|
|
tty = ttyname(2) ? : "none";
|
2006-10-03 21:00:43 +00:00
|
|
|
openlog(applet_name, 0, LOG_AUTH);
|
2003-08-29 07:38:56 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 03:05:46 +00:00
|
|
|
pw = getpwnam(opt_username);
|
2006-12-19 08:55:38 +00:00
|
|
|
if (!pw)
|
2006-12-19 00:20:20 +00:00
|
|
|
bb_error_msg_and_die("unknown id: %s", opt_username);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2002-06-04 20:45:46 +00:00
|
|
|
/* 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-12-19 00:20:20 +00:00
|
|
|
if (!pw->pw_shell || !pw->pw_shell[0])
|
|
|
|
pw->pw_shell = (char *)DEFAULT_SHELL;
|
2002-06-04 20:45:46 +00:00
|
|
|
|
2006-07-10 03:05:46 +00:00
|
|
|
if ((cur_uid == 0) || correct_password(pw)) {
|
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);
|
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();
|
|
|
|
free(old_user);
|
|
|
|
}
|
2003-08-29 07:38:56 +00:00
|
|
|
|
2006-12-19 00:20:20 +00:00
|
|
|
if (!opt_shell && (flags & SU_OPT_mp))
|
|
|
|
opt_shell = getenv("SHELL");
|
2002-06-04 20:45:46 +00:00
|
|
|
|
2006-12-19 00:20:20 +00:00
|
|
|
#if ENABLE_FEATURE_SU_CHECKS_SHELLS
|
2006-07-10 03:05:46 +00:00
|
|
|
if (opt_shell && cur_uid && 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
|
|
|
|
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");
|
2007-09-10 13:15:28 +00:00
|
|
|
opt_shell = NULL;
|
2002-06-04 20:45:46 +00:00
|
|
|
}
|
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);
|
2007-09-10 13:15:28 +00:00
|
|
|
/* setup_environment params: shell, loginshell, changeenv, pw */
|
2006-07-10 03:05:46 +00:00
|
|
|
setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
|
2006-09-07 16:20:03 +00:00
|
|
|
USE_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
|
|
|
}
|