blob: c51f26f70f0a65fc1dde85a5ecffb8203ac50272 [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Mini su implementation for busybox
Rob Landley3bfcf3c2006-07-10 03:05:46 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Denys Vlasenkoa9e25ff2010-12-31 02:52:35 +010011//usage:#define su_trivial_usage
12//usage: "[OPTIONS] [-] [USER]"
13//usage:#define su_full_usage "\n\n"
14//usage: "Run shell under USER (by default, root)\n"
Denys Vlasenkoa9e25ff2010-12-31 02:52:35 +010015//usage: "\n -,-l Clear environment, run shell as login shell"
16//usage: "\n -p,-m Do not set new $HOME, $SHELL, $USER, $LOGNAME"
17//usage: "\n -c CMD Command to pass to 'sh -c'"
18//usage: "\n -s SH Shell to use instead of user's default"
19
Denys Vlasenko26ffe812010-02-26 10:01:18 +010020#if ENABLE_FEATURE_SU_CHECKS_SHELLS
21/* Return 1 if SHELL is a restricted shell (one not returned by
Ladislav Michla73b87e2010-06-27 03:23:31 +020022 * getusershell), else 0, meaning it is a standard shell. */
Denys Vlasenko26ffe812010-02-26 10:01:18 +010023static int restricted_shell(const char *shell)
24{
25 char *line;
Ladislav Michla73b87e2010-06-27 03:23:31 +020026 int result = 1;
Denys Vlasenko26ffe812010-02-26 10:01:18 +010027
28 /*setusershell(); - getusershell does it itself*/
29 while ((line = getusershell()) != NULL) {
Ladislav Michla73b87e2010-06-27 03:23:31 +020030 if (/* *line != '#' && */ strcmp(line, shell) == 0) {
31 result = 0;
32 break;
33 }
Denys Vlasenko26ffe812010-02-26 10:01:18 +010034 }
Ladislav Michla73b87e2010-06-27 03:23:31 +020035 if (ENABLE_FEATURE_CLEAN_UP)
36 endusershell();
37 return result;
Denys Vlasenko26ffe812010-02-26 10:01:18 +010038}
39#endif
40
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000041#define SU_OPT_mp (3)
Ladislav Michla73b87e2010-06-27 03:23:31 +020042#define SU_OPT_l (4)
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000043
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000045int su_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +000046{
Denis Vlasenko15b213e2006-12-19 00:20:20 +000047 unsigned flags;
Denis Vlasenko06c0a712007-01-29 22:51:44 +000048 char *opt_shell = NULL;
49 char *opt_command = NULL;
50 const char *opt_username = "root";
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000051 struct passwd *pw;
52 uid_t cur_uid = getuid();
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000053 const char *tty;
Denys Vlasenko631fd5c2010-11-30 09:47:56 +010054#if ENABLE_FEATURE_UTMP
Ladislav Michla73b87e2010-06-27 03:23:31 +020055 char user_buf[64];
Denys Vlasenko631fd5c2010-11-30 09:47:56 +010056#endif
Ladislav Michla73b87e2010-06-27 03:23:31 +020057 const char *old_user;
Robert Griebl1fca5582002-06-04 20:45:46 +000058
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000059 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000060 //argc -= optind;
Denis Vlasenkoe13a5372006-12-23 02:59:06 +000061 argv += optind;
Robert Griebl1fca5582002-06-04 20:45:46 +000062
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000063 if (argv[0] && LONE_DASH(argv[0])) {
Bernhard Reutner-Fischerdf798b72006-06-14 16:36:45 +000064 flags |= SU_OPT_l;
Denis Vlasenko9f739442006-12-16 23:49:13 +000065 argv++;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000066 }
Robert Griebl1fca5582002-06-04 20:45:46 +000067
68 /* get user if specified */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000069 if (argv[0]) {
Denis Vlasenko9f739442006-12-16 23:49:13 +000070 opt_username = argv[0];
Denis Vlasenko9f739442006-12-16 23:49:13 +000071 argv++;
72 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000074 if (ENABLE_FEATURE_SU_SYSLOG) {
Ladislav Michla73b87e2010-06-27 03:23:31 +020075 /* The utmp entry (via getlogin) is probably the best way to
76 * identify the user, especially if someone su's from a su-shell.
Denys Vlasenkod069e532009-09-09 23:12:10 +020077 * But getlogin can fail -- usually due to lack of utmp entry.
78 * in this case resort to getpwuid. */
Denys Vlasenkod069e532009-09-09 23:12:10 +020079#if ENABLE_FEATURE_UTMP
Ladislav Michla73b87e2010-06-27 03:23:31 +020080 old_user = user_buf;
Denys Vlasenkod069e532009-09-09 23:12:10 +020081 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
82#endif
83 {
84 pw = getpwuid(cur_uid);
Ladislav Michla73b87e2010-06-27 03:23:31 +020085 old_user = pw ? xstrdup(pw->pw_name) : "";
Denys Vlasenkod069e532009-09-09 23:12:10 +020086 }
Denys Vlasenkod069e532009-09-09 23:12:10 +020087 tty = xmalloc_ttyname(2);
88 if (!tty) {
89 tty = "none";
90 }
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000091 openlog(applet_name, 0, LOG_AUTH);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000092 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000093
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000094 pw = xgetpwnam(opt_username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000095
maxwen27116ba2015-08-14 21:41:28 +020096 if (cur_uid == 0 || ask_and_check_password(pw) > 0) {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000097 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000098 syslog(LOG_NOTICE, "%c %s %s:%s",
99 '+', tty, old_user, opt_username);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +0000100 } else {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +0000101 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000102 syslog(LOG_NOTICE, "%c %s %s:%s",
103 '-', tty, old_user, opt_username);
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000104 bb_error_msg_and_die("incorrect password");
Robert Griebl1fca5582002-06-04 20:45:46 +0000105 }
106
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +0000107 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000108 closelog();
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000109 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +0000110
Ladislav Michla73b87e2010-06-27 03:23:31 +0200111 if (!opt_shell && (flags & SU_OPT_mp)) {
112 /* -s SHELL is not given, but "preserve env" opt is */
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000113 opt_shell = getenv("SHELL");
Ladislav Michla73b87e2010-06-27 03:23:31 +0200114 }
115
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000116#if ENABLE_FEATURE_SU_CHECKS_SHELLS
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100117 if (opt_shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000118 /* The user being su'd to has a nonstandard shell, and so is
Ladislav Michla73b87e2010-06-27 03:23:31 +0200119 * probably a uucp account or has restricted access. Don't
120 * compromise the account by allowing access with a standard
121 * shell. */
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000122 bb_error_msg("using restricted shell");
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100123 opt_shell = NULL; /* ignore -s PROG */
Robert Griebl1fca5582002-06-04 20:45:46 +0000124 }
Ladislav Michla73b87e2010-06-27 03:23:31 +0200125 /* else: user can run whatever he wants via "su -s PROG USER".
126 * This is safe since PROG is run under user's uid/gid. */
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000127#endif
128 if (!opt_shell)
129 opt_shell = pw->pw_shell;
Robert Griebl1fca5582002-06-04 20:45:46 +0000130
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000131 change_identity(pw);
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100132 setup_environment(opt_shell,
133 ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
Pascal Bellard0414e022012-06-12 13:21:02 +0200134 + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
135 + (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100136 pw);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000137 IF_SELINUX(set_current_security_context(NULL);)
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000138
Rob Landley18707372006-07-15 19:46:46 +0000139 /* Never returns */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000140 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000141
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000142 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000143}