]> git.ozlabs.org Git - ppp.git/blob - pppd/auth.c
Add prosthesis for SunOS, which doesn't have strtoul.
[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.15 1995/05/19 03:16:12 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(sparc) && !defined(NeXT)
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 = strrchr(devnam, '/');
511     if (tty == NULL)
512         tty = devnam;
513     else
514         tty++;
515     logwtmp(tty, user, "");             /* Add wtmp login entry */
516     logged_in = TRUE;
517
518     return (UPAP_AUTHACK);
519 }
520
521 /*
522  * logout - Logout the user.
523  */
524 static void
525 logout()
526 {
527     char *tty;
528
529     tty = strrchr(devnam, '/');
530     if (tty == NULL)
531         tty = devnam;
532     else
533         tty++;
534     logwtmp(tty, "", "");               /* Wipe out wtmp logout entry */
535     logged_in = FALSE;
536 }
537
538
539 /*
540  * null_login - Check if a username of "" and a password of "" are
541  * acceptable, and iff so, set the list of acceptable IP addresses
542  * and return 1.
543  */
544 static int
545 null_login(unit)
546     int unit;
547 {
548     char *filename;
549     FILE *f;
550     int i, ret;
551     struct wordlist *addrs;
552     char secret[MAXWORDLEN];
553
554     /*
555      * Open the file of upap secrets and scan for a suitable secret.
556      * We don't accept a wildcard client.
557      */
558     filename = _PATH_UPAPFILE;
559     addrs = NULL;
560     f = fopen(filename, "r");
561     if (f == NULL)
562         return 0;
563     check_access(f, filename);
564
565     i = scan_authfile(f, "", our_name, secret, &addrs, filename);
566     ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
567
568     if (ret) {
569         if (addresses[unit] != NULL)
570             free_wordlist(addresses[unit]);
571         addresses[unit] = addrs;
572     }
573
574     fclose(f);
575     return ret;
576 }
577
578
579 /*
580  * get_upap_passwd - get a password for authenticating ourselves with
581  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
582  * could be found.
583  */
584 static int
585 get_upap_passwd()
586 {
587     char *filename;
588     FILE *f;
589     struct wordlist *addrs;
590     char secret[MAXWORDLEN];
591
592     filename = _PATH_UPAPFILE;
593     addrs = NULL;
594     f = fopen(filename, "r");
595     if (f == NULL)
596         return 0;
597     check_access(f, filename);
598     if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
599         return 0;
600     strncpy(passwd, secret, MAXSECRETLEN);
601     passwd[MAXSECRETLEN-1] = 0;
602     return 1;
603 }
604
605
606 /*
607  * have_upap_secret - check whether we have a PAP file with any
608  * secrets that we could possibly use for authenticating the peer.
609  */
610 static int
611 have_upap_secret()
612 {
613     FILE *f;
614     int ret;
615     char *filename;
616
617     filename = _PATH_UPAPFILE;
618     f = fopen(filename, "r");
619     if (f == NULL)
620         return 0;
621
622     ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
623     fclose(f);
624     if (ret < 0)
625         return 0;
626
627     return 1;
628 }
629
630
631 /*
632  * have_chap_secret - check whether we have a CHAP file with a
633  * secret that we could possibly use for authenticating `client'
634  * on `server'.  Either can be the null string, meaning we don't
635  * know the identity yet.
636  */
637 static int
638 have_chap_secret(client, server)
639     char *client;
640     char *server;
641 {
642     FILE *f;
643     int ret;
644     char *filename;
645
646     filename = _PATH_CHAPFILE;
647     f = fopen(filename, "r");
648     if (f == NULL)
649         return 0;
650
651     if (client[0] == 0)
652         client = NULL;
653     else if (server[0] == 0)
654         server = NULL;
655
656     ret = scan_authfile(f, client, server, NULL, NULL, filename);
657     fclose(f);
658     if (ret < 0)
659         return 0;
660
661     return 1;
662 }
663
664
665 /*
666  * get_secret - open the CHAP secret file and return the secret
667  * for authenticating the given client on the given server.
668  * (We could be either client or server).
669  */
670 int
671 get_secret(unit, client, server, secret, secret_len, save_addrs)
672     int unit;
673     char *client;
674     char *server;
675     char *secret;
676     int *secret_len;
677     int save_addrs;
678 {
679     FILE *f;
680     int ret, len;
681     char *filename;
682     struct wordlist *addrs;
683     char secbuf[MAXWORDLEN];
684
685     filename = _PATH_CHAPFILE;
686     addrs = NULL;
687     secbuf[0] = 0;
688
689     f = fopen(filename, "r");
690     if (f == NULL) {
691         syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
692         return 0;
693     }
694     check_access(f, filename);
695
696     ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
697     fclose(f);
698     if (ret < 0)
699         return 0;
700
701     if (save_addrs) {
702         if (addresses[unit] != NULL)
703             free_wordlist(addresses[unit]);
704         addresses[unit] = addrs;
705     }
706
707     len = strlen(secbuf);
708     if (len > MAXSECRETLEN) {
709         syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
710         len = MAXSECRETLEN;
711     }
712     BCOPY(secbuf, secret, len);
713     *secret_len = len;
714
715     return 1;
716 }
717
718 /*
719  * auth_ip_addr - check whether the peer is authorized to use
720  * a given IP address.  Returns 1 if authorized, 0 otherwise.
721  */
722 int
723 auth_ip_addr(unit, addr)
724     int unit;
725     u_int32_t addr;
726 {
727     u_int32_t a;
728     struct hostent *hp;
729     struct wordlist *addrs;
730
731     /* don't allow loopback or multicast address */
732     if (bad_ip_adrs(addr))
733         return 0;
734
735     if ((addrs = addresses[unit]) == NULL)
736         return 1;               /* no restriction */
737
738     for (; addrs != NULL; addrs = addrs->next) {
739         /* "-" means no addresses authorized */
740         if (strcmp(addrs->word, "-") == 0)
741             break;
742         if ((a = inet_addr(addrs->word)) == -1) {
743             if ((hp = gethostbyname(addrs->word)) == NULL) {
744                 syslog(LOG_WARNING, "unknown host %s in auth. address list",
745                        addrs->word);
746                 continue;
747             } else
748                 a = *(u_int32_t *)hp->h_addr;
749         }
750         if (addr == a)
751             return 1;
752     }
753     return 0;                   /* not in list => can't have it */
754 }
755
756 /*
757  * bad_ip_adrs - return 1 if the IP address is one we don't want
758  * to use, such as an address in the loopback net or a multicast address.
759  * addr is in network byte order.
760  */
761 int
762 bad_ip_adrs(addr)
763     u_int32_t addr;
764 {
765     addr = ntohl(addr);
766     return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
767         || IN_MULTICAST(addr) || IN_BADCLASS(addr);
768 }
769
770 /*
771  * check_access - complain if a secret file has too-liberal permissions.
772  */
773 void
774 check_access(f, filename)
775     FILE *f;
776     char *filename;
777 {
778     struct stat sbuf;
779
780     if (fstat(fileno(f), &sbuf) < 0) {
781         syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
782     } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
783         syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
784     }
785 }
786
787
788 /*
789  * scan_authfile - Scan an authorization file for a secret suitable
790  * for authenticating `client' on `server'.  The return value is -1
791  * if no secret is found, otherwise >= 0.  The return value has
792  * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
793  * NONWILD_SERVER set if the secret didn't have "*" for the server.
794  * Any following words on the line (i.e. address authorization
795  * info) are placed in a wordlist and returned in *addrs.  
796  */
797 static int
798 scan_authfile(f, client, server, secret, addrs, filename)
799     FILE *f;
800     char *client;
801     char *server;
802     char *secret;
803     struct wordlist **addrs;
804     char *filename;
805 {
806     int newline, xxx;
807     int got_flag, best_flag;
808     FILE *sf;
809     struct wordlist *ap, *addr_list, *addr_last;
810     char word[MAXWORDLEN];
811     char atfile[MAXWORDLEN];
812
813     if (addrs != NULL)
814         *addrs = NULL;
815     addr_list = NULL;
816     if (!getword(f, word, &newline, filename))
817         return -1;              /* file is empty??? */
818     newline = 1;
819     best_flag = -1;
820     for (;;) {
821         /*
822          * Skip until we find a word at the start of a line.
823          */
824         while (!newline && getword(f, word, &newline, filename))
825             ;
826         if (!newline)
827             break;              /* got to end of file */
828
829         /*
830          * Got a client - check if it's a match or a wildcard.
831          */
832         got_flag = 0;
833         if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
834             newline = 0;
835             continue;
836         }
837         if (!ISWILD(word))
838             got_flag = NONWILD_CLIENT;
839
840         /*
841          * Now get a server and check if it matches.
842          */
843         if (!getword(f, word, &newline, filename))
844             break;
845         if (newline)
846             continue;
847         if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
848             continue;
849         if (!ISWILD(word))
850             got_flag |= NONWILD_SERVER;
851
852         /*
853          * Got some sort of a match - see if it's better than what
854          * we have already.
855          */
856         if (got_flag <= best_flag)
857             continue;
858
859         /*
860          * Get the secret.
861          */
862         if (!getword(f, word, &newline, filename))
863             break;
864         if (newline)
865             continue;
866
867         /*
868          * Special syntax: @filename means read secret from file.
869          */
870         if (word[0] == '@') {
871             strcpy(atfile, word+1);
872             if ((sf = fopen(atfile, "r")) == NULL) {
873                 syslog(LOG_WARNING, "can't open indirect secret file %s",
874                        atfile);
875                 continue;
876             }
877             check_access(sf, atfile);
878             if (!getword(sf, word, &xxx, atfile)) {
879                 syslog(LOG_WARNING, "no secret in indirect secret file %s",
880                        atfile);
881                 fclose(sf);
882                 continue;
883             }
884             fclose(sf);
885         }
886         if (secret != NULL)
887             strcpy(secret, word);
888                 
889         best_flag = got_flag;
890
891         /*
892          * Now read address authorization info and make a wordlist.
893          */
894         if (addr_list)
895             free_wordlist(addr_list);
896         addr_list = addr_last = NULL;
897         for (;;) {
898             if (!getword(f, word, &newline, filename) || newline)
899                 break;
900             ap = (struct wordlist *) malloc(sizeof(struct wordlist)
901                                             + strlen(word));
902             if (ap == NULL)
903                 novm("authorized addresses");
904             ap->next = NULL;
905             strcpy(ap->word, word);
906             if (addr_list == NULL)
907                 addr_list = ap;
908             else
909                 addr_last->next = ap;
910             addr_last = ap;
911         }
912         if (!newline)
913             break;
914     }
915
916     if (addrs != NULL)
917         *addrs = addr_list;
918     else if (addr_list != NULL)
919         free_wordlist(addr_list);
920
921     return best_flag;
922 }
923
924 /*
925  * free_wordlist - release memory allocated for a wordlist.
926  */
927 static void
928 free_wordlist(wp)
929     struct wordlist *wp;
930 {
931     struct wordlist *next;
932
933     while (wp != NULL) {
934         next = wp->next;
935         free(wp);
936         wp = next;
937     }
938 }