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>
37 #include <pppd/pppd.h>
38 #include <pppd/chap-new.h>
39 #ifdef PPP_WITH_CHAPMS
40 #include <pppd/chap_ms.h>
42 #include <pppd/mppe.h>
47 #include <pppd/ipcp.h>
49 #include "radiusclient.h"
53 #define MD5_HASH_SIZE 16
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 int radius_secret_check(void);
76 static int radius_pap_auth(char *user,
79 struct wordlist **paddrs,
80 struct wordlist **popts);
81 static int radius_chap_verify(char *user, char *ourname, int id,
82 struct chap_digest_type *digest,
83 unsigned char *challenge,
84 unsigned char *response,
85 char *message, int message_space);
87 static void radius_ip_up(void *opaque, int arg);
88 static void radius_ip_down(void *opaque, int arg);
89 static void make_username_realm(char *user);
90 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
91 struct chap_digest_type *digest,
92 unsigned char *challenge,
93 char *message, int message_space);
94 static void radius_choose_ip(u_int32_t *addrp);
95 static int radius_init(char *msg);
96 static int get_client_port(char *ifname);
97 static int radius_allowed_address(u_int32_t addr);
98 static void radius_acct_interim(void *);
100 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
102 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
106 #define MAXSESSIONID 32
110 #define MAXCLASSLEN 500
113 struct radius_state {
120 char user[MAXNAMELEN];
121 char config_file[MAXPATHLEN];
122 char session_id[MAXSESSIONID + 1];
124 int acct_interim_interval;
125 SERVER *authserver; /* Authentication server to use */
126 SERVER *acctserver; /* Accounting server to use */
128 char class[MAXCLASSLEN];
129 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
132 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
134 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
135 In that case, they override the values in the radiusclient.conf file */
136 void (*radius_pre_auth_hook)(char const *user,
138 SERVER **acctserver) = NULL;
140 static struct radius_state rstate;
142 char pppd_version[] = PPPD_VERSION;
144 /**********************************************************************
145 * %FUNCTION: plugin_init
151 * Initializes RADIUS plugin.
152 ***********************************************************************/
156 pap_check_hook = radius_secret_check;
157 pap_auth_hook = radius_pap_auth;
159 chap_check_hook = radius_secret_check;
160 chap_verify_hook = radius_chap_verify;
162 ip_choose_hook = radius_choose_ip;
163 allowed_address_hook = radius_allowed_address;
165 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
166 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
168 memset(&rstate, 0, sizeof(rstate));
170 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
171 sizeof(rstate.config_file));
173 add_options(Options);
175 info("RADIUS plugin initialized.");
178 /**********************************************************************
181 * argv -- the <attribute=value> pair to add
185 * Adds an av pair to be passed on to the RADIUS server on each request.
186 ***********************************************************************/
190 struct avpopt *p = malloc(sizeof(struct avpopt));
192 /* Append to a list of vp's for later parsing */
193 p->vpstr = strdup(*argv);
200 /**********************************************************************
201 * %FUNCTION: radius_secret_check
205 * 1 -- we are ALWAYS willing to supply a secret. :-)
207 * Tells pppd that we will try to authenticate the peer, and not to
208 * worry about looking in *-secrets file(s)
209 ***********************************************************************/
211 radius_secret_check(void)
216 /**********************************************************************
217 * %FUNCTION: radius_choose_ip
219 * addrp -- where to store the IP address
223 * If RADIUS server has specified an IP address, it is stored in *addrp.
224 ***********************************************************************/
226 radius_choose_ip(u_int32_t *addrp)
228 if (rstate.choose_ip) {
229 *addrp = rstate.ip_addr;
233 /**********************************************************************
234 * %FUNCTION: radius_pap_auth
236 * user -- user-name of peer
237 * passwd -- password supplied by peer
238 * msgp -- Message which will be sent in PAP response
239 * paddrs -- set to a list of possible peer IP addresses
240 * popts -- set to a list of additional pppd options
242 * 1 if we can authenticate, -1 if we cannot.
244 * Performs PAP authentication using RADIUS
245 ***********************************************************************/
247 radius_pap_auth(char *user,
250 struct wordlist **paddrs,
251 struct wordlist **popts)
253 VALUE_PAIR *send, *received;
256 static char radius_msg[BUF_LEN];
261 if (radius_init(radius_msg) < 0) {
265 /* Put user with potentially realm added in rstate.user */
266 make_username_realm(user);
268 if (radius_pre_auth_hook) {
269 radius_pre_auth_hook(rstate.user,
277 /* Hack... the "port" is the ppp interface number. Should really be
279 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
282 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
285 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
287 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
288 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
289 if (*remote_number) {
290 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
293 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
295 /* Add user specified vp's */
297 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
299 if (rstate.authserver) {
300 result = rc_auth_using_server(rstate.authserver,
301 rstate.client_port, send,
302 &received, radius_msg, NULL);
304 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
307 if (result == OK_RC) {
308 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
313 /* free value pairs */
314 rc_avpair_free(received);
315 rc_avpair_free(send);
317 return (result == OK_RC) ? 1 : 0;
320 /**********************************************************************
321 * %FUNCTION: radius_chap_verify
323 * user -- name of the peer
324 * ourname -- name for this machine
325 * id -- the ID byte in the challenge
326 * digest -- points to the structure representing the digest type
327 * challenge -- the challenge string we sent (length in first byte)
328 * response -- the response (hash) the peer sent back (length in 1st byte)
329 * message -- space for a message to be returned to the peer
330 * message_space -- number of bytes available at *message.
332 * 1 if the response is good, 0 if it is bad
334 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
335 ***********************************************************************/
337 radius_chap_verify(char *user, char *ourname, int id,
338 struct chap_digest_type *digest,
339 unsigned char *challenge, unsigned char *response,
340 char *message, int message_space)
342 VALUE_PAIR *send, *received;
344 static char radius_msg[BUF_LEN];
346 int challenge_len, response_len;
347 u_char cpassword[MAX_RESPONSE_LEN + 1];
349 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
350 REQUEST_INFO request_info, *req_info = &request_info;
352 REQUEST_INFO *req_info = NULL;
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 ? devnam : 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_HASH_SIZE)
406 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
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_HASH_SIZE + 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 if (*remote_number) {
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(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 maxconnect = vp->lvalue;
598 /* packet filter, will be handled via ip-(up|down) script */
599 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 script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
605 case PW_IDLE_TIMEOUT:
607 idle_time_limit = vp->lvalue;
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 ;
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 (bad_ip_adrs (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) {
651 rstate.class_len=vp->lvalue;
652 memcpy(rstate.class, vp->strvalue, rstate.class_len);
653 } /* else too big for our buffer - ignore it */
656 netif_set_mtu(rstate.client_port,MIN(netif_get_mtu(rstate.client_port),vp->lvalue));
661 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
662 #ifdef PPP_WITH_CHAPMS
663 switch (vp->attribute) {
664 case PW_MS_CHAP2_SUCCESS:
665 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
666 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
670 strlcpy(message, (char*) vp->strvalue + 1, message_space);
671 ms_chap2_success = 1;
675 case PW_MS_CHAP_MPPE_KEYS:
676 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
677 slprintf(msg, BUF_LEN,
678 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
684 case PW_MS_MPPE_SEND_KEY:
685 case PW_MS_MPPE_RECV_KEY:
686 if (radius_setmppekeys2(vp, req_info) < 0) {
687 slprintf(msg, BUF_LEN,
688 "RADIUS: bad MS-MPPE-%s-Key attribute",
689 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
696 case PW_MS_MPPE_ENCRYPTION_POLICY:
697 mppe_enc_policy = vp->lvalue; /* save for later */
700 case PW_MS_MPPE_ENCRYPTION_TYPES:
701 mppe_enc_types = vp->lvalue; /* save for later */
704 #endif /* PPP_WITH_MPPE */
706 case PW_MS_PRIMARY_DNS_SERVER:
707 ao->dnsaddr[0] = htonl(vp->lvalue);
710 ao->dnsaddr[1] = ao->dnsaddr[0];
712 case PW_MS_SECONDARY_DNS_SERVER:
713 ao->dnsaddr[1] = htonl(vp->lvalue);
716 ao->dnsaddr[0] = ao->dnsaddr[1];
718 case PW_MS_PRIMARY_NBNS_SERVER:
719 ao->winsaddr[0] = htonl(vp->lvalue);
722 ao->winsaddr[1] = ao->winsaddr[0];
724 case PW_MS_SECONDARY_NBNS_SERVER:
725 ao->winsaddr[1] = htonl(vp->lvalue);
728 ao->winsaddr[0] = ao->winsaddr[1];
732 #endif /* PPP_WITH_CHAPMS */
737 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
738 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
743 * Require both policy and key attributes to indicate a valid key.
744 * Note that if the policy value was '0' we don't set the key!
746 if (mppe_enc_policy && mppe_enc_keys) {
747 /* Set/modify allowed encryption types. */
749 mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
759 /**********************************************************************
760 * %FUNCTION: radius_setmppekeys
762 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
763 * req_info -- radius request information used for encryption
765 * >= 0 on success; -1 on failure
767 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
769 ***********************************************************************/
771 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
772 unsigned char *challenge)
779 if (vp->lvalue != 32) {
780 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
785 memcpy(plain, vp->strvalue, sizeof(plain));
788 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
789 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
790 MD5_Final(buf, &Context);
792 for (i = 0; i < 16; i++)
796 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
797 MD5_Update(&Context, vp->strvalue, 16);
798 MD5_Final(buf, &Context);
800 for(i = 0; i < 16; i++)
801 plain[i + 16] ^= buf[i];
804 * Annoying. The "key" returned is just the NTPasswordHashHash, which
805 * the NAS (us) doesn't need; we only need the start key. So we have
806 * to generate the start key, sigh. NB: We do not support the LM-Key.
808 mppe_set_chapv1(challenge, &plain[8]);
813 /**********************************************************************
814 * %FUNCTION: radius_setmppekeys2
816 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
817 * req_info -- radius request information used for encryption
819 * >= 0 on success; -1 on failure
821 * Decrypt the key provided by the RADIUS server for MPPE encryption.
823 ***********************************************************************/
825 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
829 u_char *salt = vp->strvalue;
830 u_char *crypt = vp->strvalue + 2;
832 u_char buf[MD5_HASH_SIZE];
835 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
838 if (vp->lvalue != 34) {
839 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
844 if ((salt[0] & 0x80) == 0) {
845 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
849 memcpy(plain, crypt, 32);
852 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
853 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
854 MD5_Update(&Context, salt, 2);
855 MD5_Final(buf, &Context);
857 for (i = 0; i < 16; i++)
860 if (plain[0] != 16) {
861 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
862 (int) plain[0], type);
867 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
868 MD5_Update(&Context, crypt, 16);
869 MD5_Final(buf, &Context);
871 plain[16] ^= buf[0]; /* only need the first byte */
873 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
874 mppe_set_keys(plain + 1, NULL, 16);
876 mppe_set_keys(NULL, plain + 1, 16);
880 #endif /* PPP_WITH_MPPE */
882 /**********************************************************************
883 * %FUNCTION: radius_acct_start
889 * Sends a "start" accounting message to the RADIUS server.
890 ***********************************************************************/
892 radius_acct_start(void)
896 VALUE_PAIR *send = NULL;
897 ipcp_options *ho = &ipcp_hisoptions[0];
900 if (!rstate.initialized) {
904 rstate.start_time = time(NULL);
906 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
908 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
909 rstate.session_id, 0, VENDOR_NONE);
910 rc_avpair_add(&send, PW_USER_NAME,
911 rstate.user, 0, VENDOR_NONE);
913 if (rstate.class_len > 0)
914 rc_avpair_add(&send, PW_CLASS,
915 rstate.class, rstate.class_len, VENDOR_NONE);
917 av_type = PW_STATUS_START;
918 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
921 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
924 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
926 if (*remote_number) {
927 rc_avpair_add(&send, PW_CALLING_STATION_ID,
928 remote_number, 0, VENDOR_NONE);
930 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
933 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
936 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
937 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
939 hisaddr = ho->hisaddr;
940 av_type = htonl(hisaddr);
941 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
943 /* Add user specified vp's */
945 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
947 if (rstate.acctserver) {
948 result = rc_acct_using_server(rstate.acctserver,
949 rstate.client_port, send);
951 result = rc_acct(rstate.client_port, send);
954 rc_avpair_free(send);
956 if (result != OK_RC) {
957 /* RADIUS server could be down so make this a warning */
959 "Accounting START failed for %s", rstate.user);
962 /* Kick off periodic accounting reports */
963 if (rstate.acct_interim_interval) {
964 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
968 /**********************************************************************
969 * %FUNCTION: radius_acct_stop
975 * Sends a "stop" accounting message to the RADIUS server.
976 ***********************************************************************/
978 radius_acct_stop(void)
981 VALUE_PAIR *send = NULL;
982 ipcp_options *ho = &ipcp_hisoptions[0];
986 if (!rstate.initialized) {
990 if (rstate.acct_interim_interval)
991 UNTIMEOUT(radius_acct_interim, NULL);
993 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
996 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
998 if (rstate.class_len > 0)
999 rc_avpair_add(&send, PW_CLASS,
1000 rstate.class, rstate.class_len, VENDOR_NONE);
1002 av_type = PW_STATUS_STOP;
1003 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1005 av_type = PW_FRAMED;
1006 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1009 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1011 av_type = PW_RADIUS;
1012 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1015 if (link_stats_valid) {
1016 av_type = link_connect_time;
1017 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1019 av_type = link_stats.bytes_out & 0xFFFFFFFF;
1020 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1022 if (link_stats.bytes_out > 0xFFFFFFFF) {
1023 av_type = link_stats.bytes_out >> 32;
1024 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1027 av_type = link_stats.bytes_in & 0xFFFFFFFF;
1028 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1030 if (link_stats.bytes_in > 0xFFFFFFFF) {
1031 av_type = link_stats.bytes_in >> 32;
1032 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1035 av_type = link_stats.pkts_out;
1036 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1038 av_type = link_stats.pkts_in;
1039 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1042 if (*remote_number) {
1043 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1044 remote_number, 0, VENDOR_NONE);
1046 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1048 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1049 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1051 av_type = PW_NAS_ERROR;
1054 case EXIT_USER_REQUEST:
1055 av_type = PW_USER_REQUEST;
1059 case EXIT_PEER_DEAD:
1060 case EXIT_CONNECT_FAILED:
1061 av_type = PW_LOST_CARRIER;
1064 case EXIT_INIT_FAILED:
1065 case EXIT_OPEN_FAILED:
1066 case EXIT_LOCK_FAILED:
1067 case EXIT_PTYCMD_FAILED:
1068 av_type = PW_PORT_ERROR;
1071 case EXIT_PEER_AUTH_FAILED:
1072 case EXIT_AUTH_TOPEER_FAILED:
1073 case EXIT_NEGOTIATION_FAILED:
1074 case EXIT_CNID_AUTH_FAILED:
1075 av_type = PW_SERVICE_UNAVAILABLE;
1078 case EXIT_IDLE_TIMEOUT:
1079 av_type = PW_ACCT_IDLE_TIMEOUT;
1083 av_type = PW_CALLBACK;
1086 case EXIT_CONNECT_TIME:
1087 av_type = PW_ACCT_SESSION_TIMEOUT;
1090 case EXIT_TRAFFIC_LIMIT:
1091 av_type = PW_NAS_REQUEST;
1095 av_type = PW_NAS_ERROR;
1098 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1100 hisaddr = ho->hisaddr;
1101 av_type = htonl(hisaddr);
1102 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1104 /* Add user specified vp's */
1106 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1108 if (rstate.acctserver) {
1109 result = rc_acct_using_server(rstate.acctserver,
1110 rstate.client_port, send);
1112 result = rc_acct(rstate.client_port, send);
1115 if (result != OK_RC) {
1116 /* RADIUS server could be down so make this a warning */
1118 "Accounting STOP failed for %s", rstate.user);
1120 rc_avpair_free(send);
1123 /**********************************************************************
1124 * %FUNCTION: radius_acct_interim
1130 * Sends an interim accounting message to the RADIUS server
1131 ***********************************************************************/
1133 radius_acct_interim(void *ignored)
1136 VALUE_PAIR *send = NULL;
1137 ipcp_options *ho = &ipcp_hisoptions[0];
1141 if (!rstate.initialized) {
1145 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1148 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1150 if (rstate.class_len > 0)
1151 rc_avpair_add(&send, PW_CLASS,
1152 rstate.class, rstate.class_len, VENDOR_NONE);
1154 av_type = PW_STATUS_ALIVE;
1155 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1157 av_type = PW_FRAMED;
1158 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1161 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1163 av_type = PW_RADIUS;
1164 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1166 /* Update link stats */
1167 update_link_stats(0);
1169 if (link_stats_valid) {
1170 link_stats_valid = 0; /* Force later code to update */
1172 av_type = link_connect_time;
1173 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1175 av_type = link_stats.bytes_out & 0xFFFFFFFF;
1176 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1178 if (link_stats.bytes_out > 0xFFFFFFFF) {
1179 av_type = link_stats.bytes_out >> 32;
1180 rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1183 av_type = link_stats.bytes_in & 0xFFFFFFFF;
1184 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1186 if (link_stats.bytes_in > 0xFFFFFFFF) {
1187 av_type = link_stats.bytes_in >> 32;
1188 rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1191 av_type = link_stats.pkts_out;
1192 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1194 av_type = link_stats.pkts_in;
1195 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1198 if (*remote_number) {
1199 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1200 remote_number, 0, VENDOR_NONE);
1202 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1204 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1205 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &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 "Interim accounting failed for %s", rstate.user);
1227 rc_avpair_free(send);
1229 /* Schedule another one */
1230 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1233 /**********************************************************************
1234 * %FUNCTION: radius_ip_up
1241 * Called when IPCP is up. We'll do a start-accounting record.
1242 ***********************************************************************/
1244 radius_ip_up(void *opaque, int arg)
1246 radius_acct_start();
1249 /**********************************************************************
1250 * %FUNCTION: radius_ip_down
1257 * Called when IPCP is down. We'll do a stop-accounting record.
1258 ***********************************************************************/
1260 radius_ip_down(void *opaque, int arg)
1265 /**********************************************************************
1266 * %FUNCTION: radius_init
1268 * msg -- buffer of size BUF_LEN for error message
1270 * negative on failure; non-negative on success
1272 * Initializes radiusclient library
1273 ***********************************************************************/
1275 radius_init(char *msg)
1277 if (rstate.initialized) {
1281 if (config_file && *config_file) {
1282 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1285 rstate.initialized = 1;
1287 if (rc_read_config(rstate.config_file) != 0) {
1288 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1289 rstate.config_file);
1293 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1294 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1295 rc_conf_str("dictionary"));
1299 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1300 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1301 rc_conf_str("mapfile"));
1305 /* Add av pairs saved during option parsing */
1307 struct avpopt *n = avpopt->next;
1309 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1310 free(avpopt->vpstr);
1317 /**********************************************************************
1318 * %FUNCTION: get_client_port
1320 * ifname -- PPP interface name (e.g. "ppp7")
1322 * The NAS port number (e.g. 7)
1324 * Extracts the port number from the interface name
1325 ***********************************************************************/
1327 get_client_port(char *ifname)
1330 if (sscanf(ifname, "ppp%d", &port) == 1) {
1333 return rc_map2id(ifname);
1336 /**********************************************************************
1337 * %FUNCTION: radius_allowed_address
1339 * addr -- IP address
1341 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1343 ***********************************************************************/
1345 radius_allowed_address(u_int32_t addr)
1347 ipcp_options *wo = &ipcp_wantoptions[0];
1349 if (!rstate.choose_ip) {
1350 /* If RADIUS server said any address is OK, then fine... */
1351 if (rstate.any_ip_addr_ok) {
1355 /* Sigh... if an address was supplied for remote host in pppd
1356 options, it has to match that. */
1357 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1363 if (addr == rstate.ip_addr) return 1;
1367 /* Useful for other plugins */
1368 char *radius_logged_in_user(void)