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 ATTRIBUTE_UNUSED)
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 ))
201 bb_do_delay(FAIL_DELAY);
202 puts("Login incorrect");
204 if ( ++count == 3 ) {
205 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
211 if ( check_nologin ( pw-> pw_uid == 0 ))
214 #ifdef CONFIG_FEATURE_UTMP
215 setutmp ( username, tty );
219 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
221 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
223 #ifdef CONFIG_SELINUX
224 if (is_selinux_enabled())
229 if (get_default_context(username, NULL, &sid))
231 fprintf(stderr, "Unable to get SID for %s\n", username);
234 rc = getfilecon(full_tty,&stat_sid);
236 if ((rc<0) || (stat(full_tty, &st)<0))
238 fprintf(stderr, "stat_secure(%.100s) failed: %.100s\n", full_tty, strerror(errno));
241 if (security_compute_relabel (sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
243 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", full_tty, strerror(errno));
246 if(setfilecon(full_tty, new_tty_sid) != 0)
248 fprintf(stderr, "chsid(%.100s, %s) failed: %.100s\n", full_tty, new_tty_sid, strerror(errno));
252 freecon(old_tty_sid);
253 freecon(new_tty_sid);
256 if ( !is_my_tty ( full_tty ))
257 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
259 /* Try these, but don't complain if they fail
260 * (for example when the root fs is read only) */
261 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
262 chmod ( full_tty, 0600 );
264 change_identity ( pw );
268 setup_environment ( tmp, 1, !opt_preserve, pw );
271 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
273 if ( pw-> pw_uid == 0 )
274 syslog ( LOG_INFO, "root login %s\n", fromhost );
275 #ifdef CONFIG_SELINUX
276 set_current_security_context(sid);
278 run_shell ( tmp, 1, 0, 0); /* exec the shell finally. */
285 static int login_prompt ( char *buf_name )
291 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
292 print_login_prompt();
294 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
297 if ( !strchr ( buf, '\n' ))
300 for ( sp = buf; isspace ( *sp ); sp++ ) { }
301 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
304 safe_strncpy(buf_name, sp, USERNAME_SIZE);
312 static int check_nologin ( int amroot )
314 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
318 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
319 while (( c = getc ( fp )) != EOF )
320 putchar (( c == '\n' ) ? '\r' : c );
325 puts ( "\r\nSystem closed for routine maintenance.\r" );
330 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
335 #ifdef CONFIG_FEATURE_SECURETTY
337 static int check_tty ( const char *tty )
343 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
344 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
345 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
346 if ( !isspace ( buf[i] ))
350 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
353 if ( strcmp ( buf, tty ) == 0 ) {
361 /* A missing securetty file is not an error. */
367 /* returns 1 if true */
368 static int is_my_tty ( const char *tty )
370 struct stat by_name, by_fd;
372 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
375 if ( by_name. st_rdev != by_fd. st_rdev )
382 static void motd (void)
387 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
388 while (( c = getc ( fp )) != EOF )
395 #ifdef CONFIG_FEATURE_UTMP
396 // vv Taken from tinylogin utmp.c vv
399 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
401 "Unable to determine your tty name."
404 * checkutmp - see if utmp file is correct for this process
406 * System V is very picky about the contents of the utmp file
407 * and requires that a slot for the current process exist.
408 * The utmp file is scanned for an entry with the same process
409 * ID. If no entry exists the process exits with a message.
411 * The "picky" flag is for network and other logins that may
412 * use special flags. It allows the pid checks to be overridden.
413 * This means that getty should never invoke login with any
414 * command line flags.
417 static void checkutmp(int picky)
421 pid_t pid = getpid();
425 /* First, try to find a valid utmp entry for this process. */
426 while ((ut = getutent()))
427 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
428 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
431 /* If there is one, just use it, otherwise create a new one. */
446 if (strncmp(line, "/dev/", 5) == 0)
448 memset((void *) &utent, 0, sizeof utent);
449 utent.ut_type = LOGIN_PROCESS;
451 strncpy(utent.ut_line, line, sizeof utent.ut_line);
452 /* XXX - assumes /dev/tty?? */
453 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
454 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
455 t_tmp = (time_t)utent.ut_time;
461 * setutmp - put a USER_PROCESS entry in the utmp file
463 * setutmp changes the type of the current utmp entry to
464 * USER_PROCESS. the wtmp file will be updated as well.
467 static void setutmp(const char *name, const char *line ATTRIBUTE_UNUSED)
469 time_t t_tmp = (time_t)utent.ut_time;
471 utent.ut_type = USER_PROCESS;
472 strncpy(utent.ut_user, name, sizeof utent.ut_user);
474 /* other fields already filled in by checkutmp above */
478 #ifdef CONFIG_FEATURE_WTMP
479 if (access(_PATH_WTMP, R_OK|W_OK) == -1) {
480 close(creat(_PATH_WTMP, 0664));
482 updwtmp(_PATH_WTMP, &utent);
485 #endif /* CONFIG_FEATURE_UTMP */