source: rtems/c/src/libnetworking/pppd/auth.c @ cb1e8a46

4.104.114.84.95
Last change on this file since cb1e8a46 was cb1e8a46, checked in by Joel Sherrill <joel.sherrill@…>, on 02/27/02 at 22:43:31

2002-02-27 Ilya Alexeev <ilya@…>

  • net/if_ppp.c, pppd/auth.c, pppd/chat.c, pppd/pppd.h, pppd/sys-rtems.c: Add server with pap-authorization capabilities as well as eliminate some warnings.
  • Property mode set to 100644
File size: 27.2 KB
Line 
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#define RCSID   "$Id$"
36
37#include <stdio.h>
38#include <stddef.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <pwd.h>
42#include <grp.h>
43#include <string.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <sys/socket.h>
47#include <fcntl.h>
48#if defined(_PATH_LASTLOG) && defined(_linux_)
49#include <lastlog.h>
50#endif
51
52#include <netdb.h>
53#include <netinet/in.h>
54#include <arpa/inet.h>
55
56#ifdef USE_PAM
57#include <security/pam_appl.h>
58#endif
59
60#ifdef HAS_SHADOW
61#include <shadow.h>
62#ifndef PW_PPP
63#define PW_PPP PW_LOGIN
64#endif
65#endif
66
67#include "pppd.h"
68#include "fsm.h"
69#include "lcp.h"
70#include "ipcp.h"
71#include "upap.h"
72#include "chap.h"
73#ifdef CBCP_SUPPORT
74#include "cbcp.h"
75#endif
76#include "pathnames.h"
77
78static const char rcsid[] = RCSID;
79
80/* The name by which the peer authenticated itself to us. */
81char peer_authname[MAXNAMELEN];
82
83/* Records which authentication operations haven't completed yet. */
84static int auth_pending[NUM_PPP];
85
86/* List of addresses which the peer may use. */
87static struct permitted_ip *addresses[NUM_PPP];
88
89/* Wordlist giving addresses which the peer may use
90   without authenticating itself. */
91static struct wordlist *noauth_addrs;
92
93/* Extra options to apply, from the secrets file entry for the peer. */
94static struct wordlist *extra_options;
95
96/* Number of network protocols which we have opened. */
97static int num_np_open;
98
99/* Number of network protocols which have come up. */
100static int num_np_up;
101
102/* Set if we got the contents of passwd[] from the pap-secrets file. */
103static int passwd_from_file;
104
105/* Set if we require authentication only because we have a default route. */
106static bool default_auth;
107
108/* Hook for a link status */
109void (*auth_linkup_hook)__P((void)) = NULL;
110void (*auth_linkdown_hook)__P((void)) = NULL;
111
112/* Hook to enable a plugin to control the idle time limit */
113int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
114
115/* Hook for a plugin to say whether we can possibly authenticate any peer */
116int (*pap_check_hook) __P((void)) = NULL;
117
118/* Hook for a plugin to check the PAP user and password */
119int (*pap_auth_hook) __P((char *user, char *passwd/*, char **msgp,
120                          struct wordlist **paddrs,
121                          struct wordlist **popts*/)) = NULL;
122
123/* Hook for a plugin to know about the PAP user logout */
124void (*pap_logout_hook) __P((void)) = NULL;
125
126/* Hook for a plugin to get the PAP password for authenticating us */
127int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
128
129/*
130 * This is used to ensure that we don't start an auth-up/down
131 * script while one is already running.
132 */
133enum script_state {
134    s_down,
135    s_up
136};
137
138static enum script_state auth_state = s_down;
139static enum script_state auth_script_state = s_down;
140
141/*
142 * Option variables.
143 */
144bool uselogin = 0;              /* Use /etc/passwd for checking PAP */
145bool cryptpap = 0;              /* Passwords in pap-secrets are encrypted */
146bool refuse_pap = 0;            /* Don't wanna auth. ourselves with PAP */
147bool refuse_chap = 0;           /* Don't wanna auth. ourselves with CHAP */
148bool usehostname = 0;           /* Use hostname for our_name */
149bool auth_required = 0;         /* Always require authentication from peer */
150bool allow_any_ip = 0;          /* Allow peer to use any IP address */
151bool explicit_remote = 0;       /* User specified explicit remote name */
152char remote_name[MAXNAMELEN];   /* Peer's name for authentication */
153
154/* Bits in auth_pending[] */
155#define PAP_WITHPEER    1
156#define PAP_PEER        2
157#define CHAP_WITHPEER   4
158#define CHAP_PEER       8
159
160extern char *crypt __P((const char *, const char *));
161
162/* Prototypes for procedures local to this file. */
163
164static void network_phase __P((int));
165static void check_idle __P((void *));
166static void connect_time_expired __P((void *));
167static int  null_login __P((int));
168static int  get_pap_passwd __P((char *));
169static int  have_pap_secret __P((int *));
170static int  have_chap_secret __P((char *, char *, int, int *));
171static int  ip_addr_check __P((u_int32_t, struct permitted_ip *));
172static void free_wordlist __P((struct wordlist *));
173static void auth_script __P((enum script_state s));
174static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
175
176
177/*
178 * Authentication-related options.
179 */
180option_t auth_options[] = {
181    { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
182      "Require PAP authentication from peer", 1, &auth_required },
183    { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
184      "Require PAP authentication from peer", 1, &auth_required },
185    { "refuse-pap", o_bool, &refuse_pap,
186      "Don't agree to auth to peer with PAP", 1 },
187    { "-pap", o_bool, &refuse_pap,
188      "Don't allow PAP authentication with peer", 1 },
189    { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
190      "Require CHAP authentication from peer", 1, &auth_required },
191    { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
192      "Require CHAP authentication from peer", 1, &auth_required },
193    { "refuse-chap", o_bool, &refuse_chap,
194      "Don't agree to auth to peer with CHAP", 1 },
195    { "-chap", o_bool, &refuse_chap,
196      "Don't allow CHAP authentication with peer", 1 },
197    { "name", o_string, our_name,
198      "Set local name for authentication",
199      OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
200    { "user", o_string, user,
201      "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
202    { "usehostname", o_bool, &usehostname,
203      "Must use hostname for authentication", 1 },
204    { "remotename", o_string, remote_name,
205      "Set remote name for authentication", OPT_STATIC,
206      &explicit_remote, MAXNAMELEN },
207    { "auth", o_bool, &auth_required,
208      "Require authentication from peer", 1 },
209    { "noauth", o_bool, &auth_required,
210      "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
211    {  "login", o_bool, &uselogin,
212      "Use system password database for PAP", 1 },
213    { "papcrypt", o_bool, &cryptpap,
214      "PAP passwords are encrypted", 1 },
215/* Removed for RTEMS PORT
216    { "+ua", o_special, setupapfile,
217      "Get PAP user and password from file" },
218*/
219    { "password", o_string, passwd,
220      "Password for authenticating us to the peer", OPT_STATIC,
221      NULL, MAXSECRETLEN },
222/* Removed for RTEMS_PORT
223    { "privgroup", o_special, privgroup,
224      "Allow group members to use privileged options", OPT_PRIV },
225    { "allow-ip", o_special, set_noauth_addr,
226      "Set IP address(es) which can be used without authentication",
227      OPT_PRIV },
228*/
229    { NULL }
230};
231
232/*
233 * An Open on LCP has requested a change from Dead to Establish phase.
234 * Do what's necessary to bring the physical layer up.
235 */
236void
237link_required(unit)
238    int unit;
239{
240}
241
242/*
243 * LCP has terminated the link; go to the Dead phase and take the
244 * physical layer down.
245 */
246void
247link_terminated(unit)
248    int unit;
249{
250    if (phase == PHASE_DEAD)
251        return;
252    if (pap_logout_hook) {
253        pap_logout_hook();
254    }
255    new_phase(PHASE_DEAD);
256    notice("Connection terminated.");
257}
258
259/*
260 * LCP has gone down; it will either die or try to re-establish.
261 */
262void
263link_down(unit)
264    int unit;
265{
266    int i;
267    struct protent *protp;
268
269    auth_state = s_down;
270    if (auth_script_state == s_up) {
271        update_link_stats(unit);
272        auth_script(s_down);
273    }
274    for (i = 0; (protp = protocols[i]) != NULL; ++i) {
275        if (!protp->enabled_flag)
276            continue;
277        if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
278            (*protp->lowerdown)(unit);
279        if (protp->protocol < 0xC000 && protp->close != NULL)
280            (*protp->close)(unit, "LCP down");
281    }
282    num_np_open = 0;
283    num_np_up = 0;
284    if (phase != PHASE_DEAD)
285        new_phase(PHASE_TERMINATE);
286}
287
288/*
289 * The link is established.
290 * Proceed to the Dead, Authenticate or Network phase as appropriate.
291 */
292void
293link_established(unit)
294    int unit;
295{
296    int auth;
297    lcp_options *wo = &lcp_wantoptions[unit];
298    lcp_options *go = &lcp_gotoptions[unit];
299    lcp_options *ho = &lcp_hisoptions[unit];
300    int i;
301    struct protent *protp;
302
303    /*
304     * Tell higher-level protocols that LCP is up.
305     */
306    for (i = 0; (protp = protocols[i]) != NULL; ++i)
307        if (protp->protocol != PPP_LCP && protp->enabled_flag
308            && protp->lowerup != NULL)
309            (*protp->lowerup)(unit);
310
311    if (auth_required && !(go->neg_chap || go->neg_upap)) {
312        /*
313         * We wanted the peer to authenticate itself, and it refused:
314         * if we have some address(es) it can use without auth, fine,
315         * otherwise treat it as though it authenticated with PAP using
316         * a username * of "" and a password of "".  If that's not OK,
317         * boot it out.
318         */
319        if (noauth_addrs != NULL) {
320            set_allowed_addrs(unit, noauth_addrs, NULL);
321        } else if (!wo->neg_upap || !null_login(unit)) {
322            warn("peer refused to authenticate: terminating link");
323            lcp_close(unit, "peer refused to authenticate");
324            status = EXIT_PEER_AUTH_FAILED;
325            return;
326        }
327    }
328
329    new_phase(PHASE_AUTHENTICATE);
330    auth = 0;
331    if (go->neg_chap) {
332        ChapAuthPeer(unit, our_name, go->chap_mdtype);
333        auth |= CHAP_PEER;
334    } else if (go->neg_upap) {
335        upap_authpeer(unit);
336        auth |= PAP_PEER;
337    }
338    if (ho->neg_chap) {
339        ChapAuthWithPeer(unit, user, ho->chap_mdtype);
340        auth |= CHAP_WITHPEER;
341    } else if (ho->neg_upap) {
342        if (passwd[0] == 0) {
343            passwd_from_file = 1;
344            if (!get_pap_passwd(passwd))
345                error("No secret found for PAP login");
346        }
347        upap_authwithpeer(unit, user, passwd);
348        auth |= PAP_WITHPEER;
349    }
350    auth_pending[unit] = auth;
351
352    if (!auth)
353        network_phase(unit);
354}
355
356/*
357 * Proceed to the network phase.
358 */
359static void
360network_phase(unit)
361    int unit;
362{
363#ifdef CBCP_SUPPORT
364    lcp_options *go = &lcp_gotoptions[unit];
365#endif
366
367    /* always run the auth-up script */
368    auth_state = s_up;
369    if (auth_script_state == s_down) {
370        auth_script(s_up);
371    }
372
373#ifdef CBCP_SUPPORT
374    /*
375     * If we negotiated callback, do it now.
376     */
377    if (go->neg_cbcp) {
378        new_phase(PHASE_CALLBACK);
379        (*cbcp_protent.open)(unit);
380        return;
381    }
382#endif
383
384    /*
385     * Process extra options from the secrets file
386     */
387    if (extra_options) {
388        options_from_list(extra_options, 1);
389        free_wordlist(extra_options);
390        extra_options = 0;
391    }
392    start_networks();
393}
394
395void
396start_networks()
397{
398    int i;
399    struct protent *protp;
400
401    new_phase(PHASE_NETWORK);
402    for (i = 0; (protp = protocols[i]) != NULL; ++i)
403        if (protp->protocol < 0xC000 && protp->enabled_flag
404            && protp->open != NULL) {
405            (*protp->open)(0);
406            if (protp->protocol != PPP_CCP)
407                ++num_np_open;
408        }
409
410    if (num_np_open == 0)
411        /* nothing to do */
412        lcp_close(0, "No network protocols running");
413}
414
415/*
416 * The peer has failed to authenticate himself using `protocol'.
417 */
418void
419auth_peer_fail(unit, protocol)
420    int unit, protocol;
421{
422    /*
423     * Authentication failure: take the link down
424     */
425    lcp_close(unit, "Authentication failed");
426    status = EXIT_PEER_AUTH_FAILED;
427}
428
429/*
430 * The peer has been successfully authenticated using `protocol'.
431 */
432void
433auth_peer_success(unit, protocol, name, namelen)
434    int unit, protocol;
435    char *name;
436    int namelen;
437{
438    int bit;
439
440    switch (protocol) {
441    case PPP_CHAP:
442        bit = CHAP_PEER;
443        break;
444    case PPP_PAP:
445        bit = PAP_PEER;
446        break;
447    default:
448        warn("auth_peer_success: unknown protocol %x", protocol);
449        return;
450    }
451
452    /*
453     * Save the authenticated name of the peer for later.
454     */
455    if (namelen > sizeof(peer_authname) - 1)
456        namelen = sizeof(peer_authname) - 1;
457    BCOPY(name, peer_authname, namelen);
458    peer_authname[namelen] = 0;
459
460    /*
461     * If there is no more authentication still to be done,
462     * proceed to the network (or callback) phase.
463     */
464    if ((auth_pending[unit] &= ~bit) == 0)
465        network_phase(unit);
466}
467
468/*
469 * We have failed to authenticate ourselves to the peer using `protocol'.
470 */
471void
472auth_withpeer_fail(unit, protocol)
473    int unit, protocol;
474{
475    if (passwd_from_file)
476        BZERO(passwd, MAXSECRETLEN);
477    /*
478     * We've failed to authenticate ourselves to our peer.
479     * Some servers keep sending CHAP challenges, but there
480     * is no point in persisting without any way to get updated
481     * authentication secrets.
482     */
483    lcp_close(unit, "Failed to authenticate ourselves to peer");
484    status = EXIT_AUTH_TOPEER_FAILED;
485}
486
487/*
488 * We have successfully authenticated ourselves with the peer using `protocol'.
489 */
490void
491auth_withpeer_success(unit, protocol)
492    int unit, protocol;
493{
494    int bit;
495
496    switch (protocol) {
497    case PPP_CHAP:
498        bit = CHAP_WITHPEER;
499        break;
500    case PPP_PAP:
501        if (passwd_from_file)
502            BZERO(passwd, MAXSECRETLEN);
503        bit = PAP_WITHPEER;
504        break;
505    default:
506        warn("auth_withpeer_success: unknown protocol %x", protocol);
507        bit = 0;
508    }
509
510    /*
511     * If there is no more authentication still being done,
512     * proceed to the network (or callback) phase.
513     */
514    if ((auth_pending[unit] &= ~bit) == 0)
515        network_phase(unit);
516}
517
518
519/*
520 * np_up - a network protocol has come up.
521 */
522void
523np_up(unit, proto)
524    int unit, proto;
525{
526    int tlim;
527
528    if (num_np_up == 0) {
529        /*
530         * At this point we consider that the link has come up successfully.
531         */
532        status = EXIT_OK;
533        unsuccess = 0;
534        new_phase(PHASE_RUNNING);
535
536        if (idle_time_hook != 0)
537            tlim = (*idle_time_hook)(NULL);
538        else
539            tlim = idle_time_limit;
540        if (tlim > 0)
541            TIMEOUT(check_idle, NULL, tlim);
542
543        /*
544         * Set a timeout to close the connection once the maximum
545         * connect time has expired.
546         */
547        if (maxconnect > 0)
548            TIMEOUT(connect_time_expired, 0, maxconnect);
549    }
550    ++num_np_up;
551}
552
553/*
554 * np_down - a network protocol has gone down.
555 */
556void
557np_down(unit, proto)
558    int unit, proto;
559{
560    if (--num_np_up == 0) {
561        UNTIMEOUT(check_idle, NULL);
562        new_phase(PHASE_NETWORK);
563    }
564}
565
566/*
567 * np_finished - a network protocol has finished using the link.
568 */
569void
570np_finished(unit, proto)
571    int unit, proto;
572{
573    if (--num_np_open <= 0) {
574        /* no further use for the link: shut up shop. */
575        lcp_close(0, "No network protocols running");
576    }
577}
578
579/*
580 * check_idle - check whether the link has been idle for long
581 * enough that we can shut it down.
582 */
583static void
584check_idle(arg)
585    void *arg;
586{
587    struct ppp_idle idle;
588    time_t itime;
589    int tlim;
590
591    if (!get_idle_time(0, &idle))
592        return;
593    if (idle_time_hook != 0) {
594        tlim = idle_time_hook(&idle);
595    } else {
596        itime = MIN(idle.xmit_idle, idle.recv_idle);
597        tlim = idle_time_limit - itime;
598    }
599    if (tlim <= 0) {
600        /* link is idle: shut it down. */
601        notice("Terminating connection due to lack of activity.");
602        lcp_close(0, "Link inactive");
603        need_holdoff = 0;
604        status = EXIT_IDLE_TIMEOUT;
605    } else {
606        TIMEOUT(check_idle, NULL, tlim);
607    }
608}
609
610/*
611 * connect_time_expired - log a message and close the connection.
612 */
613static void
614connect_time_expired(arg)
615    void *arg;
616{
617    info("Connect time expired");
618    lcp_close(0, "Connect time expired");       /* Close connection */
619    status = EXIT_CONNECT_TIME;
620}
621
622/*
623 * auth_check_options - called to check authentication options.
624 */
625int
626auth_check_options()
627{
628    lcp_options *wo = &lcp_wantoptions[0];
629    int status      = 1;
630    int can_auth;
631    int lacks_ip;
632
633    /* Default our_name to hostname, and user to our_name */
634    if (our_name[0] == 0 || usehostname)
635        strlcpy(our_name, hostname, sizeof(our_name));
636    if (user[0] == 0)
637        strlcpy(user, our_name, sizeof(user));
638
639    /*
640     * If we have a default route, require the peer to authenticate
641     * unless the noauth option was given or the real user is root.
642     */
643    if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
644        printf("auth_check_options: turning on\n");
645        auth_required = 1;
646        default_auth = 1;
647    }
648
649    /* If authentication is required, ask peer for CHAP or PAP. */
650    if (auth_required) {
651        if (!wo->neg_chap && !wo->neg_upap) {
652            wo->neg_chap = 1;
653            wo->neg_upap = 1;
654        }
655    } else {
656        wo->neg_chap = 0;
657        wo->neg_upap = 0;
658    }
659
660    /*
661     * Check whether we have appropriate secrets to use
662     * to authenticate the peer.
663     */
664    lacks_ip = 0;
665    can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
666    if (!can_auth && wo->neg_chap) {
667        can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
668                                    our_name, 1, &lacks_ip);
669    }
670
671    if (auth_required && !can_auth && noauth_addrs == NULL) {
672        if (default_auth) {
673            option_error(
674"By default the remote system is required to authenticate itself");
675            option_error(
676"(because this system has a default route to the internet)");
677        } else if (explicit_remote)
678            option_error(
679"The remote system (%s) is required to authenticate itself",
680                         remote_name);
681        else
682            option_error(
683"The remote system is required to authenticate itself");
684        option_error(
685"but I couldn't find any suitable secret (password) for it to use to do so.");
686        if (lacks_ip)
687            option_error(
688"(None of the available passwords would let it use an IP address.)");
689
690        status = 0;
691    }
692    return ( status );
693}
694
695/*
696 * auth_reset - called when LCP is starting negotiations to recheck
697 * authentication options, i.e. whether we have appropriate secrets
698 * to use for authenticating ourselves and/or the peer.
699 */
700void
701auth_reset(unit)
702    int unit;
703{
704    lcp_options *go = &lcp_gotoptions[unit];
705    lcp_options *ao = &lcp_allowoptions[0];
706
707    ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
708    ao->neg_chap = !refuse_chap
709        && (passwd[0] != 0
710            || have_chap_secret(user, (explicit_remote? remote_name: NULL),
711                                0, NULL));
712
713    if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
714        go->neg_upap = 0;
715    if (go->neg_chap) {
716        if (!have_chap_secret((explicit_remote? remote_name: NULL),
717                              our_name, 1, NULL))
718            go->neg_chap = 0;
719    }
720}
721
722
723/*
724 * check_passwd - Check the user name and passwd against the PAP secrets
725 * file.  If requested, also check against the system password database,
726 * and login the user if OK.
727 *
728 * returns:
729 *      UPAP_AUTHNAK: Authentication failed.
730 *      UPAP_AUTHACK: Authentication succeeded.
731 * In either case, msg points to an appropriate message.
732 */
733int
734check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
735    int unit;
736    char *auser;
737    int userlen;
738    char *apasswd;
739    int passwdlen;
740    char **msg;
741{
742    char passwd[64], user[64];
743
744    if (pap_auth_hook)
745    {
746        slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
747        slprintf(user, sizeof(user), "%.*v", userlen, auser);
748
749        return (*pap_auth_hook)(user, passwd/*, NULL, NULL, NULL*/) ?
750            UPAP_AUTHACK : UPAP_AUTHNAK;
751    }
752
753    return UPAP_AUTHACK;
754
755#if 0
756    int    ret = (int)UPAP_AUTHNAK;
757
758    if (( userlen == 0 ) && ( passwdlen == 0 )) {
759      ret = (int)UPAP_AUTHACK;
760    }
761    printf("check_passwd: %d\n", ret);
762
763    return ret;
764#endif
765}
766
767/*
768 * null_login - Check if a username of "" and a password of "" are
769 * acceptable, and iff so, set the list of acceptable IP addresses
770 * and return 1.
771 */
772static int
773null_login(unit)
774    int unit;
775{
776    return 0;
777}
778
779
780/*
781 * get_pap_passwd - get a password for authenticating ourselves with
782 * our peer using PAP.  Returns 1 on success, 0 if no suitable password
783 * could be found.
784 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
785 */
786static int
787get_pap_passwd(passwd)
788    char *passwd;
789{
790    int ret = (int)0;
791
792    /*
793     * Check whether a plugin wants to supply this.
794     */
795    if (pap_passwd_hook) {
796        ret = (*pap_passwd_hook)(user, passwd);
797    }
798
799    return ( ret );
800}
801
802
803/*
804 * have_pap_secret - check whether we have a PAP file with any
805 * secrets that we could possibly use for authenticating the peer.
806 */
807static int
808have_pap_secret(lacks_ipp)
809    int *lacks_ipp;
810{
811    return 1;
812
813#if 0
814    int ret = (int)0;
815
816    /* let the plugin decide, if there is one */
817    printf("have_pap_secret:\n");
818    if (pap_check_hook) {
819        ret = (*pap_check_hook)();
820    }
821
822    return ( ret );
823#endif
824}
825
826
827/*
828 * have_chap_secret - check whether we have a CHAP file with a
829 * secret that we could possibly use for authenticating `client'
830 * on `server'.  Either can be the null string, meaning we don't
831 * know the identity yet.
832 */
833static int
834have_chap_secret(client, server, need_ip, lacks_ipp)
835    char *client;
836    char *server;
837    int need_ip;
838    int *lacks_ipp;
839{
840    return 0;
841}
842
843
844/*
845 * get_secret - open the CHAP secret file and return the secret
846 * for authenticating the given client on the given server.
847 * (We could be either client or server).
848 */
849int
850get_secret(unit, client, server, secret, secret_len, am_server)
851    int unit;
852    char *client;
853    char *server;
854    char *secret;
855    int *secret_len;
856    int am_server;
857{
858    int len;
859    char secbuf[MAXWORDLEN];
860
861    if (!am_server && passwd[0] != 0) {
862        strlcpy(secbuf, passwd, sizeof(secbuf));
863    } else {
864        return 0;
865    }
866
867    len = strlen(secbuf);
868    if (len > MAXSECRETLEN) {
869        error("Secret for %s on %s is too long", client, server);
870        len = MAXSECRETLEN;
871    }
872    BCOPY(secbuf, secret, len);
873    BZERO(secbuf, sizeof(secbuf));
874    *secret_len = len;
875
876    return 1;
877}
878
879/*
880 * set_allowed_addrs() - set the list of allowed addresses.
881 * Also looks for `--' indicating options to apply for this peer
882 * and leaves the following words in extra_options.
883 */
884static void
885set_allowed_addrs(unit, addrs, opts)
886    int unit;
887    struct wordlist *addrs;
888    struct wordlist *opts;
889{
890    int n;
891    struct wordlist *ap, **pap;
892    struct permitted_ip *ip;
893    char *ptr_word, *ptr_mask;
894    struct hostent *hp;
895    struct netent *np;
896    u_int32_t a, mask, ah, offset;
897    struct ipcp_options *wo = &ipcp_wantoptions[unit];
898    u_int32_t suggested_ip = 0;
899
900    if (addresses[unit] != NULL)
901        free(addresses[unit]);
902    addresses[unit] = NULL;
903    if (extra_options != NULL)
904        free_wordlist(extra_options);
905    extra_options = opts;
906
907    /*
908     * Count the number of IP addresses given.
909     */
910    for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next)
911        ++n;
912    if (n == 0)
913        return;
914    ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
915    if (ip == 0)
916        return;
917
918    n = 0;
919    for (ap = addrs; ap != NULL; ap = ap->next) {
920        /* "-" means no addresses authorized, "*" means any address allowed */
921        ptr_word = ap->word;
922        if (strcmp(ptr_word, "-") == 0)
923            break;
924        if (strcmp(ptr_word, "*") == 0) {
925            ip[n].permit = 1;
926            ip[n].base = ip[n].mask = 0;
927            ++n;
928            break;
929        }
930
931        ip[n].permit = 1;
932        if (*ptr_word == '!') {
933            ip[n].permit = 0;
934            ++ptr_word;
935        }
936
937        mask = ~ (u_int32_t) 0;
938        offset = 0;
939        ptr_mask = strchr (ptr_word, '/');
940        if (ptr_mask != NULL) {
941            int bit_count;
942            char *endp;
943
944            bit_count = (int) strtol (ptr_mask+1, &endp, 10);
945            if (bit_count <= 0 || bit_count > 32) {
946                warn("invalid address length %v in auth. address list",
947                     ptr_mask+1);
948                continue;
949            }
950            bit_count = 32 - bit_count; /* # bits in host part */
951            if (*endp == '+') {
952                offset = pppifunit + 1;
953                ++endp;
954            }
955            if (*endp != 0) {
956                warn("invalid address length syntax: %v", ptr_mask+1);
957                continue;
958            }
959            *ptr_mask = '\0';
960            mask <<= bit_count;
961        }
962
963        hp = gethostbyname(ptr_word);
964        if (hp != NULL && hp->h_addrtype == AF_INET) {
965            a = *(u_int32_t *)hp->h_addr;
966        } else {
967            np = getnetbyname (ptr_word);
968            if (np != NULL && np->n_addrtype == AF_INET) {
969                a = htonl (*(u_int32_t *)np->n_net);
970                if (ptr_mask == NULL) {
971                    /* calculate appropriate mask for net */
972                    ah = ntohl(a);
973                    if (IN_CLASSA(ah))
974                        mask = IN_CLASSA_NET;
975                    else if (IN_CLASSB(ah))
976                        mask = IN_CLASSB_NET;
977                    else if (IN_CLASSC(ah))
978                        mask = IN_CLASSC_NET;
979                }
980            } else {
981                a = inet_addr (ptr_word);
982            }
983        }
984
985        if (ptr_mask != NULL)
986            *ptr_mask = '/';
987
988        if (a == (u_int32_t)-1L) {
989            warn("unknown host %s in auth. address list", ap->word);
990            continue;
991        }
992        if (offset != 0) {
993            if (offset >= ~mask) {
994                warn("interface unit %d too large for subnet %v",
995                     pppifunit, ptr_word);
996                continue;
997            }
998            a = htonl((ntohl(a) & mask) + offset);
999            mask = ~(u_int32_t)0;
1000        }
1001        ip[n].mask = htonl(mask);
1002        ip[n].base = a & ip[n].mask;
1003        ++n;
1004        if (~mask == 0 && suggested_ip == 0)
1005            suggested_ip = a;
1006    }
1007
1008    ip[n].permit = 0;           /* make the last entry forbid all addresses */
1009    ip[n].base = 0;             /* to terminate the list */
1010    ip[n].mask = 0;
1011
1012    addresses[unit] = ip;
1013
1014    /*
1015     * If the address given for the peer isn't authorized, or if
1016     * the user hasn't given one, AND there is an authorized address
1017     * which is a single host, then use that if we find one.
1018     */
1019    if (suggested_ip != 0
1020        && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1021        wo->hisaddr = suggested_ip;
1022}
1023
1024/*
1025 * auth_ip_addr - check whether the peer is authorized to use
1026 * a given IP address.  Returns 1 if authorized, 0 otherwise.
1027 */
1028int
1029auth_ip_addr(unit, addr)
1030    int unit;
1031    u_int32_t addr;
1032{
1033#if 0
1034    int ok;
1035#endif
1036
1037    /* don't allow loopback or multicast address */
1038    if (bad_ip_adrs(addr))
1039        return 0;
1040       
1041    return 1;
1042
1043#if 0
1044    if (addresses[unit] != NULL) {
1045        ok = ip_addr_check(addr, addresses[unit]);
1046        if (ok >= 0)
1047            return ok;
1048    }
1049    if (auth_required)
1050        return 0;               /* no addresses authorized */
1051    return allow_any_ip || !have_route_to(addr);
1052#endif
1053}
1054
1055static int
1056ip_addr_check(addr, addrs)
1057    u_int32_t addr;
1058    struct permitted_ip *addrs;
1059{
1060    for (; ; ++addrs)
1061        if ((addr & addrs->mask) == addrs->base)
1062            return addrs->permit;
1063}
1064
1065/*
1066 * bad_ip_adrs - return 1 if the IP address is one we don't want
1067 * to use, such as an address in the loopback net or a multicast address.
1068 * addr is in network byte order.
1069 */
1070int
1071bad_ip_adrs(addr)
1072    u_int32_t addr;
1073{
1074    addr = ntohl(addr);
1075    return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1076        || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1077}
1078
1079/*
1080 * free_wordlist - release memory allocated for a wordlist.
1081 */
1082static void
1083free_wordlist(wp)
1084    struct wordlist *wp;
1085{
1086    struct wordlist *next;
1087
1088    while (wp != NULL) {
1089        next = wp->next;
1090        free(wp);
1091        wp = next;
1092    }
1093}
1094
1095/*
1096 * auth_script - execute a script with arguments
1097 * interface-name peer-name real-user tty speed
1098 */
1099static void
1100auth_script(s)
1101    enum script_state s;
1102{
1103    switch (s) {
1104    case s_up:
1105        auth_script_state = s_up;
1106        if ( auth_linkup_hook ) {
1107          (*auth_linkup_hook)();
1108        }
1109        break;
1110    case s_down:
1111        auth_script_state = s_down;
1112        if ( auth_linkdown_hook ) {
1113          (*auth_linkdown_hook)();
1114        }
1115        break;
1116    }
1117}
Note: See TracBrowser for help on using the repository browser.