blob: 6ec8dc42e17c63067858d89ef631e75c8af024bb [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() */
20# include <selinux/get_context_list.h> /* for get_default_context() */
21# include <selinux/flask.h> /* for security class definitions */
Eric Andersen9e480452003-07-03 10:07:04 +000022#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000023
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000024#if ENABLE_PAM
Denis Vlasenkoc6c23452007-08-22 18:14:44 +000025/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020026# undef setlocale
Denis Vlasenkoc6c23452007-08-22 18:14:44 +000027/* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
28 * Apparently they like to confuse people. */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020029# include <security/pam_appl.h>
30# include <security/pam_misc.h>
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000031static const struct pam_conv conv = {
32 misc_conv,
33 NULL
34};
35#endif
36
Denis Vlasenko942e4292006-09-08 17:25:04 +000037enum {
38 TIMEOUT = 60,
39 EMPTY_USERNAME_COUNT = 10,
Tanguy Pruvot823694d2012-11-18 13:20:29 +010040 /* Some users found 32 chars limit to be too low: */
41 USERNAME_SIZE = 64,
Denis Vlasenko9a9edf22006-09-08 17:29:53 +000042 TTYNAME_SIZE = 32,
Denis Vlasenko942e4292006-09-08 17:25:04 +000043};
Eric Andersen0fbff132002-06-22 17:49:29 +000044
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020045struct globals {
46 struct termios tty_attrs;
47} FIX_ALIASING;
48#define G (*(struct globals*)&bb_common_bufsiz1)
49#define INIT_G() do { } while (0)
50
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000051
Denis Vlasenkof312e322007-06-12 22:04:57 +000052#if ENABLE_FEATURE_NOLOGIN
Denis Vlasenko68404f12008-03-17 09:00:54 +000053static void die_if_nologin(void)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000054{
55 FILE *fp;
56 int c;
Denis Vlasenko694b5142008-11-07 01:12:16 +000057 int empty = 1;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000058
Denis Vlasenko5415c852008-07-21 23:05:26 +000059 fp = fopen_for_read("/etc/nologin");
Denis Vlasenko694b5142008-11-07 01:12:16 +000060 if (!fp) /* assuming it does not exist */
61 return;
62
63 while ((c = getc(fp)) != EOF) {
64 if (c == '\n')
65 bb_putchar('\r');
66 bb_putchar(c);
67 empty = 0;
68 }
69 if (empty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000070 puts("\r\nSystem closed for routine maintenance\r");
Denis Vlasenko694b5142008-11-07 01:12:16 +000071
72 fclose(fp);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010073 fflush_all();
Denis Vlasenko694b5142008-11-07 01:12:16 +000074 /* Users say that they do need this prior to exit: */
75 tcdrain(STDOUT_FILENO);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000076 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000077}
Denis Vlasenkof312e322007-06-12 22:04:57 +000078#else
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020079# define die_if_nologin() ((void)0)
Denis Vlasenkof312e322007-06-12 22:04:57 +000080#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000081
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000082#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020083static int check_securetty(const char *short_tty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000084{
Denis Vlasenko084266e2008-07-26 23:08:31 +000085 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
Denis Vlasenko5415c852008-07-21 23:05:26 +000086 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000087 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
88 if (strcmp(buf, short_tty) == 0)
89 break;
90 buf = NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000091 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000092 config_close(parser);
93 /* buf != NULL here if config file was not found, empty
94 * or line was found which equals short_tty */
95 return buf != NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000096}
Robert Griebl1fca5582002-06-04 20:45:46 +000097#else
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020098static ALWAYS_INLINE int check_securetty(const char *short_tty UNUSED_PARAM) { return 1; }
Robert Griebl1fca5582002-06-04 20:45:46 +000099#endif
100
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000101#if ENABLE_SELINUX
102static void initselinux(char *username, char *full_tty,
103 security_context_t *user_sid)
104{
105 security_context_t old_tty_sid, new_tty_sid;
106
107 if (!is_selinux_enabled())
108 return;
109
110 if (get_default_context(username, NULL, user_sid)) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100111 bb_error_msg_and_die("can't get SID for %s", username);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000112 }
113 if (getfilecon(full_tty, &old_tty_sid) < 0) {
114 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
115 }
Denis Vlasenko8e5de2a2009-02-24 17:10:24 +0000116 if (security_compute_relabel(*user_sid, old_tty_sid,
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000117 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
118 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
119 }
120 if (setfilecon(full_tty, new_tty_sid) != 0) {
121 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
122 }
123}
124#endif
125
126#if ENABLE_LOGIN_SCRIPTS
127static void run_login_script(struct passwd *pw, char *full_tty)
128{
129 char *t_argv[2];
130
131 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
132 if (t_argv[0]) {
133 t_argv[1] = NULL;
134 xsetenv("LOGIN_TTY", full_tty);
135 xsetenv("LOGIN_USER", pw->pw_name);
136 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
137 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
138 xsetenv("LOGIN_SHELL", pw->pw_shell);
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200139 spawn_and_wait(t_argv); /* NOMMU-friendly */
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000140 unsetenv("LOGIN_TTY");
141 unsetenv("LOGIN_USER");
142 unsetenv("LOGIN_UID");
143 unsetenv("LOGIN_GID");
144 unsetenv("LOGIN_SHELL");
145 }
146}
147#else
148void run_login_script(struct passwd *pw, char *full_tty);
149#endif
150
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200151#if ENABLE_LOGIN_SESSION_AS_CHILD && ENABLE_PAM
152static void login_pam_end(pam_handle_t *pamh)
153{
154 int pamret;
155
156 pamret = pam_setcred(pamh, PAM_DELETE_CRED);
157 if (pamret != PAM_SUCCESS) {
158 bb_error_msg("pam_%s failed: %s (%d)", "setcred",
159 pam_strerror(pamh, pamret), pamret);
160 }
161 pamret = pam_close_session(pamh, 0);
162 if (pamret != PAM_SUCCESS) {
163 bb_error_msg("pam_%s failed: %s (%d)", "close_session",
164 pam_strerror(pamh, pamret), pamret);
165 }
166 pamret = pam_end(pamh, pamret);
167 if (pamret != PAM_SUCCESS) {
168 bb_error_msg("pam_%s failed: %s (%d)", "end",
169 pam_strerror(pamh, pamret), pamret);
170 }
171}
172#endif /* ENABLE_PAM */
173
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000174static void get_username_or_die(char *buf, int size_buf)
175{
176 int c, cntdown;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000177
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000178 cntdown = EMPTY_USERNAME_COUNT;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000179 prompt:
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000180 print_login_prompt();
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000181 /* skip whitespace */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000182 do {
183 c = getchar();
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200184 if (c == EOF)
185 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000186 if (c == '\n') {
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200187 if (!--cntdown)
188 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000189 goto prompt;
190 }
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200191 } while (isspace(c)); /* maybe isblank? */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000192
193 *buf++ = c;
194 if (!fgets(buf, size_buf-2, stdin))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000195 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000196 if (!strchr(buf, '\n'))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000197 exit(EXIT_FAILURE);
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200198 while ((unsigned char)*buf > ' ')
199 buf++;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000200 *buf = '\0';
201}
202
203static void motd(void)
204{
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000205 int fd;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000206
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000207 fd = open(bb_path_motd_file, O_RDONLY);
Denis Vlasenko52816302007-11-06 05:26:51 +0000208 if (fd >= 0) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100209 fflush_all();
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000210 bb_copyfd_eof(fd, STDOUT_FILENO);
211 close(fd);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000212 }
213}
Robert Griebl1fca5582002-06-04 20:45:46 +0000214
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000215static void alarm_handler(int sig UNUSED_PARAM)
Robert Griebl1fca5582002-06-04 20:45:46 +0000216{
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200217 /* This is the escape hatch! Poor serial line users and the like
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000218 * arrive here when their connection is broken.
219 * We don't want to block here */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200220 ndelay_on(STDOUT_FILENO);
221 /* Test for correct attr restoring:
222 * run "getty 0 -" from a shell, enter bogus username, stop at
223 * password prompt, let it time out. Without the tcsetattr below,
224 * when you are back at shell prompt, echo will be still off.
225 */
226 tcsetattr_stdin_TCSANOW(&G.tty_attrs);
227 printf("\r\nLogin timed out after %u seconds\r\n", TIMEOUT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100228 fflush_all();
Denis Vlasenko52816302007-11-06 05:26:51 +0000229 /* unix API is brain damaged regarding O_NONBLOCK,
230 * we should undo it, or else we can affect other processes */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200231 ndelay_off(STDOUT_FILENO);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000232 _exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000233}
234
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000235int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000236int login_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000237{
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000238 enum {
239 LOGIN_OPT_f = (1<<0),
240 LOGIN_OPT_h = (1<<1),
241 LOGIN_OPT_p = (1<<2),
242 };
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000243 char *fromhost;
Eric Andersen0fbff132002-06-22 17:49:29 +0000244 char username[USERNAME_SIZE];
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000245 int run_by_root;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000246 unsigned opt;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000247 int count = 0;
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000248 struct passwd *pw;
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200249 char *opt_host = NULL;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000250 char *opt_user = opt_user; /* for compiler */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000251 char *full_tty;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200252 char *short_tty;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000253 IF_SELINUX(security_context_t user_sid = NULL;)
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000254#if ENABLE_PAM
255 int pamret;
256 pam_handle_t *pamh;
257 const char *pamuser;
258 const char *failed_msg;
259 struct passwd pwdstruct;
260 char pwdbuf[256];
Ian Wienand260fb552010-12-20 11:33:38 -0800261 char **pamenv;
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000262#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200263#if ENABLE_LOGIN_SESSION_AS_CHILD
264 pid_t child_pid;
265#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000266
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200267 INIT_G();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000268
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000269 /* More of suid paranoia if called by non-root: */
270 /* Clear dangerous stuff, set PATH */
271 run_by_root = !sanitize_env_if_suid();
Denis Vlasenkoc9ca0a32008-02-18 11:08:33 +0000272
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000273 /* Mandatory paranoia for suid applet:
274 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
275 * and any extra open fd's are closed.
276 * (The name of the function is misleading. Not daemonizing here.) */
277 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
278
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200279 username[0] = '\0';
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000280 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000281 if (opt & LOGIN_OPT_f) {
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000282 if (!run_by_root)
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000283 bb_error_msg_and_die("-f is for root only");
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000284 safe_strncpy(username, opt_user, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000285 }
Denis Vlasenko1d426652008-03-17 09:09:09 +0000286 argv += optind;
287 if (argv[0]) /* user from command line (getty) */
288 safe_strncpy(username, argv[0], sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000289
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200290 /* Save tty attributes - and by doing it, check that it's indeed a tty */
291 if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0
292 || !isatty(STDOUT_FILENO)
293 /*|| !isatty(STDERR_FILENO) - no, guess some people might want to redirect this */
294 ) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200295 return EXIT_FAILURE; /* Must be a terminal */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200296 }
297
298 /* We install timeout handler only _after_ we saved G.tty_attrs */
299 signal(SIGALRM, alarm_handler);
300 alarm(TIMEOUT);
301
302 /* Find out and memorize our tty name */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000303 full_tty = xmalloc_ttyname(STDIN_FILENO);
304 if (!full_tty)
305 full_tty = xstrdup("UNKNOWN");
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200306 short_tty = skip_dev_pfx(full_tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000307
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200308 if (opt_host) {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000309 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000310 } else {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000311 fromhost = xasprintf(" on '%s'", short_tty);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000312 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000313
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000314 /* Was breaking "login <username>" from shell command line: */
315 /*bb_setpgrp();*/
Eric Andersen0fbff132002-06-22 17:49:29 +0000316
Denis Vlasenko54ac03a2009-03-11 15:59:49 +0000317 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000318
Denis Vlasenko942e4292006-09-08 17:25:04 +0000319 while (1) {
Paul Fox6e1b62b2007-11-07 15:51:35 +0000320 /* flush away any type-ahead (as getty does) */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200321 tcflush(0, TCIFLUSH);
Paul Fox6e1b62b2007-11-07 15:51:35 +0000322
Denis Vlasenko942e4292006-09-08 17:25:04 +0000323 if (!username[0])
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000324 get_username_or_die(username, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000325
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000326#if ENABLE_PAM
327 pamret = pam_start("login", username, &conv, &pamh);
328 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000329 failed_msg = "start";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000330 goto pam_auth_failed;
331 }
332 /* set TTY (so things like securetty work) */
333 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
334 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000335 failed_msg = "set_item(TTY)";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000336 goto pam_auth_failed;
337 }
Ryan Phillipscf9074b2011-03-22 18:27:21 +0100338 /* set RHOST */
339 if (opt_host) {
340 pamret = pam_set_item(pamh, PAM_RHOST, opt_host);
341 if (pamret != PAM_SUCCESS) {
342 failed_msg = "set_item(RHOST)";
343 goto pam_auth_failed;
344 }
345 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200346 if (!(opt & LOGIN_OPT_f)) {
347 pamret = pam_authenticate(pamh, 0);
348 if (pamret != PAM_SUCCESS) {
349 failed_msg = "authenticate";
350 goto pam_auth_failed;
351 /* TODO: or just "goto auth_failed"
352 * since user seems to enter wrong password
353 * (in this case pamret == 7)
354 */
355 }
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000356 }
357 /* check that the account is healthy */
358 pamret = pam_acct_mgmt(pamh, 0);
359 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000360 failed_msg = "acct_mgmt";
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000361 goto pam_auth_failed;
362 }
363 /* read user back */
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000364 pamuser = NULL;
365 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
366 * thus we cast to (void*) */
367 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
368 failed_msg = "get_item(USER)";
369 goto pam_auth_failed;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000370 }
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000371 if (!pamuser || !pamuser[0])
372 goto auth_failed;
373 safe_strncpy(username, pamuser, sizeof(username));
374 /* Don't use "pw = getpwnam(username);",
375 * PAM is said to be capable of destroying static storage
376 * used by getpwnam(). We are using safe(r) function */
377 pw = NULL;
378 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
379 if (!pw)
380 goto auth_failed;
381 pamret = pam_open_session(pamh, 0);
382 if (pamret != PAM_SUCCESS) {
383 failed_msg = "open_session";
384 goto pam_auth_failed;
385 }
386 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
387 if (pamret != PAM_SUCCESS) {
388 failed_msg = "setcred";
389 goto pam_auth_failed;
390 }
391 break; /* success, continue login process */
392
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000393 pam_auth_failed:
Denys Vlasenkoc297ea92009-09-25 01:50:45 +0200394 /* syslog, because we don't want potential attacker
395 * to know _why_ login failed */
396 syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000397 pam_strerror(pamh, pamret), pamret);
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000398 safe_strncpy(username, "UNKNOWN", sizeof(username));
399#else /* not PAM */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000400 pw = getpwnam(username);
401 if (!pw) {
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000402 strcpy(username, "UNKNOWN");
403 goto fake_it;
Robert Griebl1fca5582002-06-04 20:45:46 +0000404 }
405
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000406 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
407 goto auth_failed;
408
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000409 if (opt & LOGIN_OPT_f)
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000410 break; /* -f USER: success without asking passwd */
411
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200412 if (pw->pw_uid == 0 && !check_securetty(short_tty))
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000413 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000414
415 /* Don't check the password if password entry is empty (!) */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000416 if (!pw->pw_passwd[0])
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000417 break;
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000418 fake_it:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200419 /* Password reading and authorization takes place here.
420 * Note that reads (in no-echo mode) trash tty attributes.
421 * If we get interrupted by SIGALRM, we need to restore attrs.
422 */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000423 if (correct_password(pw))
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000424 break;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000425#endif /* ENABLE_PAM */
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000426 auth_failed:
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000427 opt &= ~LOGIN_OPT_f;
Denys Vlasenko7d4e7a22011-03-08 21:07:05 +0100428 bb_do_delay(LOGIN_FAIL_DELAY);
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000429 /* TODO: doesn't sound like correct English phrase to me */
Robert Griebl1fca5582002-06-04 20:45:46 +0000430 puts("Login incorrect");
Denis Vlasenko942e4292006-09-08 17:25:04 +0000431 if (++count == 3) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000432 syslog(LOG_WARNING, "invalid password for '%s'%s",
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000433 username, fromhost);
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300434
435 if (ENABLE_FEATURE_CLEAN_UP)
436 free(fromhost);
437
Robert Griebl1fca5582002-06-04 20:45:46 +0000438 return EXIT_FAILURE;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000439 }
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000440 username[0] = '\0';
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000441 } /* while (1) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000442
Denis Vlasenko942e4292006-09-08 17:25:04 +0000443 alarm(0);
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000444 /* We can ignore /etc/nologin if we are logging in as root,
445 * it doesn't matter whether we are run by root or not */
446 if (pw->pw_uid != 0)
Denis Vlasenko68404f12008-03-17 09:00:54 +0000447 die_if_nologin();
Robert Griebl1fca5582002-06-04 20:45:46 +0000448
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200449#if ENABLE_LOGIN_SESSION_AS_CHILD
450 child_pid = vfork();
451 if (child_pid != 0) {
452 if (child_pid < 0)
453 bb_perror_msg("vfork");
454 else {
455 if (safe_waitpid(child_pid, NULL, 0) == -1)
456 bb_perror_msg("waitpid");
457 update_utmp(child_pid, DEAD_PROCESS, NULL, NULL, NULL);
458 }
459 IF_PAM(login_pam_end(pamh);)
460 return 0;
461 }
462#endif
463
464 IF_SELINUX(initselinux(username, full_tty, &user_sid);)
Rob Landley60158cb2005-05-03 06:25:50 +0000465
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000466 /* Try these, but don't complain if they fail.
467 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
468 fchown(0, pw->pw_uid, pw->pw_gid);
469 fchmod(0, 0600);
Robert Griebl1fca5582002-06-04 20:45:46 +0000470
Denys Vlasenko3a416112010-04-05 22:10:38 +0200471 update_utmp(getpid(), USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200472
Denis Vlasenko52816302007-11-06 05:26:51 +0000473 /* We trust environment only if we run by root */
Denis Vlasenko3266aa92009-04-01 11:24:04 +0000474 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000475 run_login_script(pw, full_tty);
Denis Vlasenko2e502912006-09-08 17:22:45 +0000476
Denis Vlasenko942e4292006-09-08 17:25:04 +0000477 change_identity(pw);
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100478 setup_environment(pw->pw_shell,
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100479 (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
480 pw);
Robert Griebl1fca5582002-06-04 20:45:46 +0000481
Ian Wienand260fb552010-12-20 11:33:38 -0800482#if ENABLE_PAM
483 /* Modules such as pam_env will setup the PAM environment,
484 * which should be copied into the new environment. */
485 pamenv = pam_getenvlist(pamh);
486 if (pamenv) while (*pamenv) {
487 putenv(*pamenv);
488 pamenv++;
489 }
490#endif
491
Denis Vlasenko942e4292006-09-08 17:25:04 +0000492 motd();
Robert Griebl1fca5582002-06-04 20:45:46 +0000493
Denis Vlasenko942e4292006-09-08 17:25:04 +0000494 if (pw->pw_uid == 0)
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000495 syslog(LOG_INFO, "root login%s", fromhost);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000496
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300497 if (ENABLE_FEATURE_CLEAN_UP)
498 free(fromhost);
499
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000500 /* well, a simple setexeccon() here would do the job as well,
501 * but let's play the game for now */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000502 IF_SELINUX(set_current_security_context(user_sid);)
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000503
504 // util-linux login also does:
505 // /* start new session */
506 // setsid();
507 // /* TIOCSCTTY: steal tty from other process group */
508 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000509 // BBox login used to do this (see above):
510 // bb_setpgrp();
511 // If this stuff is really needed, add it and explain why!
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000512
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000513 /* Set signals to defaults */
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000514 /* Non-ignored signals revert to SIG_DFL on exec anyway */
515 /*signal(SIGALRM, SIG_DFL);*/
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000516
Denis Vlasenko8c764872006-10-31 18:30:56 +0000517 /* Is this correct? This way user can ctrl-c out of /etc/profile,
518 * potentially creating security breach (tested with bash 3.0).
519 * But without this, bash 3.0 will not enable ctrl-c either.
520 * Maybe bash is buggy?
521 * Need to find out what standards say about /bin/login -
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000522 * should we leave SIGINT etc enabled or disabled? */
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000523 signal(SIGINT, SIG_DFL);
524
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000525 /* Exec login shell with no additional parameters */
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100526 run_shell(pw->pw_shell, 1, NULL, NULL);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000527
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000528 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000529}