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);
25 /* Stuff global to this file */
31 #define EMPTY_USERNAME_COUNT 10
32 #define USERNAME_SIZE 32
35 static int check_nologin ( int amroot );
37 #if defined CONFIG_FEATURE_SECURETTY
38 static int check_tty ( const char *tty );
41 static inline int check_tty ( const char *tty ) { return 1; }
45 static int is_my_tty ( const char *tty );
46 static int login_prompt ( char *buf_name );
47 static void motd ( void );
50 static void alarm_handler ( int sig )
52 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
53 exit ( EXIT_SUCCESS );
57 extern int login_main(int argc, char **argv)
62 char username[USERNAME_SIZE];
68 struct passwd *pw, pw_copy;
69 #ifdef CONFIG_WHEEL_GROUP
78 amroot = ( getuid ( ) == 0 );
79 signal ( SIGALRM, alarm_handler );
81 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
86 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
93 * username must be a seperate token
94 * (-f root, *NOT* -froot). --marekm
96 if ( optarg != argv[optind-1] )
99 if ( !amroot ) /* Auth bypass only if real UID is zero */
100 error_msg_and_die ( "-f permission denied" );
102 safe_strncpy(username, optarg, USERNAME_SIZE);
113 if (optind < argc) // user from command line (getty)
114 safe_strncpy(username, argv[optind], USERNAME_SIZE);
116 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
117 return EXIT_FAILURE; /* Must be a terminal */
119 #ifdef CONFIG_FEATURE_U_W_TMP
120 checkutmp ( !amroot );
124 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
125 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
127 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
129 #ifdef CONFIG_FEATURE_U_W_TMP
131 memset ( utent.ut_host, 0, sizeof utent.ut_host );
135 #ifdef CONFIG_FEATURE_U_W_TMP
136 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
138 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
141 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
145 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
151 if(!login_prompt ( username ))
154 if ( !alarmstarted && ( TIMEOUT > 0 )) {
159 if (!( pw = getpwnam ( username ))) {
160 pw_copy.pw_name = "UNKNOWN";
161 pw_copy.pw_passwd = "!";
169 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
177 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
180 /* Don't check the password if password entry is empty (!) */
181 if ( !pw-> pw_passwd[0] )
184 /* authorization takes place here */
185 if ( correct_password ( pw ))
199 while ( difftime ( now, start ) < FAIL_DELAY) {
200 sleep ( FAIL_DELAY );
205 puts("Login incorrect");
207 if ( ++count == 3 ) {
208 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
214 if ( check_nologin ( pw-> pw_uid == 0 ))
217 #ifdef CONFIG_FEATURE_U_W_TMP
218 setutmp ( username, tty );
221 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
223 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
225 if ( !is_my_tty ( full_tty ))
226 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
228 /* Try these, but don't complain if they fail
229 * (for example when the root fs is read only) */
230 chown ( full_tty, pw-> pw_uid, pw-> pw_gid );
231 chmod ( full_tty, 0600 );
233 change_identity ( pw );
234 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
237 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
239 if ( pw-> pw_uid == 0 )
240 syslog ( LOG_INFO, "root login %s\n", fromhost );
242 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
249 static int login_prompt ( char *buf_name )
255 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
256 gethostname ( buf, sizeof( buf ));
257 printf ( "\n%s login: ", buf );
260 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
263 if ( !strchr ( buf, '\n' ))
266 for ( sp = buf; isspace ( *sp ); sp++ ) { }
267 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
270 safe_strncpy(buf_name, sp, USERNAME_SIZE);
278 static int check_nologin ( int amroot )
280 if ( access ( nologin_file, F_OK ) == 0 ) {
284 if (( fp = fopen ( nologin_file, "r" ))) {
285 while (( c = getc ( fp )) != EOF )
286 putchar (( c == '\n' ) ? '\r' : c );
291 puts ( "\r\nSystem closed for routine maintenance.\r" );
296 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
301 #ifdef CONFIG_FEATURE_SECURETTY
303 static int check_tty ( const char *tty )
309 if (( fp = fopen ( securetty_file, "r" ))) {
310 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
311 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
312 if ( !isspace ( buf[i] ))
316 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
319 if ( strcmp ( buf, tty ) == 0 ) {
327 /* A missing securetty file is not an error. */
333 /* returns 1 if true */
334 static int is_my_tty ( const char *tty )
336 struct stat by_name, by_fd;
338 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
341 if ( by_name. st_rdev != by_fd. st_rdev )
353 if (( fp = fopen ( motd_file, "r" ))) {
354 while (( c = getc ( fp )) != EOF )
361 #ifdef CONFIG_FEATURE_U_W_TMP
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);
426 * setutmp - put a USER_PROCESS entry in the utmp file
428 * setutmp changes the type of the current utmp entry to
429 * USER_PROCESS. the wtmp file will be updated as well.
432 static void setutmp(const char *name, const char *line)
434 utent.ut_type = USER_PROCESS;
435 strncpy(utent.ut_user, name, sizeof utent.ut_user);
436 time(&utent.ut_time);
437 /* other fields already filled in by checkutmp above */
441 updwtmp(_WTMP_FILE, &utent);
443 #endif /* CONFIG_FEATURE_U_W_TMP */