c1ea165c853911a506ba478fdbfefaa43e3dbc89
[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                 print_login_prompt();
257
258                 if ( !fgets ( buf, sizeof( buf ) - 1, stdin ))
259                         return 0;
260
261                 if ( !strchr ( buf, '\n' ))
262                         return 0;
263
264                 for ( sp = buf; isspace ( *sp ); sp++ ) { }
265                 for ( ep = sp; isgraph ( *ep ); ep++ ) { }
266
267                 *ep = 0;                
268                 safe_strncpy(buf_name, sp, USERNAME_SIZE);
269                 if(buf_name[0])
270                         return 1;
271         }
272         return 0;
273 }
274
275
276 static int check_nologin ( int amroot )
277 {
278         if ( access ( nologin_file, F_OK ) == 0 ) {
279                 FILE *fp;
280                 int c;
281
282                 if (( fp = fopen ( nologin_file, "r" ))) {
283                         while (( c = getc ( fp )) != EOF )
284                                 putchar (( c == '\n' ) ? '\r' : c );
285
286                         fflush ( stdout );
287                         fclose ( fp );
288                 } else {
289                         puts ( "\r\nSystem closed for routine maintenance.\r" );
290                 }
291                 if ( !amroot )
292                         return 1;
293                         
294                 puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" );
295         }
296         return 0;
297 }
298
299 #ifdef CONFIG_FEATURE_SECURETTY
300
301 static int check_tty ( const char *tty )
302 {
303         FILE *fp;
304         int i;
305         char buf[BUFSIZ];
306
307         if (( fp = fopen ( securetty_file, "r" ))) {
308                 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
309                         for ( i = xstrlen( buf ) - 1; i >= 0; --i ) {
310                                 if ( !isspace ( buf[i] ))
311                                         break;
312                         }
313                         buf[++i] = '\0';
314                         if (( buf [0] == '\0' ) || ( buf [0] == '#' ))
315                                 continue;
316
317                         if ( strcmp ( buf, tty ) == 0 ) {
318                                 fclose ( fp );
319                                 return 1;
320                         }
321                 }
322                 fclose(fp);
323                 return 0;
324         }
325         /* A missing securetty file is not an error. */
326         return 1;
327 }
328
329 #endif
330
331 /* returns 1 if true */
332 static int is_my_tty ( const char *tty )
333 {
334         struct stat by_name, by_fd;
335
336         if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd ))
337                 return 0;
338                 
339         if ( by_name. st_rdev != by_fd. st_rdev )
340                 return 0;
341         else
342                 return 1;
343 }
344
345
346 static void motd ( )
347 {
348         FILE *fp;
349         register int c;
350
351         if (( fp = fopen ( motd_file, "r" ))) {
352                 while (( c = getc ( fp )) != EOF ) 
353                         putchar ( c );          
354                 fclose ( fp );
355         }
356 }
357
358
359 #ifdef CONFIG_FEATURE_U_W_TMP
360 // vv  Taken from tinylogin utmp.c  vv
361
362 #define _WTMP_FILE "/var/log/wtmp"
363
364 #define NO_UTENT \
365         "No utmp entry.  You must exec \"login\" from the lowest level \"sh\""
366 #define NO_TTY \
367         "Unable to determine your tty name."
368
369 /*
370  * checkutmp - see if utmp file is correct for this process
371  *
372  *      System V is very picky about the contents of the utmp file
373  *      and requires that a slot for the current process exist.
374  *      The utmp file is scanned for an entry with the same process
375  *      ID.  If no entry exists the process exits with a message.
376  *
377  *      The "picky" flag is for network and other logins that may
378  *      use special flags.  It allows the pid checks to be overridden.
379  *      This means that getty should never invoke login with any
380  *      command line flags.
381  */
382
383 static void checkutmp(int picky)
384 {
385         char *line;
386         struct utmp *ut;
387         pid_t pid = getpid();
388
389         setutent();
390
391         /* First, try to find a valid utmp entry for this process.  */
392         while ((ut = getutent()))
393                 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] &&
394                         (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS))
395                         break;
396
397         /* If there is one, just use it, otherwise create a new one.  */
398         if (ut) {
399                 utent = *ut;
400         } else {
401                 if (picky) {
402                         puts(NO_UTENT);
403                         exit(1);
404                 }
405                 line = ttyname(0);
406                 if (!line) {
407                         puts(NO_TTY);
408                         exit(1);
409                 }
410                 if (strncmp(line, "/dev/", 5) == 0)
411                         line += 5;
412                 memset((void *) &utent, 0, sizeof utent);
413                 utent.ut_type = LOGIN_PROCESS;
414                 utent.ut_pid = pid;
415                 strncpy(utent.ut_line, line, sizeof utent.ut_line);
416                 /* XXX - assumes /dev/tty?? */
417                 strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
418                 strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user);
419                 time(&utent.ut_time);
420         }
421 }
422
423 /*
424  * setutmp - put a USER_PROCESS entry in the utmp file
425  *
426  *      setutmp changes the type of the current utmp entry to
427  *      USER_PROCESS.  the wtmp file will be updated as well.
428  */
429
430 static void setutmp(const char *name, const char *line)
431 {
432         utent.ut_type = USER_PROCESS;
433         strncpy(utent.ut_user, name, sizeof utent.ut_user);
434         time(&utent.ut_time);
435         /* other fields already filled in by checkutmp above */
436         setutent();
437         pututline(&utent);
438         endutent();
439         updwtmp(_WTMP_FILE, &utent);
440 }
441 #endif /* CONFIG_FEATURE_U_W_TMP */