1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
20 #include <selinux/selinux.h> /* for is_selinux_enabled() */
21 #include <selinux/get_context_list.h> /* for get_default_context() */
22 #include <selinux/flask.h> /* for security class definitions */
26 #ifdef CONFIG_FEATURE_UTMP
28 static void checkutmp(int picky);
29 static void setutmp(const char *name, const char *line);
30 /* Stuff global to this file */
31 static struct utmp utent;
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 security_context_t stat_sid = NULL, sid = NULL, old_tty_sid=NULL, new_tty_sid=NULL;
86 amroot = ( getuid ( ) == 0 );
87 signal ( SIGALRM, alarm_handler );
91 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
98 * username must be a separate token
99 * (-f root, *NOT* -froot). --marekm
101 if ( optarg != argv[optind-1] )
104 if ( !amroot ) /* Auth bypass only if real UID is zero */
105 bb_error_msg_and_die ( "-f permission denied" );
107 safe_strncpy(username, optarg, USERNAME_SIZE);
118 if (optind < argc) // user from command line (getty)
119 safe_strncpy(username, argv[optind], USERNAME_SIZE);
121 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
122 return EXIT_FAILURE; /* Must be a terminal */
124 #ifdef CONFIG_FEATURE_UTMP
125 checkutmp ( !amroot );
129 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
130 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
131 else if ( tmp && *tmp == '/' )
132 safe_strncpy ( tty, tmp, sizeof( tty ));
134 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
136 #ifdef CONFIG_FEATURE_UTMP
138 memset ( utent.ut_host, 0, sizeof utent.ut_host );
142 #ifdef CONFIG_FEATURE_UTMP
143 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
145 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
148 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
152 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
158 if(!login_prompt ( username ))
161 if ( !alarmstarted && ( TIMEOUT > 0 )) {
166 if (!( pw = getpwnam ( username ))) {
167 pw_copy.pw_name = "UNKNOWN";
168 pw_copy.pw_passwd = "!";
176 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
184 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
187 /* Don't check the password if password entry is empty (!) */
188 if ( !pw-> pw_passwd[0] )
191 /* authorization takes place here */
192 if ( correct_password ( pw ))
206 while ( difftime ( now, start ) < FAIL_DELAY) {
207 sleep ( FAIL_DELAY );
212 puts("Login incorrect");
214 if ( ++count == 3 ) {
215 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
221 if ( check_nologin ( pw-> pw_uid == 0 ))
224 #ifdef CONFIG_FEATURE_UTMP
225 setutmp ( username, tty );
229 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
231 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
233 #ifdef CONFIG_SELINUX
234 if (is_selinux_enabled())
239 if (get_default_context(username, NULL, &sid))
241 fprintf(stderr, "Unable to get SID for %s\n", username);
244 rc = getfilecon(full_tty,&stat_sid);
246 if ((rc<0) || (stat(full_tty, &st)<0))
248 fprintf(stderr, "stat_secure(%.100s) failed: %.100s\n", full_tty, strerror(errno));
251 if (security_compute_relabel (sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
253 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", full_tty, strerror(errno));
256 if(setfilecon(full_tty, new_tty_sid) != 0)
258 fprintf(stderr, "chsid(%.100s, %s) failed: %.100s\n", full_tty, new_tty_sid, strerror(errno));
262 freecon(old_tty_sid);
263 freecon(new_tty_sid);
266 if ( !is_my_tty ( full_tty ))
267 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
269 /* Try these, but don't complain if they fail
270 * (for example when the root fs is read only) */
271 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
272 chmod ( full_tty, 0600 );
274 change_identity ( pw );
278 setup_environment ( tmp, 1, !opt_preserve, pw );
281 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
283 if ( pw-> pw_uid == 0 )
284 syslog ( LOG_INFO, "root login %s\n", fromhost );
285 #ifdef CONFIG_SELINUX
286 set_current_security_context(sid);
288 run_shell ( tmp, 1, 0, 0); /* exec the shell finally. */
295 static int login_prompt ( char *buf_name )
301 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
302 print_login_prompt();
304 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
307 if ( !strchr ( buf, '\n' ))
310 for ( sp = buf; isspace ( *sp ); sp++ ) { }
311 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
314 safe_strncpy(buf_name, sp, USERNAME_SIZE);
322 static int check_nologin ( int amroot )
324 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
328 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
329 while (( c = getc ( fp )) != EOF )
330 putchar (( c == '\n' ) ? '\r' : c );
335 puts ( "\r\nSystem closed for routine maintenance.\r" );
340 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
345 #ifdef CONFIG_FEATURE_SECURETTY
347 static int check_tty ( const char *tty )
353 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
354 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
355 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
356 if ( !isspace ( buf[i] ))
360 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
363 if ( strcmp ( buf, tty ) == 0 ) {
371 /* A missing securetty file is not an error. */
377 /* returns 1 if true */
378 static int is_my_tty ( const char *tty )
380 struct stat by_name, by_fd;
382 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
385 if ( by_name. st_rdev != by_fd. st_rdev )
392 static void motd (void)
397 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
398 while (( c = getc ( fp )) != EOF )
405 #ifdef CONFIG_FEATURE_UTMP
406 // vv Taken from tinylogin utmp.c vv
409 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
411 "Unable to determine your tty name."
414 * checkutmp - see if utmp file is correct for this process
416 * System V is very picky about the contents of the utmp file
417 * and requires that a slot for the current process exist.
418 * The utmp file is scanned for an entry with the same process
419 * ID. If no entry exists the process exits with a message.
421 * The "picky" flag is for network and other logins that may
422 * use special flags. It allows the pid checks to be overridden.
423 * This means that getty should never invoke login with any
424 * command line flags.
427 static void checkutmp(int picky)
431 pid_t pid = getpid();
435 /* First, try to find a valid utmp entry for this process. */
436 while ((ut = getutent()))
437 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
438 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
441 /* If there is one, just use it, otherwise create a new one. */
454 if (strncmp(line, "/dev/", 5) == 0)
456 memset((void *) &utent, 0, sizeof utent);
457 utent.ut_type = LOGIN_PROCESS;
459 strncpy(utent.ut_line, line, sizeof utent.ut_line);
460 /* XXX - assumes /dev/tty?? */
461 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
462 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
463 time(&utent.ut_time);
468 * setutmp - put a USER_PROCESS entry in the utmp file
470 * setutmp changes the type of the current utmp entry to
471 * USER_PROCESS. the wtmp file will be updated as well.
474 static void setutmp(const char *name, const char *line)
476 utent.ut_type = USER_PROCESS;
477 strncpy(utent.ut_user, name, sizeof utent.ut_user);
478 time(&utent.ut_time);
479 /* other fields already filled in by checkutmp above */
483 #ifdef CONFIG_FEATURE_WTMP
484 if (access(_PATH_WTMP, R_OK|W_OK) == -1) {
485 close(creat(_PATH_WTMP, 0664));
487 updwtmp(_PATH_WTMP, &utent);
490 #endif /* CONFIG_FEATURE_UTMP */