blob: 1f392c10f7926141dc8f080f22845d20510f3ac8 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
Bernhard Reutner-Fischer92582f22006-09-13 15:42:47 +00006#include "busybox.h"
Robert Griebl1fca5582002-06-04 20:45:46 +00007#include <utmp.h>
8#include <sys/resource.h>
Bernhard Reutner-Fischer92582f22006-09-13 15:42:47 +00009#include <syslog.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000010
Eric Andersen9e480452003-07-03 10:07:04 +000011#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000012#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 */
15#include <errno.h>
Eric Andersen9e480452003-07-03 10:07:04 +000016#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000017
Denis Vlasenko942e4292006-09-08 17:25:04 +000018enum {
19 TIMEOUT = 60,
20 EMPTY_USERNAME_COUNT = 10,
21 USERNAME_SIZE = 32,
Denis Vlasenko9a9edf22006-09-08 17:29:53 +000022 TTYNAME_SIZE = 32,
Denis Vlasenko942e4292006-09-08 17:25:04 +000023};
Eric Andersen0fbff132002-06-22 17:49:29 +000024
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000025static char full_tty[TTYNAME_SIZE];
26static char* short_tty = full_tty;
27
28#if ENABLE_FEATURE_UTMP
29/* vv Taken from tinylogin utmp.c vv */
30/*
31 * read_or_build_utent - see if utmp file is correct for this process
32 *
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.
37 *
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
41 * command line flags.
42 */
Denis Vlasenko66fabdb2006-09-17 14:45:09 +000043
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000044static struct utmp utent;
Denis Vlasenko66fabdb2006-09-17 14:45:09 +000045
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000046static void read_or_build_utent(int picky)
47{
48 struct utmp *ut;
49 pid_t pid = getpid();
50
51 setutent();
52
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))
57 break;
58
59 /* If there is one, just use it, otherwise create a new one. */
60 if (ut) {
61 utent = *ut;
62 } else {
63 if (picky)
64 bb_error_msg_and_die("no utmp entry found");
65
66 memset(&utent, 0, sizeof(utent));
67 utent.ut_type = LOGIN_PROCESS;
68 utent.ut_pid = pid;
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);
75 }
76 if (!picky) /* root login */
77 memset(utent.ut_host, 0, sizeof(utent.ut_host));
78}
79
80/*
81 * write_utent - put a USER_PROCESS entry in the utmp file
82 *
83 * write_utent changes the type of the current utmp entry to
84 * USER_PROCESS. the wtmp file will be updated as well.
85 */
86static void write_utent(const char *username)
87{
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 */
92 setutent();
93 pututline(&utent);
94 endutent();
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));
98 }
99 updwtmp(bb_path_wtmp_file, &utent);
100#endif
101}
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000102#else /* !ENABLE_FEATURE_UTMP */
103static inline void read_or_build_utent(int ATTRIBUTE_UNUSED picky) {}
104static inline void write_utent(const char ATTRIBUTE_UNUSED *username) {}
105#endif /* !ENABLE_FEATURE_UTMP */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000106
107static void die_if_nologin_and_non_root(int amroot)
108{
109 FILE *fp;
110 int c;
111
112 if (access(bb_path_nologin_file, F_OK))
113 return;
114
115 fp = fopen(bb_path_nologin_file, "r");
116 if (fp) {
117 while ((c = getc(fp)) != EOF)
118 putchar((c=='\n') ? '\r' : c);
119 fflush(stdout);
120 fclose(fp);
121 } else
122 puts("\r\nSystem closed for routine maintenance\r");
123 if (!amroot)
124 exit(1);
125 puts("\r\n[Disconnect bypassed -- root login allowed.]\r");
126}
Robert Griebl1fca5582002-06-04 20:45:46 +0000127
Bernhard Reutner-Fischer92582f22006-09-13 15:42:47 +0000128#if ENABLE_FEATURE_SECURETTY
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000129static int check_securetty(void)
130{
131 FILE *fp;
132 int i;
133 char buf[BUFSIZ];
134
135 fp = fopen(bb_path_securetty_file, "r");
136 if (!fp) {
137 /* A missing securetty file is not an error. */
138 return 1;
139 }
140 while (fgets(buf, sizeof(buf)-1, fp)) {
141 for(i = strlen(buf)-1; i>=0; --i) {
142 if (!isspace(buf[i]))
143 break;
144 }
145 buf[++i] = '\0';
146 if ((buf[0]=='\0') || (buf[0]=='#'))
147 continue;
148 if (strcmp(buf, short_tty) == 0) {
149 fclose(fp);
150 return 1;
151 }
152 }
153 fclose(fp);
154 return 0;
155}
Robert Griebl1fca5582002-06-04 20:45:46 +0000156#else
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000157static inline int check_securetty(void) { return 1; }
Robert Griebl1fca5582002-06-04 20:45:46 +0000158#endif
159
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000160static void get_username_or_die(char *buf, int size_buf)
161{
162 int c, cntdown;
163 cntdown = EMPTY_USERNAME_COUNT;
164prompt:
165 /* skip whitespace */
166 print_login_prompt();
167 do {
168 c = getchar();
169 if (c == EOF) exit(1);
170 if (c == '\n') {
171 if (!--cntdown) exit(1);
172 goto prompt;
173 }
174 } while (isspace(c));
175
176 *buf++ = c;
177 if (!fgets(buf, size_buf-2, stdin))
178 exit(1);
179 if (!strchr(buf, '\n'))
180 exit(1);
181 while (isgraph(*buf)) buf++;
182 *buf = '\0';
183}
184
185static void motd(void)
186{
187 FILE *fp;
188 int c;
189
190 fp = fopen(bb_path_motd_file, "r");
191 if (fp) {
192 while ((c = getc(fp)) != EOF)
193 putchar(c);
194 fclose(fp);
195 }
196}
Robert Griebl1fca5582002-06-04 20:45:46 +0000197
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000198static void nonblock(int fd)
199{
200 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
201}
Robert Griebl1fca5582002-06-04 20:45:46 +0000202
Denis Vlasenko942e4292006-09-08 17:25:04 +0000203static void alarm_handler(int sig ATTRIBUTE_UNUSED)
Robert Griebl1fca5582002-06-04 20:45:46 +0000204{
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000205 /* 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 */
208 nonblock(1);
209 nonblock(2);
210 bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT);
Denis Vlasenko942e4292006-09-08 17:25:04 +0000211 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000212}
213
Rob Landleydfba7412006-03-06 20:47:33 +0000214int login_main(int argc, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000215{
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000216 enum {
217 LOGIN_OPT_f = (1<<0),
218 LOGIN_OPT_h = (1<<1),
219 LOGIN_OPT_p = (1<<2),
220 };
Robert Griebl1fca5582002-06-04 20:45:46 +0000221 char fromhost[512];
Eric Andersen0fbff132002-06-22 17:49:29 +0000222 char username[USERNAME_SIZE];
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000223 const char *tmp;
Robert Griebl1fca5582002-06-04 20:45:46 +0000224 int amroot;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000225 unsigned opt;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000226 int count = 0;
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000227 struct passwd *pw;
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000228 char *opt_host = NULL;
229 char *opt_user = NULL;
230 USE_SELINUX(security_context_t user_sid = NULL;)
Robert Griebl1fca5582002-06-04 20:45:46 +0000231
Denis Vlasenko942e4292006-09-08 17:25:04 +0000232 username[0] = '\0';
233 amroot = (getuid() == 0);
234 signal(SIGALRM, alarm_handler);
235 alarm(TIMEOUT);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000236
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000237 opt = getopt32(argc, argv, "f:h:p", &opt_user, &opt_host);
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000238 if (opt & LOGIN_OPT_f) {
239 if (!amroot)
240 bb_error_msg_and_die("-f is for root only");
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000241 safe_strncpy(username, opt_user, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000242 }
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000243 if (optind < argc) /* user from command line (getty) */
244 safe_strncpy(username, argv[optind], sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000245
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000246 /* Let's find out and memorize our tty */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000247 if (!isatty(0) || !isatty(1) || !isatty(2))
Robert Griebl1fca5582002-06-04 20:45:46 +0000248 return EXIT_FAILURE; /* Must be a terminal */
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000249 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
Denis Vlasenko942e4292006-09-08 17:25:04 +0000250 tmp = ttyname(0);
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000251 if (tmp) {
252 safe_strncpy(full_tty, tmp, sizeof(full_tty));
253 if (strncmp(full_tty, "/dev/", 5) == 0)
254 short_tty = full_tty + 5;
255 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000256
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000257 read_or_build_utent(!amroot);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000258
Denis Vlasenko942e4292006-09-08 17:25:04 +0000259 if (opt_host) {
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000260 USE_FEATURE_UTMP(
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000261 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000262 )
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000263 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s' from "
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000264 "`%.200s'", short_tty, opt_host);
Robert Griebl1fca5582002-06-04 20:45:46 +0000265 }
266 else
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000267 snprintf(fromhost, sizeof(fromhost)-1, " on `%.100s'", short_tty);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000268
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +0000269 bb_setpgrp;
Eric Andersen0fbff132002-06-22 17:49:29 +0000270
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000271 openlog(bb_applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000272
Denis Vlasenko942e4292006-09-08 17:25:04 +0000273 while (1) {
Denis Vlasenko942e4292006-09-08 17:25:04 +0000274 if (!username[0])
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000275 get_username_or_die(username, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000276
Denis Vlasenko942e4292006-09-08 17:25:04 +0000277 pw = getpwnam(username);
278 if (!pw) {
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000279 safe_strncpy(username, "UNKNOWN", sizeof(username));
280 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000281 }
282
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000283 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
284 goto auth_failed;
285
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000286 if (opt & LOGIN_OPT_f)
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000287 break; /* -f USER: success without asking passwd */
288
289 if (pw->pw_uid == 0 && !check_securetty())
290 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000291
292 /* Don't check the password if password entry is empty (!) */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000293 if (!pw->pw_passwd[0])
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000294 break;
Robert Griebl1fca5582002-06-04 20:45:46 +0000295
296 /* authorization takes place here */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000297 if (correct_password(pw))
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000298 break;
Robert Griebl1fca5582002-06-04 20:45:46 +0000299
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000300auth_failed:
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000301 opt &= ~LOGIN_OPT_f;
Rob Landley84cb7672006-01-06 20:59:09 +0000302 bb_do_delay(FAIL_DELAY);
Robert Griebl1fca5582002-06-04 20:45:46 +0000303 puts("Login incorrect");
Denis Vlasenko942e4292006-09-08 17:25:04 +0000304 if (++count == 3) {
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000305 syslog(LOG_WARNING, "invalid password for `%s'%s",
306 username, fromhost);
Robert Griebl1fca5582002-06-04 20:45:46 +0000307 return EXIT_FAILURE;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000308 }
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000309 username[0] = '\0';
Eric Andersen0fbff132002-06-22 17:49:29 +0000310 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311
Denis Vlasenko942e4292006-09-08 17:25:04 +0000312 alarm(0);
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000313 die_if_nologin_and_non_root(pw->pw_uid == 0);
Robert Griebl1fca5582002-06-04 20:45:46 +0000314
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000315 write_utent(username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000316
Rob Landley60158cb2005-05-03 06:25:50 +0000317#ifdef CONFIG_SELINUX
Denis Vlasenko942e4292006-09-08 17:25:04 +0000318 if (is_selinux_enabled()) {
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000319 security_context_t old_tty_sid, new_tty_sid;
Rob Landley60158cb2005-05-03 06:25:50 +0000320
Denis Vlasenko942e4292006-09-08 17:25:04 +0000321 if (get_default_context(username, NULL, &user_sid)) {
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000322 bb_error_msg_and_die("unable to get SID for %s",
323 username);
Rob Landley60158cb2005-05-03 06:25:50 +0000324 }
Denis Vlasenko942e4292006-09-08 17:25:04 +0000325 if (getfilecon(full_tty, &old_tty_sid) < 0) {
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000326 bb_perror_msg_and_die("getfilecon(%.100s) failed",
327 full_tty);
Rob Landley60158cb2005-05-03 06:25:50 +0000328 }
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000329 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",
332 full_tty);
Rob Landley60158cb2005-05-03 06:25:50 +0000333 }
Denis Vlasenko942e4292006-09-08 17:25:04 +0000334 if (setfilecon(full_tty, new_tty_sid) != 0) {
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000335 bb_perror_msg_and_die("chsid(%.100s, %s) failed",
336 full_tty, new_tty_sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000337 }
Rob Landley60158cb2005-05-03 06:25:50 +0000338 }
339#endif
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000340 /* 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);
343 fchmod(0, 0600);
Robert Griebl1fca5582002-06-04 20:45:46 +0000344
Denis Vlasenko2e502912006-09-08 17:22:45 +0000345 if (ENABLE_LOGIN_SCRIPTS) {
346 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
347 if (script) {
348 char *t_argv[2] = { script, NULL };
Denis Vlasenko942e4292006-09-08 17:25:04 +0000349 switch (fork()) {
Denis Vlasenko2e502912006-09-08 17:22:45 +0000350 case -1: break;
351 case 0: /* child */
352 xchdir("/");
353 setenv("LOGIN_TTY", full_tty, 1);
354 setenv("LOGIN_USER", pw->pw_name, 1);
355 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
356 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
357 setenv("LOGIN_SHELL", pw->pw_shell, 1);
358 execvp(script, t_argv);
359 exit(1);
360 default: /* parent */
361 wait(NULL);
362 }
363 }
364 }
365
Denis Vlasenko942e4292006-09-08 17:25:04 +0000366 change_identity(pw);
367 tmp = pw->pw_shell;
368 if (!tmp || !*tmp)
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000369 tmp = DEFAULT_SHELL;
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000370 setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
Robert Griebl1fca5582002-06-04 20:45:46 +0000371
Denis Vlasenko942e4292006-09-08 17:25:04 +0000372 motd();
373 signal(SIGALRM, SIG_DFL); /* default alarm signal */
Robert Griebl1fca5582002-06-04 20:45:46 +0000374
Denis Vlasenko942e4292006-09-08 17:25:04 +0000375 if (pw->pw_uid == 0)
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000376 syslog(LOG_INFO, "root login%s", fromhost);
Eric Andersen9e480452003-07-03 10:07:04 +0000377#ifdef CONFIG_SELINUX
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000378 /* 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);
Eric Andersen9e480452003-07-03 10:07:04 +0000381#endif
Denis Vlasenko942e4292006-09-08 17:25:04 +0000382 run_shell(tmp, 1, 0, 0); /* exec the shell finally. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000383
Robert Griebl1fca5582002-06-04 20:45:46 +0000384 return EXIT_FAILURE;
385}