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>
33 #include <netinet/in.h>
36 #include <pppd/pppd.h>
37 #include <pppd/chap-new.h>
38 #ifdef PPP_WITH_CHAPMS
39 #include <pppd/chap_ms.h>
41 #include <pppd/mppe.h>
46 #include <pppd/ipcp.h>
48 #include "radiusclient.h"
52 #define MD5_HASH_SIZE 16
56 static char *config_file = NULL;
57 static int add_avp(char **);
58 static struct avpopt {
62 static bool portnummap = 0;
64 static option_t Options[] = {
65 { "radius-config-file", o_string, &config_file },
66 { "avpair", o_special, add_avp },
67 { "map-to-ttyname", o_bool, &portnummap,
68 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
69 { "map-to-ifname", o_bool, &portnummap,
70 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
74 static int radius_secret_check(void);
75 static int radius_pap_auth(char *user,
78 struct wordlist **paddrs,
79 struct wordlist **popts);
80 static int radius_chap_verify(char *user, char *ourname, int id,
81 struct chap_digest_type *digest,
82 unsigned char *challenge,
83 unsigned char *response,
84 char *message, int message_space);
86 static void radius_ip_up(void *opaque, int arg);
87 static void radius_ip_down(void *opaque, int arg);
88 static void make_username_realm(char *user);
89 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
90 struct chap_digest_type *digest,
91 unsigned char *challenge,
92 char *message, int message_space);
93 static void radius_choose_ip(u_int32_t *addrp);
94 static int radius_init(char *msg);
95 static int get_client_port(char *ifname);
96 static int radius_allowed_address(u_int32_t addr);
97 static void radius_acct_interim(void *);
99 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
101 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
105 #define MAXSESSIONID 32
109 #define MAXCLASSLEN 500
112 struct radius_state {
119 char user[MAXNAMELEN];
120 char config_file[MAXPATHLEN];
121 char session_id[MAXSESSIONID + 1];
123 int acct_interim_interval;
124 SERVER *authserver; /* Authentication server to use */
125 SERVER *acctserver; /* Accounting server to use */
127 char class[MAXCLASSLEN];
128 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
131 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
133 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
134 In that case, they override the values in the radiusclient.conf file */
135 void (*radius_pre_auth_hook)(char const *user,
137 SERVER **acctserver) = NULL;
139 static struct radius_state rstate;
141 char pppd_version[] = PPPD_VERSION;
143 /**********************************************************************
144 * %FUNCTION: plugin_init
150 * Initializes RADIUS plugin.
151 ***********************************************************************/
155 pap_check_hook = radius_secret_check;
156 pap_auth_hook = radius_pap_auth;
158 chap_check_hook = radius_secret_check;
159 chap_verify_hook = radius_chap_verify;
161 ip_choose_hook = radius_choose_ip;
162 allowed_address_hook = radius_allowed_address;
164 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
165 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
167 memset(&rstate, 0, sizeof(rstate));
169 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
170 sizeof(rstate.config_file));
172 add_options(Options);
174 info("RADIUS plugin initialized.");
177 /**********************************************************************
180 * argv -- the <attribute=value> pair to add
184 * Adds an av pair to be passed on to the RADIUS server on each request.
185 ***********************************************************************/
189 struct avpopt *p = malloc(sizeof(struct avpopt));
191 /* Append to a list of vp's for later parsing */
192 p->vpstr = strdup(*argv);
199 /**********************************************************************
200 * %FUNCTION: radius_secret_check
204 * 1 -- we are ALWAYS willing to supply a secret. :-)
206 * Tells pppd that we will try to authenticate the peer, and not to
207 * worry about looking in *-secrets file(s)
208 ***********************************************************************/
210 radius_secret_check(void)
215 /**********************************************************************
216 * %FUNCTION: radius_choose_ip
218 * addrp -- where to store the IP address
222 * If RADIUS server has specified an IP address, it is stored in *addrp.
223 ***********************************************************************/
225 radius_choose_ip(u_int32_t *addrp)
227 if (rstate.choose_ip) {
228 *addrp = rstate.ip_addr;
232 /**********************************************************************
233 * %FUNCTION: radius_pap_auth
235 * user -- user-name of peer
236 * passwd -- password supplied by peer
237 * msgp -- Message which will be sent in PAP response
238 * paddrs -- set to a list of possible peer IP addresses
239 * popts -- set to a list of additional pppd options
241 * 1 if we can authenticate, -1 if we cannot.
243 * Performs PAP authentication using RADIUS
244 ***********************************************************************/
246 radius_pap_auth(char *user,
249 struct wordlist **paddrs,
250 struct wordlist **popts)
252 VALUE_PAIR *send, *received;
255 static char radius_msg[BUF_LEN];
260 if (radius_init(radius_msg) < 0) {
264 /* Put user with potentially realm added in rstate.user */
265 make_username_realm(user);
267 if (radius_pre_auth_hook) {
268 radius_pre_auth_hook(rstate.user,
276 /* Hack... the "port" is the ppp interface number. Should really be
278 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
281 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
284 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
286 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
287 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
288 if (*remote_number) {
289 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
292 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
294 /* Add user specified vp's */
296 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
298 if (rstate.authserver) {
299 result = rc_auth_using_server(rstate.authserver,
300 rstate.client_port, send,
301 &received, radius_msg, NULL);
303 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
306 if (result == OK_RC) {
307 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
312 /* free value pairs */
313 rc_avpair_free(received);
314 rc_avpair_free(send);
316 return (result == OK_RC) ? 1 : 0;
319 /**********************************************************************
320 * %FUNCTION: radius_chap_verify
322 * user -- name of the peer
323 * ourname -- name for this machine
324 * id -- the ID byte in the challenge
325 * digest -- points to the structure representing the digest type
326 * challenge -- the challenge string we sent (length in first byte)
327 * response -- the response (hash) the peer sent back (length in 1st byte)
328 * message -- space for a message to be returned to the peer
329 * message_space -- number of bytes available at *message.
331 * 1 if the response is good, 0 if it is bad
333 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
334 ***********************************************************************/
336 radius_chap_verify(char *user, char *ourname, int id,
337 struct chap_digest_type *digest,
338 unsigned char *challenge, unsigned char *response,
339 char *message, int message_space)
341 VALUE_PAIR *send, *received;
343 static char radius_msg[BUF_LEN];
345 int challenge_len, response_len;
346 u_char cpassword[MAX_RESPONSE_LEN + 1];
348 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
349 REQUEST_INFO request_info, *req_info = &request_info;
351 REQUEST_INFO *req_info = NULL;
354 challenge_len = *challenge++;
355 response_len = *response++;
359 if (radius_init(radius_msg) < 0) {
360 error("%s", radius_msg);
364 /* return error for types we can't handle */
365 if ((digest->code != CHAP_MD5)
366 #ifdef PPP_WITH_CHAPMS
367 && (digest->code != CHAP_MICROSOFT)
368 && (digest->code != CHAP_MICROSOFT_V2)
371 error("RADIUS: Challenge type %u unsupported", digest->code);
375 /* Put user with potentially realm added in rstate.user */
376 if (!rstate.done_chap_once) {
377 make_username_realm(user);
378 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
379 if (radius_pre_auth_hook) {
380 radius_pre_auth_hook(rstate.user,
386 send = received = NULL;
389 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
392 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
394 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
397 * add the challenge and response fields
399 switch (digest->code) {
401 /* CHAP-Challenge and CHAP-Password */
402 if (response_len != MD5_HASH_SIZE)
405 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
407 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
408 challenge, challenge_len, VENDOR_NONE);
409 rc_avpair_add(&send, PW_CHAP_PASSWORD,
410 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
413 #ifdef PPP_WITH_CHAPMS
416 /* MS-CHAP-Challenge and MS-CHAP-Response */
417 u_char *p = cpassword;
419 if (response_len != MS_CHAP_RESPONSE_LEN)
422 /* The idiots use a different field order in RADIUS than PPP */
423 *p++ = response[MS_CHAP_USENT];
424 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
426 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
427 challenge, challenge_len, VENDOR_MICROSOFT);
428 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
429 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
433 case CHAP_MICROSOFT_V2:
435 /* MS-CHAP-Challenge and MS-CHAP2-Response */
436 u_char *p = cpassword;
438 if (response_len != MS_CHAP2_RESPONSE_LEN)
441 /* The idiots use a different field order in RADIUS than PPP */
442 *p++ = response[MS_CHAP2_FLAGS];
443 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
444 + MS_CHAP2_NTRESP_LEN));
446 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
447 challenge, challenge_len, VENDOR_MICROSOFT);
448 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
449 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
455 if (*remote_number) {
456 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
459 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
461 /* Add user specified vp's */
463 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
466 * make authentication with RADIUS server
469 if (rstate.authserver) {
470 result = rc_auth_using_server(rstate.authserver,
471 rstate.client_port, send,
472 &received, radius_msg, req_info);
474 result = rc_auth(rstate.client_port, send, &received, radius_msg,
478 strlcpy(message, radius_msg, message_space);
480 if (result == OK_RC) {
481 if (!rstate.done_chap_once) {
482 if (radius_setparams(received, radius_msg, req_info, digest,
483 challenge, message, message_space) < 0) {
484 error("%s", radius_msg);
487 rstate.done_chap_once = 1;
492 rc_avpair_free(received);
493 rc_avpair_free (send);
494 return (result == OK_RC);
497 /**********************************************************************
498 * %FUNCTION: make_username_realm
500 * user -- the user given to pppd
504 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
505 * then the default realm from the radiusclient config file is added.
506 ***********************************************************************/
508 make_username_realm(char *user)
512 if ( user != NULL ) {
513 strlcpy(rstate.user, user, sizeof(rstate.user));
518 default_realm = rc_conf_str("default_realm");
520 if (!strchr(rstate.user, '@') &&
522 (*default_realm != '\0')) {
523 strlcat(rstate.user, "@", sizeof(rstate.user));
524 strlcat(rstate.user, default_realm, sizeof(rstate.user));
528 /**********************************************************************
529 * %FUNCTION: radius_setparams
531 * vp -- received value-pairs
532 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
534 * >= 0 on success; -1 on failure
536 * Parses attributes sent by RADIUS server and sets them in pppd.
537 ***********************************************************************/
539 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
540 struct chap_digest_type *digest, unsigned char *challenge,
541 char *message, int message_space)
544 int ms_chap2_success = 0;
546 int mppe_enc_keys = 0; /* whether or not these were received */
547 int mppe_enc_policy = 0;
548 int mppe_enc_types = 0;
551 ipcp_options *wo = &ipcp_wantoptions[0];
552 ipcp_options *ao = &ipcp_allowoptions[0];
559 /* Send RADIUS attributes to anyone else who might be interested */
560 if (radius_attributes_hook) {
561 (*radius_attributes_hook)(vp);
565 * service type (if not framed then quit),
566 * new IP address (RADIUS can define static IP for some users),
570 if (vp->vendorcode == VENDOR_NONE) {
571 switch (vp->attribute) {
572 case PW_SERVICE_TYPE:
573 /* check for service type */
574 /* if not FRAMED then exit */
575 if (vp->lvalue != PW_FRAMED) {
576 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
577 vp->lvalue, rstate.user);
582 case PW_FRAMED_PROTOCOL:
583 /* check for framed protocol type */
584 /* if not PPP then also exit */
585 if (vp->lvalue != PW_PPP) {
586 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
587 vp->lvalue, rstate.user);
592 case PW_SESSION_TIMEOUT:
593 /* Session timeout */
594 maxconnect = vp->lvalue;
597 /* packet filter, will be handled via ip-(up|down) script */
598 script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
600 case PW_FRAMED_ROUTE:
601 /* route, will be handled via ip-(up|down) script */
602 script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
604 case PW_IDLE_TIMEOUT:
606 idle_time_limit = vp->lvalue;
608 #ifdef PPP_WITH_MAXOCTETS
609 case PW_SESSION_OCTETS_LIMIT:
610 /* Session traffic limit */
611 maxoctets = vp->lvalue;
613 case PW_OCTETS_DIRECTION:
614 /* Session traffic limit direction check */
615 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
618 case PW_ACCT_INTERIM_INTERVAL:
619 /* Send accounting updates every few seconds */
620 rstate.acct_interim_interval = vp->lvalue;
621 /* RFC says it MUST NOT be less than 60 seconds */
622 /* We use "0" to signify not sending updates */
623 if (rstate.acct_interim_interval &&
624 rstate.acct_interim_interval < 60) {
625 rstate.acct_interim_interval = 60;
628 case PW_FRAMED_IP_ADDRESS:
629 /* seting up remote IP addresses */
631 if (remote == 0xffffffff) {
632 /* 0xffffffff means user should be allowed to select one */
633 rstate.any_ip_addr_ok = 1;
634 } else if (remote != 0xfffffffe) {
635 /* 0xfffffffe means NAS should select an ip address */
636 remote = htonl(vp->lvalue);
637 if (bad_ip_adrs (remote)) {
638 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
639 remote, rstate.user);
642 rstate.choose_ip = 1;
643 rstate.ip_addr = remote;
646 case PW_NAS_IP_ADDRESS:
647 wo->ouraddr = htonl(vp->lvalue);
650 /* Save Class attribute to pass it in accounting request */
651 if (vp->lvalue <= MAXCLASSLEN) {
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 netif_set_mtu(rstate.client_port,MIN(netif_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)
780 if (vp->lvalue != 32) {
781 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
786 memcpy(plain, vp->strvalue, sizeof(plain));
789 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
790 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
791 MD5_Final(buf, &Context);
793 for (i = 0; i < 16; i++)
797 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
798 MD5_Update(&Context, vp->strvalue, 16);
799 MD5_Final(buf, &Context);
801 for(i = 0; i < 16; i++)
802 plain[i + 16] ^= buf[i];
805 * Annoying. The "key" returned is just the NTPasswordHashHash, which
806 * the NAS (us) doesn't need; we only need the start key. So we have
807 * to generate the start key, sigh. NB: We do not support the LM-Key.
809 mppe_set_chapv1(challenge, &plain[8]);
814 /**********************************************************************
815 * %FUNCTION: radius_setmppekeys2
817 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
818 * req_info -- radius request information used for encryption
820 * >= 0 on success; -1 on failure
822 * Decrypt the key provided by the RADIUS server for MPPE encryption.
824 ***********************************************************************/
826 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
830 u_char *salt = vp->strvalue;
831 u_char *crypt = vp->strvalue + 2;
833 u_char buf[MD5_HASH_SIZE];
836 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
839 if (vp->lvalue != 34) {
840 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
845 if ((salt[0] & 0x80) == 0) {
846 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
850 memcpy(plain, crypt, 32);
853 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
854 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
855 MD5_Update(&Context, salt, 2);
856 MD5_Final(buf, &Context);
858 for (i = 0; i < 16; i++)
861 if (plain[0] != 16) {
862 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
863 (int) plain[0], type);
868 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
869 MD5_Update(&Context, crypt, 16);
870 MD5_Final(buf, &Context);
872 plain[16] ^= buf[0]; /* only need the first byte */
874 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
875 mppe_set_keys(plain + 1, NULL, 16);
877 mppe_set_keys(NULL, plain + 1, 16);
881 #endif /* PPP_WITH_MPPE */
883 /**********************************************************************
884 * %FUNCTION: radius_acct_start
890 * Sends a "start" accounting message to the RADIUS server.
891 ***********************************************************************/
893 radius_acct_start(void)
897 VALUE_PAIR *send = NULL;
898 ipcp_options *ho = &ipcp_hisoptions[0];
901 if (!rstate.initialized) {
905 rstate.start_time = time(NULL);
907 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
909 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
910 rstate.session_id, 0, VENDOR_NONE);
911 rc_avpair_add(&send, PW_USER_NAME,
912 rstate.user, 0, VENDOR_NONE);
914 if (rstate.class_len > 0)
915 rc_avpair_add(&send, PW_CLASS,
916 rstate.class, rstate.class_len, VENDOR_NONE);
918 av_type = PW_STATUS_START;
919 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
922 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
925 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
927 if (*remote_number) {
928 rc_avpair_add(&send, PW_CALLING_STATION_ID,
929 remote_number, 0, VENDOR_NONE);
931 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
934 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
937 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
938 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
940 hisaddr = ho->hisaddr;
941 av_type = htonl(hisaddr);
942 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
944 /* Add user specified vp's */
946 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
948 if (rstate.acctserver) {
949 result = rc_acct_using_server(rstate.acctserver,
950 rstate.client_port, send);
952 result = rc_acct(rstate.client_port, send);
955 rc_avpair_free(send);
957 if (result != OK_RC) {
958 /* RADIUS server could be down so make this a warning */
960 "Accounting START failed for %s", rstate.user);
963 /* Kick off periodic accounting reports */
964 if (rstate.acct_interim_interval) {
965 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
969 /**********************************************************************
970 * %FUNCTION: radius_acct_stop
976 * Sends a "stop" accounting message to the RADIUS server.
977 ***********************************************************************/
979 radius_acct_stop(void)
982 VALUE_PAIR *send = NULL;
983 ipcp_options *ho = &ipcp_hisoptions[0];
987 if (!rstate.initialized) {
991 if (rstate.acct_interim_interval)
992 UNTIMEOUT(radius_acct_interim, NULL);
994 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
997 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
999 if (rstate.class_len > 0)
1000 rc_avpair_add(&send, PW_CLASS,
1001 rstate.class, rstate.class_len, VENDOR_NONE);
1003 av_type = PW_STATUS_STOP;
1004 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1006 av_type = PW_FRAMED;
1007 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1010 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1012 av_type = PW_RADIUS;
1013 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1016 if (link_stats_valid) {
1017 av_type = link_connect_time;
1018 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1020 av_type = link_stats.bytes_out & 0xFFFFFFFF;
1021 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1023 if (link_stats.bytes_out > 0xFFFFFFFF) {
1024 av_type = link_stats.bytes_out >> 32;
1025 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1028 av_type = link_stats.bytes_in & 0xFFFFFFFF;
1029 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1031 if (link_stats.bytes_in > 0xFFFFFFFF) {
1032 av_type = link_stats.bytes_in >> 32;
1033 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1036 av_type = link_stats.pkts_out;
1037 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1039 av_type = link_stats.pkts_in;
1040 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1043 if (*remote_number) {
1044 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1045 remote_number, 0, VENDOR_NONE);
1047 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1049 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1050 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1052 av_type = PW_NAS_ERROR;
1055 case EXIT_USER_REQUEST:
1056 av_type = PW_USER_REQUEST;
1060 case EXIT_PEER_DEAD:
1061 case EXIT_CONNECT_FAILED:
1062 av_type = PW_LOST_CARRIER;
1065 case EXIT_INIT_FAILED:
1066 case EXIT_OPEN_FAILED:
1067 case EXIT_LOCK_FAILED:
1068 case EXIT_PTYCMD_FAILED:
1069 av_type = PW_PORT_ERROR;
1072 case EXIT_PEER_AUTH_FAILED:
1073 case EXIT_AUTH_TOPEER_FAILED:
1074 case EXIT_NEGOTIATION_FAILED:
1075 case EXIT_CNID_AUTH_FAILED:
1076 av_type = PW_SERVICE_UNAVAILABLE;
1079 case EXIT_IDLE_TIMEOUT:
1080 av_type = PW_ACCT_IDLE_TIMEOUT;
1084 av_type = PW_CALLBACK;
1087 case EXIT_CONNECT_TIME:
1088 av_type = PW_ACCT_SESSION_TIMEOUT;
1091 #ifdef PPP_WITH_MAXOCTETS
1092 case EXIT_TRAFFIC_LIMIT:
1093 av_type = PW_NAS_REQUEST;
1098 av_type = PW_NAS_ERROR;
1101 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1103 hisaddr = ho->hisaddr;
1104 av_type = htonl(hisaddr);
1105 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1107 /* Add user specified vp's */
1109 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1111 if (rstate.acctserver) {
1112 result = rc_acct_using_server(rstate.acctserver,
1113 rstate.client_port, send);
1115 result = rc_acct(rstate.client_port, send);
1118 if (result != OK_RC) {
1119 /* RADIUS server could be down so make this a warning */
1121 "Accounting STOP failed for %s", rstate.user);
1123 rc_avpair_free(send);
1126 /**********************************************************************
1127 * %FUNCTION: radius_acct_interim
1133 * Sends an interim accounting message to the RADIUS server
1134 ***********************************************************************/
1136 radius_acct_interim(void *ignored)
1139 VALUE_PAIR *send = NULL;
1140 ipcp_options *ho = &ipcp_hisoptions[0];
1144 if (!rstate.initialized) {
1148 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1151 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1153 if (rstate.class_len > 0)
1154 rc_avpair_add(&send, PW_CLASS,
1155 rstate.class, rstate.class_len, VENDOR_NONE);
1157 av_type = PW_STATUS_ALIVE;
1158 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1160 av_type = PW_FRAMED;
1161 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1164 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1166 av_type = PW_RADIUS;
1167 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1169 /* Update link stats */
1170 update_link_stats(0);
1172 if (link_stats_valid) {
1173 link_stats_valid = 0; /* Force later code to update */
1175 av_type = link_connect_time;
1176 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1178 av_type = link_stats.bytes_out & 0xFFFFFFFF;
1179 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1181 if (link_stats.bytes_out > 0xFFFFFFFF) {
1182 av_type = link_stats.bytes_out >> 32;
1183 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1186 av_type = link_stats.bytes_in & 0xFFFFFFFF;
1187 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1189 if (link_stats.bytes_in > 0xFFFFFFFF) {
1190 av_type = link_stats.bytes_in >> 32;
1191 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1194 av_type = link_stats.pkts_out;
1195 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1197 av_type = link_stats.pkts_in;
1198 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1201 if (*remote_number) {
1202 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1203 remote_number, 0, VENDOR_NONE);
1205 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1207 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1208 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1210 hisaddr = ho->hisaddr;
1211 av_type = htonl(hisaddr);
1212 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1214 /* Add user specified vp's */
1216 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1218 if (rstate.acctserver) {
1219 result = rc_acct_using_server(rstate.acctserver,
1220 rstate.client_port, send);
1222 result = rc_acct(rstate.client_port, send);
1225 if (result != OK_RC) {
1226 /* RADIUS server could be down so make this a warning */
1228 "Interim accounting failed for %s", rstate.user);
1230 rc_avpair_free(send);
1232 /* Schedule another one */
1233 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1236 /**********************************************************************
1237 * %FUNCTION: radius_ip_up
1244 * Called when IPCP is up. We'll do a start-accounting record.
1245 ***********************************************************************/
1247 radius_ip_up(void *opaque, int arg)
1249 radius_acct_start();
1252 /**********************************************************************
1253 * %FUNCTION: radius_ip_down
1260 * Called when IPCP is down. We'll do a stop-accounting record.
1261 ***********************************************************************/
1263 radius_ip_down(void *opaque, int arg)
1268 /**********************************************************************
1269 * %FUNCTION: radius_init
1271 * msg -- buffer of size BUF_LEN for error message
1273 * negative on failure; non-negative on success
1275 * Initializes radiusclient library
1276 ***********************************************************************/
1278 radius_init(char *msg)
1280 if (rstate.initialized) {
1284 if (config_file && *config_file) {
1285 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1288 rstate.initialized = 1;
1290 if (rc_read_config(rstate.config_file) != 0) {
1291 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1292 rstate.config_file);
1296 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1297 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1298 rc_conf_str("dictionary"));
1302 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1303 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1304 rc_conf_str("mapfile"));
1308 /* Add av pairs saved during option parsing */
1310 struct avpopt *n = avpopt->next;
1312 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1313 free(avpopt->vpstr);
1320 /**********************************************************************
1321 * %FUNCTION: get_client_port
1323 * ifname -- PPP interface name (e.g. "ppp7")
1325 * The NAS port number (e.g. 7)
1327 * Extracts the port number from the interface name
1328 ***********************************************************************/
1330 get_client_port(char *ifname)
1333 if (sscanf(ifname, "ppp%d", &port) == 1) {
1336 return rc_map2id(ifname);
1339 /**********************************************************************
1340 * %FUNCTION: radius_allowed_address
1342 * addr -- IP address
1344 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1346 ***********************************************************************/
1348 radius_allowed_address(u_int32_t addr)
1350 ipcp_options *wo = &ipcp_wantoptions[0];
1352 if (!rstate.choose_ip) {
1353 /* If RADIUS server said any address is OK, then fine... */
1354 if (rstate.any_ip_addr_ok) {
1358 /* Sigh... if an address was supplied for remote host in pppd
1359 options, it has to match that. */
1360 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1366 if (addr == rstate.ip_addr) return 1;
1370 /* Useful for other plugins */
1371 char *radius_logged_in_user(void)