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 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 user_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())
226 security_context_t old_tty_sid, new_tty_sid;
228 if (get_default_context(username, NULL, &user_sid))
230 fprintf(stderr, "Unable to get SID for %s\n", username);
233 if (getfilecon(full_tty, &old_tty_sid) < 0)
235 fprintf(stderr, "getfilecon(%.100s) failed: %.100s\n", full_tty, strerror(errno));
238 if (security_compute_relabel(user_sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
240 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", full_tty, strerror(errno));
243 if(setfilecon(full_tty, new_tty_sid) != 0)
245 fprintf(stderr, "chsid(%.100s, %s) failed: %.100s\n", full_tty, new_tty_sid, strerror(errno));
250 if ( !is_my_tty ( full_tty ))
251 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
253 /* Try these, but don't complain if they fail
254 * (for example when the root fs is read only) */
255 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
256 chmod ( full_tty, 0600 );
258 change_identity ( pw );
262 setup_environment ( tmp, 1, !opt_preserve, pw );
265 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
267 if ( pw-> pw_uid == 0 )
268 syslog ( LOG_INFO, "root login %s\n", fromhost );
269 #ifdef CONFIG_SELINUX
270 /* well, a simple setexeccon() here would do the job as well,
271 * but let's play the game for now */
272 set_current_security_context(user_sid);
274 run_shell ( tmp, 1, 0, 0); /* exec the shell finally. */
281 static int login_prompt ( char *buf_name )
287 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
288 print_login_prompt();
290 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
293 if ( !strchr ( buf, '\n' ))
296 for ( sp = buf; isspace ( *sp ); sp++ ) { }
297 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
300 safe_strncpy(buf_name, sp, USERNAME_SIZE);
308 static int check_nologin ( int amroot )
310 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
314 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
315 while (( c = getc ( fp )) != EOF )
316 putchar (( c == '\n' ) ? '\r' : c );
321 puts ( "\r\nSystem closed for routine maintenance.\r" );
326 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
331 #ifdef CONFIG_FEATURE_SECURETTY
333 static int check_tty ( const char *tty )
339 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
340 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
341 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
342 if ( !isspace ( buf[i] ))
346 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
349 if ( strcmp ( buf, tty ) == 0 ) {
357 /* A missing securetty file is not an error. */
363 /* returns 1 if true */
364 static int is_my_tty ( const char *tty )
366 struct stat by_name, by_fd;
368 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
371 if ( by_name. st_rdev != by_fd. st_rdev )
378 static void motd (void)
383 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
384 while (( c = getc ( fp )) != EOF )
391 #ifdef CONFIG_FEATURE_UTMP
392 // vv Taken from tinylogin utmp.c vv
395 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
397 "Unable to determine your tty name."
400 * checkutmp - see if utmp file is correct for this process
402 * System V is very picky about the contents of the utmp file
403 * and requires that a slot for the current process exist.
404 * The utmp file is scanned for an entry with the same process
405 * ID. If no entry exists the process exits with a message.
407 * The "picky" flag is for network and other logins that may
408 * use special flags. It allows the pid checks to be overridden.
409 * This means that getty should never invoke login with any
410 * command line flags.
413 static void checkutmp(int picky)
417 pid_t pid = getpid();
421 /* First, try to find a valid utmp entry for this process. */
422 while ((ut = getutent()))
423 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
424 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
427 /* If there is one, just use it, otherwise create a new one. */
442 if (strncmp(line, "/dev/", 5) == 0)
444 memset((void *) &utent, 0, sizeof utent);
445 utent.ut_type = LOGIN_PROCESS;
447 strncpy(utent.ut_line, line, sizeof utent.ut_line);
448 /* XXX - assumes /dev/tty?? */
449 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
450 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
451 t_tmp = (time_t)utent.ut_time;
457 * setutmp - put a USER_PROCESS entry in the utmp file
459 * setutmp changes the type of the current utmp entry to
460 * USER_PROCESS. the wtmp file will be updated as well.
463 static void setutmp(const char *name, const char *line ATTRIBUTE_UNUSED)
465 time_t t_tmp = (time_t)utent.ut_time;
467 utent.ut_type = USER_PROCESS;
468 strncpy(utent.ut_user, name, sizeof utent.ut_user);
470 /* other fields already filled in by checkutmp above */
474 #ifdef CONFIG_FEATURE_WTMP
475 if (access(_PATH_WTMP, R_OK|W_OK) == -1) {
476 close(creat(_PATH_WTMP, 0664));
478 updwtmp(_PATH_WTMP, &utent);
481 #endif /* CONFIG_FEATURE_UTMP */