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 EMPTY_USERNAME_COUNT = 10,
24 static char* short_tty;
26 #if ENABLE_FEATURE_UTMP
27 /* vv Taken from tinylogin utmp.c vv */
29 * read_or_build_utent - see if utmp file is correct for this process
31 * System V is very picky about the contents of the utmp file
32 * and requires that a slot for the current process exist.
33 * The utmp file is scanned for an entry with the same process
34 * ID. If no entry exists the process exits with a message.
36 * The "picky" flag is for network and other logins that may
37 * use special flags. It allows the pid checks to be overridden.
38 * This means that getty should never invoke login with any
42 static void read_or_build_utent(struct utmp *utptr, int picky)
49 /* First, try to find a valid utmp entry for this process. */
50 while ((ut = getutent()))
51 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
52 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
55 /* If there is one, just use it, otherwise create a new one. */
60 bb_error_msg_and_die("no utmp entry found");
62 memset(utptr, 0, sizeof(*utptr));
63 utptr->ut_type = LOGIN_PROCESS;
65 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
66 /* This one is only 4 chars wide. Try to fit something
67 * remotely meaningful by skipping "tty"... */
68 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
69 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
70 utptr->ut_time = time(NULL);
72 if (!picky) /* root login */
73 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
77 * write_utent - put a USER_PROCESS entry in the utmp file
79 * write_utent changes the type of the current utmp entry to
80 * USER_PROCESS. the wtmp file will be updated as well.
82 static void write_utent(struct utmp *utptr, const char *username)
84 utptr->ut_type = USER_PROCESS;
85 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
86 utptr->ut_time = time(NULL);
87 /* other fields already filled in by read_or_build_utent above */
91 #if ENABLE_FEATURE_WTMP
92 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
93 close(creat(bb_path_wtmp_file, 0664));
95 updwtmp(bb_path_wtmp_file, utptr);
98 #else /* !ENABLE_FEATURE_UTMP */
99 #define read_or_build_utent(utptr, picky) ((void)0)
100 #define write_utent(utptr, username) ((void)0)
101 #endif /* !ENABLE_FEATURE_UTMP */
103 #if ENABLE_FEATURE_NOLOGIN
104 static void die_if_nologin_and_non_root(int amroot)
109 if (access("/etc/nologin", F_OK))
112 fp = fopen("/etc/nologin", "r");
114 while ((c = getc(fp)) != EOF)
115 putchar((c=='\n') ? '\r' : c);
119 puts("\r\nSystem closed for routine maintenance\r");
122 puts("\r\n[Disconnect bypassed -- root login allowed]\r");
125 static ALWAYS_INLINE void die_if_nologin_and_non_root(int amroot) {}
128 #if ENABLE_FEATURE_SECURETTY
129 static int check_securetty(void)
135 fp = fopen("/etc/securetty", "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] || (buf[0] == '#'))
148 if (strcmp(buf, short_tty) == 0) {
157 static ALWAYS_INLINE int check_securetty(void) { return 1; }
160 static void get_username_or_die(char *buf, int size_buf)
164 cntdown = EMPTY_USERNAME_COUNT;
166 print_login_prompt();
167 /* skip whitespace */
170 if (c == EOF) exit(1);
172 if (!--cntdown) exit(1);
175 } while (isspace(c));
178 if (!fgets(buf, size_buf-2, stdin))
180 if (!strchr(buf, '\n'))
182 while (isgraph(*buf)) buf++;
186 static void motd(void)
190 fd = open(bb_path_motd_file, O_RDONLY);
193 bb_copyfd_eof(fd, STDOUT_FILENO);
198 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
200 /* This is the escape hatch! Poor serial line users and the like
201 * arrive here when their connection is broken.
202 * We don't want to block here */
205 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
209 int login_main(int argc, char **argv);
210 int login_main(int argc, char **argv)
213 LOGIN_OPT_f = (1<<0),
214 LOGIN_OPT_h = (1<<1),
215 LOGIN_OPT_p = (1<<2),
218 char username[USERNAME_SIZE];
224 char *opt_host = NULL;
225 char *opt_user = NULL;
226 char full_tty[TTYNAME_SIZE];
227 USE_SELINUX(security_context_t user_sid = NULL;)
228 USE_FEATURE_UTMP(struct utmp utent;)
230 short_tty = full_tty;
232 amroot = (getuid() == 0);
233 signal(SIGALRM, alarm_handler);
236 /* Mandatory paranoia for suid applet:
237 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
238 * and any extra open fd's are closed.
239 * (The name of the function is misleading. Not daemonizing here.) */
240 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
242 opt = getopt32(argc, argv, "f:h:p", &opt_user, &opt_host);
243 if (opt & LOGIN_OPT_f) {
245 bb_error_msg_and_die("-f is for root only");
246 safe_strncpy(username, opt_user, sizeof(username));
248 if (optind < argc) /* user from command line (getty) */
249 safe_strncpy(username, argv[optind], sizeof(username));
251 /* Let's find out and memorize our tty */
252 if (!isatty(0) || !isatty(1) || !isatty(2))
253 return EXIT_FAILURE; /* Must be a terminal */
254 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
257 safe_strncpy(full_tty, tmp, sizeof(full_tty));
258 if (strncmp(full_tty, "/dev/", 5) == 0)
259 short_tty = full_tty + 5;
262 read_or_build_utent(&utent, !amroot);
266 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
268 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s' from "
269 "'%.200s'", short_tty, opt_host);
271 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s'", short_tty);
273 // Was breaking "login <username>" from shell command line:
276 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
280 get_username_or_die(username, sizeof(username));
282 pw = getpwnam(username);
284 strcpy(username, "UNKNOWN");
288 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
291 if (opt & LOGIN_OPT_f)
292 break; /* -f USER: success without asking passwd */
294 if (pw->pw_uid == 0 && !check_securetty())
297 /* Don't check the password if password entry is empty (!) */
298 if (!pw->pw_passwd[0])
301 /* authorization takes place here */
302 if (correct_password(pw))
306 bb_do_delay(FAIL_DELAY);
307 puts("Login incorrect");
309 syslog(LOG_WARNING, "invalid password for '%s'%s",
317 die_if_nologin_and_non_root(pw->pw_uid == 0);
319 write_utent(&utent, username);
322 if (is_selinux_enabled()) {
323 security_context_t old_tty_sid, new_tty_sid;
325 if (get_default_context(username, NULL, &user_sid)) {
326 bb_error_msg_and_die("cannot get SID for %s",
329 if (getfilecon(full_tty, &old_tty_sid) < 0) {
330 bb_perror_msg_and_die("getfilecon(%s) failed",
333 if (security_compute_relabel(user_sid, old_tty_sid,
334 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
335 bb_perror_msg_and_die("security_change_sid(%s) failed",
338 if (setfilecon(full_tty, new_tty_sid) != 0) {
339 bb_perror_msg_and_die("chsid(%s, %s) failed",
340 full_tty, new_tty_sid);
344 /* Try these, but don't complain if they fail.
345 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
346 fchown(0, pw->pw_uid, pw->pw_gid);
349 if (ENABLE_LOGIN_SCRIPTS) {
352 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
355 xsetenv("LOGIN_TTY", full_tty);
356 xsetenv("LOGIN_USER", pw->pw_name);
357 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
358 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
359 xsetenv("LOGIN_SHELL", pw->pw_shell);
360 xspawn(t_argv); /* NOMMU-friendly */
361 /* All variables are unset by setup_environment */
370 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
375 syslog(LOG_INFO, "root login%s", fromhost);
377 /* well, a simple setexeccon() here would do the job as well,
378 * but let's play the game for now */
379 set_current_security_context(user_sid);
382 // util-linux login also does:
383 // /* start new session */
385 // /* TIOCSCTTY: steal tty from other process group */
386 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
387 // BBox login used to do this (see above):
389 // If this stuff is really needed, add it and explain why!
391 /* set signals to defaults */
392 signal(SIGALRM, SIG_DFL);
393 /* Is this correct? This way user can ctrl-c out of /etc/profile,
394 * potentially creating security breach (tested with bash 3.0).
395 * But without this, bash 3.0 will not enable ctrl-c either.
396 * Maybe bash is buggy?
397 * Need to find out what standards say about /bin/login -
398 * should it leave SIGINT etc enabled or disabled? */
399 signal(SIGINT, SIG_DFL);
401 run_shell(tmp, 1, 0, 0); /* exec the shell finally */