hehe. Screwed that one up. Lets do things right this time.
[oweals/busybox.git] / loginutils / login.c
1 /* 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>
17
18 #include "busybox.h"
19
20
21 #ifdef CONFIG_FEATURE_U_W_TMP
22 // import from utmp.c
23 static void checkutmp(int picky);
24 static void setutmp(const char *name, const char *line);
25 /* Stuff global to this file */
26 struct utmp utent;
27 #endif
28
29 // login defines
30 #define TIMEOUT       60
31 #define EMPTY_USERNAME_COUNT    10
32 #define USERNAME_SIZE 32
33
34
35 static int check_nologin ( int amroot );
36
37 #if defined CONFIG_FEATURE_SECURETTY
38 static int check_tty ( const char *tty );
39
40 #else
41 static inline int check_tty ( const char *tty )  { return 1; }
42
43 #endif
44
45 static int is_my_tty ( const char *tty );
46 static int login_prompt ( char *buf_name );
47 static void motd ( void );
48
49
50 static void alarm_handler ( int sig )
51 {
52         fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT );
53         exit ( EXIT_SUCCESS );
54 }
55
56
57 extern int login_main(int argc, char **argv)
58 {
59         char tty[BUFSIZ];
60         char full_tty[200];
61         char fromhost[512];
62         char username[USERNAME_SIZE];
63         char *tmp;
64         int amroot;
65         int flag;
66         int failed;
67         int count=0;
68         struct passwd *pw, pw_copy;
69 #ifdef CONFIG_WHEEL_GROUP
70         struct group *grp;
71 #endif
72         int opt_preserve = 0;
73         int opt_fflag = 0;
74         char *opt_host = 0;
75         int alarmstarted = 0;   
76
77         username[0]=0;
78         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':
89                         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 */
100                                 error_msg_and_die ( "-f permission denied" );
101                         
102                         safe_strncpy(username, optarg, USERNAME_SIZE);
103                         opt_fflag = 1;
104                         break;
105                 case 'h':
106                         opt_host = optarg;
107                         break;
108                 default:
109                         show_usage ( );
110                 }
111         }
112
113         if (optind < argc)             // user from command line (getty)
114                 safe_strncpy(username, argv[optind], USERNAME_SIZE);
115
116         if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 )) 
117                 return EXIT_FAILURE;            /* Must be a terminal */
118
119 #ifdef CONFIG_FEATURE_U_W_TMP
120         checkutmp ( !amroot );
121 #endif
122
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
129 #ifdef CONFIG_FEATURE_U_W_TMP
130         if ( amroot )
131                 memset ( utent.ut_host, 0, sizeof utent.ut_host );
132 #endif
133         
134         if ( opt_host ) {
135 #ifdef CONFIG_FEATURE_U_W_TMP
136                 safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host ));
137 #endif
138                 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         
143         setpgrp();
144
145         openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH );
146
147         while ( 1 ) {
148                 failed = 0;
149
150                 if ( !username[0] )
151                         if(!login_prompt ( username ))
152                                 return EXIT_FAILURE;
153
154                 if ( !alarmstarted && ( TIMEOUT > 0 )) {
155                         alarm ( TIMEOUT );
156                         alarmstarted = 1;
157                 }
158
159                 if (!( pw = getpwnam ( username ))) {
160                         pw_copy.pw_name   = "UNKNOWN";
161                         pw_copy.pw_passwd = "!";
162                         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
177                 if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty )))
178                         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
188                 failed = 1;
189                 
190 auth_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");
206                 username[0] = 0;
207                 if ( ++count == 3 ) {
208                         syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost);
209                         return EXIT_FAILURE;
210         }
211         }
212                 
213         alarm ( 0 );
214         if ( check_nologin ( pw-> pw_uid == 0 ))
215                 return EXIT_FAILURE;
216
217 #ifdef CONFIG_FEATURE_U_W_TMP
218         setutmp ( username, tty );
219 #endif
220         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
249 static int login_prompt ( char *buf_name )
250 {
251         char buf [1024];
252         char *sp, *ep;
253         int i;
254
255         for(i=0; i<EMPTY_USERNAME_COUNT; i++) {
256         gethostname ( buf, sizeof( buf ));
257         printf ( "\n%s login: ", buf );
258         fflush ( stdout );
259
260         if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
261                 return 0;
262                 
263                 if ( !strchr ( buf, '\n' ))
264                 return 0;
265         
266         for ( sp = buf; isspace ( *sp ); sp++ ) { }
267         for ( ep = sp; isgraph ( *ep ); ep++ ) { }
268
269         *ep = 0;                
270                 safe_strncpy(buf_name, sp, USERNAME_SIZE);
271                 if(buf_name[0])
272                         return 1;
273         }
274         return 0;
275 }
276
277
278 static int check_nologin ( int amroot )
279 {
280         if ( access ( nologin_file, F_OK ) == 0 ) {
281                 FILE *fp;
282                 int c;
283
284                 if (( fp = fopen ( nologin_file, "r" ))) {
285                         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
303 static int check_tty ( const char *tty )
304 {
305         FILE *fp;
306         int i;
307         char buf[BUFSIZ];
308
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] ))
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         }
327         /* A missing securetty file is not an error. */
328         return 1;
329 }
330
331 #endif
332
333 /* returns 1 if true */
334 static 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
348 static void motd ( )
349 {
350         FILE *fp;
351         register int c;
352
353         if (( fp = fopen ( motd_file, "r" ))) {
354                 while (( c = getc ( fp )) != EOF ) 
355                         putchar ( c );          
356                 fclose ( fp );
357         }
358 }
359
360
361 #ifdef CONFIG_FEATURE_U_W_TMP
362 // 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
385 static 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
425 /*
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
432 static 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 }
443 #endif /* CONFIG_FEATURE_U_W_TMP */