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 );
90 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
95 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
102 * username must be a seperate token
103 * (-f root, *NOT* -froot). --marekm
105 if ( optarg != argv[optind-1] )
108 if ( !amroot ) /* Auth bypass only if real UID is zero */
109 bb_error_msg_and_die ( "-f permission denied" );
111 safe_strncpy(username, optarg, USERNAME_SIZE);
122 if (optind < argc) // user from command line (getty)
123 safe_strncpy(username, argv[optind], USERNAME_SIZE);
125 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
126 return EXIT_FAILURE; /* Must be a terminal */
128 #ifdef CONFIG_FEATURE_U_W_TMP
129 checkutmp ( !amroot );
133 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
134 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
136 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
138 #ifdef CONFIG_FEATURE_U_W_TMP
140 memset ( utent.ut_host, 0, sizeof utent.ut_host );
144 #ifdef CONFIG_FEATURE_U_W_TMP
145 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
147 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
150 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
154 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
160 if(!login_prompt ( username ))
163 if ( !alarmstarted && ( TIMEOUT > 0 )) {
168 if (!( pw = getpwnam ( username ))) {
169 pw_copy.pw_name = "UNKNOWN";
170 pw_copy.pw_passwd = "!";
178 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
186 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
189 /* Don't check the password if password entry is empty (!) */
190 if ( !pw-> pw_passwd[0] )
193 /* authorization takes place here */
194 if ( correct_password ( pw ))
208 while ( difftime ( now, start ) < FAIL_DELAY) {
209 sleep ( FAIL_DELAY );
214 puts("Login incorrect");
216 if ( ++count == 3 ) {
217 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
223 if ( check_nologin ( pw-> pw_uid == 0 ))
226 #ifdef CONFIG_FEATURE_U_W_TMP
227 setutmp ( username, tty );
229 #ifdef CONFIG_SELINUX
234 if (get_default_sid(username, 0, &sid))
236 fprintf(stderr, "Unable to get SID for %s\n", username);
239 if (stat_secure(tty, &st, &old_tty_sid))
241 fprintf(stderr, "stat_secure(%.100s) failed: %.100s\n", tty, strerror(errno));
244 if (security_change_sid (sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
246 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", tty, strerror(errno));
249 if(chsid(tty, new_tty_sid) != 0)
251 fprintf(stderr, "chsid(%.100s, %d) failed: %.100s\n", tty, new_tty_sid, strerror(errno));
260 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
262 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
264 if ( !is_my_tty ( full_tty ))
265 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
267 /* Try these, but don't complain if they fail
268 * (for example when the root fs is read only) */
269 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
270 chmod ( full_tty, 0600 );
272 change_identity ( pw );
273 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
276 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
278 if ( pw-> pw_uid == 0 )
279 syslog ( LOG_INFO, "root login %s\n", fromhost );
281 run_shell ( pw-> pw_shell, 1, 0, 0
282 #ifdef CONFIG_SELINUX
285 ); /* exec the shell finally. */
292 static int login_prompt ( char *buf_name )
298 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
299 print_login_prompt();
301 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
304 if ( !strchr ( buf, '\n' ))
307 for ( sp = buf; isspace ( *sp ); sp++ ) { }
308 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
311 safe_strncpy(buf_name, sp, USERNAME_SIZE);
319 static int check_nologin ( int amroot )
321 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
325 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
326 while (( c = getc ( fp )) != EOF )
327 putchar (( c == '\n' ) ? '\r' : c );
332 puts ( "\r\nSystem closed for routine maintenance.\r" );
337 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
342 #ifdef CONFIG_FEATURE_SECURETTY
344 static int check_tty ( const char *tty )
350 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
351 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
352 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
353 if ( !isspace ( buf[i] ))
357 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
360 if ( strcmp ( buf, tty ) == 0 ) {
368 /* A missing securetty file is not an error. */
374 /* returns 1 if true */
375 static int is_my_tty ( const char *tty )
377 struct stat by_name, by_fd;
379 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
382 if ( by_name. st_rdev != by_fd. st_rdev )
394 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
395 while (( c = getc ( fp )) != EOF )
402 #ifdef CONFIG_FEATURE_U_W_TMP
403 // vv Taken from tinylogin utmp.c vv
405 #define _WTMP_FILE "/var/log/wtmp"
408 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
410 "Unable to determine your tty name."
413 * checkutmp - see if utmp file is correct for this process
415 * System V is very picky about the contents of the utmp file
416 * and requires that a slot for the current process exist.
417 * The utmp file is scanned for an entry with the same process
418 * ID. If no entry exists the process exits with a message.
420 * The "picky" flag is for network and other logins that may
421 * use special flags. It allows the pid checks to be overridden.
422 * This means that getty should never invoke login with any
423 * command line flags.
426 static void checkutmp(int picky)
430 pid_t pid = getpid();
434 /* First, try to find a valid utmp entry for this process. */
435 while ((ut = getutent()))
436 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
437 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
440 /* If there is one, just use it, otherwise create a new one. */
453 if (strncmp(line, "/dev/", 5) == 0)
455 memset((void *) &utent, 0, sizeof utent);
456 utent.ut_type = LOGIN_PROCESS;
458 strncpy(utent.ut_line, line, sizeof utent.ut_line);
459 /* XXX - assumes /dev/tty?? */
460 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
461 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
462 time(&utent.ut_time);
467 * setutmp - put a USER_PROCESS entry in the utmp file
469 * setutmp changes the type of the current utmp entry to
470 * USER_PROCESS. the wtmp file will be updated as well.
473 static void setutmp(const char *name, const char *line)
475 utent.ut_type = USER_PROCESS;
476 strncpy(utent.ut_user, name, sizeof utent.ut_user);
477 time(&utent.ut_time);
478 /* other fields already filled in by checkutmp above */
482 updwtmp(_WTMP_FILE, &utent);
484 #endif /* CONFIG_FEATURE_U_W_TMP */