blob: bf43f3aba72c9e905a630d9d8d0bf6274f340ed5 [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,
40 USERNAME_SIZE = 32,
Denis Vlasenko9a9edf22006-09-08 17:29:53 +000041 TTYNAME_SIZE = 32,
Denis Vlasenko942e4292006-09-08 17:25:04 +000042};
Eric Andersen0fbff132002-06-22 17:49:29 +000043
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020044struct globals {
45 struct termios tty_attrs;
46} FIX_ALIASING;
47#define G (*(struct globals*)&bb_common_bufsiz1)
48#define INIT_G() do { } while (0)
49
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000050
Denis Vlasenkof312e322007-06-12 22:04:57 +000051#if ENABLE_FEATURE_NOLOGIN
Denis Vlasenko68404f12008-03-17 09:00:54 +000052static void die_if_nologin(void)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000053{
54 FILE *fp;
55 int c;
Denis Vlasenko694b5142008-11-07 01:12:16 +000056 int empty = 1;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000057
Denis Vlasenko5415c852008-07-21 23:05:26 +000058 fp = fopen_for_read("/etc/nologin");
Denis Vlasenko694b5142008-11-07 01:12:16 +000059 if (!fp) /* assuming it does not exist */
60 return;
61
62 while ((c = getc(fp)) != EOF) {
63 if (c == '\n')
64 bb_putchar('\r');
65 bb_putchar(c);
66 empty = 0;
67 }
68 if (empty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000069 puts("\r\nSystem closed for routine maintenance\r");
Denis Vlasenko694b5142008-11-07 01:12:16 +000070
71 fclose(fp);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010072 fflush_all();
Denis Vlasenko694b5142008-11-07 01:12:16 +000073 /* Users say that they do need this prior to exit: */
74 tcdrain(STDOUT_FILENO);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000075 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000076}
Denis Vlasenkof312e322007-06-12 22:04:57 +000077#else
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020078# define die_if_nologin() ((void)0)
Denis Vlasenkof312e322007-06-12 22:04:57 +000079#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000080
Denis Vlasenkod6e81c72007-08-21 10:58:18 +000081#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020082static int check_securetty(const char *short_tty)
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000083{
Denis Vlasenko084266e2008-07-26 23:08:31 +000084 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
Denis Vlasenko5415c852008-07-21 23:05:26 +000085 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000086 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
87 if (strcmp(buf, short_tty) == 0)
88 break;
89 buf = NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000090 }
Denis Vlasenko084266e2008-07-26 23:08:31 +000091 config_close(parser);
92 /* buf != NULL here if config file was not found, empty
93 * or line was found which equals short_tty */
94 return buf != NULL;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +000095}
Robert Griebl1fca5582002-06-04 20:45:46 +000096#else
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020097static ALWAYS_INLINE int check_securetty(const char *short_tty UNUSED_PARAM) { return 1; }
Robert Griebl1fca5582002-06-04 20:45:46 +000098#endif
99
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000100#if ENABLE_SELINUX
101static void initselinux(char *username, char *full_tty,
102 security_context_t *user_sid)
103{
104 security_context_t old_tty_sid, new_tty_sid;
105
106 if (!is_selinux_enabled())
107 return;
108
109 if (get_default_context(username, NULL, user_sid)) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100110 bb_error_msg_and_die("can't get SID for %s", username);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000111 }
112 if (getfilecon(full_tty, &old_tty_sid) < 0) {
113 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
114 }
Denis Vlasenko8e5de2a2009-02-24 17:10:24 +0000115 if (security_compute_relabel(*user_sid, old_tty_sid,
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000116 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
117 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
118 }
119 if (setfilecon(full_tty, new_tty_sid) != 0) {
120 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
121 }
122}
123#endif
124
125#if ENABLE_LOGIN_SCRIPTS
126static void run_login_script(struct passwd *pw, char *full_tty)
127{
128 char *t_argv[2];
129
130 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
131 if (t_argv[0]) {
132 t_argv[1] = NULL;
133 xsetenv("LOGIN_TTY", full_tty);
134 xsetenv("LOGIN_USER", pw->pw_name);
135 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
136 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
137 xsetenv("LOGIN_SHELL", pw->pw_shell);
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200138 spawn_and_wait(t_argv); /* NOMMU-friendly */
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000139 unsetenv("LOGIN_TTY");
140 unsetenv("LOGIN_USER");
141 unsetenv("LOGIN_UID");
142 unsetenv("LOGIN_GID");
143 unsetenv("LOGIN_SHELL");
144 }
145}
146#else
147void run_login_script(struct passwd *pw, char *full_tty);
148#endif
149
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200150#if ENABLE_LOGIN_SESSION_AS_CHILD && ENABLE_PAM
151static void login_pam_end(pam_handle_t *pamh)
152{
153 int pamret;
154
155 pamret = pam_setcred(pamh, PAM_DELETE_CRED);
156 if (pamret != PAM_SUCCESS) {
157 bb_error_msg("pam_%s failed: %s (%d)", "setcred",
158 pam_strerror(pamh, pamret), pamret);
159 }
160 pamret = pam_close_session(pamh, 0);
161 if (pamret != PAM_SUCCESS) {
162 bb_error_msg("pam_%s failed: %s (%d)", "close_session",
163 pam_strerror(pamh, pamret), pamret);
164 }
165 pamret = pam_end(pamh, pamret);
166 if (pamret != PAM_SUCCESS) {
167 bb_error_msg("pam_%s failed: %s (%d)", "end",
168 pam_strerror(pamh, pamret), pamret);
169 }
170}
171#endif /* ENABLE_PAM */
172
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000173static void get_username_or_die(char *buf, int size_buf)
174{
175 int c, cntdown;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000176
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000177 cntdown = EMPTY_USERNAME_COUNT;
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000178 prompt:
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000179 print_login_prompt();
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000180 /* skip whitespace */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000181 do {
182 c = getchar();
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200183 if (c == EOF)
184 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000185 if (c == '\n') {
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200186 if (!--cntdown)
187 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000188 goto prompt;
189 }
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200190 } while (isspace(c)); /* maybe isblank? */
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000191
192 *buf++ = c;
193 if (!fgets(buf, size_buf-2, stdin))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000194 exit(EXIT_FAILURE);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000195 if (!strchr(buf, '\n'))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000196 exit(EXIT_FAILURE);
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200197 while ((unsigned char)*buf > ' ')
198 buf++;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000199 *buf = '\0';
200}
201
202static void motd(void)
203{
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000204 int fd;
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000205
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000206 fd = open(bb_path_motd_file, O_RDONLY);
Denis Vlasenko52816302007-11-06 05:26:51 +0000207 if (fd >= 0) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100208 fflush_all();
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000209 bb_copyfd_eof(fd, STDOUT_FILENO);
210 close(fd);
Denis Vlasenko7ccf5cc2006-09-14 17:03:18 +0000211 }
212}
Robert Griebl1fca5582002-06-04 20:45:46 +0000213
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000214static void alarm_handler(int sig UNUSED_PARAM)
Robert Griebl1fca5582002-06-04 20:45:46 +0000215{
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200216 /* This is the escape hatch! Poor serial line users and the like
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000217 * arrive here when their connection is broken.
218 * We don't want to block here */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200219 ndelay_on(STDOUT_FILENO);
220 /* Test for correct attr restoring:
221 * run "getty 0 -" from a shell, enter bogus username, stop at
222 * password prompt, let it time out. Without the tcsetattr below,
223 * when you are back at shell prompt, echo will be still off.
224 */
225 tcsetattr_stdin_TCSANOW(&G.tty_attrs);
226 printf("\r\nLogin timed out after %u seconds\r\n", TIMEOUT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100227 fflush_all();
Denis Vlasenko52816302007-11-06 05:26:51 +0000228 /* unix API is brain damaged regarding O_NONBLOCK,
229 * we should undo it, or else we can affect other processes */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200230 ndelay_off(STDOUT_FILENO);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000231 _exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000232}
233
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000234int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000235int login_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000236{
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000237 enum {
238 LOGIN_OPT_f = (1<<0),
239 LOGIN_OPT_h = (1<<1),
240 LOGIN_OPT_p = (1<<2),
241 };
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000242 char *fromhost;
Eric Andersen0fbff132002-06-22 17:49:29 +0000243 char username[USERNAME_SIZE];
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000244 int run_by_root;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000245 unsigned opt;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000246 int count = 0;
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000247 struct passwd *pw;
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200248 char *opt_host = NULL;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000249 char *opt_user = opt_user; /* for compiler */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000250 char *full_tty;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200251 char *short_tty;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000252 IF_SELINUX(security_context_t user_sid = NULL;)
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000253#if ENABLE_PAM
254 int pamret;
255 pam_handle_t *pamh;
256 const char *pamuser;
257 const char *failed_msg;
258 struct passwd pwdstruct;
259 char pwdbuf[256];
Ian Wienand260fb552010-12-20 11:33:38 -0800260 char **pamenv;
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000261#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200262#if ENABLE_LOGIN_SESSION_AS_CHILD
263 pid_t child_pid;
264#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000265
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200266 INIT_G();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000267
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000268 /* More of suid paranoia if called by non-root: */
269 /* Clear dangerous stuff, set PATH */
270 run_by_root = !sanitize_env_if_suid();
Denis Vlasenkoc9ca0a32008-02-18 11:08:33 +0000271
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000272 /* Mandatory paranoia for suid applet:
273 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
274 * and any extra open fd's are closed.
275 * (The name of the function is misleading. Not daemonizing here.) */
276 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
277
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200278 username[0] = '\0';
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000279 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000280 if (opt & LOGIN_OPT_f) {
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000281 if (!run_by_root)
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000282 bb_error_msg_and_die("-f is for root only");
Denis Vlasenko22f6dcb2006-09-26 16:31:01 +0000283 safe_strncpy(username, opt_user, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000284 }
Denis Vlasenko1d426652008-03-17 09:09:09 +0000285 argv += optind;
286 if (argv[0]) /* user from command line (getty) */
287 safe_strncpy(username, argv[0], sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000288
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200289 /* Save tty attributes - and by doing it, check that it's indeed a tty */
290 if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0
291 || !isatty(STDOUT_FILENO)
292 /*|| !isatty(STDERR_FILENO) - no, guess some people might want to redirect this */
293 ) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200294 return EXIT_FAILURE; /* Must be a terminal */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200295 }
296
297 /* We install timeout handler only _after_ we saved G.tty_attrs */
298 signal(SIGALRM, alarm_handler);
299 alarm(TIMEOUT);
300
301 /* Find out and memorize our tty name */
Denis Vlasenko512c8ae2009-02-02 00:15:57 +0000302 full_tty = xmalloc_ttyname(STDIN_FILENO);
303 if (!full_tty)
304 full_tty = xstrdup("UNKNOWN");
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200305 short_tty = skip_dev_pfx(full_tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000306
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200307 if (opt_host) {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000308 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000309 } else {
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000310 fromhost = xasprintf(" on '%s'", short_tty);
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +0000311 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000312
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000313 /* Was breaking "login <username>" from shell command line: */
314 /*bb_setpgrp();*/
Eric Andersen0fbff132002-06-22 17:49:29 +0000315
Denis Vlasenko54ac03a2009-03-11 15:59:49 +0000316 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000317
Denis Vlasenko942e4292006-09-08 17:25:04 +0000318 while (1) {
Paul Fox6e1b62b2007-11-07 15:51:35 +0000319 /* flush away any type-ahead (as getty does) */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200320 tcflush(0, TCIFLUSH);
Paul Fox6e1b62b2007-11-07 15:51:35 +0000321
Denis Vlasenko942e4292006-09-08 17:25:04 +0000322 if (!username[0])
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000323 get_username_or_die(username, sizeof(username));
Robert Griebl1fca5582002-06-04 20:45:46 +0000324
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000325#if ENABLE_PAM
326 pamret = pam_start("login", username, &conv, &pamh);
327 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000328 failed_msg = "start";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000329 goto pam_auth_failed;
330 }
331 /* set TTY (so things like securetty work) */
332 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
333 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000334 failed_msg = "set_item(TTY)";
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000335 goto pam_auth_failed;
336 }
Ryan Phillipscf9074b2011-03-22 18:27:21 +0100337 /* set RHOST */
338 if (opt_host) {
339 pamret = pam_set_item(pamh, PAM_RHOST, opt_host);
340 if (pamret != PAM_SUCCESS) {
341 failed_msg = "set_item(RHOST)";
342 goto pam_auth_failed;
343 }
344 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200345 if (!(opt & LOGIN_OPT_f)) {
346 pamret = pam_authenticate(pamh, 0);
347 if (pamret != PAM_SUCCESS) {
348 failed_msg = "authenticate";
349 goto pam_auth_failed;
350 /* TODO: or just "goto auth_failed"
351 * since user seems to enter wrong password
352 * (in this case pamret == 7)
353 */
354 }
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000355 }
356 /* check that the account is healthy */
357 pamret = pam_acct_mgmt(pamh, 0);
358 if (pamret != PAM_SUCCESS) {
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000359 failed_msg = "acct_mgmt";
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000360 goto pam_auth_failed;
361 }
362 /* read user back */
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000363 pamuser = NULL;
364 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
365 * thus we cast to (void*) */
366 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
367 failed_msg = "get_item(USER)";
368 goto pam_auth_failed;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000369 }
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000370 if (!pamuser || !pamuser[0])
371 goto auth_failed;
372 safe_strncpy(username, pamuser, sizeof(username));
373 /* Don't use "pw = getpwnam(username);",
374 * PAM is said to be capable of destroying static storage
375 * used by getpwnam(). We are using safe(r) function */
376 pw = NULL;
377 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
378 if (!pw)
379 goto auth_failed;
380 pamret = pam_open_session(pamh, 0);
381 if (pamret != PAM_SUCCESS) {
382 failed_msg = "open_session";
383 goto pam_auth_failed;
384 }
385 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
386 if (pamret != PAM_SUCCESS) {
387 failed_msg = "setcred";
388 goto pam_auth_failed;
389 }
390 break; /* success, continue login process */
391
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000392 pam_auth_failed:
Denys Vlasenkoc297ea92009-09-25 01:50:45 +0200393 /* syslog, because we don't want potential attacker
394 * to know _why_ login failed */
395 syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
Denis Vlasenkoa29a5e42007-11-07 00:23:47 +0000396 pam_strerror(pamh, pamret), pamret);
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000397 safe_strncpy(username, "UNKNOWN", sizeof(username));
398#else /* not PAM */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000399 pw = getpwnam(username);
400 if (!pw) {
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000401 strcpy(username, "UNKNOWN");
402 goto fake_it;
Robert Griebl1fca5582002-06-04 20:45:46 +0000403 }
404
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000405 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
406 goto auth_failed;
407
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000408 if (opt & LOGIN_OPT_f)
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000409 break; /* -f USER: success without asking passwd */
410
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200411 if (pw->pw_uid == 0 && !check_securetty(short_tty))
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000412 goto auth_failed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000413
414 /* Don't check the password if password entry is empty (!) */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000415 if (!pw->pw_passwd[0])
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000416 break;
Denis Vlasenko65e14b42007-06-08 15:27:06 +0000417 fake_it:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200418 /* Password reading and authorization takes place here.
419 * Note that reads (in no-echo mode) trash tty attributes.
420 * If we get interrupted by SIGALRM, we need to restore attrs.
421 */
Denis Vlasenko942e4292006-09-08 17:25:04 +0000422 if (correct_password(pw))
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000423 break;
Denis Vlasenkod6e81c72007-08-21 10:58:18 +0000424#endif /* ENABLE_PAM */
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000425 auth_failed:
Denis Vlasenko66fabdb2006-09-17 14:45:09 +0000426 opt &= ~LOGIN_OPT_f;
Denys Vlasenko7d4e7a22011-03-08 21:07:05 +0100427 bb_do_delay(LOGIN_FAIL_DELAY);
Denis Vlasenko82f3b162007-09-03 11:56:27 +0000428 /* TODO: doesn't sound like correct English phrase to me */
Robert Griebl1fca5582002-06-04 20:45:46 +0000429 puts("Login incorrect");
Denis Vlasenko942e4292006-09-08 17:25:04 +0000430 if (++count == 3) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000431 syslog(LOG_WARNING, "invalid password for '%s'%s",
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000432 username, fromhost);
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300433
434 if (ENABLE_FEATURE_CLEAN_UP)
435 free(fromhost);
436
Robert Griebl1fca5582002-06-04 20:45:46 +0000437 return EXIT_FAILURE;
Denis Vlasenko942e4292006-09-08 17:25:04 +0000438 }
Denis Vlasenko9a9edf22006-09-08 17:29:53 +0000439 username[0] = '\0';
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000440 } /* while (1) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000441
Denis Vlasenko942e4292006-09-08 17:25:04 +0000442 alarm(0);
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000443 /* We can ignore /etc/nologin if we are logging in as root,
444 * it doesn't matter whether we are run by root or not */
445 if (pw->pw_uid != 0)
Denis Vlasenko68404f12008-03-17 09:00:54 +0000446 die_if_nologin();
Robert Griebl1fca5582002-06-04 20:45:46 +0000447
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200448#if ENABLE_LOGIN_SESSION_AS_CHILD
449 child_pid = vfork();
450 if (child_pid != 0) {
451 if (child_pid < 0)
452 bb_perror_msg("vfork");
453 else {
454 if (safe_waitpid(child_pid, NULL, 0) == -1)
455 bb_perror_msg("waitpid");
456 update_utmp(child_pid, DEAD_PROCESS, NULL, NULL, NULL);
457 }
458 IF_PAM(login_pam_end(pamh);)
459 return 0;
460 }
461#endif
462
463 IF_SELINUX(initselinux(username, full_tty, &user_sid);)
Rob Landley60158cb2005-05-03 06:25:50 +0000464
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000465 /* Try these, but don't complain if they fail.
466 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
467 fchown(0, pw->pw_uid, pw->pw_gid);
468 fchmod(0, 0600);
Robert Griebl1fca5582002-06-04 20:45:46 +0000469
Denys Vlasenko3a416112010-04-05 22:10:38 +0200470 update_utmp(getpid(), USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
Denys Vlasenko37f5bef2010-04-05 03:18:40 +0200471
Denis Vlasenko52816302007-11-06 05:26:51 +0000472 /* We trust environment only if we run by root */
Denis Vlasenko3266aa92009-04-01 11:24:04 +0000473 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000474 run_login_script(pw, full_tty);
Denis Vlasenko2e502912006-09-08 17:22:45 +0000475
Denis Vlasenko942e4292006-09-08 17:25:04 +0000476 change_identity(pw);
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100477 setup_environment(pw->pw_shell,
Denys Vlasenkofd686a22010-02-26 09:52:45 +0100478 (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
479 pw);
Robert Griebl1fca5582002-06-04 20:45:46 +0000480
Ian Wienand260fb552010-12-20 11:33:38 -0800481#if ENABLE_PAM
482 /* Modules such as pam_env will setup the PAM environment,
483 * which should be copied into the new environment. */
484 pamenv = pam_getenvlist(pamh);
485 if (pamenv) while (*pamenv) {
486 putenv(*pamenv);
487 pamenv++;
488 }
489#endif
490
Denis Vlasenko942e4292006-09-08 17:25:04 +0000491 motd();
Robert Griebl1fca5582002-06-04 20:45:46 +0000492
Denis Vlasenko942e4292006-09-08 17:25:04 +0000493 if (pw->pw_uid == 0)
Denis Vlasenko2f50aa42006-09-08 17:56:52 +0000494 syslog(LOG_INFO, "root login%s", fromhost);
Denis Vlasenko4eff8ef2009-02-02 00:15:00 +0000495
Alexander Shishkin78b286f2010-10-27 19:52:40 +0300496 if (ENABLE_FEATURE_CLEAN_UP)
497 free(fromhost);
498
Rob Landleyd1f8c1c2006-03-27 23:04:42 +0000499 /* well, a simple setexeccon() here would do the job as well,
500 * but let's play the game for now */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000501 IF_SELINUX(set_current_security_context(user_sid);)
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000502
503 // util-linux login also does:
504 // /* start new session */
505 // setsid();
506 // /* TIOCSCTTY: steal tty from other process group */
507 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
Denis Vlasenko0de3c552007-04-12 12:31:02 +0000508 // BBox login used to do this (see above):
509 // bb_setpgrp();
510 // If this stuff is really needed, add it and explain why!
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000511
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000512 /* Set signals to defaults */
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000513 /* Non-ignored signals revert to SIG_DFL on exec anyway */
514 /*signal(SIGALRM, SIG_DFL);*/
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000515
Denis Vlasenko8c764872006-10-31 18:30:56 +0000516 /* Is this correct? This way user can ctrl-c out of /etc/profile,
517 * potentially creating security breach (tested with bash 3.0).
518 * But without this, bash 3.0 will not enable ctrl-c either.
519 * Maybe bash is buggy?
520 * Need to find out what standards say about /bin/login -
Denis Vlasenko2ec94a72008-11-07 12:59:31 +0000521 * should we leave SIGINT etc enabled or disabled? */
Denis Vlasenko6ae80792006-10-31 17:34:44 +0000522 signal(SIGINT, SIG_DFL);
523
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000524 /* Exec login shell with no additional parameters */
Denys Vlasenkobd74e3d2011-03-06 18:49:40 +0100525 run_shell(pw->pw_shell, 1, NULL, NULL);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000526
Denis Vlasenkoa2f61012007-09-10 13:15:28 +0000527 /* return EXIT_FAILURE; - not reached */
Robert Griebl1fca5582002-06-04 20:45:46 +0000528}