]> git.ozlabs.org Git - ppp.git/blob - pppd/auth.c
added files for demand-dialling and packet filtering; cleanup
[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.21 1996/01/01 22:53:04 paulus Exp $";
37 #endif
38
39 #include <stdio.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <syslog.h>
43 #include <pwd.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/socket.h>
48
49 #include <netdb.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52
53 #ifdef HAS_SHADOW
54 #include <shadow.h>
55 #include <shadow/pwauth.h>
56 #ifndef PW_PPP
57 #define PW_PPP PW_LOGIN
58 #endif
59 #endif
60
61 #include "pppd.h"
62 #include "fsm.h"
63 #include "lcp.h"
64 #include "upap.h"
65 #include "pathnames.h"
66
67 #if defined(sun) && defined(sparc)
68 #include <alloca.h>
69 #endif /*sparc*/
70
71 /* Used for storing a sequence of words.  Usually malloced. */
72 struct wordlist {
73     struct wordlist     *next;
74     char                word[1];
75 };
76
77 /* Bits in scan_authfile return value */
78 #define NONWILD_SERVER  1
79 #define NONWILD_CLIENT  2
80
81 #define ISWILD(word)    (word[0] == '*' && word[1] == 0)
82
83 #define FALSE   0
84 #define TRUE    1
85
86 /* Records which authentication operations haven't completed yet. */
87 static int auth_pending[NUM_PPP];
88
89 /* Set if we have successfully called login() */
90 static int logged_in;
91
92 /* List of addresses which the peer may use. */
93 static struct wordlist *addresses[NUM_PPP];
94
95 /* Number of network protocols which we have opened. */
96 static int num_np_open;
97
98 /* Number of network protocols which have come up. */
99 static int num_np_up;
100
101 /* Bits in auth_pending[] */
102 #define UPAP_WITHPEER   1
103 #define UPAP_PEER       2
104 #define CHAP_WITHPEER   4
105 #define CHAP_PEER       8
106
107 /* Prototypes for procedures local to this file. */
108
109 static void network_phase __P((int));
110 static void check_idle __P((caddr_t));
111 static int  login __P((char *, char *, char **, int *));
112 static void logout __P((void));
113 static int  null_login __P((int));
114 static int  get_upap_passwd __P((void));
115 static int  have_upap_secret __P((void));
116 static int  have_chap_secret __P((char *, char *));
117 static int  scan_authfile __P((FILE *, char *, char *, char *,
118                                   struct wordlist **, char *));
119 static void free_wordlist __P((struct wordlist *));
120
121 extern char *crypt __P((char *, char *));
122
123 /*
124  * An Open on LCP has requested a change from Dead to Establish phase.
125  * Do what's necessary to bring the physical layer up.
126  */
127 void
128 link_required(unit)
129     int unit;
130 {
131 }
132
133 /*
134  * LCP has terminated the link; go to the Dead phase and take the
135  * physical layer down.
136  */
137 void
138 link_terminated(unit)
139     int unit;
140 {
141     if (phase == PHASE_DEAD)
142         return;
143     if (logged_in)
144         logout();
145     phase = PHASE_DEAD;
146     syslog(LOG_NOTICE, "Connection terminated.");
147 }
148
149 /*
150  * LCP has gone down; it will either die or try to re-establish.
151  */
152 void
153 link_down(unit)
154     int unit;
155 {
156     int i;
157     struct protent *protp;
158
159     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
160         if (!protp->enabled_flag)
161             continue;
162         if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
163             (*protp->lowerdown)(unit);
164         if (protp->protocol < 0xC000 && protp->close != NULL)
165             (*protp->close)(unit);
166     }
167     num_np_open = 0;
168     num_np_up = 0;
169     phase = PHASE_TERMINATE;
170 }
171
172 /*
173  * The link is established.
174  * Proceed to the Dead, Authenticate or Network phase as appropriate.
175  */
176 void
177 link_established(unit)
178     int unit;
179 {
180     int auth;
181     lcp_options *wo = &lcp_wantoptions[unit];
182     lcp_options *go = &lcp_gotoptions[unit];
183     lcp_options *ho = &lcp_hisoptions[unit];
184     int i;
185     struct protent *protp;
186
187     /*
188      * Tell higher-level protocols that LCP is up.
189      */
190     for (i = 0; (protp = protocols[i]) != NULL; ++i)
191         if (protp->protocol != PPP_LCP && protp->enabled_flag
192             && protp->lowerup != NULL)
193             (*protp->lowerup)(unit);
194
195     if (auth_required && !(go->neg_chap || go->neg_upap)) {
196         /*
197          * We wanted the peer to authenticate itself, and it refused:
198          * treat it as though it authenticated with PAP using a username
199          * of "" and a password of "".  If that's not OK, boot it out.
200          */
201         if (!wo->neg_upap || !null_login(unit)) {
202             syslog(LOG_WARNING, "peer refused to authenticate");
203             lcp_close(unit, "peer refused to authenticate");
204             phase = PHASE_TERMINATE;
205             return;
206         }
207     }
208
209     phase = PHASE_AUTHENTICATE;
210     auth = 0;
211     if (go->neg_chap) {
212         ChapAuthPeer(unit, our_name, go->chap_mdtype);
213         auth |= CHAP_PEER;
214     } else if (go->neg_upap) {
215         upap_authpeer(unit);
216         auth |= UPAP_PEER;
217     }
218     if (ho->neg_chap) {
219         ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
220         auth |= CHAP_WITHPEER;
221     } else if (ho->neg_upap) {
222         upap_authwithpeer(unit, user, passwd);
223         auth |= UPAP_WITHPEER;
224     }
225     auth_pending[unit] = auth;
226
227     if (!auth)
228         network_phase(unit);
229 }
230
231 /*
232  * Proceed to the network phase.
233  */
234 static void
235 network_phase(unit)
236     int unit;
237 {
238     int i;
239     struct protent *protp;
240
241     phase = PHASE_NETWORK;
242     for (i = 0; (protp = protocols[i]) != NULL; ++i)
243         if (protp->protocol < 0xC000 && protp->enabled_flag
244             && protp->open != NULL) {
245             (*protp->open)(unit);
246             if (protp->protocol != PPP_CCP)
247                 ++num_np_open;
248         }
249 }
250
251 /*
252  * The peer has failed to authenticate himself using `protocol'.
253  */
254 void
255 auth_peer_fail(unit, protocol)
256     int unit, protocol;
257 {
258     /*
259      * Authentication failure: take the link down
260      */
261     lcp_close(unit, "Authentication failed");
262     phase = PHASE_TERMINATE;
263 }
264
265 /*
266  * The peer has been successfully authenticated using `protocol'.
267  */
268 void
269 auth_peer_success(unit, protocol)
270     int unit, protocol;
271 {
272     int bit;
273
274     switch (protocol) {
275     case PPP_CHAP:
276         bit = CHAP_PEER;
277         break;
278     case PPP_PAP:
279         bit = UPAP_PEER;
280         break;
281     default:
282         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
283                protocol);
284         return;
285     }
286
287     /*
288      * If there is no more authentication still to be done,
289      * proceed to the network phase.
290      */
291     if ((auth_pending[unit] &= ~bit) == 0)
292         network_phase(unit);
293 }
294
295 /*
296  * We have failed to authenticate ourselves to the peer using `protocol'.
297  */
298 void
299 auth_withpeer_fail(unit, protocol)
300     int unit, protocol;
301 {
302     /*
303      * We've failed to authenticate ourselves to our peer.
304      * He'll probably take the link down, and there's not much
305      * we can do except wait for that.
306      */
307 }
308
309 /*
310  * We have successfully authenticated ourselves with the peer using `protocol'.
311  */
312 void
313 auth_withpeer_success(unit, protocol)
314     int unit, protocol;
315 {
316     int bit;
317
318     switch (protocol) {
319     case PPP_CHAP:
320         bit = CHAP_WITHPEER;
321         break;
322     case PPP_PAP:
323         bit = UPAP_WITHPEER;
324         break;
325     default:
326         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
327                protocol);
328         bit = 0;
329     }
330
331     /*
332      * If there is no more authentication still being done,
333      * proceed to the network phase.
334      */
335     if ((auth_pending[unit] &= ~bit) == 0)
336         network_phase(unit);
337 }
338
339
340 /*
341  * np_up - a network protocol has come up.
342  */
343 void
344 np_up(unit, proto)
345     int unit, proto;
346 {
347     if (num_np_up == 0 && idle_time_limit > 0) {
348         TIMEOUT(check_idle, NULL, idle_time_limit);
349     }
350     ++num_np_up;
351 }
352
353 /*
354  * np_down - a network protocol has gone down.
355  */
356 void
357 np_down(unit, proto)
358     int unit, proto;
359 {
360     if (--num_np_up == 0 && idle_time_limit > 0) {
361         UNTIMEOUT(check_idle, NULL);
362     }
363 }
364
365 /*
366  * np_finished - a network protocol has finished using the link.
367  */
368 void
369 np_finished(unit, proto)
370     int unit, proto;
371 {
372     if (--num_np_open <= 0) {
373         /* no further use for the link: shut up shop. */
374         lcp_close(0, "No network protocols running");
375     }
376 }
377
378 /*
379  * check_idle - check whether the link has been idle for long
380  * enough that we can shut it down.
381  */
382 static void
383 check_idle(arg)
384     caddr_t arg;
385 {
386     struct ppp_idle idle;
387     time_t itime;
388
389     if (!get_idle_time(0, &idle))
390         return;
391     itime = MIN(idle.xmit_idle, idle.recv_idle);
392     if (itime >= idle_time_limit) {
393         /* link is idle: shut it down. */
394         syslog(LOG_INFO, "Terminating connection due to lack of activity.");
395         lcp_close(0, "Link inactive");
396     } else {
397         TIMEOUT(check_idle, NULL, idle_time_limit - itime);
398     }
399 }
400
401 /*
402  * auth_check_options - called to check authentication options.
403  */
404 void
405 auth_check_options()
406 {
407     lcp_options *wo = &lcp_wantoptions[0];
408     lcp_options *ao = &lcp_allowoptions[0];
409
410     /* Default our_name to hostname, and user to our_name */
411     if (our_name[0] == 0 || usehostname)
412         strcpy(our_name, hostname);
413     if (user[0] == 0)
414         strcpy(user, our_name);
415
416     /* If authentication is required, ask peer for CHAP or PAP. */
417     if (auth_required && !wo->neg_chap && !wo->neg_upap) {
418         wo->neg_chap = 1;
419         wo->neg_upap = 1;
420     }
421
422     /*
423      * Check whether we have appropriate secrets to use
424      * to authenticate ourselves and/or the peer.
425      */
426     if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
427         ao->neg_upap = 0;
428     if (wo->neg_upap && !uselogin && !have_upap_secret())
429         wo->neg_upap = 0;
430     if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
431         ao->neg_chap = 0;
432     if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
433         wo->neg_chap = 0;
434
435     if (auth_required && !wo->neg_chap && !wo->neg_upap) {
436         fprintf(stderr, "\
437 pppd: peer authentication required but no authentication files accessible\n");
438         exit(1);
439     }
440
441 }
442
443
444 /*
445  * check_passwd - Check the user name and passwd against the PAP secrets
446  * file.  If requested, also check against the system password database,
447  * and login the user if OK.
448  *
449  * returns:
450  *      UPAP_AUTHNAK: Authentication failed.
451  *      UPAP_AUTHACK: Authentication succeeded.
452  * In either case, msg points to an appropriate message.
453  */
454 int
455 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
456     int unit;
457     char *auser;
458     int userlen;
459     char *apasswd;
460     int passwdlen;
461     char **msg;
462     int *msglen;
463 {
464     int ret;
465     char *filename;
466     FILE *f;
467     struct wordlist *addrs;
468     char passwd[256], user[256];
469     char secret[MAXWORDLEN];
470     static int attempts = 0;
471
472     /*
473      * Make copies of apasswd and auser, then null-terminate them.
474      */
475     BCOPY(apasswd, passwd, passwdlen);
476     passwd[passwdlen] = '\0';
477     BCOPY(auser, user, userlen);
478     user[userlen] = '\0';
479
480     /*
481      * Open the file of upap secrets and scan for a suitable secret
482      * for authenticating this user.
483      */
484     filename = _PATH_UPAPFILE;
485     addrs = NULL;
486     ret = UPAP_AUTHACK;
487     f = fopen(filename, "r");
488     if (f == NULL) {
489         if (!uselogin) {
490             syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
491             ret = UPAP_AUTHNAK;
492         }
493
494     } else {
495         check_access(f, filename);
496         if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
497             || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
498                 && strcmp(crypt(passwd, secret), secret) != 0)) {
499             syslog(LOG_WARNING, "PAP authentication failure for %s", user);
500             ret = UPAP_AUTHNAK;
501         }
502         fclose(f);
503     }
504
505     if (uselogin && ret == UPAP_AUTHACK) {
506         ret = login(user, passwd, msg, msglen);
507         if (ret == UPAP_AUTHNAK) {
508             syslog(LOG_WARNING, "PAP login failure for %s", user);
509         }
510     }
511
512     if (ret == UPAP_AUTHNAK) {
513         *msg = "Login incorrect";
514         *msglen = strlen(*msg);
515         /*
516          * Frustrate passwd stealer programs.
517          * Allow 10 tries, but start backing off after 3 (stolen from login).
518          * On 10'th, drop the connection.
519          */
520         if (attempts++ >= 10) {
521             syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
522                    attempts, devnam, user);
523             quit();
524         }
525         if (attempts > 3)
526             sleep((u_int) (attempts - 3) * 5);
527         if (addrs != NULL)
528             free_wordlist(addrs);
529
530     } else {
531         attempts = 0;                   /* Reset count */
532         *msg = "Login ok";
533         *msglen = strlen(*msg);
534         if (addresses[unit] != NULL)
535             free_wordlist(addresses[unit]);
536         addresses[unit] = addrs;
537     }
538
539     return ret;
540 }
541
542
543 /*
544  * login - Check the user name and password against the system
545  * password database, and login the user if OK.
546  *
547  * returns:
548  *      UPAP_AUTHNAK: Login failed.
549  *      UPAP_AUTHACK: Login succeeded.
550  * In either case, msg points to an appropriate message.
551  */
552 static int
553 login(user, passwd, msg, msglen)
554     char *user;
555     char *passwd;
556     char **msg;
557     int *msglen;
558 {
559     struct passwd *pw;
560     char *epasswd;
561     char *tty;
562
563 #ifdef HAS_SHADOW
564     struct spwd *spwd;
565     struct spwd *getspnam();
566 #endif
567
568     if ((pw = getpwnam(user)) == NULL) {
569         return (UPAP_AUTHNAK);
570     }
571
572 #ifdef HAS_SHADOW
573     if ((spwd = getspnam(user)) == NULL) {
574         pw->pw_passwd = "";
575     } else {
576         pw->pw_passwd = spwd->sp_pwdp;
577     }
578 #endif
579
580     /*
581      * XXX If no passwd, let them login without one.
582      */
583     if (pw->pw_passwd == '\0') {
584         return (UPAP_AUTHACK);
585     }
586
587 #ifdef HAS_SHADOW
588     if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
589          && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
590         || !valid (passwd, pw)) {
591         return (UPAP_AUTHNAK);
592     }
593 #else
594     epasswd = crypt(passwd, pw->pw_passwd);
595     if (strcmp(epasswd, pw->pw_passwd)) {
596         return (UPAP_AUTHNAK);
597     }
598 #endif
599
600     syslog(LOG_INFO, "user %s logged in", user);
601
602     /*
603      * Write a wtmp entry for this user.
604      */
605     tty = devnam;
606     if (strncmp(tty, "/dev/", 5) == 0)
607         tty += 5;
608     logwtmp(tty, user, "");             /* Add wtmp login entry */
609     logged_in = TRUE;
610
611     return (UPAP_AUTHACK);
612 }
613
614 /*
615  * logout - Logout the user.
616  */
617 static void
618 logout()
619 {
620     char *tty;
621
622     tty = devnam;
623     if (strncmp(tty, "/dev/", 5) == 0)
624         tty += 5;
625     logwtmp(tty, "", "");               /* Wipe out wtmp logout entry */
626     logged_in = FALSE;
627 }
628
629
630 /*
631  * null_login - Check if a username of "" and a password of "" are
632  * acceptable, and iff so, set the list of acceptable IP addresses
633  * and return 1.
634  */
635 static int
636 null_login(unit)
637     int unit;
638 {
639     char *filename;
640     FILE *f;
641     int i, ret;
642     struct wordlist *addrs;
643     char secret[MAXWORDLEN];
644
645     /*
646      * Open the file of upap secrets and scan for a suitable secret.
647      * We don't accept a wildcard client.
648      */
649     filename = _PATH_UPAPFILE;
650     addrs = NULL;
651     f = fopen(filename, "r");
652     if (f == NULL)
653         return 0;
654     check_access(f, filename);
655
656     i = scan_authfile(f, "", our_name, secret, &addrs, filename);
657     ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
658
659     if (ret) {
660         if (addresses[unit] != NULL)
661             free_wordlist(addresses[unit]);
662         addresses[unit] = addrs;
663     }
664
665     fclose(f);
666     return ret;
667 }
668
669
670 /*
671  * get_upap_passwd - get a password for authenticating ourselves with
672  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
673  * could be found.
674  */
675 static int
676 get_upap_passwd()
677 {
678     char *filename;
679     FILE *f;
680     struct wordlist *addrs;
681     char secret[MAXWORDLEN];
682
683     filename = _PATH_UPAPFILE;
684     addrs = NULL;
685     f = fopen(filename, "r");
686     if (f == NULL)
687         return 0;
688     check_access(f, filename);
689     if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
690         return 0;
691     strncpy(passwd, secret, MAXSECRETLEN);
692     passwd[MAXSECRETLEN-1] = 0;
693     return 1;
694 }
695
696
697 /*
698  * have_upap_secret - check whether we have a PAP file with any
699  * secrets that we could possibly use for authenticating the peer.
700  */
701 static int
702 have_upap_secret()
703 {
704     FILE *f;
705     int ret;
706     char *filename;
707
708     filename = _PATH_UPAPFILE;
709     f = fopen(filename, "r");
710     if (f == NULL)
711         return 0;
712
713     ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
714     fclose(f);
715     if (ret < 0)
716         return 0;
717
718     return 1;
719 }
720
721
722 /*
723  * have_chap_secret - check whether we have a CHAP file with a
724  * secret that we could possibly use for authenticating `client'
725  * on `server'.  Either can be the null string, meaning we don't
726  * know the identity yet.
727  */
728 static int
729 have_chap_secret(client, server)
730     char *client;
731     char *server;
732 {
733     FILE *f;
734     int ret;
735     char *filename;
736
737     filename = _PATH_CHAPFILE;
738     f = fopen(filename, "r");
739     if (f == NULL)
740         return 0;
741
742     if (client[0] == 0)
743         client = NULL;
744     else if (server[0] == 0)
745         server = NULL;
746
747     ret = scan_authfile(f, client, server, NULL, NULL, filename);
748     fclose(f);
749     if (ret < 0)
750         return 0;
751
752     return 1;
753 }
754
755
756 /*
757  * get_secret - open the CHAP secret file and return the secret
758  * for authenticating the given client on the given server.
759  * (We could be either client or server).
760  */
761 int
762 get_secret(unit, client, server, secret, secret_len, save_addrs)
763     int unit;
764     char *client;
765     char *server;
766     char *secret;
767     int *secret_len;
768     int save_addrs;
769 {
770     FILE *f;
771     int ret, len;
772     char *filename;
773     struct wordlist *addrs;
774     char secbuf[MAXWORDLEN];
775
776     filename = _PATH_CHAPFILE;
777     addrs = NULL;
778     secbuf[0] = 0;
779
780     f = fopen(filename, "r");
781     if (f == NULL) {
782         syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
783         return 0;
784     }
785     check_access(f, filename);
786
787     ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
788     fclose(f);
789     if (ret < 0)
790         return 0;
791
792     if (save_addrs) {
793         if (addresses[unit] != NULL)
794             free_wordlist(addresses[unit]);
795         addresses[unit] = addrs;
796     }
797
798     len = strlen(secbuf);
799     if (len > MAXSECRETLEN) {
800         syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
801         len = MAXSECRETLEN;
802     }
803     BCOPY(secbuf, secret, len);
804     *secret_len = len;
805
806     return 1;
807 }
808
809 /*
810  * auth_ip_addr - check whether the peer is authorized to use
811  * a given IP address.  Returns 1 if authorized, 0 otherwise.
812  */
813 int
814 auth_ip_addr(unit, addr)
815     int unit;
816     u_int32_t addr;
817 {
818     u_int32_t a, mask;
819     int accept;
820     char *ptr_word, *ptr_mask;
821     struct hostent *hp;
822     struct netent *np;
823     struct wordlist *addrs;
824
825     /* don't allow loopback or multicast address */
826     if (bad_ip_adrs(addr))
827         return 0;
828
829     if ((addrs = addresses[unit]) == NULL)
830         return 1;               /* no restriction */
831
832     for (; addrs != NULL; addrs = addrs->next) {
833         /* "-" means no addresses authorized */
834         ptr_word = addrs->word;
835         if (strcmp(ptr_word, "-") == 0)
836             break;
837
838         accept = 1;
839         if (*ptr_word == '!') {
840             accept = 0;
841             ++ptr_word;
842         }
843
844         mask = ~ (u_int32_t) 0;
845         ptr_mask = strchr (ptr_word, '/');
846         if (ptr_mask != NULL) {
847             int bit_count;
848
849             bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
850             if (bit_count <= 0 || bit_count > 32) {
851                 syslog (LOG_WARNING,
852                         "invalid address length %s in auth. address list",
853                         ptr_mask);
854                 continue;
855             }
856             *ptr_mask = '\0';
857             mask <<= 32 - bit_count;
858         }
859
860         hp = gethostbyname(ptr_word);
861         if (hp != NULL && hp->h_addrtype == AF_INET) {
862             a    = *(u_int32_t *)hp->h_addr;
863             mask = ~ (u_int32_t) 0;     /* are we sure we want this? */
864         } else {
865             np = getnetbyname (ptr_word);
866             if (np != NULL && np->n_addrtype == AF_INET)
867                 a = htonl (*(u_int32_t *)np->n_net);
868             else
869                 a = inet_addr (ptr_word);
870         }
871
872         if (ptr_mask != NULL)
873             *ptr_mask = '/';
874
875         if (a == -1L)
876             syslog (LOG_WARNING,
877                     "unknown host %s in auth. address list",
878                     addrs->word);
879         else
880             if (((addr ^ a) & mask) == 0)
881                 return accept;
882     }
883     return 0;                   /* not in list => can't have it */
884 }
885
886 /*
887  * bad_ip_adrs - return 1 if the IP address is one we don't want
888  * to use, such as an address in the loopback net or a multicast address.
889  * addr is in network byte order.
890  */
891 int
892 bad_ip_adrs(addr)
893     u_int32_t addr;
894 {
895     addr = ntohl(addr);
896     return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
897         || IN_MULTICAST(addr) || IN_BADCLASS(addr);
898 }
899
900 /*
901  * check_access - complain if a secret file has too-liberal permissions.
902  */
903 void
904 check_access(f, filename)
905     FILE *f;
906     char *filename;
907 {
908     struct stat sbuf;
909
910     if (fstat(fileno(f), &sbuf) < 0) {
911         syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
912     } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
913         syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
914     }
915 }
916
917
918 /*
919  * scan_authfile - Scan an authorization file for a secret suitable
920  * for authenticating `client' on `server'.  The return value is -1
921  * if no secret is found, otherwise >= 0.  The return value has
922  * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
923  * NONWILD_SERVER set if the secret didn't have "*" for the server.
924  * Any following words on the line (i.e. address authorization
925  * info) are placed in a wordlist and returned in *addrs.  
926  */
927 static int
928 scan_authfile(f, client, server, secret, addrs, filename)
929     FILE *f;
930     char *client;
931     char *server;
932     char *secret;
933     struct wordlist **addrs;
934     char *filename;
935 {
936     int newline, xxx;
937     int got_flag, best_flag;
938     FILE *sf;
939     struct wordlist *ap, *addr_list, *addr_last;
940     char word[MAXWORDLEN];
941     char atfile[MAXWORDLEN];
942
943     if (addrs != NULL)
944         *addrs = NULL;
945     addr_list = NULL;
946     if (!getword(f, word, &newline, filename))
947         return -1;              /* file is empty??? */
948     newline = 1;
949     best_flag = -1;
950     for (;;) {
951         /*
952          * Skip until we find a word at the start of a line.
953          */
954         while (!newline && getword(f, word, &newline, filename))
955             ;
956         if (!newline)
957             break;              /* got to end of file */
958
959         /*
960          * Got a client - check if it's a match or a wildcard.
961          */
962         got_flag = 0;
963         if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
964             newline = 0;
965             continue;
966         }
967         if (!ISWILD(word))
968             got_flag = NONWILD_CLIENT;
969
970         /*
971          * Now get a server and check if it matches.
972          */
973         if (!getword(f, word, &newline, filename))
974             break;
975         if (newline)
976             continue;
977         if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
978             continue;
979         if (!ISWILD(word))
980             got_flag |= NONWILD_SERVER;
981
982         /*
983          * Got some sort of a match - see if it's better than what
984          * we have already.
985          */
986         if (got_flag <= best_flag)
987             continue;
988
989         /*
990          * Get the secret.
991          */
992         if (!getword(f, word, &newline, filename))
993             break;
994         if (newline)
995             continue;
996
997         /*
998          * Special syntax: @filename means read secret from file.
999          */
1000         if (word[0] == '@') {
1001             strcpy(atfile, word+1);
1002             if ((sf = fopen(atfile, "r")) == NULL) {
1003                 syslog(LOG_WARNING, "can't open indirect secret file %s",
1004                        atfile);
1005                 continue;
1006             }
1007             check_access(sf, atfile);
1008             if (!getword(sf, word, &xxx, atfile)) {
1009                 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1010                        atfile);
1011                 fclose(sf);
1012                 continue;
1013             }
1014             fclose(sf);
1015         }
1016         if (secret != NULL)
1017             strcpy(secret, word);
1018                 
1019         best_flag = got_flag;
1020
1021         /*
1022          * Now read address authorization info and make a wordlist.
1023          */
1024         if (addr_list)
1025             free_wordlist(addr_list);
1026         addr_list = addr_last = NULL;
1027         for (;;) {
1028             if (!getword(f, word, &newline, filename) || newline)
1029                 break;
1030             ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1031                                             + strlen(word));
1032             if (ap == NULL)
1033                 novm("authorized addresses");
1034             ap->next = NULL;
1035             strcpy(ap->word, word);
1036             if (addr_list == NULL)
1037                 addr_list = ap;
1038             else
1039                 addr_last->next = ap;
1040             addr_last = ap;
1041         }
1042         if (!newline)
1043             break;
1044     }
1045
1046     if (addrs != NULL)
1047         *addrs = addr_list;
1048     else if (addr_list != NULL)
1049         free_wordlist(addr_list);
1050
1051     return best_flag;
1052 }
1053
1054 /*
1055  * free_wordlist - release memory allocated for a wordlist.
1056  */
1057 static void
1058 free_wordlist(wp)
1059     struct wordlist *wp;
1060 {
1061     struct wordlist *next;
1062
1063     while (wp != NULL) {
1064         next = wp->next;
1065         free(wp);
1066         wp = next;
1067     }
1068 }