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.31 2006/05/22 00:01:40 paulus Exp $";
37 #include "radiusclient.h"
41 #include <sys/types.h>
44 #include <netinet/in.h>
49 #define MD5_HASH_SIZE 16
51 static char *config_file = NULL;
52 static int add_avp(char **);
53 static struct avpopt {
57 static bool portnummap = 0;
59 static option_t Options[] = {
60 { "radius-config-file", o_string, &config_file },
61 { "avpair", o_special, add_avp },
62 { "map-to-ttyname", o_bool, &portnummap,
63 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
64 { "map-to-ifname", o_bool, &portnummap,
65 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
69 static int radius_secret_check(void);
70 static int radius_pap_auth(char *user,
73 struct wordlist **paddrs,
74 struct wordlist **popts);
75 static int radius_chap_verify(char *user, char *ourname, int id,
76 struct chap_digest_type *digest,
77 unsigned char *challenge,
78 unsigned char *response,
79 char *message, int message_space);
81 static void radius_ip_up(void *opaque, int arg);
82 static void radius_ip_down(void *opaque, int arg);
83 static void make_username_realm(char *user);
84 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
85 struct chap_digest_type *digest,
86 unsigned char *challenge,
87 char *message, int message_space);
88 static void radius_choose_ip(u_int32_t *addrp);
89 static int radius_init(char *msg);
90 static int get_client_port(char *ifname);
91 static int radius_allowed_address(u_int32_t addr);
92 static void radius_acct_interim(void *);
94 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
96 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
100 #define MAXSESSIONID 32
104 #define MAXCLASSLEN 500
107 struct radius_state {
108 int accounting_started;
115 char user[MAXNAMELEN];
116 char config_file[MAXPATHLEN];
117 char session_id[MAXSESSIONID + 1];
119 int acct_interim_interval;
120 SERVER *authserver; /* Authentication server to use */
121 SERVER *acctserver; /* Accounting server to use */
123 char class[MAXCLASSLEN];
124 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
127 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
129 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
130 In that case, they override the values in the radiusclient.conf file */
131 void (*radius_pre_auth_hook)(char const *user,
133 SERVER **acctserver) = NULL;
135 static struct radius_state rstate;
137 char pppd_version[] = VERSION;
139 /**********************************************************************
140 * %FUNCTION: plugin_init
146 * Initializes RADIUS plugin.
147 ***********************************************************************/
151 pap_check_hook = radius_secret_check;
152 pap_auth_hook = radius_pap_auth;
154 chap_check_hook = radius_secret_check;
155 chap_verify_hook = radius_chap_verify;
157 ip_choose_hook = radius_choose_ip;
158 allowed_address_hook = radius_allowed_address;
160 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
161 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
163 memset(&rstate, 0, sizeof(rstate));
165 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
166 sizeof(rstate.config_file));
168 add_options(Options);
170 info("RADIUS plugin initialized.");
173 /**********************************************************************
176 * argv -- the <attribute=value> pair to add
180 * Adds an av pair to be passed on to the RADIUS server on each request.
181 ***********************************************************************/
185 struct avpopt *p = malloc(sizeof(struct avpopt));
187 /* Append to a list of vp's for later parsing */
188 p->vpstr = strdup(*argv);
195 /**********************************************************************
196 * %FUNCTION: radius_secret_check
200 * 1 -- we are ALWAYS willing to supply a secret. :-)
202 * Tells pppd that we will try to authenticate the peer, and not to
203 * worry about looking in /etc/ppp/*-secrets
204 ***********************************************************************/
206 radius_secret_check(void)
211 /**********************************************************************
212 * %FUNCTION: radius_choose_ip
214 * addrp -- where to store the IP address
218 * If RADIUS server has specified an IP address, it is stored in *addrp.
219 ***********************************************************************/
221 radius_choose_ip(u_int32_t *addrp)
223 if (rstate.choose_ip) {
224 *addrp = rstate.ip_addr;
228 /**********************************************************************
229 * %FUNCTION: radius_pap_auth
231 * user -- user-name of peer
232 * passwd -- password supplied by peer
233 * msgp -- Message which will be sent in PAP response
234 * paddrs -- set to a list of possible peer IP addresses
235 * popts -- set to a list of additional pppd options
237 * 1 if we can authenticate, -1 if we cannot.
239 * Performs PAP authentication using RADIUS
240 ***********************************************************************/
242 radius_pap_auth(char *user,
245 struct wordlist **paddrs,
246 struct wordlist **popts)
248 VALUE_PAIR *send, *received;
251 static char radius_msg[BUF_LEN];
256 if (radius_init(radius_msg) < 0) {
260 /* Put user with potentially realm added in rstate.user */
261 make_username_realm(user);
263 if (radius_pre_auth_hook) {
264 radius_pre_auth_hook(rstate.user,
272 /* Hack... the "port" is the ppp interface number. Should really be
274 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
277 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
280 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
282 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
283 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
284 if (*remote_number) {
285 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
288 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
290 /* Add user specified vp's */
292 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
294 if (rstate.authserver) {
295 result = rc_auth_using_server(rstate.authserver,
296 rstate.client_port, send,
297 &received, radius_msg, NULL);
299 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
302 if (result == OK_RC) {
303 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
308 /* free value pairs */
309 rc_avpair_free(received);
310 rc_avpair_free(send);
312 return (result == OK_RC) ? 1 : 0;
315 /**********************************************************************
316 * %FUNCTION: radius_chap_verify
318 * user -- name of the peer
319 * ourname -- name for this machine
320 * id -- the ID byte in the challenge
321 * digest -- points to the structure representing the digest type
322 * challenge -- the challenge string we sent (length in first byte)
323 * response -- the response (hash) the peer sent back (length in 1st byte)
324 * message -- space for a message to be returned to the peer
325 * message_space -- number of bytes available at *message.
327 * 1 if the response is good, 0 if it is bad
329 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
330 ***********************************************************************/
332 radius_chap_verify(char *user, char *ourname, int id,
333 struct chap_digest_type *digest,
334 unsigned char *challenge, unsigned char *response,
335 char *message, int message_space)
337 VALUE_PAIR *send, *received;
339 static char radius_msg[BUF_LEN];
341 int challenge_len, response_len;
342 u_char cpassword[MAX_RESPONSE_LEN + 1];
344 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
345 REQUEST_INFO request_info, *req_info = &request_info;
347 REQUEST_INFO *req_info = NULL;
350 challenge_len = *challenge++;
351 response_len = *response++;
355 if (radius_init(radius_msg) < 0) {
356 error("%s", radius_msg);
360 /* return error for types we can't handle */
361 if ((digest->code != CHAP_MD5)
363 && (digest->code != CHAP_MICROSOFT)
364 && (digest->code != CHAP_MICROSOFT_V2)
367 error("RADIUS: Challenge type %u unsupported", digest->code);
371 /* Put user with potentially realm added in rstate.user */
372 if (!rstate.done_chap_once) {
373 make_username_realm(user);
374 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
375 if (radius_pre_auth_hook) {
376 radius_pre_auth_hook(rstate.user,
382 send = received = NULL;
385 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
388 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
390 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
393 * add the challenge and response fields
395 switch (digest->code) {
397 /* CHAP-Challenge and CHAP-Password */
398 if (response_len != MD5_HASH_SIZE)
401 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
403 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
404 challenge, challenge_len, VENDOR_NONE);
405 rc_avpair_add(&send, PW_CHAP_PASSWORD,
406 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
412 /* MS-CHAP-Challenge and MS-CHAP-Response */
413 u_char *p = cpassword;
415 if (response_len != MS_CHAP_RESPONSE_LEN)
418 /* The idiots use a different field order in RADIUS than PPP */
419 *p++ = response[MS_CHAP_USENT];
420 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
422 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
423 challenge, challenge_len, VENDOR_MICROSOFT);
424 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
425 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
429 case CHAP_MICROSOFT_V2:
431 /* MS-CHAP-Challenge and MS-CHAP2-Response */
432 u_char *p = cpassword;
434 if (response_len != MS_CHAP2_RESPONSE_LEN)
437 /* The idiots use a different field order in RADIUS than PPP */
438 *p++ = response[MS_CHAP2_FLAGS];
439 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
440 + MS_CHAP2_NTRESP_LEN));
442 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
443 challenge, challenge_len, VENDOR_MICROSOFT);
444 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
445 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
451 if (*remote_number) {
452 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
455 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
457 /* Add user specified vp's */
459 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
462 * make authentication with RADIUS server
465 if (rstate.authserver) {
466 result = rc_auth_using_server(rstate.authserver,
467 rstate.client_port, send,
468 &received, radius_msg, req_info);
470 result = rc_auth(rstate.client_port, send, &received, radius_msg,
474 strlcpy(message, radius_msg, message_space);
476 if (result == OK_RC) {
477 if (!rstate.done_chap_once) {
478 if (radius_setparams(received, radius_msg, req_info, digest,
479 challenge, message, message_space) < 0) {
480 error("%s", radius_msg);
483 rstate.done_chap_once = 1;
488 rc_avpair_free(received);
489 rc_avpair_free (send);
490 return (result == OK_RC);
493 /**********************************************************************
494 * %FUNCTION: make_username_realm
496 * user -- the user given to pppd
500 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
501 * then the default realm from the radiusclient config file is added.
502 ***********************************************************************/
504 make_username_realm(char *user)
508 if ( user != NULL ) {
509 strlcpy(rstate.user, user, sizeof(rstate.user));
514 default_realm = rc_conf_str("default_realm");
516 if (!strchr(rstate.user, '@') &&
518 (*default_realm != '\0')) {
519 strlcat(rstate.user, "@", sizeof(rstate.user));
520 strlcat(rstate.user, default_realm, sizeof(rstate.user));
524 /**********************************************************************
525 * %FUNCTION: radius_setparams
527 * vp -- received value-pairs
528 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
530 * >= 0 on success; -1 on failure
532 * Parses attributes sent by RADIUS server and sets them in pppd.
533 ***********************************************************************/
535 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
536 struct chap_digest_type *digest, unsigned char *challenge,
537 char *message, int message_space)
540 int ms_chap2_success = 0;
542 int mppe_enc_keys = 0; /* whether or not these were received */
543 int mppe_enc_policy = 0;
544 int mppe_enc_types = 0;
547 /* Send RADIUS attributes to anyone else who might be interested */
548 if (radius_attributes_hook) {
549 (*radius_attributes_hook)(vp);
553 * service type (if not framed then quit),
554 * new IP address (RADIUS can define static IP for some users),
558 if (vp->vendorcode == VENDOR_NONE) {
559 switch (vp->attribute) {
560 case PW_SERVICE_TYPE:
561 /* check for service type */
562 /* if not FRAMED then exit */
563 if (vp->lvalue != PW_FRAMED) {
564 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
565 vp->lvalue, rstate.user);
570 case PW_FRAMED_PROTOCOL:
571 /* check for framed protocol type */
572 /* if not PPP then also exit */
573 if (vp->lvalue != PW_PPP) {
574 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
575 vp->lvalue, rstate.user);
580 case PW_SESSION_TIMEOUT:
581 /* Session timeout */
582 maxconnect = vp->lvalue;
585 case PW_SESSION_OCTETS_LIMIT:
586 /* Session traffic limit */
587 maxoctets = vp->lvalue;
589 case PW_OCTETS_DIRECTION:
590 /* Session traffic limit direction check */
591 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
594 case PW_ACCT_INTERIM_INTERVAL:
595 /* Send accounting updates every few seconds */
596 rstate.acct_interim_interval = vp->lvalue;
597 /* RFC says it MUST NOT be less than 60 seconds */
598 /* We use "0" to signify not sending updates */
599 if (rstate.acct_interim_interval &&
600 rstate.acct_interim_interval < 60) {
601 rstate.acct_interim_interval = 60;
604 case PW_FRAMED_IP_ADDRESS:
605 /* seting up remote IP addresses */
607 if (remote == 0xffffffff) {
608 /* 0xffffffff means user should be allowed to select one */
609 rstate.any_ip_addr_ok = 1;
610 } else if (remote != 0xfffffffe) {
611 /* 0xfffffffe means NAS should select an ip address */
612 remote = htonl(vp->lvalue);
613 if (bad_ip_adrs (remote)) {
614 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
615 remote, rstate.user);
618 rstate.choose_ip = 1;
619 rstate.ip_addr = remote;
623 /* Save Class attribute to pass it in accounting request */
624 if (vp->lvalue <= MAXCLASSLEN) {
625 rstate.class_len=vp->lvalue;
626 memcpy(rstate.class, vp->strvalue, rstate.class_len);
627 } /* else too big for our buffer - ignore it */
633 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
634 switch (vp->attribute) {
635 case PW_MS_CHAP2_SUCCESS:
636 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
637 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
641 strlcpy(message, vp->strvalue + 1, message_space);
642 ms_chap2_success = 1;
646 case PW_MS_CHAP_MPPE_KEYS:
647 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
648 slprintf(msg, BUF_LEN,
649 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
655 case PW_MS_MPPE_SEND_KEY:
656 case PW_MS_MPPE_RECV_KEY:
657 if (radius_setmppekeys2(vp, req_info) < 0) {
658 slprintf(msg, BUF_LEN,
659 "RADIUS: bad MS-MPPE-%s-Key attribute",
660 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
667 case PW_MS_MPPE_ENCRYPTION_POLICY:
668 mppe_enc_policy = vp->lvalue; /* save for later */
671 case PW_MS_MPPE_ENCRYPTION_TYPES:
672 mppe_enc_types = vp->lvalue; /* save for later */
677 case PW_MS_PRIMARY_DNS_SERVER:
678 case PW_MS_SECONDARY_DNS_SERVER:
679 case PW_MS_PRIMARY_NBNS_SERVER:
680 case PW_MS_SECONDARY_NBNS_SERVER:
689 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
690 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
695 * Require both policy and key attributes to indicate a valid key.
696 * Note that if the policy value was '0' we don't set the key!
698 if (mppe_enc_policy && mppe_enc_keys) {
700 /* Set/modify allowed encryption types. */
702 set_mppe_enc_types(mppe_enc_policy, mppe_enc_types);
710 /**********************************************************************
711 * %FUNCTION: radius_setmppekeys
713 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
714 * req_info -- radius request information used for encryption
716 * >= 0 on success; -1 on failure
718 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
720 ***********************************************************************/
722 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
723 unsigned char *challenge)
730 if (vp->lvalue != 32) {
731 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
736 memcpy(plain, vp->strvalue, sizeof(plain));
739 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
740 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
741 MD5_Final(buf, &Context);
743 for (i = 0; i < 16; i++)
747 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
748 MD5_Update(&Context, vp->strvalue, 16);
749 MD5_Final(buf, &Context);
751 for(i = 0; i < 16; i++)
752 plain[i + 16] ^= buf[i];
755 * Annoying. The "key" returned is just the NTPasswordHashHash, which
756 * the NAS (us) doesn't need; we only need the start key. So we have
757 * to generate the start key, sigh. NB: We do not support the LM-Key.
759 mppe_set_keys(challenge, &plain[8]);
764 /**********************************************************************
765 * %FUNCTION: radius_setmppekeys2
767 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
768 * req_info -- radius request information used for encryption
770 * >= 0 on success; -1 on failure
772 * Decrypt the key provided by the RADIUS server for MPPE encryption.
774 ***********************************************************************/
776 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
780 u_char *salt = vp->strvalue;
781 u_char *crypt = vp->strvalue + 2;
783 u_char buf[MD5_HASH_SIZE];
786 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
789 if (vp->lvalue != 34) {
790 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
795 if ((salt[0] & 0x80) == 0) {
796 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
800 memcpy(plain, crypt, 32);
803 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
804 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
805 MD5_Update(&Context, salt, 2);
806 MD5_Final(buf, &Context);
808 for (i = 0; i < 16; i++)
811 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
812 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
813 (int) plain[0], type);
818 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
819 MD5_Update(&Context, crypt, 16);
820 MD5_Final(buf, &Context);
822 plain[16] ^= buf[0]; /* only need the first byte */
824 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
825 memcpy(mppe_send_key, plain + 1, 16);
827 memcpy(mppe_recv_key, plain + 1, 16);
833 /**********************************************************************
834 * %FUNCTION: radius_acct_start
840 * Sends a "start" accounting message to the RADIUS server.
841 ***********************************************************************/
843 radius_acct_start(void)
847 VALUE_PAIR *send = NULL;
848 ipcp_options *ho = &ipcp_hisoptions[0];
851 if (!rstate.initialized) {
855 rstate.start_time = time(NULL);
857 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
859 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
860 rstate.session_id, 0, VENDOR_NONE);
861 rc_avpair_add(&send, PW_USER_NAME,
862 rstate.user, 0, VENDOR_NONE);
864 if (rstate.class_len > 0)
865 rc_avpair_add(&send, PW_CLASS,
866 rstate.class, rstate.class_len, VENDOR_NONE);
868 av_type = PW_STATUS_START;
869 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
872 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
875 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
877 if (*remote_number) {
878 rc_avpair_add(&send, PW_CALLING_STATION_ID,
879 remote_number, 0, VENDOR_NONE);
881 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
884 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
887 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
888 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
890 hisaddr = ho->hisaddr;
891 av_type = htonl(hisaddr);
892 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
894 /* Add user specified vp's */
896 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
898 if (rstate.acctserver) {
899 result = rc_acct_using_server(rstate.acctserver,
900 rstate.client_port, send);
902 result = rc_acct(rstate.client_port, send);
905 rc_avpair_free(send);
907 if (result != OK_RC) {
908 /* RADIUS server could be down so make this a warning */
910 "Accounting START failed for %s", rstate.user);
912 rstate.accounting_started = 1;
913 /* Kick off periodic accounting reports */
914 if (rstate.acct_interim_interval) {
915 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
920 /**********************************************************************
921 * %FUNCTION: radius_acct_stop
927 * Sends a "stop" accounting message to the RADIUS server.
928 ***********************************************************************/
930 radius_acct_stop(void)
933 VALUE_PAIR *send = NULL;
934 ipcp_options *ho = &ipcp_hisoptions[0];
938 if (!rstate.initialized) {
942 if (!rstate.accounting_started) {
946 rstate.accounting_started = 0;
947 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
950 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
952 av_type = PW_STATUS_STOP;
953 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
956 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
959 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
962 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
965 if (link_stats_valid) {
966 av_type = link_connect_time;
967 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
969 av_type = link_stats.bytes_out;
970 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
972 av_type = link_stats.bytes_in;
973 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
975 av_type = link_stats.pkts_out;
976 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
978 av_type = link_stats.pkts_in;
979 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
982 if (*remote_number) {
983 rc_avpair_add(&send, PW_CALLING_STATION_ID,
984 remote_number, 0, VENDOR_NONE);
986 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
988 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
989 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
991 av_type = PW_NAS_ERROR;
994 case EXIT_USER_REQUEST:
995 av_type = PW_USER_REQUEST;
1000 case EXIT_CONNECT_FAILED:
1001 av_type = PW_LOST_CARRIER;
1004 case EXIT_INIT_FAILED:
1005 case EXIT_OPEN_FAILED:
1006 case EXIT_LOCK_FAILED:
1007 case EXIT_PTYCMD_FAILED:
1008 av_type = PW_PORT_ERROR;
1011 case EXIT_PEER_AUTH_FAILED:
1012 case EXIT_AUTH_TOPEER_FAILED:
1013 case EXIT_NEGOTIATION_FAILED:
1014 case EXIT_CNID_AUTH_FAILED:
1015 av_type = PW_SERVICE_UNAVAILABLE;
1018 case EXIT_IDLE_TIMEOUT:
1019 av_type = PW_ACCT_IDLE_TIMEOUT;
1023 av_type = PW_CALLBACK;
1026 case EXIT_CONNECT_TIME:
1027 av_type = PW_ACCT_SESSION_TIMEOUT;
1031 case EXIT_TRAFFIC_LIMIT:
1032 av_type = PW_NAS_REQUEST;
1037 av_type = PW_NAS_ERROR;
1040 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1042 hisaddr = ho->hisaddr;
1043 av_type = htonl(hisaddr);
1044 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1046 /* Add user specified vp's */
1048 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1050 if (rstate.acctserver) {
1051 result = rc_acct_using_server(rstate.acctserver,
1052 rstate.client_port, send);
1054 result = rc_acct(rstate.client_port, send);
1057 if (result != OK_RC) {
1058 /* RADIUS server could be down so make this a warning */
1060 "Accounting STOP failed for %s", rstate.user);
1062 rc_avpair_free(send);
1065 /**********************************************************************
1066 * %FUNCTION: radius_acct_interim
1072 * Sends an interim accounting message to the RADIUS server
1073 ***********************************************************************/
1075 radius_acct_interim(void *ignored)
1078 VALUE_PAIR *send = NULL;
1079 ipcp_options *ho = &ipcp_hisoptions[0];
1083 if (!rstate.initialized) {
1087 if (!rstate.accounting_started) {
1091 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1094 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1096 av_type = PW_STATUS_ALIVE;
1097 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1099 av_type = PW_FRAMED;
1100 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1103 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1105 av_type = PW_RADIUS;
1106 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1108 /* Update link stats */
1109 update_link_stats(0);
1111 if (link_stats_valid) {
1112 link_stats_valid = 0; /* Force later code to update */
1114 av_type = link_connect_time;
1115 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1117 av_type = link_stats.bytes_out;
1118 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1120 av_type = link_stats.bytes_in;
1121 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1123 av_type = link_stats.pkts_out;
1124 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1126 av_type = link_stats.pkts_in;
1127 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1130 if (*remote_number) {
1131 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1132 remote_number, 0, VENDOR_NONE);
1134 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1136 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1137 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1139 hisaddr = ho->hisaddr;
1140 av_type = htonl(hisaddr);
1141 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1143 /* Add user specified vp's */
1145 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1147 if (rstate.acctserver) {
1148 result = rc_acct_using_server(rstate.acctserver,
1149 rstate.client_port, send);
1151 result = rc_acct(rstate.client_port, send);
1154 if (result != OK_RC) {
1155 /* RADIUS server could be down so make this a warning */
1157 "Interim accounting failed for %s", rstate.user);
1159 rc_avpair_free(send);
1161 /* Schedule another one */
1162 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1165 /**********************************************************************
1166 * %FUNCTION: radius_ip_up
1173 * Called when IPCP is up. We'll do a start-accounting record.
1174 ***********************************************************************/
1176 radius_ip_up(void *opaque, int arg)
1178 radius_acct_start();
1181 /**********************************************************************
1182 * %FUNCTION: radius_ip_down
1189 * Called when IPCP is down. We'll do a stop-accounting record.
1190 ***********************************************************************/
1192 radius_ip_down(void *opaque, int arg)
1197 /**********************************************************************
1198 * %FUNCTION: radius_init
1200 * msg -- buffer of size BUF_LEN for error message
1202 * negative on failure; non-negative on success
1204 * Initializes radiusclient library
1205 ***********************************************************************/
1207 radius_init(char *msg)
1209 if (rstate.initialized) {
1213 if (config_file && *config_file) {
1214 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1217 rstate.initialized = 1;
1219 if (rc_read_config(rstate.config_file) != 0) {
1220 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1221 rstate.config_file);
1225 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1226 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1227 rc_conf_str("dictionary"));
1231 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1232 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1233 rc_conf_str("mapfile"));
1237 /* Add av pairs saved during option parsing */
1239 struct avpopt *n = avpopt->next;
1241 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1242 free(avpopt->vpstr);
1249 /**********************************************************************
1250 * %FUNCTION: get_client_port
1252 * ifname -- PPP interface name (e.g. "ppp7")
1254 * The NAS port number (e.g. 7)
1256 * Extracts the port number from the interface name
1257 ***********************************************************************/
1259 get_client_port(char *ifname)
1262 if (sscanf(ifname, "ppp%d", &port) == 1) {
1265 return rc_map2id(ifname);
1268 /**********************************************************************
1269 * %FUNCTION: radius_allowed_address
1271 * addr -- IP address
1273 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1275 ***********************************************************************/
1277 radius_allowed_address(u_int32_t addr)
1279 ipcp_options *wo = &ipcp_wantoptions[0];
1281 if (!rstate.choose_ip) {
1282 /* If RADIUS server said any address is OK, then fine... */
1283 if (rstate.any_ip_addr_ok) {
1287 /* Sigh... if an address was supplied for remote host in pppd
1288 options, it has to match that. */
1289 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1295 if (addr == rstate.ip_addr) return 1;
1299 /* Useful for other plugins */
1300 char *radius_logged_in_user(void)