1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
21 #ifdef CONFIG_FEATURE_U_W_TMP
23 static void checkutmp(int picky);
24 static void setutmp(const char *name, const char *line);
27 // import from encrypt.c
28 extern char *pw_encrypt(const char *clear, const char *salt);
33 #define EMPTY_USERNAME_COUNT 10
34 #define USERNAME_SIZE 32
36 /* Stuff global to this file */
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
83 amroot = ( getuid ( ) == 0 );
84 signal ( SIGALRM, alarm_handler );
86 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
91 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
98 * username must be a seperate 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 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_U_W_TMP
125 checkutmp ( !amroot );
129 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
130 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
132 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
135 memset ( utent.ut_host, 0, sizeof utent.ut_host );
138 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
140 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
143 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
147 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
153 if(!login_prompt ( username ))
156 if ( !alarmstarted && ( TIMEOUT > 0 )) {
161 if (!( pw = getpwnam ( username ))) {
162 pw_copy.pw_name = "UNKNOWN";
163 pw_copy.pw_passwd = "!";
171 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
179 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
182 /* Don't check the password if password entry is empty (!) */
183 if ( !pw-> pw_passwd[0] )
186 /* authorization takes place here */
187 if ( correct_password ( pw ))
201 while ( difftime ( now, start ) < FAIL_DELAY) {
202 sleep ( FAIL_DELAY );
207 puts("Login incorrect");
209 if ( ++count == 3 ) {
210 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
216 if ( check_nologin ( pw-> pw_uid == 0 ))
219 #ifdef CONFIG_FEATURE_U_W_TMP
220 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 if ( !is_my_tty ( full_tty ))
228 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
230 /* Try these, but don't complain if they fail
231 * (for example when the root fs is read only) */
232 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
233 chmod ( full_tty, 0600 );
235 change_identity ( pw );
236 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
239 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
241 if ( pw-> pw_uid == 0 )
242 syslog ( LOG_INFO, "root login %s\n", fromhost );
244 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
251 static int login_prompt ( char *buf_name )
257 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
258 gethostname ( buf, sizeof( buf ));
259 printf ( "\nBusyBox on %s login: ", buf );
262 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
265 if ( !strchr ( buf, '\n' ))
268 for ( sp = buf; isspace ( *sp ); sp++ ) { }
269 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
272 safe_strncpy(buf_name, sp, USERNAME_SIZE);
280 static int check_nologin ( int amroot )
282 if ( access ( nologin_file, F_OK ) == 0 ) {
286 if (( fp = fopen ( nologin_file, "r" ))) {
287 while (( c = getc ( fp )) != EOF )
288 putchar (( c == '\n' ) ? '\r' : c );
293 puts ( "\r\nSystem closed for routine maintenance.\r" );
298 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
303 #ifdef CONFIG_FEATURE_SECURETTY
305 static int check_tty ( const char *tty )
311 if (( fp = fopen ( securetty_file, "r" ))) {
312 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
313 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
314 if ( !isspace ( buf[i] ))
318 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
321 if ( strcmp ( buf, tty ) == 0 ) {
330 syslog ( LOG_WARNING, "cannot open securetty file.\n" );
337 /* returns 1 if true */
338 static int is_my_tty ( const char *tty )
340 struct stat by_name, by_fd;
342 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
345 if ( by_name. st_rdev != by_fd. st_rdev )
357 if (( fp = fopen ( motd_file, "r" ))) {
358 while (( c = getc ( fp )) != EOF )
365 #ifdef CONFIG_FEATURE_U_W_TMP
366 // vv Taken from tinylogin utmp.c vv
368 #define _WTMP_FILE "/var/log/wtmp"
371 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
373 "Unable to determine your tty name."
376 * checkutmp - see if utmp file is correct for this process
378 * System V is very picky about the contents of the utmp file
379 * and requires that a slot for the current process exist.
380 * The utmp file is scanned for an entry with the same process
381 * ID. If no entry exists the process exits with a message.
383 * The "picky" flag is for network and other logins that may
384 * use special flags. It allows the pid checks to be overridden.
385 * This means that getty should never invoke login with any
386 * command line flags.
389 static void checkutmp(int picky)
393 pid_t pid = getpid();
397 /* First, try to find a valid utmp entry for this process. */
398 while ((ut = getutent()))
399 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
400 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
403 /* If there is one, just use it, otherwise create a new one. */
416 if (strncmp(line, "/dev/", 5) == 0)
418 memset((void *) &utent, 0, sizeof utent);
419 utent.ut_type = LOGIN_PROCESS;
421 strncpy(utent.ut_line, line, sizeof utent.ut_line);
422 /* XXX - assumes /dev/tty?? */
423 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
424 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
425 time(&utent.ut_time);
430 * setutmp - put a USER_PROCESS entry in the utmp file
432 * setutmp changes the type of the current utmp entry to
433 * USER_PROCESS. the wtmp file will be updated as well.
436 static void setutmp(const char *name, const char *line)
438 utent.ut_type = USER_PROCESS;
439 strncpy(utent.ut_user, name, sizeof utent.ut_user);
440 time(&utent.ut_time);
441 /* other fields already filled in by checkutmp above */
445 updwtmp(_WTMP_FILE, &utent);
447 #endif /* CONFIG_FEATURE_U_W_TMP */