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.15 2002/09/12 05:41:49 fcusack Exp $";
37 #include "radiusclient.h"
41 #include <sys/types.h>
47 static char *config_file = NULL;
49 static option_t Options[] = {
50 { "radius-config-file", o_string, &config_file },
54 static int radius_secret_check(void);
55 static int radius_pap_auth(char *user,
58 struct wordlist **paddrs,
59 struct wordlist **popts);
60 static int radius_chap_auth(char *user,
65 static void radius_ip_up(void *opaque, int arg);
66 static void radius_ip_down(void *opaque, int arg);
67 static void make_username_realm(char *user);
68 static int radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
69 REQUEST_INFO *req_info);
70 static void radius_choose_ip(u_int32_t *addrp);
71 static int radius_init(char *msg);
72 static int get_client_port(char *ifname);
73 static int radius_allowed_address(u_int32_t addr);
74 static void radius_acct_interim(void *);
76 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
78 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
82 #define MAXSESSIONID 32
86 #define MAXCLASSLEN 500
90 int accounting_started;
97 char user[MAXNAMELEN];
98 char config_file[MAXPATHLEN];
99 char session_id[MAXSESSIONID + 1];
101 int acct_interim_interval;
102 SERVER *authserver; /* Authentication server to use */
103 SERVER *acctserver; /* Accounting server to use */
105 char class[MAXCLASSLEN];
108 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
110 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
111 In that case, they override the values in the radiusclient.conf file */
112 void (*radius_pre_auth_hook)(char const *user,
114 SERVER **acctserver) = NULL;
116 static struct radius_state rstate;
118 char pppd_version[] = VERSION;
120 /**********************************************************************
121 * %FUNCTION: plugin_init
127 * Initializes RADIUS plugin.
128 ***********************************************************************/
132 pap_check_hook = radius_secret_check;
133 pap_auth_hook = radius_pap_auth;
135 chap_check_hook = radius_secret_check;
136 chap_auth_hook = radius_chap_auth;
138 ip_choose_hook = radius_choose_ip;
139 allowed_address_hook = radius_allowed_address;
141 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
142 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
144 memset(&rstate, 0, sizeof(rstate));
146 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
147 sizeof(rstate.config_file));
149 add_options(Options);
151 info("RADIUS plugin initialized.");
154 /**********************************************************************
155 * %FUNCTION: radius_secret_check
159 * 1 -- we are ALWAYS willing to supply a secret. :-)
161 * Tells pppd that we will try to authenticate the peer, and not to
162 * worry about looking in /etc/ppp/*-secrets
163 ***********************************************************************/
165 radius_secret_check(void)
170 /**********************************************************************
171 * %FUNCTION: radius_choose_ip
173 * addrp -- where to store the IP address
177 * If RADIUS server has specified an IP address, it is stored in *addrp.
178 ***********************************************************************/
180 radius_choose_ip(u_int32_t *addrp)
182 if (rstate.choose_ip) {
183 *addrp = rstate.ip_addr;
187 /**********************************************************************
188 * %FUNCTION: radius_pap_auth
190 * user -- user-name of peer
191 * passwd -- password supplied by peer
192 * msgp -- Message which will be sent in PAP response
193 * paddrs -- set to a list of possible peer IP addresses
194 * popts -- set to a list of additional pppd options
196 * 1 if we can authenticate, -1 if we cannot.
198 * Performs PAP authentication using RADIUS
199 ***********************************************************************/
201 radius_pap_auth(char *user,
204 struct wordlist **paddrs,
205 struct wordlist **popts)
207 VALUE_PAIR *send, *received;
210 static char radius_msg[BUF_LEN];
215 if (radius_init(radius_msg) < 0) {
219 /* Put user with potentially realm added in rstate.user */
220 make_username_realm(user);
222 if (radius_pre_auth_hook) {
223 radius_pre_auth_hook(rstate.user,
231 /* Hack... the "port" is the ppp interface number. Should really be
233 rstate.client_port = get_client_port(ifname);
236 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
239 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
241 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
242 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
243 if (*remote_number) {
244 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
248 if (rstate.authserver) {
249 result = rc_auth_using_server(rstate.authserver,
250 rstate.client_port, send,
251 &received, radius_msg, NULL);
253 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
256 if (result == OK_RC) {
257 if (radius_setparams(NULL, received, radius_msg, NULL) < 0) {
262 /* free value pairs */
263 rc_avpair_free(received);
264 rc_avpair_free(send);
266 return (result == OK_RC) ? 1 : 0;
269 /**********************************************************************
270 * %FUNCTION: radius_chap_auth
272 * user -- user-name of peer
273 * remmd -- hash received from peer
274 * remmd_len -- length of remmd
275 * cstate -- pppd's chap_state structure
277 * CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
279 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
280 ***********************************************************************/
282 radius_chap_auth(char *user,
287 VALUE_PAIR *send, *received;
289 static char radius_msg[BUF_LEN];
291 u_char cpassword[MAX_RESPONSE_LENGTH + 1];
293 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
294 REQUEST_INFO request_info, *req_info = &request_info;
296 REQUEST_INFO *req_info = NULL;
301 if (radius_init(radius_msg) < 0) {
302 error("%s", radius_msg);
306 /* return error for types we can't handle */
307 if ((cstate->chal_type != CHAP_DIGEST_MD5)
309 && (cstate->chal_type != CHAP_MICROSOFT)
310 && (cstate->chal_type != CHAP_MICROSOFT_V2)
313 error("RADIUS: Challenge type %u unsupported", cstate->chal_type);
317 /* Put user with potentially realm added in rstate.user */
318 if (!rstate.done_chap_once) {
319 make_username_realm(user);
320 rstate.client_port = get_client_port (ifname);
321 if (radius_pre_auth_hook) {
322 radius_pre_auth_hook(rstate.user,
328 send = received = NULL;
331 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
334 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
336 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
339 * add the challenge and response fields
341 switch (cstate->chal_type) {
342 case CHAP_DIGEST_MD5:
343 /* CHAP-Challenge and CHAP-Password */
344 cpassword[0] = cstate->chal_id;
345 memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
347 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
348 cstate->challenge, cstate->chal_len, VENDOR_NONE);
349 rc_avpair_add(&send, PW_CHAP_PASSWORD,
350 cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
356 /* MS-CHAP-Challenge and MS-CHAP-Response */
357 MS_ChapResponse *rmd = (MS_ChapResponse *) remmd;
358 u_char *p = cpassword;
360 *p++ = cstate->chal_id;
361 /* The idiots use a different field order in RADIUS than PPP */
362 memcpy(p, rmd->UseNT, sizeof(rmd->UseNT));
363 p += sizeof(rmd->UseNT);
364 memcpy(p, rmd->LANManResp, sizeof(rmd->LANManResp));
365 p += sizeof(rmd->LANManResp);
366 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
368 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
369 cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
370 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
371 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
375 case CHAP_MICROSOFT_V2:
377 /* MS-CHAP-Challenge and MS-CHAP2-Response */
378 MS_Chap2Response *rmd = (MS_Chap2Response *) remmd;
379 u_char *p = cpassword;
381 *p++ = cstate->chal_id;
382 /* The idiots use a different field order in RADIUS than PPP */
383 memcpy(p, rmd->Flags, sizeof(rmd->Flags));
384 p += sizeof(rmd->Flags);
385 memcpy(p, rmd->PeerChallenge, sizeof(rmd->PeerChallenge));
386 p += sizeof(rmd->PeerChallenge);
387 memcpy(p, rmd->Reserved, sizeof(rmd->Reserved));
388 p += sizeof(rmd->Reserved);
389 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
391 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
392 cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
393 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
394 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
402 * make authentication with RADIUS server
405 if (rstate.authserver) {
406 result = rc_auth_using_server(rstate.authserver,
407 rstate.client_port, send,
408 &received, radius_msg, req_info);
410 result = rc_auth(rstate.client_port, send, &received, radius_msg,
414 if (result == OK_RC) {
415 if (!rstate.done_chap_once) {
416 if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
417 error("%s", radius_msg);
420 rstate.done_chap_once = 1;
425 rc_avpair_free(received);
426 rc_avpair_free (send);
427 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
430 /**********************************************************************
431 * %FUNCTION: make_username_realm
433 * user -- the user given to pppd
437 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
438 * then the default realm from the radiusclient config file is added.
439 ***********************************************************************/
441 make_username_realm(char *user)
445 if ( user != NULL ) {
446 strlcpy(rstate.user, user, sizeof(rstate.user));
451 default_realm = rc_conf_str("default_realm");
453 if (!strchr(rstate.user, '@') &&
455 (*default_realm != '\0')) {
456 strlcat(rstate.user, "@", sizeof(rstate.user));
457 strlcat(rstate.user, default_realm, sizeof(rstate.user));
461 /**********************************************************************
462 * %FUNCTION: radius_setparams
464 * cstate -- pppd's chap_state structure
465 * vp -- received value-pairs
466 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
468 * >= 0 on success; -1 on failure
470 * Parses attributes sent by RADIUS server and sets them in pppd.
471 ***********************************************************************/
473 radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
474 REQUEST_INFO *req_info)
477 int ms_chap2_success = 0;
479 /* Send RADIUS attributes to anyone else who might be interested */
480 if (radius_attributes_hook) {
481 (*radius_attributes_hook)(vp);
485 * service type (if not framed then quit),
486 * new IP address (RADIUS can define static IP for some users),
490 if (vp->vendorcode == VENDOR_NONE) {
491 switch (vp->attribute) {
492 case PW_SERVICE_TYPE:
493 /* check for service type */
494 /* if not FRAMED then exit */
495 if (vp->lvalue != PW_FRAMED) {
496 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
497 vp->lvalue, rstate.user);
502 case PW_FRAMED_PROTOCOL:
503 /* check for framed protocol type */
504 /* if not PPP then also exit */
505 if (vp->lvalue != PW_PPP) {
506 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
507 vp->lvalue, rstate.user);
512 case PW_SESSION_TIMEOUT:
513 /* Session timeout */
514 maxconnect = vp->lvalue;
517 case PW_SESSION_OCTETS_LIMIT:
518 /* Session traffic limit */
519 maxoctets = vp->lvalue;
521 case PW_OCTETS_DIRECTION:
522 /* Session traffic limit direction check */
523 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
526 case PW_ACCT_INTERIM_INTERVAL:
527 /* Send accounting updates every few seconds */
528 rstate.acct_interim_interval = vp->lvalue;
529 /* RFC says it MUST NOT be less than 60 seconds */
530 /* We use "0" to signify not sending updates */
531 if (rstate.acct_interim_interval &&
532 rstate.acct_interim_interval < 60) {
533 rstate.acct_interim_interval = 60;
536 case PW_FRAMED_IP_ADDRESS:
537 /* seting up remote IP addresses */
539 if (remote == 0xffffffff) {
540 /* 0xffffffff means user should be allowed to select one */
541 rstate.any_ip_addr_ok = 1;
542 } else if (remote != 0xfffffffe) {
543 /* 0xfffffffe means NAS should select an ip address */
544 remote = htonl(vp->lvalue);
545 if (bad_ip_adrs (remote)) {
546 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
547 remote, rstate.user);
550 rstate.choose_ip = 1;
551 rstate.ip_addr = remote;
555 /* Save Class attribute to pass it in accounting request */
556 if (vp->lvalue <= MAXCLASSLEN) {
557 rstate.class_len=vp->lvalue;
558 memcpy(rstate.class, vp->strvalue, rstate.class_len);
559 } /* else too big for our buffer - ignore it */
565 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
566 switch (vp->attribute) {
567 case PW_MS_CHAP2_SUCCESS:
568 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
569 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
572 memcpy(cstate->saresponse, vp->strvalue + 3,
573 MS_AUTH_RESPONSE_LENGTH);
574 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
575 ms_chap2_success = 1;
579 case PW_MS_CHAP_MPPE_KEYS:
580 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
581 slprintf(msg, BUF_LEN,
582 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
587 case PW_MS_MPPE_SEND_KEY:
588 case PW_MS_MPPE_RECV_KEY:
589 if (radius_setmppekeys2(vp, req_info) < 0) {
590 slprintf(msg, BUF_LEN,
591 "RADIUS: bad MS-MPPE-%s-Key attribute",
592 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
599 case PW_MS_MPPE_ENCRYPTION_POLICY:
600 case PW_MS_MPPE_ENCRYPTION_TYPES:
601 case PW_MS_PRIMARY_DNS_SERVER:
602 case PW_MS_SECONDARY_DNS_SERVER:
603 case PW_MS_PRIMARY_NBNS_SERVER:
604 case PW_MS_SECONDARY_NBNS_SERVER:
613 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
614 if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
621 /**********************************************************************
622 * %FUNCTION: radius_setmppekeys
624 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
625 * req_info -- radius request information used for encryption
626 * cstate -- chap_state structure for challenge info
628 * >= 0 on success; -1 on failure
630 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
632 ***********************************************************************/
634 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
641 if (vp->lvalue != 32) {
642 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
647 memcpy(plain, vp->strvalue, sizeof(plain));
650 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
651 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
652 MD5Final(buf, &Context);
654 for (i = 0; i < 16; i++)
658 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
659 MD5Update(&Context, vp->strvalue, 16);
660 MD5Final(buf, &Context);
662 for(i = 0; i < 16; i++)
663 plain[i + 16] ^= buf[i];
666 * Annoying. The "key" returned is just the NTPasswordHashHash, which
667 * the NAS (us) doesn't need; we only need the start key. So we have
668 * to generate the start key, sigh. NB: We do not support the LM-Key.
670 mppe_set_keys(cstate->challenge, &plain[8]);
675 /**********************************************************************
676 * %FUNCTION: radius_setmppekeys2
678 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
679 * req_info -- radius request information used for encryption
681 * >= 0 on success; -1 on failure
683 * Decrypt the key provided by the RADIUS server for MPPE encryption.
685 ***********************************************************************/
687 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
691 u_char *salt = vp->strvalue;
692 u_char *crypt = vp->strvalue + 2;
694 u_char buf[MD5_SIGNATURE_SIZE];
697 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
700 if (vp->lvalue != 34) {
701 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
706 if ((salt[0] & 0x80) == 0) {
707 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
711 memcpy(plain, crypt, 32);
714 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
715 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
716 MD5Update(&Context, salt, 2);
717 MD5Final(buf, &Context);
719 for (i = 0; i < 16; i++)
722 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
723 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
724 (int) plain[0], type);
729 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
730 MD5Update(&Context, crypt, 16);
731 MD5Final(buf, &Context);
733 plain[16] ^= buf[0]; /* only need the first byte */
735 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
736 memcpy(mppe_send_key, plain + 1, 16);
738 memcpy(mppe_recv_key, plain + 1, 16);
744 /**********************************************************************
745 * %FUNCTION: radius_acct_start
751 * Sends a "start" accounting message to the RADIUS server.
752 ***********************************************************************/
754 radius_acct_start(void)
758 VALUE_PAIR *send = NULL;
759 ipcp_options *ho = &ipcp_hisoptions[0];
762 if (!rstate.initialized) {
766 rstate.start_time = time(NULL);
768 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
770 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
771 rstate.session_id, 0, VENDOR_NONE);
772 rc_avpair_add(&send, PW_USER_NAME,
773 rstate.user, 0, VENDOR_NONE);
775 if (rstate.class_len > 0)
776 rc_avpair_add(&send, PW_CLASS,
777 rstate.class, rstate.class_len, VENDOR_NONE);
779 av_type = PW_STATUS_START;
780 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
783 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
786 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
788 if (*remote_number) {
789 rc_avpair_add(&send, PW_CALLING_STATION_ID,
790 remote_number, 0, VENDOR_NONE);
794 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
798 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
800 hisaddr = ho->hisaddr;
801 av_type = htonl(hisaddr);
802 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
804 if (rstate.acctserver) {
805 result = rc_acct_using_server(rstate.acctserver,
806 rstate.client_port, send);
808 result = rc_acct(rstate.client_port, send);
811 rc_avpair_free(send);
813 if (result != OK_RC) {
814 /* RADIUS server could be down so make this a warning */
816 "Accounting START failed for %s", rstate.user);
818 rstate.accounting_started = 1;
819 /* Kick off periodic accounting reports */
820 if (rstate.acct_interim_interval) {
821 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
826 /**********************************************************************
827 * %FUNCTION: radius_acct_stop
833 * Sends a "stop" accounting message to the RADIUS server.
834 ***********************************************************************/
836 radius_acct_stop(void)
839 VALUE_PAIR *send = NULL;
840 ipcp_options *ho = &ipcp_hisoptions[0];
844 if (!rstate.initialized) {
848 if (!rstate.accounting_started) {
852 rstate.accounting_started = 0;
853 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
856 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
858 av_type = PW_STATUS_STOP;
859 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
862 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
865 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
868 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
871 if (link_stats_valid) {
872 av_type = link_connect_time;
873 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
875 av_type = link_stats.bytes_out;
876 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
878 av_type = link_stats.bytes_in;
879 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
881 av_type = link_stats.pkts_out;
882 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
884 av_type = link_stats.pkts_in;
885 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
888 if (*remote_number) {
889 rc_avpair_add(&send, PW_CALLING_STATION_ID,
890 remote_number, 0, VENDOR_NONE);
894 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
896 hisaddr = ho->hisaddr;
897 av_type = htonl(hisaddr);
898 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
900 if (rstate.acctserver) {
901 result = rc_acct_using_server(rstate.acctserver,
902 rstate.client_port, send);
904 result = rc_acct(rstate.client_port, send);
907 if (result != OK_RC) {
908 /* RADIUS server could be down so make this a warning */
910 "Accounting STOP failed for %s", rstate.user);
912 rc_avpair_free(send);
915 /**********************************************************************
916 * %FUNCTION: radius_acct_interim
922 * Sends an interim accounting message to the RADIUS server
923 ***********************************************************************/
925 radius_acct_interim(void *ignored)
928 VALUE_PAIR *send = NULL;
929 ipcp_options *ho = &ipcp_hisoptions[0];
933 if (!rstate.initialized) {
937 if (!rstate.accounting_started) {
941 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
944 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
946 av_type = PW_STATUS_ALIVE;
947 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
950 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
953 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
956 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
958 /* Update link stats */
959 update_link_stats(0);
961 if (link_stats_valid) {
962 link_stats_valid = 0; /* Force later code to update */
964 av_type = link_connect_time;
965 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
967 av_type = link_stats.bytes_out;
968 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
970 av_type = link_stats.bytes_in;
971 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
973 av_type = link_stats.pkts_out;
974 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
976 av_type = link_stats.pkts_in;
977 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
980 if (*remote_number) {
981 rc_avpair_add(&send, PW_CALLING_STATION_ID,
982 remote_number, 0, VENDOR_NONE);
986 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
988 hisaddr = ho->hisaddr;
989 av_type = htonl(hisaddr);
990 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
992 if (rstate.acctserver) {
993 result = rc_acct_using_server(rstate.acctserver,
994 rstate.client_port, send);
996 result = rc_acct(rstate.client_port, send);
999 if (result != OK_RC) {
1000 /* RADIUS server could be down so make this a warning */
1002 "Interim accounting failed for %s", rstate.user);
1004 rc_avpair_free(send);
1006 /* Schedule another one */
1007 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1010 /**********************************************************************
1011 * %FUNCTION: radius_ip_up
1018 * Called when IPCP is up. We'll do a start-accounting record.
1019 ***********************************************************************/
1021 radius_ip_up(void *opaque, int arg)
1023 radius_acct_start();
1026 /**********************************************************************
1027 * %FUNCTION: radius_ip_down
1034 * Called when IPCP is down. We'll do a stop-accounting record.
1035 ***********************************************************************/
1037 radius_ip_down(void *opaque, int arg)
1042 /**********************************************************************
1043 * %FUNCTION: radius_init
1045 * msg -- buffer of size BUF_LEN for error message
1047 * negative on failure; non-negative on success
1049 * Initializes radiusclient library
1050 ***********************************************************************/
1052 radius_init(char *msg)
1054 if (rstate.initialized) {
1058 if (config_file && *config_file) {
1059 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1062 rstate.initialized = 1;
1064 if (rc_read_config(rstate.config_file) != 0) {
1065 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1066 rstate.config_file);
1070 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1071 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1072 rc_conf_str("dictionary"));
1076 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1077 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1078 rc_conf_str("mapfile"));
1084 /**********************************************************************
1085 * %FUNCTION: get_client_port
1087 * ifname -- PPP interface name (e.g. "ppp7")
1089 * The NAS port number (e.g. 7)
1091 * Extracts the port number from the interface name
1092 ***********************************************************************/
1094 get_client_port(char *ifname)
1097 if (sscanf(ifname, "ppp%d", &port) == 1) {
1100 return rc_map2id(ifname);
1103 /**********************************************************************
1104 * %FUNCTION: radius_allowed_address
1106 * addr -- IP address
1108 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1110 ***********************************************************************/
1112 radius_allowed_address(u_int32_t addr)
1114 ipcp_options *wo = &ipcp_wantoptions[0];
1116 if (!rstate.choose_ip) {
1117 /* If RADIUS server said any address is OK, then fine... */
1118 if (rstate.any_ip_addr_ok) {
1122 /* Sigh... if an address was supplied for remote host in pppd
1123 options, it has to match that. */
1124 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1130 if (addr == rstate.ip_addr) return 1;
1134 /* Useful for other plugins */
1135 char *radius_logged_in_user(void)