Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/loginutils/sulogin.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 815 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 5  Line 5 
5   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6   */   */
7    
8    #include "libbb.h"
9  #include <syslog.h>  #include <syslog.h>
10    
11  #include "busybox.h"  //static void catchalarm(int UNUSED_PARAM junk)
12    //{
13    // exit(EXIT_FAILURE);
14    //}
15    
 static const char * const forbid[] = {  
  "ENV",  
  "BASH_ENV",  
  "HOME",  
  "IFS",  
  "PATH",  
  "SHELL",  
  "LD_LIBRARY_PATH",  
  "LD_PRELOAD",  
  "LD_TRACE_LOADED_OBJECTS",  
  "LD_BIND_NOW",  
  "LD_AOUT_LIBRARY_PATH",  
  "LD_AOUT_PRELOAD",  
  "LD_NOWARN",  
  "LD_KEEPDIR",  
  (char *) 0  
 };  
16    
17    int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
18  static void catchalarm(int ATTRIBUTE_UNUSED junk)  int sulogin_main(int argc UNUSED_PARAM, char **argv)
 {  
  exit(EXIT_FAILURE);  
 }  
   
   
 int sulogin_main(int argc, char **argv)  
19  {  {
20   char *cp;   char *cp;
21   int timeout = 0;   int timeout = 0;
  char *timeout_arg;  
  const char * const *p;  
22   struct passwd *pwd;   struct passwd *pwd;
23   const char *shell;   const char *shell;
24    #if ENABLE_FEATURE_SHADOWPASSWDS
25     /* Using _r function to avoid pulling in static buffers */
26     char buffer[256];
27     struct spwd spw;
28    #endif
29    
30   logmode = LOGMODE_BOTH;   logmode = LOGMODE_BOTH;
31   openlog(applet_name, 0, LOG_AUTH);   openlog(applet_name, 0, LOG_AUTH);
32    
33   if (getopt32(argc, argv, "t:", &timeout_arg)) {   opt_complementary = "t+"; /* -t N */
34   timeout = xatoi_u(timeout_arg);   getopt32(argv, "t:", &timeout);
35   }   argv += optind;
36    
37   if (argv[optind]) {   if (argv[0]) {
38   close(0);   close(0);
39   close(1);   close(1);
40   dup(xopen(argv[optind], O_RDWR));   dup(xopen(argv[0], O_RDWR));
41   close(2);   close(2);
42   dup(0);   dup(0);
43   }   }
44    
45     /* Malicious use like "sulogin /dev/sda"? */
46   if (!isatty(0) || !isatty(1) || !isatty(2)) {   if (!isatty(0) || !isatty(1) || !isatty(2)) {
47   logmode = LOGMODE_SYSLOG;   logmode = LOGMODE_SYSLOG;
48   bb_error_msg_and_die("not a tty");   bb_error_msg_and_die("not a tty");
49   }   }
50    
51   /* Clear out anything dangerous from the environment */   /* Clear dangerous stuff, set PATH */
52   for (p = forbid; *p; p++)   sanitize_env_if_suid();
  unsetenv(*p);  
53    
54   signal(SIGALRM, catchalarm);  // bb_askpass() already handles this
55    // signal(SIGALRM, catchalarm);
56    
57   pwd = getpwuid(0);   pwd = getpwuid(0);
58   if (!pwd) {   if (!pwd) {
# Line 76  int sulogin_main(int argc, char **argv) Line 61  int sulogin_main(int argc, char **argv)
61    
62  #if ENABLE_FEATURE_SHADOWPASSWDS  #if ENABLE_FEATURE_SHADOWPASSWDS
63   {   {
64   struct spwd *spwd = getspnam(pwd->pw_name);   /* getspnam_r may return 0 yet set result to NULL.
65   if (!spwd) {   * At least glibc 2.4 does this. Be extra paranoid here. */
66     struct spwd *result = NULL;
67     int r = getspnam_r(pwd->pw_name, &spw, buffer, sizeof(buffer), &result);
68     if (r || !result) {
69   goto auth_error;   goto auth_error;
70   }   }
71   pwd->pw_passwd = spwd->sp_pwdp;   pwd->pw_passwd = result->sp_pwdp;
72   }   }
73  #endif  #endif
74    
75   while (1) {   while (1) {
76     char *encrypted;
77     int r;
78    
79   /* cp points to a static buffer that is zeroed every time */   /* cp points to a static buffer that is zeroed every time */
80   cp = bb_askpass(timeout,   cp = bb_askpass(timeout,
81   "Give root password for system maintenance\n"   "Give root password for system maintenance\n"
# Line 94  int sulogin_main(int argc, char **argv) Line 85  int sulogin_main(int argc, char **argv)
85   bb_info_msg("Normal startup");   bb_info_msg("Normal startup");
86   return 0;   return 0;
87   }   }
88   if (strcmp(pw_encrypt(cp, pwd->pw_passwd), pwd->pw_passwd) == 0) {   encrypted = pw_encrypt(cp, pwd->pw_passwd, 1);
89     r = strcmp(encrypted, pwd->pw_passwd);
90     free(encrypted);
91     if (r == 0) {
92   break;   break;
93   }   }
94   bb_do_delay(FAIL_DELAY);   bb_do_delay(FAIL_DELAY);
95   bb_error_msg("login incorrect");   bb_error_msg("login incorrect");
96   }   }
97   memset(cp, 0, strlen(cp));   memset(cp, 0, strlen(cp));
98   signal(SIGALRM, SIG_DFL);  // signal(SIGALRM, SIG_DFL);
99    
100   bb_info_msg("System Maintenance Mode");   bb_info_msg("System Maintenance Mode");
101    
102   USE_SELINUX(renew_current_security_context());   USE_SELINUX(renew_current_security_context());
103    
104   shell = getenv("SUSHELL");   shell = getenv("SUSHELL");
105   if (!shell) shell = getenv("sushell");   if (!shell)
106     shell = getenv("sushell");
107   if (!shell) {   if (!shell) {
108   shell = "/bin/sh";   shell = "/bin/sh";
109   if (pwd->pw_shell[0])   if (pwd->pw_shell[0])
110   shell = pwd->pw_shell;   shell = pwd->pw_shell;
111   }   }
112   run_shell(shell, 1, 0, 0);   /* Exec login shell with no additional parameters. Never returns. */
113   /* never returns */   run_shell(shell, 1, NULL, NULL);
114    
115  auth_error:   auth_error:
116   bb_error_msg_and_die("no password entry for 'root'");   bb_error_msg_and_die("no password entry for root");
117  }  }

Legend:
Removed from v.815  
changed lines
  Added in v.816