blob: 3050c4c9cf2cdc7719b146d6fd859d066972e047 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
2#include <fcntl.h>
3#include <signal.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <syslog.h>
8#include <termios.h>
9#include <unistd.h>
10#include <utmp.h>
11#include <sys/resource.h>
12#include <sys/stat.h>
13#include <sys/time.h>
14#include <sys/types.h>
15#include <ctype.h>
16#include <time.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000017
Robert Griebl1fca5582002-06-04 20:45:46 +000018#include "busybox.h"
19
Robert Griebl1fca5582002-06-04 20:45:46 +000020
Eric Andersenfdfe2982002-10-10 03:55:09 +000021#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +000022// import from utmp.c
23static void checkutmp(int picky);
24static void setutmp(const char *name, const char *line);
Eric Andersen71ae64b2002-10-10 04:20:21 +000025/* Stuff global to this file */
26struct utmp utent;
Eric Andersenfdfe2982002-10-10 03:55:09 +000027#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000028
Robert Griebl1fca5582002-06-04 20:45:46 +000029// login defines
Robert Griebl1fca5582002-06-04 20:45:46 +000030#define TIMEOUT 60
Eric Andersen0fbff132002-06-22 17:49:29 +000031#define EMPTY_USERNAME_COUNT 10
Eric Andersen0fbff132002-06-22 17:49:29 +000032#define USERNAME_SIZE 32
33
Robert Griebl1fca5582002-06-04 20:45:46 +000034
35static int check_nologin ( int amroot );
36
37#if defined CONFIG_FEATURE_SECURETTY
38static int check_tty ( const char *tty );
39
40#else
41static inline int check_tty ( const char *tty ) { return 1; }
42
43#endif
44
45static int is_my_tty ( const char *tty );
Eric Andersen0fbff132002-06-22 17:49:29 +000046static int login_prompt ( char *buf_name );
Robert Griebl1fca5582002-06-04 20:45:46 +000047static void motd ( void );
Robert Griebl1fca5582002-06-04 20:45:46 +000048
49
50static void alarm_handler ( int sig )
51{
Eric Andersen0fbff132002-06-22 17:49:29 +000052 fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
Robert Griebl1fca5582002-06-04 20:45:46 +000053 exit ( EXIT_SUCCESS );
54}
55
56
57extern int login_main(int argc, char **argv)
58{
59 char tty[BUFSIZ];
60 char full_tty[200];
61 char fromhost[512];
Eric Andersen0fbff132002-06-22 17:49:29 +000062 char username[USERNAME_SIZE];
Robert Griebl1fca5582002-06-04 20:45:46 +000063 char *tmp;
64 int amroot;
65 int flag;
66 int failed;
67 int count=0;
68 struct passwd *pw, pw_copy;
Eric Andersen27f64e12002-06-23 04:24:25 +000069#ifdef CONFIG_WHEEL_GROUP
70 struct group *grp;
71#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000072 int opt_preserve = 0;
73 int opt_fflag = 0;
74 char *opt_host = 0;
Robert Griebl1fca5582002-06-04 20:45:46 +000075 int alarmstarted = 0;
76
Eric Andersen0fbff132002-06-22 17:49:29 +000077 username[0]=0;
Robert Griebl1fca5582002-06-04 20:45:46 +000078 amroot = ( getuid ( ) == 0 );
79 signal ( SIGALRM, alarm_handler );
80
81 if (( argc > 1 ) && ( TIMEOUT > 0 )) {
82 alarm ( TIMEOUT );
83 alarmstarted = 1;
84 }
85
86 while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) {
87 switch ( flag ) {
88 case 'p':
Robert Griebl1fca5582002-06-04 20:45:46 +000089 opt_preserve = 1;
90 break;
91 case 'f':
92 /*
93 * username must be a seperate token
94 * (-f root, *NOT* -froot). --marekm
95 */
96 if ( optarg != argv[optind-1] )
97 show_usage ( );
98
99 if ( !amroot ) /* Auth bypass only if real UID is zero */
Eric Andersen0fbff132002-06-22 17:49:29 +0000100 error_msg_and_die ( "-f permission denied" );
Robert Griebl1fca5582002-06-04 20:45:46 +0000101
Eric Andersen0fbff132002-06-22 17:49:29 +0000102 safe_strncpy(username, optarg, USERNAME_SIZE);
Robert Griebl1fca5582002-06-04 20:45:46 +0000103 opt_fflag = 1;
104 break;
105 case 'h':
106 opt_host = optarg;
107 break;
108 default:
109 show_usage ( );
110 }
111 }
112
Eric Andersen0fbff132002-06-22 17:49:29 +0000113 if (optind < argc) // user from command line (getty)
114 safe_strncpy(username, argv[optind], USERNAME_SIZE);
Robert Griebl1fca5582002-06-04 20:45:46 +0000115
116 if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 ))
117 return EXIT_FAILURE; /* Must be a terminal */
118
Eric Andersenfdfe2982002-10-10 03:55:09 +0000119#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +0000120 checkutmp ( !amroot );
Eric Andersenfdfe2982002-10-10 03:55:09 +0000121#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000122
123 tmp = ttyname ( 0 );
124 if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 ))
125 safe_strncpy ( tty, tmp + 5, sizeof( tty ));
126 else
127 safe_strncpy ( tty, "UNKNOWN", sizeof( tty ));
128
Eric Andersen71ae64b2002-10-10 04:20:21 +0000129#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +0000130 if ( amroot )
131 memset ( utent.ut_host, 0, sizeof utent.ut_host );
Eric Andersen71ae64b2002-10-10 04:20:21 +0000132#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000133
134 if ( opt_host ) {
Eric Andersen71ae64b2002-10-10 04:20:21 +0000135#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +0000136 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
Eric Andersen71ae64b2002-10-10 04:20:21 +0000137#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000138 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host );
139 }
140 else
141 snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty );
142
Eric Andersen0fbff132002-06-22 17:49:29 +0000143 setpgrp();
144
Robert Griebl1fca5582002-06-04 20:45:46 +0000145 openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
146
147 while ( 1 ) {
148 failed = 0;
149
Eric Andersen0fbff132002-06-22 17:49:29 +0000150 if ( !username[0] )
151 if(!login_prompt ( username ))
152 return EXIT_FAILURE;
Robert Griebl1fca5582002-06-04 20:45:46 +0000153
154 if ( !alarmstarted && ( TIMEOUT > 0 )) {
155 alarm ( TIMEOUT );
156 alarmstarted = 1;
157 }
158
159 if (!( pw = getpwnam ( username ))) {
Eric Andersen0fbff132002-06-22 17:49:29 +0000160 pw_copy.pw_name = "UNKNOWN";
161 pw_copy.pw_passwd = "!";
Robert Griebl1fca5582002-06-04 20:45:46 +0000162 opt_fflag = 0;
163 failed = 1;
164 } else
165 pw_copy = *pw;
166
167 pw = &pw_copy;
168
169 if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' ))
170 failed = 1;
171
172 if ( opt_fflag ) {
173 opt_fflag = 0;
174 goto auth_ok;
175 }
176
Eric Andersen0fbff132002-06-22 17:49:29 +0000177 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
Robert Griebl1fca5582002-06-04 20:45:46 +0000178 failed = 1;
179
180 /* Don't check the password if password entry is empty (!) */
181 if ( !pw-> pw_passwd[0] )
182 goto auth_ok;
183
184 /* authorization takes place here */
185 if ( correct_password ( pw ))
186 goto auth_ok;
187
Robert Griebl1fca5582002-06-04 20:45:46 +0000188 failed = 1;
189
190auth_ok:
191 if ( !failed)
192 break;
193
194 { // delay next try
195 time_t start, now;
196
197 time ( &start );
198 now = start;
199 while ( difftime ( now, start ) < FAIL_DELAY) {
200 sleep ( FAIL_DELAY );
201 time ( &now );
202 }
203 }
204
205 puts("Login incorrect");
Eric Andersen0fbff132002-06-22 17:49:29 +0000206 username[0] = 0;
207 if ( ++count == 3 ) {
208 syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
Robert Griebl1fca5582002-06-04 20:45:46 +0000209 return EXIT_FAILURE;
210 }
Eric Andersen0fbff132002-06-22 17:49:29 +0000211 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000212
213 alarm ( 0 );
214 if ( check_nologin ( pw-> pw_uid == 0 ))
215 return EXIT_FAILURE;
216
Eric Andersenfdfe2982002-10-10 03:55:09 +0000217#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +0000218 setutmp ( username, tty );
Eric Andersenfdfe2982002-10-10 03:55:09 +0000219#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000220 if ( *tty != '/' )
221 snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty);
222 else
223 safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 );
224
225 if ( !is_my_tty ( full_tty ))
226 syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty );
227
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 );
232
233 change_identity ( pw );
234 setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw );
235
236 motd ( );
237 signal ( SIGALRM, SIG_DFL ); /* default alarm signal */
238
239 if ( pw-> pw_uid == 0 )
240 syslog ( LOG_INFO, "root login %s\n", fromhost );
241
242 run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */
243
244 return EXIT_FAILURE;
245}
246
247
248
Eric Andersen0fbff132002-06-22 17:49:29 +0000249static int login_prompt ( char *buf_name )
Robert Griebl1fca5582002-06-04 20:45:46 +0000250{
251 char buf [1024];
252 char *sp, *ep;
Eric Andersen0fbff132002-06-22 17:49:29 +0000253 int i;
Robert Griebl1fca5582002-06-04 20:45:46 +0000254
Eric Andersen0fbff132002-06-22 17:49:29 +0000255 for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000256 gethostname ( buf, sizeof( buf ));
Eric Andersenb26674b2002-11-07 02:45:55 +0000257 printf ( "\n%s login: ", buf );
Robert Griebl1fca5582002-06-04 20:45:46 +0000258 fflush ( stdout );
259
260 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
261 return 0;
262
Eric Andersen0fbff132002-06-22 17:49:29 +0000263 if ( !strchr ( buf, '\n' ))
Robert Griebl1fca5582002-06-04 20:45:46 +0000264 return 0;
265
266 for ( sp = buf; isspace ( *sp ); sp++ ) { }
267 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
268
269 *ep = 0;
Eric Andersen0fbff132002-06-22 17:49:29 +0000270 safe_strncpy(buf_name, sp, USERNAME_SIZE);
271 if(buf_name[0])
272 return 1;
273 }
274 return 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000275}
276
277
278static int check_nologin ( int amroot )
279{
Eric Andersen27f64e12002-06-23 04:24:25 +0000280 if ( access ( nologin_file, F_OK ) == 0 ) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000281 FILE *fp;
282 int c;
283
Eric Andersen27f64e12002-06-23 04:24:25 +0000284 if (( fp = fopen ( nologin_file, "r" ))) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000285 while (( c = getc ( fp )) != EOF )
286 putchar (( c == '\n' ) ? '\r' : c );
287
288 fflush ( stdout );
289 fclose ( fp );
290 } else {
291 puts ( "\r\nSystem closed for routine maintenance.\r" );
292 }
293 if ( !amroot )
294 return 1;
295
296 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
297 }
298 return 0;
299}
300
301#ifdef CONFIG_FEATURE_SECURETTY
302
303static int check_tty ( const char *tty )
304{
305 FILE *fp;
306 int i;
307 char buf[BUFSIZ];
308
Eric Andersen27f64e12002-06-23 04:24:25 +0000309 if (( fp = fopen ( securetty_file, "r" ))) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000310 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
311 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
312 if ( !isspace ( buf[i] ))
313 break;
314 }
315 buf[++i] = '\0';
316 if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
317 continue;
318
319 if ( strcmp ( buf, tty ) == 0 ) {
320 fclose ( fp );
321 return 1;
322 }
323 }
324 fclose(fp);
325 return 0;
326 }
Eric Andersenf8701482002-11-14 10:58:17 +0000327 /* A missing securetty file is not an error. */
328 return 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000329}
330
331#endif
332
333/* returns 1 if true */
334static int is_my_tty ( const char *tty )
335{
336 struct stat by_name, by_fd;
337
338 if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
339 return 0;
340
341 if ( by_name. st_rdev != by_fd. st_rdev )
342 return 0;
343 else
344 return 1;
345}
346
347
348static void motd ( )
349{
350 FILE *fp;
351 register int c;
352
Eric Andersen27f64e12002-06-23 04:24:25 +0000353 if (( fp = fopen ( motd_file, "r" ))) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000354 while (( c = getc ( fp )) != EOF )
355 putchar ( c );
356 fclose ( fp );
357 }
358}
359
360
Eric Andersenfdfe2982002-10-10 03:55:09 +0000361#ifdef CONFIG_FEATURE_U_W_TMP
Robert Griebl1fca5582002-06-04 20:45:46 +0000362// vv Taken from tinylogin utmp.c vv
363
364#define _WTMP_FILE "/var/log/wtmp"
365
366#define NO_UTENT \
367 "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
368#define NO_TTY \
369 "Unable to determine your tty name."
370
371/*
372 * checkutmp - see if utmp file is correct for this process
373 *
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.
378 *
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.
383 */
384
385static void checkutmp(int picky)
386{
387 char *line;
388 struct utmp *ut;
389 pid_t pid = getpid();
390
391 setutent();
392
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))
397 break;
398
399 /* If there is one, just use it, otherwise create a new one. */
400 if (ut) {
401 utent = *ut;
402 } else {
403 if (picky) {
404 puts(NO_UTENT);
405 exit(1);
406 }
407 line = ttyname(0);
408 if (!line) {
409 puts(NO_TTY);
410 exit(1);
411 }
412 if (strncmp(line, "/dev/", 5) == 0)
413 line += 5;
414 memset((void *) &utent, 0, sizeof utent);
415 utent.ut_type = LOGIN_PROCESS;
416 utent.ut_pid = pid;
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);
422 }
423}
424
Robert Griebl1fca5582002-06-04 20:45:46 +0000425/*
426 * setutmp - put a USER_PROCESS entry in the utmp file
427 *
428 * setutmp changes the type of the current utmp entry to
429 * USER_PROCESS. the wtmp file will be updated as well.
430 */
431
432static void setutmp(const char *name, const char *line)
433{
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 */
438 setutent();
439 pututline(&utent);
440 endutent();
441 updwtmp(_WTMP_FILE, &utent);
442}
Eric Andersenfdfe2982002-10-10 03:55:09 +0000443#endif /* CONFIG_FEATURE_U_W_TMP */