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