Convert a chunk of usage.h to USE_ and SKIP_ (more to do there), and fix a
[oweals/busybox.git] / loginutils / su.c
1 /* vi: set sw=4 ts=4: */
2 /*
3    Licensed under the GPL v2, see the file LICENSE in this tarball.
4 */
5
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <syslog.h>
12 #include <termios.h>
13 #include <unistd.h>
14 #include <utmp.h>
15 #include <sys/resource.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <ctype.h>
20 #include <time.h>
21
22 #include "busybox.h"
23
24 /* The shell to run if none is given in the user's passwd entry.  */
25 #ifndef DEFAULT_SHELL
26 #define DEFAULT_SHELL "/bin/sh"
27 #endif
28
29 /* Default user.  */
30 #define DEFAULT_USER  "root"
31
32 /* #define SYSLOG_SUCCESS */
33 #define SYSLOG_FAILURE
34
35
36 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
37 /* Log the fact that someone has run su */
38
39 # if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
40 static void log_su (const char *successful, const char *old_user,
41                                         const char *tty)
42 {
43         syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
44 }
45 #  define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
46 #  define log_su_failure(cu, u, tty)    if(!cu) log_su("FAILED SU ", u, tty)
47 # else
48         /* partial logging */
49 #  if !defined( SYSLOG_SUCESS )
50 #   define log_su_successful(cu, u, tty)
51 #   define log_su_failure(cu, u, t) if(!cu) \
52                         syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
53 #  else
54 #   define log_su_successful(cu, u, t) if(!cu) \
55                         syslog(LOG_NOTICE, "%s on %s", u, t)
56 #   define log_su_failure(cu, u, tty)
57 #  endif
58 # endif
59 #else
60         /* logging not used */
61 # define log_su_successful(cu, u, tty)
62 # define log_su_failure(cu, u, tty)
63 #endif
64
65
66 int su_main ( int argc, char **argv )
67 {
68         unsigned long flags;
69         int opt_preserve;
70         int opt_loginshell;
71         char *opt_shell = 0;
72         char *opt_command = 0;
73         char *opt_username = DEFAULT_USER;
74         char **opt_args = 0;
75         struct passwd *pw;
76         uid_t cur_uid = getuid();
77
78 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
79         const char *tty;
80         const char *old_user;
81 #endif
82
83         flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
84                                                   &opt_command, &opt_shell);
85         opt_preserve = flags & 3;
86         opt_loginshell = (flags & 4 ? 1 : 0);
87
88         if (optind < argc  && argv[optind][0] == '-' && argv[optind][1] == 0) {
89                 opt_loginshell = 1;
90                 ++optind;
91     }
92
93         /* get user if specified */
94         if ( optind < argc )
95                 opt_username = argv [optind++];
96
97         if ( optind < argc )
98                 opt_args = argv + optind;
99
100 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
101 #ifdef CONFIG_FEATURE_UTMP
102         /* The utmp entry (via getlogin) is probably the best way to identify
103            the user, especially if someone su's from a su-shell.  */
104         old_user = getlogin ( );
105         if ( !old_user )
106 #endif
107                 {
108                 /* getlogin can fail -- usually due to lack of utmp entry.
109                    Resort to getpwuid.  */
110                 pw = getpwuid ( cur_uid );
111                 old_user = ( pw ? pw->pw_name : "" );
112         }
113         tty = ttyname ( 2 );
114         if(!tty)
115                 tty = "none";
116
117         openlog ( bb_applet_name, 0, LOG_AUTH );
118 #endif
119
120         pw = getpwnam ( opt_username );
121         if ( !pw )
122                 bb_error_msg_and_die ( "user %s does not exist", opt_username );
123
124         /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
125            is a username that is retrieved via NIS (YP), but that doesn't have
126            a default shell listed.  */
127         if ( !pw->pw_shell || !pw->pw_shell [0] )
128                 pw->pw_shell = (char *) DEFAULT_SHELL;
129
130         if ((( cur_uid == 0 ) || correct_password ( pw ))) {
131                 log_su_successful(pw->pw_uid, old_user, tty );
132         } else {
133                 log_su_failure (pw->pw_uid, old_user, tty );
134                 bb_error_msg_and_die ( "incorrect password" );
135         }
136
137 #if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
138         closelog();
139 #endif
140
141         if ( !opt_shell && opt_preserve )
142                 opt_shell = getenv ( "SHELL" );
143
144         if ( opt_shell && cur_uid && restricted_shell ( pw->pw_shell )) {
145                 /* The user being su'd to has a nonstandard shell, and so is
146                    probably a uucp account or has restricted access.  Don't
147                    compromise the account by allowing access with a standard
148                    shell.  */
149                 fputs ( "using restricted shell\n", stderr );
150                 opt_shell = 0;
151         }
152
153         if ( !opt_shell )
154                 opt_shell = pw->pw_shell;
155
156         change_identity ( pw );
157         setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
158 #if ENABLE_SELINUX
159        set_current_security_context(NULL);
160 #endif
161         run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args);
162
163         return EXIT_FAILURE;
164 }