1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 #include <sys/resource.h>
12 #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 */
18 /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
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 = {
32 EMPTY_USERNAME_COUNT = 10,
37 static char* short_tty;
39 #if ENABLE_FEATURE_UTMP
40 /* vv Taken from tinylogin utmp.c vv */
42 * read_or_build_utent - see if utmp file is correct for this process
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.
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
55 static void read_or_build_utent(struct utmp *utptr, int picky)
62 /* First, try to find a valid utmp entry for this process. */
63 while ((ut = getutent()))
64 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
65 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
68 /* If there is one, just use it, otherwise create a new one. */
73 bb_error_msg_and_die("no utmp entry found");
75 memset(utptr, 0, sizeof(*utptr));
76 utptr->ut_type = LOGIN_PROCESS;
78 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
79 /* This one is only 4 chars wide. Try to fit something
80 * remotely meaningful by skipping "tty"... */
81 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
82 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
83 utptr->ut_time = time(NULL);
85 if (!picky) /* root login */
86 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
90 * write_utent - put a USER_PROCESS entry in the utmp file
92 * write_utent changes the type of the current utmp entry to
93 * USER_PROCESS. the wtmp file will be updated as well.
95 static void write_utent(struct utmp *utptr, const char *username)
97 utptr->ut_type = USER_PROCESS;
98 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
99 utptr->ut_time = time(NULL);
100 /* other fields already filled in by read_or_build_utent above */
104 #if ENABLE_FEATURE_WTMP
105 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
106 close(creat(bb_path_wtmp_file, 0664));
108 updwtmp(bb_path_wtmp_file, utptr);
111 #else /* !ENABLE_FEATURE_UTMP */
112 #define read_or_build_utent(utptr, picky) ((void)0)
113 #define write_utent(utptr, username) ((void)0)
114 #endif /* !ENABLE_FEATURE_UTMP */
116 #if ENABLE_FEATURE_NOLOGIN
117 static void die_if_nologin_and_non_root(int amroot)
122 if (access("/etc/nologin", F_OK))
125 fp = fopen("/etc/nologin", "r");
127 while ((c = getc(fp)) != EOF)
128 putchar((c=='\n') ? '\r' : c);
132 puts("\r\nSystem closed for routine maintenance\r");
135 puts("\r\n[Disconnect bypassed -- root login allowed]\r");
138 static ALWAYS_INLINE void die_if_nologin_and_non_root(int amroot) {}
141 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
142 static int check_securetty(void)
148 fp = fopen("/etc/securetty", "r");
150 /* A missing securetty file is not an error. */
153 while (fgets(buf, sizeof(buf)-1, fp)) {
154 for (i = strlen(buf)-1; i >= 0; --i) {
155 if (!isspace(buf[i]))
159 if (!buf[0] || (buf[0] == '#'))
161 if (strcmp(buf, short_tty) == 0) {
170 static ALWAYS_INLINE int check_securetty(void) { return 1; }
173 static void get_username_or_die(char *buf, int size_buf)
177 cntdown = EMPTY_USERNAME_COUNT;
179 print_login_prompt();
180 /* skip whitespace */
183 if (c == EOF) exit(1);
185 if (!--cntdown) exit(1);
188 } while (isspace(c));
191 if (!fgets(buf, size_buf-2, stdin))
193 if (!strchr(buf, '\n'))
195 while (isgraph(*buf)) buf++;
199 static void motd(void)
203 fd = open(bb_path_motd_file, O_RDONLY);
206 bb_copyfd_eof(fd, STDOUT_FILENO);
211 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
213 /* This is the escape hatch! Poor serial line users and the like
214 * arrive here when their connection is broken.
215 * We don't want to block here */
218 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
222 int login_main(int argc, char **argv);
223 int login_main(int argc, char **argv)
226 LOGIN_OPT_f = (1<<0),
227 LOGIN_OPT_h = (1<<1),
228 LOGIN_OPT_p = (1<<2),
231 char username[USERNAME_SIZE];
237 char *opt_host = NULL;
238 char *opt_user = NULL;
239 char full_tty[TTYNAME_SIZE];
240 USE_SELINUX(security_context_t user_sid = NULL;)
241 USE_FEATURE_UTMP(struct utmp utent;)
242 USE_PAM(pam_handle_t *pamh;)
244 USE_PAM(const char *failed_msg;)
246 short_tty = full_tty;
248 amroot = (getuid() == 0);
249 signal(SIGALRM, alarm_handler);
252 /* Mandatory paranoia for suid applet:
253 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
254 * and any extra open fd's are closed.
255 * (The name of the function is misleading. Not daemonizing here.) */
256 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
258 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
259 if (opt & LOGIN_OPT_f) {
261 bb_error_msg_and_die("-f is for root only");
262 safe_strncpy(username, opt_user, sizeof(username));
264 if (optind < argc) /* user from command line (getty) */
265 safe_strncpy(username, argv[optind], sizeof(username));
267 /* Let's find out and memorize our tty */
268 if (!isatty(0) || !isatty(1) || !isatty(2))
269 return EXIT_FAILURE; /* Must be a terminal */
270 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
273 safe_strncpy(full_tty, tmp, sizeof(full_tty));
274 if (strncmp(full_tty, "/dev/", 5) == 0)
275 short_tty = full_tty + 5;
278 read_or_build_utent(&utent, !amroot);
282 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
284 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
286 fromhost = xasprintf(" on '%s'", short_tty);
288 /* Was breaking "login <username>" from shell command line: */
291 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
295 get_username_or_die(username, sizeof(username));
298 pamret = pam_start("login", username, &conv, &pamh);
299 if (pamret != PAM_SUCCESS) {
300 failed_msg = "pam_start";
301 goto pam_auth_failed;
303 /* set TTY (so things like securetty work) */
304 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
305 if (pamret != PAM_SUCCESS) {
306 failed_msg = "pam_set_item(TTY)";
307 goto pam_auth_failed;
309 pamret = pam_authenticate(pamh, 0);
310 if (pamret == PAM_SUCCESS) {
312 /* check that the account is healthy. */
313 pamret = pam_acct_mgmt(pamh, 0);
314 if (pamret != PAM_SUCCESS) {
315 failed_msg = "account setup";
316 goto pam_auth_failed;
319 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
320 * thus we use double cast */
321 if (pam_get_item(pamh, PAM_USER, (const void **)(void*)&pamuser) != PAM_SUCCESS) {
322 failed_msg = "pam_get_item(USER)";
323 goto pam_auth_failed;
325 safe_strncpy(username, pamuser, sizeof(username));
327 /* If we get here, the user was authenticated, and is
329 pw = getpwnam(username);
334 bb_error_msg("%s failed: %s", failed_msg, pam_strerror(pamh, pamret));
335 safe_strncpy(username, "UNKNOWN", sizeof(username));
337 pw = getpwnam(username);
339 strcpy(username, "UNKNOWN");
343 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
346 if (opt & LOGIN_OPT_f)
347 break; /* -f USER: success without asking passwd */
349 if (pw->pw_uid == 0 && !check_securetty())
352 /* Don't check the password if password entry is empty (!) */
353 if (!pw->pw_passwd[0])
356 /* authorization takes place here */
357 if (correct_password(pw))
359 #endif /* ENABLE_PAM */
362 bb_do_delay(FAIL_DELAY);
363 puts("Login incorrect");
365 syslog(LOG_WARNING, "invalid password for '%s'%s",
373 die_if_nologin_and_non_root(pw->pw_uid == 0);
375 write_utent(&utent, username);
378 if (is_selinux_enabled()) {
379 security_context_t old_tty_sid, new_tty_sid;
381 if (get_default_context(username, NULL, &user_sid)) {
382 bb_error_msg_and_die("cannot get SID for %s",
385 if (getfilecon(full_tty, &old_tty_sid) < 0) {
386 bb_perror_msg_and_die("getfilecon(%s) failed",
389 if (security_compute_relabel(user_sid, old_tty_sid,
390 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
391 bb_perror_msg_and_die("security_change_sid(%s) failed",
394 if (setfilecon(full_tty, new_tty_sid) != 0) {
395 bb_perror_msg_and_die("chsid(%s, %s) failed",
396 full_tty, new_tty_sid);
400 /* Try these, but don't complain if they fail.
401 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
402 fchown(0, pw->pw_uid, pw->pw_gid);
405 if (ENABLE_LOGIN_SCRIPTS) {
408 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
411 xsetenv("LOGIN_TTY", full_tty);
412 xsetenv("LOGIN_USER", pw->pw_name);
413 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
414 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
415 xsetenv("LOGIN_SHELL", pw->pw_shell);
416 xspawn(t_argv); /* NOMMU-friendly */
417 /* All variables are unset by setup_environment */
426 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
431 syslog(LOG_INFO, "root login%s", fromhost);
433 /* well, a simple setexeccon() here would do the job as well,
434 * but let's play the game for now */
435 set_current_security_context(user_sid);
438 // util-linux login also does:
439 // /* start new session */
441 // /* TIOCSCTTY: steal tty from other process group */
442 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
443 // BBox login used to do this (see above):
445 // If this stuff is really needed, add it and explain why!
447 /* set signals to defaults */
448 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 it leave SIGINT etc enabled or disabled? */
455 signal(SIGINT, SIG_DFL);
457 run_shell(tmp, 1, 0, 0); /* exec the shell finally */