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();
902 if (PPP_DigestInit(ctx, PPP_md5())) {
904 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
906 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
908 if (PPP_DigestUpdate(ctx, salt, 2)) {
910 buflen = sizeof(buf);
911 if (PPP_DigestFinal(ctx, buf, &buflen)) {
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();
939 if (PPP_DigestInit(ctx, PPP_md5())) {
941 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
943 if (PPP_DigestUpdate(ctx, crypt, 16)) {
945 if (PPP_DigestUpdate(ctx, salt, 2)) {
947 buflen = sizeof(buf);
948 if (PPP_DigestFinal(ctx, buf, &buflen)) {
957 PPP_MD_CTX_free(ctx);
962 plain[16] ^= buf[0]; /* only need the first byte */
964 if (vp->attribute == PW_MS_MPPE_SEND_KEY) {
965 mppe_set_keys(plain + 1, NULL, 16);
967 mppe_set_keys(NULL, plain + 1, 16);
975 #endif /* PPP_WITH_MPPE */
977 /**********************************************************************
978 * %FUNCTION: radius_acct_start
984 * Sends a "start" accounting message to the RADIUS server.
985 ***********************************************************************/
987 radius_acct_start(void)
991 VALUE_PAIR *send = NULL;
992 ipcp_options *ho = &ipcp_hisoptions[0];
994 const char *remote_number;
997 if (!rstate.initialized) {
1001 rstate.start_time = time(NULL);
1003 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
1005 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
1006 rstate.session_id, 0, VENDOR_NONE);
1007 rc_avpair_add(&send, PW_USER_NAME,
1008 rstate.user, 0, VENDOR_NONE);
1010 if (rstate.class_len > 0)
1011 rc_avpair_add(&send, PW_CLASS,
1012 rstate.class, rstate.class_len, VENDOR_NONE);
1014 av_type = PW_STATUS_START;
1015 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1017 av_type = PW_FRAMED;
1018 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1021 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1023 remote_number = ppp_get_remote_number();
1024 ipparam = ppp_ipparam();
1025 if (remote_number) {
1026 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1027 remote_number, 0, VENDOR_NONE);
1029 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1031 av_type = PW_RADIUS;
1032 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1035 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1036 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1038 hisaddr = ho->hisaddr;
1039 av_type = htonl(hisaddr);
1040 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1042 /* Add user specified vp's */
1044 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1046 if (rstate.acctserver) {
1047 result = rc_acct_using_server(rstate.acctserver,
1048 rstate.client_port, send);
1050 result = rc_acct(rstate.client_port, send);
1053 rc_avpair_free(send);
1055 if (result != OK_RC) {
1056 /* RADIUS server could be down so make this a warning */
1058 "Accounting START failed for %s", rstate.user);
1061 /* Kick off periodic accounting reports */
1062 if (rstate.acct_interim_interval) {
1063 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1067 /**********************************************************************
1068 * %FUNCTION: radius_acct_stop
1074 * Sends a "stop" accounting message to the RADIUS server.
1075 ***********************************************************************/
1077 radius_acct_stop(void)
1080 VALUE_PAIR *send = NULL;
1081 ipcp_options *ho = &ipcp_hisoptions[0];
1084 const char *remote_number;
1085 const char *ipparam;
1086 ppp_link_stats_st stats;
1088 if (!rstate.initialized) {
1092 if (rstate.acct_interim_interval)
1093 ppp_untimeout(radius_acct_interim, NULL);
1095 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1098 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1100 if (rstate.class_len > 0)
1101 rc_avpair_add(&send, PW_CLASS,
1102 rstate.class, rstate.class_len, VENDOR_NONE);
1104 av_type = PW_STATUS_STOP;
1105 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1107 av_type = PW_FRAMED;
1108 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1111 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1113 av_type = PW_RADIUS;
1114 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1116 if (ppp_get_link_stats(&stats)) {
1118 av_type = ppp_get_link_uptime();
1119 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1121 av_type = stats.bytes_out & 0xFFFFFFFF;
1122 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1124 if (stats.bytes_out > 0xFFFFFFFF) {
1125 av_type = stats.bytes_out >> 32;
1126 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1129 av_type = stats.bytes_in & 0xFFFFFFFF;
1130 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1132 if (stats.bytes_in > 0xFFFFFFFF) {
1133 av_type = stats.bytes_in >> 32;
1134 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1137 av_type = stats.pkts_out;
1138 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1140 av_type = stats.pkts_in;
1141 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1144 remote_number = ppp_get_remote_number();
1145 ipparam = ppp_ipparam();
1146 if (remote_number) {
1147 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1148 remote_number, 0, VENDOR_NONE);
1150 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1152 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1153 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1155 av_type = PW_NAS_ERROR;
1156 switch( ppp_status() ) {
1158 av_type = PW_USER_REQUEST;
1161 case EXIT_USER_REQUEST:
1162 av_type = PW_ADMIN_RESET;
1166 case EXIT_PEER_DEAD:
1167 case EXIT_CONNECT_FAILED:
1168 av_type = PW_LOST_CARRIER;
1171 case EXIT_INIT_FAILED:
1172 case EXIT_OPEN_FAILED:
1173 case EXIT_LOCK_FAILED:
1174 case EXIT_PTYCMD_FAILED:
1175 av_type = PW_PORT_ERROR;
1178 case EXIT_PEER_AUTH_FAILED:
1179 case EXIT_AUTH_TOPEER_FAILED:
1180 case EXIT_NEGOTIATION_FAILED:
1181 case EXIT_CNID_AUTH_FAILED:
1182 av_type = PW_SERVICE_UNAVAILABLE;
1185 case EXIT_IDLE_TIMEOUT:
1186 av_type = PW_ACCT_IDLE_TIMEOUT;
1190 av_type = PW_CALLBACK;
1193 case EXIT_CONNECT_TIME:
1194 av_type = PW_ACCT_SESSION_TIMEOUT;
1197 case EXIT_TRAFFIC_LIMIT:
1198 av_type = PW_NAS_REQUEST;
1202 av_type = PW_NAS_ERROR;
1205 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1207 hisaddr = ho->hisaddr;
1208 av_type = htonl(hisaddr);
1209 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1211 /* Add user specified vp's */
1213 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1215 if (rstate.acctserver) {
1216 result = rc_acct_using_server(rstate.acctserver,
1217 rstate.client_port, send);
1219 result = rc_acct(rstate.client_port, send);
1222 if (result != OK_RC) {
1223 /* RADIUS server could be down so make this a warning */
1225 "Accounting STOP failed for %s", rstate.user);
1227 rc_avpair_free(send);
1230 /**********************************************************************
1231 * %FUNCTION: radius_acct_interim
1237 * Sends an interim accounting message to the RADIUS server
1238 ***********************************************************************/
1240 radius_acct_interim(void *ignored)
1243 VALUE_PAIR *send = NULL;
1244 ipcp_options *ho = &ipcp_hisoptions[0];
1247 const char *remote_number;
1248 const char *ipparam;
1249 ppp_link_stats_st stats;
1251 if (!rstate.initialized) {
1255 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1258 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1260 if (rstate.class_len > 0)
1261 rc_avpair_add(&send, PW_CLASS,
1262 rstate.class, rstate.class_len, VENDOR_NONE);
1264 av_type = PW_STATUS_ALIVE;
1265 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1267 av_type = PW_FRAMED;
1268 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1271 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1273 av_type = PW_RADIUS;
1274 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1276 if (ppp_get_link_stats(&stats)) {
1278 av_type = ppp_get_link_uptime();
1279 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1281 av_type = stats.bytes_out & 0xFFFFFFFF;
1282 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1284 if (stats.bytes_out > 0xFFFFFFFF) {
1285 av_type = stats.bytes_out >> 32;
1286 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1289 av_type = stats.bytes_in & 0xFFFFFFFF;
1290 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1292 if (stats.bytes_in > 0xFFFFFFFF) {
1293 av_type = stats.bytes_in >> 32;
1294 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1297 av_type = stats.pkts_out;
1298 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1300 av_type = stats.pkts_in;
1301 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1304 remote_number = ppp_get_remote_number();
1305 ipparam = ppp_ipparam();
1306 if (remote_number) {
1307 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1308 remote_number, 0, VENDOR_NONE);
1310 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1312 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1313 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1315 hisaddr = ho->hisaddr;
1316 av_type = htonl(hisaddr);
1317 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1319 /* Add user specified vp's */
1321 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1323 if (rstate.acctserver) {
1324 result = rc_acct_using_server(rstate.acctserver,
1325 rstate.client_port, send);
1327 result = rc_acct(rstate.client_port, send);
1330 if (result != OK_RC) {
1331 /* RADIUS server could be down so make this a warning */
1333 "Interim accounting failed for %s", rstate.user);
1335 rc_avpair_free(send);
1337 /* Schedule another one */
1338 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1341 /**********************************************************************
1342 * %FUNCTION: radius_ip_up
1349 * Called when IPCP is up. We'll do a start-accounting record.
1350 ***********************************************************************/
1352 radius_ip_up(void *opaque, int arg)
1354 radius_acct_start();
1357 /**********************************************************************
1358 * %FUNCTION: radius_ip_down
1365 * Called when IPCP is down. We'll do a stop-accounting record.
1366 ***********************************************************************/
1368 radius_ip_down(void *opaque, int arg)
1373 /**********************************************************************
1374 * %FUNCTION: radius_init
1376 * msg -- buffer of size BUF_LEN for error message
1378 * negative on failure; non-negative on success
1380 * Initializes radiusclient library
1381 ***********************************************************************/
1383 radius_init(char *msg)
1385 if (rstate.initialized) {
1389 if (config_file && *config_file) {
1390 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1393 rstate.initialized = 1;
1395 if (rc_read_config(rstate.config_file) != 0) {
1396 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1397 rstate.config_file);
1401 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1402 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1403 rc_conf_str("dictionary"));
1407 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1408 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1409 rc_conf_str("mapfile"));
1413 /* Add av pairs saved during option parsing */
1415 struct avpopt *n = avpopt->next;
1417 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1418 free(avpopt->vpstr);
1425 /**********************************************************************
1426 * %FUNCTION: get_client_port
1428 * ifname -- PPP interface name (e.g. "ppp7")
1430 * The NAS port number (e.g. 7)
1432 * Extracts the port number from the interface name
1433 ***********************************************************************/
1435 get_client_port(const char *ifname)
1438 if (sscanf(ifname, "ppp%d", &port) == 1) {
1441 return rc_map2id(ifname);
1444 /**********************************************************************
1445 * %FUNCTION: radius_allowed_address
1447 * addr -- IP address
1449 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1451 ***********************************************************************/
1453 radius_allowed_address(u_int32_t addr)
1455 ipcp_options *wo = &ipcp_wantoptions[0];
1457 if (!rstate.choose_ip) {
1458 /* If RADIUS server said any address is OK, then fine... */
1459 if (rstate.any_ip_addr_ok) {
1463 /* Sigh... if an address was supplied for remote host in pppd
1464 options, it has to match that. */
1465 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1471 if (addr == rstate.ip_addr) return 1;
1475 /* Useful for other plugins */
1476 char *radius_logged_in_user(void)