X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fplugins%2Fradius%2Fradius.c;h=76b68e105343f61b3d6c9730f0e31a941e94976e;hp=3dc69aeae92b2a2b42d97fed65afcf8e8af2d052;hb=216967db316a190aa012a57660a2aad8124452a7;hpb=734747f7ef4d64cc7f92ea953c4ab57581c01f8f diff --git a/pppd/plugins/radius/radius.c b/pppd/plugins/radius/radius.c index 3dc69ae..76b68e1 100644 --- a/pppd/plugins/radius/radius.c +++ b/pppd/plugins/radius/radius.c @@ -2,8 +2,8 @@ * * radius.c * -* RADIUS plugin for pppd. Performs PAP and 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. * @@ -16,28 +16,46 @@ * Copyright (C) 1995,1996,1997,1998 Lars Fenneberg * Copyright (C) 2002 Roaring Penguin Software Inc. * +* MPPE support is by Ralf Hofmann, , with +* modification from Frank Cusack, . +* * 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.2 2002/02/08 17:28:31 dfs Exp $"; +"$Id: radius.c,v 1.21 2003/11/25 11:50:10 paulus Exp $"; #include "pppd.h" -#include "chap.h" +#include "chap-new.h" +#ifdef CHAPMS +#include "chap_ms.h" +#ifdef MPPE +#include "md5.h" +#endif +#endif #include "radiusclient.h" #include "fsm.h" #include "ipcp.h" #include #include #include +#include #define BUF_LEN 1024 +#define MD5_HASH_SIZE 16 + static char *config_file = NULL; +static int add_avp(char **); +static struct avpopt { + char *vpstr; + struct avpopt *next; +} *avpopt = NULL; static option_t Options[] = { { "radius-config-file", o_string, &config_file }, + { "avpair", o_special, add_avp }, { NULL } }; @@ -47,27 +65,38 @@ static int radius_pap_auth(char *user, 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 int radius_chap_verify(char *user, char *ourname, int id, + struct chap_digest_type *digest, + unsigned char *challenge, + unsigned char *response, + unsigned char *message, int message_space); 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(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 radius_allowed_address(u_int32_t addr); - -void (*radius_attributes_hook)(VALUE_PAIR *) = NULL; -void (*radius_pre_auth_hook)(char const *user) = NULL; +static void radius_acct_interim(void *); +#ifdef MPPE +static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, + unsigned char *); +static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info); +#endif #ifndef MAXSESSIONID #define MAXSESSIONID 32 #endif +#ifndef MAXCLASSLEN +#define MAXCLASSLEN 500 +#endif + struct radius_state { int accounting_started; int initialized; @@ -80,8 +109,22 @@ struct radius_state { char config_file[MAXPATHLEN]; char session_id[MAXSESSIONID + 1]; time_t start_time; + int acct_interim_interval; + SERVER *authserver; /* Authentication server to use */ + SERVER *acctserver; /* Accounting server to use */ + int class_len; + char class[MAXCLASSLEN]; + VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */ }; +void (*radius_attributes_hook)(VALUE_PAIR *) = NULL; + +/* The pre_auth_hook MAY set authserver and acctserver if it wants. + In that case, they override the values in the radiusclient.conf file */ +void (*radius_pre_auth_hook)(char const *user, + SERVER **authserver, + SERVER **acctserver) = NULL; + static struct radius_state rstate; char pppd_version[] = VERSION; @@ -102,7 +145,7 @@ 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; @@ -120,6 +163,28 @@ plugin_init(void) info("RADIUS plugin initialized."); } +/********************************************************************** +* %FUNCTION: add_avp +* %ARGUMENTS: +* argv -- the pair to add +* %RETURNS: +* 1 +* %DESCRIPTION: +* Adds an av pair to be passed on to the RADIUS server on each request. +***********************************************************************/ +static int +add_avp(char **argv) +{ + struct avpopt *p = malloc(sizeof(struct avpopt)); + + /* Append to a list of vp's for later parsing */ + p->vpstr = strdup(*argv); + p->next = avpopt; + avpopt = p; + + return 1; +} + /********************************************************************** * %FUNCTION: radius_secret_check * %ARGUMENTS: @@ -189,7 +254,9 @@ radius_pap_auth(char *user, make_username_realm(user); if (radius_pre_auth_hook) { - radius_pre_auth_hook(rstate.user); + radius_pre_auth_hook(rstate.user, + &rstate.authserver, + &rstate.acctserver); } send = NULL; @@ -212,10 +279,20 @@ radius_pap_auth(char *user, VENDOR_NONE); } - result = rc_auth(rstate.client_port, send, &received, radius_msg); + /* Add user specified vp's */ + if (rstate.avp) + rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp)); + + if (rstate.authserver) { + result = rc_auth_using_server(rstate.authserver, + rstate.client_port, send, + &received, radius_msg, NULL); + } else { + 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(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) { result = ERROR_RC; } } @@ -228,39 +305,59 @@ 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 authentication using RADIUS +* 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, + unsigned char *message, int message_space) { VALUE_PAIR *send, *received; UINT4 av_type; static char radius_msg[BUF_LEN]; int result; - u_char cpassword[MD5_SIGNATURE_SIZE+1]; + int challenge_len, response_len; + u_char cpassword[MAX_RESPONSE_LEN + 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 + + 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; } - /* we handle md5 digest at the moment */ - if (cstate->chal_type != CHAP_DIGEST_MD5) { - error("RADIUS: Challenge type not MD5"); - return CHAP_FAILURE; + /* return error for types we can't handle */ + if ((digest->code != CHAP_MD5) +#ifdef CHAPMS + && (digest->code != CHAP_MICROSOFT) + && (digest->code != CHAP_MICROSOFT_V2) +#endif + ) { + error("RADIUS: Challenge type %u unsupported", digest->code); + return 0; } /* Put user with potentially realm added in rstate.user */ @@ -268,7 +365,9 @@ radius_chap_auth(char *user, make_username_realm(user); rstate.client_port = get_client_port (ifname); if (radius_pre_auth_hook) { - radius_pre_auth_hook(rstate.user); + radius_pre_auth_hook(rstate.user, + &rstate.authserver, + &rstate.acctserver); } } @@ -283,26 +382,99 @@ radius_chap_auth(char *user, rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE); /* - * add the CHAP-Password and CHAP-Challenge fields + * add the challenge and response fields */ + switch (digest->code) { + case CHAP_MD5: + /* CHAP-Challenge and CHAP-Password */ + if (response_len != MD5_HASH_SIZE) + return 0; + cpassword[0] = id; + memcpy(&cpassword[1], response, MD5_HASH_SIZE); + + rc_avpair_add(&send, PW_CHAP_CHALLENGE, + challenge, challenge_len, VENDOR_NONE); + rc_avpair_add(&send, PW_CHAP_PASSWORD, + cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE); + break; + +#ifdef CHAPMS + case CHAP_MICROSOFT: + { + /* MS-CHAP-Challenge and MS-CHAP-Response */ + MS_ChapResponse *rmd = (MS_ChapResponse *) response; + u_char *p = cpassword; + + 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)); + + rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE, + challenge, challenge_len, VENDOR_MICROSOFT); + rc_avpair_add(&send, PW_MS_CHAP_RESPONSE, + cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT); + break; + } - cpassword[0] = cstate->chal_id; - - memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE); + case CHAP_MICROSOFT_V2: + { + /* MS-CHAP-Challenge and MS-CHAP2-Response */ + MS_Chap2Response *rmd = (MS_Chap2Response *) (response + 1); + u_char *p = cpassword; + + 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)); + + rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE, + challenge, challenge_len, VENDOR_MICROSOFT); + rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE, + cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT); + break; + } +#endif + } - rc_avpair_add(&send, PW_CHAP_PASSWORD, cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE); + if (*remote_number) { + rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0, + VENDOR_NONE); + } - rc_avpair_add(&send, PW_CHAP_CHALLENGE, cstate->challenge, cstate->chal_len, VENDOR_NONE); + /* Add user specified vp's */ + if (rstate.avp) + rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp)); /* * make authentication with RADIUS server */ - result = rc_auth (rstate.client_port, send, &received, radius_msg); + if (rstate.authserver) { + result = rc_auth_using_server(rstate.authserver, + rstate.client_port, send, + &received, radius_msg, req_info); + } else { + 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(received, radius_msg, req_info, digest, + challenge, message, message_space) < 0) { error("%s", radius_msg); result = ERROR_RC; } else { @@ -313,7 +485,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); } /********************************************************************** @@ -355,13 +527,20 @@ make_username_realm(char *user) * %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(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 MPPE + int mppe_enc_keys = 0; /* whether or not these were received */ + int mppe_enc_policy = 0; + int mppe_enc_types = 0; +#endif /* Send RADIUS attributes to anyone else who might be interested */ if (radius_attributes_hook) { @@ -374,50 +553,281 @@ radius_setparams(VALUE_PAIR *vp, char *msg) */ while (vp) { - switch (vp->attribute) { - case PW_SERVICE_TYPE: - /* check for service type */ - /* if not FRAMED then exit */ - if (vp->lvalue != PW_FRAMED) { - slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s", - vp->lvalue, rstate.user); - return -1; - } - break; - case PW_FRAMED_PROTOCOL: - /* check for framed protocol type */ - /* if not PPP then also exit */ - if (vp->lvalue != PW_PPP) { - slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s", - vp->lvalue, rstate.user); - return -1; + if (vp->vendorcode == VENDOR_NONE) { + switch (vp->attribute) { + case PW_SERVICE_TYPE: + /* check for service type */ + /* if not FRAMED then exit */ + if (vp->lvalue != PW_FRAMED) { + slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s", + vp->lvalue, rstate.user); + return -1; + } + break; + + case PW_FRAMED_PROTOCOL: + /* check for framed protocol type */ + /* if not PPP then also exit */ + if (vp->lvalue != PW_PPP) { + slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s", + vp->lvalue, rstate.user); + return -1; + } + 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_ACCT_INTERIM_INTERVAL: + /* Send accounting updates every few seconds */ + rstate.acct_interim_interval = vp->lvalue; + /* RFC says it MUST NOT be less than 60 seconds */ + /* We use "0" to signify not sending updates */ + if (rstate.acct_interim_interval && + rstate.acct_interim_interval < 60) { + rstate.acct_interim_interval = 60; + } + break; + case PW_FRAMED_IP_ADDRESS: + /* seting up remote IP addresses */ + remote = vp->lvalue; + if (remote == 0xffffffff) { + /* 0xffffffff means user should be allowed to select one */ + rstate.any_ip_addr_ok = 1; + } else if (remote != 0xfffffffe) { + /* 0xfffffffe means NAS should select an ip address */ + remote = htonl(vp->lvalue); + if (bad_ip_adrs (remote)) { + slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s", + remote, rstate.user); + return -1; + } + rstate.choose_ip = 1; + rstate.ip_addr = remote; + } + break; + case PW_CLASS: + /* Save Class attribute to pass it in accounting request */ + if (vp->lvalue <= MAXCLASSLEN) { + rstate.class_len=vp->lvalue; + memcpy(rstate.class, vp->strvalue, rstate.class_len); + } /* else too big for our buffer - ignore it */ + break; } - break; - - case PW_FRAMED_IP_ADDRESS: - /* seting up remote IP addresses */ - remote = vp->lvalue; - if (remote == 0xffffffff) { - /* 0xffffffff means user should be allowed to select one */ - rstate.any_ip_addr_ok = 1; - } else if (remote != 0xfffffffe) { - /* 0xfffffffe means NAS should select an ip address */ - remote = htonl(vp->lvalue); - if (bad_ip_adrs (remote)) { - slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s", - remote, rstate.user); + + +#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; + } + if (message != NULL) + strlcpy(message, vp->strvalue + 1, message_space); + ms_chap2_success = 1; + break; + +#ifdef MPPE + case PW_MS_CHAP_MPPE_KEYS: + 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: + 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; } - rstate.choose_ip = 1; - rstate.ip_addr = remote; + mppe_enc_keys = 1; + break; + + 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 /* MPPE */ +#if 0 + 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 } - break; +#endif /* CHAPMS */ } vp = vp->next; } + + /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */ + if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success) + return -1; + +#ifdef 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) { + mppe_keys_set = 1; + /* Set/modify allowed encryption types. */ + if (mppe_enc_types) + set_mppe_enc_types(mppe_enc_policy, mppe_enc_types); + } +#endif + 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 +* %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, + unsigned char *challenge) +{ + 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(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_HASH_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 * %ARGUMENTS: @@ -449,6 +859,10 @@ radius_acct_start(void) 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_START; rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE); @@ -474,7 +888,16 @@ radius_acct_start(void) av_type = htonl(hisaddr); rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE); - result = rc_acct(rstate.client_port, send); + /* Add user specified vp's */ + if (rstate.avp) + rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp)); + + if (rstate.acctserver) { + result = rc_acct_using_server(rstate.acctserver, + rstate.client_port, send); + } else { + result = rc_acct(rstate.client_port, send); + } rc_avpair_free(send); @@ -484,6 +907,10 @@ radius_acct_start(void) "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); + } } } @@ -561,7 +988,17 @@ radius_acct_stop(void) av_type = htonl(hisaddr); rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE); - result = rc_acct(rstate.client_port, send); + /* Add user specified vp's */ + if (rstate.avp) + rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp)); + + if (rstate.acctserver) { + result = rc_acct_using_server(rstate.acctserver, + rstate.client_port, send); + } else { + result = rc_acct(rstate.client_port, send); + } + if (result != OK_RC) { /* RADIUS server could be down so make this a warning */ syslog(LOG_WARNING, @@ -570,6 +1007,105 @@ radius_acct_stop(void) rc_avpair_free(send); } +/********************************************************************** +* %FUNCTION: radius_acct_interim +* %ARGUMENTS: +* None +* %RETURNS: +* Nothing +* %DESCRIPTION: +* Sends an interim accounting message to the RADIUS server +***********************************************************************/ +static void +radius_acct_interim(void *ignored) +{ + UINT4 av_type; + VALUE_PAIR *send = NULL; + ipcp_options *ho = &ipcp_hisoptions[0]; + u_int32_t hisaddr; + int result; + + 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); + + av_type = PW_STATUS_ALIVE; + rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE); + + av_type = PW_FRAMED; + rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE); + + av_type = PW_PPP; + rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE); + + av_type = PW_RADIUS; + rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE); + + /* Update link stats */ + update_link_stats(0); + + if (link_stats_valid) { + link_stats_valid = 0; /* Force later code to update */ + + av_type = link_connect_time; + rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE); + + av_type = link_stats.bytes_out; + rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE); + + av_type = link_stats.bytes_in; + rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE); + + av_type = link_stats.pkts_out; + rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE); + + av_type = link_stats.pkts_in; + rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE); + } + + if (*remote_number) { + rc_avpair_add(&send, PW_CALLING_STATION_ID, + remote_number, 0, VENDOR_NONE); + } + + av_type = PW_ASYNC; + rc_avpair_add(&send, PW_NAS_PORT_TYPE, &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); + + /* Add user specified vp's */ + if (rstate.avp) + rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp)); + + if (rstate.acctserver) { + result = rc_acct_using_server(rstate.acctserver, + rstate.client_port, send); + } else { + result = rc_acct(rstate.client_port, send); + } + + if (result != OK_RC) { + /* RADIUS server could be down so make this a warning */ + syslog(LOG_WARNING, + "Interim accounting failed for %s", rstate.user); + } + rc_avpair_free(send); + + /* Schedule another one */ + TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval); +} + /********************************************************************** * %FUNCTION: radius_ip_up * %ARGUMENTS: @@ -641,6 +1177,16 @@ radius_init(char *msg) rc_conf_str("mapfile")); return -1; } + + /* Add av pairs saved during option parsing */ + while (avpopt) { + struct avpopt *n = avpopt->next; + + rc_avpair_parse(avpopt->vpstr, &rstate.avp); + free(avpopt->vpstr); + free(avpopt); + avpopt = n; + } return 0; } @@ -699,4 +1245,3 @@ char *radius_logged_in_user(void) { return rstate.user; } -