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 {
121 char user[MAXNAMELEN];
122 char config_file[MAXPATHLEN];
123 char session_id[MAXSESSIONID + 1];
125 int acct_interim_interval;
126 SERVER *authserver; /* Authentication server to use */
127 SERVER *acctserver; /* Accounting server to use */
129 char class[MAXCLASSLEN];
130 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
133 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
135 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
136 In that case, they override the values in the radiusclient.conf file */
137 void (*radius_pre_auth_hook)(char const *user,
139 SERVER **acctserver) = NULL;
141 static struct radius_state rstate;
143 char pppd_version[] = VERSION;
145 /**********************************************************************
146 * %FUNCTION: plugin_init
152 * Initializes RADIUS plugin.
153 ***********************************************************************/
157 pap_check_hook = radius_secret_check;
158 pap_auth_hook = radius_pap_auth;
160 chap_check_hook = radius_secret_check;
161 chap_verify_hook = radius_chap_verify;
163 ip_choose_hook = radius_choose_ip;
164 allowed_address_hook = radius_allowed_address;
166 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
167 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
169 memset(&rstate, 0, sizeof(rstate));
171 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
172 sizeof(rstate.config_file));
174 add_options(Options);
176 info("RADIUS plugin initialized.");
179 /**********************************************************************
182 * argv -- the <attribute=value> pair to add
186 * Adds an av pair to be passed on to the RADIUS server on each request.
187 ***********************************************************************/
191 struct avpopt *p = malloc(sizeof(struct avpopt));
193 /* Append to a list of vp's for later parsing */
194 p->vpstr = strdup(*argv);
201 /**********************************************************************
202 * %FUNCTION: radius_secret_check
206 * 1 -- we are ALWAYS willing to supply a secret. :-)
208 * Tells pppd that we will try to authenticate the peer, and not to
209 * worry about looking in /etc/ppp/*-secrets
210 ***********************************************************************/
212 radius_secret_check(void)
217 /**********************************************************************
218 * %FUNCTION: radius_choose_ip
220 * addrp -- where to store the IP address
224 * If RADIUS server has specified an IP address, it is stored in *addrp.
225 ***********************************************************************/
227 radius_choose_ip(u_int32_t *addrp)
229 if (rstate.choose_ip) {
230 *addrp = rstate.ip_addr;
234 /**********************************************************************
235 * %FUNCTION: radius_pap_auth
237 * user -- user-name of peer
238 * passwd -- password supplied by peer
239 * msgp -- Message which will be sent in PAP response
240 * paddrs -- set to a list of possible peer IP addresses
241 * popts -- set to a list of additional pppd options
243 * 1 if we can authenticate, -1 if we cannot.
245 * Performs PAP authentication using RADIUS
246 ***********************************************************************/
248 radius_pap_auth(char *user,
251 struct wordlist **paddrs,
252 struct wordlist **popts)
254 VALUE_PAIR *send, *received;
257 static char radius_msg[BUF_LEN];
262 if (radius_init(radius_msg) < 0) {
266 /* Put user with potentially realm added in rstate.user */
267 make_username_realm(user);
269 if (radius_pre_auth_hook) {
270 radius_pre_auth_hook(rstate.user,
278 /* Hack... the "port" is the ppp interface number. Should really be
280 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
283 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
286 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
288 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
289 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
290 if (*remote_number) {
291 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
294 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
296 /* Add user specified vp's */
298 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
300 if (rstate.authserver) {
301 result = rc_auth_using_server(rstate.authserver,
302 rstate.client_port, send,
303 &received, radius_msg, NULL);
305 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
308 if (result == OK_RC) {
309 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
314 /* free value pairs */
315 rc_avpair_free(received);
316 rc_avpair_free(send);
318 return (result == OK_RC) ? 1 : 0;
321 /**********************************************************************
322 * %FUNCTION: radius_chap_verify
324 * user -- name of the peer
325 * ourname -- name for this machine
326 * id -- the ID byte in the challenge
327 * digest -- points to the structure representing the digest type
328 * challenge -- the challenge string we sent (length in first byte)
329 * response -- the response (hash) the peer sent back (length in 1st byte)
330 * message -- space for a message to be returned to the peer
331 * message_space -- number of bytes available at *message.
333 * 1 if the response is good, 0 if it is bad
335 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
336 ***********************************************************************/
338 radius_chap_verify(char *user, char *ourname, int id,
339 struct chap_digest_type *digest,
340 unsigned char *challenge, unsigned char *response,
341 char *message, int message_space)
343 VALUE_PAIR *send, *received;
345 static char radius_msg[BUF_LEN];
347 int challenge_len, response_len;
348 u_char cpassword[MAX_RESPONSE_LEN + 1];
350 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
351 REQUEST_INFO request_info, *req_info = &request_info;
353 REQUEST_INFO *req_info = NULL;
356 challenge_len = *challenge++;
357 response_len = *response++;
361 if (radius_init(radius_msg) < 0) {
362 error("%s", radius_msg);
366 /* return error for types we can't handle */
367 if ((digest->code != CHAP_MD5)
369 && (digest->code != CHAP_MICROSOFT)
370 && (digest->code != CHAP_MICROSOFT_V2)
373 error("RADIUS: Challenge type %u unsupported", digest->code);
377 /* Put user with potentially realm added in rstate.user */
378 if (!rstate.done_chap_once) {
379 make_username_realm(user);
380 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
381 if (radius_pre_auth_hook) {
382 radius_pre_auth_hook(rstate.user,
388 send = received = NULL;
391 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
394 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
396 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
399 * add the challenge and response fields
401 switch (digest->code) {
403 /* CHAP-Challenge and CHAP-Password */
404 if (response_len != MD5_HASH_SIZE)
407 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
409 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
410 challenge, challenge_len, VENDOR_NONE);
411 rc_avpair_add(&send, PW_CHAP_PASSWORD,
412 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
418 /* MS-CHAP-Challenge and MS-CHAP-Response */
419 u_char *p = cpassword;
421 if (response_len != MS_CHAP_RESPONSE_LEN)
424 /* The idiots use a different field order in RADIUS than PPP */
425 *p++ = response[MS_CHAP_USENT];
426 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
428 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
429 challenge, challenge_len, VENDOR_MICROSOFT);
430 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
431 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
435 case CHAP_MICROSOFT_V2:
437 /* MS-CHAP-Challenge and MS-CHAP2-Response */
438 u_char *p = cpassword;
440 if (response_len != MS_CHAP2_RESPONSE_LEN)
443 /* The idiots use a different field order in RADIUS than PPP */
444 *p++ = response[MS_CHAP2_FLAGS];
445 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
446 + MS_CHAP2_NTRESP_LEN));
448 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
449 challenge, challenge_len, VENDOR_MICROSOFT);
450 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
451 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
457 if (*remote_number) {
458 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
461 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
463 /* Add user specified vp's */
465 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
468 * make authentication with RADIUS server
471 if (rstate.authserver) {
472 result = rc_auth_using_server(rstate.authserver,
473 rstate.client_port, send,
474 &received, radius_msg, req_info);
476 result = rc_auth(rstate.client_port, send, &received, radius_msg,
480 strlcpy(message, radius_msg, message_space);
482 if (result == OK_RC) {
483 if (!rstate.done_chap_once) {
484 if (radius_setparams(received, radius_msg, req_info, digest,
485 challenge, message, message_space) < 0) {
486 error("%s", radius_msg);
489 rstate.done_chap_once = 1;
494 rc_avpair_free(received);
495 rc_avpair_free (send);
496 return (result == OK_RC);
499 /**********************************************************************
500 * %FUNCTION: make_username_realm
502 * user -- the user given to pppd
506 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
507 * then the default realm from the radiusclient config file is added.
508 ***********************************************************************/
510 make_username_realm(char *user)
514 if ( user != NULL ) {
515 strlcpy(rstate.user, user, sizeof(rstate.user));
520 default_realm = rc_conf_str("default_realm");
522 if (!strchr(rstate.user, '@') &&
524 (*default_realm != '\0')) {
525 strlcat(rstate.user, "@", sizeof(rstate.user));
526 strlcat(rstate.user, default_realm, sizeof(rstate.user));
530 /**********************************************************************
531 * %FUNCTION: radius_setparams
533 * vp -- received value-pairs
534 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
536 * >= 0 on success; -1 on failure
538 * Parses attributes sent by RADIUS server and sets them in pppd.
539 ***********************************************************************/
541 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
542 struct chap_digest_type *digest, unsigned char *challenge,
543 char *message, int message_space)
546 int ms_chap2_success = 0;
548 int mppe_enc_keys = 0; /* whether or not these were received */
549 int mppe_enc_policy = 0;
550 int mppe_enc_types = 0;
553 ipcp_options *wo = &ipcp_wantoptions[0];
554 ipcp_options *ao = &ipcp_allowoptions[0];
561 /* Send RADIUS attributes to anyone else who might be interested */
562 if (radius_attributes_hook) {
563 (*radius_attributes_hook)(vp);
567 * service type (if not framed then quit),
568 * new IP address (RADIUS can define static IP for some users),
572 if (vp->vendorcode == VENDOR_NONE) {
573 switch (vp->attribute) {
574 case PW_SERVICE_TYPE:
575 /* check for service type */
576 /* if not FRAMED then exit */
577 if (vp->lvalue != PW_FRAMED) {
578 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
579 vp->lvalue, rstate.user);
584 case PW_FRAMED_PROTOCOL:
585 /* check for framed protocol type */
586 /* if not PPP then also exit */
587 if (vp->lvalue != PW_PPP) {
588 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
589 vp->lvalue, rstate.user);
594 case PW_SESSION_TIMEOUT:
595 /* Session timeout */
596 maxconnect = vp->lvalue;
599 /* packet filter, will be handled via ip-(up|down) script */
600 script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
602 case PW_FRAMED_ROUTE:
603 /* route, will be handled via ip-(up|down) script */
604 script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
606 case PW_IDLE_TIMEOUT:
608 idle_time_limit = vp->lvalue;
611 case PW_SESSION_OCTETS_LIMIT:
612 /* Session traffic limit */
613 maxoctets = vp->lvalue;
615 case PW_OCTETS_DIRECTION:
616 /* Session traffic limit direction check */
617 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
620 case PW_ACCT_INTERIM_INTERVAL:
621 /* Send accounting updates every few seconds */
622 rstate.acct_interim_interval = vp->lvalue;
623 /* RFC says it MUST NOT be less than 60 seconds */
624 /* We use "0" to signify not sending updates */
625 if (rstate.acct_interim_interval &&
626 rstate.acct_interim_interval < 60) {
627 rstate.acct_interim_interval = 60;
630 case PW_FRAMED_IP_ADDRESS:
631 /* seting up remote IP addresses */
633 if (remote == 0xffffffff) {
634 /* 0xffffffff means user should be allowed to select one */
635 rstate.any_ip_addr_ok = 1;
636 } else if (remote != 0xfffffffe) {
637 /* 0xfffffffe means NAS should select an ip address */
638 remote = htonl(vp->lvalue);
639 if (bad_ip_adrs (remote)) {
640 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
641 remote, rstate.user);
644 rstate.choose_ip = 1;
645 rstate.ip_addr = remote;
648 case PW_NAS_IP_ADDRESS:
649 wo->ouraddr = htonl(vp->lvalue);
652 /* Save Class attribute to pass it in accounting request */
653 if (vp->lvalue <= MAXCLASSLEN) {
654 rstate.class_len=vp->lvalue;
655 memcpy(rstate.class, vp->strvalue, rstate.class_len);
656 } /* else too big for our buffer - ignore it */
659 netif_set_mtu(rstate.client_port,MIN(netif_get_mtu(rstate.client_port),vp->lvalue));
664 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
666 switch (vp->attribute) {
667 case PW_MS_CHAP2_SUCCESS:
668 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
669 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
673 strlcpy(message, (char*) vp->strvalue + 1, message_space);
674 ms_chap2_success = 1;
678 case PW_MS_CHAP_MPPE_KEYS:
679 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
680 slprintf(msg, BUF_LEN,
681 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
687 case PW_MS_MPPE_SEND_KEY:
688 case PW_MS_MPPE_RECV_KEY:
689 if (radius_setmppekeys2(vp, req_info) < 0) {
690 slprintf(msg, BUF_LEN,
691 "RADIUS: bad MS-MPPE-%s-Key attribute",
692 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
699 case PW_MS_MPPE_ENCRYPTION_POLICY:
700 mppe_enc_policy = vp->lvalue; /* save for later */
703 case PW_MS_MPPE_ENCRYPTION_TYPES:
704 mppe_enc_types = vp->lvalue; /* save for later */
709 case PW_MS_PRIMARY_DNS_SERVER:
710 ao->dnsaddr[0] = htonl(vp->lvalue);
713 ao->dnsaddr[1] = ao->dnsaddr[0];
715 case PW_MS_SECONDARY_DNS_SERVER:
716 ao->dnsaddr[1] = htonl(vp->lvalue);
719 ao->dnsaddr[0] = ao->dnsaddr[1];
721 case PW_MS_PRIMARY_NBNS_SERVER:
722 ao->winsaddr[0] = htonl(vp->lvalue);
725 ao->winsaddr[1] = ao->winsaddr[0];
727 case PW_MS_SECONDARY_NBNS_SERVER:
728 ao->winsaddr[1] = htonl(vp->lvalue);
731 ao->winsaddr[0] = ao->winsaddr[1];
740 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
741 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
746 * Require both policy and key attributes to indicate a valid key.
747 * Note that if the policy value was '0' we don't set the key!
749 if (mppe_enc_policy && mppe_enc_keys) {
750 /* Set/modify allowed encryption types. */
752 mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
762 /**********************************************************************
763 * %FUNCTION: radius_setmppekeys
765 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
766 * req_info -- radius request information used for encryption
768 * >= 0 on success; -1 on failure
770 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
772 ***********************************************************************/
774 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
775 unsigned char *challenge)
782 if (vp->lvalue != 32) {
783 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
788 memcpy(plain, vp->strvalue, sizeof(plain));
791 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
792 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
793 MD5_Final(buf, &Context);
795 for (i = 0; i < 16; i++)
799 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
800 MD5_Update(&Context, vp->strvalue, 16);
801 MD5_Final(buf, &Context);
803 for(i = 0; i < 16; i++)
804 plain[i + 16] ^= buf[i];
807 * Annoying. The "key" returned is just the NTPasswordHashHash, which
808 * the NAS (us) doesn't need; we only need the start key. So we have
809 * to generate the start key, sigh. NB: We do not support the LM-Key.
811 mppe_set_chapv1(challenge, &plain[8]);
816 /**********************************************************************
817 * %FUNCTION: radius_setmppekeys2
819 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
820 * req_info -- radius request information used for encryption
822 * >= 0 on success; -1 on failure
824 * Decrypt the key provided by the RADIUS server for MPPE encryption.
826 ***********************************************************************/
828 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
832 u_char *salt = vp->strvalue;
833 u_char *crypt = vp->strvalue + 2;
835 u_char buf[MD5_HASH_SIZE];
838 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
841 if (vp->lvalue != 34) {
842 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
847 if ((salt[0] & 0x80) == 0) {
848 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
852 memcpy(plain, crypt, 32);
855 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
856 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
857 MD5_Update(&Context, salt, 2);
858 MD5_Final(buf, &Context);
860 for (i = 0; i < 16; i++)
863 if (plain[0] != 16) {
864 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
865 (int) plain[0], type);
870 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
871 MD5_Update(&Context, crypt, 16);
872 MD5_Final(buf, &Context);
874 plain[16] ^= buf[0]; /* only need the first byte */
876 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
877 mppe_set_keys(plain + 1, NULL, 16);
879 mppe_set_keys(NULL, plain + 1, 16);
885 /**********************************************************************
886 * %FUNCTION: radius_acct_start
892 * Sends a "start" accounting message to the RADIUS server.
893 ***********************************************************************/
895 radius_acct_start(void)
899 VALUE_PAIR *send = NULL;
900 ipcp_options *ho = &ipcp_hisoptions[0];
903 if (!rstate.initialized) {
907 rstate.start_time = time(NULL);
909 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
911 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
912 rstate.session_id, 0, VENDOR_NONE);
913 rc_avpair_add(&send, PW_USER_NAME,
914 rstate.user, 0, VENDOR_NONE);
916 if (rstate.class_len > 0)
917 rc_avpair_add(&send, PW_CLASS,
918 rstate.class, rstate.class_len, VENDOR_NONE);
920 av_type = PW_STATUS_START;
921 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
924 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
927 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
929 if (*remote_number) {
930 rc_avpair_add(&send, PW_CALLING_STATION_ID,
931 remote_number, 0, VENDOR_NONE);
933 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
936 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
939 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
940 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
942 hisaddr = ho->hisaddr;
943 av_type = htonl(hisaddr);
944 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
946 /* Add user specified vp's */
948 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
950 if (rstate.acctserver) {
951 result = rc_acct_using_server(rstate.acctserver,
952 rstate.client_port, send);
954 result = rc_acct(rstate.client_port, send);
957 rc_avpair_free(send);
959 if (result != OK_RC) {
960 /* RADIUS server could be down so make this a warning */
962 "Accounting START failed for %s", rstate.user);
965 /* Kick off periodic accounting reports */
966 if (rstate.acct_interim_interval) {
967 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
971 /**********************************************************************
972 * %FUNCTION: radius_acct_stop
978 * Sends a "stop" accounting message to the RADIUS server.
979 ***********************************************************************/
981 radius_acct_stop(void)
984 VALUE_PAIR *send = NULL;
985 ipcp_options *ho = &ipcp_hisoptions[0];
989 if (!rstate.initialized) {
993 if (rstate.acct_interim_interval)
994 UNTIMEOUT(radius_acct_interim, NULL);
996 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
999 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1001 if (rstate.class_len > 0)
1002 rc_avpair_add(&send, PW_CLASS,
1003 rstate.class, rstate.class_len, VENDOR_NONE);
1005 av_type = PW_STATUS_STOP;
1006 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1008 av_type = PW_FRAMED;
1009 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1012 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1014 av_type = PW_RADIUS;
1015 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1018 if (link_stats_valid) {
1019 av_type = link_connect_time;
1020 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1022 av_type = link_stats.bytes_out;
1023 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1025 av_type = link_stats.bytes_in;
1026 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1028 av_type = link_stats.pkts_out;
1029 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1031 av_type = link_stats.pkts_in;
1032 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1035 if (*remote_number) {
1036 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1037 remote_number, 0, VENDOR_NONE);
1039 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1041 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1042 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1044 av_type = PW_NAS_ERROR;
1047 case EXIT_USER_REQUEST:
1048 av_type = PW_USER_REQUEST;
1052 case EXIT_PEER_DEAD:
1053 case EXIT_CONNECT_FAILED:
1054 av_type = PW_LOST_CARRIER;
1057 case EXIT_INIT_FAILED:
1058 case EXIT_OPEN_FAILED:
1059 case EXIT_LOCK_FAILED:
1060 case EXIT_PTYCMD_FAILED:
1061 av_type = PW_PORT_ERROR;
1064 case EXIT_PEER_AUTH_FAILED:
1065 case EXIT_AUTH_TOPEER_FAILED:
1066 case EXIT_NEGOTIATION_FAILED:
1067 case EXIT_CNID_AUTH_FAILED:
1068 av_type = PW_SERVICE_UNAVAILABLE;
1071 case EXIT_IDLE_TIMEOUT:
1072 av_type = PW_ACCT_IDLE_TIMEOUT;
1076 av_type = PW_CALLBACK;
1079 case EXIT_CONNECT_TIME:
1080 av_type = PW_ACCT_SESSION_TIMEOUT;
1084 case EXIT_TRAFFIC_LIMIT:
1085 av_type = PW_NAS_REQUEST;
1090 av_type = PW_NAS_ERROR;
1093 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1095 hisaddr = ho->hisaddr;
1096 av_type = htonl(hisaddr);
1097 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1099 /* Add user specified vp's */
1101 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1103 if (rstate.acctserver) {
1104 result = rc_acct_using_server(rstate.acctserver,
1105 rstate.client_port, send);
1107 result = rc_acct(rstate.client_port, send);
1110 if (result != OK_RC) {
1111 /* RADIUS server could be down so make this a warning */
1113 "Accounting STOP failed for %s", rstate.user);
1115 rc_avpair_free(send);
1118 /**********************************************************************
1119 * %FUNCTION: radius_acct_interim
1125 * Sends an interim accounting message to the RADIUS server
1126 ***********************************************************************/
1128 radius_acct_interim(void *ignored)
1131 VALUE_PAIR *send = NULL;
1132 ipcp_options *ho = &ipcp_hisoptions[0];
1136 if (!rstate.initialized) {
1140 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1143 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1145 if (rstate.class_len > 0)
1146 rc_avpair_add(&send, PW_CLASS,
1147 rstate.class, rstate.class_len, VENDOR_NONE);
1149 av_type = PW_STATUS_ALIVE;
1150 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1152 av_type = PW_FRAMED;
1153 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1156 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1158 av_type = PW_RADIUS;
1159 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1161 /* Update link stats */
1162 update_link_stats(0);
1164 if (link_stats_valid) {
1165 link_stats_valid = 0; /* Force later code to update */
1167 av_type = link_connect_time;
1168 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1170 av_type = link_stats.bytes_out;
1171 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1173 av_type = link_stats.bytes_in;
1174 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1176 av_type = link_stats.pkts_out;
1177 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1179 av_type = link_stats.pkts_in;
1180 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1183 if (*remote_number) {
1184 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1185 remote_number, 0, VENDOR_NONE);
1187 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1189 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1190 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1192 hisaddr = ho->hisaddr;
1193 av_type = htonl(hisaddr);
1194 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1196 /* Add user specified vp's */
1198 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1200 if (rstate.acctserver) {
1201 result = rc_acct_using_server(rstate.acctserver,
1202 rstate.client_port, send);
1204 result = rc_acct(rstate.client_port, send);
1207 if (result != OK_RC) {
1208 /* RADIUS server could be down so make this a warning */
1210 "Interim accounting failed for %s", rstate.user);
1212 rc_avpair_free(send);
1214 /* Schedule another one */
1215 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1218 /**********************************************************************
1219 * %FUNCTION: radius_ip_up
1226 * Called when IPCP is up. We'll do a start-accounting record.
1227 ***********************************************************************/
1229 radius_ip_up(void *opaque, int arg)
1231 radius_acct_start();
1234 /**********************************************************************
1235 * %FUNCTION: radius_ip_down
1242 * Called when IPCP is down. We'll do a stop-accounting record.
1243 ***********************************************************************/
1245 radius_ip_down(void *opaque, int arg)
1250 /**********************************************************************
1251 * %FUNCTION: radius_init
1253 * msg -- buffer of size BUF_LEN for error message
1255 * negative on failure; non-negative on success
1257 * Initializes radiusclient library
1258 ***********************************************************************/
1260 radius_init(char *msg)
1262 if (rstate.initialized) {
1266 if (config_file && *config_file) {
1267 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1270 rstate.initialized = 1;
1272 if (rc_read_config(rstate.config_file) != 0) {
1273 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1274 rstate.config_file);
1278 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1279 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1280 rc_conf_str("dictionary"));
1284 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1285 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1286 rc_conf_str("mapfile"));
1290 /* Add av pairs saved during option parsing */
1292 struct avpopt *n = avpopt->next;
1294 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1295 free(avpopt->vpstr);
1302 /**********************************************************************
1303 * %FUNCTION: get_client_port
1305 * ifname -- PPP interface name (e.g. "ppp7")
1307 * The NAS port number (e.g. 7)
1309 * Extracts the port number from the interface name
1310 ***********************************************************************/
1312 get_client_port(char *ifname)
1315 if (sscanf(ifname, "ppp%d", &port) == 1) {
1318 return rc_map2id(ifname);
1321 /**********************************************************************
1322 * %FUNCTION: radius_allowed_address
1324 * addr -- IP address
1326 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1328 ***********************************************************************/
1330 radius_allowed_address(u_int32_t addr)
1332 ipcp_options *wo = &ipcp_wantoptions[0];
1334 if (!rstate.choose_ip) {
1335 /* If RADIUS server said any address is OK, then fine... */
1336 if (rstate.any_ip_addr_ok) {
1340 /* Sigh... if an address was supplied for remote host in pppd
1341 options, it has to match that. */
1342 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1348 if (addr == rstate.ip_addr) return 1;
1352 /* Useful for other plugins */
1353 char *radius_logged_in_user(void)