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