1 /* vi: set sw=4 ts=4: */
11 #include <sys/resource.h>
14 #include <sys/types.h>
22 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
26 #include "tinylogin.h"
29 static void checkutmp(int picky);
30 static void setutmp(const char *name, const char *line);
32 // import from encrypt.c
33 extern char *pw_encrypt(const char *clear, const char *salt);
39 #define EMPTY_USERNAME_COUNT 10
40 #define MOTD_FILE "/etc/motd"
41 #define NOLOGIN_FILE "/etc/nologin"
42 #define SECURETTY_FILE "/etc/securetty"
44 #define USERNAME_SIZE 32
46 /* Stuff global to this file */
50 static int check_nologin ( int amroot );
52 #if defined CONFIG_FEATURE_SECURETTY
53 static int check_tty ( const char *tty );
56 static inline int check_tty ( const char *tty ) { return 1; }
60 static int is_my_tty ( const char *tty );
61 static int login_prompt ( char *buf_name );
62 static void motd ( void );
65 static void alarm_handler ( int sig )
67 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
68 exit ( EXIT_SUCCESS );
72 extern int login_main(int argc, char **argv)
77 char username[USERNAME_SIZE];
83 struct passwd *pw, pw_copy;
91 amroot = ( getuid ( ) == 0 );
92 signal ( SIGALRM, alarm_handler );
94 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
99 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
106 * username must be a seperate token
107 * (-f root, *NOT* -froot). --marekm
109 if ( optarg != argv[optind-1] )
112 if ( !amroot ) /* Auth bypass only if real UID is zero */
113 error_msg_and_die ( "-f permission denied" );
115 safe_strncpy(username, optarg, USERNAME_SIZE);
126 if (optind < argc) // user from command line (getty)
127 safe_strncpy(username, argv[optind], USERNAME_SIZE);
129 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
130 return EXIT_FAILURE; /* Must be a terminal */
132 checkutmp ( !amroot );
135 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
136 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
138 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
141 memset ( utent.ut_host, 0, sizeof utent.ut_host );
144 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
146 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
149 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
153 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
159 if(!login_prompt ( username ))
162 if ( !alarmstarted && ( TIMEOUT > 0 )) {
167 if (!( pw = getpwnam ( username ))) {
168 pw_copy.pw_name = "UNKNOWN";
169 pw_copy.pw_passwd = "!";
177 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
185 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
188 /* Don't check the password if password entry is empty (!) */
189 if ( !pw-> pw_passwd[0] )
192 /* authorization takes place here */
193 if ( correct_password ( pw ))
207 while ( difftime ( now, start ) < FAIL_DELAY) {
208 sleep ( FAIL_DELAY );
213 puts("Login incorrect");
215 if ( ++count == 3 ) {
216 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
222 if ( check_nologin ( pw-> pw_uid == 0 ))
225 setutmp ( username, tty );
227 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
229 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
231 if ( !is_my_tty ( full_tty ))
232 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
234 /* Try these, but don't complain if they fail
235 * (for example when the root fs is read only) */
236 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
237 chmod ( full_tty, 0600 );
239 change_identity ( pw );
240 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
243 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
245 if ( pw-> pw_uid == 0 )
246 syslog ( LOG_INFO, "root login %s\n", fromhost );
248 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
255 static int login_prompt ( char *buf_name )
261 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
262 gethostname ( buf, sizeof( buf ));
263 printf ( "\nBusyBox on %s login: ", buf );
266 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
269 if ( !strchr ( buf, '\n' ))
272 for ( sp = buf; isspace ( *sp ); sp++ ) { }
273 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
276 safe_strncpy(buf_name, sp, USERNAME_SIZE);
284 static int check_nologin ( int amroot )
286 if ( access ( NOLOGIN_FILE, F_OK ) == 0 ) {
290 if (( fp = fopen ( NOLOGIN_FILE, "r" ))) {
291 while (( c = getc ( fp )) != EOF )
292 putchar (( c == '\n' ) ? '\r' : c );
297 puts ( "\r\nSystem closed for routine maintenance.\r" );
302 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
307 #ifdef CONFIG_FEATURE_SECURETTY
309 static int check_tty ( const char *tty )
315 if (( fp = fopen ( SECURETTY_FILE, "r" ))) {
316 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
317 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
318 if ( !isspace ( buf[i] ))
322 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
325 if ( strcmp ( buf, tty ) == 0 ) {
334 syslog ( LOG_WARNING, "cannot open securetty file.\n" );
341 /* returns 1 if true */
342 static int is_my_tty ( const char *tty )
344 struct stat by_name, by_fd;
346 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
349 if ( by_name. st_rdev != by_fd. st_rdev )
361 if (( fp = fopen ( MOTD_FILE, "r" ))) {
362 while (( c = getc ( fp )) != EOF )
369 // vv Taken from tinylogin utmp.c vv
371 #define _WTMP_FILE "/var/log/wtmp"
374 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
376 "Unable to determine your tty name."
379 * checkutmp - see if utmp file is correct for this process
381 * System V is very picky about the contents of the utmp file
382 * and requires that a slot for the current process exist.
383 * The utmp file is scanned for an entry with the same process
384 * ID. If no entry exists the process exits with a message.
386 * The "picky" flag is for network and other logins that may
387 * use special flags. It allows the pid checks to be overridden.
388 * This means that getty should never invoke login with any
389 * command line flags.
392 static void checkutmp(int picky)
396 pid_t pid = getpid();
400 /* First, try to find a valid utmp entry for this process. */
401 while ((ut = getutent()))
402 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
403 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
406 /* If there is one, just use it, otherwise create a new one. */
419 if (strncmp(line, "/dev/", 5) == 0)
421 memset((void *) &utent, 0, sizeof utent);
422 utent.ut_type = LOGIN_PROCESS;
424 strncpy(utent.ut_line, line, sizeof utent.ut_line);
425 /* XXX - assumes /dev/tty?? */
426 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
427 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
428 time(&utent.ut_time);
432 #if __GNU_LIBRARY__ < 5
434 * Some systems already have updwtmp() and possibly updwtmpx(). Others
435 * don't, so we re-implement these functions if necessary. --marekm
437 static void updwtmp(const char *filename, const struct utmp *ut)
441 fd = open(filename, O_APPEND | O_WRONLY, 0);
443 write(fd, (const char *) ut, sizeof(*ut));
450 * setutmp - put a USER_PROCESS entry in the utmp file
452 * setutmp changes the type of the current utmp entry to
453 * USER_PROCESS. the wtmp file will be updated as well.
456 static void setutmp(const char *name, const char *line)
458 utent.ut_type = USER_PROCESS;
459 strncpy(utent.ut_user, name, sizeof utent.ut_user);
460 time(&utent.ut_time);
461 /* other fields already filled in by checkutmp above */
465 updwtmp(_WTMP_FILE, &utent);