blob: 862104c9fac8dad703eec9b3a93a7019f7e2f7ad [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 */
Pere Orga6a3e01d2011-04-01 22:56:30 +02005
6//usage:#define login_trivial_usage
7//usage: "[-p] [-h HOST] [[-f] USER]"
8//usage:#define login_full_usage "\n\n"
9//usage: "Begin a new session on the system\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020010//usage: "\n -f Don't authenticate (user already authenticated)"
11//usage: "\n -h Name of the remote host"
12//usage: "\n -p Preserve environment"
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000015#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000016#include <sys/resource.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000017
Denis Vlasenkoc0415a92007-03-24 17:04:07 +000018#if ENABLE_SELINUX
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020019# include <selinux/selinux.h> /* for is_selinux_enabled() */
maxwen27116ba2015-08-14 21:41:28 +020020#ifndef __BIONIC__
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020021# include <selinux/get_context_list.h> /* for get_default_context() */
22# include <selinux/flask.h> /* for security class definitions */
Eric Andersen9e480452003-07-03 10:07:04 +000023#endif
maxwen27116ba2015-08-14 21:41:28 +020024#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000025
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000026#if ENABLE_PAM
Denis Vlasenkoc6c23452007-08-22 18:14:44 +000027/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020028# undef setlocale
Denis Vlasenkoc6c23452007-08-22 18:14:44 +000029/* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
30 * Apparently they like to confuse people. */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020031# include <security/pam_appl.h>
32# include <security/pam_misc.h>
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000033static const struct pam_conv conv = {
34 misc_conv,
35 NULL
36};
37#endif
38
Denis Vlasenko942e4292006-09-08 17:25:04 +000039enum {
40 TIMEOUT = 60,
41 EMPTY_USERNAME_COUNT = 10,
Tanguy Pruvot823694d2012-11-18 13:20:29 +010042 /* Some users found 32 chars limit to be too low: */
43 USERNAME_SIZE = 64,
Denis Vlasenko9a9edf22006-09-08 17:29:53 +000044 TTYNAME_SIZE = 32,
Denis Vlasenko942e4292006-09-08 17:25:04 +000045};
Eric Andersen0fbff132002-06-22 17:49:29 +000046
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020047struct globals {
48 struct termios tty_attrs;
49} FIX_ALIASING;
50#define G (*(struct globals*)&bb_common_bufsiz1)
51#define INIT_G() do { } while (0)
52
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000053
Denis Vlasenkof312e322007-06-12 22:04:57 +000054#if ENABLE_FEATURE_NOLOGIN
Denis Vlasenko68404f12008-03-17 09:00:54 +000055static void die_if_nologin(void)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000056{
57 FILE *fp;
58 int c;
Denis Vlasenko694b5142008-11-07 01:12:16 +000059 int empty = 1;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000060
Denis Vlasenko5415c852008-07-21 23:05:26 +000061 fp = fopen_for_read("/etc/nologin");
Denis Vlasenko694b5142008-11-07 01:12:16 +000062 if (!fp) /* assuming it does not exist */
63 return;
64
65 while ((c = getc(fp)) != EOF) {
66 if (c == '\n')
67 bb_putchar('\r');
68 bb_putchar(c);
69 empty = 0;
70 }
71 if (empty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000072 puts("\r\nSystem closed for routine maintenance\r");
Denis Vlasenko694b5142008-11-07 01:12:16 +000073
74 fclose(fp);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010075 fflush_all();
Denis Vlasenko694b5142008-11-07 01:12:16 +000076 /* Users say that they do need this prior to exit: */
77 tcdrain(STDOUT_FILENO);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000078 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000079}
Denis Vlasenkof312e322007-06-12 22:04:57 +000080#else
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020081# define die_if_nologin() ((void)0)
Denis Vlasenkof312e322007-06-12 22:04:57 +000082#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000083
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000084#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020085static int check_securetty(const char *short_tty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000086{
Denis Vlasenko084266e2008-07-26 23:08:31 +000087 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
Denis Vlasenko5415c852008-07-21 23:05:26 +000088 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000089 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
90 if (strcmp(buf, short_tty) == 0)
91 break;
92 buf = NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000093 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000094 config_close(parser);
95 /* buf != NULL here if config file was not found, empty
96 * or line was found which equals short_tty */
97 return buf != NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000098}
Robert Griebl1fca5582002-06-04 20:45:46 +000099#else
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200100static ALWAYS_INLINE int check_securetty(const char *short_tty UNUSED_PARAM) { return 1; }
Robert Griebl1fca5582002-06-04 20:45:46 +0000101#endif
102
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000103#if ENABLE_SELINUX
104static void initselinux(char *username, char *full_tty,
105 security_context_t *user_sid)
106{
107 security_context_t old_tty_sid, new_tty_sid;
108
109 if (!is_selinux_enabled())
110 return;
111
112 if (get_default_context(username, NULL, user_sid)) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100113 bb_error_msg_and_die("can't get SID for %s", username);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000114 }
115 if (getfilecon(full_tty, &old_tty_sid) < 0) {
116 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
117 }
Denis Vlasenko8e5de2a2009-02-24 17:10:24 +0000118 if (security_compute_relabel(*user_sid, old_tty_sid,
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000119 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
120 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
121 }
122 if (setfilecon(full_tty, new_tty_sid) != 0) {
maxwen27116ba2015-08-14 21:41:28 +0200123 if (strcmp(old_tty_sid, new_tty_sid))
124 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000125 }
126}
127#endif
128
129#if ENABLE_LOGIN_SCRIPTS
130static void run_login_script(struct passwd *pw, char *full_tty)
131{
132 char *t_argv[2];
133
134 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
135 if (t_argv[0]) {
136 t_argv[1] = NULL;
137 xsetenv("LOGIN_TTY", full_tty);
138 xsetenv("LOGIN_USER", pw->pw_name);
139 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
140 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
141 xsetenv("LOGIN_SHELL", pw->pw_shell);
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200142 spawn_and_wait(t_argv); /* NOMMU-friendly */
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000143 unsetenv("LOGIN_TTY");
144 unsetenv("LOGIN_USER");
145 unsetenv("LOGIN_UID");
146 unsetenv("LOGIN_GID");
147 unsetenv("LOGIN_SHELL");
148 }
149}
150#else
151void run_login_script(struct passwd *pw, char *full_tty);
152#endif
153
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200154#if ENABLE_LOGIN_SESSION_AS_CHILD && ENABLE_PAM
155static void login_pam_end(pam_handle_t *pamh)
156{
157 int pamret;
158
159 pamret = pam_setcred(pamh, PAM_DELETE_CRED);
160 if (pamret != PAM_SUCCESS) {
161 bb_error_msg("pam_%s failed: %s (%d)", "setcred",
162 pam_strerror(pamh, pamret), pamret);
163 }
164 pamret = pam_close_session(pamh, 0);
165 if (pamret != PAM_SUCCESS) {
166 bb_error_msg("pam_%s failed: %s (%d)", "close_session",
167 pam_strerror(pamh, pamret), pamret);
168 }
169 pamret = pam_end(pamh, pamret);
170 if (pamret != PAM_SUCCESS) {
171 bb_error_msg("pam_%s failed: %s (%d)", "end",
172 pam_strerror(pamh, pamret), pamret);
173 }
174}
175#endif /* ENABLE_PAM */
176
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000177static void get_username_or_die(char *buf, int size_buf)
178{
179 int c, cntdown;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000180
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000181 cntdown = EMPTY_USERNAME_COUNT;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000182 prompt:
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000183 print_login_prompt();
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000184 /* skip whitespace */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000185 do {
186 c = getchar();
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200187 if (c == EOF)
188 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000189 if (c == '\n') {
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200190 if (!--cntdown)
191 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000192 goto prompt;
193 }
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200194 } while (isspace(c)); /* maybe isblank? */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000195
196 *buf++ = c;
197 if (!fgets(buf, size_buf-2, stdin))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000198 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000199 if (!strchr(buf, '\n'))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000200 exit(EXIT_FAILURE);
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200201 while ((unsigned char)*buf > ' ')
202 buf++;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000203 *buf = '\0';
204}
205
206static void motd(void)
207{
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000208 int fd;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000209
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000210 fd = open(bb_path_motd_file, O_RDONLY);
Denis Vlasenko52816302007-11-06 05:26:51 +0000211 if (fd >= 0) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100212 fflush_all();
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000213 bb_copyfd_eof(fd, STDOUT_FILENO);
214 close(fd);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000215 }
216}
Robert Griebl1fca5582002-06-04 20:45:46 +0000217
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000218static void alarm_handler(int sig UNUSED_PARAM)
Robert Griebl1fca5582002-06-04 20:45:46 +0000219{
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200220 /* This is the escape hatch! Poor serial line users and the like
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000221 * arrive here when their connection is broken.
222 * We don't want to block here */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200223 ndelay_on(STDOUT_FILENO);
224 /* Test for correct attr restoring:
225 * run "getty 0 -" from a shell, enter bogus username, stop at
226 * password prompt, let it time out. Without the tcsetattr below,
227 * when you are back at shell prompt, echo will be still off.
228 */
229 tcsetattr_stdin_TCSANOW(&G.tty_attrs);
230 printf("\r\nLogin timed out after %u seconds\r\n", TIMEOUT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100231 fflush_all();
Denis Vlasenko52816302007-11-06 05:26:51 +0000232 /* unix API is brain damaged regarding O_NONBLOCK,
233 * we should undo it, or else we can affect other processes */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200234 ndelay_off(STDOUT_FILENO);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000235 _exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000236}
237
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000238int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000239int login_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000240{
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000241 enum {
242 LOGIN_OPT_f = (1<<0),
243 LOGIN_OPT_h = (1<<1),
244 LOGIN_OPT_p = (1<<2),
245 };
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000246 char *fromhost;
Eric Andersen0fbff132002-06-22 17:49:29 +0000247 char username[USERNAME_SIZE];
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000248 int run_by_root;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000249 unsigned opt;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000250 int count = 0;
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000251 struct passwd *pw;
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200252 char *opt_host = NULL;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000253 char *opt_user = opt_user; /* for compiler */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000254 char *full_tty;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200255 char *short_tty;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000256 IF_SELINUX(security_context_t user_sid = NULL;)
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000257#if ENABLE_PAM
258 int pamret;
259 pam_handle_t *pamh;
260 const char *pamuser;
261 const char *failed_msg;
262 struct passwd pwdstruct;
263 char pwdbuf[256];
Ian Wienand260fb552010-12-20 11:33:38 -0800264 char **pamenv;
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000265#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200266#if ENABLE_LOGIN_SESSION_AS_CHILD
267 pid_t child_pid;
268#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000269
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200270 INIT_G();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000271
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000272 /* More of suid paranoia if called by non-root: */
273 /* Clear dangerous stuff, set PATH */
274 run_by_root = !sanitize_env_if_suid();
Denis Vlasenkoc9ca0a32008-02-18 11:08:33 +0000275
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000276 /* Mandatory paranoia for suid applet:
277 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
278 * and any extra open fd's are closed.
279 * (The name of the function is misleading. Not daemonizing here.) */
280 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
281
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200282 username[0] = '\0';
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000283 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000284 if (opt & LOGIN_OPT_f) {
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000285 if (!run_by_root)
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000286 bb_error_msg_and_die("-f is for root only");
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000287 safe_strncpy(username, opt_user, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000288 }
Denis Vlasenko1d426652008-03-17 09:09:09 +0000289 argv += optind;
290 if (argv[0]) /* user from command line (getty) */
291 safe_strncpy(username, argv[0], sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000292
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200293 /* Save tty attributes - and by doing it, check that it's indeed a tty */
294 if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0
295 || !isatty(STDOUT_FILENO)
296 /*|| !isatty(STDERR_FILENO) - no, guess some people might want to redirect this */
297 ) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200298 return EXIT_FAILURE; /* Must be a terminal */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200299 }
300
301 /* We install timeout handler only _after_ we saved G.tty_attrs */
302 signal(SIGALRM, alarm_handler);
303 alarm(TIMEOUT);
304
305 /* Find out and memorize our tty name */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000306 full_tty = xmalloc_ttyname(STDIN_FILENO);
307 if (!full_tty)
308 full_tty = xstrdup("UNKNOWN");
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200309 short_tty = skip_dev_pfx(full_tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000310
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200311 if (opt_host) {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000312 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000313 } else {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000314 fromhost = xasprintf(" on '%s'", short_tty);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000315 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000316
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000317 /* Was breaking "login <username>" from shell command line: */
318 /*bb_setpgrp();*/
Eric Andersen0fbff132002-06-22 17:49:29 +0000319
Denis Vlasenko54ac03a2009-03-11 15:59:49 +0000320 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000321
Denis Vlasenko942e4292006-09-08 17:25:04 +0000322 while (1) {
Paul Fox6e1b62b2007-11-07 15:51:35 +0000323 /* flush away any type-ahead (as getty does) */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200324 tcflush(0, TCIFLUSH);
Paul Fox6e1b62b2007-11-07 15:51:35 +0000325
Denis Vlasenko942e4292006-09-08 17:25:04 +0000326 if (!username[0])
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000327 get_username_or_die(username, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000328
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000329#if ENABLE_PAM
330 pamret = pam_start("login", username, &conv, &pamh);
331 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000332 failed_msg = "start";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000333 goto pam_auth_failed;
334 }
335 /* set TTY (so things like securetty work) */
336 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
337 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000338 failed_msg = "set_item(TTY)";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000339 goto pam_auth_failed;
340 }
Ryan Phillipscf9074b2011-03-22 18:27:21 +0100341 /* set RHOST */
342 if (opt_host) {
343 pamret = pam_set_item(pamh, PAM_RHOST, opt_host);
344 if (pamret != PAM_SUCCESS) {
345 failed_msg = "set_item(RHOST)";
346 goto pam_auth_failed;
347 }
348 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200349 if (!(opt & LOGIN_OPT_f)) {
350 pamret = pam_authenticate(pamh, 0);
351 if (pamret != PAM_SUCCESS) {
352 failed_msg = "authenticate";
353 goto pam_auth_failed;
354 /* TODO: or just "goto auth_failed"
355 * since user seems to enter wrong password
356 * (in this case pamret == 7)
357 */
358 }
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000359 }
360 /* check that the account is healthy */
361 pamret = pam_acct_mgmt(pamh, 0);
362 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000363 failed_msg = "acct_mgmt";
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000364 goto pam_auth_failed;
365 }
366 /* read user back */
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000367 pamuser = NULL;
368 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
369 * thus we cast to (void*) */
370 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
371 failed_msg = "get_item(USER)";
372 goto pam_auth_failed;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000373 }
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000374 if (!pamuser || !pamuser[0])
375 goto auth_failed;
376 safe_strncpy(username, pamuser, sizeof(username));
377 /* Don't use "pw = getpwnam(username);",
378 * PAM is said to be capable of destroying static storage
379 * used by getpwnam(). We are using safe(r) function */
380 pw = NULL;
381 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
382 if (!pw)
383 goto auth_failed;
384 pamret = pam_open_session(pamh, 0);
385 if (pamret != PAM_SUCCESS) {
386 failed_msg = "open_session";
387 goto pam_auth_failed;
388 }
389 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
390 if (pamret != PAM_SUCCESS) {
391 failed_msg = "setcred";
392 goto pam_auth_failed;
393 }
394 break; /* success, continue login process */
395
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000396 pam_auth_failed:
Denys Vlasenkoc297ea92009-09-25 01:50:45 +0200397 /* syslog, because we don't want potential attacker
398 * to know _why_ login failed */
399 syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000400 pam_strerror(pamh, pamret), pamret);
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000401 safe_strncpy(username, "UNKNOWN", sizeof(username));
402#else /* not PAM */
maxwen27116ba2015-08-14 21:41:28 +0200403 pw = safegetpwnam(username);
Denis Vlasenko942e4292006-09-08 17:25:04 +0000404 if (!pw) {
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000405 strcpy(username, "UNKNOWN");
406 goto fake_it;
Robert Griebl1fca5582002-06-04 20:45:46 +0000407 }
408
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000409 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
410 goto auth_failed;
411
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000412 if (opt & LOGIN_OPT_f)
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000413 break; /* -f USER: success without asking passwd */
414
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200415 if (pw->pw_uid == 0 && !check_securetty(short_tty))
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000416 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000417
418 /* Don't check the password if password entry is empty (!) */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000419 if (!pw->pw_passwd[0])
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000420 break;
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000421 fake_it:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200422 /* Password reading and authorization takes place here.
423 * Note that reads (in no-echo mode) trash tty attributes.
424 * If we get interrupted by SIGALRM, we need to restore attrs.
425 */
maxwen27116ba2015-08-14 21:41:28 +0200426 if (ask_and_check_password(pw) > 0)
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000427 break;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000428#endif /* ENABLE_PAM */
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000429 auth_failed:
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000430 opt &= ~LOGIN_OPT_f;
Denys Vlasenko7d4e7a22011-03-08 21:07:05 +0100431 bb_do_delay(LOGIN_FAIL_DELAY);
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000432 /* TODO: doesn't sound like correct English phrase to me */
Robert Griebl1fca5582002-06-04 20:45:46 +0000433 puts("Login incorrect");
Denis Vlasenko942e4292006-09-08 17:25:04 +0000434 if (++count == 3) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000435 syslog(LOG_WARNING, "invalid password for '%s'%s",
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000436 username, fromhost);
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300437
438 if (ENABLE_FEATURE_CLEAN_UP)
439 free(fromhost);
440
Robert Griebl1fca5582002-06-04 20:45:46 +0000441 return EXIT_FAILURE;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000442 }
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000443 username[0] = '\0';
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000444 } /* while (1) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000445
Denis Vlasenko942e4292006-09-08 17:25:04 +0000446 alarm(0);
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000447 /* We can ignore /etc/nologin if we are logging in as root,
448 * it doesn't matter whether we are run by root or not */
449 if (pw->pw_uid != 0)
Denis Vlasenko68404f12008-03-17 09:00:54 +0000450 die_if_nologin();
Robert Griebl1fca5582002-06-04 20:45:46 +0000451
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200452#if ENABLE_LOGIN_SESSION_AS_CHILD
453 child_pid = vfork();
454 if (child_pid != 0) {
455 if (child_pid < 0)
456 bb_perror_msg("vfork");
457 else {
458 if (safe_waitpid(child_pid, NULL, 0) == -1)
459 bb_perror_msg("waitpid");
460 update_utmp(child_pid, DEAD_PROCESS, NULL, NULL, NULL);
461 }
462 IF_PAM(login_pam_end(pamh);)
463 return 0;
464 }
465#endif
466
467 IF_SELINUX(initselinux(username, full_tty, &user_sid);)
Rob Landley60158cb2005-05-03 06:25:50 +0000468
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000469 /* Try these, but don't complain if they fail.
470 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
471 fchown(0, pw->pw_uid, pw->pw_gid);
472 fchmod(0, 0600);
Robert Griebl1fca5582002-06-04 20:45:46 +0000473
Denys Vlasenko3a416112010-04-05 22:10:38 +0200474 update_utmp(getpid(), USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200475
Denis Vlasenko52816302007-11-06 05:26:51 +0000476 /* We trust environment only if we run by root */
Denis Vlasenko3266aa92009-04-01 11:24:04 +0000477 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000478 run_login_script(pw, full_tty);
Denis Vlasenko2e502912006-09-08 17:22:45 +0000479
Denis Vlasenko942e4292006-09-08 17:25:04 +0000480 change_identity(pw);
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100481 setup_environment(pw->pw_shell,
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100482 (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
483 pw);
Robert Griebl1fca5582002-06-04 20:45:46 +0000484
Ian Wienand260fb552010-12-20 11:33:38 -0800485#if ENABLE_PAM
486 /* Modules such as pam_env will setup the PAM environment,
487 * which should be copied into the new environment. */
488 pamenv = pam_getenvlist(pamh);
489 if (pamenv) while (*pamenv) {
490 putenv(*pamenv);
491 pamenv++;
492 }
493#endif
494
Denis Vlasenko942e4292006-09-08 17:25:04 +0000495 motd();
Robert Griebl1fca5582002-06-04 20:45:46 +0000496
Denis Vlasenko942e4292006-09-08 17:25:04 +0000497 if (pw->pw_uid == 0)
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000498 syslog(LOG_INFO, "root login%s", fromhost);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000499
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300500 if (ENABLE_FEATURE_CLEAN_UP)
501 free(fromhost);
502
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000503 /* well, a simple setexeccon() here would do the job as well,
504 * but let's play the game for now */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000505 IF_SELINUX(set_current_security_context(user_sid);)
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000506
507 // util-linux login also does:
508 // /* start new session */
509 // setsid();
510 // /* TIOCSCTTY: steal tty from other process group */
511 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000512 // BBox login used to do this (see above):
513 // bb_setpgrp();
514 // If this stuff is really needed, add it and explain why!
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000515
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000516 /* Set signals to defaults */
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000517 /* Non-ignored signals revert to SIG_DFL on exec anyway */
518 /*signal(SIGALRM, SIG_DFL);*/
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000519
Denis Vlasenko8c764872006-10-31 18:30:56 +0000520 /* Is this correct? This way user can ctrl-c out of /etc/profile,
521 * potentially creating security breach (tested with bash 3.0).
522 * But without this, bash 3.0 will not enable ctrl-c either.
523 * Maybe bash is buggy?
524 * Need to find out what standards say about /bin/login -
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000525 * should we leave SIGINT etc enabled or disabled? */
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000526 signal(SIGINT, SIG_DFL);
527
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000528 /* Exec login shell with no additional parameters */
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100529 run_shell(pw->pw_shell, 1, NULL, NULL);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000531 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000532}