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