1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/resource.h>
17 #include <sys/types.h>
24 #include <selinux/selinux.h> /* for is_selinux_enabled() */
25 #include <selinux/get_context_list.h> /* for get_default_context() */
26 #include <selinux/flask.h> /* for security class definitions */
30 #ifdef CONFIG_FEATURE_UTMP
32 static struct utmp utent;
33 static void read_or_build_utent(int picky);
34 static void write_utent(const char *username);
39 EMPTY_USERNAME_COUNT = 10,
44 static void die_if_nologin_and_non_root(int amroot);
46 #if defined CONFIG_FEATURE_SECURETTY
47 static int check_securetty(void);
50 static inline int check_securetty(void) { return 1; }
54 static void get_username_or_die(char *buf, int size_buf);
55 static void motd(void);
57 static void nonblock(int fd)
59 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
62 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
64 /* This is the escape hatch! Poor serial line users and the like
65 * arrive here when their connection is broken.
66 * We don't want to block here */
69 bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT);
74 static char full_tty[TTYNAME_SIZE];
75 static char* short_tty = full_tty;
78 int login_main(int argc, char **argv)
81 char username[USERNAME_SIZE];
87 #ifdef CONFIG_WHEEL_GROUP
94 security_context_t user_sid = NULL;
98 amroot = (getuid() == 0);
99 signal(SIGALRM, alarm_handler);
102 while ((flag = getopt(argc, argv, "f:h:p")) != EOF) {
109 * username must be a separate token
110 * (-f root, *NOT* -froot). --marekm
112 if (optarg != argv[optind-1])
115 if (!amroot) /* Auth bypass only if real UID is zero */
116 bb_error_msg_and_die("-f is for root only");
118 safe_strncpy(username, optarg, sizeof(username));
128 if (optind < argc) /* user from command line (getty) */
129 safe_strncpy(username, argv[optind], sizeof(username));
131 /* Let's find out and memorize our tty */
132 if (!isatty(0) || !isatty(1) || !isatty(2))
133 return EXIT_FAILURE; /* Must be a terminal */
134 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
137 safe_strncpy(full_tty, tmp, sizeof(full_tty));
138 if (strncmp(full_tty, "/dev/", 5) == 0)
139 short_tty = full_tty + 5;
142 if (ENABLE_FEATURE_UTMP) {
143 read_or_build_utent(!amroot);
145 memset(utent.ut_host, 0, sizeof(utent.ut_host));
149 if (ENABLE_FEATURE_UTMP)
150 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
151 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s' from "
152 "`%.200s'", short_tty, opt_host);
155 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s'", short_tty);
159 openlog(bb_applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
163 get_username_or_die(username, sizeof(username));
165 pw = getpwnam(username);
167 safe_strncpy(username, "UNKNOWN", sizeof(username));
171 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
175 break; /* -f USER: success without asking passwd */
177 if (pw->pw_uid == 0 && !check_securetty())
180 /* Don't check the password if password entry is empty (!) */
181 if (!pw->pw_passwd[0])
184 /* authorization takes place here */
185 if (correct_password(pw))
190 bb_do_delay(FAIL_DELAY);
191 puts("Login incorrect");
193 syslog(LOG_WARNING, "invalid password for `%s'%s",
201 die_if_nologin_and_non_root(pw->pw_uid == 0);
203 if (ENABLE_FEATURE_UTMP)
204 write_utent(username);
206 #ifdef CONFIG_SELINUX
207 if (is_selinux_enabled()) {
208 security_context_t old_tty_sid, new_tty_sid;
210 if (get_default_context(username, NULL, &user_sid)) {
211 bb_error_msg_and_die("unable to get SID for %s",
214 if (getfilecon(full_tty, &old_tty_sid) < 0) {
215 bb_perror_msg_and_die("getfilecon(%.100s) failed",
218 if (security_compute_relabel(user_sid, old_tty_sid,
219 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
220 bb_perror_msg_and_die("security_change_sid(%.100s) failed",
223 if (setfilecon(full_tty, new_tty_sid) != 0) {
224 bb_perror_msg_and_die("chsid(%.100s, %s) failed",
225 full_tty, new_tty_sid);
229 /* Try these, but don't complain if they fail.
230 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
231 fchown(0, pw->pw_uid, pw->pw_gid);
234 if (ENABLE_LOGIN_SCRIPTS) {
235 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
237 char *t_argv[2] = { script, NULL };
242 setenv("LOGIN_TTY", full_tty, 1);
243 setenv("LOGIN_USER", pw->pw_name, 1);
244 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
245 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
246 setenv("LOGIN_SHELL", pw->pw_shell, 1);
247 execvp(script, t_argv);
249 default: /* parent */
259 setup_environment(tmp, 1, !opt_preserve, pw);
262 signal(SIGALRM, SIG_DFL); /* default alarm signal */
265 syslog(LOG_INFO, "root login%s", fromhost);
266 #ifdef CONFIG_SELINUX
267 /* well, a simple setexeccon() here would do the job as well,
268 * but let's play the game for now */
269 set_current_security_context(user_sid);
271 run_shell(tmp, 1, 0, 0); /* exec the shell finally. */
277 static void get_username_or_die(char *buf, int size_buf)
280 cntdown = EMPTY_USERNAME_COUNT;
282 /* skip whitespace */
283 print_login_prompt();
286 if (c == EOF) exit(1);
288 if (!--cntdown) exit(1);
291 } while (isspace(c));
294 if (!fgets(buf, size_buf-2, stdin))
296 if (!strchr(buf, '\n'))
298 while (isgraph(*buf)) buf++;
303 static void die_if_nologin_and_non_root(int amroot)
308 if (access(bb_path_nologin_file, F_OK))
311 fp = fopen(bb_path_nologin_file, "r");
313 while ((c = getc(fp)) != EOF)
314 putchar((c=='\n') ? '\r' : c);
318 puts("\r\nSystem closed for routine maintenance\r");
321 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
324 #ifdef CONFIG_FEATURE_SECURETTY
326 static int check_securetty(void)
332 fp = fopen(bb_path_securetty_file, "r");
334 /* A missing securetty file is not an error. */
337 while (fgets(buf, sizeof(buf)-1, fp)) {
338 for(i = strlen(buf)-1; i>=0; --i) {
339 if (!isspace(buf[i]))
343 if ((buf[0]=='\0') || (buf[0]=='#'))
345 if (strcmp(buf, short_tty) == 0) {
356 static void motd(void)
361 fp = fopen(bb_path_motd_file, "r");
363 while ((c = getc(fp)) != EOF)
370 #ifdef CONFIG_FEATURE_UTMP
371 /* vv Taken from tinylogin utmp.c vv */
374 * read_or_build_utent - see if utmp file is correct for this process
376 * System V is very picky about the contents of the utmp file
377 * and requires that a slot for the current process exist.
378 * The utmp file is scanned for an entry with the same process
379 * ID. If no entry exists the process exits with a message.
381 * The "picky" flag is for network and other logins that may
382 * use special flags. It allows the pid checks to be overridden.
383 * This means that getty should never invoke login with any
384 * command line flags.
387 static void read_or_build_utent(int picky)
390 pid_t pid = getpid();
394 /* First, try to find a valid utmp entry for this process. */
395 while ((ut = getutent()))
396 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
397 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
400 /* If there is one, just use it, otherwise create a new one. */
405 bb_error_msg_and_die("no utmp entry found");
407 memset(&utent, 0, sizeof(utent));
408 utent.ut_type = LOGIN_PROCESS;
410 strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line));
411 /* This one is only 4 chars wide. Try to fit something
412 * remotely meaningful by skipping "tty"... */
413 strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id));
414 strncpy(utent.ut_user, "LOGIN", sizeof(utent.ut_user));
415 utent.ut_time = time(NULL);
420 * write_utent - put a USER_PROCESS entry in the utmp file
422 * write_utent changes the type of the current utmp entry to
423 * USER_PROCESS. the wtmp file will be updated as well.
426 static void write_utent(const char *username)
428 utent.ut_type = USER_PROCESS;
429 strncpy(utent.ut_user, username, sizeof(utent.ut_user));
430 utent.ut_time = time(NULL);
431 /* other fields already filled in by read_or_build_utent above */
435 #ifdef CONFIG_FEATURE_WTMP
436 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
437 close(creat(bb_path_wtmp_file, 0664));
439 updwtmp(bb_path_wtmp_file, &utent);
442 #endif /* CONFIG_FEATURE_UTMP */