blob: d9a2f8a41926f5837d868f27f28fdfbaccd1fa7b [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00006#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +00007#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +00008#include <utmp.h>
9#include <sys/resource.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000010
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000011#if ENABLE_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 */
Eric Andersen9e480452003-07-03 10:07:04 +000015#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000016
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000017#if ENABLE_PAM
Denis Vlasenkoc6c23452007-08-22 18:14:44 +000018/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
19#undef setlocale
20/* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
21 * Apparently they like to confuse people. */
22#include <security/pam_appl.h>
23#include <security/pam_misc.h>
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000024static const struct pam_conv conv = {
25 misc_conv,
26 NULL
27};
28#endif
29
Denis Vlasenko942e4292006-09-08 17:25:04 +000030enum {
31 TIMEOUT = 60,
32 EMPTY_USERNAME_COUNT = 10,
33 USERNAME_SIZE = 32,
Denis Vlasenko9a9edf22006-09-08 17:29:53 +000034 TTYNAME_SIZE = 32,
Denis Vlasenko942e4292006-09-08 17:25:04 +000035};
Eric Andersen0fbff132002-06-22 17:49:29 +000036
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000037static char* short_tty;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000038
39#if ENABLE_FEATURE_UTMP
40/* vv Taken from tinylogin utmp.c vv */
41/*
42 * read_or_build_utent - see if utmp file is correct for this process
43 *
44 * System V is very picky about the contents of the utmp file
45 * and requires that a slot for the current process exist.
46 * The utmp file is scanned for an entry with the same process
47 * ID. If no entry exists the process exits with a message.
48 *
49 * The "picky" flag is for network and other logins that may
50 * use special flags. It allows the pid checks to be overridden.
51 * This means that getty should never invoke login with any
52 * command line flags.
53 */
Denis Vlasenko66fabdb2006-09-17 14:45:09 +000054
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000055static void read_or_build_utent(struct utmp *utptr, int picky)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000056{
57 struct utmp *ut;
58 pid_t pid = getpid();
59
60 setutent();
61
62 /* First, try to find a valid utmp entry for this process. */
63 while ((ut = getutent()))
64 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
65 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
66 break;
67
68 /* If there is one, just use it, otherwise create a new one. */
69 if (ut) {
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000070 *utptr = *ut;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000071 } else {
72 if (picky)
73 bb_error_msg_and_die("no utmp entry found");
74
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000075 memset(utptr, 0, sizeof(*utptr));
76 utptr->ut_type = LOGIN_PROCESS;
77 utptr->ut_pid = pid;
78 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000079 /* This one is only 4 chars wide. Try to fit something
80 * remotely meaningful by skipping "tty"... */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000081 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
82 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
Bernhard Reutner-Fischer62d85032008-06-01 10:10:22 +000083 utptr->ut_tv.tv_sec = time(NULL);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000084 }
85 if (!picky) /* root login */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000086 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000087}
88
89/*
90 * write_utent - put a USER_PROCESS entry in the utmp file
91 *
92 * write_utent changes the type of the current utmp entry to
93 * USER_PROCESS. the wtmp file will be updated as well.
94 */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000095static void write_utent(struct utmp *utptr, const char *username)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000096{
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000097 utptr->ut_type = USER_PROCESS;
98 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
Bernhard Reutner-Fischer62d85032008-06-01 10:10:22 +000099 utptr->ut_tv.tv_sec = time(NULL);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000100 /* other fields already filled in by read_or_build_utent above */
101 setutent();
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000102 pututline(utptr);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000103 endutent();
104#if ENABLE_FEATURE_WTMP
105 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
106 close(creat(bb_path_wtmp_file, 0664));
107 }
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000108 updwtmp(bb_path_wtmp_file, utptr);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000109#endif
110}
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000111#else /* !ENABLE_FEATURE_UTMP */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000112#define read_or_build_utent(utptr, picky) ((void)0)
113#define write_utent(utptr, username) ((void)0)
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000114#endif /* !ENABLE_FEATURE_UTMP */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000115
Denis Vlasenkof312e322007-06-12 22:04:57 +0000116#if ENABLE_FEATURE_NOLOGIN
Denis Vlasenko68404f12008-03-17 09:00:54 +0000117static void die_if_nologin(void)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000118{
119 FILE *fp;
120 int c;
121
Denis Vlasenkof312e322007-06-12 22:04:57 +0000122 if (access("/etc/nologin", F_OK))
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000123 return;
124
Denis Vlasenkof312e322007-06-12 22:04:57 +0000125 fp = fopen("/etc/nologin", "r");
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000126 if (fp) {
127 while ((c = getc(fp)) != EOF)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000128 bb_putchar((c=='\n') ? '\r' : c);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000129 fflush(stdout);
130 fclose(fp);
131 } else
132 puts("\r\nSystem closed for routine maintenance\r");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000133 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000134}
Denis Vlasenkof312e322007-06-12 22:04:57 +0000135#else
Denis Vlasenko68404f12008-03-17 09:00:54 +0000136static ALWAYS_INLINE void die_if_nologin(void) {}
Denis Vlasenkof312e322007-06-12 22:04:57 +0000137#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000138
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000139#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000140static int check_securetty(void)
141{
142 FILE *fp;
143 int i;
Denis Vlasenkof312e322007-06-12 22:04:57 +0000144 char buf[256];
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000145
Denis Vlasenkof312e322007-06-12 22:04:57 +0000146 fp = fopen("/etc/securetty", "r");
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000147 if (!fp) {
148 /* A missing securetty file is not an error. */
149 return 1;
150 }
151 while (fgets(buf, sizeof(buf)-1, fp)) {
Denis Vlasenkof312e322007-06-12 22:04:57 +0000152 for (i = strlen(buf)-1; i >= 0; --i) {
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000153 if (!isspace(buf[i]))
154 break;
155 }
156 buf[++i] = '\0';
Denis Vlasenkof312e322007-06-12 22:04:57 +0000157 if (!buf[0] || (buf[0] == '#'))
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000158 continue;
159 if (strcmp(buf, short_tty) == 0) {
160 fclose(fp);
161 return 1;
162 }
163 }
164 fclose(fp);
165 return 0;
166}
Robert Griebl1fca5582002-06-04 20:45:46 +0000167#else
Denis Vlasenkof312e322007-06-12 22:04:57 +0000168static ALWAYS_INLINE int check_securetty(void) { return 1; }
Robert Griebl1fca5582002-06-04 20:45:46 +0000169#endif
170
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000171static void get_username_or_die(char *buf, int size_buf)
172{
173 int c, cntdown;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000174
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000175 cntdown = EMPTY_USERNAME_COUNT;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000176 prompt:
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000177 print_login_prompt();
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000178 /* skip whitespace */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000179 do {
180 c = getchar();
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000181 if (c == EOF) exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000182 if (c == '\n') {
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000183 if (!--cntdown) exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000184 goto prompt;
185 }
186 } while (isspace(c));
187
188 *buf++ = c;
189 if (!fgets(buf, size_buf-2, stdin))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000190 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000191 if (!strchr(buf, '\n'))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000192 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000193 while (isgraph(*buf)) buf++;
194 *buf = '\0';
195}
196
197static void motd(void)
198{
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000199 int fd;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000200
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000201 fd = open(bb_path_motd_file, O_RDONLY);
Denis Vlasenko52816302007-11-06 05:26:51 +0000202 if (fd >= 0) {
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000203 fflush(stdout);
204 bb_copyfd_eof(fd, STDOUT_FILENO);
205 close(fd);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000206 }
207}
Robert Griebl1fca5582002-06-04 20:45:46 +0000208
Denis Vlasenko942e4292006-09-08 17:25:04 +0000209static void alarm_handler(int sig ATTRIBUTE_UNUSED)
Robert Griebl1fca5582002-06-04 20:45:46 +0000210{
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000211 /* This is the escape hatch! Poor serial line users and the like
212 * arrive here when their connection is broken.
213 * We don't want to block here */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000214 ndelay_on(1);
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000215 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000216 fflush(stdout);
Denis Vlasenko52816302007-11-06 05:26:51 +0000217 /* unix API is brain damaged regarding O_NONBLOCK,
218 * we should undo it, or else we can affect other processes */
219 ndelay_off(1);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000220 _exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000221}
222
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000223int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000224int login_main(int argc ATTRIBUTE_UNUSED, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000225{
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000226 enum {
227 LOGIN_OPT_f = (1<<0),
228 LOGIN_OPT_h = (1<<1),
229 LOGIN_OPT_p = (1<<2),
230 };
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000231 char *fromhost;
Eric Andersen0fbff132002-06-22 17:49:29 +0000232 char username[USERNAME_SIZE];
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000233 const char *tmp;
Robert Griebl1fca5582002-06-04 20:45:46 +0000234 int amroot;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000235 unsigned opt;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000236 int count = 0;
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000237 struct passwd *pw;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000238 char *opt_host = opt_host; /* for compiler */
239 char *opt_user = opt_user; /* for compiler */
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000240 char full_tty[TTYNAME_SIZE];
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000241 USE_SELINUX(security_context_t user_sid = NULL;)
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000242 USE_FEATURE_UTMP(struct utmp utent;)
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000243#if ENABLE_PAM
244 int pamret;
245 pam_handle_t *pamh;
246 const char *pamuser;
247 const char *failed_msg;
248 struct passwd pwdstruct;
249 char pwdbuf[256];
250#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000251
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000252 short_tty = full_tty;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000253 username[0] = '\0';
Denis Vlasenko942e4292006-09-08 17:25:04 +0000254 signal(SIGALRM, alarm_handler);
255 alarm(TIMEOUT);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000256
Denis Vlasenkoc9ca0a32008-02-18 11:08:33 +0000257 /* More of suid paranoia if called by non-root */
258 amroot = !sanitize_env_if_suid(); /* Clear dangerous stuff, set PATH */
259
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000260 /* Mandatory paranoia for suid applet:
261 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
262 * and any extra open fd's are closed.
263 * (The name of the function is misleading. Not daemonizing here.) */
264 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
265
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000266 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000267 if (opt & LOGIN_OPT_f) {
268 if (!amroot)
269 bb_error_msg_and_die("-f is for root only");
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000270 safe_strncpy(username, opt_user, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000271 }
Denis Vlasenko1d426652008-03-17 09:09:09 +0000272 argv += optind;
273 if (argv[0]) /* user from command line (getty) */
274 safe_strncpy(username, argv[0], sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000275
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000276 /* Let's find out and memorize our tty */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000277 if (!isatty(0) || !isatty(1) || !isatty(2))
Robert Griebl1fca5582002-06-04 20:45:46 +0000278 return EXIT_FAILURE; /* Must be a terminal */
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000279 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
Denis Vlasenko942e4292006-09-08 17:25:04 +0000280 tmp = ttyname(0);
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000281 if (tmp) {
282 safe_strncpy(full_tty, tmp, sizeof(full_tty));
283 if (strncmp(full_tty, "/dev/", 5) == 0)
284 short_tty = full_tty + 5;
285 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000286
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000287 read_or_build_utent(&utent, !amroot);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000288
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000289 if (opt & LOGIN_OPT_h) {
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000290 USE_FEATURE_UTMP(
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000291 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000292 )
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000293 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000294 } else
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000295 fromhost = xasprintf(" on '%s'", short_tty);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000296
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000297 /* Was breaking "login <username>" from shell command line: */
298 /*bb_setpgrp();*/
Eric Andersen0fbff132002-06-22 17:49:29 +0000299
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000300 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000301
Denis Vlasenko942e4292006-09-08 17:25:04 +0000302 while (1) {
Paul Fox6e1b62b2007-11-07 15:51:35 +0000303 /* flush away any type-ahead (as getty does) */
Denis Vlasenkoabbd3632007-11-08 17:40:23 +0000304 ioctl(0, TCFLSH, TCIFLUSH);
Paul Fox6e1b62b2007-11-07 15:51:35 +0000305
Denis Vlasenko942e4292006-09-08 17:25:04 +0000306 if (!username[0])
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000307 get_username_or_die(username, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000308
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000309#if ENABLE_PAM
310 pamret = pam_start("login", username, &conv, &pamh);
311 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000312 failed_msg = "start";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000313 goto pam_auth_failed;
314 }
315 /* set TTY (so things like securetty work) */
316 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
317 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000318 failed_msg = "set_item(TTY)";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000319 goto pam_auth_failed;
320 }
321 pamret = pam_authenticate(pamh, 0);
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000322 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000323 failed_msg = "authenticate";
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000324 goto pam_auth_failed;
325 /* TODO: or just "goto auth_failed"
326 * since user seems to enter wrong password
327 * (in this case pamret == 7)
328 */
329 }
330 /* check that the account is healthy */
331 pamret = pam_acct_mgmt(pamh, 0);
332 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000333 failed_msg = "acct_mgmt";
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000334 goto pam_auth_failed;
335 }
336 /* read user back */
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000337 pamuser = NULL;
338 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
339 * thus we cast to (void*) */
340 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
341 failed_msg = "get_item(USER)";
342 goto pam_auth_failed;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000343 }
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000344 if (!pamuser || !pamuser[0])
345 goto auth_failed;
346 safe_strncpy(username, pamuser, sizeof(username));
347 /* Don't use "pw = getpwnam(username);",
348 * PAM is said to be capable of destroying static storage
349 * used by getpwnam(). We are using safe(r) function */
350 pw = NULL;
351 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
352 if (!pw)
353 goto auth_failed;
354 pamret = pam_open_session(pamh, 0);
355 if (pamret != PAM_SUCCESS) {
356 failed_msg = "open_session";
357 goto pam_auth_failed;
358 }
359 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
360 if (pamret != PAM_SUCCESS) {
361 failed_msg = "setcred";
362 goto pam_auth_failed;
363 }
364 break; /* success, continue login process */
365
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000366 pam_auth_failed:
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000367 bb_error_msg("pam_%s call failed: %s (%d)", failed_msg,
368 pam_strerror(pamh, pamret), pamret);
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000369 safe_strncpy(username, "UNKNOWN", sizeof(username));
370#else /* not PAM */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000371 pw = getpwnam(username);
372 if (!pw) {
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000373 strcpy(username, "UNKNOWN");
374 goto fake_it;
Robert Griebl1fca5582002-06-04 20:45:46 +0000375 }
376
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000377 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
378 goto auth_failed;
379
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000380 if (opt & LOGIN_OPT_f)
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000381 break; /* -f USER: success without asking passwd */
382
383 if (pw->pw_uid == 0 && !check_securetty())
384 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000385
386 /* Don't check the password if password entry is empty (!) */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000387 if (!pw->pw_passwd[0])
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000388 break;
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000389 fake_it:
Robert Griebl1fca5582002-06-04 20:45:46 +0000390 /* authorization takes place here */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000391 if (correct_password(pw))
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000392 break;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000393#endif /* ENABLE_PAM */
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000394 auth_failed:
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000395 opt &= ~LOGIN_OPT_f;
Rob Landley84cb7672006-01-06 20:59:09 +0000396 bb_do_delay(FAIL_DELAY);
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000397 /* TODO: doesn't sound like correct English phrase to me */
Robert Griebl1fca5582002-06-04 20:45:46 +0000398 puts("Login incorrect");
Denis Vlasenko942e4292006-09-08 17:25:04 +0000399 if (++count == 3) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000400 syslog(LOG_WARNING, "invalid password for '%s'%s",
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000401 username, fromhost);
Robert Griebl1fca5582002-06-04 20:45:46 +0000402 return EXIT_FAILURE;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000403 }
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000404 username[0] = '\0';
Eric Andersen0fbff132002-06-22 17:49:29 +0000405 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000406
Denis Vlasenko942e4292006-09-08 17:25:04 +0000407 alarm(0);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000408 if (!amroot)
409 die_if_nologin();
Robert Griebl1fca5582002-06-04 20:45:46 +0000410
Denis Vlasenkoc0415a92007-03-24 17:04:07 +0000411 write_utent(&utent, username);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000412
Denis Vlasenkof312e322007-06-12 22:04:57 +0000413#if ENABLE_SELINUX
Denis Vlasenko942e4292006-09-08 17:25:04 +0000414 if (is_selinux_enabled()) {
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000415 security_context_t old_tty_sid, new_tty_sid;
Rob Landley60158cb2005-05-03 06:25:50 +0000416
Denis Vlasenko942e4292006-09-08 17:25:04 +0000417 if (get_default_context(username, NULL, &user_sid)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000418 bb_error_msg_and_die("cannot get SID for %s",
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000419 username);
Rob Landley60158cb2005-05-03 06:25:50 +0000420 }
Denis Vlasenko942e4292006-09-08 17:25:04 +0000421 if (getfilecon(full_tty, &old_tty_sid) < 0) {
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000422 bb_perror_msg_and_die("getfilecon(%s) failed",
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000423 full_tty);
Rob Landley60158cb2005-05-03 06:25:50 +0000424 }
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000425 if (security_compute_relabel(user_sid, old_tty_sid,
426 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000427 bb_perror_msg_and_die("security_change_sid(%s) failed",
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000428 full_tty);
Rob Landley60158cb2005-05-03 06:25:50 +0000429 }
Denis Vlasenko942e4292006-09-08 17:25:04 +0000430 if (setfilecon(full_tty, new_tty_sid) != 0) {
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000431 bb_perror_msg_and_die("chsid(%s, %s) failed",
Denis Vlasenko6bbd1742006-09-08 17:26:20 +0000432 full_tty, new_tty_sid);
Rob Landley60158cb2005-05-03 06:25:50 +0000433 }
Rob Landley60158cb2005-05-03 06:25:50 +0000434 }
435#endif
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000436 /* Try these, but don't complain if they fail.
437 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
438 fchown(0, pw->pw_uid, pw->pw_gid);
439 fchmod(0, 0600);
Robert Griebl1fca5582002-06-04 20:45:46 +0000440
Denis Vlasenko52816302007-11-06 05:26:51 +0000441 /* We trust environment only if we run by root */
442 if (ENABLE_LOGIN_SCRIPTS && amroot) {
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000443 char *t_argv[2];
444
445 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
446 if (t_argv[0]) {
447 t_argv[1] = NULL;
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000448 xsetenv("LOGIN_TTY", full_tty);
449 xsetenv("LOGIN_USER", pw->pw_name);
450 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
451 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
452 xsetenv("LOGIN_SHELL", pw->pw_shell);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000453 spawn_and_wait(t_argv); /* NOMMU-friendly */
454 unsetenv("LOGIN_TTY" );
455 unsetenv("LOGIN_USER" );
456 unsetenv("LOGIN_UID" );
457 unsetenv("LOGIN_GID" );
458 unsetenv("LOGIN_SHELL");
Denis Vlasenko2e502912006-09-08 17:22:45 +0000459 }
460 }
461
Denis Vlasenko942e4292006-09-08 17:25:04 +0000462 change_identity(pw);
463 tmp = pw->pw_shell;
464 if (!tmp || !*tmp)
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000465 tmp = DEFAULT_SHELL;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000466 /* setup_environment params: shell, clear_env, change_env, pw */
467 setup_environment(tmp, !(opt & LOGIN_OPT_p), 1, pw);
Robert Griebl1fca5582002-06-04 20:45:46 +0000468
Denis Vlasenko942e4292006-09-08 17:25:04 +0000469 motd();
Robert Griebl1fca5582002-06-04 20:45:46 +0000470
Denis Vlasenko942e4292006-09-08 17:25:04 +0000471 if (pw->pw_uid == 0)
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000472 syslog(LOG_INFO, "root login%s", fromhost);
Denis Vlasenkof312e322007-06-12 22:04:57 +0000473#if ENABLE_SELINUX
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000474 /* well, a simple setexeccon() here would do the job as well,
475 * but let's play the game for now */
476 set_current_security_context(user_sid);
Eric Andersen9e480452003-07-03 10:07:04 +0000477#endif
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000478
479 // util-linux login also does:
480 // /* start new session */
481 // setsid();
482 // /* TIOCSCTTY: steal tty from other process group */
483 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000484 // BBox login used to do this (see above):
485 // bb_setpgrp();
486 // If this stuff is really needed, add it and explain why!
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000487
Denis Vlasenko8c764872006-10-31 18:30:56 +0000488 /* set signals to defaults */
489 signal(SIGALRM, SIG_DFL);
490 /* Is this correct? This way user can ctrl-c out of /etc/profile,
491 * potentially creating security breach (tested with bash 3.0).
492 * But without this, bash 3.0 will not enable ctrl-c either.
493 * Maybe bash is buggy?
494 * Need to find out what standards say about /bin/login -
495 * should it leave SIGINT etc enabled or disabled? */
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000496 signal(SIGINT, SIG_DFL);
497
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000498 /* Exec login shell with no additional parameters */
499 run_shell(tmp, 1, NULL, NULL);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000500
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000501 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000502}