1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
20 #include <flask_util.h>
21 #include <get_sid_list.h>
22 #include <proc_secure.h>
23 #include <fs_secure.h>
26 #ifdef CONFIG_FEATURE_U_W_TMP
28 static void checkutmp(int picky);
29 static void setutmp(const char *name, const char *line);
30 /* Stuff global to this file */
36 #define EMPTY_USERNAME_COUNT 10
37 #define USERNAME_SIZE 32
40 static int check_nologin ( int amroot );
42 #if defined CONFIG_FEATURE_SECURETTY
43 static int check_tty ( const char *tty );
46 static inline int check_tty ( const char *tty ) { return 1; }
50 static int is_my_tty ( const char *tty );
51 static int login_prompt ( char *buf_name );
52 static void motd ( void );
55 static void alarm_handler ( int sig )
57 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
58 exit ( EXIT_SUCCESS );
62 extern int login_main(int argc, char **argv)
67 char username[USERNAME_SIZE];
73 struct passwd *pw, pw_copy;
74 #ifdef CONFIG_WHEEL_GROUP
82 int flask_enabled = is_flask_enabled();
83 security_id_t sid = 0, old_tty_sid, new_tty_sid;
87 amroot = ( getuid ( ) == 0 );
88 signal ( SIGALRM, alarm_handler );
92 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
99 * username must be a separate token
100 * (-f root, *NOT* -froot). --marekm
102 if ( optarg != argv[optind-1] )
105 if ( !amroot ) /* Auth bypass only if real UID is zero */
106 bb_error_msg_and_die ( "-f permission denied" );
108 safe_strncpy(username, optarg, USERNAME_SIZE);
119 if (optind < argc) // user from command line (getty)
120 safe_strncpy(username, argv[optind], USERNAME_SIZE);
122 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
123 return EXIT_FAILURE; /* Must be a terminal */
125 #ifdef CONFIG_FEATURE_U_W_TMP
126 checkutmp ( !amroot );
130 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
131 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
132 else if ( tmp && *tmp == '/' )
133 safe_strncpy ( tty, tmp, sizeof( tty ));
135 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
137 #ifdef CONFIG_FEATURE_U_W_TMP
139 memset ( utent.ut_host, 0, sizeof utent.ut_host );
143 #ifdef CONFIG_FEATURE_U_W_TMP
144 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
146 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
149 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
153 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
159 if(!login_prompt ( username ))
162 if ( !alarmstarted && ( TIMEOUT > 0 )) {
167 if (!( pw = getpwnam ( username ))) {
168 pw_copy.pw_name = "UNKNOWN";
169 pw_copy.pw_passwd = "!";
177 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
185 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
188 /* Don't check the password if password entry is empty (!) */
189 if ( !pw-> pw_passwd[0] )
192 /* authorization takes place here */
193 if ( correct_password ( pw ))
207 while ( difftime ( now, start ) < FAIL_DELAY) {
208 sleep ( FAIL_DELAY );
213 puts("Login incorrect");
215 if ( ++count == 3 ) {
216 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
222 if ( check_nologin ( pw-> pw_uid == 0 ))
225 #ifdef CONFIG_FEATURE_U_W_TMP
226 setutmp ( username, tty );
228 #ifdef CONFIG_SELINUX
233 if (get_default_sid(username, 0, &sid))
235 fprintf(stderr, "Unable to get SID for %s\n", username);
238 if (stat_secure(tty, &st, &old_tty_sid))
240 fprintf(stderr, "stat_secure(%.100s) failed: %.100s\n", tty, strerror(errno));
243 if (security_change_sid (sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
245 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", tty, strerror(errno));
248 if(chsid(tty, new_tty_sid) != 0)
250 fprintf(stderr, "chsid(%.100s, %d) failed: %.100s\n", tty, new_tty_sid, strerror(errno));
259 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
261 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
263 if ( !is_my_tty ( full_tty ))
264 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
266 /* Try these, but don't complain if they fail
267 * (for example when the root fs is read only) */
268 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
269 chmod ( full_tty, 0600 );
271 change_identity ( pw );
275 setup_environment ( tmp, 1, !opt_preserve, pw );
278 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
280 if ( pw-> pw_uid == 0 )
281 syslog ( LOG_INFO, "root login %s\n", fromhost );
282 run_shell ( tmp, 1, 0, 0
283 #ifdef CONFIG_SELINUX
286 ); /* exec the shell finally. */
293 static int login_prompt ( char *buf_name )
299 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
300 print_login_prompt();
302 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
305 if ( !strchr ( buf, '\n' ))
308 for ( sp = buf; isspace ( *sp ); sp++ ) { }
309 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
312 safe_strncpy(buf_name, sp, USERNAME_SIZE);
320 static int check_nologin ( int amroot )
322 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
326 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
327 while (( c = getc ( fp )) != EOF )
328 putchar (( c == '\n' ) ? '\r' : c );
333 puts ( "\r\nSystem closed for routine maintenance.\r" );
338 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
343 #ifdef CONFIG_FEATURE_SECURETTY
345 static int check_tty ( const char *tty )
351 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
352 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
353 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
354 if ( !isspace ( buf[i] ))
358 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
361 if ( strcmp ( buf, tty ) == 0 ) {
369 /* A missing securetty file is not an error. */
375 /* returns 1 if true */
376 static int is_my_tty ( const char *tty )
378 struct stat by_name, by_fd;
380 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
383 if ( by_name. st_rdev != by_fd. st_rdev )
395 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
396 while (( c = getc ( fp )) != EOF )
403 #ifdef CONFIG_FEATURE_U_W_TMP
404 // vv Taken from tinylogin utmp.c vv
407 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
409 "Unable to determine your tty name."
412 * checkutmp - see if utmp file is correct for this process
414 * System V is very picky about the contents of the utmp file
415 * and requires that a slot for the current process exist.
416 * The utmp file is scanned for an entry with the same process
417 * ID. If no entry exists the process exits with a message.
419 * The "picky" flag is for network and other logins that may
420 * use special flags. It allows the pid checks to be overridden.
421 * This means that getty should never invoke login with any
422 * command line flags.
425 static void checkutmp(int picky)
429 pid_t pid = getpid();
433 /* First, try to find a valid utmp entry for this process. */
434 while ((ut = getutent()))
435 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
436 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
439 /* If there is one, just use it, otherwise create a new one. */
452 if (strncmp(line, "/dev/", 5) == 0)
454 memset((void *) &utent, 0, sizeof utent);
455 utent.ut_type = LOGIN_PROCESS;
457 strncpy(utent.ut_line, line, sizeof utent.ut_line);
458 /* XXX - assumes /dev/tty?? */
459 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
460 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
461 time(&utent.ut_time);
466 * setutmp - put a USER_PROCESS entry in the utmp file
468 * setutmp changes the type of the current utmp entry to
469 * USER_PROCESS. the wtmp file will be updated as well.
472 static void setutmp(const char *name, const char *line)
474 utent.ut_type = USER_PROCESS;
475 strncpy(utent.ut_user, name, sizeof utent.ut_user);
476 time(&utent.ut_time);
477 /* other fields already filled in by checkutmp above */
481 if (access(_PATH_WTMP, R_OK|W_OK) == -1) {
482 creat(_PATH_WTMP, 0664);
484 updwtmp(_PATH_WTMP, &utent);
486 #endif /* CONFIG_FEATURE_U_W_TMP */