preparatory patch for -Wwrite-strings #3
[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 "busybox.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)
15 {
16         unsigned flags;
17         char *opt_shell = NULL;
18         char *opt_command = NULL;
19         const char *opt_username = "root";
20         struct passwd *pw;
21         uid_t cur_uid = getuid();
22         const char *tty;
23         char *old_user;
24
25         flags = getopt32(argc, argv, "mplc:s:", &opt_command, &opt_shell);
26         argc -= optind;
27         argv += optind;
28
29         if (argc && LONE_DASH(argv[0])) {
30                 flags |= SU_OPT_l;
31                 argc--;
32                 argv++;
33         }
34
35         /* get user if specified */
36         if (argc) {
37                 opt_username = argv[0];
38 //              argc--;
39                 argv++;
40         }
41
42         if (ENABLE_FEATURE_SU_SYSLOG) {
43                 /* The utmp entry (via getlogin) is probably the best way to identify
44                 the user, especially if someone su's from a su-shell.
45                 But getlogin can fail -- usually due to lack of utmp entry.
46                 in this case resort to getpwuid.  */
47                 old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
48                 tty = ttyname(2) ? : "none";
49                 openlog(applet_name, 0, LOG_AUTH);
50         }
51
52         pw = getpwnam(opt_username);
53         if (!pw)
54                 bb_error_msg_and_die("unknown id: %s", opt_username);
55
56         /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
57            is a username that is retrieved via NIS (YP), but that doesn't have
58            a default shell listed.  */
59         if (!pw->pw_shell || !pw->pw_shell[0])
60                 pw->pw_shell = (char *)DEFAULT_SHELL;
61
62         if ((cur_uid == 0) || correct_password(pw)) {
63                 if (ENABLE_FEATURE_SU_SYSLOG)
64                         syslog(LOG_NOTICE, "%c %s %s:%s",
65                                 '+', tty, old_user, opt_username);
66         } else {
67                 if (ENABLE_FEATURE_SU_SYSLOG)
68                         syslog(LOG_NOTICE, "%c %s %s:%s",
69                                 '-', tty, old_user, opt_username);
70                 bb_error_msg_and_die("incorrect password");
71         }
72
73         if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
74                 closelog();
75                 free(old_user);
76         }
77
78         if (!opt_shell && (flags & SU_OPT_mp))
79                 opt_shell = getenv("SHELL");
80
81 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
82         if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
83                 /* The user being su'd to has a nonstandard shell, and so is
84                    probably a uucp account or has restricted access.  Don't
85                    compromise the account by allowing access with a standard
86                    shell.  */
87                 bb_error_msg("using restricted shell");
88                 opt_shell = 0;
89         }
90 #endif
91         if (!opt_shell)
92                 opt_shell = pw->pw_shell;
93
94         change_identity(pw);
95         setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
96         USE_SELINUX(set_current_security_context(NULL);)
97
98         /* Never returns */
99         run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
100
101         return EXIT_FAILURE;
102 }