a few more GCC-isms removed
[oweals/busybox.git] / loginutils / su.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Mini su implementation for busybox
4  *
5  *  Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6  */
7
8 #include "libbb.h"
9 #include <syslog.h>
10
11 #define SU_OPT_mp (3)
12 #define SU_OPT_l (4)
13
14 int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int su_main(int argc UNUSED_PARAM, char **argv)
16 {
17         unsigned flags;
18         char *opt_shell = NULL;
19         char *opt_command = NULL;
20         const char *opt_username = "root";
21         struct passwd *pw;
22         uid_t cur_uid = getuid();
23         const char *tty;
24         char *old_user;
25
26         flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
27         //argc -= optind;
28         argv += optind;
29
30         if (argv[0] && LONE_DASH(argv[0])) {
31                 flags |= SU_OPT_l;
32                 argv++;
33         }
34
35         /* get user if specified */
36         if (argv[0]) {
37                 opt_username = argv[0];
38                 argv++;
39         }
40
41         if (ENABLE_FEATURE_SU_SYSLOG) {
42                 /* 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.  */
46                 const char *user;
47 #if ENABLE_FEATURE_UTMP
48                 char user_buf[64];
49                 user = user_buf;
50                 if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
51 #endif
52                 {
53                         pw = getpwuid(cur_uid);
54                         user = pw ? pw->pw_name : "";
55                 }
56                 old_user = xstrdup(user);
57                 tty = xmalloc_ttyname(2);
58                 if (!tty) {
59                         tty = "none";
60                 }
61                 openlog(applet_name, 0, LOG_AUTH);
62         }
63
64         pw = xgetpwnam(opt_username);
65
66         /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
67            is a username that is retrieved via NIS (YP), but that doesn't have
68            a default shell listed.  */
69         if (!pw->pw_shell || !pw->pw_shell[0])
70                 pw->pw_shell = (char *)DEFAULT_SHELL;
71
72         if ((cur_uid == 0) || correct_password(pw)) {
73                 if (ENABLE_FEATURE_SU_SYSLOG)
74                         syslog(LOG_NOTICE, "%c %s %s:%s",
75                                 '+', tty, old_user, opt_username);
76         } else {
77                 if (ENABLE_FEATURE_SU_SYSLOG)
78                         syslog(LOG_NOTICE, "%c %s %s:%s",
79                                 '-', tty, old_user, opt_username);
80                 bb_error_msg_and_die("incorrect password");
81         }
82
83         if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
84                 closelog();
85                 free(old_user);
86         }
87
88         if (!opt_shell && (flags & SU_OPT_mp))
89                 opt_shell = getenv("SHELL");
90
91 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
92         if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
93                 /* The user being su'd to has a nonstandard shell, and so is
94                    probably a uucp account or has restricted access.  Don't
95                    compromise the account by allowing access with a standard
96                    shell.  */
97                 bb_error_msg("using restricted shell");
98                 opt_shell = NULL;
99         }
100 #endif
101         if (!opt_shell)
102                 opt_shell = pw->pw_shell;
103
104         change_identity(pw);
105         /* setup_environment params: shell, clear_env, change_env, pw */
106         setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
107         IF_SELINUX(set_current_security_context(NULL);)
108
109         /* Never returns */
110         run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
111
112         /* return EXIT_FAILURE; - not reached */
113 }