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.16 2002/10/01 08:36:49 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, 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);
434 /* Add user specified vp's */
436 rc_avpair_insert(&send, NULL, rstate.avp);
439 * make authentication with RADIUS server
442 if (rstate.authserver) {
443 result = rc_auth_using_server(rstate.authserver,
444 rstate.client_port, send,
445 &received, radius_msg, req_info);
447 result = rc_auth(rstate.client_port, send, &received, radius_msg,
451 if (result == OK_RC) {
452 if (!rstate.done_chap_once) {
453 if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
454 error("%s", radius_msg);
457 rstate.done_chap_once = 1;
462 rc_avpair_free(received);
463 rc_avpair_free (send);
464 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
467 /**********************************************************************
468 * %FUNCTION: make_username_realm
470 * user -- the user given to pppd
474 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
475 * then the default realm from the radiusclient config file is added.
476 ***********************************************************************/
478 make_username_realm(char *user)
482 if ( user != NULL ) {
483 strlcpy(rstate.user, user, sizeof(rstate.user));
488 default_realm = rc_conf_str("default_realm");
490 if (!strchr(rstate.user, '@') &&
492 (*default_realm != '\0')) {
493 strlcat(rstate.user, "@", sizeof(rstate.user));
494 strlcat(rstate.user, default_realm, sizeof(rstate.user));
498 /**********************************************************************
499 * %FUNCTION: radius_setparams
501 * cstate -- pppd's chap_state structure
502 * vp -- received value-pairs
503 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
505 * >= 0 on success; -1 on failure
507 * Parses attributes sent by RADIUS server and sets them in pppd.
508 ***********************************************************************/
510 radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
511 REQUEST_INFO *req_info)
514 int ms_chap2_success = 0;
516 /* Send RADIUS attributes to anyone else who might be interested */
517 if (radius_attributes_hook) {
518 (*radius_attributes_hook)(vp);
522 * service type (if not framed then quit),
523 * new IP address (RADIUS can define static IP for some users),
527 if (vp->vendorcode == VENDOR_NONE) {
528 switch (vp->attribute) {
529 case PW_SERVICE_TYPE:
530 /* check for service type */
531 /* if not FRAMED then exit */
532 if (vp->lvalue != PW_FRAMED) {
533 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
534 vp->lvalue, rstate.user);
539 case PW_FRAMED_PROTOCOL:
540 /* check for framed protocol type */
541 /* if not PPP then also exit */
542 if (vp->lvalue != PW_PPP) {
543 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
544 vp->lvalue, rstate.user);
549 case PW_SESSION_TIMEOUT:
550 /* Session timeout */
551 maxconnect = vp->lvalue;
554 case PW_SESSION_OCTETS_LIMIT:
555 /* Session traffic limit */
556 maxoctets = vp->lvalue;
558 case PW_OCTETS_DIRECTION:
559 /* Session traffic limit direction check */
560 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
563 case PW_ACCT_INTERIM_INTERVAL:
564 /* Send accounting updates every few seconds */
565 rstate.acct_interim_interval = vp->lvalue;
566 /* RFC says it MUST NOT be less than 60 seconds */
567 /* We use "0" to signify not sending updates */
568 if (rstate.acct_interim_interval &&
569 rstate.acct_interim_interval < 60) {
570 rstate.acct_interim_interval = 60;
573 case PW_FRAMED_IP_ADDRESS:
574 /* seting up remote IP addresses */
576 if (remote == 0xffffffff) {
577 /* 0xffffffff means user should be allowed to select one */
578 rstate.any_ip_addr_ok = 1;
579 } else if (remote != 0xfffffffe) {
580 /* 0xfffffffe means NAS should select an ip address */
581 remote = htonl(vp->lvalue);
582 if (bad_ip_adrs (remote)) {
583 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
584 remote, rstate.user);
587 rstate.choose_ip = 1;
588 rstate.ip_addr = remote;
592 /* Save Class attribute to pass it in accounting request */
593 if (vp->lvalue <= MAXCLASSLEN) {
594 rstate.class_len=vp->lvalue;
595 memcpy(rstate.class, vp->strvalue, rstate.class_len);
596 } /* else too big for our buffer - ignore it */
602 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
603 switch (vp->attribute) {
604 case PW_MS_CHAP2_SUCCESS:
605 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
606 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
609 memcpy(cstate->saresponse, vp->strvalue + 3,
610 MS_AUTH_RESPONSE_LENGTH);
611 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
612 ms_chap2_success = 1;
616 case PW_MS_CHAP_MPPE_KEYS:
617 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
618 slprintf(msg, BUF_LEN,
619 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
624 case PW_MS_MPPE_SEND_KEY:
625 case PW_MS_MPPE_RECV_KEY:
626 if (radius_setmppekeys2(vp, req_info) < 0) {
627 slprintf(msg, BUF_LEN,
628 "RADIUS: bad MS-MPPE-%s-Key attribute",
629 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
636 case PW_MS_MPPE_ENCRYPTION_POLICY:
637 case PW_MS_MPPE_ENCRYPTION_TYPES:
638 case PW_MS_PRIMARY_DNS_SERVER:
639 case PW_MS_SECONDARY_DNS_SERVER:
640 case PW_MS_PRIMARY_NBNS_SERVER:
641 case PW_MS_SECONDARY_NBNS_SERVER:
650 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
651 if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
658 /**********************************************************************
659 * %FUNCTION: radius_setmppekeys
661 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
662 * req_info -- radius request information used for encryption
663 * cstate -- chap_state structure for challenge info
665 * >= 0 on success; -1 on failure
667 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
669 ***********************************************************************/
671 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
678 if (vp->lvalue != 32) {
679 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
684 memcpy(plain, vp->strvalue, sizeof(plain));
687 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
688 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
689 MD5Final(buf, &Context);
691 for (i = 0; i < 16; i++)
695 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
696 MD5Update(&Context, vp->strvalue, 16);
697 MD5Final(buf, &Context);
699 for(i = 0; i < 16; i++)
700 plain[i + 16] ^= buf[i];
703 * Annoying. The "key" returned is just the NTPasswordHashHash, which
704 * the NAS (us) doesn't need; we only need the start key. So we have
705 * to generate the start key, sigh. NB: We do not support the LM-Key.
707 mppe_set_keys(cstate->challenge, &plain[8]);
712 /**********************************************************************
713 * %FUNCTION: radius_setmppekeys2
715 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
716 * req_info -- radius request information used for encryption
718 * >= 0 on success; -1 on failure
720 * Decrypt the key provided by the RADIUS server for MPPE encryption.
722 ***********************************************************************/
724 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
728 u_char *salt = vp->strvalue;
729 u_char *crypt = vp->strvalue + 2;
731 u_char buf[MD5_SIGNATURE_SIZE];
734 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
737 if (vp->lvalue != 34) {
738 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
743 if ((salt[0] & 0x80) == 0) {
744 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
748 memcpy(plain, crypt, 32);
751 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
752 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
753 MD5Update(&Context, salt, 2);
754 MD5Final(buf, &Context);
756 for (i = 0; i < 16; i++)
759 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
760 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
761 (int) plain[0], type);
766 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
767 MD5Update(&Context, crypt, 16);
768 MD5Final(buf, &Context);
770 plain[16] ^= buf[0]; /* only need the first byte */
772 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
773 memcpy(mppe_send_key, plain + 1, 16);
775 memcpy(mppe_recv_key, plain + 1, 16);
781 /**********************************************************************
782 * %FUNCTION: radius_acct_start
788 * Sends a "start" accounting message to the RADIUS server.
789 ***********************************************************************/
791 radius_acct_start(void)
795 VALUE_PAIR *send = NULL;
796 ipcp_options *ho = &ipcp_hisoptions[0];
799 if (!rstate.initialized) {
803 rstate.start_time = time(NULL);
805 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
807 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
808 rstate.session_id, 0, VENDOR_NONE);
809 rc_avpair_add(&send, PW_USER_NAME,
810 rstate.user, 0, VENDOR_NONE);
812 if (rstate.class_len > 0)
813 rc_avpair_add(&send, PW_CLASS,
814 rstate.class, rstate.class_len, VENDOR_NONE);
816 av_type = PW_STATUS_START;
817 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
820 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
823 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
825 if (*remote_number) {
826 rc_avpair_add(&send, PW_CALLING_STATION_ID,
827 remote_number, 0, VENDOR_NONE);
831 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
835 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
837 hisaddr = ho->hisaddr;
838 av_type = htonl(hisaddr);
839 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
841 /* Add user specified vp's */
843 rc_avpair_insert(&send, NULL, rstate.avp);
845 if (rstate.acctserver) {
846 result = rc_acct_using_server(rstate.acctserver,
847 rstate.client_port, send);
849 result = rc_acct(rstate.client_port, send);
852 rc_avpair_free(send);
854 if (result != OK_RC) {
855 /* RADIUS server could be down so make this a warning */
857 "Accounting START failed for %s", rstate.user);
859 rstate.accounting_started = 1;
860 /* Kick off periodic accounting reports */
861 if (rstate.acct_interim_interval) {
862 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
867 /**********************************************************************
868 * %FUNCTION: radius_acct_stop
874 * Sends a "stop" accounting message to the RADIUS server.
875 ***********************************************************************/
877 radius_acct_stop(void)
880 VALUE_PAIR *send = NULL;
881 ipcp_options *ho = &ipcp_hisoptions[0];
885 if (!rstate.initialized) {
889 if (!rstate.accounting_started) {
893 rstate.accounting_started = 0;
894 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
897 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
899 av_type = PW_STATUS_STOP;
900 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
903 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
906 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
909 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
912 if (link_stats_valid) {
913 av_type = link_connect_time;
914 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
916 av_type = link_stats.bytes_out;
917 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
919 av_type = link_stats.bytes_in;
920 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
922 av_type = link_stats.pkts_out;
923 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
925 av_type = link_stats.pkts_in;
926 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
929 if (*remote_number) {
930 rc_avpair_add(&send, PW_CALLING_STATION_ID,
931 remote_number, 0, VENDOR_NONE);
935 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
937 hisaddr = ho->hisaddr;
938 av_type = htonl(hisaddr);
939 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
941 /* Add user specified vp's */
943 rc_avpair_insert(&send, NULL, rstate.avp);
945 if (rstate.acctserver) {
946 result = rc_acct_using_server(rstate.acctserver,
947 rstate.client_port, send);
949 result = rc_acct(rstate.client_port, send);
952 if (result != OK_RC) {
953 /* RADIUS server could be down so make this a warning */
955 "Accounting STOP failed for %s", rstate.user);
957 rc_avpair_free(send);
960 /**********************************************************************
961 * %FUNCTION: radius_acct_interim
967 * Sends an interim accounting message to the RADIUS server
968 ***********************************************************************/
970 radius_acct_interim(void *ignored)
973 VALUE_PAIR *send = NULL;
974 ipcp_options *ho = &ipcp_hisoptions[0];
978 if (!rstate.initialized) {
982 if (!rstate.accounting_started) {
986 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
989 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
991 av_type = PW_STATUS_ALIVE;
992 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
995 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
998 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1000 av_type = PW_RADIUS;
1001 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1003 /* Update link stats */
1004 update_link_stats(0);
1006 if (link_stats_valid) {
1007 link_stats_valid = 0; /* Force later code to update */
1009 av_type = link_connect_time;
1010 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1012 av_type = link_stats.bytes_out;
1013 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1015 av_type = link_stats.bytes_in;
1016 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1018 av_type = link_stats.pkts_out;
1019 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1021 av_type = link_stats.pkts_in;
1022 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1025 if (*remote_number) {
1026 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1027 remote_number, 0, VENDOR_NONE);
1031 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1033 hisaddr = ho->hisaddr;
1034 av_type = htonl(hisaddr);
1035 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1037 /* Add user specified vp's */
1039 rc_avpair_insert(&send, NULL, rstate.avp);
1041 if (rstate.acctserver) {
1042 result = rc_acct_using_server(rstate.acctserver,
1043 rstate.client_port, send);
1045 result = rc_acct(rstate.client_port, send);
1048 if (result != OK_RC) {
1049 /* RADIUS server could be down so make this a warning */
1051 "Interim accounting failed for %s", rstate.user);
1053 rc_avpair_free(send);
1055 /* Schedule another one */
1056 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1059 /**********************************************************************
1060 * %FUNCTION: radius_ip_up
1067 * Called when IPCP is up. We'll do a start-accounting record.
1068 ***********************************************************************/
1070 radius_ip_up(void *opaque, int arg)
1072 radius_acct_start();
1075 /**********************************************************************
1076 * %FUNCTION: radius_ip_down
1083 * Called when IPCP is down. We'll do a stop-accounting record.
1084 ***********************************************************************/
1086 radius_ip_down(void *opaque, int arg)
1091 /**********************************************************************
1092 * %FUNCTION: radius_init
1094 * msg -- buffer of size BUF_LEN for error message
1096 * negative on failure; non-negative on success
1098 * Initializes radiusclient library
1099 ***********************************************************************/
1101 radius_init(char *msg)
1103 if (rstate.initialized) {
1107 if (config_file && *config_file) {
1108 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1111 rstate.initialized = 1;
1113 if (rc_read_config(rstate.config_file) != 0) {
1114 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1115 rstate.config_file);
1119 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1120 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1121 rc_conf_str("dictionary"));
1125 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1126 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1127 rc_conf_str("mapfile"));
1131 /* Add av pairs saved during option parsing */
1133 struct avpopt *n = avpopt->next;
1135 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1136 free(avpopt->vpstr);
1143 /**********************************************************************
1144 * %FUNCTION: get_client_port
1146 * ifname -- PPP interface name (e.g. "ppp7")
1148 * The NAS port number (e.g. 7)
1150 * Extracts the port number from the interface name
1151 ***********************************************************************/
1153 get_client_port(char *ifname)
1156 if (sscanf(ifname, "ppp%d", &port) == 1) {
1159 return rc_map2id(ifname);
1162 /**********************************************************************
1163 * %FUNCTION: radius_allowed_address
1165 * addr -- IP address
1167 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1169 ***********************************************************************/
1171 radius_allowed_address(u_int32_t addr)
1173 ipcp_options *wo = &ipcp_wantoptions[0];
1175 if (!rstate.choose_ip) {
1176 /* If RADIUS server said any address is OK, then fine... */
1177 if (rstate.any_ip_addr_ok) {
1181 /* Sigh... if an address was supplied for remote host in pppd
1182 options, it has to match that. */
1183 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1189 if (addr == rstate.ip_addr) return 1;
1193 /* Useful for other plugins */
1194 char *radius_logged_in_user(void)