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