1 /***********************************************************************
5 * RADIUS plugin for pppd. Performs PAP, CHAP, MS-CHAP, MS-CHAPv2
6 * authentication using RADIUS.
8 * Copyright (C) 2002 Roaring Penguin Software Inc.
10 * Based on a patch for ipppd, which is:
11 * Copyright (C) 1996, Matjaz Godec <gody@elgo.si>
12 * Copyright (C) 1996, Lars Fenneberg <in5y050@public.uni-hamburg.de>
13 * Copyright (C) 1997, Miguel A.L. Paraz <map@iphil.net>
15 * Uses radiusclient library, which is:
16 * Copyright (C) 1995,1996,1997,1998 Lars Fenneberg <lf@elemental.net>
17 * Copyright (C) 2002 Roaring Penguin Software Inc.
19 * MPPE support is by Ralf Hofmann, <ralf.hofmann@elvido.net>, with
20 * modification from Frank Cusack, <frank@google.com>.
22 * This plugin may be distributed according to the terms of the GNU
23 * General Public License, version 2 or (at your option) any later version.
25 ***********************************************************************/
26 static char const RCSID[] =
27 "$Id: radius.c,v 1.32 2008/05/26 09:18:08 paulus Exp $";
30 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <netinet/in.h>
39 #include <pppd/pppd.h>
40 #include <pppd/options.h>
41 #include <pppd/chap.h>
42 #include <pppd/upap.h>
43 #ifdef PPP_WITH_CHAPMS
44 #include <pppd/chap_ms.h>
46 #include <pppd/mppe.h>
49 #include <pppd/crypto.h>
51 #include <pppd/ipcp.h>
53 #include "radiusclient.h"
59 static char *config_file = NULL;
60 static int add_avp(char **);
61 static struct avpopt {
65 static bool portnummap = 0;
67 static option_t Options[] = {
68 { "radius-config-file", o_string, &config_file },
69 { "avpair", o_special, add_avp },
70 { "map-to-ttyname", o_bool, &portnummap,
71 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
72 { "map-to-ifname", o_bool, &portnummap,
73 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
77 static pap_check_hook_fn radius_secret_check;
78 static pap_auth_hook_fn radius_pap_auth;
79 static chap_verify_hook_fn radius_chap_verify;
81 static void radius_ip_up(void *opaque, int arg);
82 static void radius_ip_down(void *opaque, int arg);
83 static void make_username_realm(const char *user);
84 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
85 struct chap_digest_type *digest,
86 unsigned char *challenge,
87 char *message, int message_space);
88 static void radius_choose_ip(u_int32_t *addrp);
89 static int radius_init(char *msg);
90 static int get_client_port(const char *ifname);
91 static int radius_allowed_address(u_int32_t addr);
92 static void radius_acct_interim(void *);
94 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
96 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
100 #define MAXSESSIONID 32
104 #define MAXCLASSLEN 500
107 struct radius_state {
114 char user[MAXNAMELEN];
115 char config_file[MAXPATHLEN];
116 char session_id[MAXSESSIONID + 1];
118 int acct_interim_interval;
119 SERVER *authserver; /* Authentication server to use */
120 SERVER *acctserver; /* Accounting server to use */
122 char class[MAXCLASSLEN];
123 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
126 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
128 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
129 In that case, they override the values in the radiusclient.conf file */
130 void (*radius_pre_auth_hook)(char const *user,
132 SERVER **acctserver) = NULL;
134 static struct radius_state rstate;
136 char pppd_version[] = PPPD_VERSION;
138 /**********************************************************************
139 * %FUNCTION: plugin_init
145 * Initializes RADIUS plugin.
146 ***********************************************************************/
150 pap_check_hook = radius_secret_check;
151 pap_auth_hook = radius_pap_auth;
153 chap_check_hook = radius_secret_check;
154 chap_verify_hook = radius_chap_verify;
156 ip_choose_hook = radius_choose_ip;
157 allowed_address_hook = radius_allowed_address;
159 ppp_add_notify(NF_IP_UP, radius_ip_up, NULL);
160 ppp_add_notify(NF_IP_DOWN, radius_ip_down, NULL);
162 memset(&rstate, 0, sizeof(rstate));
164 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
165 sizeof(rstate.config_file));
167 ppp_add_options(Options);
169 info("RADIUS plugin initialized.");
172 /**********************************************************************
175 * argv -- the <attribute=value> pair to add
179 * Adds an av pair to be passed on to the RADIUS server on each request.
180 ***********************************************************************/
184 struct avpopt *p = malloc(sizeof(struct avpopt));
186 /* Append to a list of vp's for later parsing */
187 p->vpstr = strdup(*argv);
194 /**********************************************************************
195 * %FUNCTION: radius_secret_check
199 * 1 -- we are ALWAYS willing to supply a secret. :-)
201 * Tells pppd that we will try to authenticate the peer, and not to
202 * worry about looking in *-secrets file(s)
203 ***********************************************************************/
205 radius_secret_check(void)
210 /**********************************************************************
211 * %FUNCTION: radius_choose_ip
213 * addrp -- where to store the IP address
217 * If RADIUS server has specified an IP address, it is stored in *addrp.
218 ***********************************************************************/
220 radius_choose_ip(u_int32_t *addrp)
222 if (rstate.choose_ip) {
223 *addrp = rstate.ip_addr;
227 /**********************************************************************
228 * %FUNCTION: radius_pap_auth
230 * user -- user-name of peer
231 * passwd -- password supplied by peer
232 * msgp -- Message which will be sent in PAP response
233 * paddrs -- set to a list of possible peer IP addresses
234 * popts -- set to a list of additional pppd options
236 * 1 if we can authenticate, -1 if we cannot.
238 * Performs PAP authentication using RADIUS
239 ***********************************************************************/
241 radius_pap_auth(char *user,
244 struct wordlist **paddrs,
245 struct wordlist **popts)
247 VALUE_PAIR *send, *received;
250 static char radius_msg[BUF_LEN];
251 const char *remote_number;
257 if (radius_init(radius_msg) < 0) {
261 /* Put user with potentially realm added in rstate.user */
262 make_username_realm(user);
264 if (radius_pre_auth_hook) {
265 radius_pre_auth_hook(rstate.user,
273 /* Hack... the "port" is the ppp interface number. Should really be
275 rstate.client_port = get_client_port(portnummap ? ppp_devnam() : ppp_ifname());
278 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
281 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
283 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
284 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
285 remote_number = ppp_get_remote_number();
286 ipparam = ppp_ipparam();
288 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
291 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
293 /* Add user specified vp's */
295 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
297 if (rstate.authserver) {
298 result = rc_auth_using_server(rstate.authserver,
299 rstate.client_port, send,
300 &received, radius_msg, NULL);
302 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
305 if (result == OK_RC) {
306 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
311 /* free value pairs */
312 rc_avpair_free(received);
313 rc_avpair_free(send);
315 return (result == OK_RC) ? 1 : 0;
318 /**********************************************************************
319 * %FUNCTION: radius_chap_verify
321 * user -- name of the peer
322 * ourname -- name for this machine
323 * id -- the ID byte in the challenge
324 * digest -- points to the structure representing the digest type
325 * challenge -- the challenge string we sent (length in first byte)
326 * response -- the response (hash) the peer sent back (length in 1st byte)
327 * message -- space for a message to be returned to the peer
328 * message_space -- number of bytes available at *message.
330 * 1 if the response is good, 0 if it is bad
332 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
333 ***********************************************************************/
335 radius_chap_verify(char *user, char *ourname, int id,
336 struct chap_digest_type *digest,
337 unsigned char *challenge, unsigned char *response,
338 char *message, int message_space)
340 VALUE_PAIR *send, *received;
342 static char radius_msg[BUF_LEN];
344 int challenge_len, response_len;
345 u_char cpassword[MAX_RESPONSE_LEN + 1];
347 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
348 REQUEST_INFO request_info, *req_info = &request_info;
350 REQUEST_INFO *req_info = NULL;
352 const char *remote_number;
355 challenge_len = *challenge++;
356 response_len = *response++;
360 if (radius_init(radius_msg) < 0) {
361 error("%s", radius_msg);
365 /* return error for types we can't handle */
366 if ((digest->code != CHAP_MD5)
367 #ifdef PPP_WITH_CHAPMS
368 && (digest->code != CHAP_MICROSOFT)
369 && (digest->code != CHAP_MICROSOFT_V2)
372 error("RADIUS: Challenge type %u unsupported", digest->code);
376 /* Put user with potentially realm added in rstate.user */
377 if (!rstate.done_chap_once) {
378 make_username_realm(user);
379 rstate.client_port = get_client_port (portnummap ? ppp_devnam() : ppp_ifname());
380 if (radius_pre_auth_hook) {
381 radius_pre_auth_hook(rstate.user,
387 send = received = NULL;
390 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
393 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
395 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
398 * add the challenge and response fields
400 switch (digest->code) {
402 /* CHAP-Challenge and CHAP-Password */
403 if (response_len != MD5_DIGEST_LENGTH)
406 memcpy(&cpassword[1], response, MD5_DIGEST_LENGTH);
408 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
409 challenge, challenge_len, VENDOR_NONE);
410 rc_avpair_add(&send, PW_CHAP_PASSWORD,
411 cpassword, MD5_DIGEST_LENGTH + 1, VENDOR_NONE);
414 #ifdef PPP_WITH_CHAPMS
417 /* MS-CHAP-Challenge and MS-CHAP-Response */
418 u_char *p = cpassword;
420 if (response_len != MS_CHAP_RESPONSE_LEN)
423 /* The idiots use a different field order in RADIUS than PPP */
424 *p++ = response[MS_CHAP_USENT];
425 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
427 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
428 challenge, challenge_len, VENDOR_MICROSOFT);
429 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
430 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
434 case CHAP_MICROSOFT_V2:
436 /* MS-CHAP-Challenge and MS-CHAP2-Response */
437 u_char *p = cpassword;
439 if (response_len != MS_CHAP2_RESPONSE_LEN)
442 /* The idiots use a different field order in RADIUS than PPP */
443 *p++ = response[MS_CHAP2_FLAGS];
444 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
445 + MS_CHAP2_NTRESP_LEN));
447 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
448 challenge, challenge_len, VENDOR_MICROSOFT);
449 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
450 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
456 remote_number = ppp_get_remote_number();
457 ipparam = ppp_ipparam();
459 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
462 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
464 /* Add user specified vp's */
466 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
469 * make authentication with RADIUS server
472 if (rstate.authserver) {
473 result = rc_auth_using_server(rstate.authserver,
474 rstate.client_port, send,
475 &received, radius_msg, req_info);
477 result = rc_auth(rstate.client_port, send, &received, radius_msg,
481 strlcpy(message, radius_msg, message_space);
483 if (result == OK_RC) {
484 if (!rstate.done_chap_once) {
485 if (radius_setparams(received, radius_msg, req_info, digest,
486 challenge, message, message_space) < 0) {
487 error("%s", radius_msg);
490 rstate.done_chap_once = 1;
495 rc_avpair_free(received);
496 rc_avpair_free (send);
497 return (result == OK_RC);
500 /**********************************************************************
501 * %FUNCTION: make_username_realm
503 * user -- the user given to pppd
507 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
508 * then the default realm from the radiusclient config file is added.
509 ***********************************************************************/
511 make_username_realm(const char *user)
515 if ( user != NULL ) {
516 strlcpy(rstate.user, user, sizeof(rstate.user));
521 default_realm = rc_conf_str("default_realm");
523 if (!strchr(rstate.user, '@') &&
525 (*default_realm != '\0')) {
526 strlcat(rstate.user, "@", sizeof(rstate.user));
527 strlcat(rstate.user, default_realm, sizeof(rstate.user));
531 /**********************************************************************
532 * %FUNCTION: radius_setparams
534 * vp -- received value-pairs
535 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
537 * >= 0 on success; -1 on failure
539 * Parses attributes sent by RADIUS server and sets them in pppd.
540 ***********************************************************************/
542 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
543 struct chap_digest_type *digest, unsigned char *challenge,
544 char *message, int message_space)
547 int ms_chap2_success = 0;
549 int mppe_enc_keys = 0; /* whether or not these were received */
550 int mppe_enc_policy = 0;
551 int mppe_enc_types = 0;
554 ipcp_options *wo = &ipcp_wantoptions[0];
555 ipcp_options *ao = &ipcp_allowoptions[0];
562 /* Send RADIUS attributes to anyone else who might be interested */
563 if (radius_attributes_hook) {
564 (*radius_attributes_hook)(vp);
568 * service type (if not framed then quit),
569 * new IP address (RADIUS can define static IP for some users),
573 if (vp->vendorcode == VENDOR_NONE) {
574 switch (vp->attribute) {
575 case PW_SERVICE_TYPE:
576 /* check for service type */
577 /* if not FRAMED then exit */
578 if (vp->lvalue != PW_FRAMED) {
579 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
580 vp->lvalue, rstate.user);
585 case PW_FRAMED_PROTOCOL:
586 /* check for framed protocol type */
587 /* if not PPP then also exit */
588 if (vp->lvalue != PW_PPP) {
589 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
590 vp->lvalue, rstate.user);
595 case PW_SESSION_TIMEOUT:
596 /* Session timeout */
597 ppp_set_max_connect_time(vp->lvalue);
600 /* packet filter, will be handled via ip-(up|down) script */
601 ppp_script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
603 case PW_FRAMED_ROUTE:
604 /* route, will be handled via ip-(up|down) script */
605 ppp_script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
607 case PW_IDLE_TIMEOUT:
609 ppp_set_max_idle_time(vp->lvalue);
611 case PW_SESSION_OCTETS_LIMIT:
612 /* Session traffic limit */
613 ppp_set_session_limit(vp->lvalue);
615 case PW_OCTETS_DIRECTION:
616 /* Session traffic limit direction check */
617 ppp_set_session_limit_dir(vp->lvalue);
619 case PW_ACCT_INTERIM_INTERVAL:
620 /* Send accounting updates every few seconds */
621 rstate.acct_interim_interval = vp->lvalue;
622 /* RFC says it MUST NOT be less than 60 seconds */
623 /* We use "0" to signify not sending updates */
624 if (rstate.acct_interim_interval &&
625 rstate.acct_interim_interval < 60) {
626 rstate.acct_interim_interval = 60;
629 case PW_FRAMED_IP_ADDRESS:
630 /* seting up remote IP addresses */
632 if (remote == 0xffffffff) {
633 /* 0xffffffff means user should be allowed to select one */
634 rstate.any_ip_addr_ok = 1;
635 } else if (remote != 0xfffffffe) {
636 /* 0xfffffffe means NAS should select an ip address */
637 remote = htonl(vp->lvalue);
638 if (ppp_bad_ip_addr (remote)) {
639 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
640 remote, rstate.user);
643 rstate.choose_ip = 1;
644 rstate.ip_addr = remote;
647 case PW_NAS_IP_ADDRESS:
648 wo->ouraddr = htonl(vp->lvalue);
651 /* Save Class attribute to pass it in accounting request */
652 // if (vp->lvalue <= MAXCLASSLEN) { // <- Attribute could be this big, but vp->strvalue is limited to AUTH_STRING_LEN characters
653 if (vp->lvalue <= AUTH_STRING_LEN) {
654 rstate.class_len=vp->lvalue;
655 memcpy(rstate.class, vp->strvalue, rstate.class_len);
656 } /* else too big for our buffer - ignore it */
659 ppp_set_mtu(rstate.client_port,MIN(ppp_get_mtu(rstate.client_port),vp->lvalue));
664 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
665 #ifdef PPP_WITH_CHAPMS
666 switch (vp->attribute) {
667 case PW_MS_CHAP2_SUCCESS:
668 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
669 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
673 strlcpy(message, (char*) vp->strvalue + 1, message_space);
674 ms_chap2_success = 1;
678 case PW_MS_CHAP_MPPE_KEYS:
679 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
680 slprintf(msg, BUF_LEN,
681 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
687 case PW_MS_MPPE_SEND_KEY:
688 case PW_MS_MPPE_RECV_KEY:
689 if (radius_setmppekeys2(vp, req_info) < 0) {
690 slprintf(msg, BUF_LEN,
691 "RADIUS: bad MS-MPPE-%s-Key attribute",
692 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
699 case PW_MS_MPPE_ENCRYPTION_POLICY:
700 mppe_enc_policy = vp->lvalue; /* save for later */
703 case PW_MS_MPPE_ENCRYPTION_TYPES:
704 mppe_enc_types = vp->lvalue; /* save for later */
707 #endif /* PPP_WITH_MPPE */
709 case PW_MS_PRIMARY_DNS_SERVER:
710 ao->dnsaddr[0] = htonl(vp->lvalue);
713 ao->dnsaddr[1] = ao->dnsaddr[0];
715 case PW_MS_SECONDARY_DNS_SERVER:
716 ao->dnsaddr[1] = htonl(vp->lvalue);
719 ao->dnsaddr[0] = ao->dnsaddr[1];
721 case PW_MS_PRIMARY_NBNS_SERVER:
722 ao->winsaddr[0] = htonl(vp->lvalue);
725 ao->winsaddr[1] = ao->winsaddr[0];
727 case PW_MS_SECONDARY_NBNS_SERVER:
728 ao->winsaddr[1] = htonl(vp->lvalue);
731 ao->winsaddr[0] = ao->winsaddr[1];
735 #endif /* PPP_WITH_CHAPMS */
740 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
741 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
746 * Require both policy and key attributes to indicate a valid key.
747 * Note that if the policy value was '0' we don't set the key!
749 if (mppe_enc_policy && mppe_enc_keys) {
750 /* Set/modify allowed encryption types. */
752 mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
762 /**********************************************************************
763 * %FUNCTION: radius_setmppekeys
765 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
766 * req_info -- radius request information used for encryption
768 * >= 0 on success; -1 on failure
770 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
772 ***********************************************************************/
774 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
775 unsigned char *challenge)
780 unsigned char plain[32];
781 unsigned char buf[MD5_DIGEST_LENGTH];
785 if (vp->lvalue != 32) {
786 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
791 memcpy(plain, vp->strvalue, sizeof(plain));
793 ctx = PPP_MD_CTX_new();
796 if (PPP_DigestInit(ctx, PPP_md5())) {
798 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
800 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
802 buflen = sizeof(buf);
803 if (PPP_DigestFinal(ctx, buf, &buflen)) {
810 PPP_MD_CTX_free(ctx);
815 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
820 ctx = PPP_MD_CTX_new();
823 if (PPP_DigestInit(ctx, PPP_md5())) {
825 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
827 if (PPP_DigestUpdate(ctx, vp->strvalue, 16)) {
829 buflen = MD5_DIGEST_LENGTH;
830 if (PPP_DigestFinal(ctx, buf, &buflen)) {
837 PPP_MD_CTX_free(ctx);
842 for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
843 plain[i + 16] ^= buf[i];
847 * Annoying. The "key" returned is just the NTPasswordHashHash, which
848 * the NAS (us) doesn't need; we only need the start key. So we have
849 * to generate the start key, sigh. NB: We do not support the LM-Key.
851 mppe_set_chapv1(challenge, &plain[8]);
859 /**********************************************************************
860 * %FUNCTION: radius_setmppekeys2
862 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
863 * req_info -- radius request information used for encryption
865 * >= 0 on success; -1 on failure
867 * Decrypt the key provided by the RADIUS server for MPPE encryption.
869 ***********************************************************************/
871 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
876 unsigned char *salt = vp->strvalue;
877 unsigned char *crypt = vp->strvalue + 2;
878 unsigned char plain[32];
879 unsigned char buf[MD5_DIGEST_LENGTH];
883 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
886 if (vp->lvalue != 34) {
887 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
892 if ((salt[0] & 0x80) == 0) {
893 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
897 memcpy(plain, crypt, 32);
899 ctx = PPP_MD_CTX_new();
901 error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key attribute", type);
905 buflen = sizeof(buf);
906 if (!PPP_DigestInit(ctx, PPP_md5())) {
907 error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key attribute", type);
908 } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
909 error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key attribute", type);
910 } else if (!PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
911 error("RADIUS: Error mixing in request vector for MS-MPPE-%s-Key attribute", type);
912 } else if (!PPP_DigestUpdate(ctx, salt, 2)) {
913 error("RADIUS: Error mixing in salt for MS-MPPE-%s-Key attribute", type);
914 } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
915 error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key attribute", type);
920 PPP_MD_CTX_free(ctx);
925 for (i = 0; i < 16; i++) {
929 if (plain[0] != 16) {
930 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
931 (int) plain[0], type);
936 ctx = PPP_MD_CTX_new();
938 error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key(2) attribute", type);
942 buflen = sizeof(buf);
944 if (!PPP_DigestInit(ctx, PPP_md5())) {
945 error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key(2) attribute", type);
946 } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
947 error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key(2) attribute", type);
948 } else if (!PPP_DigestUpdate(ctx, crypt, 16)) {
949 error("RADIUS: Error mixing in crypt vector for MS-MPPE-%s-Key(2) attribute", type);
950 } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
951 error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key(2) attribute", type);
956 PPP_MD_CTX_free(ctx);
961 plain[16] ^= buf[0]; /* only need the first byte */
963 if (vp->attribute == PW_MS_MPPE_SEND_KEY) {
964 mppe_set_keys(plain + 1, NULL, 16);
966 mppe_set_keys(NULL, plain + 1, 16);
970 #endif /* PPP_WITH_MPPE */
972 /**********************************************************************
973 * %FUNCTION: radius_acct_start
979 * Sends a "start" accounting message to the RADIUS server.
980 ***********************************************************************/
982 radius_acct_start(void)
986 VALUE_PAIR *send = NULL;
987 ipcp_options *ho = &ipcp_hisoptions[0];
989 const char *remote_number;
992 if (!rstate.initialized) {
996 rstate.start_time = time(NULL);
998 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
1000 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
1001 rstate.session_id, 0, VENDOR_NONE);
1002 rc_avpair_add(&send, PW_USER_NAME,
1003 rstate.user, 0, VENDOR_NONE);
1005 if (rstate.class_len > 0)
1006 rc_avpair_add(&send, PW_CLASS,
1007 rstate.class, rstate.class_len, VENDOR_NONE);
1009 av_type = PW_STATUS_START;
1010 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1012 av_type = PW_FRAMED;
1013 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1016 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1018 remote_number = ppp_get_remote_number();
1019 ipparam = ppp_ipparam();
1020 if (remote_number) {
1021 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1022 remote_number, 0, VENDOR_NONE);
1024 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1026 av_type = PW_RADIUS;
1027 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1030 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1031 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1033 hisaddr = ho->hisaddr;
1034 av_type = htonl(hisaddr);
1035 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1037 /* Add user specified vp's */
1039 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1041 if (rstate.acctserver) {
1042 result = rc_acct_using_server(rstate.acctserver,
1043 rstate.client_port, send);
1045 result = rc_acct(rstate.client_port, send);
1048 rc_avpair_free(send);
1050 if (result != OK_RC) {
1051 /* RADIUS server could be down so make this a warning */
1053 "Accounting START failed for %s", rstate.user);
1056 /* Kick off periodic accounting reports */
1057 if (rstate.acct_interim_interval) {
1058 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1062 /**********************************************************************
1063 * %FUNCTION: radius_acct_stop
1069 * Sends a "stop" accounting message to the RADIUS server.
1070 ***********************************************************************/
1072 radius_acct_stop(void)
1075 VALUE_PAIR *send = NULL;
1076 ipcp_options *ho = &ipcp_hisoptions[0];
1079 const char *remote_number;
1080 const char *ipparam;
1081 ppp_link_stats_st stats;
1083 if (!rstate.initialized) {
1087 if (rstate.acct_interim_interval)
1088 ppp_untimeout(radius_acct_interim, NULL);
1090 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1093 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1095 if (rstate.class_len > 0)
1096 rc_avpair_add(&send, PW_CLASS,
1097 rstate.class, rstate.class_len, VENDOR_NONE);
1099 av_type = PW_STATUS_STOP;
1100 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1102 av_type = PW_FRAMED;
1103 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1106 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1108 av_type = PW_RADIUS;
1109 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1111 if (ppp_get_link_stats(&stats)) {
1113 av_type = ppp_get_link_uptime();
1114 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1116 av_type = stats.bytes_out & 0xFFFFFFFF;
1117 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1119 if (stats.bytes_out > 0xFFFFFFFF) {
1120 av_type = stats.bytes_out >> 32;
1121 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1124 av_type = stats.bytes_in & 0xFFFFFFFF;
1125 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1127 if (stats.bytes_in > 0xFFFFFFFF) {
1128 av_type = stats.bytes_in >> 32;
1129 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1132 av_type = stats.pkts_out;
1133 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1135 av_type = stats.pkts_in;
1136 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1139 remote_number = ppp_get_remote_number();
1140 ipparam = ppp_ipparam();
1141 if (remote_number) {
1142 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1143 remote_number, 0, VENDOR_NONE);
1145 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1147 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1148 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1150 av_type = PW_NAS_ERROR;
1151 switch( ppp_status() ) {
1153 av_type = PW_USER_REQUEST;
1156 case EXIT_USER_REQUEST:
1157 av_type = PW_ADMIN_RESET;
1161 case EXIT_PEER_DEAD:
1162 case EXIT_CONNECT_FAILED:
1163 av_type = PW_LOST_CARRIER;
1166 case EXIT_INIT_FAILED:
1167 case EXIT_OPEN_FAILED:
1168 case EXIT_LOCK_FAILED:
1169 case EXIT_PTYCMD_FAILED:
1170 av_type = PW_PORT_ERROR;
1173 case EXIT_PEER_AUTH_FAILED:
1174 case EXIT_AUTH_TOPEER_FAILED:
1175 case EXIT_NEGOTIATION_FAILED:
1176 case EXIT_CNID_AUTH_FAILED:
1177 av_type = PW_SERVICE_UNAVAILABLE;
1180 case EXIT_IDLE_TIMEOUT:
1181 av_type = PW_ACCT_IDLE_TIMEOUT;
1185 av_type = PW_CALLBACK;
1188 case EXIT_CONNECT_TIME:
1189 av_type = PW_ACCT_SESSION_TIMEOUT;
1192 case EXIT_TRAFFIC_LIMIT:
1193 av_type = PW_NAS_REQUEST;
1197 av_type = PW_NAS_ERROR;
1200 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1202 hisaddr = ho->hisaddr;
1203 av_type = htonl(hisaddr);
1204 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1206 /* Add user specified vp's */
1208 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1210 if (rstate.acctserver) {
1211 result = rc_acct_using_server(rstate.acctserver,
1212 rstate.client_port, send);
1214 result = rc_acct(rstate.client_port, send);
1217 if (result != OK_RC) {
1218 /* RADIUS server could be down so make this a warning */
1220 "Accounting STOP failed for %s", rstate.user);
1222 rc_avpair_free(send);
1225 /**********************************************************************
1226 * %FUNCTION: radius_acct_interim
1232 * Sends an interim accounting message to the RADIUS server
1233 ***********************************************************************/
1235 radius_acct_interim(void *ignored)
1238 VALUE_PAIR *send = NULL;
1239 ipcp_options *ho = &ipcp_hisoptions[0];
1242 const char *remote_number;
1243 const char *ipparam;
1244 ppp_link_stats_st stats;
1246 if (!rstate.initialized) {
1250 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1253 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1255 if (rstate.class_len > 0)
1256 rc_avpair_add(&send, PW_CLASS,
1257 rstate.class, rstate.class_len, VENDOR_NONE);
1259 av_type = PW_STATUS_ALIVE;
1260 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1262 av_type = PW_FRAMED;
1263 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1266 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1268 av_type = PW_RADIUS;
1269 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1271 if (ppp_get_link_stats(&stats)) {
1273 av_type = ppp_get_link_uptime();
1274 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1276 av_type = stats.bytes_out & 0xFFFFFFFF;
1277 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1279 if (stats.bytes_out > 0xFFFFFFFF) {
1280 av_type = stats.bytes_out >> 32;
1281 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1284 av_type = stats.bytes_in & 0xFFFFFFFF;
1285 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1287 if (stats.bytes_in > 0xFFFFFFFF) {
1288 av_type = stats.bytes_in >> 32;
1289 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1292 av_type = stats.pkts_out;
1293 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1295 av_type = stats.pkts_in;
1296 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1299 remote_number = ppp_get_remote_number();
1300 ipparam = ppp_ipparam();
1301 if (remote_number) {
1302 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1303 remote_number, 0, VENDOR_NONE);
1305 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1307 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1308 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1310 hisaddr = ho->hisaddr;
1311 av_type = htonl(hisaddr);
1312 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1314 /* Add user specified vp's */
1316 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1318 if (rstate.acctserver) {
1319 result = rc_acct_using_server(rstate.acctserver,
1320 rstate.client_port, send);
1322 result = rc_acct(rstate.client_port, send);
1325 if (result != OK_RC) {
1326 /* RADIUS server could be down so make this a warning */
1328 "Interim accounting failed for %s", rstate.user);
1330 rc_avpair_free(send);
1332 /* Schedule another one */
1333 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1336 /**********************************************************************
1337 * %FUNCTION: radius_ip_up
1344 * Called when IPCP is up. We'll do a start-accounting record.
1345 ***********************************************************************/
1347 radius_ip_up(void *opaque, int arg)
1349 radius_acct_start();
1352 /**********************************************************************
1353 * %FUNCTION: radius_ip_down
1360 * Called when IPCP is down. We'll do a stop-accounting record.
1361 ***********************************************************************/
1363 radius_ip_down(void *opaque, int arg)
1368 /**********************************************************************
1369 * %FUNCTION: radius_init
1371 * msg -- buffer of size BUF_LEN for error message
1373 * negative on failure; non-negative on success
1375 * Initializes radiusclient library
1376 ***********************************************************************/
1378 radius_init(char *msg)
1380 if (rstate.initialized) {
1384 if (config_file && *config_file) {
1385 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1388 rstate.initialized = 1;
1390 if (rc_read_config(rstate.config_file) != 0) {
1391 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1392 rstate.config_file);
1396 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1397 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1398 rc_conf_str("dictionary"));
1402 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1403 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1404 rc_conf_str("mapfile"));
1408 /* Add av pairs saved during option parsing */
1410 struct avpopt *n = avpopt->next;
1412 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1413 free(avpopt->vpstr);
1420 /**********************************************************************
1421 * %FUNCTION: get_client_port
1423 * ifname -- PPP interface name (e.g. "ppp7")
1425 * The NAS port number (e.g. 7)
1427 * Extracts the port number from the interface name
1428 ***********************************************************************/
1430 get_client_port(const char *ifname)
1433 if (sscanf(ifname, "ppp%d", &port) == 1) {
1436 return rc_map2id(ifname);
1439 /**********************************************************************
1440 * %FUNCTION: radius_allowed_address
1442 * addr -- IP address
1444 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1446 ***********************************************************************/
1448 radius_allowed_address(u_int32_t addr)
1450 ipcp_options *wo = &ipcp_wantoptions[0];
1452 if (!rstate.choose_ip) {
1453 /* If RADIUS server said any address is OK, then fine... */
1454 if (rstate.any_ip_addr_ok) {
1458 /* Sigh... if an address was supplied for remote host in pppd
1459 options, it has to match that. */
1460 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1466 if (addr == rstate.ip_addr) return 1;
1470 /* Useful for other plugins */
1471 char *radius_logged_in_user(void)