blob: af25655fd4986ca4eba4523917c42b9e26a0ceb1 [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
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
13 getusershell), else 0, meaning it is a standard shell. */
14static int restricted_shell(const char *shell)
15{
16 char *line;
17
18 /*setusershell(); - getusershell does it itself*/
19 while ((line = getusershell()) != NULL) {
20 if (/* *line != '#' && */ strcmp(line, shell) == 0)
21 return 0;
22 }
23 endusershell();
24 return 1;
25}
26#endif
27
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000028#define SU_OPT_mp (3)
29#define SU_OPT_l (4)
30
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000031int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000032int su_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +000033{
Denis Vlasenko15b213e2006-12-19 00:20:20 +000034 unsigned flags;
Denis Vlasenko06c0a712007-01-29 22:51:44 +000035 char *opt_shell = NULL;
36 char *opt_command = NULL;
37 const char *opt_username = "root";
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000038 struct passwd *pw;
39 uid_t cur_uid = getuid();
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000040 const char *tty;
Rob Landley3bfcf3c2006-07-10 03:05:46 +000041 char *old_user;
Robert Griebl1fca5582002-06-04 20:45:46 +000042
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000043 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000044 //argc -= optind;
Denis Vlasenkoe13a5372006-12-23 02:59:06 +000045 argv += optind;
Robert Griebl1fca5582002-06-04 20:45:46 +000046
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000047 if (argv[0] && LONE_DASH(argv[0])) {
Bernhard Reutner-Fischerdf798b72006-06-14 16:36:45 +000048 flags |= SU_OPT_l;
Denis Vlasenko9f739442006-12-16 23:49:13 +000049 argv++;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000050 }
Robert Griebl1fca5582002-06-04 20:45:46 +000051
52 /* get user if specified */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000053 if (argv[0]) {
Denis Vlasenko9f739442006-12-16 23:49:13 +000054 opt_username = argv[0];
Denis Vlasenko9f739442006-12-16 23:49:13 +000055 argv++;
56 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000058 if (ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +000059 /* The utmp entry (via getlogin) is probably the best way to identify
Denys Vlasenkod069e532009-09-09 23:12:10 +020060 * the user, especially if someone su's from a su-shell.
61 * But getlogin can fail -- usually due to lack of utmp entry.
62 * in this case resort to getpwuid. */
63 const char *user;
64#if ENABLE_FEATURE_UTMP
65 char user_buf[64];
66 user = user_buf;
67 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
68#endif
69 {
70 pw = getpwuid(cur_uid);
71 user = pw ? pw->pw_name : "";
72 }
73 old_user = xstrdup(user);
74 tty = xmalloc_ttyname(2);
75 if (!tty) {
76 tty = "none";
77 }
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000078 openlog(applet_name, 0, LOG_AUTH);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000079 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000080
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000081 pw = xgetpwnam(opt_username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082
Robert Griebl1fca5582002-06-04 20:45:46 +000083 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
84 is a username that is retrieved via NIS (YP), but that doesn't have
85 a default shell listed. */
Denis Vlasenko15b213e2006-12-19 00:20:20 +000086 if (!pw->pw_shell || !pw->pw_shell[0])
87 pw->pw_shell = (char *)DEFAULT_SHELL;
Robert Griebl1fca5582002-06-04 20:45:46 +000088
Rob Landley3bfcf3c2006-07-10 03:05:46 +000089 if ((cur_uid == 0) || correct_password(pw)) {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000090 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000091 syslog(LOG_NOTICE, "%c %s %s:%s",
92 '+', tty, old_user, opt_username);
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +000093 } else {
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +000094 if (ENABLE_FEATURE_SU_SYSLOG)
Denis Vlasenko15b213e2006-12-19 00:20:20 +000095 syslog(LOG_NOTICE, "%c %s %s:%s",
96 '-', tty, old_user, opt_username);
Rob Landley3bfcf3c2006-07-10 03:05:46 +000097 bb_error_msg_and_die("incorrect password");
Robert Griebl1fca5582002-06-04 20:45:46 +000098 }
99
Bernhard Reutner-Fischer359d7ca2006-12-19 08:55:38 +0000100 if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000101 closelog();
102 free(old_user);
103 }
Glenn L McGratha6b7bdc2003-08-29 07:38:56 +0000104
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000105 if (!opt_shell && (flags & SU_OPT_mp))
106 opt_shell = getenv("SHELL");
Robert Griebl1fca5582002-06-04 20:45:46 +0000107
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000108#if ENABLE_FEATURE_SU_CHECKS_SHELLS
Denys Vlasenko26ffe812010-02-26 10:01:18 +0100109 if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000110 /* The user being su'd to has a nonstandard shell, and so is
111 probably a uucp account or has restricted access. Don't
112 compromise the account by allowing access with a standard
113 shell. */
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000114 bb_error_msg("using restricted shell");
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000115 opt_shell = NULL;
Robert Griebl1fca5582002-06-04 20:45:46 +0000116 }
Denis Vlasenko15b213e2006-12-19 00:20:20 +0000117#endif
118 if (!opt_shell)
119 opt_shell = pw->pw_shell;
Robert Griebl1fca5582002-06-04 20:45:46 +0000120
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000121 change_identity(pw);
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100122 setup_environment(opt_shell,
123 ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
124 + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
125 pw);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000126 IF_SELINUX(set_current_security_context(NULL);)
Rob Landley3bfcf3c2006-07-10 03:05:46 +0000127
Rob Landley18707372006-07-15 19:46:46 +0000128 /* Never returns */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000129 run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000131 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000132}