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