1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
22 static void checkutmp(int picky);
23 static void setutmp(const char *name, const char *line);
25 // import from encrypt.c
26 extern char *pw_encrypt(const char *clear, const char *salt);
31 #define EMPTY_USERNAME_COUNT 10
32 #define USERNAME_SIZE 32
34 /* Stuff global to this file */
38 static int check_nologin ( int amroot );
40 #if defined CONFIG_FEATURE_SECURETTY
41 static int check_tty ( const char *tty );
44 static inline int check_tty ( const char *tty ) { return 1; }
48 static int is_my_tty ( const char *tty );
49 static int login_prompt ( char *buf_name );
50 static void motd ( void );
53 static void alarm_handler ( int sig )
55 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
56 exit ( EXIT_SUCCESS );
60 extern int login_main(int argc, char **argv)
65 char username[USERNAME_SIZE];
71 struct passwd *pw, pw_copy;
72 #ifdef CONFIG_WHEEL_GROUP
81 amroot = ( getuid ( ) == 0 );
82 signal ( SIGALRM, alarm_handler );
84 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
89 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
96 * username must be a seperate token
97 * (-f root, *NOT* -froot). --marekm
99 if ( optarg != argv[optind-1] )
102 if ( !amroot ) /* Auth bypass only if real UID is zero */
103 error_msg_and_die ( "-f permission denied" );
105 safe_strncpy(username, optarg, USERNAME_SIZE);
116 if (optind < argc) // user from command line (getty)
117 safe_strncpy(username, argv[optind], USERNAME_SIZE);
119 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
120 return EXIT_FAILURE; /* Must be a terminal */
122 checkutmp ( !amroot );
125 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
126 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
128 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
131 memset ( utent.ut_host, 0, sizeof utent.ut_host );
134 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
136 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
139 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
143 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
149 if(!login_prompt ( username ))
152 if ( !alarmstarted && ( TIMEOUT > 0 )) {
157 if (!( pw = getpwnam ( username ))) {
158 pw_copy.pw_name = "UNKNOWN";
159 pw_copy.pw_passwd = "!";
167 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
175 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
178 /* Don't check the password if password entry is empty (!) */
179 if ( !pw-> pw_passwd[0] )
182 /* authorization takes place here */
183 if ( correct_password ( pw ))
197 while ( difftime ( now, start ) < FAIL_DELAY) {
198 sleep ( FAIL_DELAY );
203 puts("Login incorrect");
205 if ( ++count == 3 ) {
206 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
212 if ( check_nologin ( pw-> pw_uid == 0 ))
215 setutmp ( username, tty );
217 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
219 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
221 if ( !is_my_tty ( full_tty ))
222 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
224 /* Try these, but don't complain if they fail
225 * (for example when the root fs is read only) */
226 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
227 chmod ( full_tty, 0600 );
229 change_identity ( pw );
230 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
233 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
235 if ( pw-> pw_uid == 0 )
236 syslog ( LOG_INFO, "root login %s\n", fromhost );
238 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
245 static int login_prompt ( char *buf_name )
251 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
252 gethostname ( buf, sizeof( buf ));
253 printf ( "\nBusyBox on %s login: ", buf );
256 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
259 if ( !strchr ( buf, '\n' ))
262 for ( sp = buf; isspace ( *sp ); sp++ ) { }
263 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
266 safe_strncpy(buf_name, sp, USERNAME_SIZE);
274 static int check_nologin ( int amroot )
276 if ( access ( nologin_file, F_OK ) == 0 ) {
280 if (( fp = fopen ( nologin_file, "r" ))) {
281 while (( c = getc ( fp )) != EOF )
282 putchar (( c == '\n' ) ? '\r' : c );
287 puts ( "\r\nSystem closed for routine maintenance.\r" );
292 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
297 #ifdef CONFIG_FEATURE_SECURETTY
299 static int check_tty ( const char *tty )
305 if (( fp = fopen ( securetty_file, "r" ))) {
306 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
307 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
308 if ( !isspace ( buf[i] ))
312 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
315 if ( strcmp ( buf, tty ) == 0 ) {
324 syslog ( LOG_WARNING, "cannot open securetty file.\n" );
331 /* returns 1 if true */
332 static int is_my_tty ( const char *tty )
334 struct stat by_name, by_fd;
336 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
339 if ( by_name. st_rdev != by_fd. st_rdev )
351 if (( fp = fopen ( motd_file, "r" ))) {
352 while (( c = getc ( fp )) != EOF )
359 // vv Taken from tinylogin utmp.c vv
361 #define _WTMP_FILE "/var/log/wtmp"
364 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
366 "Unable to determine your tty name."
369 * checkutmp - see if utmp file is correct for this process
371 * System V is very picky about the contents of the utmp file
372 * and requires that a slot for the current process exist.
373 * The utmp file is scanned for an entry with the same process
374 * ID. If no entry exists the process exits with a message.
376 * The "picky" flag is for network and other logins that may
377 * use special flags. It allows the pid checks to be overridden.
378 * This means that getty should never invoke login with any
379 * command line flags.
382 static void checkutmp(int picky)
386 pid_t pid = getpid();
390 /* First, try to find a valid utmp entry for this process. */
391 while ((ut = getutent()))
392 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
393 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
396 /* If there is one, just use it, otherwise create a new one. */
409 if (strncmp(line, "/dev/", 5) == 0)
411 memset((void *) &utent, 0, sizeof utent);
412 utent.ut_type = LOGIN_PROCESS;
414 strncpy(utent.ut_line, line, sizeof utent.ut_line);
415 /* XXX - assumes /dev/tty?? */
416 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
417 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
418 time(&utent.ut_time);
423 * setutmp - put a USER_PROCESS entry in the utmp file
425 * setutmp changes the type of the current utmp entry to
426 * USER_PROCESS. the wtmp file will be updated as well.
429 static void setutmp(const char *name, const char *line)
431 utent.ut_type = USER_PROCESS;
432 strncpy(utent.ut_user, name, sizeof utent.ut_user);
433 time(&utent.ut_time);
434 /* other fields already filled in by checkutmp above */
438 updwtmp(_WTMP_FILE, &utent);