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 */
20 EMPTY_USERNAME_COUNT = 10,
25 static char full_tty[TTYNAME_SIZE];
26 static char* short_tty = full_tty;
28 #if ENABLE_FEATURE_UTMP
29 /* vv Taken from tinylogin utmp.c vv */
31 * read_or_build_utent - see if utmp file is correct for this process
33 * System V is very picky about the contents of the utmp file
34 * and requires that a slot for the current process exist.
35 * The utmp file is scanned for an entry with the same process
36 * ID. If no entry exists the process exits with a message.
38 * The "picky" flag is for network and other logins that may
39 * use special flags. It allows the pid checks to be overridden.
40 * This means that getty should never invoke login with any
43 static struct utmp utent;
44 static void read_or_build_utent(int picky)
51 /* First, try to find a valid utmp entry for this process. */
52 while ((ut = getutent()))
53 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
54 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
57 /* If there is one, just use it, otherwise create a new one. */
62 bb_error_msg_and_die("no utmp entry found");
64 memset(&utent, 0, sizeof(utent));
65 utent.ut_type = LOGIN_PROCESS;
67 strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line));
68 /* This one is only 4 chars wide. Try to fit something
69 * remotely meaningful by skipping "tty"... */
70 strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id));
71 strncpy(utent.ut_user, "LOGIN", sizeof(utent.ut_user));
72 utent.ut_time = time(NULL);
74 if (!picky) /* root login */
75 memset(utent.ut_host, 0, sizeof(utent.ut_host));
79 * write_utent - put a USER_PROCESS entry in the utmp file
81 * write_utent changes the type of the current utmp entry to
82 * USER_PROCESS. the wtmp file will be updated as well.
84 static void write_utent(const char *username)
86 utent.ut_type = USER_PROCESS;
87 strncpy(utent.ut_user, username, sizeof(utent.ut_user));
88 utent.ut_time = time(NULL);
89 /* other fields already filled in by read_or_build_utent above */
93 #if ENABLE_FEATURE_WTMP
94 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
95 close(creat(bb_path_wtmp_file, 0664));
97 updwtmp(bb_path_wtmp_file, &utent);
100 #else /* !CONFIG_FEATURE_UTMP */
101 static inline void read_or_build_utent(int) {}
102 static inline void write_utent(const char *) {}
103 #endif /* !CONFIG_FEATURE_UTMP */
105 static void die_if_nologin_and_non_root(int amroot)
110 if (access(bb_path_nologin_file, F_OK))
113 fp = fopen(bb_path_nologin_file, "r");
115 while ((c = getc(fp)) != EOF)
116 putchar((c=='\n') ? '\r' : c);
120 puts("\r\nSystem closed for routine maintenance\r");
123 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
126 #if ENABLE_FEATURE_SECURETTY
127 static int check_securetty(void)
133 fp = fopen(bb_path_securetty_file, "r");
135 /* A missing securetty file is not an error. */
138 while (fgets(buf, sizeof(buf)-1, fp)) {
139 for(i = strlen(buf)-1; i>=0; --i) {
140 if (!isspace(buf[i]))
144 if ((buf[0]=='\0') || (buf[0]=='#'))
146 if (strcmp(buf, short_tty) == 0) {
155 static inline int check_securetty(void) { return 1; }
158 static void get_username_or_die(char *buf, int size_buf)
161 cntdown = EMPTY_USERNAME_COUNT;
163 /* skip whitespace */
164 print_login_prompt();
167 if (c == EOF) exit(1);
169 if (!--cntdown) exit(1);
172 } while (isspace(c));
175 if (!fgets(buf, size_buf-2, stdin))
177 if (!strchr(buf, '\n'))
179 while (isgraph(*buf)) buf++;
183 static void motd(void)
188 fp = fopen(bb_path_motd_file, "r");
190 while ((c = getc(fp)) != EOF)
196 static void nonblock(int fd)
198 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
201 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
203 /* This is the escape hatch! Poor serial line users and the like
204 * arrive here when their connection is broken.
205 * We don't want to block here */
208 bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT);
212 int login_main(int argc, char **argv)
215 char username[USERNAME_SIZE];
221 int opt_preserve = 0;
224 #ifdef CONFIG_SELINUX
225 security_context_t user_sid = NULL;
229 amroot = (getuid() == 0);
230 signal(SIGALRM, alarm_handler);
233 while ((flag = getopt(argc, argv, "f:h:p")) != EOF) {
240 * username must be a separate token
241 * (-f root, *NOT* -froot). --marekm
243 if (optarg != argv[optind-1])
246 if (!amroot) /* Auth bypass only if real UID is zero */
247 bb_error_msg_and_die("-f is for root only");
249 safe_strncpy(username, optarg, sizeof(username));
259 if (optind < argc) /* user from command line (getty) */
260 safe_strncpy(username, argv[optind], sizeof(username));
262 /* Let's find out and memorize our tty */
263 if (!isatty(0) || !isatty(1) || !isatty(2))
264 return EXIT_FAILURE; /* Must be a terminal */
265 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
268 safe_strncpy(full_tty, tmp, sizeof(full_tty));
269 if (strncmp(full_tty, "/dev/", 5) == 0)
270 short_tty = full_tty + 5;
273 read_or_build_utent(!amroot);
276 if (ENABLE_FEATURE_UTMP)
277 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
278 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s' from "
279 "`%.200s'", short_tty, opt_host);
282 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s'", short_tty);
286 openlog(bb_applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
290 get_username_or_die(username, sizeof(username));
292 pw = getpwnam(username);
294 safe_strncpy(username, "UNKNOWN", sizeof(username));
298 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
302 break; /* -f USER: success without asking passwd */
304 if (pw->pw_uid == 0 && !check_securetty())
307 /* Don't check the password if password entry is empty (!) */
308 if (!pw->pw_passwd[0])
311 /* authorization takes place here */
312 if (correct_password(pw))
317 bb_do_delay(FAIL_DELAY);
318 puts("Login incorrect");
320 syslog(LOG_WARNING, "invalid password for `%s'%s",
328 die_if_nologin_and_non_root(pw->pw_uid == 0);
330 write_utent(username);
332 #ifdef CONFIG_SELINUX
333 if (is_selinux_enabled()) {
334 security_context_t old_tty_sid, new_tty_sid;
336 if (get_default_context(username, NULL, &user_sid)) {
337 bb_error_msg_and_die("unable to get SID for %s",
340 if (getfilecon(full_tty, &old_tty_sid) < 0) {
341 bb_perror_msg_and_die("getfilecon(%.100s) failed",
344 if (security_compute_relabel(user_sid, old_tty_sid,
345 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
346 bb_perror_msg_and_die("security_change_sid(%.100s) failed",
349 if (setfilecon(full_tty, new_tty_sid) != 0) {
350 bb_perror_msg_and_die("chsid(%.100s, %s) failed",
351 full_tty, new_tty_sid);
355 /* Try these, but don't complain if they fail.
356 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
357 fchown(0, pw->pw_uid, pw->pw_gid);
360 if (ENABLE_LOGIN_SCRIPTS) {
361 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
363 char *t_argv[2] = { script, NULL };
368 setenv("LOGIN_TTY", full_tty, 1);
369 setenv("LOGIN_USER", pw->pw_name, 1);
370 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
371 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
372 setenv("LOGIN_SHELL", pw->pw_shell, 1);
373 execvp(script, t_argv);
375 default: /* parent */
385 setup_environment(tmp, 1, !opt_preserve, pw);
388 signal(SIGALRM, SIG_DFL); /* default alarm signal */
391 syslog(LOG_INFO, "root login%s", fromhost);
392 #ifdef CONFIG_SELINUX
393 /* well, a simple setexeccon() here would do the job as well,
394 * but let's play the game for now */
395 set_current_security_context(user_sid);
397 run_shell(tmp, 1, 0, 0); /* exec the shell finally. */