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
44 static struct utmp utent;
46 static void read_or_build_utent(int picky)
53 /* First, try to find a valid utmp entry for this process. */
54 while ((ut = getutent()))
55 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
56 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
59 /* If there is one, just use it, otherwise create a new one. */
64 bb_error_msg_and_die("no utmp entry found");
66 memset(&utent, 0, sizeof(utent));
67 utent.ut_type = LOGIN_PROCESS;
69 strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line));
70 /* This one is only 4 chars wide. Try to fit something
71 * remotely meaningful by skipping "tty"... */
72 strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id));
73 strncpy(utent.ut_user, "LOGIN", sizeof(utent.ut_user));
74 utent.ut_time = time(NULL);
76 if (!picky) /* root login */
77 memset(utent.ut_host, 0, sizeof(utent.ut_host));
81 * write_utent - put a USER_PROCESS entry in the utmp file
83 * write_utent changes the type of the current utmp entry to
84 * USER_PROCESS. the wtmp file will be updated as well.
86 static void write_utent(const char *username)
88 utent.ut_type = USER_PROCESS;
89 strncpy(utent.ut_user, username, sizeof(utent.ut_user));
90 utent.ut_time = time(NULL);
91 /* other fields already filled in by read_or_build_utent above */
95 #if ENABLE_FEATURE_WTMP
96 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
97 close(creat(bb_path_wtmp_file, 0664));
99 updwtmp(bb_path_wtmp_file, &utent);
102 #else /* !ENABLE_FEATURE_UTMP */
103 static inline void read_or_build_utent(int ATTRIBUTE_UNUSED picky) {}
104 static inline void write_utent(const char ATTRIBUTE_UNUSED *username) {}
105 #endif /* !ENABLE_FEATURE_UTMP */
107 static void die_if_nologin_and_non_root(int amroot)
112 if (access(bb_path_nologin_file, F_OK))
115 fp = fopen(bb_path_nologin_file, "r");
117 while ((c = getc(fp)) != EOF)
118 putchar((c=='\n') ? '\r' : c);
122 puts("\r\nSystem closed for routine maintenance\r");
125 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
128 #if ENABLE_FEATURE_SECURETTY
129 static int check_securetty(void)
135 fp = fopen(bb_path_securetty_file, "r");
137 /* A missing securetty file is not an error. */
140 while (fgets(buf, sizeof(buf)-1, fp)) {
141 for(i = strlen(buf)-1; i>=0; --i) {
142 if (!isspace(buf[i]))
146 if ((buf[0]=='\0') || (buf[0]=='#'))
148 if (strcmp(buf, short_tty) == 0) {
157 static inline int check_securetty(void) { return 1; }
160 static void get_username_or_die(char *buf, int size_buf)
163 cntdown = EMPTY_USERNAME_COUNT;
165 /* skip whitespace */
166 print_login_prompt();
169 if (c == EOF) exit(1);
171 if (!--cntdown) exit(1);
174 } while (isspace(c));
177 if (!fgets(buf, size_buf-2, stdin))
179 if (!strchr(buf, '\n'))
181 while (isgraph(*buf)) buf++;
185 static void motd(void)
190 fp = fopen(bb_path_motd_file, "r");
192 while ((c = getc(fp)) != EOF)
198 static void nonblock(int fd)
200 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
203 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
205 /* This is the escape hatch! Poor serial line users and the like
206 * arrive here when their connection is broken.
207 * We don't want to block here */
210 bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT);
214 int login_main(int argc, char **argv)
217 LOGIN_OPT_f = (1<<0),
218 LOGIN_OPT_h = (1<<1),
219 LOGIN_OPT_p = (1<<2),
222 char username[USERNAME_SIZE];
228 char *opt_host = NULL;
229 char *opt_user = NULL;
230 USE_SELINUX(security_context_t user_sid = NULL;)
233 amroot = (getuid() == 0);
234 signal(SIGALRM, alarm_handler);
237 opt = getopt32(argc, argv, "f:h:p", &opt_user, &opt_host);
238 if (opt & LOGIN_OPT_f) {
240 bb_error_msg_and_die("-f is for root only");
241 safe_strncpy(username, opt_user, sizeof(username));
243 if (optind < argc) /* user from command line (getty) */
244 safe_strncpy(username, argv[optind], sizeof(username));
246 /* Let's find out and memorize our tty */
247 if (!isatty(0) || !isatty(1) || !isatty(2))
248 return EXIT_FAILURE; /* Must be a terminal */
249 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
252 safe_strncpy(full_tty, tmp, sizeof(full_tty));
253 if (strncmp(full_tty, "/dev/", 5) == 0)
254 short_tty = full_tty + 5;
257 read_or_build_utent(!amroot);
261 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
263 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s' from "
264 "'%.200s'", short_tty, opt_host);
267 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s'", short_tty);
271 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
275 get_username_or_die(username, sizeof(username));
277 pw = getpwnam(username);
279 safe_strncpy(username, "UNKNOWN", sizeof(username));
283 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
286 if (opt & LOGIN_OPT_f)
287 break; /* -f USER: success without asking passwd */
289 if (pw->pw_uid == 0 && !check_securetty())
292 /* Don't check the password if password entry is empty (!) */
293 if (!pw->pw_passwd[0])
296 /* authorization takes place here */
297 if (correct_password(pw))
302 bb_do_delay(FAIL_DELAY);
303 puts("Login incorrect");
305 syslog(LOG_WARNING, "invalid password for '%s'%s",
313 die_if_nologin_and_non_root(pw->pw_uid == 0);
315 write_utent(username);
317 #ifdef CONFIG_SELINUX
318 if (is_selinux_enabled()) {
319 security_context_t old_tty_sid, new_tty_sid;
321 if (get_default_context(username, NULL, &user_sid)) {
322 bb_error_msg_and_die("cannot get SID for %s",
325 if (getfilecon(full_tty, &old_tty_sid) < 0) {
326 bb_perror_msg_and_die("getfilecon(%.100s) failed",
329 if (security_compute_relabel(user_sid, old_tty_sid,
330 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
331 bb_perror_msg_and_die("security_change_sid(%.100s) failed",
334 if (setfilecon(full_tty, new_tty_sid) != 0) {
335 bb_perror_msg_and_die("chsid(%.100s, %s) failed",
336 full_tty, new_tty_sid);
340 /* Try these, but don't complain if they fail.
341 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
342 fchown(0, pw->pw_uid, pw->pw_gid);
345 /* TODO: be nommu-friendly, use spawn? */
346 if (ENABLE_LOGIN_SCRIPTS) {
347 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
349 char *t_argv[2] = { script, NULL };
354 setenv("LOGIN_TTY", full_tty, 1);
355 setenv("LOGIN_USER", pw->pw_name, 1);
356 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
357 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
358 setenv("LOGIN_SHELL", pw->pw_shell, 1);
359 execvp(script, t_argv);
361 default: /* parent */
371 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
376 syslog(LOG_INFO, "root login%s", fromhost);
377 #ifdef CONFIG_SELINUX
378 /* well, a simple setexeccon() here would do the job as well,
379 * but let's play the game for now */
380 set_current_security_context(user_sid);
383 // util-linux login also does:
384 // /* start new session */
386 // /* TIOCSCTTY: steal tty from other process group */
387 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
389 /* set signals to defaults */
390 signal(SIGALRM, SIG_DFL);
391 /* Is this correct? This way user can ctrl-c out of /etc/profile,
392 * potentially creating security breach (tested with bash 3.0).
393 * But without this, bash 3.0 will not enable ctrl-c either.
394 * Maybe bash is buggy?
395 * Need to find out what standards say about /bin/login -
396 * should it leave SIGINT etc enabled or disabled? */
397 signal(SIGINT, SIG_DFL);
399 run_shell(tmp, 1, 0, 0); /* exec the shell finally */