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