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 #include <pam/pam_appl.h>
19 #include <pam/pam_misc.h>
20 static const struct pam_conv conv = {
28 EMPTY_USERNAME_COUNT = 10,
33 static char* short_tty;
35 #if ENABLE_FEATURE_UTMP
36 /* vv Taken from tinylogin utmp.c vv */
38 * read_or_build_utent - see if utmp file is correct for this process
40 * System V is very picky about the contents of the utmp file
41 * and requires that a slot for the current process exist.
42 * The utmp file is scanned for an entry with the same process
43 * ID. If no entry exists the process exits with a message.
45 * The "picky" flag is for network and other logins that may
46 * use special flags. It allows the pid checks to be overridden.
47 * This means that getty should never invoke login with any
51 static void read_or_build_utent(struct utmp *utptr, int picky)
58 /* First, try to find a valid utmp entry for this process. */
59 while ((ut = getutent()))
60 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
61 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
64 /* If there is one, just use it, otherwise create a new one. */
69 bb_error_msg_and_die("no utmp entry found");
71 memset(utptr, 0, sizeof(*utptr));
72 utptr->ut_type = LOGIN_PROCESS;
74 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
75 /* This one is only 4 chars wide. Try to fit something
76 * remotely meaningful by skipping "tty"... */
77 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
78 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
79 utptr->ut_time = time(NULL);
81 if (!picky) /* root login */
82 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
86 * write_utent - put a USER_PROCESS entry in the utmp file
88 * write_utent changes the type of the current utmp entry to
89 * USER_PROCESS. the wtmp file will be updated as well.
91 static void write_utent(struct utmp *utptr, const char *username)
93 utptr->ut_type = USER_PROCESS;
94 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
95 utptr->ut_time = time(NULL);
96 /* other fields already filled in by read_or_build_utent above */
100 #if ENABLE_FEATURE_WTMP
101 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
102 close(creat(bb_path_wtmp_file, 0664));
104 updwtmp(bb_path_wtmp_file, utptr);
107 #else /* !ENABLE_FEATURE_UTMP */
108 #define read_or_build_utent(utptr, picky) ((void)0)
109 #define write_utent(utptr, username) ((void)0)
110 #endif /* !ENABLE_FEATURE_UTMP */
112 #if ENABLE_FEATURE_NOLOGIN
113 static void die_if_nologin_and_non_root(int amroot)
118 if (access("/etc/nologin", F_OK))
121 fp = fopen("/etc/nologin", "r");
123 while ((c = getc(fp)) != EOF)
124 putchar((c=='\n') ? '\r' : c);
128 puts("\r\nSystem closed for routine maintenance\r");
131 puts("\r\n[Disconnect bypassed -- root login allowed]\r");
134 static ALWAYS_INLINE void die_if_nologin_and_non_root(int amroot) {}
137 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
138 static int check_securetty(void)
144 fp = fopen("/etc/securetty", "r");
146 /* A missing securetty file is not an error. */
149 while (fgets(buf, sizeof(buf)-1, fp)) {
150 for (i = strlen(buf)-1; i >= 0; --i) {
151 if (!isspace(buf[i]))
155 if (!buf[0] || (buf[0] == '#'))
157 if (strcmp(buf, short_tty) == 0) {
166 static ALWAYS_INLINE int check_securetty(void) { return 1; }
169 static void get_username_or_die(char *buf, int size_buf)
173 cntdown = EMPTY_USERNAME_COUNT;
175 print_login_prompt();
176 /* skip whitespace */
179 if (c == EOF) exit(1);
181 if (!--cntdown) exit(1);
184 } while (isspace(c));
187 if (!fgets(buf, size_buf-2, stdin))
189 if (!strchr(buf, '\n'))
191 while (isgraph(*buf)) buf++;
195 static void motd(void)
199 fd = open(bb_path_motd_file, O_RDONLY);
202 bb_copyfd_eof(fd, STDOUT_FILENO);
207 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
209 /* This is the escape hatch! Poor serial line users and the like
210 * arrive here when their connection is broken.
211 * We don't want to block here */
214 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
218 int login_main(int argc, char **argv);
219 int login_main(int argc, char **argv)
222 LOGIN_OPT_f = (1<<0),
223 LOGIN_OPT_h = (1<<1),
224 LOGIN_OPT_p = (1<<2),
227 char username[USERNAME_SIZE];
233 char *opt_host = NULL;
234 char *opt_user = NULL;
235 char full_tty[TTYNAME_SIZE];
236 USE_SELINUX(security_context_t user_sid = NULL;)
237 USE_FEATURE_UTMP(struct utmp utent;)
238 USE_PAM(pam_handle_t *pamh;)
240 USE_PAM(const char *failed_msg;)
242 short_tty = full_tty;
244 amroot = (getuid() == 0);
245 signal(SIGALRM, alarm_handler);
248 /* Mandatory paranoia for suid applet:
249 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
250 * and any extra open fd's are closed.
251 * (The name of the function is misleading. Not daemonizing here.) */
252 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
254 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
255 if (opt & LOGIN_OPT_f) {
257 bb_error_msg_and_die("-f is for root only");
258 safe_strncpy(username, opt_user, sizeof(username));
260 if (optind < argc) /* user from command line (getty) */
261 safe_strncpy(username, argv[optind], sizeof(username));
263 /* Let's find out and memorize our tty */
264 if (!isatty(0) || !isatty(1) || !isatty(2))
265 return EXIT_FAILURE; /* Must be a terminal */
266 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
269 safe_strncpy(full_tty, tmp, sizeof(full_tty));
270 if (strncmp(full_tty, "/dev/", 5) == 0)
271 short_tty = full_tty + 5;
274 read_or_build_utent(&utent, !amroot);
278 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
280 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
282 fromhost = xasprintf(" on '%s'", short_tty);
284 /* Was breaking "login <username>" from shell command line: */
287 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
291 get_username_or_die(username, sizeof(username));
294 pamret = pam_start("login", username, &conv, &pamh);
295 if (pamret != PAM_SUCCESS) {
296 failed_msg = "pam_start";
297 goto pam_auth_failed;
299 /* set TTY (so things like securetty work) */
300 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
301 if (pamret != PAM_SUCCESS) {
302 failed_msg = "pam_set_item(TTY)";
303 goto pam_auth_failed;
305 pamret = pam_authenticate(pamh, 0);
306 if (pamret == PAM_SUCCESS) {
308 /* check that the account is healthy. */
309 pamret = pam_acct_mgmt(pamh, 0);
310 if (pamret != PAM_SUCCESS) {
311 failed_msg = "account setup";
312 goto pam_auth_failed;
315 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
316 * thus we use double cast */
317 if (pam_get_item(pamh, PAM_USER, (const void **)(void*)&pamuser) != PAM_SUCCESS) {
318 failed_msg = "pam_get_item(USER)";
319 goto pam_auth_failed;
321 safe_strncpy(username, pamuser, sizeof(username));
323 /* If we get here, the user was authenticated, and is
325 pw = getpwnam(username);
330 bb_error_msg("%s failed: %s", failed_msg, pam_strerror(pamh, pamret));
331 safe_strncpy(username, "UNKNOWN", sizeof(username));
333 pw = getpwnam(username);
335 strcpy(username, "UNKNOWN");
339 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
342 if (opt & LOGIN_OPT_f)
343 break; /* -f USER: success without asking passwd */
345 if (pw->pw_uid == 0 && !check_securetty())
348 /* Don't check the password if password entry is empty (!) */
349 if (!pw->pw_passwd[0])
352 /* authorization takes place here */
353 if (correct_password(pw))
355 #endif /* ENABLE_PAM */
358 bb_do_delay(FAIL_DELAY);
359 puts("Login incorrect");
361 syslog(LOG_WARNING, "invalid password for '%s'%s",
369 die_if_nologin_and_non_root(pw->pw_uid == 0);
371 write_utent(&utent, username);
374 if (is_selinux_enabled()) {
375 security_context_t old_tty_sid, new_tty_sid;
377 if (get_default_context(username, NULL, &user_sid)) {
378 bb_error_msg_and_die("cannot get SID for %s",
381 if (getfilecon(full_tty, &old_tty_sid) < 0) {
382 bb_perror_msg_and_die("getfilecon(%s) failed",
385 if (security_compute_relabel(user_sid, old_tty_sid,
386 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
387 bb_perror_msg_and_die("security_change_sid(%s) failed",
390 if (setfilecon(full_tty, new_tty_sid) != 0) {
391 bb_perror_msg_and_die("chsid(%s, %s) failed",
392 full_tty, new_tty_sid);
396 /* Try these, but don't complain if they fail.
397 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
398 fchown(0, pw->pw_uid, pw->pw_gid);
401 if (ENABLE_LOGIN_SCRIPTS) {
404 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
407 xsetenv("LOGIN_TTY", full_tty);
408 xsetenv("LOGIN_USER", pw->pw_name);
409 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
410 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
411 xsetenv("LOGIN_SHELL", pw->pw_shell);
412 xspawn(t_argv); /* NOMMU-friendly */
413 /* All variables are unset by setup_environment */
422 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
427 syslog(LOG_INFO, "root login%s", fromhost);
429 /* well, a simple setexeccon() here would do the job as well,
430 * but let's play the game for now */
431 set_current_security_context(user_sid);
434 // util-linux login also does:
435 // /* start new session */
437 // /* TIOCSCTTY: steal tty from other process group */
438 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
439 // BBox login used to do this (see above):
441 // If this stuff is really needed, add it and explain why!
443 /* set signals to defaults */
444 signal(SIGALRM, SIG_DFL);
445 /* Is this correct? This way user can ctrl-c out of /etc/profile,
446 * potentially creating security breach (tested with bash 3.0).
447 * But without this, bash 3.0 will not enable ctrl-c either.
448 * Maybe bash is buggy?
449 * Need to find out what standards say about /bin/login -
450 * should it leave SIGINT etc enabled or disabled? */
451 signal(SIGINT, SIG_DFL);
453 run_shell(tmp, 1, 0, 0); /* exec the shell finally */