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.17 2002/10/01 09:27:50 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);
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, 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 /* Send RADIUS attributes to anyone else who might be interested */
521 if (radius_attributes_hook) {
522 (*radius_attributes_hook)(vp);
526 * service type (if not framed then quit),
527 * new IP address (RADIUS can define static IP for some users),
531 if (vp->vendorcode == VENDOR_NONE) {
532 switch (vp->attribute) {
533 case PW_SERVICE_TYPE:
534 /* check for service type */
535 /* if not FRAMED then exit */
536 if (vp->lvalue != PW_FRAMED) {
537 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
538 vp->lvalue, rstate.user);
543 case PW_FRAMED_PROTOCOL:
544 /* check for framed protocol type */
545 /* if not PPP then also exit */
546 if (vp->lvalue != PW_PPP) {
547 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
548 vp->lvalue, rstate.user);
553 case PW_SESSION_TIMEOUT:
554 /* Session timeout */
555 maxconnect = vp->lvalue;
558 case PW_SESSION_OCTETS_LIMIT:
559 /* Session traffic limit */
560 maxoctets = vp->lvalue;
562 case PW_OCTETS_DIRECTION:
563 /* Session traffic limit direction check */
564 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
567 case PW_ACCT_INTERIM_INTERVAL:
568 /* Send accounting updates every few seconds */
569 rstate.acct_interim_interval = vp->lvalue;
570 /* RFC says it MUST NOT be less than 60 seconds */
571 /* We use "0" to signify not sending updates */
572 if (rstate.acct_interim_interval &&
573 rstate.acct_interim_interval < 60) {
574 rstate.acct_interim_interval = 60;
577 case PW_FRAMED_IP_ADDRESS:
578 /* seting up remote IP addresses */
580 if (remote == 0xffffffff) {
581 /* 0xffffffff means user should be allowed to select one */
582 rstate.any_ip_addr_ok = 1;
583 } else if (remote != 0xfffffffe) {
584 /* 0xfffffffe means NAS should select an ip address */
585 remote = htonl(vp->lvalue);
586 if (bad_ip_adrs (remote)) {
587 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
588 remote, rstate.user);
591 rstate.choose_ip = 1;
592 rstate.ip_addr = remote;
596 /* Save Class attribute to pass it in accounting request */
597 if (vp->lvalue <= MAXCLASSLEN) {
598 rstate.class_len=vp->lvalue;
599 memcpy(rstate.class, vp->strvalue, rstate.class_len);
600 } /* else too big for our buffer - ignore it */
606 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
607 switch (vp->attribute) {
608 case PW_MS_CHAP2_SUCCESS:
609 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
610 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
613 memcpy(cstate->saresponse, vp->strvalue + 3,
614 MS_AUTH_RESPONSE_LENGTH);
615 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
616 ms_chap2_success = 1;
620 case PW_MS_CHAP_MPPE_KEYS:
621 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
622 slprintf(msg, BUF_LEN,
623 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
628 case PW_MS_MPPE_SEND_KEY:
629 case PW_MS_MPPE_RECV_KEY:
630 if (radius_setmppekeys2(vp, req_info) < 0) {
631 slprintf(msg, BUF_LEN,
632 "RADIUS: bad MS-MPPE-%s-Key attribute",
633 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
640 case PW_MS_MPPE_ENCRYPTION_POLICY:
641 case PW_MS_MPPE_ENCRYPTION_TYPES:
642 case PW_MS_PRIMARY_DNS_SERVER:
643 case PW_MS_SECONDARY_DNS_SERVER:
644 case PW_MS_PRIMARY_NBNS_SERVER:
645 case PW_MS_SECONDARY_NBNS_SERVER:
654 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
655 if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
662 /**********************************************************************
663 * %FUNCTION: radius_setmppekeys
665 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
666 * req_info -- radius request information used for encryption
667 * cstate -- chap_state structure for challenge info
669 * >= 0 on success; -1 on failure
671 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
673 ***********************************************************************/
675 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
682 if (vp->lvalue != 32) {
683 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
688 memcpy(plain, vp->strvalue, sizeof(plain));
691 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
692 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
693 MD5Final(buf, &Context);
695 for (i = 0; i < 16; i++)
699 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
700 MD5Update(&Context, vp->strvalue, 16);
701 MD5Final(buf, &Context);
703 for(i = 0; i < 16; i++)
704 plain[i + 16] ^= buf[i];
707 * Annoying. The "key" returned is just the NTPasswordHashHash, which
708 * the NAS (us) doesn't need; we only need the start key. So we have
709 * to generate the start key, sigh. NB: We do not support the LM-Key.
711 mppe_set_keys(cstate->challenge, &plain[8]);
716 /**********************************************************************
717 * %FUNCTION: radius_setmppekeys2
719 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
720 * req_info -- radius request information used for encryption
722 * >= 0 on success; -1 on failure
724 * Decrypt the key provided by the RADIUS server for MPPE encryption.
726 ***********************************************************************/
728 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
732 u_char *salt = vp->strvalue;
733 u_char *crypt = vp->strvalue + 2;
735 u_char buf[MD5_SIGNATURE_SIZE];
738 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
741 if (vp->lvalue != 34) {
742 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
747 if ((salt[0] & 0x80) == 0) {
748 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
752 memcpy(plain, crypt, 32);
755 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
756 MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
757 MD5Update(&Context, salt, 2);
758 MD5Final(buf, &Context);
760 for (i = 0; i < 16; i++)
763 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
764 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
765 (int) plain[0], type);
770 MD5Update(&Context, req_info->secret, strlen(req_info->secret));
771 MD5Update(&Context, crypt, 16);
772 MD5Final(buf, &Context);
774 plain[16] ^= buf[0]; /* only need the first byte */
776 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
777 memcpy(mppe_send_key, plain + 1, 16);
779 memcpy(mppe_recv_key, plain + 1, 16);
785 /**********************************************************************
786 * %FUNCTION: radius_acct_start
792 * Sends a "start" accounting message to the RADIUS server.
793 ***********************************************************************/
795 radius_acct_start(void)
799 VALUE_PAIR *send = NULL;
800 ipcp_options *ho = &ipcp_hisoptions[0];
803 if (!rstate.initialized) {
807 rstate.start_time = time(NULL);
809 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
811 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
812 rstate.session_id, 0, VENDOR_NONE);
813 rc_avpair_add(&send, PW_USER_NAME,
814 rstate.user, 0, VENDOR_NONE);
816 if (rstate.class_len > 0)
817 rc_avpair_add(&send, PW_CLASS,
818 rstate.class, rstate.class_len, VENDOR_NONE);
820 av_type = PW_STATUS_START;
821 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
824 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
827 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
829 if (*remote_number) {
830 rc_avpair_add(&send, PW_CALLING_STATION_ID,
831 remote_number, 0, VENDOR_NONE);
835 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
839 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
841 hisaddr = ho->hisaddr;
842 av_type = htonl(hisaddr);
843 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
845 /* Add user specified vp's */
847 rc_avpair_insert(&send, NULL, rstate.avp);
849 if (rstate.acctserver) {
850 result = rc_acct_using_server(rstate.acctserver,
851 rstate.client_port, send);
853 result = rc_acct(rstate.client_port, send);
856 rc_avpair_free(send);
858 if (result != OK_RC) {
859 /* RADIUS server could be down so make this a warning */
861 "Accounting START failed for %s", rstate.user);
863 rstate.accounting_started = 1;
864 /* Kick off periodic accounting reports */
865 if (rstate.acct_interim_interval) {
866 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
871 /**********************************************************************
872 * %FUNCTION: radius_acct_stop
878 * Sends a "stop" accounting message to the RADIUS server.
879 ***********************************************************************/
881 radius_acct_stop(void)
884 VALUE_PAIR *send = NULL;
885 ipcp_options *ho = &ipcp_hisoptions[0];
889 if (!rstate.initialized) {
893 if (!rstate.accounting_started) {
897 rstate.accounting_started = 0;
898 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
901 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
903 av_type = PW_STATUS_STOP;
904 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
907 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
910 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
913 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
916 if (link_stats_valid) {
917 av_type = link_connect_time;
918 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
920 av_type = link_stats.bytes_out;
921 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
923 av_type = link_stats.bytes_in;
924 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
926 av_type = link_stats.pkts_out;
927 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
929 av_type = link_stats.pkts_in;
930 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
933 if (*remote_number) {
934 rc_avpair_add(&send, PW_CALLING_STATION_ID,
935 remote_number, 0, VENDOR_NONE);
939 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
941 hisaddr = ho->hisaddr;
942 av_type = htonl(hisaddr);
943 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
945 /* Add user specified vp's */
947 rc_avpair_insert(&send, NULL, rstate.avp);
949 if (rstate.acctserver) {
950 result = rc_acct_using_server(rstate.acctserver,
951 rstate.client_port, send);
953 result = rc_acct(rstate.client_port, send);
956 if (result != OK_RC) {
957 /* RADIUS server could be down so make this a warning */
959 "Accounting STOP failed for %s", rstate.user);
961 rc_avpair_free(send);
964 /**********************************************************************
965 * %FUNCTION: radius_acct_interim
971 * Sends an interim accounting message to the RADIUS server
972 ***********************************************************************/
974 radius_acct_interim(void *ignored)
977 VALUE_PAIR *send = NULL;
978 ipcp_options *ho = &ipcp_hisoptions[0];
982 if (!rstate.initialized) {
986 if (!rstate.accounting_started) {
990 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
993 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
995 av_type = PW_STATUS_ALIVE;
996 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
999 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1002 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1004 av_type = PW_RADIUS;
1005 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1007 /* Update link stats */
1008 update_link_stats(0);
1010 if (link_stats_valid) {
1011 link_stats_valid = 0; /* Force later code to update */
1013 av_type = link_connect_time;
1014 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1016 av_type = link_stats.bytes_out;
1017 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1019 av_type = link_stats.bytes_in;
1020 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1022 av_type = link_stats.pkts_out;
1023 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1025 av_type = link_stats.pkts_in;
1026 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1029 if (*remote_number) {
1030 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1031 remote_number, 0, VENDOR_NONE);
1035 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1037 hisaddr = ho->hisaddr;
1038 av_type = htonl(hisaddr);
1039 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1041 /* Add user specified vp's */
1043 rc_avpair_insert(&send, NULL, rstate.avp);
1045 if (rstate.acctserver) {
1046 result = rc_acct_using_server(rstate.acctserver,
1047 rstate.client_port, send);
1049 result = rc_acct(rstate.client_port, send);
1052 if (result != OK_RC) {
1053 /* RADIUS server could be down so make this a warning */
1055 "Interim accounting failed for %s", rstate.user);
1057 rc_avpair_free(send);
1059 /* Schedule another one */
1060 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1063 /**********************************************************************
1064 * %FUNCTION: radius_ip_up
1071 * Called when IPCP is up. We'll do a start-accounting record.
1072 ***********************************************************************/
1074 radius_ip_up(void *opaque, int arg)
1076 radius_acct_start();
1079 /**********************************************************************
1080 * %FUNCTION: radius_ip_down
1087 * Called when IPCP is down. We'll do a stop-accounting record.
1088 ***********************************************************************/
1090 radius_ip_down(void *opaque, int arg)
1095 /**********************************************************************
1096 * %FUNCTION: radius_init
1098 * msg -- buffer of size BUF_LEN for error message
1100 * negative on failure; non-negative on success
1102 * Initializes radiusclient library
1103 ***********************************************************************/
1105 radius_init(char *msg)
1107 if (rstate.initialized) {
1111 if (config_file && *config_file) {
1112 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1115 rstate.initialized = 1;
1117 if (rc_read_config(rstate.config_file) != 0) {
1118 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1119 rstate.config_file);
1123 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1124 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1125 rc_conf_str("dictionary"));
1129 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1130 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1131 rc_conf_str("mapfile"));
1135 /* Add av pairs saved during option parsing */
1137 struct avpopt *n = avpopt->next;
1139 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1140 free(avpopt->vpstr);
1147 /**********************************************************************
1148 * %FUNCTION: get_client_port
1150 * ifname -- PPP interface name (e.g. "ppp7")
1152 * The NAS port number (e.g. 7)
1154 * Extracts the port number from the interface name
1155 ***********************************************************************/
1157 get_client_port(char *ifname)
1160 if (sscanf(ifname, "ppp%d", &port) == 1) {
1163 return rc_map2id(ifname);
1166 /**********************************************************************
1167 * %FUNCTION: radius_allowed_address
1169 * addr -- IP address
1171 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1173 ***********************************************************************/
1175 radius_allowed_address(u_int32_t addr)
1177 ipcp_options *wo = &ipcp_wantoptions[0];
1179 if (!rstate.choose_ip) {
1180 /* If RADIUS server said any address is OK, then fine... */
1181 if (rstate.any_ip_addr_ok) {
1185 /* Sigh... if an address was supplied for remote host in pppd
1186 options, it has to match that. */
1187 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1193 if (addr == rstate.ip_addr) return 1;
1197 /* Useful for other plugins */
1198 char *radius_logged_in_user(void)