Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/loginutils/su.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 816 - (hide annotations) (download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 2845 byte(s)
-updated to busybox-1.13.4
1 niro 532 /* vi: set sw=4 ts=4: */
2     /*
3     * Mini su implementation for busybox
4     *
5     * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6     */
7    
8 niro 816 #include "libbb.h"
9 niro 532 #include <syslog.h>
10    
11     #define SU_OPT_mp (3)
12     #define SU_OPT_l (4)
13    
14 niro 816 int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15     int su_main(int argc UNUSED_PARAM, char **argv)
16 niro 532 {
17     unsigned flags;
18 niro 816 char *opt_shell = NULL;
19     char *opt_command = NULL;
20     const char *opt_username = "root";
21 niro 532 struct passwd *pw;
22     uid_t cur_uid = getuid();
23     const char *tty;
24     char *old_user;
25    
26 niro 816 flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
27     //argc -= optind;
28 niro 532 argv += optind;
29    
30 niro 816 if (argv[0] && LONE_DASH(argv[0])) {
31 niro 532 flags |= SU_OPT_l;
32     argv++;
33     }
34    
35     /* get user if specified */
36 niro 816 if (argv[0]) {
37 niro 532 opt_username = argv[0];
38     argv++;
39     }
40    
41     if (ENABLE_FEATURE_SU_SYSLOG) {
42     /* The utmp entry (via getlogin) is probably the best way to identify
43     the user, especially if someone su's from a su-shell.
44     But getlogin can fail -- usually due to lack of utmp entry.
45     in this case resort to getpwuid. */
46     old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
47     tty = ttyname(2) ? : "none";
48     openlog(applet_name, 0, LOG_AUTH);
49     }
50    
51     pw = getpwnam(opt_username);
52     if (!pw)
53     bb_error_msg_and_die("unknown id: %s", opt_username);
54    
55     /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
56     is a username that is retrieved via NIS (YP), but that doesn't have
57     a default shell listed. */
58     if (!pw->pw_shell || !pw->pw_shell[0])
59     pw->pw_shell = (char *)DEFAULT_SHELL;
60    
61     if ((cur_uid == 0) || correct_password(pw)) {
62     if (ENABLE_FEATURE_SU_SYSLOG)
63     syslog(LOG_NOTICE, "%c %s %s:%s",
64     '+', tty, old_user, opt_username);
65     } else {
66     if (ENABLE_FEATURE_SU_SYSLOG)
67     syslog(LOG_NOTICE, "%c %s %s:%s",
68     '-', tty, old_user, opt_username);
69     bb_error_msg_and_die("incorrect password");
70     }
71    
72     if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
73     closelog();
74     free(old_user);
75     }
76    
77     if (!opt_shell && (flags & SU_OPT_mp))
78     opt_shell = getenv("SHELL");
79    
80     #if ENABLE_FEATURE_SU_CHECKS_SHELLS
81     if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
82     /* The user being su'd to has a nonstandard shell, and so is
83     probably a uucp account or has restricted access. Don't
84     compromise the account by allowing access with a standard
85     shell. */
86     bb_error_msg("using restricted shell");
87 niro 816 opt_shell = NULL;
88 niro 532 }
89     #endif
90     if (!opt_shell)
91     opt_shell = pw->pw_shell;
92    
93     change_identity(pw);
94 niro 816 /* setup_environment params: shell, clear_env, change_env, pw */
95 niro 532 setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
96     USE_SELINUX(set_current_security_context(NULL);)
97    
98     /* Never returns */
99     run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
100    
101 niro 816 /* return EXIT_FAILURE; - not reached */
102 niro 532 }