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);
37 #define DEFAULT_USER "UNKNOWN"
38 #define DEFAULT_PWD "!"
39 #define DEFAULT_SHELL "/bin/sh"
42 #define MOTD_FILE "/etc/motd"
43 #define NOLOGIN_FILE "/etc/nologin"
44 #define SECURETTY_FILE "/etc/securetty"
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 const char *login_prompt ( void );
62 static void motd ( void );
63 static void set_env(int argc, char *const *argv);
66 static void alarm_handler ( int sig )
68 error_msg ( "\nLogin timed out after %d seconds.\n", TIMEOUT );
69 exit ( EXIT_SUCCESS );
73 extern int login_main(int argc, char **argv)
83 struct passwd *pw, pw_copy;
88 const char *username = 0;
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 ) {
102 printf ( "PRESERVE\n" );
107 * username must be a seperate token
108 * (-f root, *NOT* -froot). --marekm
110 if ( optarg != argv[optind-1] )
113 if ( !amroot ) /* Auth bypass only if real UID is zero */
114 error_msg_and_die ( "login: -f permission denied\n" );
116 printf ( "USERNAME: %s\n", optarg );
129 if ( optind < argc ) // got a username
130 username = argv [optind++];
132 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
133 return EXIT_FAILURE; /* Must be a terminal */
135 checkutmp ( !amroot );
138 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
139 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
141 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
144 memset ( utent.ut_host, 0, sizeof utent.ut_host );
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 );
154 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
159 if ( !username || !username[0] )
160 username = login_prompt ( );
162 if ( !alarmstarted && ( TIMEOUT > 0 )) {
167 if (!( pw = getpwnam ( username ))) {
168 pw_copy. pw_name = DEFAULT_USER;
169 pw_copy. pw_passwd = DEFAULT_PWD;
170 pw_copy. pw_shell = DEFAULT_SHELL;
178 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
186 if (( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
189 /* Don't check the password if password entry is empty (!) */
190 if ( !pw-> pw_passwd[0] )
193 /* authorization takes place here */
194 if ( correct_password ( pw ))
197 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw-> pw_name, fromhost);
209 while ( difftime ( now, start ) < FAIL_DELAY) {
210 sleep ( FAIL_DELAY );
215 puts("Login incorrect");
221 if ( check_nologin ( pw-> pw_uid == 0 ))
224 setutmp ( username, tty );
226 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
228 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
230 if ( !is_my_tty ( full_tty ))
231 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
233 /* Try these, but don't complain if they fail
234 * (for example when the root fs is read only) */
235 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
236 chmod ( full_tty, 0600 );
238 change_identity ( pw );
239 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
242 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
244 if ( pw-> pw_uid == 0 )
245 syslog ( LOG_INFO, "root login %s\n", fromhost );
247 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
254 static const char *login_prompt ( void )
259 gethostname ( buf, sizeof( buf ));
260 printf ( "\nBusyBox on %s login: ", buf );
263 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
266 if ( !strchr ( buf, '\n' ));
269 for ( sp = buf; isspace ( *sp ); sp++ ) { }
270 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
277 static int check_nologin ( int amroot )
279 if ( access ( NOLOGIN_FILE, F_OK ) == 0 ) {
283 if (( fp = fopen ( NOLOGIN_FILE, "r" ))) {
284 while (( c = getc ( fp )) != EOF )
285 putchar (( c == '\n' ) ? '\r' : c );
290 puts ( "\r\nSystem closed for routine maintenance.\r" );
295 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
300 #ifdef CONFIG_FEATURE_SECURETTY
302 static int check_tty ( const char *tty )
308 if (( fp = fopen ( SECURETTY_FILE, "r" ))) {
309 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
310 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
311 if ( !isspace ( buf[i] ))
315 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
318 if ( strcmp ( buf, tty ) == 0 ) {
327 syslog ( LOG_WARNING, "cannot open securetty file.\n" );
334 /* returns 1 if true */
335 static int is_my_tty ( const char *tty )
337 struct stat by_name, by_fd;
339 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
342 if ( by_name. st_rdev != by_fd. st_rdev )
354 if (( fp = fopen ( MOTD_FILE, "r" ))) {
355 while (( c = getc ( fp )) != EOF )
362 // vv Taken from tinylogin utmp.c vv
364 #define _WTMP_FILE "/var/log/wtmp"
367 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
369 "Unable to determine your tty name."
372 * checkutmp - see if utmp file is correct for this process
374 * System V is very picky about the contents of the utmp file
375 * and requires that a slot for the current process exist.
376 * The utmp file is scanned for an entry with the same process
377 * ID. If no entry exists the process exits with a message.
379 * The "picky" flag is for network and other logins that may
380 * use special flags. It allows the pid checks to be overridden.
381 * This means that getty should never invoke login with any
382 * command line flags.
385 static void checkutmp(int picky)
389 pid_t pid = getpid();
393 /* First, try to find a valid utmp entry for this process. */
394 while ((ut = getutent()))
395 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
396 (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
399 /* If there is one, just use it, otherwise create a new one. */
412 if (strncmp(line, "/dev/", 5) == 0)
414 memset((void *) &utent, 0, sizeof utent);
415 utent.ut_type = LOGIN_PROCESS;
417 strncpy(utent.ut_line, line, sizeof utent.ut_line);
418 /* XXX - assumes /dev/tty?? */
419 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
420 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
421 time(&utent.ut_time);
425 #if __GNU_LIBRARY__ < 5
427 * Some systems already have updwtmp() and possibly updwtmpx(). Others
428 * don't, so we re-implement these functions if necessary. --marekm
430 static void updwtmp(const char *filename, const struct utmp *ut)
434 fd = open(filename, O_APPEND | O_WRONLY, 0);
436 write(fd, (const char *) ut, sizeof(*ut));
443 * setutmp - put a USER_PROCESS entry in the utmp file
445 * setutmp changes the type of the current utmp entry to
446 * USER_PROCESS. the wtmp file will be updated as well.
449 static void setutmp(const char *name, const char *line)
451 utent.ut_type = USER_PROCESS;
452 strncpy(utent.ut_user, name, sizeof utent.ut_user);
453 time(&utent.ut_time);
454 /* other fields already filled in by checkutmp above */
458 updwtmp(_WTMP_FILE, &utent);