]> git.ozlabs.org Git - ppp.git/blob - pppd/auth.c
MD4Update takes bytes not bits! Now the regression test of the README.MSCHAP80
[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.39 1998/11/07 06:59:25 paulus Exp $";
37 #endif
38
39 #include <stdio.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <syslog.h>
44 #include <pwd.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/socket.h>
49 #include <utmp.h>
50 #include <fcntl.h>
51 #if defined(_PATH_LASTLOG) && defined(_linux_)
52 #include <lastlog.h>
53 #endif
54
55 #include <netdb.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58
59 #ifdef USE_PAM
60 #include <security/pam_appl.h>
61 #endif
62
63 #ifdef HAS_SHADOW
64 #include <shadow.h>
65 #ifndef PW_PPP
66 #define PW_PPP PW_LOGIN
67 #endif
68 #endif
69
70 #include "pppd.h"
71 #include "fsm.h"
72 #include "lcp.h"
73 #include "ipcp.h"
74 #include "upap.h"
75 #include "chap.h"
76 #ifdef CBCP_SUPPORT
77 #include "cbcp.h"
78 #endif
79 #include "pathnames.h"
80
81 /* Used for storing a sequence of words.  Usually malloced. */
82 struct wordlist {
83     struct wordlist     *next;
84     char                word[1];
85 };
86
87 /* Bits in scan_authfile return value */
88 #define NONWILD_SERVER  1
89 #define NONWILD_CLIENT  2
90
91 #define ISWILD(word)    (word[0] == '*' && word[1] == 0)
92
93 #define FALSE   0
94 #define TRUE    1
95
96 /* The name by which the peer authenticated itself to us. */
97 char peer_authname[MAXNAMELEN];
98
99 /* Records which authentication operations haven't completed yet. */
100 static int auth_pending[NUM_PPP];
101
102 /* Set if we have successfully called plogin() */
103 static int logged_in;
104
105 /* Set if we have run the /etc/ppp/auth-up script. */
106 static int did_authup;
107 static pid_t authup_pid;        /* process ID of auth-up/down script */
108
109 /* List of addresses which the peer may use. */
110 static struct wordlist *addresses[NUM_PPP];
111
112 /* Number of network protocols which we have opened. */
113 static int num_np_open;
114
115 /* Number of network protocols which have come up. */
116 static int num_np_up;
117
118 /* Set if we got the contents of passwd[] from the pap-secrets file. */
119 static int passwd_from_file;
120
121 /*
122  * Option variables.
123  */
124 bool uselogin = 0;              /* Use /etc/passwd for checking PAP */
125 bool cryptpap = 0;              /* Passwords in pap-secrets are encrypted */
126 bool refuse_pap = 0;            /* Don't wanna auth. ourselves with PAP */
127 bool refuse_chap = 0;           /* Don't wanna auth. ourselves with CHAP */
128 bool usehostname = 0;           /* Use hostname for our_name */
129 bool auth_required = 0;         /* Always require authentication from peer */
130 bool allow_any_ip = 0;          /* Allow peer to use any IP address */
131
132 /* Bits in auth_pending[] */
133 #define PAP_WITHPEER    1
134 #define PAP_PEER        2
135 #define CHAP_WITHPEER   4
136 #define CHAP_PEER       8
137
138 extern char *crypt __P((const char *, const char *));
139
140 /* Prototypes for procedures local to this file. */
141
142 static void network_phase __P((int));
143 static void check_idle __P((void *));
144 static void connect_time_expired __P((void *));
145 static int  plogin __P((char *, char *, char **, int *));
146 static void plogout __P((void));
147 static int  null_login __P((int));
148 static int  get_pap_passwd __P((char *));
149 static int  have_pap_secret __P((void));
150 static int  have_chap_secret __P((char *, char *, u_int32_t));
151 static int  ip_addr_check __P((u_int32_t, struct wordlist *));
152 static int  scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
153                                struct wordlist **, char *));
154 static void free_wordlist __P((struct wordlist *));
155 static void auth_script __P((char *));
156 static void set_allowed_addrs __P((int, struct wordlist *));
157
158 #ifdef OLD_OPTIONS
159 static int setupapfile __P((char **));
160 #endif
161
162 /*
163  * Authentication-related options.
164  */
165 option_t auth_options[] = {
166     { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
167       "Require PAP authentication from peer", 1, &auth_required },
168     { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
169       "Require PAP authentication from peer", 1, &auth_required },
170     { "refuse-pap", o_bool, &refuse_pap,
171       "Don't agree to auth to peer with PAP", 1 },
172     { "-pap", o_bool, &refuse_pap,
173       "Don't allow UPAP authentication with peer", 1 },
174     { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
175       "Require CHAP authentication from peer", 1, &auth_required },
176     { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
177       "Require CHAP authentication from peer", 1, &auth_required },
178     { "refuse-chap", o_bool, &refuse_chap,
179       "Don't agree to auth to peer with CHAP", 1 },
180     { "-chap", o_bool, &refuse_chap,
181       "Don't allow CHAP authentication with peer", 1 },
182     { "name", o_string, our_name,
183       "Set local name for authentication",
184       OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
185     { "user", o_string, user,
186       "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
187     { "usehostname", o_bool, &usehostname,
188       "Must use hostname for authentication", 1 },
189     { "remotename", o_string, remote_name,
190       "Set remote name for authentication", OPT_STATIC, NULL, MAXNAMELEN },
191     { "auth", o_bool, &auth_required,
192       "Require authentication from peer", 1 },
193     { "noauth", o_bool, &auth_required,
194       "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
195     {  "login", o_bool, &uselogin,
196       "Use system password database for PAP", 1 },
197     { "papcrypt", o_bool, &cryptpap,
198       "PAP passwords are encrypted", 1 },
199 #if OLD_OPTIONS
200     { "+ua", o_special, setupapfile,
201       "Get PAP user and password from file" },
202 #endif
203     { NULL }
204 };
205
206 #if OLD_OPTIONS
207 /*
208  * setupapfile - specifies UPAP info for authenticating with peer.
209  */
210 static int
211 setupapfile(argv)
212     char **argv;
213 {
214     FILE * ufile;
215     int l;
216
217     lcp_allowoptions[0].neg_upap = 1;
218
219     /* open user info file */
220     if ((ufile = fopen(*argv, "r")) == NULL) {
221         option_error("unable to open user login data file %s", *argv);
222         return 0;
223     }
224     if (!readable(fileno(ufile))) {
225         option_error("%s: access denied", *argv);
226         return 0;
227     }
228     check_access(ufile, *argv);
229
230     /* get username */
231     if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
232         || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
233         option_error("unable to read user login data file %s", *argv);
234         return 0;
235     }
236     fclose(ufile);
237
238     /* get rid of newlines */
239     l = strlen(user);
240     if (l > 0 && user[l-1] == '\n')
241         user[l-1] = 0;
242     l = strlen(passwd);
243     if (l > 0 && passwd[l-1] == '\n')
244         passwd[l-1] = 0;
245
246     return (1);
247 }
248 #endif
249
250
251 /*
252  * An Open on LCP has requested a change from Dead to Establish phase.
253  * Do what's necessary to bring the physical layer up.
254  */
255 void
256 link_required(unit)
257     int unit;
258 {
259 }
260
261 /*
262  * LCP has terminated the link; go to the Dead phase and take the
263  * physical layer down.
264  */
265 void
266 link_terminated(unit)
267     int unit;
268 {
269     if (phase == PHASE_DEAD)
270         return;
271     if (logged_in)
272         plogout();
273     phase = PHASE_DEAD;
274     syslog(LOG_NOTICE, "Connection terminated.");
275 }
276
277 /*
278  * LCP has gone down; it will either die or try to re-establish.
279  */
280 void
281 link_down(unit)
282     int unit;
283 {
284     int i;
285     struct protent *protp;
286
287     if (did_authup) {
288         auth_script(_PATH_AUTHDOWN);
289         did_authup = 0;
290     }
291     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
292         if (!protp->enabled_flag)
293             continue;
294         if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
295             (*protp->lowerdown)(unit);
296         if (protp->protocol < 0xC000 && protp->close != NULL)
297             (*protp->close)(unit, "LCP down");
298     }
299     num_np_open = 0;
300     num_np_up = 0;
301     if (phase != PHASE_DEAD)
302         phase = PHASE_TERMINATE;
303 }
304
305 /*
306  * The link is established.
307  * Proceed to the Dead, Authenticate or Network phase as appropriate.
308  */
309 void
310 link_established(unit)
311     int unit;
312 {
313     int auth;
314     lcp_options *wo = &lcp_wantoptions[unit];
315     lcp_options *go = &lcp_gotoptions[unit];
316     lcp_options *ho = &lcp_hisoptions[unit];
317     int i;
318     struct protent *protp;
319
320     /*
321      * Tell higher-level protocols that LCP is up.
322      */
323     for (i = 0; (protp = protocols[i]) != NULL; ++i)
324         if (protp->protocol != PPP_LCP && protp->enabled_flag
325             && protp->lowerup != NULL)
326             (*protp->lowerup)(unit);
327
328     if (auth_required && !(go->neg_chap || go->neg_upap)) {
329         /*
330          * We wanted the peer to authenticate itself, and it refused:
331          * treat it as though it authenticated with PAP using a username
332          * of "" and a password of "".  If that's not OK, boot it out.
333          */
334         if (!wo->neg_upap || !null_login(unit)) {
335             syslog(LOG_WARNING, "peer refused to authenticate");
336             lcp_close(unit, "peer refused to authenticate");
337             return;
338         }
339     }
340
341     phase = PHASE_AUTHENTICATE;
342     auth = 0;
343     if (go->neg_chap) {
344         ChapAuthPeer(unit, our_name, go->chap_mdtype);
345         auth |= CHAP_PEER;
346     } else if (go->neg_upap) {
347         upap_authpeer(unit);
348         auth |= PAP_PEER;
349     }
350     if (ho->neg_chap) {
351         ChapAuthWithPeer(unit, user, ho->chap_mdtype);
352         auth |= CHAP_WITHPEER;
353     } else if (ho->neg_upap) {
354         if (passwd[0] == 0) {
355             passwd_from_file = 1;
356             if (!get_pap_passwd(passwd))
357                 syslog(LOG_ERR, "No secret found for PAP login");
358         }
359         upap_authwithpeer(unit, user, passwd);
360         auth |= PAP_WITHPEER;
361     }
362     auth_pending[unit] = auth;
363
364     if (!auth)
365         network_phase(unit);
366 }
367
368 /*
369  * Proceed to the network phase.
370  */
371 static void
372 network_phase(unit)
373     int unit;
374 {
375     int i;
376     struct protent *protp;
377     lcp_options *go = &lcp_gotoptions[unit];
378
379     /*
380      * If the peer had to authenticate, run the auth-up script now.
381      */
382     if ((go->neg_chap || go->neg_upap) && !did_authup) {
383         auth_script(_PATH_AUTHUP);
384         did_authup = 1;
385     }
386
387 #ifdef CBCP_SUPPORT
388     /*
389      * If we negotiated callback, do it now.
390      */
391     if (go->neg_cbcp) {
392         phase = PHASE_CALLBACK;
393         (*cbcp_protent.open)(unit);
394         return;
395     }
396 #endif
397
398     phase = PHASE_NETWORK;
399 #if 0
400     if (!demand)
401         set_filters(&pass_filter, &active_filter);
402 #endif
403     for (i = 0; (protp = protocols[i]) != NULL; ++i)
404         if (protp->protocol < 0xC000 && protp->enabled_flag
405             && protp->open != NULL) {
406             (*protp->open)(unit);
407             if (protp->protocol != PPP_CCP)
408                 ++num_np_open;
409         }
410
411     if (num_np_open == 0)
412         /* nothing to do */
413         lcp_close(0, "No network protocols running");
414 }
415
416 /*
417  * The peer has failed to authenticate himself using `protocol'.
418  */
419 void
420 auth_peer_fail(unit, protocol)
421     int unit, protocol;
422 {
423     /*
424      * Authentication failure: take the link down
425      */
426     lcp_close(unit, "Authentication failed");
427 }
428
429 /*
430  * The peer has been successfully authenticated using `protocol'.
431  */
432 void
433 auth_peer_success(unit, protocol, name, namelen)
434     int unit, protocol;
435     char *name;
436     int namelen;
437 {
438     int bit;
439
440     switch (protocol) {
441     case PPP_CHAP:
442         bit = CHAP_PEER;
443         break;
444     case PPP_PAP:
445         bit = PAP_PEER;
446         break;
447     default:
448         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
449                protocol);
450         return;
451     }
452
453     /*
454      * Save the authenticated name of the peer for later.
455      */
456     if (namelen > sizeof(peer_authname) - 1)
457         namelen = sizeof(peer_authname) - 1;
458     BCOPY(name, peer_authname, namelen);
459     peer_authname[namelen] = 0;
460     script_setenv("PEERNAME", peer_authname);
461
462     /*
463      * If there is no more authentication still to be done,
464      * proceed to the network (or callback) phase.
465      */
466     if ((auth_pending[unit] &= ~bit) == 0)
467         network_phase(unit);
468 }
469
470 /*
471  * We have failed to authenticate ourselves to the peer using `protocol'.
472  */
473 void
474 auth_withpeer_fail(unit, protocol)
475     int unit, protocol;
476 {
477     if (passwd_from_file)
478         BZERO(passwd, MAXSECRETLEN);
479     /*
480      * We've failed to authenticate ourselves to our peer.
481      * He'll probably take the link down, and there's not much
482      * we can do except wait for that.
483      */
484 }
485
486 /*
487  * We have successfully authenticated ourselves with the peer using `protocol'.
488  */
489 void
490 auth_withpeer_success(unit, protocol)
491     int unit, protocol;
492 {
493     int bit;
494
495     switch (protocol) {
496     case PPP_CHAP:
497         bit = CHAP_WITHPEER;
498         break;
499     case PPP_PAP:
500         if (passwd_from_file)
501             BZERO(passwd, MAXSECRETLEN);
502         bit = PAP_WITHPEER;
503         break;
504     default:
505         syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
506                protocol);
507         bit = 0;
508     }
509
510     /*
511      * If there is no more authentication still being done,
512      * proceed to the network (or callback) phase.
513      */
514     if ((auth_pending[unit] &= ~bit) == 0)
515         network_phase(unit);
516 }
517
518
519 /*
520  * np_up - a network protocol has come up.
521  */
522 void
523 np_up(unit, proto)
524     int unit, proto;
525 {
526     if (num_np_up == 0) {
527         /*
528          * At this point we consider that the link has come up successfully.
529          */
530         need_holdoff = 0;
531
532         if (idle_time_limit > 0)
533             TIMEOUT(check_idle, NULL, idle_time_limit);
534
535         /*
536          * Set a timeout to close the connection once the maximum
537          * connect time has expired.
538          */
539         if (maxconnect > 0)
540             TIMEOUT(connect_time_expired, 0, maxconnect);
541
542         /*
543          * Detach now, if the updetach option was given.
544          */
545         if (updetach && !nodetach)
546             detach();
547     }
548     ++num_np_up;
549 }
550
551 /*
552  * np_down - a network protocol has gone down.
553  */
554 void
555 np_down(unit, proto)
556     int unit, proto;
557 {
558     if (--num_np_up == 0 && idle_time_limit > 0) {
559         UNTIMEOUT(check_idle, NULL);
560     }
561 }
562
563 /*
564  * np_finished - a network protocol has finished using the link.
565  */
566 void
567 np_finished(unit, proto)
568     int unit, proto;
569 {
570     if (--num_np_open <= 0) {
571         /* no further use for the link: shut up shop. */
572         lcp_close(0, "No network protocols running");
573     }
574 }
575
576 /*
577  * check_idle - check whether the link has been idle for long
578  * enough that we can shut it down.
579  */
580 static void
581 check_idle(arg)
582      void *arg;
583 {
584     struct ppp_idle idle;
585     time_t itime;
586
587     if (!get_idle_time(0, &idle))
588         return;
589     itime = MIN(idle.xmit_idle, idle.recv_idle);
590     if (itime >= idle_time_limit) {
591         /* link is idle: shut it down. */
592         syslog(LOG_INFO, "Terminating connection due to lack of activity.");
593         lcp_close(0, "Link inactive");
594     } else {
595         TIMEOUT(check_idle, NULL, idle_time_limit - itime);
596     }
597 }
598
599 /*
600  * connect_time_expired - log a message and close the connection.
601  */
602 static void
603 connect_time_expired(arg)
604     void *arg;
605 {
606     syslog(LOG_INFO, "Connect time expired");
607     lcp_close(0, "Connect time expired");       /* Close connection */
608 }
609
610 /*
611  * auth_check_options - called to check authentication options.
612  */
613 void
614 auth_check_options()
615 {
616     lcp_options *wo = &lcp_wantoptions[0];
617     int can_auth;
618     ipcp_options *ipwo = &ipcp_wantoptions[0];
619     u_int32_t remote;
620
621     /* Default our_name to hostname, and user to our_name */
622     if (our_name[0] == 0 || usehostname)
623         strcpy(our_name, hostname);
624     if (user[0] == 0)
625         strcpy(user, our_name);
626
627     /* If authentication is required, ask peer for CHAP or PAP. */
628     if (auth_required) {
629         if (!wo->neg_chap && !wo->neg_upap) {
630             wo->neg_chap = 1;
631             wo->neg_upap = 1;
632         }
633     } else {
634         wo->neg_chap = 0;
635         wo->neg_upap = 0;
636     }
637
638     /*
639      * Check whether we have appropriate secrets to use
640      * to authenticate the peer.
641      */
642     can_auth = wo->neg_upap && (uselogin || have_pap_secret());
643     if (!can_auth && wo->neg_chap) {
644         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
645         can_auth = have_chap_secret(remote_name, our_name, remote);
646     }
647
648     if (auth_required && !can_auth) {
649         option_error("peer authentication required but no suitable secret(s) found\n");
650         if (remote_name[0] == 0)
651             option_error("for authenticating any peer to us (%s)\n", our_name);
652         else
653             option_error("for authenticating peer %s to us (%s)\n",
654                          remote_name, our_name);
655         exit(1);
656     }
657
658     /*
659      * Check whether the user tried to override certain values
660      * set by root.
661      */
662     if (allow_any_ip) {
663         if (!default_device && devnam_info.priv == 0) {
664             option_error("can't override device name when noauth option used");
665             exit(1);
666         }
667         if ((connector != NULL && connector_info.priv == 0)
668             || (disconnector != NULL && disconnector_info.priv == 0)
669             || (welcomer != NULL && welcomer_info.priv == 0)) {
670             option_error("connect, disconnect and welcome options");
671             option_error("are privileged when noauth option is used");
672             exit(1);
673         }
674     }
675 }
676
677 /*
678  * auth_reset - called when LCP is starting negotiations to recheck
679  * authentication options, i.e. whether we have appropriate secrets
680  * to use for authenticating ourselves and/or the peer.
681  */
682 void
683 auth_reset(unit)
684     int unit;
685 {
686     lcp_options *go = &lcp_gotoptions[unit];
687     lcp_options *ao = &lcp_allowoptions[0];
688     ipcp_options *ipwo = &ipcp_wantoptions[0];
689     u_int32_t remote;
690
691     ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
692     ao->neg_chap = !refuse_chap
693         && have_chap_secret(user, remote_name, (u_int32_t)0);
694
695     if (go->neg_upap && !uselogin && !have_pap_secret())
696         go->neg_upap = 0;
697     if (go->neg_chap) {
698         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
699         if (!have_chap_secret(remote_name, our_name, remote))
700             go->neg_chap = 0;
701     }
702 }
703
704
705 /*
706  * check_passwd - Check the user name and passwd against the PAP secrets
707  * file.  If requested, also check against the system password database,
708  * and login the user if OK.
709  *
710  * returns:
711  *      UPAP_AUTHNAK: Authentication failed.
712  *      UPAP_AUTHACK: Authentication succeeded.
713  * In either case, msg points to an appropriate message.
714  */
715 int
716 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
717     int unit;
718     char *auser;
719     int userlen;
720     char *apasswd;
721     int passwdlen;
722     char **msg;
723     int *msglen;
724 {
725     int ret;
726     char *filename;
727     FILE *f;
728     struct wordlist *addrs;
729     u_int32_t remote;
730     ipcp_options *ipwo = &ipcp_wantoptions[unit];
731     char passwd[256], user[256];
732     char secret[MAXWORDLEN];
733     static int attempts = 0;
734
735     /*
736      * Make copies of apasswd and auser, then null-terminate them.
737      */
738     BCOPY(apasswd, passwd, passwdlen);
739     passwd[passwdlen] = '\0';
740     BCOPY(auser, user, userlen);
741     user[userlen] = '\0';
742     *msg = (char *) 0;
743
744     /*
745      * Open the file of pap secrets and scan for a suitable secret
746      * for authenticating this user.
747      */
748     filename = _PATH_UPAPFILE;
749     addrs = NULL;
750     ret = UPAP_AUTHACK;
751     f = fopen(filename, "r");
752     if (f == NULL) {
753         syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
754         ret = UPAP_AUTHNAK;
755
756     } else {
757         check_access(f, filename);
758         remote = ipwo->accept_remote? 0: ipwo->hisaddr;
759         if (scan_authfile(f, user, our_name, remote,
760                           secret, &addrs, filename) < 0
761             || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
762                 && strcmp(crypt(passwd, secret), secret) != 0)) {
763             syslog(LOG_WARNING, "PAP authentication failure for %s", user);
764             ret = UPAP_AUTHNAK;
765         }
766         fclose(f);
767     }
768
769     if (uselogin && ret == UPAP_AUTHACK) {
770         ret = plogin(user, passwd, msg, msglen);
771         if (ret == UPAP_AUTHNAK) {
772             syslog(LOG_WARNING, "PAP login failure for %s", user);
773         }
774     }
775
776     if (ret == UPAP_AUTHNAK) {
777         if (*msg == (char *) 0)
778             *msg = "Login incorrect";
779         *msglen = strlen(*msg);
780         /*
781          * Frustrate passwd stealer programs.
782          * Allow 10 tries, but start backing off after 3 (stolen from login).
783          * On 10'th, drop the connection.
784          */
785         if (attempts++ >= 10) {
786             syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
787                    attempts, devnam, user);
788             quit();
789         }
790         if (attempts > 3)
791             sleep((u_int) (attempts - 3) * 5);
792         if (addrs != NULL)
793             free_wordlist(addrs);
794
795     } else {
796         attempts = 0;                   /* Reset count */
797         if (*msg == (char *) 0)
798             *msg = "Login ok";
799         *msglen = strlen(*msg);
800         set_allowed_addrs(unit, addrs);
801     }
802
803     BZERO(passwd, sizeof(passwd));
804     BZERO(secret, sizeof(secret));
805
806     return ret;
807 }
808
809 /*
810  * This function is needed for PAM.
811  */
812
813 #ifdef USE_PAM
814 static char *PAM_username = "";
815 static char *PAM_password = "";
816
817 #ifdef PAM_ESTABLISH_CRED       /* new PAM defines :(^ */
818 #define MY_PAM_STRERROR(err_code)  (char *) pam_strerror(pamh,err_code)
819 #else
820 #define MY_PAM_STRERROR(err_code)  (char *) pam_strerror(err_code)
821 #endif
822
823 static int pam_conv (int num_msg,
824                      const struct pam_message **msg,
825                      struct pam_response **resp,
826                      void *appdata_ptr)
827 {
828     int count = 0, replies = 0;
829     struct pam_response *reply = NULL;
830     int size = 0;
831
832     for (count = 0; count < num_msg; count++)
833       {
834         size += sizeof (struct pam_response);
835         reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
836         if (!reply)
837             return PAM_CONV_ERR;
838
839         switch (msg[count]->msg_style)
840           {
841         case PAM_PROMPT_ECHO_ON:
842             reply[replies].resp_retcode = PAM_SUCCESS;
843             reply[replies++].resp = strdup(PAM_username); /* never NULL */
844             break;
845
846         case PAM_PROMPT_ECHO_OFF:
847             reply[replies].resp_retcode = PAM_SUCCESS;
848             reply[replies++].resp = strdup(PAM_password); /* never NULL */
849             break;
850
851         case PAM_TEXT_INFO:
852             reply[replies].resp_retcode = PAM_SUCCESS;
853             reply[replies++].resp = NULL;
854             break;
855
856         case PAM_ERROR_MSG:
857         default:
858             free (reply);
859             return PAM_CONV_ERR;
860           }
861       }
862
863     if (resp)
864         *resp = reply;
865     else
866         free (reply);
867
868     return PAM_SUCCESS;
869 }
870 #endif
871
872 /*
873  * plogin - Check the user name and password against the system
874  * password database, and login the user if OK.
875  *
876  * returns:
877  *      UPAP_AUTHNAK: Login failed.
878  *      UPAP_AUTHACK: Login succeeded.
879  * In either case, msg points to an appropriate message.
880  */
881
882 static int
883 plogin(user, passwd, msg, msglen)
884     char *user;
885     char *passwd;
886     char **msg;
887     int *msglen;
888 {
889
890 #ifdef USE_PAM
891
892     struct pam_conv pam_conversation;
893     pam_handle_t *pamh;
894     int pam_error;
895 /*
896  * Fill the pam_conversion structure
897  */
898     memset (&pam_conversation, '\0', sizeof (struct pam_conv));
899     pam_conversation.conv = &pam_conv;
900
901     pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
902
903     if (pam_error != PAM_SUCCESS) {
904         *msg = MY_PAM_STRERROR (pam_error);
905         return UPAP_AUTHNAK;
906     }
907 /*
908  * Define the fields for the credintial validation
909  */
910     (void) pam_set_item (pamh, PAM_TTY, devnam);
911     PAM_username = user;
912     PAM_password = passwd;
913 /*
914  * Validate the user
915  */
916     pam_error = pam_authenticate (pamh, PAM_SILENT);
917     if (pam_error == PAM_SUCCESS) {
918         pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
919
920         /* start a session for this user. Session closed when link ends. */
921         if (pam_error == PAM_SUCCESS)
922            (void) pam_open_session (pamh, PAM_SILENT);
923     }
924
925     *msg = MY_PAM_STRERROR (pam_error);
926
927     PAM_username =
928     PAM_password = "";
929 /*
930  * Clean up the mess
931  */
932     (void) pam_end (pamh, pam_error);
933
934     if (pam_error != PAM_SUCCESS)
935         return UPAP_AUTHNAK;
936 /*
937  * Use the non-PAM methods directly
938  */
939 #else /* #ifdef USE_PAM */
940
941     struct passwd *pw;
942     char *tty;
943
944 #ifdef HAS_SHADOW
945     struct spwd *spwd;
946     struct spwd *getspnam();
947 #endif
948
949     pw = getpwnam(user);
950     endpwent();
951     if (pw == NULL) {
952         return (UPAP_AUTHNAK);
953     }
954
955 #ifdef HAS_SHADOW
956     spwd = getspnam(user);
957     endspent();
958     if (spwd) {
959         /* check the age of the password entry */
960         long now = time(NULL) / 86400L;
961
962         if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
963             || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
964                 && spwd->sp_lstchg >= 0
965                 && now >= spwd->sp_lstchg + spwd->sp_max)) {
966             syslog(LOG_WARNING, "Password for %s has expired", user);
967             return (UPAP_AUTHNAK);
968         }
969         pw->pw_passwd = spwd->sp_pwdp;
970     }
971 #endif
972
973     /*
974      * If no passwd, don't let them login.
975      */
976     if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
977         || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
978         return (UPAP_AUTHNAK);
979
980     /* These functions are not enabled for PAM. The reason for this is that */
981     /* there is not necessarily a "passwd" entry for this user. That is     */
982     /* real purpose of 'PAM' -- to virtualize the account data from the     */
983     /* application. If you want to do the same thing, write the entry in    */
984     /* the 'session' hook.                                                  */
985
986     /*
987      * Write a wtmp entry for this user.
988      */
989
990     tty = devnam;
991     if (strncmp(tty, "/dev/", 5) == 0)
992         tty += 5;
993     logwtmp(tty, user, remote_name);            /* Add wtmp login entry */
994
995 #if defined(_PATH_LASTLOG)
996     {
997             struct lastlog ll;
998             int fd;
999
1000             if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1001                 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1002                 memset((void *)&ll, 0, sizeof(ll));
1003                 (void)time(&ll.ll_time);
1004                 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1005                 (void)write(fd, (char *)&ll, sizeof(ll));
1006                 (void)close(fd);
1007             }
1008     }
1009 #endif
1010
1011 #endif /* #ifdef USE_PAM */
1012
1013     syslog(LOG_INFO, "user %s logged in", user);
1014     logged_in = TRUE;
1015
1016     return (UPAP_AUTHACK);
1017 }
1018
1019 /*
1020  * plogout - Logout the user.
1021  */
1022 static void
1023 plogout()
1024 {
1025 #ifdef USE_PAM
1026     struct pam_conv pam_conversation;
1027     pam_handle_t *pamh;
1028     int pam_error;
1029 /*
1030  * Fill the pam_conversion structure. The PAM specification states that the
1031  * session must be able to be closed by a totally different handle from which
1032  * it was created. Hold the PAM group to their own specification!
1033  */
1034     memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1035     pam_conversation.conv = &pam_conv;
1036
1037     pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1038     if (pam_error == PAM_SUCCESS) {
1039         (void) pam_set_item (pamh, PAM_TTY, devnam);
1040         (void) pam_close_session (pamh, PAM_SILENT);
1041         (void) pam_end (pamh, PAM_SUCCESS);
1042     }
1043
1044 #else
1045     char *tty;
1046
1047     tty = devnam;
1048     if (strncmp(tty, "/dev/", 5) == 0)
1049         tty += 5;
1050     logwtmp(tty, "", "");               /* Wipe out utmp logout entry */
1051 #endif
1052
1053     logged_in = FALSE;
1054 }
1055
1056
1057 /*
1058  * null_login - Check if a username of "" and a password of "" are
1059  * acceptable, and iff so, set the list of acceptable IP addresses
1060  * and return 1.
1061  */
1062 static int
1063 null_login(unit)
1064     int unit;
1065 {
1066     char *filename;
1067     FILE *f;
1068     int i, ret;
1069     struct wordlist *addrs;
1070     char secret[MAXWORDLEN];
1071
1072     /*
1073      * Open the file of pap secrets and scan for a suitable secret.
1074      * We don't accept a wildcard client.
1075      */
1076     filename = _PATH_UPAPFILE;
1077     addrs = NULL;
1078     f = fopen(filename, "r");
1079     if (f == NULL)
1080         return 0;
1081     check_access(f, filename);
1082
1083     i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1084     ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1085     BZERO(secret, sizeof(secret));
1086
1087     if (ret)
1088         set_allowed_addrs(unit, addrs);
1089     else
1090         free_wordlist(addrs);
1091
1092     fclose(f);
1093     return ret;
1094 }
1095
1096
1097 /*
1098  * get_pap_passwd - get a password for authenticating ourselves with
1099  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
1100  * could be found.
1101  */
1102 static int
1103 get_pap_passwd(passwd)
1104     char *passwd;
1105 {
1106     char *filename;
1107     FILE *f;
1108     int ret;
1109     struct wordlist *addrs;
1110     char secret[MAXWORDLEN];
1111
1112     filename = _PATH_UPAPFILE;
1113     addrs = NULL;
1114     f = fopen(filename, "r");
1115     if (f == NULL)
1116         return 0;
1117     check_access(f, filename);
1118     ret = scan_authfile(f, user,
1119                         remote_name[0]? remote_name: NULL,
1120                         (u_int32_t)0, secret, NULL, filename);
1121     fclose(f);
1122     if (ret < 0)
1123         return 0;
1124     if (passwd != NULL) {
1125         strncpy(passwd, secret, MAXSECRETLEN);
1126         passwd[MAXSECRETLEN-1] = 0;
1127     }
1128     BZERO(secret, sizeof(secret));
1129     return 1;
1130 }
1131
1132
1133 /*
1134  * have_pap_secret - check whether we have a PAP file with any
1135  * secrets that we could possibly use for authenticating the peer.
1136  */
1137 static int
1138 have_pap_secret()
1139 {
1140     FILE *f;
1141     int ret;
1142     char *filename;
1143     ipcp_options *ipwo = &ipcp_wantoptions[0];
1144     u_int32_t remote;
1145
1146     filename = _PATH_UPAPFILE;
1147     f = fopen(filename, "r");
1148     if (f == NULL)
1149         return 0;
1150
1151     remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1152     ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1153     fclose(f);
1154     if (ret < 0)
1155         return 0;
1156
1157     return 1;
1158 }
1159
1160
1161 /*
1162  * have_chap_secret - check whether we have a CHAP file with a
1163  * secret that we could possibly use for authenticating `client'
1164  * on `server'.  Either can be the null string, meaning we don't
1165  * know the identity yet.
1166  */
1167 static int
1168 have_chap_secret(client, server, remote)
1169     char *client;
1170     char *server;
1171     u_int32_t remote;
1172 {
1173     FILE *f;
1174     int ret;
1175     char *filename;
1176
1177     filename = _PATH_CHAPFILE;
1178     f = fopen(filename, "r");
1179     if (f == NULL)
1180         return 0;
1181
1182     if (client[0] == 0)
1183         client = NULL;
1184     else if (server[0] == 0)
1185         server = NULL;
1186
1187     ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1188     fclose(f);
1189     if (ret < 0)
1190         return 0;
1191
1192     return 1;
1193 }
1194
1195
1196 /*
1197  * get_secret - open the CHAP secret file and return the secret
1198  * for authenticating the given client on the given server.
1199  * (We could be either client or server).
1200  */
1201 int
1202 get_secret(unit, client, server, secret, secret_len, save_addrs)
1203     int unit;
1204     char *client;
1205     char *server;
1206     char *secret;
1207     int *secret_len;
1208     int save_addrs;
1209 {
1210     FILE *f;
1211     int ret, len;
1212     char *filename;
1213     struct wordlist *addrs;
1214     char secbuf[MAXWORDLEN];
1215
1216     filename = _PATH_CHAPFILE;
1217     addrs = NULL;
1218     secbuf[0] = 0;
1219
1220     f = fopen(filename, "r");
1221     if (f == NULL) {
1222         syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1223         return 0;
1224     }
1225     check_access(f, filename);
1226
1227     ret = scan_authfile(f, client, server, (u_int32_t)0,
1228                         secbuf, &addrs, filename);
1229     fclose(f);
1230     if (ret < 0)
1231         return 0;
1232
1233     if (save_addrs)
1234         set_allowed_addrs(unit, addrs);
1235
1236     len = strlen(secbuf);
1237     if (len > MAXSECRETLEN) {
1238         syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1239         len = MAXSECRETLEN;
1240     }
1241     BCOPY(secbuf, secret, len);
1242     BZERO(secbuf, sizeof(secbuf));
1243     *secret_len = len;
1244
1245     return 1;
1246 }
1247
1248 /*
1249  * set_allowed_addrs() - set the list of allowed addresses.
1250  */
1251 static void
1252 set_allowed_addrs(unit, addrs)
1253     int unit;
1254     struct wordlist *addrs;
1255 {
1256     if (addresses[unit] != NULL)
1257         free_wordlist(addresses[unit]);
1258     addresses[unit] = addrs;
1259
1260     /*
1261      * If there's only one authorized address we might as well
1262      * ask our peer for that one right away
1263      */
1264     if (addrs != NULL && addrs->next == NULL) {
1265         char *p = addrs->word;
1266         struct ipcp_options *wo = &ipcp_wantoptions[unit];
1267         u_int32_t a;
1268         struct hostent *hp;
1269
1270         if (*p != '!' && *p != '-' && *p != '*' && strchr(p, '/') == NULL) {
1271             hp = gethostbyname(p);
1272             if (hp != NULL && hp->h_addrtype == AF_INET)
1273                 a = *(u_int32_t *)hp->h_addr;
1274             else
1275                 a = inet_addr(p);
1276             if (a != (u_int32_t) -1)
1277                 wo->hisaddr = a;
1278         }
1279     }
1280 }
1281
1282 /*
1283  * auth_ip_addr - check whether the peer is authorized to use
1284  * a given IP address.  Returns 1 if authorized, 0 otherwise.
1285  */
1286 int
1287 auth_ip_addr(unit, addr)
1288     int unit;
1289     u_int32_t addr;
1290 {
1291     return ip_addr_check(addr, addresses[unit]);
1292 }
1293
1294 static int
1295 ip_addr_check(addr, addrs)
1296     u_int32_t addr;
1297     struct wordlist *addrs;
1298 {
1299     u_int32_t a, mask, ah;
1300     int accept;
1301     char *ptr_word, *ptr_mask;
1302     struct hostent *hp;
1303     struct netent *np;
1304
1305     /* don't allow loopback or multicast address */
1306     if (bad_ip_adrs(addr))
1307         return 0;
1308
1309     if (addrs == NULL) {
1310         if (auth_required)
1311             return 0;           /* no addresses authorized */
1312         return allow_any_ip || !have_route_to(addr);
1313     }
1314
1315     for (; addrs != NULL; addrs = addrs->next) {
1316         /* "-" means no addresses authorized, "*" means any address allowed */
1317         ptr_word = addrs->word;
1318         if (strcmp(ptr_word, "-") == 0)
1319             break;
1320         if (strcmp(ptr_word, "*") == 0)
1321             return 1;
1322
1323         accept = 1;
1324         if (*ptr_word == '!') {
1325             accept = 0;
1326             ++ptr_word;
1327         }
1328
1329         mask = ~ (u_int32_t) 0;
1330         ptr_mask = strchr (ptr_word, '/');
1331         if (ptr_mask != NULL) {
1332             int bit_count;
1333
1334             bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1335             if (bit_count <= 0 || bit_count > 32) {
1336                 syslog (LOG_WARNING,
1337                         "invalid address length %s in auth. address list",
1338                         ptr_mask);
1339                 continue;
1340             }
1341             *ptr_mask = '\0';
1342             mask <<= 32 - bit_count;
1343         }
1344
1345         hp = gethostbyname(ptr_word);
1346         if (hp != NULL && hp->h_addrtype == AF_INET) {
1347             a = *(u_int32_t *)hp->h_addr;
1348         } else {
1349             np = getnetbyname (ptr_word);
1350             if (np != NULL && np->n_addrtype == AF_INET) {
1351                 a = htonl (*(u_int32_t *)np->n_net);
1352                 if (ptr_mask == NULL) {
1353                     /* calculate appropriate mask for net */
1354                     ah = ntohl(a);
1355                     if (IN_CLASSA(ah))
1356                         mask = IN_CLASSA_NET;
1357                     else if (IN_CLASSB(ah))
1358                         mask = IN_CLASSB_NET;
1359                     else if (IN_CLASSC(ah))
1360                         mask = IN_CLASSC_NET;
1361                 }
1362             } else {
1363                 a = inet_addr (ptr_word);
1364             }
1365         }
1366
1367         if (ptr_mask != NULL)
1368             *ptr_mask = '/';
1369
1370         if (a == (u_int32_t)-1L)
1371             syslog (LOG_WARNING,
1372                     "unknown host %s in auth. address list",
1373                     addrs->word);
1374         else
1375             /* Here a and addr are in network byte order,
1376                and mask is in host order. */
1377             if (((addr ^ a) & htonl(mask)) == 0)
1378                 return accept;
1379     }
1380     return 0;                   /* not in list => can't have it */
1381 }
1382
1383 /*
1384  * bad_ip_adrs - return 1 if the IP address is one we don't want
1385  * to use, such as an address in the loopback net or a multicast address.
1386  * addr is in network byte order.
1387  */
1388 int
1389 bad_ip_adrs(addr)
1390     u_int32_t addr;
1391 {
1392     addr = ntohl(addr);
1393     return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1394         || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1395 }
1396
1397 /*
1398  * check_access - complain if a secret file has too-liberal permissions.
1399  */
1400 void
1401 check_access(f, filename)
1402     FILE *f;
1403     char *filename;
1404 {
1405     struct stat sbuf;
1406
1407     if (fstat(fileno(f), &sbuf) < 0) {
1408         syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1409     } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1410         syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1411     }
1412 }
1413
1414
1415 /*
1416  * scan_authfile - Scan an authorization file for a secret suitable
1417  * for authenticating `client' on `server'.  The return value is -1
1418  * if no secret is found, otherwise >= 0.  The return value has
1419  * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1420  * NONWILD_SERVER set if the secret didn't have "*" for the server.
1421  * Any following words on the line (i.e. address authorization
1422  * info) are placed in a wordlist and returned in *addrs.  
1423  */
1424 static int
1425 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1426     FILE *f;
1427     char *client;
1428     char *server;
1429     u_int32_t ipaddr;
1430     char *secret;
1431     struct wordlist **addrs;
1432     char *filename;
1433 {
1434     int newline, xxx;
1435     int got_flag, best_flag;
1436     FILE *sf;
1437     struct wordlist *ap, *addr_list, *alist, *alast;
1438     char word[MAXWORDLEN];
1439     char atfile[MAXWORDLEN];
1440     char lsecret[MAXWORDLEN];
1441
1442     if (addrs != NULL)
1443         *addrs = NULL;
1444     addr_list = NULL;
1445     if (!getword(f, word, &newline, filename))
1446         return -1;              /* file is empty??? */
1447     newline = 1;
1448     best_flag = -1;
1449     for (;;) {
1450         /*
1451          * Skip until we find a word at the start of a line.
1452          */
1453         while (!newline && getword(f, word, &newline, filename))
1454             ;
1455         if (!newline)
1456             break;              /* got to end of file */
1457
1458         /*
1459          * Got a client - check if it's a match or a wildcard.
1460          */
1461         got_flag = 0;
1462         if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1463             newline = 0;
1464             continue;
1465         }
1466         if (!ISWILD(word))
1467             got_flag = NONWILD_CLIENT;
1468
1469         /*
1470          * Now get a server and check if it matches.
1471          */
1472         if (!getword(f, word, &newline, filename))
1473             break;
1474         if (newline)
1475             continue;
1476         if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1477             continue;
1478         if (!ISWILD(word))
1479             got_flag |= NONWILD_SERVER;
1480
1481         /*
1482          * Got some sort of a match - see if it's better than what
1483          * we have already.
1484          */
1485         if (got_flag <= best_flag)
1486             continue;
1487
1488         /*
1489          * Get the secret.
1490          */
1491         if (!getword(f, word, &newline, filename))
1492             break;
1493         if (newline)
1494             continue;
1495
1496         /*
1497          * Special syntax: @filename means read secret from file.
1498          */
1499         if (word[0] == '@') {
1500             strcpy(atfile, word+1);
1501             if ((sf = fopen(atfile, "r")) == NULL) {
1502                 syslog(LOG_WARNING, "can't open indirect secret file %s",
1503                        atfile);
1504                 continue;
1505             }
1506             check_access(sf, atfile);
1507             if (!getword(sf, word, &xxx, atfile)) {
1508                 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1509                        atfile);
1510                 fclose(sf);
1511                 continue;
1512             }
1513             fclose(sf);
1514         }
1515         if (secret != NULL)
1516             strcpy(lsecret, word);
1517
1518         /*
1519          * Now read address authorization info and make a wordlist.
1520          */
1521         alist = alast = NULL;
1522         for (;;) {
1523             if (!getword(f, word, &newline, filename) || newline)
1524                 break;
1525             ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1526                                             + strlen(word));
1527             if (ap == NULL)
1528                 novm("authorized addresses");
1529             ap->next = NULL;
1530             strcpy(ap->word, word);
1531             if (alist == NULL)
1532                 alist = ap;
1533             else
1534                 alast->next = ap;
1535             alast = ap;
1536         }
1537
1538         /*
1539          * Check if the given IP address is allowed by the wordlist.
1540          */
1541         if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1542             free_wordlist(alist);
1543             continue;
1544         }
1545
1546         /*
1547          * This is the best so far; remember it.
1548          */
1549         best_flag = got_flag;
1550         if (addr_list)
1551             free_wordlist(addr_list);
1552         addr_list = alist;
1553         if (secret != NULL)
1554             strcpy(secret, lsecret);
1555
1556         if (!newline)
1557             break;
1558     }
1559
1560     if (addrs != NULL)
1561         *addrs = addr_list;
1562     else if (addr_list != NULL)
1563         free_wordlist(addr_list);
1564
1565     return best_flag;
1566 }
1567
1568 /*
1569  * free_wordlist - release memory allocated for a wordlist.
1570  */
1571 static void
1572 free_wordlist(wp)
1573     struct wordlist *wp;
1574 {
1575     struct wordlist *next;
1576
1577     while (wp != NULL) {
1578         next = wp->next;
1579         free(wp);
1580         wp = next;
1581     }
1582 }
1583
1584 /*
1585  * auth_script - execute a script with arguments
1586  * interface-name peer-name real-user tty speed
1587  */
1588 static void
1589 auth_script(script)
1590     char *script;
1591 {
1592     char strspeed[32];
1593     struct passwd *pw;
1594     char struid[32];
1595     char *user_name;
1596     char *argv[8];
1597
1598     if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1599         user_name = pw->pw_name;
1600     else {
1601         sprintf(struid, "%d", getuid());
1602         user_name = struid;
1603     }
1604     sprintf(strspeed, "%d", baud_rate);
1605
1606     argv[0] = script;
1607     argv[1] = ifname;
1608     argv[2] = peer_authname;
1609     argv[3] = user_name;
1610     argv[4] = devnam;
1611     argv[5] = strspeed;
1612     argv[6] = NULL;
1613
1614     authup_pid = run_program(script, argv, 0, NULL, NULL);
1615 }