1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 //usage:#define login_trivial_usage
7 //usage: "[-p] [-h HOST] [[-f] USER]"
8 //usage:#define login_full_usage "\n\n"
9 //usage: "Begin a new session on the system\n"
10 //usage: "\n -f Don't authenticate (user already authenticated)"
11 //usage: "\n -h Name of the remote host"
12 //usage: "\n -p Preserve environment"
16 #include <sys/resource.h>
19 # include <selinux/selinux.h> /* for is_selinux_enabled() */
20 # include <selinux/get_context_list.h> /* for get_default_context() */
21 # include <selinux/flask.h> /* for security class definitions */
25 /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
27 /* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
28 * Apparently they like to confuse people. */
29 # include <security/pam_appl.h>
30 # include <security/pam_misc.h>
31 static const struct pam_conv conv = {
39 EMPTY_USERNAME_COUNT = 10,
44 static char* short_tty;
46 #if ENABLE_FEATURE_NOLOGIN
47 static void die_if_nologin(void)
53 fp = fopen_for_read("/etc/nologin");
54 if (!fp) /* assuming it does not exist */
57 while ((c = getc(fp)) != EOF) {
64 puts("\r\nSystem closed for routine maintenance\r");
68 /* Users say that they do need this prior to exit: */
69 tcdrain(STDOUT_FILENO);
73 # define die_if_nologin() ((void)0)
76 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
77 static int check_securetty(void)
79 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
80 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
81 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
82 if (strcmp(buf, short_tty) == 0)
87 /* buf != NULL here if config file was not found, empty
88 * or line was found which equals short_tty */
92 static ALWAYS_INLINE int check_securetty(void) { return 1; }
96 static void initselinux(char *username, char *full_tty,
97 security_context_t *user_sid)
99 security_context_t old_tty_sid, new_tty_sid;
101 if (!is_selinux_enabled())
104 if (get_default_context(username, NULL, user_sid)) {
105 bb_error_msg_and_die("can't get SID for %s", username);
107 if (getfilecon(full_tty, &old_tty_sid) < 0) {
108 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
110 if (security_compute_relabel(*user_sid, old_tty_sid,
111 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
112 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
114 if (setfilecon(full_tty, new_tty_sid) != 0) {
115 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
120 #if ENABLE_LOGIN_SCRIPTS
121 static void run_login_script(struct passwd *pw, char *full_tty)
125 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
128 xsetenv("LOGIN_TTY", full_tty);
129 xsetenv("LOGIN_USER", pw->pw_name);
130 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
131 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
132 xsetenv("LOGIN_SHELL", pw->pw_shell);
133 spawn_and_wait(t_argv); /* NOMMU-friendly */
134 unsetenv("LOGIN_TTY");
135 unsetenv("LOGIN_USER");
136 unsetenv("LOGIN_UID");
137 unsetenv("LOGIN_GID");
138 unsetenv("LOGIN_SHELL");
142 void run_login_script(struct passwd *pw, char *full_tty);
145 static void get_username_or_die(char *buf, int size_buf)
149 cntdown = EMPTY_USERNAME_COUNT;
151 print_login_prompt();
152 /* skip whitespace */
162 } while (isspace(c)); /* maybe isblank? */
165 if (!fgets(buf, size_buf-2, stdin))
167 if (!strchr(buf, '\n'))
169 while ((unsigned char)*buf > ' ')
174 static void motd(void)
178 fd = open(bb_path_motd_file, O_RDONLY);
181 bb_copyfd_eof(fd, STDOUT_FILENO);
186 static void alarm_handler(int sig UNUSED_PARAM)
188 /* This is the escape hatch! Poor serial line users and the like
189 * arrive here when their connection is broken.
190 * We don't want to block here */
192 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
194 /* unix API is brain damaged regarding O_NONBLOCK,
195 * we should undo it, or else we can affect other processes */
200 int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
201 int login_main(int argc UNUSED_PARAM, char **argv)
204 LOGIN_OPT_f = (1<<0),
205 LOGIN_OPT_h = (1<<1),
206 LOGIN_OPT_p = (1<<2),
209 char username[USERNAME_SIZE];
214 char *opt_host = NULL;
215 char *opt_user = opt_user; /* for compiler */
217 IF_SELINUX(security_context_t user_sid = NULL;)
222 const char *failed_msg;
223 struct passwd pwdstruct;
229 signal(SIGALRM, alarm_handler);
232 /* More of suid paranoia if called by non-root: */
233 /* Clear dangerous stuff, set PATH */
234 run_by_root = !sanitize_env_if_suid();
236 /* Mandatory paranoia for suid applet:
237 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
238 * and any extra open fd's are closed.
239 * (The name of the function is misleading. Not daemonizing here.) */
240 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
242 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
243 if (opt & LOGIN_OPT_f) {
245 bb_error_msg_and_die("-f is for root only");
246 safe_strncpy(username, opt_user, sizeof(username));
249 if (argv[0]) /* user from command line (getty) */
250 safe_strncpy(username, argv[0], sizeof(username));
252 /* Let's find out and memorize our tty */
253 if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO))
254 return EXIT_FAILURE; /* Must be a terminal */
255 full_tty = xmalloc_ttyname(STDIN_FILENO);
257 full_tty = xstrdup("UNKNOWN");
258 short_tty = skip_dev_pfx(full_tty);
261 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
263 fromhost = xasprintf(" on '%s'", short_tty);
266 /* Was breaking "login <username>" from shell command line: */
269 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
272 /* flush away any type-ahead (as getty does) */
273 tcflush(0, TCIFLUSH);
276 get_username_or_die(username, sizeof(username));
279 pamret = pam_start("login", username, &conv, &pamh);
280 if (pamret != PAM_SUCCESS) {
281 failed_msg = "start";
282 goto pam_auth_failed;
284 /* set TTY (so things like securetty work) */
285 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
286 if (pamret != PAM_SUCCESS) {
287 failed_msg = "set_item(TTY)";
288 goto pam_auth_failed;
292 pamret = pam_set_item(pamh, PAM_RHOST, opt_host);
293 if (pamret != PAM_SUCCESS) {
294 failed_msg = "set_item(RHOST)";
295 goto pam_auth_failed;
298 pamret = pam_authenticate(pamh, 0);
299 if (pamret != PAM_SUCCESS) {
300 failed_msg = "authenticate";
301 goto pam_auth_failed;
302 /* TODO: or just "goto auth_failed"
303 * since user seems to enter wrong password
304 * (in this case pamret == 7)
307 /* check that the account is healthy */
308 pamret = pam_acct_mgmt(pamh, 0);
309 if (pamret != PAM_SUCCESS) {
310 failed_msg = "acct_mgmt";
311 goto pam_auth_failed;
315 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
316 * thus we cast to (void*) */
317 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
318 failed_msg = "get_item(USER)";
319 goto pam_auth_failed;
321 if (!pamuser || !pamuser[0])
323 safe_strncpy(username, pamuser, sizeof(username));
324 /* Don't use "pw = getpwnam(username);",
325 * PAM is said to be capable of destroying static storage
326 * used by getpwnam(). We are using safe(r) function */
328 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
331 pamret = pam_open_session(pamh, 0);
332 if (pamret != PAM_SUCCESS) {
333 failed_msg = "open_session";
334 goto pam_auth_failed;
336 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
337 if (pamret != PAM_SUCCESS) {
338 failed_msg = "setcred";
339 goto pam_auth_failed;
341 break; /* success, continue login process */
344 /* syslog, because we don't want potential attacker
345 * to know _why_ login failed */
346 syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
347 pam_strerror(pamh, pamret), pamret);
348 safe_strncpy(username, "UNKNOWN", sizeof(username));
350 pw = getpwnam(username);
352 strcpy(username, "UNKNOWN");
356 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
359 if (opt & LOGIN_OPT_f)
360 break; /* -f USER: success without asking passwd */
362 if (pw->pw_uid == 0 && !check_securetty())
365 /* Don't check the password if password entry is empty (!) */
366 if (!pw->pw_passwd[0])
369 /* authorization takes place here */
370 if (correct_password(pw))
372 #endif /* ENABLE_PAM */
375 bb_do_delay(LOGIN_FAIL_DELAY);
376 /* TODO: doesn't sound like correct English phrase to me */
377 puts("Login incorrect");
379 syslog(LOG_WARNING, "invalid password for '%s'%s",
382 if (ENABLE_FEATURE_CLEAN_UP)
391 /* We can ignore /etc/nologin if we are logging in as root,
392 * it doesn't matter whether we are run by root or not */
396 IF_SELINUX(initselinux(username, full_tty, &user_sid));
398 /* Try these, but don't complain if they fail.
399 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
400 fchown(0, pw->pw_uid, pw->pw_gid);
403 update_utmp(getpid(), USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
405 /* We trust environment only if we run by root */
406 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
407 run_login_script(pw, full_tty);
410 setup_environment(pw->pw_shell,
411 (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
415 /* Modules such as pam_env will setup the PAM environment,
416 * which should be copied into the new environment. */
417 pamenv = pam_getenvlist(pamh);
418 if (pamenv) while (*pamenv) {
427 syslog(LOG_INFO, "root login%s", fromhost);
429 if (ENABLE_FEATURE_CLEAN_UP)
432 /* well, a simple setexeccon() here would do the job as well,
433 * but let's play the game for now */
434 IF_SELINUX(set_current_security_context(user_sid);)
436 // util-linux login also does:
437 // /* start new session */
439 // /* TIOCSCTTY: steal tty from other process group */
440 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
441 // BBox login used to do this (see above):
443 // If this stuff is really needed, add it and explain why!
445 /* Set signals to defaults */
446 /* Non-ignored signals revert to SIG_DFL on exec anyway */
447 /*signal(SIGALRM, SIG_DFL);*/
449 /* Is this correct? This way user can ctrl-c out of /etc/profile,
450 * potentially creating security breach (tested with bash 3.0).
451 * But without this, bash 3.0 will not enable ctrl-c either.
452 * Maybe bash is buggy?
453 * Need to find out what standards say about /bin/login -
454 * should we leave SIGINT etc enabled or disabled? */
455 signal(SIGINT, SIG_DFL);
457 /* Exec login shell with no additional parameters */
458 run_shell(pw->pw_shell, 1, NULL, NULL);
460 /* return EXIT_FAILURE; - not reached */