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.20 2002/12/24 03:43:35 fcusack Exp $";
37 #include "radiusclient.h"
41 #include <sys/types.h>
47 static char *config_file = NULL;
48 static int add_avp(char **);
49 static struct avpopt {
54 static option_t Options[] = {
55 { "radius-config-file", o_string, &config_file },
56 { "avpair", o_special, add_avp },
60 static int radius_secret_check(void);
61 static int radius_pap_auth(char *user,
64 struct wordlist **paddrs,
65 struct wordlist **popts);
66 static int radius_chap_auth(char *user,
71 static void radius_ip_up(void *opaque, int arg);
72 static void radius_ip_down(void *opaque, int arg);
73 static void make_username_realm(char *user);
74 static int radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
75 REQUEST_INFO *req_info);
76 static void radius_choose_ip(u_int32_t *addrp);
77 static int radius_init(char *msg);
78 static int get_client_port(char *ifname);
79 static int radius_allowed_address(u_int32_t addr);
80 static void radius_acct_interim(void *);
82 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
84 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
88 #define MAXSESSIONID 32
92 #define MAXCLASSLEN 500
96 int accounting_started;
103 char user[MAXNAMELEN];
104 char config_file[MAXPATHLEN];
105 char session_id[MAXSESSIONID + 1];
107 int acct_interim_interval;
108 SERVER *authserver; /* Authentication server to use */
109 SERVER *acctserver; /* Accounting server to use */
111 char class[MAXCLASSLEN];
112 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
115 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
117 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
118 In that case, they override the values in the radiusclient.conf file */
119 void (*radius_pre_auth_hook)(char const *user,
121 SERVER **acctserver) = NULL;
123 static struct radius_state rstate;
125 char pppd_version[] = VERSION;
127 /**********************************************************************
128 * %FUNCTION: plugin_init
134 * Initializes RADIUS plugin.
135 ***********************************************************************/
139 pap_check_hook = radius_secret_check;
140 pap_auth_hook = radius_pap_auth;
142 chap_check_hook = radius_secret_check;
143 chap_auth_hook = radius_chap_auth;
145 ip_choose_hook = radius_choose_ip;
146 allowed_address_hook = radius_allowed_address;
148 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
149 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
151 memset(&rstate, 0, sizeof(rstate));
153 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
154 sizeof(rstate.config_file));
156 add_options(Options);
158 info("RADIUS plugin initialized.");
161 /**********************************************************************
164 * argv -- the <attribute=value> pair to add
168 * Adds an av pair to be passed on to the RADIUS server on each request.
169 ***********************************************************************/
173 struct avpopt *p = malloc(sizeof(struct avpopt));
175 /* Append to a list of vp's for later parsing */
176 p->vpstr = strdup(*argv);
183 /**********************************************************************
184 * %FUNCTION: radius_secret_check
188 * 1 -- we are ALWAYS willing to supply a secret. :-)
190 * Tells pppd that we will try to authenticate the peer, and not to
191 * worry about looking in /etc/ppp/*-secrets
192 ***********************************************************************/
194 radius_secret_check(void)
199 /**********************************************************************
200 * %FUNCTION: radius_choose_ip
202 * addrp -- where to store the IP address
206 * If RADIUS server has specified an IP address, it is stored in *addrp.
207 ***********************************************************************/
209 radius_choose_ip(u_int32_t *addrp)
211 if (rstate.choose_ip) {
212 *addrp = rstate.ip_addr;
216 /**********************************************************************
217 * %FUNCTION: radius_pap_auth
219 * user -- user-name of peer
220 * passwd -- password supplied by peer
221 * msgp -- Message which will be sent in PAP response
222 * paddrs -- set to a list of possible peer IP addresses
223 * popts -- set to a list of additional pppd options
225 * 1 if we can authenticate, -1 if we cannot.
227 * Performs PAP authentication using RADIUS
228 ***********************************************************************/
230 radius_pap_auth(char *user,
233 struct wordlist **paddrs,
234 struct wordlist **popts)
236 VALUE_PAIR *send, *received;
239 static char radius_msg[BUF_LEN];
244 if (radius_init(radius_msg) < 0) {
248 /* Put user with potentially realm added in rstate.user */
249 make_username_realm(user);
251 if (radius_pre_auth_hook) {
252 radius_pre_auth_hook(rstate.user,
260 /* Hack... the "port" is the ppp interface number. Should really be
262 rstate.client_port = get_client_port(ifname);
265 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
268 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
270 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
271 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
272 if (*remote_number) {
273 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
277 /* Add user specified vp's */
279 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
281 if (rstate.authserver) {
282 result = rc_auth_using_server(rstate.authserver,
283 rstate.client_port, send,
284 &received, radius_msg, NULL);
286 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
289 if (result == OK_RC) {
290 if (radius_setparams(NULL, received, radius_msg, NULL) < 0) {
295 /* free value pairs */
296 rc_avpair_free(received);
297 rc_avpair_free(send);
299 return (result == OK_RC) ? 1 : 0;
302 /**********************************************************************
303 * %FUNCTION: radius_chap_auth
305 * user -- user-name of peer
306 * remmd -- hash received from peer
307 * remmd_len -- length of remmd
308 * cstate -- pppd's chap_state structure
310 * CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
312 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
313 ***********************************************************************/
315 radius_chap_auth(char *user,
320 VALUE_PAIR *send, *received;
322 static char radius_msg[BUF_LEN];
324 u_char cpassword[MAX_RESPONSE_LENGTH + 1];
326 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
327 REQUEST_INFO request_info, *req_info = &request_info;
329 REQUEST_INFO *req_info = NULL;
334 if (radius_init(radius_msg) < 0) {
335 error("%s", radius_msg);
339 /* return error for types we can't handle */
340 if ((cstate->chal_type != CHAP_DIGEST_MD5)
342 && (cstate->chal_type != CHAP_MICROSOFT)
343 && (cstate->chal_type != CHAP_MICROSOFT_V2)
346 error("RADIUS: Challenge type %u unsupported", cstate->chal_type);
350 /* Put user with potentially realm added in rstate.user */
351 if (!rstate.done_chap_once) {
352 make_username_realm(user);
353 rstate.client_port = get_client_port (ifname);
354 if (radius_pre_auth_hook) {
355 radius_pre_auth_hook(rstate.user,
361 send = received = NULL;
364 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
367 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
369 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
372 * add the challenge and response fields
374 switch (cstate->chal_type) {
375 case CHAP_DIGEST_MD5:
376 /* CHAP-Challenge and CHAP-Password */
377 cpassword[0] = cstate->chal_id;
378 memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
380 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
381 cstate->challenge, cstate->chal_len, VENDOR_NONE);
382 rc_avpair_add(&send, PW_CHAP_PASSWORD,
383 cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
389 /* MS-CHAP-Challenge and MS-CHAP-Response */
390 MS_ChapResponse *rmd = (MS_ChapResponse *) remmd;
391 u_char *p = cpassword;
393 *p++ = cstate->chal_id;
394 /* The idiots use a different field order in RADIUS than PPP */
395 memcpy(p, rmd->UseNT, sizeof(rmd->UseNT));
396 p += sizeof(rmd->UseNT);
397 memcpy(p, rmd->LANManResp, sizeof(rmd->LANManResp));
398 p += sizeof(rmd->LANManResp);
399 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
401 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
402 cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
403 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
404 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
408 case CHAP_MICROSOFT_V2:
410 /* MS-CHAP-Challenge and MS-CHAP2-Response */
411 MS_Chap2Response *rmd = (MS_Chap2Response *) remmd;
412 u_char *p = cpassword;
414 *p++ = cstate->chal_id;
415 /* The idiots use a different field order in RADIUS than PPP */
416 memcpy(p, rmd->Flags, sizeof(rmd->Flags));
417 p += sizeof(rmd->Flags);
418 memcpy(p, rmd->PeerChallenge, sizeof(rmd->PeerChallenge));
419 p += sizeof(rmd->PeerChallenge);
420 memcpy(p, rmd->Reserved, sizeof(rmd->Reserved));
421 p += sizeof(rmd->Reserved);
422 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
424 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
425 cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
426 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
427 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
433 if (*remote_number) {
434 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
438 /* Add user specified vp's */
440 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
443 * make authentication with RADIUS server
446 if (rstate.authserver) {
447 result = rc_auth_using_server(rstate.authserver,
448 rstate.client_port, send,
449 &received, radius_msg, req_info);
451 result = rc_auth(rstate.client_port, send, &received, radius_msg,
455 if (result == OK_RC) {
456 if (!rstate.done_chap_once) {
457 if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
458 error("%s", radius_msg);
461 rstate.done_chap_once = 1;
466 rc_avpair_free(received);
467 rc_avpair_free (send);
468 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
471 /**********************************************************************
472 * %FUNCTION: make_username_realm
474 * user -- the user given to pppd
478 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
479 * then the default realm from the radiusclient config file is added.
480 ***********************************************************************/
482 make_username_realm(char *user)
486 if ( user != NULL ) {
487 strlcpy(rstate.user, user, sizeof(rstate.user));
492 default_realm = rc_conf_str("default_realm");
494 if (!strchr(rstate.user, '@') &&
496 (*default_realm != '\0')) {
497 strlcat(rstate.user, "@", sizeof(rstate.user));
498 strlcat(rstate.user, default_realm, sizeof(rstate.user));
502 /**********************************************************************
503 * %FUNCTION: radius_setparams
505 * cstate -- pppd's chap_state structure
506 * vp -- received value-pairs
507 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
509 * >= 0 on success; -1 on failure
511 * Parses attributes sent by RADIUS server and sets them in pppd.
512 ***********************************************************************/
514 radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
515 REQUEST_INFO *req_info)
518 int ms_chap2_success = 0;
520 int mppe_enc_keys = 0; /* whether or not these were received */
521 int mppe_enc_policy = 0;
522 int mppe_enc_types = 0;
525 /* Send RADIUS attributes to anyone else who might be interested */
526 if (radius_attributes_hook) {
527 (*radius_attributes_hook)(vp);
531 * service type (if not framed then quit),
532 * new IP address (RADIUS can define static IP for some users),
536 if (vp->vendorcode == VENDOR_NONE) {
537 switch (vp->attribute) {
538 case PW_SERVICE_TYPE:
539 /* check for service type */
540 /* if not FRAMED then exit */
541 if (vp->lvalue != PW_FRAMED) {
542 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
543 vp->lvalue, rstate.user);
548 case PW_FRAMED_PROTOCOL:
549 /* check for framed protocol type */
550 /* if not PPP then also exit */
551 if (vp->lvalue != PW_PPP) {
552 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
553 vp->lvalue, rstate.user);
558 case PW_SESSION_TIMEOUT:
559 /* Session timeout */
560 maxconnect = vp->lvalue;
563 case PW_SESSION_OCTETS_LIMIT:
564 /* Session traffic limit */
565 maxoctets = vp->lvalue;
567 case PW_OCTETS_DIRECTION:
568 /* Session traffic limit direction check */
569 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
572 case PW_ACCT_INTERIM_INTERVAL:
573 /* Send accounting updates every few seconds */
574 rstate.acct_interim_interval = vp->lvalue;
575 /* RFC says it MUST NOT be less than 60 seconds */
576 /* We use "0" to signify not sending updates */
577 if (rstate.acct_interim_interval &&
578 rstate.acct_interim_interval < 60) {
579 rstate.acct_interim_interval = 60;
582 case PW_FRAMED_IP_ADDRESS:
583 /* seting up remote IP addresses */
585 if (remote == 0xffffffff) {
586 /* 0xffffffff means user should be allowed to select one */
587 rstate.any_ip_addr_ok = 1;
588 } else if (remote != 0xfffffffe) {
589 /* 0xfffffffe means NAS should select an ip address */
590 remote = htonl(vp->lvalue);
591 if (bad_ip_adrs (remote)) {
592 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
593 remote, rstate.user);
596 rstate.choose_ip = 1;
597 rstate.ip_addr = remote;
601 /* Save Class attribute to pass it in accounting request */
602 if (vp->lvalue <= MAXCLASSLEN) {
603 rstate.class_len=vp->lvalue;
604 memcpy(rstate.class, vp->strvalue, rstate.class_len);
605 } /* else too big for our buffer - ignore it */
611 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
612 switch (vp->attribute) {
613 case PW_MS_CHAP2_SUCCESS:
614 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
615 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
618 memcpy(cstate->saresponse, vp->strvalue + 3,
619 MS_AUTH_RESPONSE_LENGTH);
620 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
621 ms_chap2_success = 1;
625 case PW_MS_CHAP_MPPE_KEYS:
626 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
627 slprintf(msg, BUF_LEN,
628 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
634 case PW_MS_MPPE_SEND_KEY:
635 case PW_MS_MPPE_RECV_KEY:
636 if (radius_setmppekeys2(vp, req_info) < 0) {
637 slprintf(msg, BUF_LEN,
638 "RADIUS: bad MS-MPPE-%s-Key attribute",
639 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
646 case PW_MS_MPPE_ENCRYPTION_POLICY:
647 mppe_enc_policy = vp->lvalue; /* save for later */
650 case PW_MS_MPPE_ENCRYPTION_TYPES:
651 mppe_enc_types = vp->lvalue; /* save for later */
656 case PW_MS_PRIMARY_DNS_SERVER:
657 case PW_MS_SECONDARY_DNS_SERVER:
658 case PW_MS_PRIMARY_NBNS_SERVER:
659 case PW_MS_SECONDARY_NBNS_SERVER:
668 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
669 if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
674 * Require both policy and key attributes to indicate a valid key.
675 * Note that if the policy value was '0' we don't set the key!
677 if (mppe_enc_policy && mppe_enc_keys) {
679 /* Set/modify allowed encryption types. */
681 set_mppe_enc_types(mppe_enc_policy, mppe_enc_types);
689 /**********************************************************************
690 * %FUNCTION: radius_setmppekeys
692 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
693 * req_info -- radius request information used for encryption
694 * cstate -- chap_state structure for challenge info
696 * >= 0 on success; -1 on failure
698 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
700 ***********************************************************************/
702 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
709 if (vp->lvalue != 32) {
710 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
715 memcpy(plain, vp->strvalue, sizeof(plain));
718 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
719 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
720 MD5Final(buf, &Context);
722 for (i = 0; i < 16; i++)
726 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
727 MD5Update(&Context, vp->strvalue, 16);
728 MD5Final(buf, &Context);
730 for(i = 0; i < 16; i++)
731 plain[i + 16] ^= buf[i];
734 * Annoying. The "key" returned is just the NTPasswordHashHash, which
735 * the NAS (us) doesn't need; we only need the start key. So we have
736 * to generate the start key, sigh. NB: We do not support the LM-Key.
738 mppe_set_keys(cstate->challenge, &plain[8]);
743 /**********************************************************************
744 * %FUNCTION: radius_setmppekeys2
746 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
747 * req_info -- radius request information used for encryption
749 * >= 0 on success; -1 on failure
751 * Decrypt the key provided by the RADIUS server for MPPE encryption.
753 ***********************************************************************/
755 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
759 u_char *salt = vp->strvalue;
760 u_char *crypt = vp->strvalue + 2;
762 u_char buf[MD5_SIGNATURE_SIZE];
765 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
768 if (vp->lvalue != 34) {
769 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
774 if ((salt[0] & 0x80) == 0) {
775 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
779 memcpy(plain, crypt, 32);
782 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
783 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
784 MD5Update(&Context, salt, 2);
785 MD5Final(buf, &Context);
787 for (i = 0; i < 16; i++)
790 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
791 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
792 (int) plain[0], type);
797 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
798 MD5Update(&Context, crypt, 16);
799 MD5Final(buf, &Context);
801 plain[16] ^= buf[0]; /* only need the first byte */
803 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
804 memcpy(mppe_send_key, plain + 1, 16);
806 memcpy(mppe_recv_key, plain + 1, 16);
812 /**********************************************************************
813 * %FUNCTION: radius_acct_start
819 * Sends a "start" accounting message to the RADIUS server.
820 ***********************************************************************/
822 radius_acct_start(void)
826 VALUE_PAIR *send = NULL;
827 ipcp_options *ho = &ipcp_hisoptions[0];
830 if (!rstate.initialized) {
834 rstate.start_time = time(NULL);
836 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
838 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
839 rstate.session_id, 0, VENDOR_NONE);
840 rc_avpair_add(&send, PW_USER_NAME,
841 rstate.user, 0, VENDOR_NONE);
843 if (rstate.class_len > 0)
844 rc_avpair_add(&send, PW_CLASS,
845 rstate.class, rstate.class_len, VENDOR_NONE);
847 av_type = PW_STATUS_START;
848 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
851 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
854 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
856 if (*remote_number) {
857 rc_avpair_add(&send, PW_CALLING_STATION_ID,
858 remote_number, 0, VENDOR_NONE);
862 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
866 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
868 hisaddr = ho->hisaddr;
869 av_type = htonl(hisaddr);
870 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
872 /* Add user specified vp's */
874 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
876 if (rstate.acctserver) {
877 result = rc_acct_using_server(rstate.acctserver,
878 rstate.client_port, send);
880 result = rc_acct(rstate.client_port, send);
883 rc_avpair_free(send);
885 if (result != OK_RC) {
886 /* RADIUS server could be down so make this a warning */
888 "Accounting START failed for %s", rstate.user);
890 rstate.accounting_started = 1;
891 /* Kick off periodic accounting reports */
892 if (rstate.acct_interim_interval) {
893 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
898 /**********************************************************************
899 * %FUNCTION: radius_acct_stop
905 * Sends a "stop" accounting message to the RADIUS server.
906 ***********************************************************************/
908 radius_acct_stop(void)
911 VALUE_PAIR *send = NULL;
912 ipcp_options *ho = &ipcp_hisoptions[0];
916 if (!rstate.initialized) {
920 if (!rstate.accounting_started) {
924 rstate.accounting_started = 0;
925 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
928 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
930 av_type = PW_STATUS_STOP;
931 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
934 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
937 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
940 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
943 if (link_stats_valid) {
944 av_type = link_connect_time;
945 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
947 av_type = link_stats.bytes_out;
948 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
950 av_type = link_stats.bytes_in;
951 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
953 av_type = link_stats.pkts_out;
954 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
956 av_type = link_stats.pkts_in;
957 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
960 if (*remote_number) {
961 rc_avpair_add(&send, PW_CALLING_STATION_ID,
962 remote_number, 0, VENDOR_NONE);
966 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
968 hisaddr = ho->hisaddr;
969 av_type = htonl(hisaddr);
970 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
972 /* Add user specified vp's */
974 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
976 if (rstate.acctserver) {
977 result = rc_acct_using_server(rstate.acctserver,
978 rstate.client_port, send);
980 result = rc_acct(rstate.client_port, send);
983 if (result != OK_RC) {
984 /* RADIUS server could be down so make this a warning */
986 "Accounting STOP failed for %s", rstate.user);
988 rc_avpair_free(send);
991 /**********************************************************************
992 * %FUNCTION: radius_acct_interim
998 * Sends an interim accounting message to the RADIUS server
999 ***********************************************************************/
1001 radius_acct_interim(void *ignored)
1004 VALUE_PAIR *send = NULL;
1005 ipcp_options *ho = &ipcp_hisoptions[0];
1009 if (!rstate.initialized) {
1013 if (!rstate.accounting_started) {
1017 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1020 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1022 av_type = PW_STATUS_ALIVE;
1023 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1025 av_type = PW_FRAMED;
1026 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1029 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1031 av_type = PW_RADIUS;
1032 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1034 /* Update link stats */
1035 update_link_stats(0);
1037 if (link_stats_valid) {
1038 link_stats_valid = 0; /* Force later code to update */
1040 av_type = link_connect_time;
1041 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1043 av_type = link_stats.bytes_out;
1044 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1046 av_type = link_stats.bytes_in;
1047 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1049 av_type = link_stats.pkts_out;
1050 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1052 av_type = link_stats.pkts_in;
1053 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1056 if (*remote_number) {
1057 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1058 remote_number, 0, VENDOR_NONE);
1062 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1064 hisaddr = ho->hisaddr;
1065 av_type = htonl(hisaddr);
1066 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1068 /* Add user specified vp's */
1070 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1072 if (rstate.acctserver) {
1073 result = rc_acct_using_server(rstate.acctserver,
1074 rstate.client_port, send);
1076 result = rc_acct(rstate.client_port, send);
1079 if (result != OK_RC) {
1080 /* RADIUS server could be down so make this a warning */
1082 "Interim accounting failed for %s", rstate.user);
1084 rc_avpair_free(send);
1086 /* Schedule another one */
1087 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1090 /**********************************************************************
1091 * %FUNCTION: radius_ip_up
1098 * Called when IPCP is up. We'll do a start-accounting record.
1099 ***********************************************************************/
1101 radius_ip_up(void *opaque, int arg)
1103 radius_acct_start();
1106 /**********************************************************************
1107 * %FUNCTION: radius_ip_down
1114 * Called when IPCP is down. We'll do a stop-accounting record.
1115 ***********************************************************************/
1117 radius_ip_down(void *opaque, int arg)
1122 /**********************************************************************
1123 * %FUNCTION: radius_init
1125 * msg -- buffer of size BUF_LEN for error message
1127 * negative on failure; non-negative on success
1129 * Initializes radiusclient library
1130 ***********************************************************************/
1132 radius_init(char *msg)
1134 if (rstate.initialized) {
1138 if (config_file && *config_file) {
1139 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1142 rstate.initialized = 1;
1144 if (rc_read_config(rstate.config_file) != 0) {
1145 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1146 rstate.config_file);
1150 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1151 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1152 rc_conf_str("dictionary"));
1156 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1157 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1158 rc_conf_str("mapfile"));
1162 /* Add av pairs saved during option parsing */
1164 struct avpopt *n = avpopt->next;
1166 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1167 free(avpopt->vpstr);
1174 /**********************************************************************
1175 * %FUNCTION: get_client_port
1177 * ifname -- PPP interface name (e.g. "ppp7")
1179 * The NAS port number (e.g. 7)
1181 * Extracts the port number from the interface name
1182 ***********************************************************************/
1184 get_client_port(char *ifname)
1187 if (sscanf(ifname, "ppp%d", &port) == 1) {
1190 return rc_map2id(ifname);
1193 /**********************************************************************
1194 * %FUNCTION: radius_allowed_address
1196 * addr -- IP address
1198 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1200 ***********************************************************************/
1202 radius_allowed_address(u_int32_t addr)
1204 ipcp_options *wo = &ipcp_wantoptions[0];
1206 if (!rstate.choose_ip) {
1207 /* If RADIUS server said any address is OK, then fine... */
1208 if (rstate.any_ip_addr_ok) {
1212 /* Sigh... if an address was supplied for remote host in pppd
1213 options, it has to match that. */
1214 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1220 if (addr == rstate.ip_addr) return 1;
1224 /* Useful for other plugins */
1225 char *radius_logged_in_user(void)