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