blob: c31e7e771276bf544aa186932629e8474bade3ad [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 Vlasenko26ffe812010-02-26 10:01:18 +010011#if ENABLE_FEATURE_SU_CHECKS_SHELLS
12/* Return 1 if SHELL is a restricted shell (one not returned by
Ladislav Michla73b87e2010-06-27 03:23:31 +020013 * getusershell), else 0, meaning it is a standard shell. */
Denys Vlasenko26ffe812010-02-26 10:01:18 +010014static int restricted_shell(const char *shell)
15{
16 char *line;
Ladislav Michla73b87e2010-06-27 03:23:31 +020017 int result = 1;
Denys Vlasenko26ffe812010-02-26 10:01:18 +010018
19 /*setusershell(); - getusershell does it itself*/
20 while ((line = getusershell()) != NULL) {
Ladislav Michla73b87e2010-06-27 03:23:31 +020021 if (/* *line != '#' && */ strcmp(line, shell) == 0) {
22 result = 0;
23 break;
24 }
Denys Vlasenko26ffe812010-02-26 10:01:18 +010025 }
Ladislav Michla73b87e2010-06-27 03:23:31 +020026 if (ENABLE_FEATURE_CLEAN_UP)
27 endusershell();
28 return result;
Denys Vlasenko26ffe812010-02-26 10:01:18 +010029}
30#endif
31
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000032#define SU_OPT_mp (3)
Ladislav Michla73b87e2010-06-27 03:23:31 +020033#define SU_OPT_l (4)
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000034
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000035int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000036int su_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +000037{
Denis Vlasenko15b213e2006-12-19 00:20:20 +000038 unsigned flags;
Denis Vlasenko06c0a712007-01-29 22:51:44 +000039 char *opt_shell = NULL;
40 char *opt_command = NULL;
41 const char *opt_username = "root";
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000042 struct passwd *pw;
43 uid_t cur_uid = getuid();
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000044 const char *tty;
Denys Vlasenko631fd5c2010-11-30 09:47:56 +010045#if ENABLE_FEATURE_UTMP
Ladislav Michla73b87e2010-06-27 03:23:31 +020046 char user_buf[64];
Denys Vlasenko631fd5c2010-11-30 09:47:56 +010047#endif
Ladislav Michla73b87e2010-06-27 03:23:31 +020048 const char *old_user;
Robert Griebl1fca5582002-06-04 20:45:46 +000049
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000050 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000051 //argc -= optind;
Denis Vlasenkoe13a5372006-12-23 02:59:06 +000052 argv += optind;
Robert Griebl1fca5582002-06-04 20:45:46 +000053
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000054 if (argv[0] && LONE_DASH(argv[0])) {
Bernhard Reutner-Fischerdf798b72006-06-14 16:36:45 +000055 flags |= SU_OPT_l;
Denis Vlasenko9f739442006-12-16 23:49:13 +000056 argv++;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000057 }
Robert Griebl1fca5582002-06-04 20:45:46 +000058
59 /* get user if specified */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000060 if (argv[0]) {
Denis Vlasenko9f739442006-12-16 23:49:13 +000061 opt_username = argv[0];
Denis Vlasenko9f739442006-12-16 23:49:13 +000062 argv++;
63 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000065 if (ENABLE_FEATURE_SU_SYSLOG) {
Ladislav Michla73b87e2010-06-27 03:23:31 +020066 /* The utmp entry (via getlogin) is probably the best way to
67 * identify the user, especially if someone su's from a su-shell.
Denys Vlasenkod069e532009-09-09 23:12:10 +020068 * But getlogin can fail -- usually due to lack of utmp entry.
69 * in this case resort to getpwuid. */
Denys Vlasenkod069e532009-09-09 23:12:10 +020070#if ENABLE_FEATURE_UTMP
Ladislav Michla73b87e2010-06-27 03:23:31 +020071 old_user = user_buf;
Denys Vlasenkod069e532009-09-09 23:12:10 +020072 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
73#endif
74 {
75 pw = getpwuid(cur_uid);
Ladislav Michla73b87e2010-06-27 03:23:31 +020076 old_user = pw ? xstrdup(pw->pw_name) : "";
Denys Vlasenkod069e532009-09-09 23:12:10 +020077 }
Denys Vlasenkod069e532009-09-09 23:12:10 +020078 tty = xmalloc_ttyname(2);
79 if (!tty) {
80 tty = "none";
81 }
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000082 openlog(applet_name, 0, LOG_AUTH);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000083 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000084
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000085 pw = xgetpwnam(opt_username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086
Ladislav Michla73b87e2010-06-27 03:23:31 +020087 if (cur_uid == 0 || correct_password(pw)) {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000088 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000089 syslog(LOG_NOTICE, "%c %s %s:%s",
90 '+', tty, old_user, opt_username);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000091 } else {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000092 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000093 syslog(LOG_NOTICE, "%c %s %s:%s",
94 '-', tty, old_user, opt_username);
Rob Landley3bfcf3c2006-07-10 03:05:46 +000095 bb_error_msg_and_die("incorrect password");
Robert Griebl1fca5582002-06-04 20:45:46 +000096 }
97
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000098 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +000099 closelog();
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000100 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +0000101
Ladislav Michla73b87e2010-06-27 03:23:31 +0200102 if (!opt_shell && (flags & SU_OPT_mp)) {
103 /* -s SHELL is not given, but "preserve env" opt is */
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000104 opt_shell = getenv("SHELL");
Ladislav Michla73b87e2010-06-27 03:23:31 +0200105 }
106
107 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
108 * is a username that is retrieved via NIS (YP), that doesn't have
109 * a default shell listed. */
110 if (!pw->pw_shell || !pw->pw_shell[0])
111 pw->pw_shell = (char *)DEFAULT_SHELL;
Robert Griebl1fca5582002-06-04 20:45:46 +0000112
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000113#if ENABLE_FEATURE_SU_CHECKS_SHELLS
Denys Vlasenko26ffe812010-02-26 10:01:18 +0100114 if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000115 /* The user being su'd to has a nonstandard shell, and so is
Ladislav Michla73b87e2010-06-27 03:23:31 +0200116 * probably a uucp account or has restricted access. Don't
117 * compromise the account by allowing access with a standard
118 * shell. */
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000119 bb_error_msg("using restricted shell");
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000120 opt_shell = NULL;
Robert Griebl1fca5582002-06-04 20:45:46 +0000121 }
Ladislav Michla73b87e2010-06-27 03:23:31 +0200122 /* else: user can run whatever he wants via "su -s PROG USER".
123 * This is safe since PROG is run under user's uid/gid. */
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000124#endif
125 if (!opt_shell)
126 opt_shell = pw->pw_shell;
Robert Griebl1fca5582002-06-04 20:45:46 +0000127
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000128 change_identity(pw);
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100129 setup_environment(opt_shell,
130 ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
131 + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
132 pw);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000133 IF_SELINUX(set_current_security_context(NULL);)
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000134
Rob Landley18707372006-07-15 19:46:46 +0000135 /* Never returns */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000136 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000138 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000139}