Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 4 | */ |
| 5 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 7 | #include <utmp.h> |
| 8 | #include <sys/resource.h> |
Bernhard Reutner-Fischer | 92582f2 | 2006-09-13 15:42:47 +0000 | [diff] [blame] | 9 | #include <syslog.h> |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 11 | #if ENABLE_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 12 | #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 Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 15 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 17 | #if ENABLE_PAM |
Denis Vlasenko | c6c2345 | 2007-08-22 18:14:44 +0000 | [diff] [blame] | 18 | /* 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 Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 24 | static const struct pam_conv conv = { |
| 25 | misc_conv, |
| 26 | NULL |
| 27 | }; |
| 28 | #endif |
| 29 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 30 | enum { |
| 31 | TIMEOUT = 60, |
| 32 | EMPTY_USERNAME_COUNT = 10, |
| 33 | USERNAME_SIZE = 32, |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 34 | TTYNAME_SIZE = 32, |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 35 | }; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 37 | static char* short_tty; |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 38 | |
| 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 Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 55 | static void read_or_build_utent(struct utmp *utptr, int picky) |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 56 | { |
| 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 Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 70 | *utptr = *ut; |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 71 | } else { |
| 72 | if (picky) |
| 73 | bb_error_msg_and_die("no utmp entry found"); |
| 74 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 75 | 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 Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 79 | /* This one is only 4 chars wide. Try to fit something |
| 80 | * remotely meaningful by skipping "tty"... */ |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 81 | strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id)); |
| 82 | strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user)); |
| 83 | utptr->ut_time = time(NULL); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 84 | } |
| 85 | if (!picky) /* root login */ |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 86 | memset(utptr->ut_host, 0, sizeof(utptr->ut_host)); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 87 | } |
| 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 Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 95 | static void write_utent(struct utmp *utptr, const char *username) |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 97 | utptr->ut_type = USER_PROCESS; |
| 98 | strncpy(utptr->ut_user, username, sizeof(utptr->ut_user)); |
| 99 | utptr->ut_time = time(NULL); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 100 | /* other fields already filled in by read_or_build_utent above */ |
| 101 | setutent(); |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 102 | pututline(utptr); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 103 | 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 Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 108 | updwtmp(bb_path_wtmp_file, utptr); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 109 | #endif |
| 110 | } |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 111 | #else /* !ENABLE_FEATURE_UTMP */ |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 112 | #define read_or_build_utent(utptr, picky) ((void)0) |
| 113 | #define write_utent(utptr, username) ((void)0) |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 114 | #endif /* !ENABLE_FEATURE_UTMP */ |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 116 | #if ENABLE_FEATURE_NOLOGIN |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 117 | static void die_if_nologin_and_non_root(int amroot) |
| 118 | { |
| 119 | FILE *fp; |
| 120 | int c; |
| 121 | |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 122 | if (access("/etc/nologin", F_OK)) |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 123 | return; |
| 124 | |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 125 | fp = fopen("/etc/nologin", "r"); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 126 | if (fp) { |
| 127 | while ((c = getc(fp)) != EOF) |
| 128 | putchar((c=='\n') ? '\r' : c); |
| 129 | fflush(stdout); |
| 130 | fclose(fp); |
| 131 | } else |
| 132 | puts("\r\nSystem closed for routine maintenance\r"); |
| 133 | if (!amroot) |
| 134 | exit(1); |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 135 | puts("\r\n[Disconnect bypassed -- root login allowed]\r"); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 136 | } |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 137 | #else |
| 138 | static ALWAYS_INLINE void die_if_nologin_and_non_root(int amroot) {} |
| 139 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 141 | #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 142 | static int check_securetty(void) |
| 143 | { |
| 144 | FILE *fp; |
| 145 | int i; |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 146 | char buf[256]; |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 148 | fp = fopen("/etc/securetty", "r"); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 149 | if (!fp) { |
| 150 | /* A missing securetty file is not an error. */ |
| 151 | return 1; |
| 152 | } |
| 153 | while (fgets(buf, sizeof(buf)-1, fp)) { |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 154 | for (i = strlen(buf)-1; i >= 0; --i) { |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 155 | if (!isspace(buf[i])) |
| 156 | break; |
| 157 | } |
| 158 | buf[++i] = '\0'; |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 159 | if (!buf[0] || (buf[0] == '#')) |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 160 | continue; |
| 161 | if (strcmp(buf, short_tty) == 0) { |
| 162 | fclose(fp); |
| 163 | return 1; |
| 164 | } |
| 165 | } |
| 166 | fclose(fp); |
| 167 | return 0; |
| 168 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 169 | #else |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 170 | static ALWAYS_INLINE int check_securetty(void) { return 1; } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 173 | static void get_username_or_die(char *buf, int size_buf) |
| 174 | { |
| 175 | int c, cntdown; |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 177 | cntdown = EMPTY_USERNAME_COUNT; |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 178 | prompt: |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 179 | print_login_prompt(); |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 180 | /* skip whitespace */ |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 181 | do { |
| 182 | c = getchar(); |
| 183 | if (c == EOF) exit(1); |
| 184 | if (c == '\n') { |
| 185 | if (!--cntdown) exit(1); |
| 186 | goto prompt; |
| 187 | } |
| 188 | } while (isspace(c)); |
| 189 | |
| 190 | *buf++ = c; |
| 191 | if (!fgets(buf, size_buf-2, stdin)) |
| 192 | exit(1); |
| 193 | if (!strchr(buf, '\n')) |
| 194 | exit(1); |
| 195 | while (isgraph(*buf)) buf++; |
| 196 | *buf = '\0'; |
| 197 | } |
| 198 | |
| 199 | static void motd(void) |
| 200 | { |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 201 | int fd; |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 203 | fd = open(bb_path_motd_file, O_RDONLY); |
| 204 | if (fd) { |
| 205 | fflush(stdout); |
| 206 | bb_copyfd_eof(fd, STDOUT_FILENO); |
| 207 | close(fd); |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 211 | static void alarm_handler(int sig ATTRIBUTE_UNUSED) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 212 | { |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 213 | /* This is the escape hatch! Poor serial line users and the like |
| 214 | * arrive here when their connection is broken. |
| 215 | * We don't want to block here */ |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 216 | ndelay_on(1); |
| 217 | ndelay_on(2); |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 218 | printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 219 | exit(EXIT_SUCCESS); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 222 | int login_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 223 | int login_main(int argc, char **argv) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 224 | { |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 225 | enum { |
| 226 | LOGIN_OPT_f = (1<<0), |
| 227 | LOGIN_OPT_h = (1<<1), |
| 228 | LOGIN_OPT_p = (1<<2), |
| 229 | }; |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 230 | char *fromhost; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 231 | char username[USERNAME_SIZE]; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 232 | const char *tmp; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 233 | int amroot; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 234 | unsigned opt; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 235 | int count = 0; |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 236 | struct passwd *pw; |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 237 | char *opt_host = NULL; |
| 238 | char *opt_user = NULL; |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 239 | char full_tty[TTYNAME_SIZE]; |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 240 | USE_SELINUX(security_context_t user_sid = NULL;) |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 241 | USE_FEATURE_UTMP(struct utmp utent;) |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 242 | USE_PAM(pam_handle_t *pamh;) |
| 243 | USE_PAM(int pamret;) |
| 244 | USE_PAM(const char *failed_msg;) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 246 | short_tty = full_tty; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 247 | username[0] = '\0'; |
| 248 | amroot = (getuid() == 0); |
| 249 | signal(SIGALRM, alarm_handler); |
| 250 | alarm(TIMEOUT); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 251 | |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 252 | /* Mandatory paranoia for suid applet: |
| 253 | * ensure that fd# 0,1,2 are opened (at least to /dev/null) |
| 254 | * and any extra open fd's are closed. |
| 255 | * (The name of the function is misleading. Not daemonizing here.) */ |
| 256 | bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL); |
| 257 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 258 | opt = getopt32(argv, "f:h:p", &opt_user, &opt_host); |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 259 | if (opt & LOGIN_OPT_f) { |
| 260 | if (!amroot) |
| 261 | bb_error_msg_and_die("-f is for root only"); |
Denis Vlasenko | 22f6dcb | 2006-09-26 16:31:01 +0000 | [diff] [blame] | 262 | safe_strncpy(username, opt_user, sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 263 | } |
Denis Vlasenko | 22f6dcb | 2006-09-26 16:31:01 +0000 | [diff] [blame] | 264 | if (optind < argc) /* user from command line (getty) */ |
| 265 | safe_strncpy(username, argv[optind], sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 267 | /* Let's find out and memorize our tty */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 268 | if (!isatty(0) || !isatty(1) || !isatty(2)) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 269 | return EXIT_FAILURE; /* Must be a terminal */ |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 270 | safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty)); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 271 | tmp = ttyname(0); |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 272 | if (tmp) { |
| 273 | safe_strncpy(full_tty, tmp, sizeof(full_tty)); |
| 274 | if (strncmp(full_tty, "/dev/", 5) == 0) |
| 275 | short_tty = full_tty + 5; |
| 276 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 278 | read_or_build_utent(&utent, !amroot); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 280 | if (opt_host) { |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 281 | USE_FEATURE_UTMP( |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 282 | safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 283 | ) |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 284 | fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host); |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 285 | } else |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 286 | fromhost = xasprintf(" on '%s'", short_tty); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 288 | /* Was breaking "login <username>" from shell command line: */ |
| 289 | /*bb_setpgrp();*/ |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 290 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 291 | openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 293 | while (1) { |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 294 | if (!username[0]) |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 295 | get_username_or_die(username, sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 297 | #if ENABLE_PAM |
| 298 | pamret = pam_start("login", username, &conv, &pamh); |
| 299 | if (pamret != PAM_SUCCESS) { |
| 300 | failed_msg = "pam_start"; |
| 301 | goto pam_auth_failed; |
| 302 | } |
| 303 | /* set TTY (so things like securetty work) */ |
| 304 | pamret = pam_set_item(pamh, PAM_TTY, short_tty); |
| 305 | if (pamret != PAM_SUCCESS) { |
| 306 | failed_msg = "pam_set_item(TTY)"; |
| 307 | goto pam_auth_failed; |
| 308 | } |
| 309 | pamret = pam_authenticate(pamh, 0); |
Denis Vlasenko | 82f3b16 | 2007-09-03 11:56:27 +0000 | [diff] [blame] | 310 | if (pamret != PAM_SUCCESS) { |
| 311 | failed_msg = "pam_authenticate"; |
| 312 | goto pam_auth_failed; |
| 313 | /* TODO: or just "goto auth_failed" |
| 314 | * since user seems to enter wrong password |
| 315 | * (in this case pamret == 7) |
| 316 | */ |
| 317 | } |
| 318 | /* check that the account is healthy */ |
| 319 | pamret = pam_acct_mgmt(pamh, 0); |
| 320 | if (pamret != PAM_SUCCESS) { |
| 321 | failed_msg = "account setup"; |
| 322 | goto pam_auth_failed; |
| 323 | } |
| 324 | /* read user back */ |
| 325 | { |
| 326 | const char *pamuser; |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 327 | /* gcc: "dereferencing type-punned pointer breaks aliasing rules..." |
Denis Vlasenko | 82f3b16 | 2007-09-03 11:56:27 +0000 | [diff] [blame] | 328 | * thus we cast to (void*) */ |
| 329 | if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) { |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 330 | failed_msg = "pam_get_item(USER)"; |
| 331 | goto pam_auth_failed; |
| 332 | } |
| 333 | safe_strncpy(username, pamuser, sizeof(username)); |
| 334 | } |
| 335 | /* If we get here, the user was authenticated, and is |
| 336 | * granted access. */ |
| 337 | pw = getpwnam(username); |
| 338 | if (pw) |
| 339 | break; |
| 340 | goto auth_failed; |
| 341 | pam_auth_failed: |
Denis Vlasenko | 82f3b16 | 2007-09-03 11:56:27 +0000 | [diff] [blame] | 342 | bb_error_msg("%s failed: %s (%d)", failed_msg, pam_strerror(pamh, pamret), pamret); |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 343 | safe_strncpy(username, "UNKNOWN", sizeof(username)); |
| 344 | #else /* not PAM */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 345 | pw = getpwnam(username); |
| 346 | if (!pw) { |
Denis Vlasenko | 65e14b4 | 2007-06-08 15:27:06 +0000 | [diff] [blame] | 347 | strcpy(username, "UNKNOWN"); |
| 348 | goto fake_it; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 351 | if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') |
| 352 | goto auth_failed; |
| 353 | |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 354 | if (opt & LOGIN_OPT_f) |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 355 | break; /* -f USER: success without asking passwd */ |
| 356 | |
| 357 | if (pw->pw_uid == 0 && !check_securetty()) |
| 358 | goto auth_failed; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 359 | |
| 360 | /* Don't check the password if password entry is empty (!) */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 361 | if (!pw->pw_passwd[0]) |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 362 | break; |
Denis Vlasenko | 65e14b4 | 2007-06-08 15:27:06 +0000 | [diff] [blame] | 363 | fake_it: |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 364 | /* authorization takes place here */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 365 | if (correct_password(pw)) |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 366 | break; |
Denis Vlasenko | d6e81c7 | 2007-08-21 10:58:18 +0000 | [diff] [blame] | 367 | #endif /* ENABLE_PAM */ |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 368 | auth_failed: |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 369 | opt &= ~LOGIN_OPT_f; |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 370 | bb_do_delay(FAIL_DELAY); |
Denis Vlasenko | 82f3b16 | 2007-09-03 11:56:27 +0000 | [diff] [blame] | 371 | /* TODO: doesn't sound like correct English phrase to me */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 372 | puts("Login incorrect"); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 373 | if (++count == 3) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 374 | syslog(LOG_WARNING, "invalid password for '%s'%s", |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 375 | username, fromhost); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 376 | return EXIT_FAILURE; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 377 | } |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 378 | username[0] = '\0'; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 379 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 381 | alarm(0); |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 382 | die_if_nologin_and_non_root(pw->pw_uid == 0); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 383 | |
Denis Vlasenko | c0415a9 | 2007-03-24 17:04:07 +0000 | [diff] [blame] | 384 | write_utent(&utent, username); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 386 | #if ENABLE_SELINUX |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 387 | if (is_selinux_enabled()) { |
Rob Landley | d1f8c1c | 2006-03-27 23:04:42 +0000 | [diff] [blame] | 388 | security_context_t old_tty_sid, new_tty_sid; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 390 | if (get_default_context(username, NULL, &user_sid)) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 391 | bb_error_msg_and_die("cannot get SID for %s", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 392 | username); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 393 | } |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 394 | if (getfilecon(full_tty, &old_tty_sid) < 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 395 | bb_perror_msg_and_die("getfilecon(%s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 396 | full_tty); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 397 | } |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 398 | if (security_compute_relabel(user_sid, old_tty_sid, |
| 399 | SECCLASS_CHR_FILE, &new_tty_sid) != 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 400 | bb_perror_msg_and_die("security_change_sid(%s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 401 | full_tty); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 402 | } |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 403 | if (setfilecon(full_tty, new_tty_sid) != 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 404 | bb_perror_msg_and_die("chsid(%s, %s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 405 | full_tty, new_tty_sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 406 | } |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 407 | } |
| 408 | #endif |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 409 | /* Try these, but don't complain if they fail. |
| 410 | * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */ |
| 411 | fchown(0, pw->pw_uid, pw->pw_gid); |
| 412 | fchmod(0, 0600); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | 2e50291 | 2006-09-08 17:22:45 +0000 | [diff] [blame] | 414 | if (ENABLE_LOGIN_SCRIPTS) { |
Denis Vlasenko | 53091ec | 2007-03-26 13:35:09 +0000 | [diff] [blame] | 415 | char *t_argv[2]; |
| 416 | |
| 417 | t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT"); |
| 418 | if (t_argv[0]) { |
| 419 | t_argv[1] = NULL; |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 420 | xsetenv("LOGIN_TTY", full_tty); |
| 421 | xsetenv("LOGIN_USER", pw->pw_name); |
| 422 | xsetenv("LOGIN_UID", utoa(pw->pw_uid)); |
| 423 | xsetenv("LOGIN_GID", utoa(pw->pw_gid)); |
| 424 | xsetenv("LOGIN_SHELL", pw->pw_shell); |
Denis Vlasenko | 3211adc | 2007-04-12 20:33:01 +0000 | [diff] [blame] | 425 | xspawn(t_argv); /* NOMMU-friendly */ |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 426 | /* All variables are unset by setup_environment */ |
Denis Vlasenko | 53091ec | 2007-03-26 13:35:09 +0000 | [diff] [blame] | 427 | wait(NULL); |
Denis Vlasenko | 2e50291 | 2006-09-08 17:22:45 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 431 | change_identity(pw); |
| 432 | tmp = pw->pw_shell; |
| 433 | if (!tmp || !*tmp) |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 434 | tmp = DEFAULT_SHELL; |
Denis Vlasenko | a2f6101 | 2007-09-10 13:15:28 +0000 | [diff] [blame^] | 435 | /* setup_environment params: shell, loginshell, changeenv, pw */ |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 436 | setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw); |
Denis Vlasenko | a2f6101 | 2007-09-10 13:15:28 +0000 | [diff] [blame^] | 437 | /* FIXME: login shell = 1 -> 3rd parameter is ignored! */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 438 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 439 | motd(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 440 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 441 | if (pw->pw_uid == 0) |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 442 | syslog(LOG_INFO, "root login%s", fromhost); |
Denis Vlasenko | f312e32 | 2007-06-12 22:04:57 +0000 | [diff] [blame] | 443 | #if ENABLE_SELINUX |
Rob Landley | d1f8c1c | 2006-03-27 23:04:42 +0000 | [diff] [blame] | 444 | /* well, a simple setexeccon() here would do the job as well, |
| 445 | * but let's play the game for now */ |
| 446 | set_current_security_context(user_sid); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 447 | #endif |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 448 | |
| 449 | // util-linux login also does: |
| 450 | // /* start new session */ |
| 451 | // setsid(); |
| 452 | // /* TIOCSCTTY: steal tty from other process group */ |
| 453 | // if (ioctl(0, TIOCSCTTY, 1)) error_msg... |
Denis Vlasenko | 0de3c55 | 2007-04-12 12:31:02 +0000 | [diff] [blame] | 454 | // BBox login used to do this (see above): |
| 455 | // bb_setpgrp(); |
| 456 | // If this stuff is really needed, add it and explain why! |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | 8c76487 | 2006-10-31 18:30:56 +0000 | [diff] [blame] | 458 | /* set signals to defaults */ |
| 459 | signal(SIGALRM, SIG_DFL); |
| 460 | /* Is this correct? This way user can ctrl-c out of /etc/profile, |
| 461 | * potentially creating security breach (tested with bash 3.0). |
| 462 | * But without this, bash 3.0 will not enable ctrl-c either. |
| 463 | * Maybe bash is buggy? |
| 464 | * Need to find out what standards say about /bin/login - |
| 465 | * should it leave SIGINT etc enabled or disabled? */ |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 466 | signal(SIGINT, SIG_DFL); |
| 467 | |
Denis Vlasenko | a2f6101 | 2007-09-10 13:15:28 +0000 | [diff] [blame^] | 468 | /* Exec login shell with no additional parameters */ |
| 469 | run_shell(tmp, 1, NULL, NULL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 470 | |
Denis Vlasenko | a2f6101 | 2007-09-10 13:15:28 +0000 | [diff] [blame^] | 471 | /* return EXIT_FAILURE; - not reached */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 472 | } |