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