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 static void die_if_nologin_and_non_root(int amroot)
108 if (access(bb_path_nologin_file, F_OK))
111 fp = fopen(bb_path_nologin_file, "r");
113 while ((c = getc(fp)) != EOF)
114 putchar((c=='\n') ? '\r' : c);
118 puts("\r\nSystem closed for routine maintenance\r");
121 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
124 #if ENABLE_FEATURE_SECURETTY
125 static int check_securetty(void)
131 fp = fopen(bb_path_securetty_file, "r");
133 /* A missing securetty file is not an error. */
136 while (fgets(buf, sizeof(buf)-1, fp)) {
137 for (i = strlen(buf)-1; i>=0; --i) {
138 if (!isspace(buf[i]))
142 if ((buf[0]=='\0') || (buf[0]=='#'))
144 if (strcmp(buf, short_tty) == 0) {
153 static inline int check_securetty(void) { return 1; }
156 static void get_username_or_die(char *buf, int size_buf)
159 cntdown = EMPTY_USERNAME_COUNT;
161 /* skip whitespace */
162 print_login_prompt();
165 if (c == EOF) exit(1);
167 if (!--cntdown) exit(1);
170 } while (isspace(c));
173 if (!fgets(buf, size_buf-2, stdin))
175 if (!strchr(buf, '\n'))
177 while (isgraph(*buf)) buf++;
181 static void motd(void)
185 fd = open(bb_path_motd_file, O_RDONLY);
188 bb_copyfd_eof(fd, STDOUT_FILENO);
193 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
195 /* This is the escape hatch! Poor serial line users and the like
196 * arrive here when their connection is broken.
197 * We don't want to block here */
200 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
204 int login_main(int argc, char **argv);
205 int login_main(int argc, char **argv)
208 LOGIN_OPT_f = (1<<0),
209 LOGIN_OPT_h = (1<<1),
210 LOGIN_OPT_p = (1<<2),
213 char username[USERNAME_SIZE];
219 char *opt_host = NULL;
220 char *opt_user = NULL;
221 char full_tty[TTYNAME_SIZE];
222 USE_SELINUX(security_context_t user_sid = NULL;)
223 USE_FEATURE_UTMP(struct utmp utent;)
225 short_tty = full_tty;
227 amroot = (getuid() == 0);
228 signal(SIGALRM, alarm_handler);
231 /* Mandatory paranoia for suid applet:
232 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
233 * and any extra open fd's are closed.
234 * (The name of the function is misleading. Not daemonizing here.) */
235 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
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(&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);
266 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s'", short_tty);
268 // Was breaking "login <username>" from shell command line:
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 strcpy(username, "UNKNOWN");
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))
301 bb_do_delay(FAIL_DELAY);
302 puts("Login incorrect");
304 syslog(LOG_WARNING, "invalid password for '%s'%s",
312 die_if_nologin_and_non_root(pw->pw_uid == 0);
314 write_utent(&utent, username);
316 #ifdef CONFIG_SELINUX
317 if (is_selinux_enabled()) {
318 security_context_t old_tty_sid, new_tty_sid;
320 if (get_default_context(username, NULL, &user_sid)) {
321 bb_error_msg_and_die("cannot get SID for %s",
324 if (getfilecon(full_tty, &old_tty_sid) < 0) {
325 bb_perror_msg_and_die("getfilecon(%s) failed",
328 if (security_compute_relabel(user_sid, old_tty_sid,
329 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
330 bb_perror_msg_and_die("security_change_sid(%s) failed",
333 if (setfilecon(full_tty, new_tty_sid) != 0) {
334 bb_perror_msg_and_die("chsid(%s, %s) failed",
335 full_tty, new_tty_sid);
339 /* Try these, but don't complain if they fail.
340 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
341 fchown(0, pw->pw_uid, pw->pw_gid);
344 if (ENABLE_LOGIN_SCRIPTS) {
347 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
350 xsetenv("LOGIN_TTY", full_tty);
351 xsetenv("LOGIN_USER", pw->pw_name);
352 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
353 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
354 xsetenv("LOGIN_SHELL", pw->pw_shell);
355 xspawn(t_argv); /* NOMMU-friendly */
356 /* All variables are unset by setup_environment */
365 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
370 syslog(LOG_INFO, "root login%s", fromhost);
371 #ifdef CONFIG_SELINUX
372 /* well, a simple setexeccon() here would do the job as well,
373 * but let's play the game for now */
374 set_current_security_context(user_sid);
377 // util-linux login also does:
378 // /* start new session */
380 // /* TIOCSCTTY: steal tty from other process group */
381 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
382 // BBox login used to do this (see above):
384 // If this stuff is really needed, add it and explain why!
386 /* set signals to defaults */
387 signal(SIGALRM, SIG_DFL);
388 /* Is this correct? This way user can ctrl-c out of /etc/profile,
389 * potentially creating security breach (tested with bash 3.0).
390 * But without this, bash 3.0 will not enable ctrl-c either.
391 * Maybe bash is buggy?
392 * Need to find out what standards say about /bin/login -
393 * should it leave SIGINT etc enabled or disabled? */
394 signal(SIGINT, SIG_DFL);
396 run_shell(tmp, 1, 0, 0); /* exec the shell finally */