]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/plugins/radius/radius.c
pppd.8: Document netmask option
[ppp.git] / pppd / plugins / radius / radius.c
index 48882a9e1c84be070d6a56e21e9c01b7704280e4..e99bc7511f65c2eb338c9a279211d2791751607b 100644 (file)
 *
 ***********************************************************************/
 static char const RCSID[] =
-"$Id: radius.c,v 1.19 2002/12/23 23:24:37 fcusack Exp $";
-
-#include "pppd.h"
-#include "chap.h"
-#ifdef CHAPMS
-#include "chap_ms.h"
-#ifdef MPPE
-#include "md5.h"
-#endif
-#endif
-#include "radiusclient.h"
-#include "fsm.h"
-#include "ipcp.h"
+"$Id: radius.c,v 1.32 2008/05/26 09:18:08 paulus Exp $";
+
 #include <syslog.h>
 #include <sys/types.h>
 #include <sys/time.h>
+#include <sys/param.h>
 #include <string.h>
+#include <netinet/in.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+
+#include <pppd/pppd.h>
+#include <pppd/options.h>
+#include <pppd/chap.h>
+#include <pppd/upap.h>
+#ifdef PPP_WITH_CHAPMS
+#include <pppd/chap_ms.h>
+#ifdef PPP_WITH_MPPE
+#include <pppd/mppe.h>
+#endif
+#endif
+#include <pppd/crypto.h>
+#include <pppd/fsm.h>
+#include <pppd/ipcp.h>
+
+#include "radiusclient.h"
 
 #define BUF_LEN 1024
 
+#define MSDNS 1
+
 static char *config_file = NULL;
 static int add_avp(char **);
 static struct avpopt {
     char *vpstr;
     struct avpopt *next;
 } *avpopt = NULL;
+static bool portnummap = 0;
 
 static option_t Options[] = {
     { "radius-config-file", o_string, &config_file },
     { "avpair", o_special, add_avp },
+    { "map-to-ttyname", o_bool, &portnummap,
+       "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
+    { "map-to-ifname", o_bool, &portnummap,
+       "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
     { NULL }
 };
 
-static int radius_secret_check(void);
-static int radius_pap_auth(char *user,
-                          char *passwd,
-                          char **msgp,
-                          struct wordlist **paddrs,
-                          struct wordlist **popts);
-static int radius_chap_auth(char *user,
-                           u_char *remmd,
-                           int remmd_len,
-                           chap_state *cstate);
+static pap_check_hook_fn radius_secret_check;
+static pap_auth_hook_fn radius_pap_auth;
+static chap_verify_hook_fn radius_chap_verify;
 
 static void radius_ip_up(void *opaque, int arg);
 static void radius_ip_down(void *opaque, int arg);
-static void make_username_realm(char *user);
-static int radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
-                           REQUEST_INFO *req_info);
+static void make_username_realm(const char *user);
+static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
+                           struct chap_digest_type *digest,
+                           unsigned char *challenge,
+                           char *message, int message_space);
 static void radius_choose_ip(u_int32_t *addrp);
 static int radius_init(char *msg);
-static int get_client_port(char *ifname);
+static int get_client_port(const char *ifname);
 static int radius_allowed_address(u_int32_t addr);
 static void radius_acct_interim(void *);
-#ifdef MPPE
+#ifdef PPP_WITH_MPPE
 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
-                             chap_state *);
+                             unsigned char *);
 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
 #endif
 
@@ -93,7 +105,6 @@ static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
 #endif
 
 struct radius_state {
-    int accounting_started;
     int initialized;
     int client_port;
     int choose_ip;
@@ -122,7 +133,7 @@ void (*radius_pre_auth_hook)(char const *user,
 
 static struct radius_state rstate;
 
-char pppd_version[] = VERSION;
+char pppd_version[] = PPPD_VERSION;
 
 /**********************************************************************
 * %FUNCTION: plugin_init
@@ -140,20 +151,20 @@ plugin_init(void)
     pap_auth_hook = radius_pap_auth;
 
     chap_check_hook = radius_secret_check;
-    chap_auth_hook = radius_chap_auth;
+    chap_verify_hook = radius_chap_verify;
 
     ip_choose_hook = radius_choose_ip;
     allowed_address_hook = radius_allowed_address;
 
-    add_notifier(&ip_up_notifier, radius_ip_up, NULL);
-    add_notifier(&ip_down_notifier, radius_ip_down, NULL);
+    ppp_add_notify(NF_IP_UP, radius_ip_up, NULL);
+    ppp_add_notify(NF_IP_DOWN, radius_ip_down, NULL);
 
     memset(&rstate, 0, sizeof(rstate));
 
     strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
            sizeof(rstate.config_file));
 
-    add_options(Options);
+    ppp_add_options(Options);
 
     info("RADIUS plugin initialized.");
 }
@@ -188,7 +199,7 @@ add_avp(char **argv)
 *  1 -- we are ALWAYS willing to supply a secret. :-)
 * %DESCRIPTION:
 * Tells pppd that we will try to authenticate the peer, and not to
-* worry about looking in /etc/ppp/*-secrets
+* worry about looking in *-secrets file(s)
 ***********************************************************************/
 static int
 radius_secret_check(void)
@@ -237,6 +248,8 @@ radius_pap_auth(char *user,
     UINT4 av_type;
     int result;
     static char radius_msg[BUF_LEN];
+    const char *remote_number;
+    const char *ipparam;
 
     radius_msg[0] = 0;
     *msgp = radius_msg;
@@ -259,7 +272,7 @@ radius_pap_auth(char *user,
 
     /* Hack... the "port" is the ppp interface number.  Should really be
        the tty */
-    rstate.client_port = get_client_port(ifname);
+    rstate.client_port = get_client_port(portnummap ? ppp_devnam() : ppp_ifname());
 
     av_type = PW_FRAMED;
     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
@@ -269,10 +282,13 @@ radius_pap_auth(char *user,
 
     rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
     rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
-    if (*remote_number) {
+    remote_number = ppp_get_remote_number();
+    ipparam = ppp_ipparam();
+    if (remote_number) {
        rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
                       VENDOR_NONE);
-    }
+    } else if (ipparam)
+       rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
 
     /* Add user specified vp's */
     if (rstate.avp)
@@ -287,7 +303,7 @@ radius_pap_auth(char *user,
     }
 
     if (result == OK_RC) {
-       if (radius_setparams(NULL, received, radius_msg, NULL) < 0) {
+       if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
            result = ERROR_RC;
        }
     }
@@ -300,57 +316,67 @@ radius_pap_auth(char *user,
 }
 
 /**********************************************************************
-* %FUNCTION: radius_chap_auth
+* %FUNCTION: radius_chap_verify
 * %ARGUMENTS:
-*  user -- user-name of peer
-*  remmd -- hash received from peer
-*  remmd_len -- length of remmd
-*  cstate -- pppd's chap_state structure
+*  user -- name of the peer
+*  ourname -- name for this machine
+*  id -- the ID byte in the challenge
+*  digest -- points to the structure representing the digest type
+*  challenge -- the challenge string we sent (length in first byte)
+*  response -- the response (hash) the peer sent back (length in 1st byte)
+*  message -- space for a message to be returned to the peer
+*  message_space -- number of bytes available at *message.
 * %RETURNS:
-*  CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
+*  1 if the response is good, 0 if it is bad
 * %DESCRIPTION:
 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
 ***********************************************************************/
 static int
-radius_chap_auth(char *user,
-                u_char *remmd,
-                int remmd_len,
-                chap_state *cstate)
+radius_chap_verify(char *user, char *ourname, int id,
+                  struct chap_digest_type *digest,
+                  unsigned char *challenge, unsigned char *response,
+                  char *message, int message_space)
 {
     VALUE_PAIR *send, *received;
     UINT4 av_type;
     static char radius_msg[BUF_LEN];
     int result;
-    u_char cpassword[MAX_RESPONSE_LENGTH + 1];
-#ifdef MPPE
+    int challenge_len, response_len;
+    u_char cpassword[MAX_RESPONSE_LEN + 1];
+#ifdef PPP_WITH_MPPE
     /* Need the RADIUS secret and Request Authenticator to decode MPPE */
     REQUEST_INFO request_info, *req_info = &request_info;
 #else
     REQUEST_INFO *req_info = NULL;
 #endif
+    const char *remote_number;
+    const char *ipparam;
+
+    challenge_len = *challenge++;
+    response_len = *response++;
 
     radius_msg[0] = 0;
 
     if (radius_init(radius_msg) < 0) {
        error("%s", radius_msg);
-       return CHAP_FAILURE;
+       return 0;
     }
 
     /* return error for types we can't handle */
-    if ((cstate->chal_type != CHAP_DIGEST_MD5)
-#ifdef CHAPMS
-       && (cstate->chal_type != CHAP_MICROSOFT)
-       && (cstate->chal_type != CHAP_MICROSOFT_V2)
+    if ((digest->code != CHAP_MD5)
+#ifdef PPP_WITH_CHAPMS
+       && (digest->code != CHAP_MICROSOFT)
+       && (digest->code != CHAP_MICROSOFT_V2)
 #endif
        ) {
-       error("RADIUS: Challenge type %u unsupported", cstate->chal_type);
-       return CHAP_FAILURE;
+       error("RADIUS: Challenge type %u unsupported", digest->code);
+       return 0;
     }
 
     /* Put user with potentially realm added in rstate.user */
     if (!rstate.done_chap_once) {
        make_username_realm(user);
-       rstate.client_port = get_client_port (ifname);
+       rstate.client_port = get_client_port (portnummap ? ppp_devnam() : ppp_ifname());
        if (radius_pre_auth_hook) {
            radius_pre_auth_hook(rstate.user,
                                 &rstate.authserver,
@@ -371,35 +397,35 @@ radius_chap_auth(char *user,
     /*
      * add the challenge and response fields
      */
-    switch (cstate->chal_type) {
-    case CHAP_DIGEST_MD5:
+    switch (digest->code) {
+    case CHAP_MD5:
        /* CHAP-Challenge and CHAP-Password */
-       cpassword[0] = cstate->chal_id;
-       memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
+       if (response_len != MD5_DIGEST_LENGTH)
+           return 0;
+       cpassword[0] = id;
+       memcpy(&cpassword[1], response, MD5_DIGEST_LENGTH);
 
        rc_avpair_add(&send, PW_CHAP_CHALLENGE,
-                     cstate->challenge, cstate->chal_len, VENDOR_NONE);
+                     challenge, challenge_len, VENDOR_NONE);
        rc_avpair_add(&send, PW_CHAP_PASSWORD,
-                     cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
+                     cpassword, MD5_DIGEST_LENGTH + 1, VENDOR_NONE);
        break;
 
-#ifdef CHAPMS
+#ifdef PPP_WITH_CHAPMS
     case CHAP_MICROSOFT:
     {
        /* MS-CHAP-Challenge and MS-CHAP-Response */
-       MS_ChapResponse *rmd = (MS_ChapResponse *) remmd;
        u_char *p = cpassword;
 
-       *p++ = cstate->chal_id;
+       if (response_len != MS_CHAP_RESPONSE_LEN)
+           return 0;
+       *p++ = id;
        /* The idiots use a different field order in RADIUS than PPP */
-       memcpy(p, rmd->UseNT, sizeof(rmd->UseNT));
-       p += sizeof(rmd->UseNT);
-       memcpy(p, rmd->LANManResp, sizeof(rmd->LANManResp));
-       p += sizeof(rmd->LANManResp);
-       memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
+       *p++ = response[MS_CHAP_USENT];
+       memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
 
        rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
-                     cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
+                     challenge, challenge_len, VENDOR_MICROSOFT);
        rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
                      cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
        break;
@@ -408,21 +434,18 @@ radius_chap_auth(char *user,
     case CHAP_MICROSOFT_V2:
     {
        /* MS-CHAP-Challenge and MS-CHAP2-Response */
-       MS_Chap2Response *rmd = (MS_Chap2Response *) remmd;
        u_char *p = cpassword;
 
-       *p++ = cstate->chal_id;
+       if (response_len != MS_CHAP2_RESPONSE_LEN)
+           return 0;
+       *p++ = id;
        /* The idiots use a different field order in RADIUS than PPP */
-       memcpy(p, rmd->Flags, sizeof(rmd->Flags));
-       p += sizeof(rmd->Flags);
-       memcpy(p, rmd->PeerChallenge, sizeof(rmd->PeerChallenge));
-       p += sizeof(rmd->PeerChallenge);
-       memcpy(p, rmd->Reserved, sizeof(rmd->Reserved));
-       p += sizeof(rmd->Reserved);
-       memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
+       *p++ = response[MS_CHAP2_FLAGS];
+       memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
+                            + MS_CHAP2_NTRESP_LEN));
 
        rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
-                     cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
+                     challenge, challenge_len, VENDOR_MICROSOFT);
        rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
                      cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
        break;
@@ -430,10 +453,13 @@ radius_chap_auth(char *user,
 #endif
     }
 
-    if (*remote_number) {
+    remote_number = ppp_get_remote_number();
+    ipparam = ppp_ipparam();
+    if (remote_number) {
        rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
                       VENDOR_NONE);
-    }
+    } else if (ipparam)
+       rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
 
     /* Add user specified vp's */
     if (rstate.avp)
@@ -452,9 +478,12 @@ radius_chap_auth(char *user,
                         req_info);
     }
 
+    strlcpy(message, radius_msg, message_space);
+
     if (result == OK_RC) {
        if (!rstate.done_chap_once) {
-           if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
+           if (radius_setparams(received, radius_msg, req_info, digest,
+                                challenge, message, message_space) < 0) {
                error("%s", radius_msg);
                result = ERROR_RC;
            } else {
@@ -465,7 +494,7 @@ radius_chap_auth(char *user,
 
     rc_avpair_free(received);
     rc_avpair_free (send);
-    return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
+    return (result == OK_RC);
 }
 
 /**********************************************************************
@@ -479,7 +508,7 @@ radius_chap_auth(char *user,
 * then the default realm from the radiusclient config file is added.
 ***********************************************************************/
 static void
-make_username_realm(char *user)
+make_username_realm(const char *user)
 {
     char *default_realm;
 
@@ -502,7 +531,6 @@ make_username_realm(char *user)
 /**********************************************************************
 * %FUNCTION: radius_setparams
 * %ARGUMENTS:
-*  cstate -- pppd's chap_state structure
 *  vp -- received value-pairs
 *  msg -- buffer in which to place error message.  Holds up to BUF_LEN chars
 * %RETURNS:
@@ -511,11 +539,25 @@ make_username_realm(char *user)
 *  Parses attributes sent by RADIUS server and sets them in pppd.
 ***********************************************************************/
 static int
-radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
-                REQUEST_INFO *req_info)
+radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
+                struct chap_digest_type *digest, unsigned char *challenge,
+                char *message, int message_space)
 {
     u_int32_t remote;
     int ms_chap2_success = 0;
+#ifdef PPP_WITH_MPPE
+    int mppe_enc_keys = 0;     /* whether or not these were received */
+    int mppe_enc_policy = 0;
+    int mppe_enc_types = 0;
+#endif
+#ifdef MSDNS
+    ipcp_options *wo = &ipcp_wantoptions[0];
+    ipcp_options *ao = &ipcp_allowoptions[0];
+    int got_msdns_1 = 0;
+    int got_msdns_2 = 0;
+    int got_wins_1 = 0;
+    int got_wins_2 = 0;
+#endif
 
     /* Send RADIUS attributes to anyone else who might be interested */
     if (radius_attributes_hook) {
@@ -552,18 +594,28 @@ radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
 
            case PW_SESSION_TIMEOUT:
                /* Session timeout */
-               maxconnect = vp->lvalue;
+               ppp_set_max_connect_time(vp->lvalue);
                break;
-#ifdef MAXOCTETS
+           case PW_FILTER_ID:
+               /* packet filter, will be handled via ip-(up|down) script */
+               ppp_script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
+               break;
+           case PW_FRAMED_ROUTE:
+               /* route, will be handled via ip-(up|down) script */
+               ppp_script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
+               break;
+           case PW_IDLE_TIMEOUT:
+               /* idle parameter */
+               ppp_set_max_idle_time(vp->lvalue);
+               break;
            case PW_SESSION_OCTETS_LIMIT:
                /* Session traffic limit */
-               maxoctets = vp->lvalue;
+               ppp_set_session_limit(vp->lvalue);
                break;
            case PW_OCTETS_DIRECTION:
                /* Session traffic limit direction check */
-               maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
+               ppp_set_session_limit_dir(vp->lvalue);
                break;
-#endif
            case PW_ACCT_INTERIM_INTERVAL:
                /* Send accounting updates every few seconds */
                rstate.acct_interim_interval = vp->lvalue;
@@ -583,7 +635,7 @@ radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
                } else if (remote != 0xfffffffe) {
                    /* 0xfffffffe means NAS should select an ip address */
                    remote = htonl(vp->lvalue);
-                   if (bad_ip_adrs (remote)) {
+                   if (ppp_bad_ip_addr (remote)) {
                        slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
                                 remote, rstate.user);
                        return -1;
@@ -592,37 +644,44 @@ radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
                    rstate.ip_addr = remote;
                }
                break;
+            case PW_NAS_IP_ADDRESS:
+                wo->ouraddr = htonl(vp->lvalue);
+                break;
            case PW_CLASS:
                /* Save Class attribute to pass it in accounting request */
-               if (vp->lvalue <= MAXCLASSLEN) {
+               // if (vp->lvalue <= MAXCLASSLEN) { // <- Attribute could be this big, but vp->strvalue is limited to AUTH_STRING_LEN characters
+               if (vp->lvalue <= AUTH_STRING_LEN) {
                    rstate.class_len=vp->lvalue;
                    memcpy(rstate.class, vp->strvalue, rstate.class_len);
                } /* else too big for our buffer - ignore it */
                break;
+           case PW_FRAMED_MTU:
+               ppp_set_mtu(rstate.client_port,MIN(ppp_get_mtu(rstate.client_port),vp->lvalue));
+               break;
            }
 
 
-#ifdef CHAPMS
        } else if (vp->vendorcode == VENDOR_MICROSOFT) {
+#ifdef PPP_WITH_CHAPMS
            switch (vp->attribute) {
            case PW_MS_CHAP2_SUCCESS:
-               if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
+               if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
                    slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
                    return -1;
                }
-               memcpy(cstate->saresponse, vp->strvalue + 3,
-                      MS_AUTH_RESPONSE_LENGTH);
-               cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
+               if (message != NULL)
+                   strlcpy(message, (char*) vp->strvalue + 1, message_space);
                ms_chap2_success = 1;
                break;
 
-#ifdef MPPE
+#ifdef PPP_WITH_MPPE
            case PW_MS_CHAP_MPPE_KEYS:
-               if (radius_setmppekeys(vp, req_info, cstate) < 0) {
+               if (radius_setmppekeys(vp, req_info, challenge) < 0) {
                    slprintf(msg, BUF_LEN,
                             "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
                    return -1;
                }
+               mppe_enc_keys = 1;
                break;
 
            case PW_MS_MPPE_SEND_KEY:
@@ -634,37 +693,77 @@ radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
                             "Send": "Recv");
                    return -1;
                }
+               mppe_enc_keys = 1;
                break;
-#endif /* MPPE */
-#if 0
+
            case PW_MS_MPPE_ENCRYPTION_POLICY:
+               mppe_enc_policy = vp->lvalue;   /* save for later */
+               break;
+
            case PW_MS_MPPE_ENCRYPTION_TYPES:
+               mppe_enc_types = vp->lvalue;    /* save for later */
+               break;
+
+#endif /* PPP_WITH_MPPE */
+#ifdef MSDNS
            case PW_MS_PRIMARY_DNS_SERVER:
+               ao->dnsaddr[0] = htonl(vp->lvalue);
+               got_msdns_1 = 1;
+               if (!got_msdns_2)
+                   ao->dnsaddr[1] = ao->dnsaddr[0];
+               break;
            case PW_MS_SECONDARY_DNS_SERVER:
+               ao->dnsaddr[1] = htonl(vp->lvalue);
+               got_msdns_2 = 1;
+               if (!got_msdns_1)
+                   ao->dnsaddr[0] = ao->dnsaddr[1];
+               break;
            case PW_MS_PRIMARY_NBNS_SERVER:
+               ao->winsaddr[0] = htonl(vp->lvalue);
+               got_wins_1 = 1;
+               if (!got_wins_2)
+                   ao->winsaddr[1] = ao->winsaddr[0];
+               break;
            case PW_MS_SECONDARY_NBNS_SERVER:
+               ao->winsaddr[1] = htonl(vp->lvalue);
+               got_wins_2 = 1;
+               if (!got_wins_1)
+                   ao->winsaddr[0] = ao->winsaddr[1];
                break;
-#endif
+#endif /* MSDNS */
            }
-#endif /* CHAPMS */
+#endif /* PPP_WITH_CHAPMS */
        }
        vp = vp->next;
     }
 
     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
-    if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
+    if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
        return -1;
 
+#ifdef PPP_WITH_MPPE
+    /*
+     * Require both policy and key attributes to indicate a valid key.
+     * Note that if the policy value was '0' we don't set the key!
+     */
+    if (mppe_enc_policy && mppe_enc_keys) {
+       /* Set/modify allowed encryption types. */
+       if (mppe_enc_types)
+           mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
+       return 0;
+    }
+    mppe_clear_keys();
+#endif
+
     return 0;
 }
 
-#ifdef MPPE
+#ifdef PPP_WITH_MPPE
 /**********************************************************************
 * %FUNCTION: radius_setmppekeys
 * %ARGUMENTS:
 *  vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
 *  req_info -- radius request information used for encryption
-*  cstate -- chap_state structure for challenge info
 * %RETURNS:
 *  >= 0 on success; -1 on failure
 * %DESCRIPTION:
@@ -672,12 +771,16 @@ radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
 *  See RFC 2548.
 ***********************************************************************/
 static int
-radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
+radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
+                  unsigned char *challenge)
 {
     int i;
-    MD5_CTX Context;
-    u_char  plain[32];
-    u_char  buf[16];
+    int status = 0;
+    PPP_MD_CTX *ctx;
+    unsigned char plain[32];
+    unsigned char buf[MD5_DIGEST_LENGTH];
+    unsigned int  buflen;
+
 
     if (vp->lvalue != 32) {
        error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
@@ -687,31 +790,70 @@ radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
 
     memcpy(plain, vp->strvalue, sizeof(plain));
 
-    MD5Init(&Context);
-    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
-    MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
-    MD5Final(buf, &Context);
+    ctx = PPP_MD_CTX_new();
+    if (ctx) {
 
-    for (i = 0; i < 16; i++)
-       plain[i] ^= buf[i];
+        if (PPP_DigestInit(ctx, PPP_md5())) {
 
-    MD5Init(&Context);
-    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
-    MD5Update(&Context, vp->strvalue, 16);
-    MD5Final(buf, &Context);
+            if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
 
-    for(i = 0; i < 16; i++)
-       plain[i + 16] ^= buf[i];
+                if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
 
-    /*
-     * Annoying.  The "key" returned is just the NTPasswordHashHash, which
-     * the NAS (us) doesn't need; we only need the start key.  So we have
-     * to generate the start key, sigh.  NB: We do not support the LM-Key.
-     */
-    mppe_set_keys(cstate->challenge, &plain[8]);
-    mppe_keys_set = 1;
+                    buflen = sizeof(buf);
+                    if (PPP_DigestFinal(ctx, buf, &buflen)) {
+
+                        status = 1;
+                    }
+                }
+            }
+        }
+        PPP_MD_CTX_free(ctx);
+    }
+
+    if (status) {
+
+        for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
+            plain[i] ^= buf[i];
+        }
+
+        status = 0;
+        ctx = PPP_MD_CTX_new();
+        if (ctx) {
+
+            if (PPP_DigestInit(ctx, PPP_md5())) {
+
+                if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
+
+                    if (PPP_DigestUpdate(ctx, vp->strvalue, 16)) {
+
+                        buflen = MD5_DIGEST_LENGTH;
+                        if (PPP_DigestFinal(ctx, buf, &buflen)) {
+
+                            status = 1;
+                        }
+                    }
+                }
+            }
+            PPP_MD_CTX_free(ctx);
+        }
+
+        if (status) {
+
+            for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
+                plain[i + 16] ^= buf[i];
+            }
+
+            /*
+             * Annoying.  The "key" returned is just the NTPasswordHashHash, which
+             * the NAS (us) doesn't need; we only need the start key.  So we have
+             * to generate the start key, sigh.  NB: We do not support the LM-Key.
+             */
+            mppe_set_chapv1(challenge, &plain[8]);
+            return 0;
+        }
+    }
 
-    return 0;    
+    return -1;
 }
 
 /**********************************************************************
@@ -729,11 +871,13 @@ static int
 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
 {
     int i;
-    MD5_CTX Context;
-    u_char  *salt = vp->strvalue;
-    u_char  *crypt = vp->strvalue + 2;
-    u_char  plain[32];
-    u_char  buf[MD5_SIGNATURE_SIZE];
+    int status = 0;
+    PPP_MD_CTX *ctx;
+    unsigned char *salt = vp->strvalue;
+    unsigned char *crypt = vp->strvalue + 2;
+    unsigned char plain[32];
+    unsigned char buf[MD5_DIGEST_LENGTH];
+    unsigned int  buflen;
     char    *type = "Send";
 
     if (vp->attribute == PW_MS_MPPE_RECV_KEY)
@@ -752,37 +896,78 @@ radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
 
     memcpy(plain, crypt, 32);
 
-    MD5Init(&Context);
-    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
-    MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
-    MD5Update(&Context, salt, 2);
-    MD5Final(buf, &Context);
+    ctx = PPP_MD_CTX_new();
+    if (!ctx) {
+       error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key attribute", type);
+       return -1;
+    }
+
+    buflen = sizeof(buf);
+    if (!PPP_DigestInit(ctx, PPP_md5())) {
+       error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key attribute", type);
+    } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
+       error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key attribute", type);
+    } else if (!PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
+       error("RADIUS: Error mixing in request vector for MS-MPPE-%s-Key attribute", type);
+    } else if (!PPP_DigestUpdate(ctx, salt, 2)) {
+       error("RADIUS: Error mixing in salt for MS-MPPE-%s-Key attribute", type);
+    } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
+       error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key attribute", type);
+    } else {
+       status = 1;
+    }
+
+    PPP_MD_CTX_free(ctx);
 
-    for (i = 0; i < 16; i++)
+    if (!status)
+       return -1;
+
+    for (i = 0; i < 16; i++) {
        plain[i] ^= buf[i];
+    }
 
-    if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
+    if (plain[0] != 16) {
        error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
-             (int) plain[0], type);
+               (int) plain[0], type);
        return -1;
     }
 
-    MD5Init(&Context);
-    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
-    MD5Update(&Context, crypt, 16);
-    MD5Final(buf, &Context);
+    status = 0;
+    ctx = PPP_MD_CTX_new();
+    if (!ctx) {
+       error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key(2) attribute", type);
+       return -1;
+    }
 
-    plain[16] ^= buf[0]; /* only need the first byte */
+    buflen = sizeof(buf);
 
-    if (vp->attribute == PW_MS_MPPE_SEND_KEY)
-       memcpy(mppe_send_key, plain + 1, 16);
-    else
-       memcpy(mppe_recv_key, plain + 1, 16);
-    mppe_keys_set = 1;
+    if (!PPP_DigestInit(ctx, PPP_md5())) {
+       error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key(2) attribute", type);
+    } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
+       error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key(2) attribute", type);
+    } else if (!PPP_DigestUpdate(ctx, crypt, 16)) {
+       error("RADIUS: Error mixing in crypt vector for MS-MPPE-%s-Key(2) attribute", type);
+    } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
+       error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key(2) attribute", type);
+    } else {
+       status = 1;
+    }
+
+    PPP_MD_CTX_free(ctx);
+
+    if (!status)
+       return -1;
 
+    plain[16] ^= buf[0]; /* only need the first byte */
+
+    if (vp->attribute == PW_MS_MPPE_SEND_KEY) {
+       mppe_set_keys(plain + 1, NULL, 16);
+    } else {
+       mppe_set_keys(NULL, plain + 1, 16);
+    }
     return 0;
 }
-#endif /* MPPE */
+#endif /* PPP_WITH_MPPE */
 
 /**********************************************************************
 * %FUNCTION: radius_acct_start
@@ -801,6 +986,8 @@ radius_acct_start(void)
     VALUE_PAIR *send = NULL;
     ipcp_options *ho = &ipcp_hisoptions[0];
     u_int32_t hisaddr;
+    const char *remote_number;
+    const char *ipparam;
 
     if (!rstate.initialized) {
        return;
@@ -808,7 +995,7 @@ radius_acct_start(void)
 
     rstate.start_time = time(NULL);
 
-    strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
+    strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
 
     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
                   rstate.session_id, 0, VENDOR_NONE);
@@ -828,16 +1015,19 @@ radius_acct_start(void)
     av_type = PW_PPP;
     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
 
-    if (*remote_number) {
+    remote_number = ppp_get_remote_number();
+    ipparam = ppp_ipparam();
+    if (remote_number) {
        rc_avpair_add(&send, PW_CALLING_STATION_ID,
                       remote_number, 0, VENDOR_NONE);
-    }
+    } else if (ipparam)
+       rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
 
     av_type = PW_RADIUS;
     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
 
 
-    av_type = PW_ASYNC;
+    av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
 
     hisaddr = ho->hisaddr;
@@ -861,12 +1051,11 @@ radius_acct_start(void)
        /* RADIUS server could be down so make this a warning */
        syslog(LOG_WARNING,
                "Accounting START failed for %s", rstate.user);
-    } else {
-       rstate.accounting_started = 1;
-       /* Kick off periodic accounting reports */
-       if (rstate.acct_interim_interval) {
-           TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
-       }
+    }
+
+    /* Kick off periodic accounting reports */
+    if (rstate.acct_interim_interval) {
+       ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
     }
 }
 
@@ -887,21 +1076,26 @@ radius_acct_stop(void)
     ipcp_options *ho = &ipcp_hisoptions[0];
     u_int32_t hisaddr;
     int result;
+    const char *remote_number;
+    const char *ipparam;
+    ppp_link_stats_st stats;
 
     if (!rstate.initialized) {
        return;
     }
 
-    if (!rstate.accounting_started) {
-       return;
-    }
+    if (rstate.acct_interim_interval)
+       ppp_untimeout(radius_acct_interim, NULL);
 
-    rstate.accounting_started = 0;
     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
                   0, VENDOR_NONE);
 
     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
 
+    if (rstate.class_len > 0)
+       rc_avpair_add(&send, PW_CLASS,
+                     rstate.class, rstate.class_len, VENDOR_NONE);
+
     av_type = PW_STATUS_STOP;
     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
 
@@ -914,32 +1108,97 @@ radius_acct_stop(void)
     av_type = PW_RADIUS;
     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
 
+    if (ppp_get_link_stats(&stats)) {
 
-    if (link_stats_valid) {
-       av_type = link_connect_time;
+       av_type = ppp_get_link_uptime();
        rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.bytes_out;
+       av_type = stats.bytes_out & 0xFFFFFFFF;
        rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.bytes_in;
+       if (stats.bytes_out > 0xFFFFFFFF) {
+           av_type = stats.bytes_out >> 32;
+           rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
+       }
+
+       av_type = stats.bytes_in & 0xFFFFFFFF;
        rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.pkts_out;
+       if (stats.bytes_in > 0xFFFFFFFF) {
+           av_type = stats.bytes_in >> 32;
+           rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
+       }
+
+       av_type = stats.pkts_out;
        rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.pkts_in;
+       av_type = stats.pkts_in;
        rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
     }
 
-    if (*remote_number) {
+    remote_number = ppp_get_remote_number();
+    ipparam = ppp_ipparam();
+    if (remote_number) {
        rc_avpair_add(&send, PW_CALLING_STATION_ID,
                       remote_number, 0, VENDOR_NONE);
-    }
+    } else if (ipparam)
+       rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
 
-    av_type = PW_ASYNC;
+    av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
 
+    av_type = PW_NAS_ERROR;
+    switch( ppp_status() ) {
+       case EXIT_OK:
+           av_type = PW_USER_REQUEST;
+           break;
+
+       case EXIT_USER_REQUEST:
+           av_type = PW_ADMIN_RESET;
+           break;
+
+       case EXIT_HANGUP:
+       case EXIT_PEER_DEAD:
+       case EXIT_CONNECT_FAILED:
+           av_type = PW_LOST_CARRIER;
+           break;
+
+       case EXIT_INIT_FAILED:
+       case EXIT_OPEN_FAILED:
+       case EXIT_LOCK_FAILED:
+       case EXIT_PTYCMD_FAILED:
+           av_type = PW_PORT_ERROR;
+           break;
+
+       case EXIT_PEER_AUTH_FAILED:
+       case EXIT_AUTH_TOPEER_FAILED:
+       case EXIT_NEGOTIATION_FAILED:
+       case EXIT_CNID_AUTH_FAILED:
+           av_type = PW_SERVICE_UNAVAILABLE;
+           break;
+
+       case EXIT_IDLE_TIMEOUT:
+           av_type = PW_ACCT_IDLE_TIMEOUT;
+           break;
+
+       case EXIT_CALLBACK:
+           av_type = PW_CALLBACK;
+           break;
+           
+       case EXIT_CONNECT_TIME:
+           av_type = PW_ACCT_SESSION_TIMEOUT;
+           break;
+           
+       case EXIT_TRAFFIC_LIMIT:
+           av_type = PW_NAS_REQUEST;
+           break;
+
+       default:
+           av_type = PW_NAS_ERROR;
+           break;
+    }
+    rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
+
     hisaddr = ho->hisaddr;
     av_type = htonl(hisaddr);
     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
@@ -980,20 +1239,23 @@ radius_acct_interim(void *ignored)
     ipcp_options *ho = &ipcp_hisoptions[0];
     u_int32_t hisaddr;
     int result;
+    const char *remote_number;
+    const char *ipparam;
+    ppp_link_stats_st stats;
 
     if (!rstate.initialized) {
        return;
     }
 
-    if (!rstate.accounting_started) {
-       return;
-    }
-
     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
                   0, VENDOR_NONE);
 
     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
 
+    if (rstate.class_len > 0)
+       rc_avpair_add(&send, PW_CLASS,
+                     rstate.class, rstate.class_len, VENDOR_NONE);
+
     av_type = PW_STATUS_ALIVE;
     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
 
@@ -1006,34 +1268,43 @@ radius_acct_interim(void *ignored)
     av_type = PW_RADIUS;
     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
 
-    /* Update link stats */
-    update_link_stats(0);
+    if (ppp_get_link_stats(&stats)) {
 
-    if (link_stats_valid) {
-       link_stats_valid = 0; /* Force later code to update */
-
-       av_type = link_connect_time;
+       av_type = ppp_get_link_uptime();
        rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.bytes_out;
+       av_type = stats.bytes_out & 0xFFFFFFFF;
        rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.bytes_in;
+       if (stats.bytes_out > 0xFFFFFFFF) {
+           av_type = stats.bytes_out >> 32;
+           rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
+       }
+
+       av_type = stats.bytes_in & 0xFFFFFFFF;
        rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.pkts_out;
+       if (stats.bytes_in > 0xFFFFFFFF) {
+           av_type = stats.bytes_in >> 32;
+           rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
+       }
+
+       av_type = stats.pkts_out;
        rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
 
-       av_type = link_stats.pkts_in;
+       av_type = stats.pkts_in;
        rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
     }
 
-    if (*remote_number) {
+    remote_number = ppp_get_remote_number();
+    ipparam = ppp_ipparam();
+    if (remote_number) {
        rc_avpair_add(&send, PW_CALLING_STATION_ID,
                       remote_number, 0, VENDOR_NONE);
-    }
+    } else if (ipparam)
+       rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
 
-    av_type = PW_ASYNC;
+    av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
 
     hisaddr = ho->hisaddr;
@@ -1059,7 +1330,7 @@ radius_acct_interim(void *ignored)
     rc_avpair_free(send);
 
     /* Schedule another one */
-    TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
+    ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
 }
 
 /**********************************************************************
@@ -1156,7 +1427,7 @@ radius_init(char *msg)
 *  Extracts the port number from the interface name
 ***********************************************************************/
 static int
-get_client_port(char *ifname)
+get_client_port(const char *ifname)
 {
     int port;
     if (sscanf(ifname, "ppp%d", &port) == 1) {