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 );
92 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
99 * username must be a seperate token
100 * (-f root, *NOT* -froot). --marekm
102 if ( optarg != argv[optind-1] )
105 if ( !amroot ) /* Auth bypass only if real UID is zero */
106 bb_error_msg_and_die ( "-f permission denied" );
108 safe_strncpy(username, optarg, USERNAME_SIZE);
119 if (optind < argc) // user from command line (getty)
120 safe_strncpy(username, argv[optind], USERNAME_SIZE);
122 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
123 return EXIT_FAILURE; /* Must be a terminal */
125 #ifdef CONFIG_FEATURE_U_W_TMP
126 checkutmp ( !amroot );
131 safe_strncpy ( tty, tmp, sizeof( tty ));
133 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
135 #ifdef CONFIG_FEATURE_U_W_TMP
137 memset ( utent.ut_host, 0, sizeof utent.ut_host );
141 #ifdef CONFIG_FEATURE_U_W_TMP
142 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
144 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
147 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
151 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
157 if(!login_prompt ( username ))
160 if ( !alarmstarted && ( TIMEOUT > 0 )) {
165 if (!( pw = getpwnam ( username ))) {
166 pw_copy.pw_name = "UNKNOWN";
167 pw_copy.pw_passwd = "!";
175 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
183 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
186 /* Don't check the password if password entry is empty (!) */
187 if ( !pw-> pw_passwd[0] )
190 /* authorization takes place here */
191 if ( correct_password ( pw ))
205 while ( difftime ( now, start ) < FAIL_DELAY) {
206 sleep ( FAIL_DELAY );
211 puts("Login incorrect");
213 if ( ++count == 3 ) {
214 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
220 if ( check_nologin ( pw-> pw_uid == 0 ))
223 #ifdef CONFIG_FEATURE_U_W_TMP
224 setutmp ( username, tty );
226 #ifdef CONFIG_SELINUX
231 if (get_default_sid(username, 0, &sid))
233 fprintf(stderr, "Unable to get SID for %s\n", username);
236 if (stat_secure(tty, &st, &old_tty_sid))
238 fprintf(stderr, "stat_secure(%.100s) failed: %.100s\n", tty, strerror(errno));
241 if (security_change_sid (sid, old_tty_sid, SECCLASS_CHR_FILE, &new_tty_sid) != 0)
243 fprintf(stderr, "security_change_sid(%.100s) failed: %.100s\n", tty, strerror(errno));
246 if(chsid(tty, new_tty_sid) != 0)
248 fprintf(stderr, "chsid(%.100s, %d) failed: %.100s\n", tty, new_tty_sid, strerror(errno));
257 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
259 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
261 if ( !is_my_tty ( full_tty ))
262 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
264 /* Try these, but don't complain if they fail
265 * (for example when the root fs is read only) */
266 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
267 chmod ( full_tty, 0600 );
269 change_identity ( pw );
273 setup_environment ( tmp, 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 );
280 run_shell ( tmp, 1, 0, 0
281 #ifdef CONFIG_SELINUX
284 ); /* exec the shell finally. */
291 static int login_prompt ( char *buf_name )
297 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
298 print_login_prompt();
300 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
303 if ( !strchr ( buf, '\n' ))
306 for ( sp = buf; isspace ( *sp ); sp++ ) { }
307 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
310 safe_strncpy(buf_name, sp, USERNAME_SIZE);
318 static int check_nologin ( int amroot )
320 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
324 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
325 while (( c = getc ( fp )) != EOF )
326 putchar (( c == '\n' ) ? '\r' : c );
331 puts ( "\r\nSystem closed for routine maintenance.\r" );
336 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
341 #ifdef CONFIG_FEATURE_SECURETTY
343 static int check_tty ( const char *tty )
349 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
350 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
351 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
352 if ( !isspace ( buf[i] ))
356 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
359 if ( strcmp ( buf, tty ) == 0 ) {
367 /* A missing securetty file is not an error. */
373 /* returns 1 if true */
374 static int is_my_tty ( const char *tty )
376 struct stat by_name, by_fd;
378 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
381 if ( by_name. st_rdev != by_fd. st_rdev )
393 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
394 while (( c = getc ( fp )) != EOF )
401 #ifdef CONFIG_FEATURE_U_W_TMP
402 // vv Taken from tinylogin utmp.c vv
404 #define _WTMP_FILE "/var/log/wtmp"
407 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
409 "Unable to determine your tty name."
412 * checkutmp - see if utmp file is correct for this process
414 * System V is very picky about the contents of the utmp file
415 * and requires that a slot for the current process exist.
416 * The utmp file is scanned for an entry with the same process
417 * ID. If no entry exists the process exits with a message.
419 * The "picky" flag is for network and other logins that may
420 * use special flags. It allows the pid checks to be overridden.
421 * This means that getty should never invoke login with any
422 * command line flags.
425 static void checkutmp(int picky)
429 pid_t pid = getpid();
433 /* First, try to find a valid utmp entry for this process. */
434 while ((ut = getutent()))
435 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
436 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
439 /* If there is one, just use it, otherwise create a new one. */
452 if (strncmp(line, "/dev/", 5) == 0)
454 memset((void *) &utent, 0, sizeof utent);
455 utent.ut_type = LOGIN_PROCESS;
457 strncpy(utent.ut_line, line, sizeof utent.ut_line);
458 /* XXX - assumes /dev/tty?? */
459 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
460 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
461 time(&utent.ut_time);
466 * setutmp - put a USER_PROCESS entry in the utmp file
468 * setutmp changes the type of the current utmp entry to
469 * USER_PROCESS. the wtmp file will be updated as well.
472 static void setutmp(const char *name, const char *line)
474 utent.ut_type = USER_PROCESS;
475 strncpy(utent.ut_user, name, sizeof utent.ut_user);
476 time(&utent.ut_time);
477 /* other fields already filled in by checkutmp above */
481 updwtmp(_WTMP_FILE, &utent);
483 #endif /* CONFIG_FEATURE_U_W_TMP */