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 ***********************************************************************/
28 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
37 #include <pppd/pppd.h>
38 #include <pppd/options.h>
39 #include <pppd/chap.h>
40 #include <pppd/upap.h>
41 #ifdef PPP_WITH_CHAPMS
42 #include <pppd/chap_ms.h>
44 #include <pppd/mppe.h>
47 #include <pppd/crypto.h>
49 #include <pppd/ipcp.h>
51 #include "radiusclient.h"
57 static char *config_file = NULL;
58 static int add_avp(char **);
59 static struct avpopt {
63 static bool portnummap = 0;
65 static option_t Options[] = {
66 { "radius-config-file", o_string, &config_file },
67 { "avpair", o_special, add_avp },
68 { "map-to-ttyname", o_bool, &portnummap,
69 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
70 { "map-to-ifname", o_bool, &portnummap,
71 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
75 static pap_check_hook_fn radius_secret_check;
76 static pap_auth_hook_fn radius_pap_auth;
77 static chap_verify_hook_fn radius_chap_verify;
79 static void radius_ip_up(void *opaque, int arg);
80 static void radius_ip_down(void *opaque, int arg);
81 static void make_username_realm(const char *user);
82 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
83 struct chap_digest_type *digest,
84 unsigned char *challenge,
85 char *message, int message_space);
86 static void radius_choose_ip(u_int32_t *addrp);
87 static int radius_init(char *msg);
88 static int get_client_port(const char *ifname);
89 static int radius_allowed_address(u_int32_t addr);
90 static void radius_acct_interim(void *);
92 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
94 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
98 #define MAXSESSIONID 32
102 #define MAXCLASSLEN 500
105 struct radius_state {
112 char user[MAXNAMELEN];
113 char config_file[MAXPATHLEN];
114 char session_id[MAXSESSIONID + 1];
116 int acct_interim_interval;
117 SERVER *authserver; /* Authentication server to use */
118 SERVER *acctserver; /* Accounting server to use */
120 char class[MAXCLASSLEN];
121 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
124 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
126 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
127 In that case, they override the values in the radiusclient.conf file */
128 void (*radius_pre_auth_hook)(char const *user,
130 SERVER **acctserver) = NULL;
132 static struct radius_state rstate;
134 char pppd_version[] = PPPD_VERSION;
136 /**********************************************************************
137 * %FUNCTION: plugin_init
143 * Initializes RADIUS plugin.
144 ***********************************************************************/
148 pap_check_hook = radius_secret_check;
149 pap_auth_hook = radius_pap_auth;
151 chap_check_hook = radius_secret_check;
152 chap_verify_hook = radius_chap_verify;
154 ip_choose_hook = radius_choose_ip;
155 allowed_address_hook = radius_allowed_address;
157 ppp_add_notify(NF_IP_UP, radius_ip_up, NULL);
158 ppp_add_notify(NF_IP_DOWN, radius_ip_down, NULL);
160 memset(&rstate, 0, sizeof(rstate));
162 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
163 sizeof(rstate.config_file));
165 ppp_add_options(Options);
167 info("RADIUS plugin initialized.");
170 /**********************************************************************
173 * argv -- the <attribute=value> pair to add
177 * Adds an av pair to be passed on to the RADIUS server on each request.
178 ***********************************************************************/
182 struct avpopt *p = malloc(sizeof(struct avpopt));
184 /* Append to a list of vp's for later parsing */
185 p->vpstr = strdup(*argv);
192 /**********************************************************************
193 * %FUNCTION: radius_secret_check
197 * 1 -- we are ALWAYS willing to supply a secret. :-)
199 * Tells pppd that we will try to authenticate the peer, and not to
200 * worry about looking in *-secrets file(s)
201 ***********************************************************************/
203 radius_secret_check(void)
208 /**********************************************************************
209 * %FUNCTION: radius_choose_ip
211 * addrp -- where to store the IP address
215 * If RADIUS server has specified an IP address, it is stored in *addrp.
216 ***********************************************************************/
218 radius_choose_ip(u_int32_t *addrp)
220 if (rstate.choose_ip) {
221 *addrp = rstate.ip_addr;
225 /**********************************************************************
226 * %FUNCTION: radius_pap_auth
228 * user -- user-name of peer
229 * passwd -- password supplied by peer
230 * msgp -- Message which will be sent in PAP response
231 * paddrs -- set to a list of possible peer IP addresses
232 * popts -- set to a list of additional pppd options
234 * 1 if we can authenticate, -1 if we cannot.
236 * Performs PAP authentication using RADIUS
237 ***********************************************************************/
239 radius_pap_auth(char *user,
242 struct wordlist **paddrs,
243 struct wordlist **popts)
245 VALUE_PAIR *send, *received;
248 static char radius_msg[BUF_LEN];
249 const char *remote_number;
255 if (radius_init(radius_msg) < 0) {
259 /* Put user with potentially realm added in rstate.user */
260 make_username_realm(user);
262 if (radius_pre_auth_hook) {
263 radius_pre_auth_hook(rstate.user,
271 /* Hack... the "port" is the ppp interface number. Should really be
273 rstate.client_port = get_client_port(portnummap ? ppp_devnam() : ppp_ifname());
276 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
279 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
281 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
282 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
283 remote_number = ppp_get_remote_number();
284 ipparam = ppp_ipparam();
286 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
289 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
291 /* Add user specified vp's */
293 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
295 if (rstate.authserver) {
296 result = rc_auth_using_server(rstate.authserver,
297 rstate.client_port, send,
298 &received, radius_msg, NULL);
300 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
303 if (result == OK_RC) {
304 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
309 /* free value pairs */
310 rc_avpair_free(received);
311 rc_avpair_free(send);
313 return (result == OK_RC) ? 1 : 0;
316 /**********************************************************************
317 * %FUNCTION: radius_chap_verify
319 * user -- name of the peer
320 * ourname -- name for this machine
321 * id -- the ID byte in the challenge
322 * digest -- points to the structure representing the digest type
323 * challenge -- the challenge string we sent (length in first byte)
324 * response -- the response (hash) the peer sent back (length in 1st byte)
325 * message -- space for a message to be returned to the peer
326 * message_space -- number of bytes available at *message.
328 * 1 if the response is good, 0 if it is bad
330 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
331 ***********************************************************************/
333 radius_chap_verify(char *user, char *ourname, int id,
334 struct chap_digest_type *digest,
335 unsigned char *challenge, unsigned char *response,
336 char *message, int message_space)
338 VALUE_PAIR *send, *received;
340 static char radius_msg[BUF_LEN];
342 int challenge_len, response_len;
343 u_char cpassword[MAX_RESPONSE_LEN + 1];
345 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
346 REQUEST_INFO request_info, *req_info = &request_info;
348 REQUEST_INFO *req_info = NULL;
350 const char *remote_number;
353 challenge_len = *challenge++;
354 response_len = *response++;
358 if (radius_init(radius_msg) < 0) {
359 error("%s", radius_msg);
363 /* return error for types we can't handle */
364 if ((digest->code != CHAP_MD5)
365 #ifdef PPP_WITH_CHAPMS
366 && (digest->code != CHAP_MICROSOFT)
367 && (digest->code != CHAP_MICROSOFT_V2)
370 error("RADIUS: Challenge type %u unsupported", digest->code);
374 /* Put user with potentially realm added in rstate.user */
375 if (!rstate.done_chap_once) {
376 make_username_realm(user);
377 rstate.client_port = get_client_port (portnummap ? ppp_devnam() : ppp_ifname());
378 if (radius_pre_auth_hook) {
379 radius_pre_auth_hook(rstate.user,
385 send = received = NULL;
388 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
391 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
393 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
396 * add the challenge and response fields
398 switch (digest->code) {
400 /* CHAP-Challenge and CHAP-Password */
401 if (response_len != MD5_DIGEST_LENGTH)
404 memcpy(&cpassword[1], response, MD5_DIGEST_LENGTH);
406 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
407 challenge, challenge_len, VENDOR_NONE);
408 rc_avpair_add(&send, PW_CHAP_PASSWORD,
409 cpassword, MD5_DIGEST_LENGTH + 1, VENDOR_NONE);
412 #ifdef PPP_WITH_CHAPMS
415 /* MS-CHAP-Challenge and MS-CHAP-Response */
416 u_char *p = cpassword;
418 if (response_len != MS_CHAP_RESPONSE_LEN)
421 /* The idiots use a different field order in RADIUS than PPP */
422 *p++ = response[MS_CHAP_USENT];
423 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
425 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
426 challenge, challenge_len, VENDOR_MICROSOFT);
427 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
428 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
432 case CHAP_MICROSOFT_V2:
434 /* MS-CHAP-Challenge and MS-CHAP2-Response */
435 u_char *p = cpassword;
437 if (response_len != MS_CHAP2_RESPONSE_LEN)
440 /* The idiots use a different field order in RADIUS than PPP */
441 *p++ = response[MS_CHAP2_FLAGS];
442 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
443 + MS_CHAP2_NTRESP_LEN));
445 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
446 challenge, challenge_len, VENDOR_MICROSOFT);
447 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
448 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
454 remote_number = ppp_get_remote_number();
455 ipparam = ppp_ipparam();
457 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
460 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
462 /* Add user specified vp's */
464 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
467 * make authentication with RADIUS server
470 if (rstate.authserver) {
471 result = rc_auth_using_server(rstate.authserver,
472 rstate.client_port, send,
473 &received, radius_msg, req_info);
475 result = rc_auth(rstate.client_port, send, &received, radius_msg,
479 strlcpy(message, radius_msg, message_space);
481 if (result == OK_RC) {
482 if (!rstate.done_chap_once) {
483 if (radius_setparams(received, radius_msg, req_info, digest,
484 challenge, message, message_space) < 0) {
485 error("%s", radius_msg);
488 rstate.done_chap_once = 1;
493 rc_avpair_free(received);
494 rc_avpair_free (send);
495 return (result == OK_RC);
498 /**********************************************************************
499 * %FUNCTION: make_username_realm
501 * user -- the user given to pppd
505 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
506 * then the default realm from the radiusclient config file is added.
507 ***********************************************************************/
509 make_username_realm(const char *user)
513 if ( user != NULL ) {
514 strlcpy(rstate.user, user, sizeof(rstate.user));
519 default_realm = rc_conf_str("default_realm");
521 if (!strchr(rstate.user, '@') &&
523 (*default_realm != '\0')) {
524 strlcat(rstate.user, "@", sizeof(rstate.user));
525 strlcat(rstate.user, default_realm, sizeof(rstate.user));
529 /**********************************************************************
530 * %FUNCTION: radius_setparams
532 * vp -- received value-pairs
533 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
535 * >= 0 on success; -1 on failure
537 * Parses attributes sent by RADIUS server and sets them in pppd.
538 ***********************************************************************/
540 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
541 struct chap_digest_type *digest, unsigned char *challenge,
542 char *message, int message_space)
545 int ms_chap2_success = 0;
547 int mppe_enc_keys = 0; /* whether or not these were received */
548 int mppe_enc_policy = 0;
549 int mppe_enc_types = 0;
552 ipcp_options *wo = &ipcp_wantoptions[0];
553 ipcp_options *ao = &ipcp_allowoptions[0];
560 /* Send RADIUS attributes to anyone else who might be interested */
561 if (radius_attributes_hook) {
562 (*radius_attributes_hook)(vp);
566 * service type (if not framed then quit),
567 * new IP address (RADIUS can define static IP for some users),
571 if (vp->vendorcode == VENDOR_NONE) {
572 switch (vp->attribute) {
573 case PW_SERVICE_TYPE:
574 /* check for service type */
575 /* if not FRAMED then exit */
576 if (vp->lvalue != PW_FRAMED) {
577 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
578 vp->lvalue, rstate.user);
583 case PW_FRAMED_PROTOCOL:
584 /* check for framed protocol type */
585 /* if not PPP then also exit */
586 if (vp->lvalue != PW_PPP) {
587 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
588 vp->lvalue, rstate.user);
593 case PW_SESSION_TIMEOUT:
594 /* Session timeout */
595 ppp_set_max_connect_time(vp->lvalue);
598 /* packet filter, will be handled via ip-(up|down) script */
599 ppp_script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
601 case PW_FRAMED_ROUTE:
602 /* route, will be handled via ip-(up|down) script */
603 ppp_script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
605 case PW_IDLE_TIMEOUT:
607 ppp_set_max_idle_time(vp->lvalue);
609 case PW_SESSION_OCTETS_LIMIT:
610 /* Session traffic limit */
611 ppp_set_session_limit(vp->lvalue);
613 case PW_OCTETS_DIRECTION:
614 /* Session traffic limit direction check */
615 ppp_set_session_limit_dir(vp->lvalue);
617 case PW_ACCT_INTERIM_INTERVAL:
618 /* Send accounting updates every few seconds */
619 rstate.acct_interim_interval = vp->lvalue;
620 /* RFC says it MUST NOT be less than 60 seconds */
621 /* We use "0" to signify not sending updates */
622 if (rstate.acct_interim_interval &&
623 rstate.acct_interim_interval < 60) {
624 rstate.acct_interim_interval = 60;
627 case PW_FRAMED_IP_ADDRESS:
628 /* seting up remote IP addresses */
630 if (remote == 0xffffffff) {
631 /* 0xffffffff means user should be allowed to select one */
632 rstate.any_ip_addr_ok = 1;
633 } else if (remote != 0xfffffffe) {
634 /* 0xfffffffe means NAS should select an ip address */
635 remote = htonl(vp->lvalue);
636 if (ppp_bad_ip_addr (remote)) {
637 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
638 remote, rstate.user);
641 rstate.choose_ip = 1;
642 rstate.ip_addr = remote;
645 case PW_NAS_IP_ADDRESS:
646 wo->ouraddr = htonl(vp->lvalue);
649 /* Save Class attribute to pass it in accounting request */
650 // if (vp->lvalue <= MAXCLASSLEN) { // <- Attribute could be this big, but vp->strvalue is limited to AUTH_STRING_LEN characters
651 if (vp->lvalue <= AUTH_STRING_LEN) {
652 rstate.class_len=vp->lvalue;
653 memcpy(rstate.class, vp->strvalue, rstate.class_len);
654 } /* else too big for our buffer - ignore it */
657 ppp_set_mtu(rstate.client_port,MIN(ppp_get_mtu(rstate.client_port),vp->lvalue));
662 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
663 #ifdef PPP_WITH_CHAPMS
664 switch (vp->attribute) {
665 case PW_MS_CHAP2_SUCCESS:
666 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
667 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
671 strlcpy(message, (char*) vp->strvalue + 1, message_space);
672 ms_chap2_success = 1;
676 case PW_MS_CHAP_MPPE_KEYS:
677 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
678 slprintf(msg, BUF_LEN,
679 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
685 case PW_MS_MPPE_SEND_KEY:
686 case PW_MS_MPPE_RECV_KEY:
687 if (radius_setmppekeys2(vp, req_info) < 0) {
688 slprintf(msg, BUF_LEN,
689 "RADIUS: bad MS-MPPE-%s-Key attribute",
690 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
697 case PW_MS_MPPE_ENCRYPTION_POLICY:
698 mppe_enc_policy = vp->lvalue; /* save for later */
701 case PW_MS_MPPE_ENCRYPTION_TYPES:
702 mppe_enc_types = vp->lvalue; /* save for later */
705 #endif /* PPP_WITH_MPPE */
707 case PW_MS_PRIMARY_DNS_SERVER:
708 ao->dnsaddr[0] = htonl(vp->lvalue);
711 ao->dnsaddr[1] = ao->dnsaddr[0];
713 case PW_MS_SECONDARY_DNS_SERVER:
714 ao->dnsaddr[1] = htonl(vp->lvalue);
717 ao->dnsaddr[0] = ao->dnsaddr[1];
719 case PW_MS_PRIMARY_NBNS_SERVER:
720 ao->winsaddr[0] = htonl(vp->lvalue);
723 ao->winsaddr[1] = ao->winsaddr[0];
725 case PW_MS_SECONDARY_NBNS_SERVER:
726 ao->winsaddr[1] = htonl(vp->lvalue);
729 ao->winsaddr[0] = ao->winsaddr[1];
733 #endif /* PPP_WITH_CHAPMS */
738 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
739 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
744 * Require both policy and key attributes to indicate a valid key.
745 * Note that if the policy value was '0' we don't set the key!
747 if (mppe_enc_policy && mppe_enc_keys) {
748 /* Set/modify allowed encryption types. */
750 mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
760 /**********************************************************************
761 * %FUNCTION: radius_setmppekeys
763 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
764 * req_info -- radius request information used for encryption
766 * >= 0 on success; -1 on failure
768 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
770 ***********************************************************************/
772 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
773 unsigned char *challenge)
778 unsigned char plain[32];
779 unsigned char buf[MD5_DIGEST_LENGTH];
783 if (vp->lvalue != 32) {
784 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
789 memcpy(plain, vp->strvalue, sizeof(plain));
791 ctx = PPP_MD_CTX_new();
794 if (PPP_DigestInit(ctx, PPP_md5())) {
796 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
798 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
800 buflen = sizeof(buf);
801 if (PPP_DigestFinal(ctx, buf, &buflen)) {
808 PPP_MD_CTX_free(ctx);
813 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
818 ctx = PPP_MD_CTX_new();
821 if (PPP_DigestInit(ctx, PPP_md5())) {
823 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
825 if (PPP_DigestUpdate(ctx, vp->strvalue, 16)) {
827 buflen = MD5_DIGEST_LENGTH;
828 if (PPP_DigestFinal(ctx, buf, &buflen)) {
835 PPP_MD_CTX_free(ctx);
840 for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
841 plain[i + 16] ^= buf[i];
845 * Annoying. The "key" returned is just the NTPasswordHashHash, which
846 * the NAS (us) doesn't need; we only need the start key. So we have
847 * to generate the start key, sigh. NB: We do not support the LM-Key.
849 mppe_set_chapv1(challenge, &plain[8]);
857 /**********************************************************************
858 * %FUNCTION: radius_setmppekeys2
860 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
861 * req_info -- radius request information used for encryption
863 * >= 0 on success; -1 on failure
865 * Decrypt the key provided by the RADIUS server for MPPE encryption.
867 ***********************************************************************/
869 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
874 unsigned char *salt = vp->strvalue;
875 unsigned char *crypt = vp->strvalue + 2;
876 unsigned char plain[32];
877 unsigned char buf[MD5_DIGEST_LENGTH];
881 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
884 if (vp->lvalue != 34) {
885 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
890 if ((salt[0] & 0x80) == 0) {
891 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
895 memcpy(plain, crypt, 32);
897 ctx = PPP_MD_CTX_new();
899 error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key attribute", type);
903 buflen = sizeof(buf);
904 if (!PPP_DigestInit(ctx, PPP_md5())) {
905 error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key attribute", type);
906 } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
907 error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key attribute", type);
908 } else if (!PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
909 error("RADIUS: Error mixing in request vector for MS-MPPE-%s-Key attribute", type);
910 } else if (!PPP_DigestUpdate(ctx, salt, 2)) {
911 error("RADIUS: Error mixing in salt for MS-MPPE-%s-Key attribute", type);
912 } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
913 error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key attribute", type);
918 PPP_MD_CTX_free(ctx);
923 for (i = 0; i < 16; i++) {
927 if (plain[0] != 16) {
928 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
929 (int) plain[0], type);
934 ctx = PPP_MD_CTX_new();
936 error("RADIUS: Error creating PPP_MD_CTX for MS-MPPE-%s-Key(2) attribute", type);
940 buflen = sizeof(buf);
942 if (!PPP_DigestInit(ctx, PPP_md5())) {
943 error("RADIUS: Error setting hash algorithm to MD5 for MS-MPPE-%s-Key(2) attribute", type);
944 } else if (!PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
945 error("RADIUS: Error mixing in radius secret for MS-MPPE-%s-Key(2) attribute", type);
946 } else if (!PPP_DigestUpdate(ctx, crypt, 16)) {
947 error("RADIUS: Error mixing in crypt vector for MS-MPPE-%s-Key(2) attribute", type);
948 } else if (!PPP_DigestFinal(ctx, buf, &buflen)) {
949 error("RADIUS: Error finalizing key buffer for MS-MPPE-%s-Key(2) attribute", type);
954 PPP_MD_CTX_free(ctx);
959 plain[16] ^= buf[0]; /* only need the first byte */
961 if (vp->attribute == PW_MS_MPPE_SEND_KEY) {
962 mppe_set_keys(plain + 1, NULL, 16);
964 mppe_set_keys(NULL, plain + 1, 16);
968 #endif /* PPP_WITH_MPPE */
970 /**********************************************************************
971 * %FUNCTION: radius_acct_start
977 * Sends a "start" accounting message to the RADIUS server.
978 ***********************************************************************/
980 radius_acct_start(void)
984 VALUE_PAIR *send = NULL;
985 ipcp_options *ho = &ipcp_hisoptions[0];
987 const char *remote_number;
990 if (!rstate.initialized) {
994 rstate.start_time = time(NULL);
996 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
998 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
999 rstate.session_id, 0, VENDOR_NONE);
1000 rc_avpair_add(&send, PW_USER_NAME,
1001 rstate.user, 0, VENDOR_NONE);
1003 if (rstate.class_len > 0)
1004 rc_avpair_add(&send, PW_CLASS,
1005 rstate.class, rstate.class_len, VENDOR_NONE);
1007 av_type = PW_STATUS_START;
1008 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1010 av_type = PW_FRAMED;
1011 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1014 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1016 remote_number = ppp_get_remote_number();
1017 ipparam = ppp_ipparam();
1018 if (remote_number) {
1019 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1020 remote_number, 0, VENDOR_NONE);
1022 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1024 av_type = PW_RADIUS;
1025 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1028 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1029 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1031 hisaddr = ho->hisaddr;
1032 av_type = htonl(hisaddr);
1033 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1035 /* Add user specified vp's */
1037 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1039 if (rstate.acctserver) {
1040 result = rc_acct_using_server(rstate.acctserver,
1041 rstate.client_port, send);
1043 result = rc_acct(rstate.client_port, send);
1046 rc_avpair_free(send);
1048 if (result != OK_RC) {
1049 /* RADIUS server could be down so make this a warning */
1051 "Accounting START failed for %s", rstate.user);
1054 /* Kick off periodic accounting reports */
1055 if (rstate.acct_interim_interval) {
1056 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1060 /**********************************************************************
1061 * %FUNCTION: radius_acct_stop
1067 * Sends a "stop" accounting message to the RADIUS server.
1068 ***********************************************************************/
1070 radius_acct_stop(void)
1073 VALUE_PAIR *send = NULL;
1074 ipcp_options *ho = &ipcp_hisoptions[0];
1077 const char *remote_number;
1078 const char *ipparam;
1079 ppp_link_stats_st stats;
1081 if (!rstate.initialized) {
1085 if (rstate.acct_interim_interval)
1086 ppp_untimeout(radius_acct_interim, NULL);
1088 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1091 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1093 if (rstate.class_len > 0)
1094 rc_avpair_add(&send, PW_CLASS,
1095 rstate.class, rstate.class_len, VENDOR_NONE);
1097 av_type = PW_STATUS_STOP;
1098 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1100 av_type = PW_FRAMED;
1101 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1104 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1106 av_type = PW_RADIUS;
1107 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1109 if (ppp_get_link_stats(&stats)) {
1111 av_type = ppp_get_link_uptime();
1112 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1114 av_type = stats.bytes_out & 0xFFFFFFFF;
1115 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1117 if (stats.bytes_out > 0xFFFFFFFF) {
1118 av_type = stats.bytes_out >> 32;
1119 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1122 av_type = stats.bytes_in & 0xFFFFFFFF;
1123 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1125 if (stats.bytes_in > 0xFFFFFFFF) {
1126 av_type = stats.bytes_in >> 32;
1127 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1130 av_type = stats.pkts_out;
1131 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1133 av_type = stats.pkts_in;
1134 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1137 remote_number = ppp_get_remote_number();
1138 ipparam = ppp_ipparam();
1139 if (remote_number) {
1140 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1141 remote_number, 0, VENDOR_NONE);
1143 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1145 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1146 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1148 av_type = PW_NAS_ERROR;
1149 switch( ppp_status() ) {
1151 av_type = PW_USER_REQUEST;
1154 case EXIT_USER_REQUEST:
1155 av_type = PW_ADMIN_RESET;
1159 case EXIT_PEER_DEAD:
1160 case EXIT_CONNECT_FAILED:
1161 av_type = PW_LOST_CARRIER;
1164 case EXIT_INIT_FAILED:
1165 case EXIT_OPEN_FAILED:
1166 case EXIT_LOCK_FAILED:
1167 case EXIT_PTYCMD_FAILED:
1168 av_type = PW_PORT_ERROR;
1171 case EXIT_PEER_AUTH_FAILED:
1172 case EXIT_AUTH_TOPEER_FAILED:
1173 case EXIT_NEGOTIATION_FAILED:
1174 case EXIT_CNID_AUTH_FAILED:
1175 av_type = PW_SERVICE_UNAVAILABLE;
1178 case EXIT_IDLE_TIMEOUT:
1179 av_type = PW_ACCT_IDLE_TIMEOUT;
1183 av_type = PW_CALLBACK;
1186 case EXIT_CONNECT_TIME:
1187 av_type = PW_ACCT_SESSION_TIMEOUT;
1190 case EXIT_TRAFFIC_LIMIT:
1191 av_type = PW_NAS_REQUEST;
1195 av_type = PW_NAS_ERROR;
1198 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1200 hisaddr = ho->hisaddr;
1201 av_type = htonl(hisaddr);
1202 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1204 /* Add user specified vp's */
1206 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1208 if (rstate.acctserver) {
1209 result = rc_acct_using_server(rstate.acctserver,
1210 rstate.client_port, send);
1212 result = rc_acct(rstate.client_port, send);
1215 if (result != OK_RC) {
1216 /* RADIUS server could be down so make this a warning */
1218 "Accounting STOP failed for %s", rstate.user);
1220 rc_avpair_free(send);
1223 /**********************************************************************
1224 * %FUNCTION: radius_acct_interim
1230 * Sends an interim accounting message to the RADIUS server
1231 ***********************************************************************/
1233 radius_acct_interim(void *ignored)
1236 VALUE_PAIR *send = NULL;
1237 ipcp_options *ho = &ipcp_hisoptions[0];
1240 const char *remote_number;
1241 const char *ipparam;
1242 ppp_link_stats_st stats;
1244 if (!rstate.initialized) {
1248 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1251 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1253 if (rstate.class_len > 0)
1254 rc_avpair_add(&send, PW_CLASS,
1255 rstate.class, rstate.class_len, VENDOR_NONE);
1257 av_type = PW_STATUS_ALIVE;
1258 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1260 av_type = PW_FRAMED;
1261 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1264 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1266 av_type = PW_RADIUS;
1267 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1269 if (ppp_get_link_stats(&stats)) {
1271 av_type = ppp_get_link_uptime();
1272 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1274 av_type = stats.bytes_out & 0xFFFFFFFF;
1275 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1277 if (stats.bytes_out > 0xFFFFFFFF) {
1278 av_type = stats.bytes_out >> 32;
1279 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1282 av_type = stats.bytes_in & 0xFFFFFFFF;
1283 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1285 if (stats.bytes_in > 0xFFFFFFFF) {
1286 av_type = stats.bytes_in >> 32;
1287 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1290 av_type = stats.pkts_out;
1291 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1293 av_type = stats.pkts_in;
1294 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1297 remote_number = ppp_get_remote_number();
1298 ipparam = ppp_ipparam();
1299 if (remote_number) {
1300 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1301 remote_number, 0, VENDOR_NONE);
1303 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1305 av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1306 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1308 hisaddr = ho->hisaddr;
1309 av_type = htonl(hisaddr);
1310 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1312 /* Add user specified vp's */
1314 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1316 if (rstate.acctserver) {
1317 result = rc_acct_using_server(rstate.acctserver,
1318 rstate.client_port, send);
1320 result = rc_acct(rstate.client_port, send);
1323 if (result != OK_RC) {
1324 /* RADIUS server could be down so make this a warning */
1326 "Interim accounting failed for %s", rstate.user);
1328 rc_avpair_free(send);
1330 /* Schedule another one */
1331 ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1334 /**********************************************************************
1335 * %FUNCTION: radius_ip_up
1342 * Called when IPCP is up. We'll do a start-accounting record.
1343 ***********************************************************************/
1345 radius_ip_up(void *opaque, int arg)
1347 radius_acct_start();
1350 /**********************************************************************
1351 * %FUNCTION: radius_ip_down
1358 * Called when IPCP is down. We'll do a stop-accounting record.
1359 ***********************************************************************/
1361 radius_ip_down(void *opaque, int arg)
1366 /**********************************************************************
1367 * %FUNCTION: radius_init
1369 * msg -- buffer of size BUF_LEN for error message
1371 * negative on failure; non-negative on success
1373 * Initializes radiusclient library
1374 ***********************************************************************/
1376 radius_init(char *msg)
1378 if (rstate.initialized) {
1382 if (config_file && *config_file) {
1383 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1386 rstate.initialized = 1;
1388 if (rc_read_config(rstate.config_file) != 0) {
1389 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1390 rstate.config_file);
1394 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1395 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1396 rc_conf_str("dictionary"));
1400 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1401 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1402 rc_conf_str("mapfile"));
1406 /* Add av pairs saved during option parsing */
1408 struct avpopt *n = avpopt->next;
1410 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1411 free(avpopt->vpstr);
1418 /**********************************************************************
1419 * %FUNCTION: get_client_port
1421 * ifname -- PPP interface name (e.g. "ppp7")
1423 * The NAS port number (e.g. 7)
1425 * Extracts the port number from the interface name
1426 ***********************************************************************/
1428 get_client_port(const char *ifname)
1431 if (sscanf(ifname, "ppp%d", &port) == 1) {
1434 return rc_map2id(ifname);
1437 /**********************************************************************
1438 * %FUNCTION: radius_allowed_address
1440 * addr -- IP address
1442 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1444 ***********************************************************************/
1446 radius_allowed_address(u_int32_t addr)
1448 ipcp_options *wo = &ipcp_wantoptions[0];
1450 if (!rstate.choose_ip) {
1451 /* If RADIUS server said any address is OK, then fine... */
1452 if (rstate.any_ip_addr_ok) {
1456 /* Sigh... if an address was supplied for remote host in pppd
1457 options, it has to match that. */
1458 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1464 if (addr == rstate.ip_addr) return 1;
1468 /* Useful for other plugins */
1469 char *radius_logged_in_user(void)