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* short_tty;
27 #if ENABLE_FEATURE_UTMP
28 /* vv Taken from tinylogin utmp.c vv */
30 * read_or_build_utent - see if utmp file is correct for this process
32 * System V is very picky about the contents of the utmp file
33 * and requires that a slot for the current process exist.
34 * The utmp file is scanned for an entry with the same process
35 * ID. If no entry exists the process exits with a message.
37 * The "picky" flag is for network and other logins that may
38 * use special flags. It allows the pid checks to be overridden.
39 * This means that getty should never invoke login with any
43 static void read_or_build_utent(struct utmp *utptr, int picky)
50 /* First, try to find a valid utmp entry for this process. */
51 while ((ut = getutent()))
52 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
53 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
56 /* If there is one, just use it, otherwise create a new one. */
61 bb_error_msg_and_die("no utmp entry found");
63 memset(utptr, 0, sizeof(*utptr));
64 utptr->ut_type = LOGIN_PROCESS;
66 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
67 /* This one is only 4 chars wide. Try to fit something
68 * remotely meaningful by skipping "tty"... */
69 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
70 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
71 utptr->ut_time = time(NULL);
73 if (!picky) /* root login */
74 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
78 * write_utent - put a USER_PROCESS entry in the utmp file
80 * write_utent changes the type of the current utmp entry to
81 * USER_PROCESS. the wtmp file will be updated as well.
83 static void write_utent(struct utmp *utptr, const char *username)
85 utptr->ut_type = USER_PROCESS;
86 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
87 utptr->ut_time = time(NULL);
88 /* other fields already filled in by read_or_build_utent above */
92 #if ENABLE_FEATURE_WTMP
93 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
94 close(creat(bb_path_wtmp_file, 0664));
96 updwtmp(bb_path_wtmp_file, utptr);
99 #else /* !ENABLE_FEATURE_UTMP */
100 #define read_or_build_utent(utptr, picky) ((void)0)
101 #define write_utent(utptr, username) ((void)0)
102 #endif /* !ENABLE_FEATURE_UTMP */
104 static void die_if_nologin_and_non_root(int amroot)
109 if (access(bb_path_nologin_file, F_OK))
112 fp = fopen(bb_path_nologin_file, "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 #if ENABLE_FEATURE_SECURETTY
126 static int check_securetty(void)
132 fp = fopen(bb_path_securetty_file, "r");
134 /* A missing securetty file is not an error. */
137 while (fgets(buf, sizeof(buf)-1, fp)) {
138 for (i = strlen(buf)-1; i>=0; --i) {
139 if (!isspace(buf[i]))
143 if ((buf[0]=='\0') || (buf[0]=='#'))
145 if (strcmp(buf, short_tty) == 0) {
154 static inline int check_securetty(void) { return 1; }
157 static void get_username_or_die(char *buf, int size_buf)
160 cntdown = EMPTY_USERNAME_COUNT;
162 /* skip whitespace */
163 print_login_prompt();
166 if (c == EOF) exit(1);
168 if (!--cntdown) exit(1);
171 } while (isspace(c));
174 if (!fgets(buf, size_buf-2, stdin))
176 if (!strchr(buf, '\n'))
178 while (isgraph(*buf)) buf++;
182 static void motd(void)
186 fd = open(bb_path_motd_file, O_RDONLY);
189 bb_copyfd_eof(fd, STDOUT_FILENO);
194 static void alarm_handler(int sig ATTRIBUTE_UNUSED)
196 /* This is the escape hatch! Poor serial line users and the like
197 * arrive here when their connection is broken.
198 * We don't want to block here */
201 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
205 int login_main(int argc, char **argv);
206 int login_main(int argc, char **argv)
209 LOGIN_OPT_f = (1<<0),
210 LOGIN_OPT_h = (1<<1),
211 LOGIN_OPT_p = (1<<2),
214 char username[USERNAME_SIZE];
220 char *opt_host = NULL;
221 char *opt_user = NULL;
222 char full_tty[TTYNAME_SIZE];
223 USE_SELINUX(security_context_t user_sid = NULL;)
224 USE_FEATURE_UTMP(struct utmp utent;)
226 short_tty = full_tty;
228 amroot = (getuid() == 0);
229 signal(SIGALRM, alarm_handler);
232 /* Mandatory paranoia for suid applet:
233 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
234 * and any extra open fd's are closed.
235 * (The name of the function is misleading. Not daemonizing here.) */
236 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
238 opt = getopt32(argc, argv, "f:h:p", &opt_user, &opt_host);
239 if (opt & LOGIN_OPT_f) {
241 bb_error_msg_and_die("-f is for root only");
242 safe_strncpy(username, opt_user, sizeof(username));
244 if (optind < argc) /* user from command line (getty) */
245 safe_strncpy(username, argv[optind], sizeof(username));
247 /* Let's find out and memorize our tty */
248 if (!isatty(0) || !isatty(1) || !isatty(2))
249 return EXIT_FAILURE; /* Must be a terminal */
250 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
253 safe_strncpy(full_tty, tmp, sizeof(full_tty));
254 if (strncmp(full_tty, "/dev/", 5) == 0)
255 short_tty = full_tty + 5;
258 read_or_build_utent(&utent, !amroot);
262 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
264 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s' from "
265 "'%.200s'", short_tty, opt_host);
267 snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s'", short_tty);
269 // Was breaking "login <username>" from shell command line:
272 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
276 get_username_or_die(username, sizeof(username));
278 pw = getpwnam(username);
280 safe_strncpy(username, "UNKNOWN", sizeof(username));
284 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
287 if (opt & LOGIN_OPT_f)
288 break; /* -f USER: success without asking passwd */
290 if (pw->pw_uid == 0 && !check_securetty())
293 /* Don't check the password if password entry is empty (!) */
294 if (!pw->pw_passwd[0])
297 /* authorization takes place here */
298 if (correct_password(pw))
303 bb_do_delay(FAIL_DELAY);
304 puts("Login incorrect");
306 syslog(LOG_WARNING, "invalid password for '%s'%s",
314 die_if_nologin_and_non_root(pw->pw_uid == 0);
316 write_utent(&utent, username);
318 #ifdef CONFIG_SELINUX
319 if (is_selinux_enabled()) {
320 security_context_t old_tty_sid, new_tty_sid;
322 if (get_default_context(username, NULL, &user_sid)) {
323 bb_error_msg_and_die("cannot get SID for %s",
326 if (getfilecon(full_tty, &old_tty_sid) < 0) {
327 bb_perror_msg_and_die("getfilecon(%s) failed",
330 if (security_compute_relabel(user_sid, old_tty_sid,
331 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
332 bb_perror_msg_and_die("security_change_sid(%s) failed",
335 if (setfilecon(full_tty, new_tty_sid) != 0) {
336 bb_perror_msg_and_die("chsid(%s, %s) failed",
337 full_tty, new_tty_sid);
341 /* Try these, but don't complain if they fail.
342 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
343 fchown(0, pw->pw_uid, pw->pw_gid);
346 if (ENABLE_LOGIN_SCRIPTS) {
349 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
352 xsetenv("LOGIN_TTY", full_tty);
353 xsetenv("LOGIN_USER", pw->pw_name);
354 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
355 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
356 xsetenv("LOGIN_SHELL", pw->pw_shell);
357 xspawn(t_argv); /* NOMMU-friendly */
358 /* All variables are unset by setup_environment */
367 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
372 syslog(LOG_INFO, "root login%s", fromhost);
373 #ifdef CONFIG_SELINUX
374 /* well, a simple setexeccon() here would do the job as well,
375 * but let's play the game for now */
376 set_current_security_context(user_sid);
379 // util-linux login also does:
380 // /* start new session */
382 // /* TIOCSCTTY: steal tty from other process group */
383 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
384 // BBox login used to do this (see above):
386 // If this stuff is really needed, add it and explain why!
388 /* set signals to defaults */
389 signal(SIGALRM, SIG_DFL);
390 /* Is this correct? This way user can ctrl-c out of /etc/profile,
391 * potentially creating security breach (tested with bash 3.0).
392 * But without this, bash 3.0 will not enable ctrl-c either.
393 * Maybe bash is buggy?
394 * Need to find out what standards say about /bin/login -
395 * should it leave SIGINT etc enabled or disabled? */
396 signal(SIGINT, SIG_DFL);
398 run_shell(tmp, 1, 0, 0); /* exec the shell finally */