]> git.ozlabs.org Git - ppp.git/blob - pppd/auth.c
connect time limit stuff moved here,
[ppp.git] / pppd / auth.c
1 /*
2  * auth.c - PPP authentication and phase control.
3  *
4  * Copyright (c) 1993 The Australian National University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by the Australian National University.  The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Copyright (c) 1989 Carnegie Mellon University.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that the above copyright notice and this paragraph are
24  * duplicated in all such forms and that any documentation,
25  * advertising materials, and other materials related to such
26  * distribution and use acknowledge that the software was developed
27  * by Carnegie Mellon University.  The name of the
28  * University may not be used to endorse or promote products derived
29  * from this software without specific prior written permission.
30  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33  */
34
35 #ifndef lint
36 static char rcsid[] = "$Id: auth.c,v 1.30 1997/03/04 03:37:21 paulus Exp $";
37 #endif
38
39 #include <stdio.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <syslog.h>
44 #include <pwd.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/socket.h>
49
50 #include <netdb.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53
54 #if defined(SVR4) || defined(_linux_)
55 #include <crypt.h>
56 #else
57 #if defined(SUNOS4) || defined(ULTRIX)
58 extern char *crypt();
59 #endif
60 #endif
61
62 #ifdef USE_PAM
63 #include <security/pam_appl.h>
64 #include <security/pam_modules.h>
65 #endif
66
67 #ifdef HAS_SHADOW
68 #include <shadow.h>
69 #include <shadow/pwauth.h>
70 #ifndef PW_PPP
71 #define PW_PPP PW_LOGIN
72 #endif
73 #endif
74
75 #include "pppd.h"
76 #include "fsm.h"
77 #include "lcp.h"
78 #include "ipcp.h"
79 #include "upap.h"
80 #include "chap.h"
81 #ifdef CBCP_SUPPORT
82 #include "cbcp.h"
83 #endif
84 #include "pathnames.h"
85
86 /* Used for storing a sequence of words.  Usually malloced. */
87 struct wordlist {
88     struct wordlist     *next;
89     char                word[1];
90 };
91
92 /* Bits in scan_authfile return value */
93 #define NONWILD_SERVER  1
94 #define NONWILD_CLIENT  2
95
96 #define ISWILD(word)    (word[0] == '*' && word[1] == 0)
97
98 #define FALSE   0
99 #define TRUE    1
100
101 /* The name by which the peer authenticated itself to us. */
102 char peer_authname[MAXNAMELEN];
103
104 /* Records which authentication operations haven't completed yet. */
105 static int auth_pending[NUM_PPP];
106
107 /* Set if we have successfully called login() */
108 static int logged_in;
109
110 /* Set if we have run the /etc/ppp/auth-up script. */
111 static int did_authup;
112
113 /* List of addresses which the peer may use. */
114 static struct wordlist *addresses[NUM_PPP];
115
116 /* Number of network protocols which we have opened. */
117 static int num_np_open;
118
119 /* Number of network protocols which have come up. */
120 static int num_np_up;
121
122 /* Set if we got the contents of passwd[] from the pap-secrets file. */
123 static int passwd_from_file;
124
125 /* Bits in auth_pending[] */
126 #define PAP_WITHPEER    1
127 #define PAP_PEER        2
128 #define CHAP_WITHPEER   4
129 #define CHAP_PEER       8
130
131 /* Prototypes for procedures local to this file. */
132
133 static void network_phase __P((int));
134 static void check_idle __P((caddr_t));
135 static void connect_time_expired __P((caddr_t));
136 static int  login __P((char *, char *, char **, int *));
137 static void logout __P((void));
138 static int  null_login __P((int));
139 static int  get_pap_passwd __P((char *));
140 static int  have_pap_secret __P((void));
141 static int  have_chap_secret __P((char *, char *, u_int32_t));
142 static int  ip_addr_check __P((u_int32_t, struct wordlist *));
143 static int  scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
144                                struct wordlist **, char *));
145 static void free_wordlist __P((struct wordlist *));
146 static void auth_script __P((char *));
147 static void set_allowed_addrs __P((int, struct wordlist *));
148 #ifdef CBCP_SUPPORT
149 static void callback_phase __P((int));
150 #endif
151
152 /*
153  * An Open on LCP has requested a change from Dead to Establish phase.
154  * Do what's necessary to bring the physical layer up.
155  */
156 void
157 link_required(unit)
158     int unit;
159 {
160 }
161
162 /*
163  * LCP has terminated the link; go to the Dead phase and take the
164  * physical layer down.
165  */
166 void
167 link_terminated(unit)
168     int unit;
169 {
170     if (phase == PHASE_DEAD)
171         return;
172     if (logged_in)
173         logout();
174     phase = PHASE_DEAD;
175     syslog(LOG_NOTICE, "Connection terminated.");
176 }
177
178 /*
179  * LCP has gone down; it will either die or try to re-establish.
180  */
181 void
182 link_down(unit)
183     int unit;
184 {
185     int i;
186     struct protent *protp;
187
188     if (did_authup) {
189         auth_script(_PATH_AUTHDOWN);
190         did_authup = 0;
191     }
192     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
193         if (!protp->enabled_flag)
194             continue;
195         if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
196             (*protp->lowerdown)(unit);
197         if (protp->protocol < 0xC000 && protp->close != NULL)
198             (*protp->close)(unit, "LCP down");
199     }
200     num_np_open = 0;
201     num_np_up = 0;
202     if (phase != PHASE_DEAD)
203         phase = PHASE_TERMINATE;
204 }
205
206 /*
207  * The link is established.
208  * Proceed to the Dead, Authenticate or Network phase as appropriate.
209  */
210 void
211 link_established(unit)
212     int unit;
213 {
214     int auth;
215     lcp_options *wo = &lcp_wantoptions[unit];
216     lcp_options *go = &lcp_gotoptions[unit];
217     lcp_options *ho = &lcp_hisoptions[unit];
218     int i;
219     struct protent *protp;
220
221     /*
222      * Tell higher-level protocols that LCP is up.
223      */
224     for (i = 0; (protp = protocols[i]) != NULL; ++i)
225         if (protp->protocol != PPP_LCP && protp->enabled_flag
226             && protp->lowerup != NULL)
227             (*protp->lowerup)(unit);
228
229     if (auth_required && !(go->neg_chap || go->neg_upap)) {
230         /*
231          * We wanted the peer to authenticate itself, and it refused:
232          * treat it as though it authenticated with PAP using a username
233          * of "" and a password of "".  If that's not OK, boot it out.
234          */
235         if (!wo->neg_upap || !null_login(unit)) {
236             syslog(LOG_WARNING, "peer refused to authenticate");
237             lcp_close(unit, "peer refused to authenticate");
238             return;
239         }
240     }
241
242     phase = PHASE_AUTHENTICATE;
243     auth = 0;
244     if (go->neg_chap) {
245         ChapAuthPeer(unit, our_name, go->chap_mdtype);
246         auth |= CHAP_PEER;
247     } else if (go->neg_upap) {
248         upap_authpeer(unit);
249         auth |= PAP_PEER;
250     }
251     if (ho->neg_chap) {
252         ChapAuthWithPeer(unit, user, ho->chap_mdtype);
253         auth |= CHAP_WITHPEER;
254     } else if (ho->neg_upap) {
255         if (passwd[0] == 0) {
256             passwd_from_file = 1;
257             if (!get_pap_passwd(passwd))
258                 syslog(LOG_ERR, "No secret found for PAP login");
259         }
260         upap_authwithpeer(unit, user, passwd);
261         auth |= PAP_WITHPEER;
262     }
263     auth_pending[unit] = auth;
264
265     if (!auth)
266         network_phase(unit);
267 }
268
269 /*
270  * Proceed to the network phase.
271  */
272 static void
273 network_phase(unit)
274     int unit;
275 {
276     int i;
277     struct protent *protp;
278     lcp_options *go = &lcp_gotoptions[unit];
279
280     /*
281      * If the peer had to authenticate, run the auth-up script now.
282      */
283     if ((go->neg_chap || go->neg_upap) && !did_authup) {
284         auth_script(_PATH_AUTHUP);
285         did_authup = 1;
286     }
287
288 #ifdef CBCP_SUPPORT
289     /*
290      * If we negotiated callback, do it now.
291      */
292     if (go->neg_cbcp) {
293         phase = PHASE_CALLBACK;
294         (*cbcp_protent.open)(unit);
295         return;
296     }
297 #endif
298
299     phase = PHASE_NETWORK;
300 #if 0
301     if (!demand)
302         set_filters(&pass_filter, &active_filter);
303 #endif
304     for (i = 0; (protp = protocols[i]) != NULL; ++i)
305         if (protp->protocol < 0xC000 && protp->enabled_flag
306             && protp->open != NULL) {
307             (*protp->open)(unit);
308             if (protp->protocol != PPP_CCP)
309                 ++num_np_open;
310         }
311 }
312
313 /*
314  * The peer has failed to authenticate himself using `protocol'.
315  */
316 void
317 auth_peer_fail(unit, protocol)
318     int unit, protocol;
319 {
320     /*
321      * Authentication failure: take the link down
322      */
323     lcp_close(unit, "Authentication failed");
324 }
325
326 /*
327  * The peer has been successfully authenticated using `protocol'.
328  */
329 void
330 auth_peer_success(unit, protocol, name, namelen)
331     int unit, protocol;
332     char *name;
333     int namelen;
334 {
335     int bit;
336
337     switch (protocol) {
338     case PPP_CHAP:
339         bit = CHAP_PEER;
340         break;
341     case PPP_PAP:
342         bit = PAP_PEER;
343         break;
344     default:
345         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
346                protocol);
347         return;
348     }
349
350     /*
351      * Save the authenticated name of the peer for later.
352      */
353     if (namelen > sizeof(peer_authname) - 1)
354         namelen = sizeof(peer_authname) - 1;
355     BCOPY(name, peer_authname, namelen);
356     peer_authname[namelen] = 0;
357
358     /*
359      * If there is no more authentication still to be done,
360      * proceed to the network (or callback) phase.
361      */
362     if ((auth_pending[unit] &= ~bit) == 0)
363         network_phase(unit);
364 }
365
366 /*
367  * We have failed to authenticate ourselves to the peer using `protocol'.
368  */
369 void
370 auth_withpeer_fail(unit, protocol)
371     int unit, protocol;
372 {
373     if (passwd_from_file)
374         BZERO(passwd, MAXSECRETLEN);
375     /*
376      * We've failed to authenticate ourselves to our peer.
377      * He'll probably take the link down, and there's not much
378      * we can do except wait for that.
379      */
380 }
381
382 /*
383  * We have successfully authenticated ourselves with the peer using `protocol'.
384  */
385 void
386 auth_withpeer_success(unit, protocol)
387     int unit, protocol;
388 {
389     int bit;
390
391     switch (protocol) {
392     case PPP_CHAP:
393         bit = CHAP_WITHPEER;
394         break;
395     case PPP_PAP:
396         if (passwd_from_file)
397             BZERO(passwd, MAXSECRETLEN);
398         bit = PAP_WITHPEER;
399         break;
400     default:
401         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
402                protocol);
403         bit = 0;
404     }
405
406     /*
407      * If there is no more authentication still being done,
408      * proceed to the network (or callback) phase.
409      */
410     if ((auth_pending[unit] &= ~bit) == 0)
411         network_phase(unit);
412 }
413
414
415 /*
416  * np_up - a network protocol has come up.
417  */
418 void
419 np_up(unit, proto)
420     int unit, proto;
421 {
422     if (num_np_up == 0 && idle_time_limit > 0) {
423         TIMEOUT(check_idle, NULL, idle_time_limit);
424
425         /*
426          * Set a timeout to close the connection once the maximum
427          * connect time has expired.
428          */
429         if (maxconnect > 0)
430             TIMEOUT(connect_time_expired, 0, maxconnect);
431     }
432     ++num_np_up;
433 }
434
435 /*
436  * np_down - a network protocol has gone down.
437  */
438 void
439 np_down(unit, proto)
440     int unit, proto;
441 {
442     if (--num_np_up == 0 && idle_time_limit > 0) {
443         UNTIMEOUT(check_idle, NULL);
444     }
445 }
446
447 /*
448  * np_finished - a network protocol has finished using the link.
449  */
450 void
451 np_finished(unit, proto)
452     int unit, proto;
453 {
454     if (--num_np_open <= 0) {
455         /* no further use for the link: shut up shop. */
456         lcp_close(0, "No network protocols running");
457     }
458 }
459
460 /*
461  * check_idle - check whether the link has been idle for long
462  * enough that we can shut it down.
463  */
464 static void
465 check_idle(arg)
466     caddr_t arg;
467 {
468     struct ppp_idle idle;
469     time_t itime;
470
471     if (!get_idle_time(0, &idle))
472         return;
473     itime = MIN(idle.xmit_idle, idle.recv_idle);
474     if (itime >= idle_time_limit) {
475         /* link is idle: shut it down. */
476         syslog(LOG_INFO, "Terminating connection due to lack of activity.");
477         need_holdoff = 0;
478         lcp_close(0, "Link inactive");
479     } else {
480         TIMEOUT(check_idle, NULL, idle_time_limit - itime);
481     }
482 }
483
484 /*
485  * connect_time_expired - log a message and close the connection.
486  */
487 static void
488 connect_time_expired(arg)
489     caddr_t arg;
490 {
491     syslog(LOG_INFO, "Connect time expired");
492     lcp_close(0, "Connect time expired");       /* Close connection */
493 }
494
495 /*
496  * auth_check_options - called to check authentication options.
497  */
498 void
499 auth_check_options()
500 {
501     lcp_options *wo = &lcp_wantoptions[0];
502     int can_auth;
503     ipcp_options *ipwo = &ipcp_wantoptions[0];
504     u_int32_t remote;
505
506     /* Default our_name to hostname, and user to our_name */
507     if (our_name[0] == 0 || usehostname)
508         strcpy(our_name, hostname);
509     if (user[0] == 0)
510         strcpy(user, our_name);
511
512     /* If authentication is required, ask peer for CHAP or PAP. */
513     if (auth_required && !wo->neg_chap && !wo->neg_upap) {
514         wo->neg_chap = 1;
515         wo->neg_upap = 1;
516     }
517
518     /*
519      * Check whether we have appropriate secrets to use
520      * to authenticate the peer.
521      */
522     can_auth = wo->neg_upap && (uselogin || have_pap_secret());
523     if (!can_auth && wo->neg_chap) {
524         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
525         can_auth = have_chap_secret(remote_name, our_name, remote);
526     }
527
528     if (auth_required && !can_auth) {
529         option_error("peer authentication required but no suitable secret(s) found\n");
530         if (remote_name[0] == 0)
531             option_error("for authenticating any peer to us (%s)\n", our_name);
532         else
533             option_error("for authenticating peer %s to us (%s)\n",
534                          remote_name, our_name);
535         exit(1);
536     }
537
538     /*
539      * Check whether the user tried to override certain values
540      * set by root.
541      */
542     if (!auth_required && auth_req_info.priv > 0) {
543         if (!default_device && devnam_info.priv == 0) {
544             option_error("can't override device name when noauth option used");
545             exit(1);
546         }
547         if (connector != NULL && connector_info.priv == 0
548             || disconnector != NULL && disconnector_info.priv == 0
549             || welcomer != NULL && welcomer_info.priv == 0) {
550             option_error("can't override connect, disconnect or welcome");
551             option_error("option values when noauth option used");
552             exit(1);
553         }
554     }
555 }
556
557 /*
558  * auth_reset - called when LCP is starting negotiations to recheck
559  * authentication options, i.e. whether we have appropriate secrets
560  * to use for authenticating ourselves and/or the peer.
561  */
562 void
563 auth_reset(unit)
564     int unit;
565 {
566     lcp_options *go = &lcp_gotoptions[unit];
567     lcp_options *ao = &lcp_allowoptions[0];
568     ipcp_options *ipwo = &ipcp_wantoptions[0];
569     u_int32_t remote;
570
571     ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
572     ao->neg_chap = !refuse_chap
573         && have_chap_secret(user, remote_name, (u_int32_t)0);
574
575     if (go->neg_upap && !uselogin && !have_pap_secret())
576         go->neg_upap = 0;
577     if (go->neg_chap) {
578         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
579         if (!have_chap_secret(remote_name, our_name, remote))
580             go->neg_chap = 0;
581     }
582
583 }
584
585
586 /*
587  * check_passwd - Check the user name and passwd against the PAP secrets
588  * file.  If requested, also check against the system password database,
589  * and login the user if OK.
590  *
591  * returns:
592  *      UPAP_AUTHNAK: Authentication failed.
593  *      UPAP_AUTHACK: Authentication succeeded.
594  * In either case, msg points to an appropriate message.
595  */
596 int
597 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
598     int unit;
599     char *auser;
600     int userlen;
601     char *apasswd;
602     int passwdlen;
603     char **msg;
604     int *msglen;
605 {
606     int ret;
607     char *filename;
608     FILE *f;
609     struct wordlist *addrs;
610     u_int32_t remote;
611     ipcp_options *ipwo = &ipcp_wantoptions[unit];
612     char passwd[256], user[256];
613     char secret[MAXWORDLEN];
614     static int attempts = 0;
615
616     /*
617      * Make copies of apasswd and auser, then null-terminate them.
618      */
619     BCOPY(apasswd, passwd, passwdlen);
620     passwd[passwdlen] = '\0';
621     BCOPY(auser, user, userlen);
622     user[userlen] = '\0';
623     *msg = (char *) 0;
624
625     /*
626      * Open the file of pap secrets and scan for a suitable secret
627      * for authenticating this user.
628      */
629     filename = _PATH_UPAPFILE;
630     addrs = NULL;
631     ret = UPAP_AUTHACK;
632     f = fopen(filename, "r");
633     if (f == NULL) {
634         syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
635         ret = UPAP_AUTHNAK;
636
637     } else {
638         check_access(f, filename);
639         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
640         if (scan_authfile(f, user, our_name, remote,
641                           secret, &addrs, filename) < 0
642             || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
643                 && strcmp(crypt(passwd, secret), secret) != 0)) {
644             syslog(LOG_WARNING, "PAP authentication failure for %s", user);
645             ret = UPAP_AUTHNAK;
646         }
647         fclose(f);
648     }
649
650     if (uselogin && ret == UPAP_AUTHACK) {
651         ret = login(user, passwd, msg, msglen);
652         if (ret == UPAP_AUTHNAK) {
653             syslog(LOG_WARNING, "PAP login failure for %s", user);
654         }
655     }
656
657     if (ret == UPAP_AUTHNAK) {
658         if (*msg == (char *) 0)
659             *msg = "Login incorrect";
660         *msglen = strlen(*msg);
661         /*
662          * Frustrate passwd stealer programs.
663          * Allow 10 tries, but start backing off after 3 (stolen from login).
664          * On 10'th, drop the connection.
665          */
666         if (attempts++ >= 10) {
667             syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
668                    attempts, devnam, user);
669             quit();
670         }
671         if (attempts > 3)
672             sleep((u_int) (attempts - 3) * 5);
673         if (addrs != NULL)
674             free_wordlist(addrs);
675
676     } else {
677         attempts = 0;                   /* Reset count */
678         if (*msg == (char *) 0)
679             *msg = "Login ok";
680         *msglen = strlen(*msg);
681         set_allowed_addrs(unit, addrs);
682     }
683
684     BZERO(passwd, sizeof(passwd));
685     BZERO(secret, sizeof(secret));
686
687     return ret;
688 }
689
690 /*
691  * This function is needed for PAM. However, it should not be called.
692  * If it is, return the error code.
693  */
694
695 #ifdef USE_PAM
696 static int pam_conv(int num_msg, const struct pam_message **msg,
697                     struct pam_response **resp, void *appdata_ptr)
698 {
699     return PAM_CONV_ERR;
700 }
701 #endif
702
703 /*
704  * login - Check the user name and password against the system
705  * password database, and login the user if OK.
706  *
707  * returns:
708  *      UPAP_AUTHNAK: Login failed.
709  *      UPAP_AUTHACK: Login succeeded.
710  * In either case, msg points to an appropriate message.
711  */
712
713 static int
714 login(user, passwd, msg, msglen)
715     char *user;
716     char *passwd;
717     char **msg;
718     int *msglen;
719 {
720     char *tty;
721
722 #ifdef USE_PAM
723     struct pam_conv pam_conversation;
724     pam_handle_t *pamh;
725     int pam_error;
726     char *pass;
727     char *dev;
728 /*
729  * Fill the pam_conversion structure
730  */
731     memset (&pam_conversation, '\0', sizeof (struct pam_conv));
732     pam_conversation.conv = &pam_conv;
733
734     pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
735     if (pam_error != PAM_SUCCESS) {
736         *msg = (char *) pam_strerror (pam_error);
737         return UPAP_AUTHNAK;
738     }
739 /*
740  * Define the fields for the credintial validation
741  */
742     (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
743     (void) pam_set_item (pamh, PAM_TTY,     devnam);
744 /*
745  * Validate the user
746  */
747     pam_error = pam_authenticate (pamh, PAM_SILENT);
748     if (pam_error == PAM_SUCCESS)
749         pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
750
751     *msg = (char *) pam_strerror (pam_error);
752 /*
753  * Clean up the mess
754  */
755     (void) pam_end (pamh, pam_error);
756
757     if (pam_error != PAM_SUCCESS)
758         return UPAP_AUTHNAK;
759 /*
760  * Use the non-PAM methods directly
761  */
762 #else /* #ifdef USE_PAM */
763
764     struct passwd *pw;
765     char *epasswd;
766
767 #ifdef HAS_SHADOW
768     struct spwd *spwd;
769     struct spwd *getspnam();
770     extern int isexpired (struct passwd *, struct spwd *); /* in libshadow.a */
771 #endif
772
773     pw = getpwnam(user);
774     if (pw == NULL) {
775         return (UPAP_AUTHNAK);
776     }
777
778 #ifdef HAS_SHADOW
779     spwd = getspnam(user);
780     endspent();
781     if (spwd) {
782         /* check the age of the password entry */
783         if (isexpired(pw, spwd)) {
784             syslog(LOG_WARNING,"Expired password for %s",user);
785             return (UPAP_AUTHNAK);
786         }
787         pw->pw_passwd = spwd->sp_pwdp;
788     }
789 #endif
790
791     /*
792      * If no passwd, don't let them login.
793      */
794     if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
795         || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
796         return (UPAP_AUTHNAK);
797
798 #endif /* #ifdef USE_PAM */
799
800     syslog(LOG_INFO, "user %s logged in", user);
801
802     /*
803      * Write a wtmp entry for this user.
804      */
805     tty = devnam;
806     if (strncmp(tty, "/dev/", 5) == 0)
807         tty += 5;
808     logwtmp(tty, user, remote_name);            /* Add wtmp login entry */
809     logged_in = TRUE;
810
811     return (UPAP_AUTHACK);
812 }
813
814 /*
815  * logout - Logout the user.
816  */
817 static void
818 logout()
819 {
820     char *tty;
821
822     tty = devnam;
823     if (strncmp(tty, "/dev/", 5) == 0)
824         tty += 5;
825     logwtmp(tty, "", "");               /* Wipe out wtmp logout entry */
826     logged_in = FALSE;
827 }
828
829
830 /*
831  * null_login - Check if a username of "" and a password of "" are
832  * acceptable, and iff so, set the list of acceptable IP addresses
833  * and return 1.
834  */
835 static int
836 null_login(unit)
837     int unit;
838 {
839     char *filename;
840     FILE *f;
841     int i, ret;
842     struct wordlist *addrs;
843     char secret[MAXWORDLEN];
844
845     /*
846      * Open the file of pap secrets and scan for a suitable secret.
847      * We don't accept a wildcard client.
848      */
849     filename = _PATH_UPAPFILE;
850     addrs = NULL;
851     f = fopen(filename, "r");
852     if (f == NULL)
853         return 0;
854     check_access(f, filename);
855
856     i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
857     ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
858     BZERO(secret, sizeof(secret));
859
860     if (ret)
861         set_allowed_addrs(unit, addrs);
862     else
863         free_wordlist(addrs);
864
865     fclose(f);
866     return ret;
867 }
868
869
870 /*
871  * get_pap_passwd - get a password for authenticating ourselves with
872  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
873  * could be found.
874  */
875 static int
876 get_pap_passwd(passwd)
877     char *passwd;
878 {
879     char *filename;
880     FILE *f;
881     struct wordlist *addrs;
882     char secret[MAXWORDLEN];
883
884     filename = _PATH_UPAPFILE;
885     addrs = NULL;
886     f = fopen(filename, "r");
887     if (f == NULL)
888         return 0;
889     check_access(f, filename);
890     if (scan_authfile(f, user,
891                       remote_name[0]? remote_name: NULL,
892                       (u_int32_t)0, secret, NULL, filename) < 0)
893         return 0;
894     if (passwd != NULL) {
895         strncpy(passwd, secret, MAXSECRETLEN);
896         passwd[MAXSECRETLEN-1] = 0;
897     }
898     BZERO(secret, sizeof(secret));
899     return 1;
900 }
901
902
903 /*
904  * have_pap_secret - check whether we have a PAP file with any
905  * secrets that we could possibly use for authenticating the peer.
906  */
907 static int
908 have_pap_secret()
909 {
910     FILE *f;
911     int ret;
912     char *filename;
913     ipcp_options *ipwo = &ipcp_wantoptions[0];
914     u_int32_t remote;
915
916     filename = _PATH_UPAPFILE;
917     f = fopen(filename, "r");
918     if (f == NULL)
919         return 0;
920
921     remote = ipwo->accept_remote? 0: ipwo->hisaddr;
922     ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
923     fclose(f);
924     if (ret < 0)
925         return 0;
926
927     return 1;
928 }
929
930
931 /*
932  * have_chap_secret - check whether we have a CHAP file with a
933  * secret that we could possibly use for authenticating `client'
934  * on `server'.  Either can be the null string, meaning we don't
935  * know the identity yet.
936  */
937 static int
938 have_chap_secret(client, server, remote)
939     char *client;
940     char *server;
941     u_int32_t remote;
942 {
943     FILE *f;
944     int ret;
945     char *filename;
946
947     filename = _PATH_CHAPFILE;
948     f = fopen(filename, "r");
949     if (f == NULL)
950         return 0;
951
952     if (client[0] == 0)
953         client = NULL;
954     else if (server[0] == 0)
955         server = NULL;
956
957     ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
958     fclose(f);
959     if (ret < 0)
960         return 0;
961
962     return 1;
963 }
964
965
966 /*
967  * get_secret - open the CHAP secret file and return the secret
968  * for authenticating the given client on the given server.
969  * (We could be either client or server).
970  */
971 int
972 get_secret(unit, client, server, secret, secret_len, save_addrs)
973     int unit;
974     char *client;
975     char *server;
976     char *secret;
977     int *secret_len;
978     int save_addrs;
979 {
980     FILE *f;
981     int ret, len;
982     char *filename;
983     struct wordlist *addrs;
984     char secbuf[MAXWORDLEN];
985
986     filename = _PATH_CHAPFILE;
987     addrs = NULL;
988     secbuf[0] = 0;
989
990     f = fopen(filename, "r");
991     if (f == NULL) {
992         syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
993         return 0;
994     }
995     check_access(f, filename);
996
997     ret = scan_authfile(f, client, server, (u_int32_t)0,
998                         secbuf, &addrs, filename);
999     fclose(f);
1000     if (ret < 0)
1001         return 0;
1002
1003     if (save_addrs)
1004         set_allowed_addrs(unit, addrs);
1005
1006     len = strlen(secbuf);
1007     if (len > MAXSECRETLEN) {
1008         syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1009         len = MAXSECRETLEN;
1010     }
1011     BCOPY(secbuf, secret, len);
1012     BZERO(secbuf, sizeof(secbuf));
1013     *secret_len = len;
1014
1015     return 1;
1016 }
1017
1018 /*
1019  * set_allowed_addrs() - set the list of allowed addresses.
1020  */
1021 static void
1022 set_allowed_addrs(unit, addrs)
1023     int unit;
1024     struct wordlist *addrs;
1025 {
1026     if (addresses[unit] != NULL)
1027         free_wordlist(addresses[unit]);
1028     addresses[unit] = addrs;
1029
1030     /*
1031      * If there's only one authorized address we might as well
1032      * ask our peer for that one right away
1033      */
1034     if (addrs != NULL && addrs->next == NULL) {
1035         char *p = addrs->word;
1036         struct ipcp_options *wo = &ipcp_wantoptions[unit];
1037         u_int32_t a;
1038         struct hostent *hp;
1039
1040         if (wo->hisaddr == 0 && *p != '!' && *p != '-'
1041             && strchr(p, '/') == NULL) {
1042             hp = gethostbyname(p);
1043             if (hp != NULL && hp->h_addrtype == AF_INET)
1044                 a = *(u_int32_t *)hp->h_addr;
1045             else
1046                 a = inet_addr(p);
1047             if (a != (u_int32_t) -1)
1048                 wo->hisaddr = a;
1049         }
1050     }
1051 }
1052
1053 /*
1054  * auth_ip_addr - check whether the peer is authorized to use
1055  * a given IP address.  Returns 1 if authorized, 0 otherwise.
1056  */
1057 int
1058 auth_ip_addr(unit, addr)
1059     int unit;
1060     u_int32_t addr;
1061 {
1062     return ip_addr_check(addr, addresses[unit]);
1063 }
1064
1065 static int
1066 ip_addr_check(addr, addrs)
1067     u_int32_t addr;
1068     struct wordlist *addrs;
1069 {
1070     u_int32_t a, mask, ah;
1071     int accept;
1072     char *ptr_word, *ptr_mask;
1073     struct hostent *hp;
1074     struct netent *np;
1075
1076     /* don't allow loopback or multicast address */
1077     if (bad_ip_adrs(addr))
1078         return 0;
1079
1080     if (addrs == NULL)
1081         return !auth_required;          /* no addresses authorized */
1082
1083     for (; addrs != NULL; addrs = addrs->next) {
1084         /* "-" means no addresses authorized, "*" means any address allowed */
1085         ptr_word = addrs->word;
1086         if (strcmp(ptr_word, "-") == 0)
1087             break;
1088         if (strcmp(ptr_word, "*") == 0)
1089             return 1;
1090
1091         accept = 1;
1092         if (*ptr_word == '!') {
1093             accept = 0;
1094             ++ptr_word;
1095         }
1096
1097         mask = ~ (u_int32_t) 0;
1098         ptr_mask = strchr (ptr_word, '/');
1099         if (ptr_mask != NULL) {
1100             int bit_count;
1101
1102             bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1103             if (bit_count <= 0 || bit_count > 32) {
1104                 syslog (LOG_WARNING,
1105                         "invalid address length %s in auth. address list",
1106                         ptr_mask);
1107                 continue;
1108             }
1109             *ptr_mask = '\0';
1110             mask <<= 32 - bit_count;
1111         }
1112
1113         hp = gethostbyname(ptr_word);
1114         if (hp != NULL && hp->h_addrtype == AF_INET) {
1115             a = *(u_int32_t *)hp->h_addr;
1116         } else {
1117             np = getnetbyname (ptr_word);
1118             if (np != NULL && np->n_addrtype == AF_INET) {
1119                 a = htonl (*(u_int32_t *)np->n_net);
1120                 if (ptr_mask == NULL) {
1121                     /* calculate appropriate mask for net */
1122                     ah = ntohl(a);
1123                     if (IN_CLASSA(ah))
1124                         mask = IN_CLASSA_NET;
1125                     else if (IN_CLASSB(ah))
1126                         mask = IN_CLASSB_NET;
1127                     else if (IN_CLASSC(ah))
1128                         mask = IN_CLASSC_NET;
1129                 }
1130             } else {
1131                 a = inet_addr (ptr_word);
1132             }
1133         }
1134
1135         if (ptr_mask != NULL)
1136             *ptr_mask = '/';
1137
1138         if (a == -1L)
1139             syslog (LOG_WARNING,
1140                     "unknown host %s in auth. address list",
1141                     addrs->word);
1142         else
1143             /* Here a and addr are in network byte order,
1144                and mask is in host order. */
1145             if (((addr ^ a) & htonl(mask)) == 0)
1146                 return accept;
1147     }
1148     return 0;                   /* not in list => can't have it */
1149 }
1150
1151 /*
1152  * bad_ip_adrs - return 1 if the IP address is one we don't want
1153  * to use, such as an address in the loopback net or a multicast address.
1154  * addr is in network byte order.
1155  */
1156 int
1157 bad_ip_adrs(addr)
1158     u_int32_t addr;
1159 {
1160     addr = ntohl(addr);
1161     return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1162         || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1163 }
1164
1165 /*
1166  * check_access - complain if a secret file has too-liberal permissions.
1167  */
1168 void
1169 check_access(f, filename)
1170     FILE *f;
1171     char *filename;
1172 {
1173     struct stat sbuf;
1174
1175     if (fstat(fileno(f), &sbuf) < 0) {
1176         syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1177     } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1178         syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1179     }
1180 }
1181
1182
1183 /*
1184  * scan_authfile - Scan an authorization file for a secret suitable
1185  * for authenticating `client' on `server'.  The return value is -1
1186  * if no secret is found, otherwise >= 0.  The return value has
1187  * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1188  * NONWILD_SERVER set if the secret didn't have "*" for the server.
1189  * Any following words on the line (i.e. address authorization
1190  * info) are placed in a wordlist and returned in *addrs.  
1191  */
1192 static int
1193 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1194     FILE *f;
1195     char *client;
1196     char *server;
1197     u_int32_t ipaddr;
1198     char *secret;
1199     struct wordlist **addrs;
1200     char *filename;
1201 {
1202     int newline, xxx;
1203     int got_flag, best_flag;
1204     FILE *sf;
1205     struct wordlist *ap, *addr_list, *alist, *alast;
1206     char word[MAXWORDLEN];
1207     char atfile[MAXWORDLEN];
1208     char lsecret[MAXWORDLEN];
1209
1210     if (addrs != NULL)
1211         *addrs = NULL;
1212     addr_list = NULL;
1213     if (!getword(f, word, &newline, filename))
1214         return -1;              /* file is empty??? */
1215     newline = 1;
1216     best_flag = -1;
1217     for (;;) {
1218         /*
1219          * Skip until we find a word at the start of a line.
1220          */
1221         while (!newline && getword(f, word, &newline, filename))
1222             ;
1223         if (!newline)
1224             break;              /* got to end of file */
1225
1226         /*
1227          * Got a client - check if it's a match or a wildcard.
1228          */
1229         got_flag = 0;
1230         if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1231             newline = 0;
1232             continue;
1233         }
1234         if (!ISWILD(word))
1235             got_flag = NONWILD_CLIENT;
1236
1237         /*
1238          * Now get a server and check if it matches.
1239          */
1240         if (!getword(f, word, &newline, filename))
1241             break;
1242         if (newline)
1243             continue;
1244         if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1245             continue;
1246         if (!ISWILD(word))
1247             got_flag |= NONWILD_SERVER;
1248
1249         /*
1250          * Got some sort of a match - see if it's better than what
1251          * we have already.
1252          */
1253         if (got_flag <= best_flag)
1254             continue;
1255
1256         /*
1257          * Get the secret.
1258          */
1259         if (!getword(f, word, &newline, filename))
1260             break;
1261         if (newline)
1262             continue;
1263
1264         /*
1265          * Special syntax: @filename means read secret from file.
1266          */
1267         if (word[0] == '@') {
1268             strcpy(atfile, word+1);
1269             if ((sf = fopen(atfile, "r")) == NULL) {
1270                 syslog(LOG_WARNING, "can't open indirect secret file %s",
1271                        atfile);
1272                 continue;
1273             }
1274             check_access(sf, atfile);
1275             if (!getword(sf, word, &xxx, atfile)) {
1276                 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1277                        atfile);
1278                 fclose(sf);
1279                 continue;
1280             }
1281             fclose(sf);
1282         }
1283         if (secret != NULL)
1284             strcpy(lsecret, word);
1285
1286         /*
1287          * Now read address authorization info and make a wordlist.
1288          */
1289         alist = alast = NULL;
1290         for (;;) {
1291             if (!getword(f, word, &newline, filename) || newline)
1292                 break;
1293             ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1294                                             + strlen(word));
1295             if (ap == NULL)
1296                 novm("authorized addresses");
1297             ap->next = NULL;
1298             strcpy(ap->word, word);
1299             if (alist == NULL)
1300                 alist = ap;
1301             else
1302                 alast->next = ap;
1303             alast = ap;
1304         }
1305
1306         /*
1307          * Check if the given IP address is allowed by the wordlist.
1308          */
1309         if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1310             free_wordlist(alist);
1311             continue;
1312         }
1313
1314         /*
1315          * This is the best so far; remember it.
1316          */
1317         best_flag = got_flag;
1318         if (addr_list)
1319             free_wordlist(addr_list);
1320         addr_list = alist;
1321         if (secret != NULL)
1322             strcpy(secret, lsecret);
1323
1324         if (!newline)
1325             break;
1326     }
1327
1328     if (addrs != NULL)
1329         *addrs = addr_list;
1330     else if (addr_list != NULL)
1331         free_wordlist(addr_list);
1332
1333     return best_flag;
1334 }
1335
1336 /*
1337  * free_wordlist - release memory allocated for a wordlist.
1338  */
1339 static void
1340 free_wordlist(wp)
1341     struct wordlist *wp;
1342 {
1343     struct wordlist *next;
1344
1345     while (wp != NULL) {
1346         next = wp->next;
1347         free(wp);
1348         wp = next;
1349     }
1350 }
1351
1352 /*
1353  * auth_script - execute a script with arguments
1354  * interface-name peer-name real-user tty speed
1355  */
1356 static void
1357 auth_script(script)
1358     char *script;
1359 {
1360     char strspeed[32];
1361     struct passwd *pw;
1362     char struid[32];
1363     char *user_name;
1364     char *argv[8];
1365
1366     if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1367         user_name = pw->pw_name;
1368     else {
1369         sprintf(struid, "%d", getuid());
1370         user_name = struid;
1371     }
1372     sprintf(strspeed, "%d", baud_rate);
1373
1374     argv[0] = script;
1375     argv[1] = ifname;
1376     argv[2] = peer_authname;
1377     argv[3] = user_name;
1378     argv[4] = devnam;
1379     argv[5] = strspeed;
1380     argv[6] = NULL;
1381
1382     run_program(script, argv, 0);
1383 }