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