blob: 1a35f0e4fcee3a68304b0f5969d6bb4664103f0a [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischer5a620ea2006-01-19 18:04:15 +00002/*
Rob Landley3bfcf3c2006-07-10 03:05:46 +00003 * Mini su implementation for busybox
4 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00005 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Rob Landley3bfcf3c2006-07-10 03:05:46 +00006 */
Robert Griebl1fca5582002-06-04 20:45:46 +00007
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00008#include "libbb.h"
Rob Landley3bfcf3c2006-07-10 03:05:46 +00009#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000010
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000011#define SU_OPT_mp (3)
12#define SU_OPT_l (4)
13
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000015int su_main(int argc ATTRIBUTE_UNUSED, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +000016{
Denis Vlasenko15b213e2006-12-19 00:20:20 +000017 unsigned flags;
Denis Vlasenko06c0a712007-01-29 22:51:44 +000018 char *opt_shell = NULL;
19 char *opt_command = NULL;
20 const char *opt_username = "root";
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000021 struct passwd *pw;
22 uid_t cur_uid = getuid();
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000023 const char *tty;
Rob Landley3bfcf3c2006-07-10 03:05:46 +000024 char *old_user;
Robert Griebl1fca5582002-06-04 20:45:46 +000025
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000026 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000027 //argc -= optind;
Denis Vlasenkoe13a5372006-12-23 02:59:06 +000028 argv += optind;
Robert Griebl1fca5582002-06-04 20:45:46 +000029
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000030 if (argv[0] && LONE_DASH(argv[0])) {
Bernhard Reutner-Fischerdf798b72006-06-14 16:36:45 +000031 flags |= SU_OPT_l;
Denis Vlasenko9f739442006-12-16 23:49:13 +000032 argv++;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000033 }
Robert Griebl1fca5582002-06-04 20:45:46 +000034
35 /* get user if specified */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000036 if (argv[0]) {
Denis Vlasenko9f739442006-12-16 23:49:13 +000037 opt_username = argv[0];
Denis Vlasenko9f739442006-12-16 23:49:13 +000038 argv++;
39 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000040
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000041 if (ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +000042 /* The utmp entry (via getlogin) is probably the best way to identify
43 the user, especially if someone su's from a su-shell.
44 But getlogin can fail -- usually due to lack of utmp entry.
45 in this case resort to getpwuid. */
Rob Landleyd921b2e2006-08-03 15:41:12 +000046 old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
Rob Landley3bfcf3c2006-07-10 03:05:46 +000047 tty = ttyname(2) ? : "none";
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000048 openlog(applet_name, 0, LOG_AUTH);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000049 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000050
Rob Landley3bfcf3c2006-07-10 03:05:46 +000051 pw = getpwnam(opt_username);
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000052 if (!pw)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000053 bb_error_msg_and_die("unknown id: %s", opt_username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Robert Griebl1fca5582002-06-04 20:45:46 +000055 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
56 is a username that is retrieved via NIS (YP), but that doesn't have
57 a default shell listed. */
Denis Vlasenko15b213e2006-12-19 00:20:20 +000058 if (!pw->pw_shell || !pw->pw_shell[0])
59 pw->pw_shell = (char *)DEFAULT_SHELL;
Robert Griebl1fca5582002-06-04 20:45:46 +000060
Rob Landley3bfcf3c2006-07-10 03:05:46 +000061 if ((cur_uid == 0) || correct_password(pw)) {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000062 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000063 syslog(LOG_NOTICE, "%c %s %s:%s",
64 '+', tty, old_user, opt_username);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000065 } else {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000066 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000067 syslog(LOG_NOTICE, "%c %s %s:%s",
68 '-', tty, old_user, opt_username);
Rob Landley3bfcf3c2006-07-10 03:05:46 +000069 bb_error_msg_and_die("incorrect password");
Robert Griebl1fca5582002-06-04 20:45:46 +000070 }
71
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000072 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +000073 closelog();
74 free(old_user);
75 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000076
Denis Vlasenko15b213e2006-12-19 00:20:20 +000077 if (!opt_shell && (flags & SU_OPT_mp))
78 opt_shell = getenv("SHELL");
Robert Griebl1fca5582002-06-04 20:45:46 +000079
Denis Vlasenko15b213e2006-12-19 00:20:20 +000080#if ENABLE_FEATURE_SU_CHECKS_SHELLS
Rob Landley3bfcf3c2006-07-10 03:05:46 +000081 if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
Robert Griebl1fca5582002-06-04 20:45:46 +000082 /* The user being su'd to has a nonstandard shell, and so is
83 probably a uucp account or has restricted access. Don't
84 compromise the account by allowing access with a standard
85 shell. */
Rob Landley3bfcf3c2006-07-10 03:05:46 +000086 bb_error_msg("using restricted shell");
Denis Vlasenkoa2f61012007-09-10 13:15:28 +000087 opt_shell = NULL;
Robert Griebl1fca5582002-06-04 20:45:46 +000088 }
Denis Vlasenko15b213e2006-12-19 00:20:20 +000089#endif
90 if (!opt_shell)
91 opt_shell = pw->pw_shell;
Robert Griebl1fca5582002-06-04 20:45:46 +000092
Rob Landley3bfcf3c2006-07-10 03:05:46 +000093 change_identity(pw);
Denis Vlasenkocdf62772008-03-17 08:42:43 +000094 /* setup_environment params: shell, clear_env, change_env, pw */
Rob Landley3bfcf3c2006-07-10 03:05:46 +000095 setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
Denis Vlasenkoa9801652006-09-07 16:20:03 +000096 USE_SELINUX(set_current_security_context(NULL);)
Rob Landley3bfcf3c2006-07-10 03:05:46 +000097
Rob Landley18707372006-07-15 19:46:46 +000098 /* Never returns */
Denis Vlasenko9f739442006-12-16 23:49:13 +000099 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000101 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000102}