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