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