]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/plugins/radius/radius.c
make Octets-Direction flag accept value=4.
[ppp.git] / pppd / plugins / radius / radius.c
index 358fc95a654c733dcd704b9bb7857a1073fdbb41..389dff9152bc5510332e5c5bd760daffcdbce6a4 100644 (file)
@@ -2,8 +2,8 @@
 *
 * radius.c
 *
-* RADIUS plugin for pppd.  Performs PAP, CHAP and MS-CHAP authentication
-* using RADIUS.
+* RADIUS plugin for pppd.  Performs PAP, CHAP, MS-CHAP, MS-CHAPv2
+* authentication using RADIUS.
 *
 * Copyright (C) 2002 Roaring Penguin Software Inc.
 *
 *    Copyright (C) 1995,1996,1997,1998 Lars Fenneberg <lf@elemental.net>
 *    Copyright (C) 2002 Roaring Penguin Software Inc.
 *
+* MPPE support is by Ralf Hofmann, <ralf.hofmann@elvido.net>, with
+* modification from Frank Cusack, <frank@google.com>.
+*
 * This plugin may be distributed according to the terms of the GNU
 * General Public License, version 2 or (at your option) any later version.
 *
 ***********************************************************************/
 static char const RCSID[] =
-"$Id: radius.c,v 1.5 2002/03/04 14:59:51 dfs Exp $";
+"$Id: radius.c,v 1.11 2002/07/15 11:04:18 kad 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"
@@ -58,11 +64,17 @@ static int radius_chap_auth(char *user,
 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(VALUE_PAIR *vp, char *msg);
+static int radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
+                           REQUEST_INFO *req_info);
 static void radius_choose_ip(u_int32_t *addrp);
 static int radius_init(char *msg);
 static int get_client_port(char *ifname);
 static int radius_allowed_address(u_int32_t addr);
+#ifdef MPPE
+static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
+                             chap_state *);
+static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
+#endif
 
 #ifndef MAXSESSIONID
 #define MAXSESSIONID 32
@@ -227,13 +239,13 @@ radius_pap_auth(char *user,
     if (rstate.authserver) {
        result = rc_auth_using_server(rstate.authserver,
                                      rstate.client_port, send,
-                                     &received, radius_msg);
+                                     &received, radius_msg, NULL);
     } else {
-       result = rc_auth(rstate.client_port, send, &received, radius_msg);
+       result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
     }
 
     if (result == OK_RC) {
-       if (radius_setparams(received, radius_msg) < 0) {
+       if (radius_setparams(NULL, received, radius_msg, NULL) < 0) {
            result = ERROR_RC;
        }
     }
@@ -255,7 +267,7 @@ radius_pap_auth(char *user,
 * %RETURNS:
 *  CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
 * %DESCRIPTION:
-* Performs CHAP and MS-CHAP authentication using RADIUS
+* Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
 ***********************************************************************/
 static int
 radius_chap_auth(char *user,
@@ -268,6 +280,13 @@ radius_chap_auth(char *user,
     static char radius_msg[BUF_LEN];
     int result;
     u_char cpassword[MAX_RESPONSE_LENGTH + 1];
+#ifdef 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
+
     radius_msg[0] = 0;
 
     if (radius_init(radius_msg) < 0) {
@@ -279,6 +298,7 @@ radius_chap_auth(char *user,
     if ((cstate->chal_type != CHAP_DIGEST_MD5)
 #ifdef CHAPMS
        && (cstate->chal_type != CHAP_MICROSOFT)
+       && (cstate->chal_type != CHAP_MICROSOFT_V2)
 #endif
        ) {
        error("RADIUS: Challenge type %u unsupported", cstate->chal_type);
@@ -325,7 +345,7 @@ radius_chap_auth(char *user,
     case CHAP_MICROSOFT:
     {
        /* MS-CHAP-Challenge and MS-CHAP-Response */
-       MS_ChapResponse *rmd = remmd;
+       MS_ChapResponse *rmd = (MS_ChapResponse *) remmd;
        u_char *p = cpassword;
 
        *p++ = cstate->chal_id;
@@ -342,6 +362,29 @@ radius_chap_auth(char *user,
                      cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
        break;
     }
+
+    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;
+       /* 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));
+
+       rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
+                     cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
+       rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
+                     cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
+       break;
+    }
 #endif
 
     }
@@ -353,14 +396,15 @@ radius_chap_auth(char *user,
     if (rstate.authserver) {
        result = rc_auth_using_server(rstate.authserver,
                                      rstate.client_port, send,
-                                     &received, radius_msg);
+                                     &received, radius_msg, req_info);
     } else {
-       result = rc_auth(rstate.client_port, send, &received, radius_msg);
+       result = rc_auth(rstate.client_port, send, &received, radius_msg,
+                        req_info);
     }
 
     if (result == OK_RC) {
        if (!rstate.done_chap_once) {
-           if (radius_setparams(received, radius_msg) < 0) {
+           if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
                error("%s", radius_msg);
                result = ERROR_RC;
            } else {
@@ -408,18 +452,20 @@ 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:
 *  >= 0 on success; -1 on failure
 * %DESCRIPTION:
-*  Parses attributes sent by RADIUS server and sets them in pppd.  Currently,
-*  used only to set IP address.
+*  Parses attributes sent by RADIUS server and sets them in pppd.
 ***********************************************************************/
 static int
-radius_setparams(VALUE_PAIR *vp, char *msg)
+radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
+                REQUEST_INFO *req_info)
 {
     u_int32_t remote;
+    int ms_chap2_success = 0;
 
     /* Send RADIUS attributes to anyone else who might be interested */
     if (radius_attributes_hook) {
@@ -443,6 +489,7 @@ radius_setparams(VALUE_PAIR *vp, char *msg)
                    return -1;
                }
                break;
+
            case PW_FRAMED_PROTOCOL:
                /* check for framed protocol type       */
                /* if not PPP then also exit            */
@@ -453,6 +500,20 @@ radius_setparams(VALUE_PAIR *vp, char *msg)
                }
                break;
 
+           case PW_SESSION_TIMEOUT:
+               /* Session timeout */
+               maxconnect = vp->lvalue;
+               break;
+#ifdef MAXOCTETS
+           case PW_SESSION_OCTETS_LIMIT:
+               /* Session traffic limit */
+               maxoctets = vp->lvalue;
+               break;
+           case PW_OCTETS_DIRECTION:
+               /* Session traffic limit direction check */
+               maxoctets_dir = ( vp->lvalue > 4 ) 0 : vp->lvalue ;
+               break;
+#endif         
            case PW_FRAMED_IP_ADDRESS:
                /* seting up remote IP addresses */
                remote = vp->lvalue;
@@ -470,13 +531,187 @@ radius_setparams(VALUE_PAIR *vp, char *msg)
                    rstate.choose_ip = 1;
                    rstate.ip_addr = remote;
                }
-           break;
+               break;
            }
+#ifdef CHAPMS
+       } else if (vp->vendorcode == VENDOR_MICROSOFT) {
+           switch (vp->attribute) {
+           case PW_MS_CHAP2_SUCCESS:
+               if ((vp->lvalue != 43) || strncmp(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';
+               ms_chap2_success = 1;
+               break;
+
+#ifdef MPPE
+           case PW_MS_CHAP_MPPE_KEYS:
+               if (radius_setmppekeys(vp, req_info, cstate) < 0) {
+                   slprintf(msg, BUF_LEN,
+                            "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
+                   return -1;
+               }
+               break;
+
+           case PW_MS_MPPE_SEND_KEY:
+           case PW_MS_MPPE_RECV_KEY:
+               if (radius_setmppekeys2(vp, req_info) < 0) {
+                   slprintf(msg, BUF_LEN,
+                            "RADIUS: bad MS-MPPE-%s-Key attribute",
+                            (vp->attribute == PW_MS_MPPE_SEND_KEY)?
+                            "Send": "Recv");
+                   return -1;
+               }
+               break;
+#endif /* MPPE */
+#if 0
+           case PW_MS_MPPE_ENCRYPTION_POLICY:
+           case PW_MS_MPPE_ENCRYPTION_TYPES:
+           case PW_MS_PRIMARY_DNS_SERVER:
+           case PW_MS_SECONDARY_DNS_SERVER:
+           case PW_MS_PRIMARY_NBNS_SERVER:
+           case PW_MS_SECONDARY_NBNS_SERVER:
+               break;
+#endif
+           }
+#endif /* 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)
+       return -1;
+
+    return 0;
+}
+
+#ifdef 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:
+*  Decrypt the "key" provided by the RADIUS server for MPPE encryption.
+*  See RFC 2548.
+***********************************************************************/
+static int
+radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
+{
+    int i;
+    MD5_CTX Context;
+    u_char  plain[32];
+    u_char  buf[16];
+
+    if (vp->lvalue != 32) {
+       error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
+             vp->lvalue);
+       return -1;
+    }
+
+    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);
+
+    for (i = 0; i < 16; i++)
+       plain[i] ^= buf[i];
+
+    MD5Init(&Context);
+    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
+    MD5Update(&Context, vp->strvalue, 16);
+    MD5Final(buf, &Context);
+
+    for(i = 0; i < 16; 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_keys(cstate->challenge, &plain[8]);
+
+    return 0;    
+}
+
+/**********************************************************************
+* %FUNCTION: radius_setmppekeys2
+* %ARGUMENTS:
+*  vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
+*  req_info -- radius request information used for encryption
+* %RETURNS:
+*  >= 0 on success; -1 on failure
+* %DESCRIPTION:
+*  Decrypt the key provided by the RADIUS server for MPPE encryption.
+*  See RFC 2548.
+***********************************************************************/
+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];
+    char    *type = "Send";
+
+    if (vp->attribute == PW_MS_MPPE_RECV_KEY)
+       type = "Recv";
+
+    if (vp->lvalue != 34) {
+       error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
+             vp->lvalue, type);
+       return -1;
+    }
+
+    if ((salt[0] & 0x80) == 0) {
+       error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
+       return -1;
+    }
+
+    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);
+
+    for (i = 0; i < 16; i++)
+       plain[i] ^= buf[i];
+
+    if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
+       error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
+             (int) plain[0], type);
+       return -1;
+    }
+
+    MD5Init(&Context);
+    MD5Update(&Context, req_info->secret, strlen(req_info->secret));
+    MD5Update(&Context, crypt, 16);
+    MD5Final(buf, &Context);
+
+    plain[16] ^= buf[0]; /* only need the first byte */
+
+    if (vp->attribute == PW_MS_MPPE_SEND_KEY)
+       memcpy(mppe_send_key, plain + 1, 16);
+    else
+       memcpy(mppe_recv_key, plain + 1, 16);
+
     return 0;
 }
+#endif /* MPPE */
 
 /**********************************************************************
 * %FUNCTION: radius_acct_start