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 $";
42 #include "radiusclient.h"
46 #include <sys/types.h>
49 #include <netinet/in.h>
54 #define MD5_HASH_SIZE 16
58 static char *config_file = NULL;
59 static int add_avp(char **);
60 static struct avpopt {
64 static bool portnummap = 0;
66 static option_t Options[] = {
67 { "radius-config-file", o_string, &config_file },
68 { "avpair", o_special, add_avp },
69 { "map-to-ttyname", o_bool, &portnummap,
70 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
71 { "map-to-ifname", o_bool, &portnummap,
72 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
76 static int radius_secret_check(void);
77 static int radius_pap_auth(char *user,
80 struct wordlist **paddrs,
81 struct wordlist **popts);
82 static int radius_chap_verify(char *user, char *ourname, int id,
83 struct chap_digest_type *digest,
84 unsigned char *challenge,
85 unsigned char *response,
86 char *message, int message_space);
88 static void radius_ip_up(void *opaque, int arg);
89 static void radius_ip_down(void *opaque, int arg);
90 static void make_username_realm(char *user);
91 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
92 struct chap_digest_type *digest,
93 unsigned char *challenge,
94 char *message, int message_space);
95 static void radius_choose_ip(u_int32_t *addrp);
96 static int radius_init(char *msg);
97 static int get_client_port(char *ifname);
98 static int radius_allowed_address(u_int32_t addr);
99 static void radius_acct_interim(void *);
101 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
103 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
107 #define MAXSESSIONID 32
111 #define MAXCLASSLEN 500
114 struct radius_state {
115 int accounting_started;
122 char user[MAXNAMELEN];
123 char config_file[MAXPATHLEN];
124 char session_id[MAXSESSIONID + 1];
126 int acct_interim_interval;
127 SERVER *authserver; /* Authentication server to use */
128 SERVER *acctserver; /* Accounting server to use */
130 char class[MAXCLASSLEN];
131 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
134 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
136 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
137 In that case, they override the values in the radiusclient.conf file */
138 void (*radius_pre_auth_hook)(char const *user,
140 SERVER **acctserver) = NULL;
142 static struct radius_state rstate;
144 char pppd_version[] = VERSION;
146 /**********************************************************************
147 * %FUNCTION: plugin_init
153 * Initializes RADIUS plugin.
154 ***********************************************************************/
158 pap_check_hook = radius_secret_check;
159 pap_auth_hook = radius_pap_auth;
161 chap_check_hook = radius_secret_check;
162 chap_verify_hook = radius_chap_verify;
164 ip_choose_hook = radius_choose_ip;
165 allowed_address_hook = radius_allowed_address;
167 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
168 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
170 memset(&rstate, 0, sizeof(rstate));
172 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
173 sizeof(rstate.config_file));
175 add_options(Options);
177 info("RADIUS plugin initialized.");
180 /**********************************************************************
183 * argv -- the <attribute=value> pair to add
187 * Adds an av pair to be passed on to the RADIUS server on each request.
188 ***********************************************************************/
192 struct avpopt *p = malloc(sizeof(struct avpopt));
194 /* Append to a list of vp's for later parsing */
195 p->vpstr = strdup(*argv);
202 /**********************************************************************
203 * %FUNCTION: radius_secret_check
207 * 1 -- we are ALWAYS willing to supply a secret. :-)
209 * Tells pppd that we will try to authenticate the peer, and not to
210 * worry about looking in /etc/ppp/*-secrets
211 ***********************************************************************/
213 radius_secret_check(void)
218 /**********************************************************************
219 * %FUNCTION: radius_choose_ip
221 * addrp -- where to store the IP address
225 * If RADIUS server has specified an IP address, it is stored in *addrp.
226 ***********************************************************************/
228 radius_choose_ip(u_int32_t *addrp)
230 if (rstate.choose_ip) {
231 *addrp = rstate.ip_addr;
235 /**********************************************************************
236 * %FUNCTION: radius_pap_auth
238 * user -- user-name of peer
239 * passwd -- password supplied by peer
240 * msgp -- Message which will be sent in PAP response
241 * paddrs -- set to a list of possible peer IP addresses
242 * popts -- set to a list of additional pppd options
244 * 1 if we can authenticate, -1 if we cannot.
246 * Performs PAP authentication using RADIUS
247 ***********************************************************************/
249 radius_pap_auth(char *user,
252 struct wordlist **paddrs,
253 struct wordlist **popts)
255 VALUE_PAIR *send, *received;
258 static char radius_msg[BUF_LEN];
263 if (radius_init(radius_msg) < 0) {
267 /* Put user with potentially realm added in rstate.user */
268 make_username_realm(user);
270 if (radius_pre_auth_hook) {
271 radius_pre_auth_hook(rstate.user,
279 /* Hack... the "port" is the ppp interface number. Should really be
281 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
284 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
287 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
289 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
290 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
291 if (*remote_number) {
292 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
295 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
297 /* Add user specified vp's */
299 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
301 if (rstate.authserver) {
302 result = rc_auth_using_server(rstate.authserver,
303 rstate.client_port, send,
304 &received, radius_msg, NULL);
306 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
309 if (result == OK_RC) {
310 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
315 /* free value pairs */
316 rc_avpair_free(received);
317 rc_avpair_free(send);
319 return (result == OK_RC) ? 1 : 0;
322 /**********************************************************************
323 * %FUNCTION: radius_chap_verify
325 * user -- name of the peer
326 * ourname -- name for this machine
327 * id -- the ID byte in the challenge
328 * digest -- points to the structure representing the digest type
329 * challenge -- the challenge string we sent (length in first byte)
330 * response -- the response (hash) the peer sent back (length in 1st byte)
331 * message -- space for a message to be returned to the peer
332 * message_space -- number of bytes available at *message.
334 * 1 if the response is good, 0 if it is bad
336 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
337 ***********************************************************************/
339 radius_chap_verify(char *user, char *ourname, int id,
340 struct chap_digest_type *digest,
341 unsigned char *challenge, unsigned char *response,
342 char *message, int message_space)
344 VALUE_PAIR *send, *received;
346 static char radius_msg[BUF_LEN];
348 int challenge_len, response_len;
349 u_char cpassword[MAX_RESPONSE_LEN + 1];
351 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
352 REQUEST_INFO request_info, *req_info = &request_info;
354 REQUEST_INFO *req_info = NULL;
357 challenge_len = *challenge++;
358 response_len = *response++;
362 if (radius_init(radius_msg) < 0) {
363 error("%s", radius_msg);
367 /* return error for types we can't handle */
368 if ((digest->code != CHAP_MD5)
370 && (digest->code != CHAP_MICROSOFT)
371 && (digest->code != CHAP_MICROSOFT_V2)
374 error("RADIUS: Challenge type %u unsupported", digest->code);
378 /* Put user with potentially realm added in rstate.user */
379 if (!rstate.done_chap_once) {
380 make_username_realm(user);
381 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
382 if (radius_pre_auth_hook) {
383 radius_pre_auth_hook(rstate.user,
389 send = received = NULL;
392 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
395 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
397 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
400 * add the challenge and response fields
402 switch (digest->code) {
404 /* CHAP-Challenge and CHAP-Password */
405 if (response_len != MD5_HASH_SIZE)
408 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
410 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
411 challenge, challenge_len, VENDOR_NONE);
412 rc_avpair_add(&send, PW_CHAP_PASSWORD,
413 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
419 /* MS-CHAP-Challenge and MS-CHAP-Response */
420 u_char *p = cpassword;
422 if (response_len != MS_CHAP_RESPONSE_LEN)
425 /* The idiots use a different field order in RADIUS than PPP */
426 *p++ = response[MS_CHAP_USENT];
427 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
429 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
430 challenge, challenge_len, VENDOR_MICROSOFT);
431 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
432 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
436 case CHAP_MICROSOFT_V2:
438 /* MS-CHAP-Challenge and MS-CHAP2-Response */
439 u_char *p = cpassword;
441 if (response_len != MS_CHAP2_RESPONSE_LEN)
444 /* The idiots use a different field order in RADIUS than PPP */
445 *p++ = response[MS_CHAP2_FLAGS];
446 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
447 + MS_CHAP2_NTRESP_LEN));
449 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
450 challenge, challenge_len, VENDOR_MICROSOFT);
451 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
452 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
458 if (*remote_number) {
459 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
462 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
464 /* Add user specified vp's */
466 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
469 * make authentication with RADIUS server
472 if (rstate.authserver) {
473 result = rc_auth_using_server(rstate.authserver,
474 rstate.client_port, send,
475 &received, radius_msg, req_info);
477 result = rc_auth(rstate.client_port, send, &received, radius_msg,
481 strlcpy(message, radius_msg, message_space);
483 if (result == OK_RC) {
484 if (!rstate.done_chap_once) {
485 if (radius_setparams(received, radius_msg, req_info, digest,
486 challenge, message, message_space) < 0) {
487 error("%s", radius_msg);
490 rstate.done_chap_once = 1;
495 rc_avpair_free(received);
496 rc_avpair_free (send);
497 return (result == OK_RC);
500 /**********************************************************************
501 * %FUNCTION: make_username_realm
503 * user -- the user given to pppd
507 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
508 * then the default realm from the radiusclient config file is added.
509 ***********************************************************************/
511 make_username_realm(char *user)
515 if ( user != NULL ) {
516 strlcpy(rstate.user, user, sizeof(rstate.user));
521 default_realm = rc_conf_str("default_realm");
523 if (!strchr(rstate.user, '@') &&
525 (*default_realm != '\0')) {
526 strlcat(rstate.user, "@", sizeof(rstate.user));
527 strlcat(rstate.user, default_realm, sizeof(rstate.user));
531 /**********************************************************************
532 * %FUNCTION: radius_setparams
534 * vp -- received value-pairs
535 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
537 * >= 0 on success; -1 on failure
539 * Parses attributes sent by RADIUS server and sets them in pppd.
540 ***********************************************************************/
542 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
543 struct chap_digest_type *digest, unsigned char *challenge,
544 char *message, int message_space)
547 int ms_chap2_success = 0;
549 int mppe_enc_keys = 0; /* whether or not these were received */
550 int mppe_enc_policy = 0;
551 int mppe_enc_types = 0;
554 ipcp_options *wo = &ipcp_wantoptions[0];
555 ipcp_options *ao = &ipcp_allowoptions[0];
562 /* Send RADIUS attributes to anyone else who might be interested */
563 if (radius_attributes_hook) {
564 (*radius_attributes_hook)(vp);
568 * service type (if not framed then quit),
569 * new IP address (RADIUS can define static IP for some users),
573 if (vp->vendorcode == VENDOR_NONE) {
574 switch (vp->attribute) {
575 case PW_SERVICE_TYPE:
576 /* check for service type */
577 /* if not FRAMED then exit */
578 if (vp->lvalue != PW_FRAMED) {
579 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
580 vp->lvalue, rstate.user);
585 case PW_FRAMED_PROTOCOL:
586 /* check for framed protocol type */
587 /* if not PPP then also exit */
588 if (vp->lvalue != PW_PPP) {
589 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
590 vp->lvalue, rstate.user);
595 case PW_SESSION_TIMEOUT:
596 /* Session timeout */
597 maxconnect = vp->lvalue;
600 /* packet filter, will be handled via ip-(up|down) script */
601 script_setenv("RADIUS_FILTER_ID", vp->strvalue, 1);
603 case PW_FRAMED_ROUTE:
604 /* route, will be handled via ip-(up|down) script */
605 script_setenv("RADIUS_FRAMED_ROUTE", vp->strvalue, 1);
607 case PW_IDLE_TIMEOUT:
609 idle_time_limit = vp->lvalue;
612 case PW_SESSION_OCTETS_LIMIT:
613 /* Session traffic limit */
614 maxoctets = vp->lvalue;
616 case PW_OCTETS_DIRECTION:
617 /* Session traffic limit direction check */
618 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
621 case PW_ACCT_INTERIM_INTERVAL:
622 /* Send accounting updates every few seconds */
623 rstate.acct_interim_interval = vp->lvalue;
624 /* RFC says it MUST NOT be less than 60 seconds */
625 /* We use "0" to signify not sending updates */
626 if (rstate.acct_interim_interval &&
627 rstate.acct_interim_interval < 60) {
628 rstate.acct_interim_interval = 60;
631 case PW_FRAMED_IP_ADDRESS:
632 /* seting up remote IP addresses */
634 if (remote == 0xffffffff) {
635 /* 0xffffffff means user should be allowed to select one */
636 rstate.any_ip_addr_ok = 1;
637 } else if (remote != 0xfffffffe) {
638 /* 0xfffffffe means NAS should select an ip address */
639 remote = htonl(vp->lvalue);
640 if (bad_ip_adrs (remote)) {
641 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
642 remote, rstate.user);
645 rstate.choose_ip = 1;
646 rstate.ip_addr = remote;
649 case PW_NAS_IP_ADDRESS:
650 wo->ouraddr = htonl(vp->lvalue);
653 /* Save Class attribute to pass it in accounting request */
654 if (vp->lvalue <= MAXCLASSLEN) {
655 rstate.class_len=vp->lvalue;
656 memcpy(rstate.class, vp->strvalue, rstate.class_len);
657 } /* else too big for our buffer - ignore it */
660 netif_set_mtu(rstate.client_port,MIN(netif_get_mtu(rstate.client_port),vp->lvalue));
665 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
667 switch (vp->attribute) {
668 case PW_MS_CHAP2_SUCCESS:
669 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
670 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
674 strlcpy(message, vp->strvalue + 1, message_space);
675 ms_chap2_success = 1;
679 case PW_MS_CHAP_MPPE_KEYS:
680 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
681 slprintf(msg, BUF_LEN,
682 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
688 case PW_MS_MPPE_SEND_KEY:
689 case PW_MS_MPPE_RECV_KEY:
690 if (radius_setmppekeys2(vp, req_info) < 0) {
691 slprintf(msg, BUF_LEN,
692 "RADIUS: bad MS-MPPE-%s-Key attribute",
693 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
700 case PW_MS_MPPE_ENCRYPTION_POLICY:
701 mppe_enc_policy = vp->lvalue; /* save for later */
704 case PW_MS_MPPE_ENCRYPTION_TYPES:
705 mppe_enc_types = vp->lvalue; /* save for later */
710 case PW_MS_PRIMARY_DNS_SERVER:
711 ao->dnsaddr[0] = htonl(vp->lvalue);
714 ao->dnsaddr[1] = ao->dnsaddr[0];
716 case PW_MS_SECONDARY_DNS_SERVER:
717 ao->dnsaddr[1] = htonl(vp->lvalue);
720 ao->dnsaddr[0] = ao->dnsaddr[1];
722 case PW_MS_PRIMARY_NBNS_SERVER:
723 ao->winsaddr[0] = htonl(vp->lvalue);
726 ao->winsaddr[1] = ao->winsaddr[0];
728 case PW_MS_SECONDARY_NBNS_SERVER:
729 ao->winsaddr[1] = htonl(vp->lvalue);
732 ao->winsaddr[0] = ao->winsaddr[1];
741 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
742 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
747 * Require both policy and key attributes to indicate a valid key.
748 * Note that if the policy value was '0' we don't set the key!
750 if (mppe_enc_policy && mppe_enc_keys) {
751 /* Set/modify allowed encryption types. */
753 mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
763 /**********************************************************************
764 * %FUNCTION: radius_setmppekeys
766 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
767 * req_info -- radius request information used for encryption
769 * >= 0 on success; -1 on failure
771 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
773 ***********************************************************************/
775 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
776 unsigned char *challenge)
783 if (vp->lvalue != 32) {
784 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
789 memcpy(plain, vp->strvalue, sizeof(plain));
792 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
793 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
794 MD5_Final(buf, &Context);
796 for (i = 0; i < 16; i++)
800 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
801 MD5_Update(&Context, vp->strvalue, 16);
802 MD5_Final(buf, &Context);
804 for(i = 0; i < 16; i++)
805 plain[i + 16] ^= buf[i];
808 * Annoying. The "key" returned is just the NTPasswordHashHash, which
809 * the NAS (us) doesn't need; we only need the start key. So we have
810 * to generate the start key, sigh. NB: We do not support the LM-Key.
812 mppe_set_chapv1(challenge, &plain[8]);
817 /**********************************************************************
818 * %FUNCTION: radius_setmppekeys2
820 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
821 * req_info -- radius request information used for encryption
823 * >= 0 on success; -1 on failure
825 * Decrypt the key provided by the RADIUS server for MPPE encryption.
827 ***********************************************************************/
829 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
833 u_char *salt = vp->strvalue;
834 u_char *crypt = vp->strvalue + 2;
836 u_char buf[MD5_HASH_SIZE];
839 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
842 if (vp->lvalue != 34) {
843 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
848 if ((salt[0] & 0x80) == 0) {
849 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
853 memcpy(plain, crypt, 32);
856 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
857 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
858 MD5_Update(&Context, salt, 2);
859 MD5_Final(buf, &Context);
861 for (i = 0; i < 16; i++)
864 if (plain[0] != 16) {
865 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
866 (int) plain[0], type);
871 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
872 MD5_Update(&Context, crypt, 16);
873 MD5_Final(buf, &Context);
875 plain[16] ^= buf[0]; /* only need the first byte */
877 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
878 mppe_set_keys(plain + 1, NULL, 16);
880 mppe_set_keys(NULL, plain + 1, 16);
886 /**********************************************************************
887 * %FUNCTION: radius_acct_start
893 * Sends a "start" accounting message to the RADIUS server.
894 ***********************************************************************/
896 radius_acct_start(void)
900 VALUE_PAIR *send = NULL;
901 ipcp_options *ho = &ipcp_hisoptions[0];
904 if (!rstate.initialized) {
908 rstate.start_time = time(NULL);
910 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
912 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
913 rstate.session_id, 0, VENDOR_NONE);
914 rc_avpair_add(&send, PW_USER_NAME,
915 rstate.user, 0, VENDOR_NONE);
917 if (rstate.class_len > 0)
918 rc_avpair_add(&send, PW_CLASS,
919 rstate.class, rstate.class_len, VENDOR_NONE);
921 av_type = PW_STATUS_START;
922 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
925 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
928 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
930 if (*remote_number) {
931 rc_avpair_add(&send, PW_CALLING_STATION_ID,
932 remote_number, 0, VENDOR_NONE);
934 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
937 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
940 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
941 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
943 hisaddr = ho->hisaddr;
944 av_type = htonl(hisaddr);
945 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
947 /* Add user specified vp's */
949 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
951 if (rstate.acctserver) {
952 result = rc_acct_using_server(rstate.acctserver,
953 rstate.client_port, send);
955 result = rc_acct(rstate.client_port, send);
958 rc_avpair_free(send);
960 if (result != OK_RC) {
961 /* RADIUS server could be down so make this a warning */
963 "Accounting START failed for %s", rstate.user);
965 rstate.accounting_started = 1;
966 /* Kick off periodic accounting reports */
967 if (rstate.acct_interim_interval) {
968 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
973 /**********************************************************************
974 * %FUNCTION: radius_acct_stop
980 * Sends a "stop" accounting message to the RADIUS server.
981 ***********************************************************************/
983 radius_acct_stop(void)
986 VALUE_PAIR *send = NULL;
987 ipcp_options *ho = &ipcp_hisoptions[0];
991 if (!rstate.initialized) {
995 if (!rstate.accounting_started) {
999 if (rstate.acct_interim_interval)
1000 UNTIMEOUT(radius_acct_interim, NULL);
1002 rstate.accounting_started = 0;
1003 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1006 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1008 if (rstate.class_len > 0)
1009 rc_avpair_add(&send, PW_CLASS,
1010 rstate.class, rstate.class_len, VENDOR_NONE);
1012 av_type = PW_STATUS_STOP;
1013 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1015 av_type = PW_FRAMED;
1016 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1019 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1021 av_type = PW_RADIUS;
1022 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1025 if (link_stats_valid) {
1026 av_type = link_connect_time;
1027 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1029 av_type = link_stats.bytes_out;
1030 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1032 av_type = link_stats.bytes_in;
1033 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &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;
1091 case EXIT_TRAFFIC_LIMIT:
1092 av_type = PW_NAS_REQUEST;
1097 av_type = PW_NAS_ERROR;
1100 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1102 hisaddr = ho->hisaddr;
1103 av_type = htonl(hisaddr);
1104 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1106 /* Add user specified vp's */
1108 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1110 if (rstate.acctserver) {
1111 result = rc_acct_using_server(rstate.acctserver,
1112 rstate.client_port, send);
1114 result = rc_acct(rstate.client_port, send);
1117 if (result != OK_RC) {
1118 /* RADIUS server could be down so make this a warning */
1120 "Accounting STOP failed for %s", rstate.user);
1122 rc_avpair_free(send);
1125 /**********************************************************************
1126 * %FUNCTION: radius_acct_interim
1132 * Sends an interim accounting message to the RADIUS server
1133 ***********************************************************************/
1135 radius_acct_interim(void *ignored)
1138 VALUE_PAIR *send = NULL;
1139 ipcp_options *ho = &ipcp_hisoptions[0];
1143 if (!rstate.initialized) {
1147 if (!rstate.accounting_started) {
1151 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1154 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1156 if (rstate.class_len > 0)
1157 rc_avpair_add(&send, PW_CLASS,
1158 rstate.class, rstate.class_len, VENDOR_NONE);
1160 av_type = PW_STATUS_ALIVE;
1161 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1163 av_type = PW_FRAMED;
1164 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1167 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1169 av_type = PW_RADIUS;
1170 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1172 /* Update link stats */
1173 update_link_stats(0);
1175 if (link_stats_valid) {
1176 link_stats_valid = 0; /* Force later code to update */
1178 av_type = link_connect_time;
1179 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1181 av_type = link_stats.bytes_out;
1182 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1184 av_type = link_stats.bytes_in;
1185 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1187 av_type = link_stats.pkts_out;
1188 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1190 av_type = link_stats.pkts_in;
1191 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1194 if (*remote_number) {
1195 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1196 remote_number, 0, VENDOR_NONE);
1198 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1200 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1201 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1203 hisaddr = ho->hisaddr;
1204 av_type = htonl(hisaddr);
1205 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1207 /* Add user specified vp's */
1209 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1211 if (rstate.acctserver) {
1212 result = rc_acct_using_server(rstate.acctserver,
1213 rstate.client_port, send);
1215 result = rc_acct(rstate.client_port, send);
1218 if (result != OK_RC) {
1219 /* RADIUS server could be down so make this a warning */
1221 "Interim accounting failed for %s", rstate.user);
1223 rc_avpair_free(send);
1225 /* Schedule another one */
1226 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1229 /**********************************************************************
1230 * %FUNCTION: radius_ip_up
1237 * Called when IPCP is up. We'll do a start-accounting record.
1238 ***********************************************************************/
1240 radius_ip_up(void *opaque, int arg)
1242 radius_acct_start();
1245 /**********************************************************************
1246 * %FUNCTION: radius_ip_down
1253 * Called when IPCP is down. We'll do a stop-accounting record.
1254 ***********************************************************************/
1256 radius_ip_down(void *opaque, int arg)
1261 /**********************************************************************
1262 * %FUNCTION: radius_init
1264 * msg -- buffer of size BUF_LEN for error message
1266 * negative on failure; non-negative on success
1268 * Initializes radiusclient library
1269 ***********************************************************************/
1271 radius_init(char *msg)
1273 if (rstate.initialized) {
1277 if (config_file && *config_file) {
1278 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1281 rstate.initialized = 1;
1283 if (rc_read_config(rstate.config_file) != 0) {
1284 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1285 rstate.config_file);
1289 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1290 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1291 rc_conf_str("dictionary"));
1295 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1296 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1297 rc_conf_str("mapfile"));
1301 /* Add av pairs saved during option parsing */
1303 struct avpopt *n = avpopt->next;
1305 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1306 free(avpopt->vpstr);
1313 /**********************************************************************
1314 * %FUNCTION: get_client_port
1316 * ifname -- PPP interface name (e.g. "ppp7")
1318 * The NAS port number (e.g. 7)
1320 * Extracts the port number from the interface name
1321 ***********************************************************************/
1323 get_client_port(char *ifname)
1326 if (sscanf(ifname, "ppp%d", &port) == 1) {
1329 return rc_map2id(ifname);
1332 /**********************************************************************
1333 * %FUNCTION: radius_allowed_address
1335 * addr -- IP address
1337 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1339 ***********************************************************************/
1341 radius_allowed_address(u_int32_t addr)
1343 ipcp_options *wo = &ipcp_wantoptions[0];
1345 if (!rstate.choose_ip) {
1346 /* If RADIUS server said any address is OK, then fine... */
1347 if (rstate.any_ip_addr_ok) {
1351 /* Sigh... if an address was supplied for remote host in pppd
1352 options, it has to match that. */
1353 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1359 if (addr == rstate.ip_addr) return 1;
1363 /* Useful for other plugins */
1364 char *radius_logged_in_user(void)