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 */
19 * XXX: FIXME: provide empty bodies if ENABLE_FEATURE_UTMP == 0
21 static struct utmp utent;
22 static void read_or_build_utent(int);
23 static void write_utent(const char *);
27 EMPTY_USERNAME_COUNT = 10,
32 static void die_if_nologin_and_non_root(int amroot);
34 #if ENABLE_FEATURE_SECURETTY
35 static int check_securetty(void);
37 static inline int check_securetty(void) { return 1; }
40 static void get_username_or_die(char *buf, int size_buf);
41 static void motd(void);
43 static void nonblock(int fd)
45 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
48 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
50 /* This is the escape hatch! Poor serial line users and the like
51 * arrive here when their connection is broken.
52 * We don't want to block here */
55 bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT);
60 static char full_tty[TTYNAME_SIZE];
61 static char* short_tty = full_tty;
64 int login_main(int argc, char **argv)
67 char username[USERNAME_SIZE];
77 security_context_t user_sid = NULL;
81 amroot = (getuid() == 0);
82 signal(SIGALRM, alarm_handler);
85 while ((flag = getopt(argc, argv, "f:h:p")) != EOF) {
92 * username must be a separate token
93 * (-f root, *NOT* -froot). --marekm
95 if (optarg != argv[optind-1])
98 if (!amroot) /* Auth bypass only if real UID is zero */
99 bb_error_msg_and_die("-f is for root only");
101 safe_strncpy(username, optarg, sizeof(username));
111 if (optind < argc) /* user from command line (getty) */
112 safe_strncpy(username, argv[optind], sizeof(username));
114 /* Let's find out and memorize our tty */
115 if (!isatty(0) || !isatty(1) || !isatty(2))
116 return EXIT_FAILURE; /* Must be a terminal */
117 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
120 safe_strncpy(full_tty, tmp, sizeof(full_tty));
121 if (strncmp(full_tty, "/dev/", 5) == 0)
122 short_tty = full_tty + 5;
125 if (ENABLE_FEATURE_UTMP) {
126 read_or_build_utent(!amroot);
128 memset(utent.ut_host, 0, sizeof(utent.ut_host));
132 if (ENABLE_FEATURE_UTMP)
133 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
134 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s' from "
135 "`%.200s'", short_tty, opt_host);
138 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s'", short_tty);
142 openlog(bb_applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
146 get_username_or_die(username, sizeof(username));
148 pw = getpwnam(username);
150 safe_strncpy(username, "UNKNOWN", sizeof(username));
154 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
158 break; /* -f USER: success without asking passwd */
160 if (pw->pw_uid == 0 && !check_securetty())
163 /* Don't check the password if password entry is empty (!) */
164 if (!pw->pw_passwd[0])
167 /* authorization takes place here */
168 if (correct_password(pw))
173 bb_do_delay(FAIL_DELAY);
174 puts("Login incorrect");
176 syslog(LOG_WARNING, "invalid password for `%s'%s",
184 die_if_nologin_and_non_root(pw->pw_uid == 0);
186 if (ENABLE_FEATURE_UTMP)
187 write_utent(username);
189 #ifdef CONFIG_SELINUX
190 if (is_selinux_enabled()) {
191 security_context_t old_tty_sid, new_tty_sid;
193 if (get_default_context(username, NULL, &user_sid)) {
194 bb_error_msg_and_die("unable to get SID for %s",
197 if (getfilecon(full_tty, &old_tty_sid) < 0) {
198 bb_perror_msg_and_die("getfilecon(%.100s) failed",
201 if (security_compute_relabel(user_sid, old_tty_sid,
202 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
203 bb_perror_msg_and_die("security_change_sid(%.100s) failed",
206 if (setfilecon(full_tty, new_tty_sid) != 0) {
207 bb_perror_msg_and_die("chsid(%.100s, %s) failed",
208 full_tty, new_tty_sid);
212 /* Try these, but don't complain if they fail.
213 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
214 fchown(0, pw->pw_uid, pw->pw_gid);
217 if (ENABLE_LOGIN_SCRIPTS) {
218 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
220 char *t_argv[2] = { script, NULL };
225 setenv("LOGIN_TTY", full_tty, 1);
226 setenv("LOGIN_USER", pw->pw_name, 1);
227 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
228 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
229 setenv("LOGIN_SHELL", pw->pw_shell, 1);
230 execvp(script, t_argv);
232 default: /* parent */
242 setup_environment(tmp, 1, !opt_preserve, pw);
245 signal(SIGALRM, SIG_DFL); /* default alarm signal */
248 syslog(LOG_INFO, "root login%s", fromhost);
249 #ifdef CONFIG_SELINUX
250 /* well, a simple setexeccon() here would do the job as well,
251 * but let's play the game for now */
252 set_current_security_context(user_sid);
254 run_shell(tmp, 1, 0, 0); /* exec the shell finally. */
260 static void get_username_or_die(char *buf, int size_buf)
263 cntdown = EMPTY_USERNAME_COUNT;
265 /* skip whitespace */
266 print_login_prompt();
269 if (c == EOF) exit(1);
271 if (!--cntdown) exit(1);
274 } while (isspace(c));
277 if (!fgets(buf, size_buf-2, stdin))
279 if (!strchr(buf, '\n'))
281 while (isgraph(*buf)) buf++;
286 static void die_if_nologin_and_non_root(int amroot)
291 if (access(bb_path_nologin_file, F_OK))
294 fp = fopen(bb_path_nologin_file, "r");
296 while ((c = getc(fp)) != EOF)
297 putchar((c=='\n') ? '\r' : c);
301 puts("\r\nSystem closed for routine maintenance\r");
304 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
307 #if ENABLE_FEATURE_SECURETTY
309 static int check_securetty(void)
315 fp = fopen(bb_path_securetty_file, "r");
317 /* A missing securetty file is not an error. */
320 while (fgets(buf, sizeof(buf)-1, fp)) {
321 for(i = strlen(buf)-1; i>=0; --i) {
322 if (!isspace(buf[i]))
326 if ((buf[0]=='\0') || (buf[0]=='#'))
328 if (strcmp(buf, short_tty) == 0) {
339 static void motd(void)
344 fp = fopen(bb_path_motd_file, "r");
346 while ((c = getc(fp)) != EOF)
353 #if ENABLE_FEATURE_UTMP
354 /* vv Taken from tinylogin utmp.c vv */
357 * read_or_build_utent - see if utmp file is correct for this process
359 * System V is very picky about the contents of the utmp file
360 * and requires that a slot for the current process exist.
361 * The utmp file is scanned for an entry with the same process
362 * ID. If no entry exists the process exits with a message.
364 * The "picky" flag is for network and other logins that may
365 * use special flags. It allows the pid checks to be overridden.
366 * This means that getty should never invoke login with any
367 * command line flags.
370 static void read_or_build_utent(int picky)
373 pid_t pid = getpid();
377 /* First, try to find a valid utmp entry for this process. */
378 while ((ut = getutent()))
379 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
380 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
383 /* If there is one, just use it, otherwise create a new one. */
388 bb_error_msg_and_die("no utmp entry found");
390 memset(&utent, 0, sizeof(utent));
391 utent.ut_type = LOGIN_PROCESS;
393 strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line));
394 /* This one is only 4 chars wide. Try to fit something
395 * remotely meaningful by skipping "tty"... */
396 strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id));
397 strncpy(utent.ut_user, "LOGIN", sizeof(utent.ut_user));
398 utent.ut_time = time(NULL);
403 * write_utent - put a USER_PROCESS entry in the utmp file
405 * write_utent changes the type of the current utmp entry to
406 * USER_PROCESS. the wtmp file will be updated as well.
409 static void write_utent(const char *username)
411 utent.ut_type = USER_PROCESS;
412 strncpy(utent.ut_user, username, sizeof(utent.ut_user));
413 utent.ut_time = time(NULL);
414 /* other fields already filled in by read_or_build_utent above */
418 #if ENABLE_FEATURE_WTMP
419 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
420 close(creat(bb_path_wtmp_file, 0664));
422 updwtmp(bb_path_wtmp_file, &utent);
425 #endif /* CONFIG_FEATURE_UTMP */