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 | |
Bernhard Reutner-Fischer | 92582f2 | 2006-09-13 15:42:47 +0000 | [diff] [blame] | 6 | #include "busybox.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 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 11 | #ifdef CONFIG_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 */ |
| 15 | #include <errno.h> |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 16 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 18 | enum { |
| 19 | TIMEOUT = 60, |
| 20 | EMPTY_USERNAME_COUNT = 10, |
| 21 | USERNAME_SIZE = 32, |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 22 | TTYNAME_SIZE = 32, |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 23 | }; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 25 | static char full_tty[TTYNAME_SIZE]; |
| 26 | static char* short_tty = full_tty; |
| 27 | |
| 28 | #if ENABLE_FEATURE_UTMP |
| 29 | /* vv Taken from tinylogin utmp.c vv */ |
| 30 | /* |
| 31 | * read_or_build_utent - see if utmp file is correct for this process |
| 32 | * |
| 33 | * System V is very picky about the contents of the utmp file |
| 34 | * and requires that a slot for the current process exist. |
| 35 | * The utmp file is scanned for an entry with the same process |
| 36 | * ID. If no entry exists the process exits with a message. |
| 37 | * |
| 38 | * The "picky" flag is for network and other logins that may |
| 39 | * use special flags. It allows the pid checks to be overridden. |
| 40 | * This means that getty should never invoke login with any |
| 41 | * command line flags. |
| 42 | */ |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 44 | static struct utmp utent; |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 46 | static void read_or_build_utent(int picky) |
| 47 | { |
| 48 | struct utmp *ut; |
| 49 | pid_t pid = getpid(); |
| 50 | |
| 51 | setutent(); |
| 52 | |
| 53 | /* First, try to find a valid utmp entry for this process. */ |
| 54 | while ((ut = getutent())) |
| 55 | if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] && |
| 56 | (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)) |
| 57 | break; |
| 58 | |
| 59 | /* If there is one, just use it, otherwise create a new one. */ |
| 60 | if (ut) { |
| 61 | utent = *ut; |
| 62 | } else { |
| 63 | if (picky) |
| 64 | bb_error_msg_and_die("no utmp entry found"); |
| 65 | |
| 66 | memset(&utent, 0, sizeof(utent)); |
| 67 | utent.ut_type = LOGIN_PROCESS; |
| 68 | utent.ut_pid = pid; |
| 69 | strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line)); |
| 70 | /* This one is only 4 chars wide. Try to fit something |
| 71 | * remotely meaningful by skipping "tty"... */ |
| 72 | strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id)); |
| 73 | strncpy(utent.ut_user, "LOGIN", sizeof(utent.ut_user)); |
| 74 | utent.ut_time = time(NULL); |
| 75 | } |
| 76 | if (!picky) /* root login */ |
| 77 | memset(utent.ut_host, 0, sizeof(utent.ut_host)); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * write_utent - put a USER_PROCESS entry in the utmp file |
| 82 | * |
| 83 | * write_utent changes the type of the current utmp entry to |
| 84 | * USER_PROCESS. the wtmp file will be updated as well. |
| 85 | */ |
| 86 | static void write_utent(const char *username) |
| 87 | { |
| 88 | utent.ut_type = USER_PROCESS; |
| 89 | strncpy(utent.ut_user, username, sizeof(utent.ut_user)); |
| 90 | utent.ut_time = time(NULL); |
| 91 | /* other fields already filled in by read_or_build_utent above */ |
| 92 | setutent(); |
| 93 | pututline(&utent); |
| 94 | endutent(); |
| 95 | #if ENABLE_FEATURE_WTMP |
| 96 | if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { |
| 97 | close(creat(bb_path_wtmp_file, 0664)); |
| 98 | } |
| 99 | updwtmp(bb_path_wtmp_file, &utent); |
| 100 | #endif |
| 101 | } |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 102 | #else /* !ENABLE_FEATURE_UTMP */ |
| 103 | static inline void read_or_build_utent(int ATTRIBUTE_UNUSED picky) {} |
| 104 | static inline void write_utent(const char ATTRIBUTE_UNUSED *username) {} |
| 105 | #endif /* !ENABLE_FEATURE_UTMP */ |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 106 | |
| 107 | static void die_if_nologin_and_non_root(int amroot) |
| 108 | { |
| 109 | FILE *fp; |
| 110 | int c; |
| 111 | |
| 112 | if (access(bb_path_nologin_file, F_OK)) |
| 113 | return; |
| 114 | |
| 115 | fp = fopen(bb_path_nologin_file, "r"); |
| 116 | if (fp) { |
| 117 | while ((c = getc(fp)) != EOF) |
| 118 | putchar((c=='\n') ? '\r' : c); |
| 119 | fflush(stdout); |
| 120 | fclose(fp); |
| 121 | } else |
| 122 | puts("\r\nSystem closed for routine maintenance\r"); |
| 123 | if (!amroot) |
| 124 | exit(1); |
| 125 | puts("\r\n[Disconnect bypassed -- root login allowed.]\r"); |
| 126 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 127 | |
Bernhard Reutner-Fischer | 92582f2 | 2006-09-13 15:42:47 +0000 | [diff] [blame] | 128 | #if ENABLE_FEATURE_SECURETTY |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 129 | static int check_securetty(void) |
| 130 | { |
| 131 | FILE *fp; |
| 132 | int i; |
| 133 | char buf[BUFSIZ]; |
| 134 | |
| 135 | fp = fopen(bb_path_securetty_file, "r"); |
| 136 | if (!fp) { |
| 137 | /* A missing securetty file is not an error. */ |
| 138 | return 1; |
| 139 | } |
| 140 | while (fgets(buf, sizeof(buf)-1, fp)) { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 141 | for (i = strlen(buf)-1; i>=0; --i) { |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 142 | if (!isspace(buf[i])) |
| 143 | break; |
| 144 | } |
| 145 | buf[++i] = '\0'; |
| 146 | if ((buf[0]=='\0') || (buf[0]=='#')) |
| 147 | continue; |
| 148 | if (strcmp(buf, short_tty) == 0) { |
| 149 | fclose(fp); |
| 150 | return 1; |
| 151 | } |
| 152 | } |
| 153 | fclose(fp); |
| 154 | return 0; |
| 155 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 156 | #else |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 157 | static inline int check_securetty(void) { return 1; } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 160 | static void get_username_or_die(char *buf, int size_buf) |
| 161 | { |
| 162 | int c, cntdown; |
| 163 | cntdown = EMPTY_USERNAME_COUNT; |
| 164 | prompt: |
| 165 | /* skip whitespace */ |
| 166 | print_login_prompt(); |
| 167 | do { |
| 168 | c = getchar(); |
| 169 | if (c == EOF) exit(1); |
| 170 | if (c == '\n') { |
| 171 | if (!--cntdown) exit(1); |
| 172 | goto prompt; |
| 173 | } |
| 174 | } while (isspace(c)); |
| 175 | |
| 176 | *buf++ = c; |
| 177 | if (!fgets(buf, size_buf-2, stdin)) |
| 178 | exit(1); |
| 179 | if (!strchr(buf, '\n')) |
| 180 | exit(1); |
| 181 | while (isgraph(*buf)) buf++; |
| 182 | *buf = '\0'; |
| 183 | } |
| 184 | |
| 185 | static void motd(void) |
| 186 | { |
| 187 | FILE *fp; |
| 188 | int c; |
| 189 | |
| 190 | fp = fopen(bb_path_motd_file, "r"); |
| 191 | if (fp) { |
| 192 | while ((c = getc(fp)) != EOF) |
| 193 | putchar(c); |
| 194 | fclose(fp); |
| 195 | } |
| 196 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 197 | |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 198 | static void nonblock(int fd) |
| 199 | { |
| 200 | fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL)); |
| 201 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 203 | static void alarm_handler(int sig ATTRIBUTE_UNUSED) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 204 | { |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 205 | /* This is the escape hatch! Poor serial line users and the like |
| 206 | * arrive here when their connection is broken. |
| 207 | * We don't want to block here */ |
| 208 | nonblock(1); |
| 209 | nonblock(2); |
| 210 | bb_info_msg("\r\nLogin timed out after %d seconds\r", TIMEOUT); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 211 | exit(EXIT_SUCCESS); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 214 | int login_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 215 | int login_main(int argc, char **argv) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 216 | { |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 217 | enum { |
| 218 | LOGIN_OPT_f = (1<<0), |
| 219 | LOGIN_OPT_h = (1<<1), |
| 220 | LOGIN_OPT_p = (1<<2), |
| 221 | }; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 222 | char fromhost[512]; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 223 | char username[USERNAME_SIZE]; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 224 | const char *tmp; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 225 | int amroot; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 226 | unsigned opt; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 227 | int count = 0; |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 228 | struct passwd *pw; |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 229 | char *opt_host = NULL; |
| 230 | char *opt_user = NULL; |
| 231 | USE_SELINUX(security_context_t user_sid = NULL;) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 232 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 233 | username[0] = '\0'; |
| 234 | amroot = (getuid() == 0); |
| 235 | signal(SIGALRM, alarm_handler); |
| 236 | alarm(TIMEOUT); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 237 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 238 | opt = getopt32(argc, argv, "f:h:p", &opt_user, &opt_host); |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 239 | if (opt & LOGIN_OPT_f) { |
| 240 | if (!amroot) |
| 241 | bb_error_msg_and_die("-f is for root only"); |
Denis Vlasenko | 22f6dcb | 2006-09-26 16:31:01 +0000 | [diff] [blame] | 242 | safe_strncpy(username, opt_user, sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 243 | } |
Denis Vlasenko | 22f6dcb | 2006-09-26 16:31:01 +0000 | [diff] [blame] | 244 | if (optind < argc) /* user from command line (getty) */ |
| 245 | safe_strncpy(username, argv[optind], sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 247 | /* Let's find out and memorize our tty */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 248 | if (!isatty(0) || !isatty(1) || !isatty(2)) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 249 | return EXIT_FAILURE; /* Must be a terminal */ |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 250 | safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty)); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 251 | tmp = ttyname(0); |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 252 | if (tmp) { |
| 253 | safe_strncpy(full_tty, tmp, sizeof(full_tty)); |
| 254 | if (strncmp(full_tty, "/dev/", 5) == 0) |
| 255 | short_tty = full_tty + 5; |
| 256 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 258 | read_or_build_utent(!amroot); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 259 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 260 | if (opt_host) { |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 261 | USE_FEATURE_UTMP( |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 262 | safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 263 | ) |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 264 | snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s' from " |
| 265 | "'%.200s'", short_tty, opt_host); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 266 | } |
| 267 | else |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 268 | snprintf(fromhost, sizeof(fromhost)-1, " on '%.100s'", short_tty); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 269 | |
Bernhard Reutner-Fischer | b180e5a | 2006-05-26 14:24:56 +0000 | [diff] [blame] | 270 | bb_setpgrp; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 272 | openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 274 | while (1) { |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 275 | if (!username[0]) |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 276 | get_username_or_die(username, sizeof(username)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 278 | pw = getpwnam(username); |
| 279 | if (!pw) { |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 280 | safe_strncpy(username, "UNKNOWN", sizeof(username)); |
| 281 | goto auth_failed; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 284 | if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') |
| 285 | goto auth_failed; |
| 286 | |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 287 | if (opt & LOGIN_OPT_f) |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 288 | break; /* -f USER: success without asking passwd */ |
| 289 | |
| 290 | if (pw->pw_uid == 0 && !check_securetty()) |
| 291 | goto auth_failed; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 292 | |
| 293 | /* Don't check the password if password entry is empty (!) */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 294 | if (!pw->pw_passwd[0]) |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 295 | break; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 296 | |
| 297 | /* authorization takes place here */ |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 298 | if (correct_password(pw)) |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 299 | break; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 300 | |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 301 | auth_failed: |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 302 | opt &= ~LOGIN_OPT_f; |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 303 | bb_do_delay(FAIL_DELAY); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 304 | puts("Login incorrect"); |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 305 | if (++count == 3) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 306 | syslog(LOG_WARNING, "invalid password for '%s'%s", |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 307 | username, fromhost); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 308 | return EXIT_FAILURE; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 309 | } |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 310 | username[0] = '\0'; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 311 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 312 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 313 | alarm(0); |
Denis Vlasenko | 9a9edf2 | 2006-09-08 17:29:53 +0000 | [diff] [blame] | 314 | die_if_nologin_and_non_root(pw->pw_uid == 0); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 315 | |
Denis Vlasenko | 7ccf5cc | 2006-09-14 17:03:18 +0000 | [diff] [blame] | 316 | write_utent(username); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 317 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 318 | #ifdef CONFIG_SELINUX |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 319 | if (is_selinux_enabled()) { |
Rob Landley | d1f8c1c | 2006-03-27 23:04:42 +0000 | [diff] [blame] | 320 | security_context_t old_tty_sid, new_tty_sid; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 321 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 322 | if (get_default_context(username, NULL, &user_sid)) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 323 | bb_error_msg_and_die("cannot get SID for %s", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 324 | username); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 326 | if (getfilecon(full_tty, &old_tty_sid) < 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 327 | bb_perror_msg_and_die("getfilecon(%s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 328 | full_tty); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 329 | } |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 330 | if (security_compute_relabel(user_sid, old_tty_sid, |
| 331 | SECCLASS_CHR_FILE, &new_tty_sid) != 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 332 | bb_perror_msg_and_die("security_change_sid(%s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 333 | full_tty); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 334 | } |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 335 | if (setfilecon(full_tty, new_tty_sid) != 0) { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 336 | bb_perror_msg_and_die("chsid(%s, %s) failed", |
Denis Vlasenko | 6bbd174 | 2006-09-08 17:26:20 +0000 | [diff] [blame] | 337 | full_tty, new_tty_sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 338 | } |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 339 | } |
| 340 | #endif |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 341 | /* Try these, but don't complain if they fail. |
| 342 | * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */ |
| 343 | fchown(0, pw->pw_uid, pw->pw_gid); |
| 344 | fchmod(0, 0600); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 346 | /* TODO: be nommu-friendly, use spawn? */ |
Denis Vlasenko | 2e50291 | 2006-09-08 17:22:45 +0000 | [diff] [blame] | 347 | if (ENABLE_LOGIN_SCRIPTS) { |
| 348 | char *script = getenv("LOGIN_PRE_SUID_SCRIPT"); |
| 349 | if (script) { |
| 350 | char *t_argv[2] = { script, NULL }; |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 351 | switch (fork()) { |
Denis Vlasenko | 2e50291 | 2006-09-08 17:22:45 +0000 | [diff] [blame] | 352 | case -1: break; |
| 353 | case 0: /* child */ |
| 354 | xchdir("/"); |
| 355 | setenv("LOGIN_TTY", full_tty, 1); |
| 356 | setenv("LOGIN_USER", pw->pw_name, 1); |
| 357 | setenv("LOGIN_UID", utoa(pw->pw_uid), 1); |
| 358 | setenv("LOGIN_GID", utoa(pw->pw_gid), 1); |
| 359 | setenv("LOGIN_SHELL", pw->pw_shell, 1); |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame^] | 360 | BB_EXECVP(script, t_argv); |
Denis Vlasenko | 2e50291 | 2006-09-08 17:22:45 +0000 | [diff] [blame] | 361 | exit(1); |
| 362 | default: /* parent */ |
| 363 | wait(NULL); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 368 | change_identity(pw); |
| 369 | tmp = pw->pw_shell; |
| 370 | if (!tmp || !*tmp) |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 371 | tmp = DEFAULT_SHELL; |
Denis Vlasenko | 66fabdb | 2006-09-17 14:45:09 +0000 | [diff] [blame] | 372 | setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 373 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 374 | motd(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | 942e429 | 2006-09-08 17:25:04 +0000 | [diff] [blame] | 376 | if (pw->pw_uid == 0) |
Denis Vlasenko | 2f50aa4 | 2006-09-08 17:56:52 +0000 | [diff] [blame] | 377 | syslog(LOG_INFO, "root login%s", fromhost); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 378 | #ifdef CONFIG_SELINUX |
Rob Landley | d1f8c1c | 2006-03-27 23:04:42 +0000 | [diff] [blame] | 379 | /* well, a simple setexeccon() here would do the job as well, |
| 380 | * but let's play the game for now */ |
| 381 | set_current_security_context(user_sid); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 382 | #endif |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 383 | |
| 384 | // util-linux login also does: |
| 385 | // /* start new session */ |
| 386 | // setsid(); |
| 387 | // /* TIOCSCTTY: steal tty from other process group */ |
| 388 | // if (ioctl(0, TIOCSCTTY, 1)) error_msg... |
| 389 | |
Denis Vlasenko | 8c76487 | 2006-10-31 18:30:56 +0000 | [diff] [blame] | 390 | /* set signals to defaults */ |
| 391 | signal(SIGALRM, SIG_DFL); |
| 392 | /* Is this correct? This way user can ctrl-c out of /etc/profile, |
| 393 | * potentially creating security breach (tested with bash 3.0). |
| 394 | * But without this, bash 3.0 will not enable ctrl-c either. |
| 395 | * Maybe bash is buggy? |
| 396 | * Need to find out what standards say about /bin/login - |
| 397 | * should it leave SIGINT etc enabled or disabled? */ |
Denis Vlasenko | 6ae8079 | 2006-10-31 17:34:44 +0000 | [diff] [blame] | 398 | signal(SIGINT, SIG_DFL); |
| 399 | |
| 400 | run_shell(tmp, 1, 0, 0); /* exec the shell finally */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 401 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 402 | return EXIT_FAILURE; |
| 403 | } |