1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/resource.h>
17 #include <sys/types.h>
24 #include <selinux/selinux.h> /* for is_selinux_enabled() */
25 #include <selinux/get_context_list.h> /* for get_default_context() */
26 #include <selinux/flask.h> /* for security class definitions */
30 #ifdef CONFIG_FEATURE_UTMP
32 static void checkutmp(int picky);
33 static void setutmp(const char *name, const char *line);
34 /* Stuff global to this file */
35 static struct utmp utent;
40 #define EMPTY_USERNAME_COUNT 10
41 #define USERNAME_SIZE 32
44 static int check_nologin ( int amroot );
46 #if defined CONFIG_FEATURE_SECURETTY
47 static int check_tty ( const char *tty );
50 static inline int check_tty ( const char *tty ) { return 1; }
54 static int is_my_tty ( const char *tty );
55 static int login_prompt ( char *buf_name );
56 static void motd ( void );
59 static void alarm_handler ( int sig ATTRIBUTE_UNUSED)
61 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
62 exit ( EXIT_SUCCESS );
66 int login_main(int argc, char **argv)
71 char username[USERNAME_SIZE];
77 struct passwd *pw, pw_copy;
78 #ifdef CONFIG_WHEEL_GROUP
86 security_context_t user_sid = NULL;
90 amroot = ( getuid ( ) == 0 );
91 signal ( SIGALRM, alarm_handler );
95 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
102 * username must be a separate 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_UTMP
129 checkutmp ( !amroot );
133 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
134 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
135 else if ( tmp && *tmp == '/' )
136 safe_strncpy ( tty, tmp, sizeof( tty ));
138 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
140 #ifdef CONFIG_FEATURE_UTMP
142 memset ( utent.ut_host, 0, sizeof utent.ut_host );
146 #ifdef CONFIG_FEATURE_UTMP
147 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
149 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
152 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
156 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
162 if(!login_prompt ( username ))
165 if ( !alarmstarted && ( TIMEOUT > 0 )) {
170 if (!( pw = getpwnam ( username ))) {
171 pw_copy.pw_name = "UNKNOWN";
172 pw_copy.pw_passwd = "!";
180 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
188 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
191 /* Don't check the password if password entry is empty (!) */
192 if ( !pw-> pw_passwd[0] )
195 /* authorization takes place here */
196 if ( correct_password ( pw ))
205 bb_do_delay(FAIL_DELAY);
206 puts("Login incorrect");
208 if ( ++count == 3 ) {
209 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
215 if ( check_nologin ( pw-> pw_uid == 0 ))
218 #ifdef CONFIG_FEATURE_UTMP
219 setutmp ( username, tty );
223 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
225 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
227 #ifdef CONFIG_SELINUX
228 if (is_selinux_enabled())
230 security_context_t old_tty_sid, new_tty_sid;
232 if (get_default_context(username, NULL, &user_sid))
234 fprintf(stderr, "Unable to get SID for %s\n", username);
237 if (getfilecon(full_tty, &old_tty_sid) < 0)
239 fprintf(stderr, "getfilecon(%.100s) failed: %.100s\n", full_tty, strerror(errno));
242 if (security_compute_relabel(user_sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
244 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", full_tty, strerror(errno));
247 if(setfilecon(full_tty, new_tty_sid) != 0)
249 fprintf(stderr, "chsid(%.100s, %s) failed: %.100s\n", full_tty, new_tty_sid, strerror(errno));
254 if ( !is_my_tty ( full_tty ))
255 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
257 /* Try these, but don't complain if they fail
258 * (for example when the root fs is read only) */
259 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
260 chmod ( full_tty, 0600 );
262 if (ENABLE_LOGIN_SCRIPTS) {
263 char *script = getenv("LOGIN_PRE_SUID_SCRIPT");
265 char *t_argv[2] = { script, NULL };
270 setenv("LOGIN_TTY", full_tty, 1);
271 setenv("LOGIN_USER", pw->pw_name, 1);
272 setenv("LOGIN_UID", utoa(pw->pw_uid), 1);
273 setenv("LOGIN_GID", utoa(pw->pw_gid), 1);
274 setenv("LOGIN_SHELL", pw->pw_shell, 1);
275 execvp(script, t_argv);
277 default: /* parent */
283 change_identity ( pw );
287 setup_environment ( tmp, 1, !opt_preserve, pw );
290 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
292 if ( pw-> pw_uid == 0 )
293 syslog ( LOG_INFO, "root login %s\n", fromhost );
294 #ifdef CONFIG_SELINUX
295 /* well, a simple setexeccon() here would do the job as well,
296 * but let's play the game for now */
297 set_current_security_context(user_sid);
299 run_shell ( tmp, 1, 0, 0); /* exec the shell finally. */
306 static int login_prompt ( char *buf_name )
312 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
313 print_login_prompt();
315 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
318 if ( !strchr ( buf, '\n' ))
321 for ( sp = buf; isspace ( *sp ); sp++ ) { }
322 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
325 safe_strncpy(buf_name, sp, USERNAME_SIZE);
333 static int check_nologin ( int amroot )
335 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
339 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
340 while (( c = getc ( fp )) != EOF )
341 putchar (( c == '\n' ) ? '\r' : c );
346 puts ( "\r\nSystem closed for routine maintenance.\r" );
351 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
356 #ifdef CONFIG_FEATURE_SECURETTY
358 static int check_tty ( const char *tty )
364 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
365 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
366 for ( i = strlen( buf ) - 1; i >= 0; --i ) {
367 if ( !isspace ( buf[i] ))
371 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
374 if ( strcmp ( buf, tty ) == 0 ) {
382 /* A missing securetty file is not an error. */
388 /* returns 1 if true */
389 static int is_my_tty ( const char *tty )
391 struct stat by_name, by_fd;
393 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
396 if ( by_name. st_rdev != by_fd. st_rdev )
403 static void motd (void)
408 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
409 while (( c = getc ( fp )) != EOF )
416 #ifdef CONFIG_FEATURE_UTMP
417 // vv Taken from tinylogin utmp.c vv
420 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
422 "Unable to determine your tty name."
425 * checkutmp - see if utmp file is correct for this process
427 * System V is very picky about the contents of the utmp file
428 * and requires that a slot for the current process exist.
429 * The utmp file is scanned for an entry with the same process
430 * ID. If no entry exists the process exits with a message.
432 * The "picky" flag is for network and other logins that may
433 * use special flags. It allows the pid checks to be overridden.
434 * This means that getty should never invoke login with any
435 * command line flags.
438 static void checkutmp(int picky)
442 pid_t pid = getpid();
446 /* First, try to find a valid utmp entry for this process. */
447 while ((ut = getutent()))
448 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
449 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
452 /* If there is one, just use it, otherwise create a new one. */
467 if (strncmp(line, "/dev/", 5) == 0)
469 memset(&utent, 0, sizeof utent);
470 utent.ut_type = LOGIN_PROCESS;
472 strncpy(utent.ut_line, line, sizeof utent.ut_line);
473 /* XXX - assumes /dev/tty?? */
474 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
475 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
476 t_tmp = (time_t)utent.ut_time;
482 * setutmp - put a USER_PROCESS entry in the utmp file
484 * setutmp changes the type of the current utmp entry to
485 * USER_PROCESS. the wtmp file will be updated as well.
488 static void setutmp(const char *name, const char *line ATTRIBUTE_UNUSED)
490 time_t t_tmp = (time_t)utent.ut_time;
492 utent.ut_type = USER_PROCESS;
493 strncpy(utent.ut_user, name, sizeof utent.ut_user);
495 /* other fields already filled in by checkutmp above */
499 #ifdef CONFIG_FEATURE_WTMP
500 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
501 close(creat(bb_path_wtmp_file, 0664));
503 updwtmp(bb_path_wtmp_file, &utent);
506 #endif /* CONFIG_FEATURE_UTMP */