source: rtems/cpukit/pppd/auth.c @ 6ac6a5c8

5
Last change on this file since 6ac6a5c8 was 33a1a4db, checked in by Peng Fan <van.freenix@…>, on 04/05/16 at 12:45:55

cpukit: pppd: fix compile warning

rcsid is defined, but not used. So discard it.

Signed-off-by: Peng Fan <van.freenix@…>

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