- char tty[BUFSIZ];
- char full_tty[200];
- char fromhost[512];
- char username[USERNAME_SIZE];
- const char *tmp;
- int amroot;
- int flag;
- int failed;
- int count=0;
- struct passwd *pw, pw_copy;
-#ifdef CONFIG_WHEEL_GROUP
- struct group *grp;
-#endif
- int opt_preserve = 0;
- int opt_fflag = 0;
- char *opt_host = 0;
- int alarmstarted = 0;
-#ifdef CONFIG_SELINUX
- int flask_enabled = is_flask_enabled();
- security_id_t sid = 0, old_tty_sid, new_tty_sid;
-#endif
-
- username[0]=0;
- amroot = ( getuid ( ) == 0 );
- signal ( SIGALRM, alarm_handler );
- alarm ( TIMEOUT );
- alarmstarted = 1;
-
- while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
- switch ( flag ) {
- case 'p':
- opt_preserve = 1;
- break;
- case 'f':
- /*
- * username must be a separate token
- * (-f root, *NOT* -froot). --marekm
- */
- if ( optarg != argv[optind-1] )
- bb_show_usage( );
-
- if ( !amroot ) /* Auth bypass only if real UID is zero */
- bb_error_msg_and_die ( "-f permission denied" );
-
- safe_strncpy(username, optarg, USERNAME_SIZE);
- opt_fflag = 1;
- break;
- case 'h':
- opt_host = optarg;
- break;
- default:
- bb_show_usage( );
- }
- }
-
- if (optind < argc) // user from command line (getty)
- safe_strncpy(username, argv[optind], USERNAME_SIZE);
-
- if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
- return EXIT_FAILURE; /* Must be a terminal */
-
-#ifdef CONFIG_FEATURE_U_W_TMP
- checkutmp ( !amroot );
-#endif
-
- tmp = ttyname ( 0 );
- if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
- safe_strncpy ( tty, tmp + 5, sizeof( tty ));
- else if ( tmp && *tmp == '/' )
- safe_strncpy ( tty, tmp, sizeof( tty ));
- else
- safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
-
-#ifdef CONFIG_FEATURE_U_W_TMP
- if ( amroot )
- memset ( utent.ut_host, 0, sizeof utent.ut_host );
-#endif
-
- if ( opt_host ) {
-#ifdef CONFIG_FEATURE_U_W_TMP
- safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
-#endif
- snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
- }
- else
- snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
-
- setpgrp();
-
- openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
-
- while ( 1 ) {
- failed = 0;
-
- if ( !username[0] )
- if(!login_prompt ( username ))
- return EXIT_FAILURE;
-
- if ( !alarmstarted && ( TIMEOUT > 0 )) {
- alarm ( TIMEOUT );
- alarmstarted = 1;
- }
-
- if (!( pw = getpwnam ( username ))) {
- pw_copy.pw_name = "UNKNOWN";
- pw_copy.pw_passwd = "!";
- opt_fflag = 0;
- failed = 1;
- } else
- pw_copy = *pw;
-
- pw = &pw_copy;
-
- if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
- failed = 1;
-
- if ( opt_fflag ) {
- opt_fflag = 0;
- goto auth_ok;
- }
-
- if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
- failed = 1;
-
- /* Don't check the password if password entry is empty (!) */
- if ( !pw-> pw_passwd[0] )
- goto auth_ok;
-
- /* authorization takes place here */
- if ( correct_password ( pw ))
- goto auth_ok;