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