Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (hide annotations) (download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 14433 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 niro 532 /* vi: set sw=4 ts=4: */
2     /*
3     * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4     */
5    
6 niro 816 #include "libbb.h"
7     #include <syslog.h>
8 niro 532 #include <utmp.h>
9     #include <sys/resource.h>
10    
11 niro 816 #if ENABLE_SELINUX
12 niro 532 #include <selinux/selinux.h> /* for is_selinux_enabled() */
13     #include <selinux/get_context_list.h> /* for get_default_context() */
14     #include <selinux/flask.h> /* for security class definitions */
15     #endif
16    
17 niro 816 #if ENABLE_PAM
18     /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
19     #undef setlocale
20     /* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
21     * Apparently they like to confuse people. */
22     #include <security/pam_appl.h>
23     #include <security/pam_misc.h>
24     static const struct pam_conv conv = {
25     misc_conv,
26     NULL
27     };
28     #endif
29    
30 niro 532 enum {
31     TIMEOUT = 60,
32     EMPTY_USERNAME_COUNT = 10,
33     USERNAME_SIZE = 32,
34     TTYNAME_SIZE = 32,
35     };
36    
37 niro 816 static char* short_tty;
38 niro 532
39     #if ENABLE_FEATURE_UTMP
40     /* vv Taken from tinylogin utmp.c vv */
41     /*
42     * read_or_build_utent - see if utmp file is correct for this process
43     *
44     * System V is very picky about the contents of the utmp file
45     * and requires that a slot for the current process exist.
46     * The utmp file is scanned for an entry with the same process
47     * ID. If no entry exists the process exits with a message.
48     *
49     * The "picky" flag is for network and other logins that may
50     * use special flags. It allows the pid checks to be overridden.
51     * This means that getty should never invoke login with any
52     * command line flags.
53     */
54    
55 niro 816 static void read_or_build_utent(struct utmp *utptr, int run_by_root)
56 niro 532 {
57     struct utmp *ut;
58     pid_t pid = getpid();
59    
60     setutent();
61    
62     /* First, try to find a valid utmp entry for this process. */
63 niro 816 /* If there is one, just use it. */
64     while ((ut = getutent()) != NULL)
65     if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0]
66     && (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)
67     ) {
68     *utptr = *ut; /* struct copy */
69     if (run_by_root) /* why only for root? */
70     memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
71     return;
72     }
73 niro 532
74 niro 816 // Why? Do we require non-root to exec login from another
75     // former login process (e.g. login shell)? Some login's have
76     // login shells as children, so it won't work...
77     // if (!run_by_root)
78     // bb_error_msg_and_die("no utmp entry found");
79 niro 532
80 niro 816 /* Otherwise create a new one. */
81     memset(utptr, 0, sizeof(*utptr));
82     utptr->ut_type = LOGIN_PROCESS;
83     utptr->ut_pid = pid;
84     strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
85     /* This one is only 4 chars wide. Try to fit something
86     * remotely meaningful by skipping "tty"... */
87     strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
88     strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
89     utptr->ut_tv.tv_sec = time(NULL);
90 niro 532 }
91    
92     /*
93     * write_utent - put a USER_PROCESS entry in the utmp file
94     *
95     * write_utent changes the type of the current utmp entry to
96     * USER_PROCESS. the wtmp file will be updated as well.
97     */
98 niro 816 static void write_utent(struct utmp *utptr, const char *username)
99 niro 532 {
100 niro 816 utptr->ut_type = USER_PROCESS;
101     strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
102     utptr->ut_tv.tv_sec = time(NULL);
103 niro 532 /* other fields already filled in by read_or_build_utent above */
104     setutent();
105 niro 816 pututline(utptr);
106 niro 532 endutent();
107     #if ENABLE_FEATURE_WTMP
108     if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
109     close(creat(bb_path_wtmp_file, 0664));
110     }
111 niro 816 updwtmp(bb_path_wtmp_file, utptr);
112 niro 532 #endif
113     }
114     #else /* !ENABLE_FEATURE_UTMP */
115 niro 816 #define read_or_build_utent(utptr, run_by_root) ((void)0)
116     #define write_utent(utptr, username) ((void)0)
117 niro 532 #endif /* !ENABLE_FEATURE_UTMP */
118    
119 niro 816 #if ENABLE_FEATURE_NOLOGIN
120     static void die_if_nologin(void)
121 niro 532 {
122     FILE *fp;
123     int c;
124 niro 816 int empty = 1;
125 niro 532
126 niro 816 fp = fopen_for_read("/etc/nologin");
127     if (!fp) /* assuming it does not exist */
128 niro 532 return;
129    
130 niro 816 while ((c = getc(fp)) != EOF) {
131     if (c == '\n')
132     bb_putchar('\r');
133     bb_putchar(c);
134     empty = 0;
135     }
136     if (empty)
137 niro 532 puts("\r\nSystem closed for routine maintenance\r");
138 niro 816
139     fclose(fp);
140 niro 984 fflush_all();
141 niro 816 /* Users say that they do need this prior to exit: */
142     tcdrain(STDOUT_FILENO);
143     exit(EXIT_FAILURE);
144 niro 532 }
145 niro 816 #else
146     static ALWAYS_INLINE void die_if_nologin(void) {}
147     #endif
148 niro 532
149 niro 816 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
150 niro 532 static int check_securetty(void)
151     {
152 niro 816 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
153     parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
154     while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
155     if (strcmp(buf, short_tty) == 0)
156     break;
157     buf = NULL;
158 niro 532 }
159 niro 816 config_close(parser);
160     /* buf != NULL here if config file was not found, empty
161     * or line was found which equals short_tty */
162     return buf != NULL;
163 niro 532 }
164     #else
165 niro 816 static ALWAYS_INLINE int check_securetty(void) { return 1; }
166 niro 532 #endif
167    
168 niro 984 #if ENABLE_SELINUX
169     static void initselinux(char *username, char *full_tty,
170     security_context_t *user_sid)
171     {
172     security_context_t old_tty_sid, new_tty_sid;
173    
174     if (!is_selinux_enabled())
175     return;
176    
177     if (get_default_context(username, NULL, user_sid)) {
178     bb_error_msg_and_die("can't get SID for %s", username);
179     }
180     if (getfilecon(full_tty, &old_tty_sid) < 0) {
181     bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
182     }
183     if (security_compute_relabel(*user_sid, old_tty_sid,
184     SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
185     bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
186     }
187     if (setfilecon(full_tty, new_tty_sid) != 0) {
188     bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
189     }
190     }
191     #endif
192    
193     #if ENABLE_LOGIN_SCRIPTS
194     static void run_login_script(struct passwd *pw, char *full_tty)
195     {
196     char *t_argv[2];
197    
198     t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
199     if (t_argv[0]) {
200     t_argv[1] = NULL;
201     xsetenv("LOGIN_TTY", full_tty);
202     xsetenv("LOGIN_USER", pw->pw_name);
203     xsetenv("LOGIN_UID", utoa(pw->pw_uid));
204     xsetenv("LOGIN_GID", utoa(pw->pw_gid));
205     xsetenv("LOGIN_SHELL", pw->pw_shell);
206     spawn_and_wait(t_argv); /* NOMMU-friendly */
207     unsetenv("LOGIN_TTY");
208     unsetenv("LOGIN_USER");
209     unsetenv("LOGIN_UID");
210     unsetenv("LOGIN_GID");
211     unsetenv("LOGIN_SHELL");
212     }
213     }
214     #else
215     void run_login_script(struct passwd *pw, char *full_tty);
216     #endif
217    
218 niro 532 static void get_username_or_die(char *buf, int size_buf)
219     {
220     int c, cntdown;
221 niro 816
222 niro 532 cntdown = EMPTY_USERNAME_COUNT;
223 niro 816 prompt:
224     print_login_prompt();
225 niro 532 /* skip whitespace */
226     do {
227     c = getchar();
228 niro 984 if (c == EOF)
229     exit(EXIT_FAILURE);
230 niro 532 if (c == '\n') {
231 niro 984 if (!--cntdown)
232     exit(EXIT_FAILURE);
233 niro 532 goto prompt;
234     }
235 niro 984 } while (isspace(c)); /* maybe isblank? */
236 niro 532
237     *buf++ = c;
238     if (!fgets(buf, size_buf-2, stdin))
239 niro 816 exit(EXIT_FAILURE);
240 niro 532 if (!strchr(buf, '\n'))
241 niro 816 exit(EXIT_FAILURE);
242 niro 984 while ((unsigned char)*buf > ' ')
243     buf++;
244 niro 532 *buf = '\0';
245     }
246    
247     static void motd(void)
248     {
249 niro 816 int fd;
250 niro 532
251 niro 816 fd = open(bb_path_motd_file, O_RDONLY);
252     if (fd >= 0) {
253 niro 984 fflush_all();
254 niro 816 bb_copyfd_eof(fd, STDOUT_FILENO);
255     close(fd);
256 niro 532 }
257     }
258    
259 niro 816 static void alarm_handler(int sig UNUSED_PARAM)
260 niro 532 {
261     /* This is the escape hatch! Poor serial line users and the like
262     * arrive here when their connection is broken.
263     * We don't want to block here */
264 niro 816 ndelay_on(1);
265     printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
266 niro 984 fflush_all();
267 niro 816 /* unix API is brain damaged regarding O_NONBLOCK,
268     * we should undo it, or else we can affect other processes */
269     ndelay_off(1);
270     _exit(EXIT_SUCCESS);
271 niro 532 }
272    
273 niro 816 int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
274     int login_main(int argc UNUSED_PARAM, char **argv)
275 niro 532 {
276     enum {
277     LOGIN_OPT_f = (1<<0),
278     LOGIN_OPT_h = (1<<1),
279     LOGIN_OPT_p = (1<<2),
280     };
281 niro 816 char *fromhost;
282 niro 532 char username[USERNAME_SIZE];
283     const char *tmp;
284 niro 816 int run_by_root;
285 niro 532 unsigned opt;
286     int count = 0;
287     struct passwd *pw;
288 niro 816 char *opt_host = opt_host; /* for compiler */
289     char *opt_user = opt_user; /* for compiler */
290 niro 984 char *full_tty;
291     IF_SELINUX(security_context_t user_sid = NULL;)
292     IF_FEATURE_UTMP(struct utmp utent;)
293 niro 816 #if ENABLE_PAM
294     int pamret;
295     pam_handle_t *pamh;
296     const char *pamuser;
297     const char *failed_msg;
298     struct passwd pwdstruct;
299     char pwdbuf[256];
300     #endif
301 niro 532
302     username[0] = '\0';
303     signal(SIGALRM, alarm_handler);
304     alarm(TIMEOUT);
305    
306 niro 816 /* More of suid paranoia if called by non-root: */
307     /* Clear dangerous stuff, set PATH */
308     run_by_root = !sanitize_env_if_suid();
309    
310     /* Mandatory paranoia for suid applet:
311     * ensure that fd# 0,1,2 are opened (at least to /dev/null)
312     * and any extra open fd's are closed.
313     * (The name of the function is misleading. Not daemonizing here.) */
314     bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
315    
316     opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
317 niro 532 if (opt & LOGIN_OPT_f) {
318 niro 816 if (!run_by_root)
319 niro 532 bb_error_msg_and_die("-f is for root only");
320     safe_strncpy(username, opt_user, sizeof(username));
321     }
322 niro 816 argv += optind;
323     if (argv[0]) /* user from command line (getty) */
324     safe_strncpy(username, argv[0], sizeof(username));
325 niro 532
326     /* Let's find out and memorize our tty */
327 niro 984 if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO))
328 niro 532 return EXIT_FAILURE; /* Must be a terminal */
329 niro 984 full_tty = xmalloc_ttyname(STDIN_FILENO);
330     if (!full_tty)
331     full_tty = xstrdup("UNKNOWN");
332     short_tty = full_tty;
333     if (strncmp(full_tty, "/dev/", 5) == 0)
334     short_tty += 5;
335 niro 532
336 niro 816 read_or_build_utent(&utent, run_by_root);
337 niro 532
338 niro 816 if (opt & LOGIN_OPT_h) {
339 niro 984 IF_FEATURE_UTMP(safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));)
340 niro 816 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
341 niro 984 } else {
342 niro 816 fromhost = xasprintf(" on '%s'", short_tty);
343 niro 984 }
344 niro 532
345 niro 816 /* Was breaking "login <username>" from shell command line: */
346     /*bb_setpgrp();*/
347 niro 532
348 niro 984 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
349 niro 532
350     while (1) {
351 niro 816 /* flush away any type-ahead (as getty does) */
352     ioctl(0, TCFLSH, TCIFLUSH);
353    
354 niro 532 if (!username[0])
355     get_username_or_die(username, sizeof(username));
356    
357 niro 816 #if ENABLE_PAM
358     pamret = pam_start("login", username, &conv, &pamh);
359     if (pamret != PAM_SUCCESS) {
360     failed_msg = "start";
361     goto pam_auth_failed;
362     }
363     /* set TTY (so things like securetty work) */
364     pamret = pam_set_item(pamh, PAM_TTY, short_tty);
365     if (pamret != PAM_SUCCESS) {
366     failed_msg = "set_item(TTY)";
367     goto pam_auth_failed;
368     }
369     pamret = pam_authenticate(pamh, 0);
370     if (pamret != PAM_SUCCESS) {
371     failed_msg = "authenticate";
372     goto pam_auth_failed;
373     /* TODO: or just "goto auth_failed"
374     * since user seems to enter wrong password
375     * (in this case pamret == 7)
376     */
377     }
378     /* check that the account is healthy */
379     pamret = pam_acct_mgmt(pamh, 0);
380     if (pamret != PAM_SUCCESS) {
381     failed_msg = "acct_mgmt";
382     goto pam_auth_failed;
383     }
384     /* read user back */
385     pamuser = NULL;
386     /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
387     * thus we cast to (void*) */
388     if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
389     failed_msg = "get_item(USER)";
390     goto pam_auth_failed;
391     }
392     if (!pamuser || !pamuser[0])
393     goto auth_failed;
394     safe_strncpy(username, pamuser, sizeof(username));
395     /* Don't use "pw = getpwnam(username);",
396     * PAM is said to be capable of destroying static storage
397     * used by getpwnam(). We are using safe(r) function */
398     pw = NULL;
399     getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
400     if (!pw)
401     goto auth_failed;
402     pamret = pam_open_session(pamh, 0);
403     if (pamret != PAM_SUCCESS) {
404     failed_msg = "open_session";
405     goto pam_auth_failed;
406     }
407     pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
408     if (pamret != PAM_SUCCESS) {
409     failed_msg = "setcred";
410     goto pam_auth_failed;
411     }
412     break; /* success, continue login process */
413    
414     pam_auth_failed:
415 niro 984 /* syslog, because we don't want potential attacker
416     * to know _why_ login failed */
417     syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
418 niro 816 pam_strerror(pamh, pamret), pamret);
419     safe_strncpy(username, "UNKNOWN", sizeof(username));
420     #else /* not PAM */
421 niro 532 pw = getpwnam(username);
422     if (!pw) {
423 niro 816 strcpy(username, "UNKNOWN");
424     goto fake_it;
425 niro 532 }
426    
427     if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
428     goto auth_failed;
429    
430     if (opt & LOGIN_OPT_f)
431     break; /* -f USER: success without asking passwd */
432    
433     if (pw->pw_uid == 0 && !check_securetty())
434     goto auth_failed;
435    
436     /* Don't check the password if password entry is empty (!) */
437     if (!pw->pw_passwd[0])
438     break;
439 niro 816 fake_it:
440 niro 532 /* authorization takes place here */
441     if (correct_password(pw))
442     break;
443 niro 816 #endif /* ENABLE_PAM */
444     auth_failed:
445 niro 532 opt &= ~LOGIN_OPT_f;
446     bb_do_delay(FAIL_DELAY);
447 niro 816 /* TODO: doesn't sound like correct English phrase to me */
448 niro 532 puts("Login incorrect");
449     if (++count == 3) {
450     syslog(LOG_WARNING, "invalid password for '%s'%s",
451     username, fromhost);
452     return EXIT_FAILURE;
453     }
454     username[0] = '\0';
455 niro 816 } /* while (1) */
456 niro 532
457     alarm(0);
458 niro 816 /* We can ignore /etc/nologin if we are logging in as root,
459     * it doesn't matter whether we are run by root or not */
460     if (pw->pw_uid != 0)
461     die_if_nologin();
462 niro 532
463 niro 816 write_utent(&utent, username);
464 niro 532
465 niro 984 IF_SELINUX(initselinux(username, full_tty, &user_sid));
466 niro 532
467     /* Try these, but don't complain if they fail.
468     * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
469     fchown(0, pw->pw_uid, pw->pw_gid);
470     fchmod(0, 0600);
471    
472 niro 816 /* We trust environment only if we run by root */
473 niro 984 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
474     run_login_script(pw, full_tty);
475 niro 816
476 niro 532 change_identity(pw);
477     tmp = pw->pw_shell;
478     if (!tmp || !*tmp)
479     tmp = DEFAULT_SHELL;
480 niro 816 /* setup_environment params: shell, clear_env, change_env, pw */
481     setup_environment(tmp, !(opt & LOGIN_OPT_p), 1, pw);
482 niro 532
483     motd();
484    
485     if (pw->pw_uid == 0)
486     syslog(LOG_INFO, "root login%s", fromhost);
487 niro 984
488 niro 532 /* well, a simple setexeccon() here would do the job as well,
489     * but let's play the game for now */
490 niro 984 IF_SELINUX(set_current_security_context(user_sid);)
491 niro 532
492     // util-linux login also does:
493     // /* start new session */
494     // setsid();
495     // /* TIOCSCTTY: steal tty from other process group */
496     // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
497 niro 816 // BBox login used to do this (see above):
498     // bb_setpgrp();
499     // If this stuff is really needed, add it and explain why!
500 niro 532
501 niro 816 /* Set signals to defaults */
502     /* Non-ignored signals revert to SIG_DFL on exec anyway */
503     /*signal(SIGALRM, SIG_DFL);*/
504    
505 niro 532 /* Is this correct? This way user can ctrl-c out of /etc/profile,
506     * potentially creating security breach (tested with bash 3.0).
507     * But without this, bash 3.0 will not enable ctrl-c either.
508     * Maybe bash is buggy?
509     * Need to find out what standards say about /bin/login -
510 niro 816 * should we leave SIGINT etc enabled or disabled? */
511 niro 532 signal(SIGINT, SIG_DFL);
512    
513 niro 816 /* Exec login shell with no additional parameters */
514     run_shell(tmp, 1, NULL, NULL);
515 niro 532
516 niro 816 /* return EXIT_FAILURE; - not reached */
517 niro 532 }