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.32 2008/05/26 09:18:08 paulus Exp $";
37 #include "radiusclient.h"
41 #include <sys/types.h>
44 #include <netinet/in.h>
49 #define MD5_HASH_SIZE 16
53 static char *config_file = NULL;
54 static int add_avp(char **);
55 static struct avpopt {
59 static bool portnummap = 0;
61 static option_t Options[] = {
62 { "radius-config-file", o_string, &config_file },
63 { "avpair", o_special, add_avp },
64 { "map-to-ttyname", o_bool, &portnummap,
65 "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
66 { "map-to-ifname", o_bool, &portnummap,
67 "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
71 static int radius_secret_check(void);
72 static int radius_pap_auth(char *user,
75 struct wordlist **paddrs,
76 struct wordlist **popts);
77 static int radius_chap_verify(char *user, char *ourname, int id,
78 struct chap_digest_type *digest,
79 unsigned char *challenge,
80 unsigned char *response,
81 char *message, int message_space);
83 static void radius_ip_up(void *opaque, int arg);
84 static void radius_ip_down(void *opaque, int arg);
85 static void make_username_realm(char *user);
86 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
87 struct chap_digest_type *digest,
88 unsigned char *challenge,
89 char *message, int message_space);
90 static void radius_choose_ip(u_int32_t *addrp);
91 static int radius_init(char *msg);
92 static int get_client_port(char *ifname);
93 static int radius_allowed_address(u_int32_t addr);
94 static void radius_acct_interim(void *);
96 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
98 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
102 #define MAXSESSIONID 32
106 #define MAXCLASSLEN 500
109 struct radius_state {
110 int accounting_started;
117 char user[MAXNAMELEN];
118 char config_file[MAXPATHLEN];
119 char session_id[MAXSESSIONID + 1];
121 int acct_interim_interval;
122 SERVER *authserver; /* Authentication server to use */
123 SERVER *acctserver; /* Accounting server to use */
125 char class[MAXCLASSLEN];
126 VALUE_PAIR *avp; /* Additional (user supplied) vp's to send to server */
129 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
131 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
132 In that case, they override the values in the radiusclient.conf file */
133 void (*radius_pre_auth_hook)(char const *user,
135 SERVER **acctserver) = NULL;
137 static struct radius_state rstate;
139 char pppd_version[] = VERSION;
141 /**********************************************************************
142 * %FUNCTION: plugin_init
148 * Initializes RADIUS plugin.
149 ***********************************************************************/
153 pap_check_hook = radius_secret_check;
154 pap_auth_hook = radius_pap_auth;
156 chap_check_hook = radius_secret_check;
157 chap_verify_hook = radius_chap_verify;
159 ip_choose_hook = radius_choose_ip;
160 allowed_address_hook = radius_allowed_address;
162 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
163 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
165 memset(&rstate, 0, sizeof(rstate));
167 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
168 sizeof(rstate.config_file));
170 add_options(Options);
172 info("RADIUS plugin initialized.");
175 /**********************************************************************
178 * argv -- the <attribute=value> pair to add
182 * Adds an av pair to be passed on to the RADIUS server on each request.
183 ***********************************************************************/
187 struct avpopt *p = malloc(sizeof(struct avpopt));
189 /* Append to a list of vp's for later parsing */
190 p->vpstr = strdup(*argv);
197 /**********************************************************************
198 * %FUNCTION: radius_secret_check
202 * 1 -- we are ALWAYS willing to supply a secret. :-)
204 * Tells pppd that we will try to authenticate the peer, and not to
205 * worry about looking in /etc/ppp/*-secrets
206 ***********************************************************************/
208 radius_secret_check(void)
213 /**********************************************************************
214 * %FUNCTION: radius_choose_ip
216 * addrp -- where to store the IP address
220 * If RADIUS server has specified an IP address, it is stored in *addrp.
221 ***********************************************************************/
223 radius_choose_ip(u_int32_t *addrp)
225 if (rstate.choose_ip) {
226 *addrp = rstate.ip_addr;
230 /**********************************************************************
231 * %FUNCTION: radius_pap_auth
233 * user -- user-name of peer
234 * passwd -- password supplied by peer
235 * msgp -- Message which will be sent in PAP response
236 * paddrs -- set to a list of possible peer IP addresses
237 * popts -- set to a list of additional pppd options
239 * 1 if we can authenticate, -1 if we cannot.
241 * Performs PAP authentication using RADIUS
242 ***********************************************************************/
244 radius_pap_auth(char *user,
247 struct wordlist **paddrs,
248 struct wordlist **popts)
250 VALUE_PAIR *send, *received;
253 static char radius_msg[BUF_LEN];
258 if (radius_init(radius_msg) < 0) {
262 /* Put user with potentially realm added in rstate.user */
263 make_username_realm(user);
265 if (radius_pre_auth_hook) {
266 radius_pre_auth_hook(rstate.user,
274 /* Hack... the "port" is the ppp interface number. Should really be
276 rstate.client_port = get_client_port(portnummap ? devnam : ifname);
279 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
282 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
284 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
285 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
286 if (*remote_number) {
287 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
290 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
292 /* Add user specified vp's */
294 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
296 if (rstate.authserver) {
297 result = rc_auth_using_server(rstate.authserver,
298 rstate.client_port, send,
299 &received, radius_msg, NULL);
301 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
304 if (result == OK_RC) {
305 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
310 /* free value pairs */
311 rc_avpair_free(received);
312 rc_avpair_free(send);
314 return (result == OK_RC) ? 1 : 0;
317 /**********************************************************************
318 * %FUNCTION: radius_chap_verify
320 * user -- name of the peer
321 * ourname -- name for this machine
322 * id -- the ID byte in the challenge
323 * digest -- points to the structure representing the digest type
324 * challenge -- the challenge string we sent (length in first byte)
325 * response -- the response (hash) the peer sent back (length in 1st byte)
326 * message -- space for a message to be returned to the peer
327 * message_space -- number of bytes available at *message.
329 * 1 if the response is good, 0 if it is bad
331 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
332 ***********************************************************************/
334 radius_chap_verify(char *user, char *ourname, int id,
335 struct chap_digest_type *digest,
336 unsigned char *challenge, unsigned char *response,
337 char *message, int message_space)
339 VALUE_PAIR *send, *received;
341 static char radius_msg[BUF_LEN];
343 int challenge_len, response_len;
344 u_char cpassword[MAX_RESPONSE_LEN + 1];
346 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
347 REQUEST_INFO request_info, *req_info = &request_info;
349 REQUEST_INFO *req_info = NULL;
352 challenge_len = *challenge++;
353 response_len = *response++;
357 if (radius_init(radius_msg) < 0) {
358 error("%s", radius_msg);
362 /* return error for types we can't handle */
363 if ((digest->code != CHAP_MD5)
365 && (digest->code != CHAP_MICROSOFT)
366 && (digest->code != CHAP_MICROSOFT_V2)
369 error("RADIUS: Challenge type %u unsupported", digest->code);
373 /* Put user with potentially realm added in rstate.user */
374 if (!rstate.done_chap_once) {
375 make_username_realm(user);
376 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
377 if (radius_pre_auth_hook) {
378 radius_pre_auth_hook(rstate.user,
384 send = received = NULL;
387 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
390 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
392 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
395 * add the challenge and response fields
397 switch (digest->code) {
399 /* CHAP-Challenge and CHAP-Password */
400 if (response_len != MD5_HASH_SIZE)
403 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
405 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
406 challenge, challenge_len, VENDOR_NONE);
407 rc_avpair_add(&send, PW_CHAP_PASSWORD,
408 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
414 /* MS-CHAP-Challenge and MS-CHAP-Response */
415 u_char *p = cpassword;
417 if (response_len != MS_CHAP_RESPONSE_LEN)
420 /* The idiots use a different field order in RADIUS than PPP */
421 *p++ = response[MS_CHAP_USENT];
422 memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
424 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
425 challenge, challenge_len, VENDOR_MICROSOFT);
426 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
427 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
431 case CHAP_MICROSOFT_V2:
433 /* MS-CHAP-Challenge and MS-CHAP2-Response */
434 u_char *p = cpassword;
436 if (response_len != MS_CHAP2_RESPONSE_LEN)
439 /* The idiots use a different field order in RADIUS than PPP */
440 *p++ = response[MS_CHAP2_FLAGS];
441 memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
442 + MS_CHAP2_NTRESP_LEN));
444 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
445 challenge, challenge_len, VENDOR_MICROSOFT);
446 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
447 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
453 if (*remote_number) {
454 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
457 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
459 /* Add user specified vp's */
461 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
464 * make authentication with RADIUS server
467 if (rstate.authserver) {
468 result = rc_auth_using_server(rstate.authserver,
469 rstate.client_port, send,
470 &received, radius_msg, req_info);
472 result = rc_auth(rstate.client_port, send, &received, radius_msg,
476 strlcpy(message, radius_msg, message_space);
478 if (result == OK_RC) {
479 if (!rstate.done_chap_once) {
480 if (radius_setparams(received, radius_msg, req_info, digest,
481 challenge, message, message_space) < 0) {
482 error("%s", radius_msg);
485 rstate.done_chap_once = 1;
490 rc_avpair_free(received);
491 rc_avpair_free (send);
492 return (result == OK_RC);
495 /**********************************************************************
496 * %FUNCTION: make_username_realm
498 * user -- the user given to pppd
502 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
503 * then the default realm from the radiusclient config file is added.
504 ***********************************************************************/
506 make_username_realm(char *user)
510 if ( user != NULL ) {
511 strlcpy(rstate.user, user, sizeof(rstate.user));
516 default_realm = rc_conf_str("default_realm");
518 if (!strchr(rstate.user, '@') &&
520 (*default_realm != '\0')) {
521 strlcat(rstate.user, "@", sizeof(rstate.user));
522 strlcat(rstate.user, default_realm, sizeof(rstate.user));
526 /**********************************************************************
527 * %FUNCTION: radius_setparams
529 * vp -- received value-pairs
530 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
532 * >= 0 on success; -1 on failure
534 * Parses attributes sent by RADIUS server and sets them in pppd.
535 ***********************************************************************/
537 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
538 struct chap_digest_type *digest, unsigned char *challenge,
539 char *message, int message_space)
542 int ms_chap2_success = 0;
544 int mppe_enc_keys = 0; /* whether or not these were received */
545 int mppe_enc_policy = 0;
546 int mppe_enc_types = 0;
549 ipcp_options *wo = &ipcp_wantoptions[0];
550 ipcp_options *ao = &ipcp_allowoptions[0];
557 /* Send RADIUS attributes to anyone else who might be interested */
558 if (radius_attributes_hook) {
559 (*radius_attributes_hook)(vp);
563 * service type (if not framed then quit),
564 * new IP address (RADIUS can define static IP for some users),
568 if (vp->vendorcode == VENDOR_NONE) {
569 switch (vp->attribute) {
570 case PW_SERVICE_TYPE:
571 /* check for service type */
572 /* if not FRAMED then exit */
573 if (vp->lvalue != PW_FRAMED) {
574 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
575 vp->lvalue, rstate.user);
580 case PW_FRAMED_PROTOCOL:
581 /* check for framed protocol type */
582 /* if not PPP then also exit */
583 if (vp->lvalue != PW_PPP) {
584 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
585 vp->lvalue, rstate.user);
590 case PW_SESSION_TIMEOUT:
591 /* Session timeout */
592 maxconnect = vp->lvalue;
595 /* packet filter, will be handled via ip-(up|down) script */
596 script_setenv("RADIUS_FILTER_ID", vp->strvalue, 1);
598 case PW_FRAMED_ROUTE:
599 /* route, will be handled via ip-(up|down) script */
600 script_setenv("RADIUS_FRAMED_ROUTE", vp->strvalue, 1);
602 case PW_IDLE_TIMEOUT:
604 idle_time_limit = vp->lvalue;
607 case PW_SESSION_OCTETS_LIMIT:
608 /* Session traffic limit */
609 maxoctets = vp->lvalue;
611 case PW_OCTETS_DIRECTION:
612 /* Session traffic limit direction check */
613 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
616 case PW_ACCT_INTERIM_INTERVAL:
617 /* Send accounting updates every few seconds */
618 rstate.acct_interim_interval = vp->lvalue;
619 /* RFC says it MUST NOT be less than 60 seconds */
620 /* We use "0" to signify not sending updates */
621 if (rstate.acct_interim_interval &&
622 rstate.acct_interim_interval < 60) {
623 rstate.acct_interim_interval = 60;
626 case PW_FRAMED_IP_ADDRESS:
627 /* seting up remote IP addresses */
629 if (remote == 0xffffffff) {
630 /* 0xffffffff means user should be allowed to select one */
631 rstate.any_ip_addr_ok = 1;
632 } else if (remote != 0xfffffffe) {
633 /* 0xfffffffe means NAS should select an ip address */
634 remote = htonl(vp->lvalue);
635 if (bad_ip_adrs (remote)) {
636 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
637 remote, rstate.user);
640 rstate.choose_ip = 1;
641 rstate.ip_addr = remote;
644 case PW_NAS_IP_ADDRESS:
645 wo->ouraddr = htonl(vp->lvalue);
648 /* Save Class attribute to pass it in accounting request */
649 if (vp->lvalue <= MAXCLASSLEN) {
650 rstate.class_len=vp->lvalue;
651 memcpy(rstate.class, vp->strvalue, rstate.class_len);
652 } /* else too big for our buffer - ignore it */
657 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
659 switch (vp->attribute) {
660 case PW_MS_CHAP2_SUCCESS:
661 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
662 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
666 strlcpy(message, vp->strvalue + 1, message_space);
667 ms_chap2_success = 1;
671 case PW_MS_CHAP_MPPE_KEYS:
672 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
673 slprintf(msg, BUF_LEN,
674 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
680 case PW_MS_MPPE_SEND_KEY:
681 case PW_MS_MPPE_RECV_KEY:
682 if (radius_setmppekeys2(vp, req_info) < 0) {
683 slprintf(msg, BUF_LEN,
684 "RADIUS: bad MS-MPPE-%s-Key attribute",
685 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
692 case PW_MS_MPPE_ENCRYPTION_POLICY:
693 mppe_enc_policy = vp->lvalue; /* save for later */
696 case PW_MS_MPPE_ENCRYPTION_TYPES:
697 mppe_enc_types = vp->lvalue; /* save for later */
702 case PW_MS_PRIMARY_DNS_SERVER:
703 ao->dnsaddr[0] = htonl(vp->lvalue);
706 ao->dnsaddr[1] = ao->dnsaddr[0];
708 case PW_MS_SECONDARY_DNS_SERVER:
709 ao->dnsaddr[1] = htonl(vp->lvalue);
712 ao->dnsaddr[0] = ao->dnsaddr[1];
714 case PW_MS_PRIMARY_NBNS_SERVER:
715 ao->winsaddr[0] = htonl(vp->lvalue);
718 ao->winsaddr[1] = ao->winsaddr[0];
720 case PW_MS_SECONDARY_NBNS_SERVER:
721 ao->winsaddr[1] = htonl(vp->lvalue);
724 ao->winsaddr[0] = ao->winsaddr[1];
733 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
734 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
739 * Require both policy and key attributes to indicate a valid key.
740 * Note that if the policy value was '0' we don't set the key!
742 if (mppe_enc_policy && mppe_enc_keys) {
744 /* Set/modify allowed encryption types. */
746 set_mppe_enc_types(mppe_enc_policy, mppe_enc_types);
754 /**********************************************************************
755 * %FUNCTION: radius_setmppekeys
757 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
758 * req_info -- radius request information used for encryption
760 * >= 0 on success; -1 on failure
762 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
764 ***********************************************************************/
766 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
767 unsigned char *challenge)
774 if (vp->lvalue != 32) {
775 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
780 memcpy(plain, vp->strvalue, sizeof(plain));
783 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
784 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
785 MD5_Final(buf, &Context);
787 for (i = 0; i < 16; i++)
791 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
792 MD5_Update(&Context, vp->strvalue, 16);
793 MD5_Final(buf, &Context);
795 for(i = 0; i < 16; i++)
796 plain[i + 16] ^= buf[i];
799 * Annoying. The "key" returned is just the NTPasswordHashHash, which
800 * the NAS (us) doesn't need; we only need the start key. So we have
801 * to generate the start key, sigh. NB: We do not support the LM-Key.
803 mppe_set_keys(challenge, &plain[8]);
808 /**********************************************************************
809 * %FUNCTION: radius_setmppekeys2
811 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
812 * req_info -- radius request information used for encryption
814 * >= 0 on success; -1 on failure
816 * Decrypt the key provided by the RADIUS server for MPPE encryption.
818 ***********************************************************************/
820 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
824 u_char *salt = vp->strvalue;
825 u_char *crypt = vp->strvalue + 2;
827 u_char buf[MD5_HASH_SIZE];
830 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
833 if (vp->lvalue != 34) {
834 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
839 if ((salt[0] & 0x80) == 0) {
840 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
844 memcpy(plain, crypt, 32);
847 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
848 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
849 MD5_Update(&Context, salt, 2);
850 MD5_Final(buf, &Context);
852 for (i = 0; i < 16; i++)
855 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
856 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
857 (int) plain[0], type);
862 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
863 MD5_Update(&Context, crypt, 16);
864 MD5_Final(buf, &Context);
866 plain[16] ^= buf[0]; /* only need the first byte */
868 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
869 memcpy(mppe_send_key, plain + 1, 16);
871 memcpy(mppe_recv_key, plain + 1, 16);
877 /**********************************************************************
878 * %FUNCTION: radius_acct_start
884 * Sends a "start" accounting message to the RADIUS server.
885 ***********************************************************************/
887 radius_acct_start(void)
891 VALUE_PAIR *send = NULL;
892 ipcp_options *ho = &ipcp_hisoptions[0];
895 if (!rstate.initialized) {
899 rstate.start_time = time(NULL);
901 strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
903 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
904 rstate.session_id, 0, VENDOR_NONE);
905 rc_avpair_add(&send, PW_USER_NAME,
906 rstate.user, 0, VENDOR_NONE);
908 if (rstate.class_len > 0)
909 rc_avpair_add(&send, PW_CLASS,
910 rstate.class, rstate.class_len, VENDOR_NONE);
912 av_type = PW_STATUS_START;
913 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
916 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
919 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
921 if (*remote_number) {
922 rc_avpair_add(&send, PW_CALLING_STATION_ID,
923 remote_number, 0, VENDOR_NONE);
925 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
928 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
931 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
932 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
934 hisaddr = ho->hisaddr;
935 av_type = htonl(hisaddr);
936 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
938 /* Add user specified vp's */
940 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
942 if (rstate.acctserver) {
943 result = rc_acct_using_server(rstate.acctserver,
944 rstate.client_port, send);
946 result = rc_acct(rstate.client_port, send);
949 rc_avpair_free(send);
951 if (result != OK_RC) {
952 /* RADIUS server could be down so make this a warning */
954 "Accounting START failed for %s", rstate.user);
956 rstate.accounting_started = 1;
957 /* Kick off periodic accounting reports */
958 if (rstate.acct_interim_interval) {
959 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
964 /**********************************************************************
965 * %FUNCTION: radius_acct_stop
971 * Sends a "stop" accounting message to the RADIUS server.
972 ***********************************************************************/
974 radius_acct_stop(void)
977 VALUE_PAIR *send = NULL;
978 ipcp_options *ho = &ipcp_hisoptions[0];
982 if (!rstate.initialized) {
986 if (!rstate.accounting_started) {
990 if (rstate.acct_interim_interval)
991 UNTIMEOUT(radius_acct_interim, NULL);
993 rstate.accounting_started = 0;
994 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
997 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
999 if (rstate.class_len > 0)
1000 rc_avpair_add(&send, PW_CLASS,
1001 rstate.class, rstate.class_len, VENDOR_NONE);
1003 av_type = PW_STATUS_STOP;
1004 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1006 av_type = PW_FRAMED;
1007 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1010 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1012 av_type = PW_RADIUS;
1013 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1016 if (link_stats_valid) {
1017 av_type = link_connect_time;
1018 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1020 av_type = link_stats.bytes_out;
1021 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1023 av_type = link_stats.bytes_in;
1024 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1026 av_type = link_stats.pkts_out;
1027 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1029 av_type = link_stats.pkts_in;
1030 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1033 if (*remote_number) {
1034 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1035 remote_number, 0, VENDOR_NONE);
1037 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1039 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1040 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1042 av_type = PW_NAS_ERROR;
1045 case EXIT_USER_REQUEST:
1046 av_type = PW_USER_REQUEST;
1050 case EXIT_PEER_DEAD:
1051 case EXIT_CONNECT_FAILED:
1052 av_type = PW_LOST_CARRIER;
1055 case EXIT_INIT_FAILED:
1056 case EXIT_OPEN_FAILED:
1057 case EXIT_LOCK_FAILED:
1058 case EXIT_PTYCMD_FAILED:
1059 av_type = PW_PORT_ERROR;
1062 case EXIT_PEER_AUTH_FAILED:
1063 case EXIT_AUTH_TOPEER_FAILED:
1064 case EXIT_NEGOTIATION_FAILED:
1065 case EXIT_CNID_AUTH_FAILED:
1066 av_type = PW_SERVICE_UNAVAILABLE;
1069 case EXIT_IDLE_TIMEOUT:
1070 av_type = PW_ACCT_IDLE_TIMEOUT;
1074 av_type = PW_CALLBACK;
1077 case EXIT_CONNECT_TIME:
1078 av_type = PW_ACCT_SESSION_TIMEOUT;
1082 case EXIT_TRAFFIC_LIMIT:
1083 av_type = PW_NAS_REQUEST;
1088 av_type = PW_NAS_ERROR;
1091 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1093 hisaddr = ho->hisaddr;
1094 av_type = htonl(hisaddr);
1095 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1097 /* Add user specified vp's */
1099 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1101 if (rstate.acctserver) {
1102 result = rc_acct_using_server(rstate.acctserver,
1103 rstate.client_port, send);
1105 result = rc_acct(rstate.client_port, send);
1108 if (result != OK_RC) {
1109 /* RADIUS server could be down so make this a warning */
1111 "Accounting STOP failed for %s", rstate.user);
1113 rc_avpair_free(send);
1116 /**********************************************************************
1117 * %FUNCTION: radius_acct_interim
1123 * Sends an interim accounting message to the RADIUS server
1124 ***********************************************************************/
1126 radius_acct_interim(void *ignored)
1129 VALUE_PAIR *send = NULL;
1130 ipcp_options *ho = &ipcp_hisoptions[0];
1134 if (!rstate.initialized) {
1138 if (!rstate.accounting_started) {
1142 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1145 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1147 if (rstate.class_len > 0)
1148 rc_avpair_add(&send, PW_CLASS,
1149 rstate.class, rstate.class_len, VENDOR_NONE);
1151 av_type = PW_STATUS_ALIVE;
1152 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1154 av_type = PW_FRAMED;
1155 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1158 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1160 av_type = PW_RADIUS;
1161 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1163 /* Update link stats */
1164 update_link_stats(0);
1166 if (link_stats_valid) {
1167 link_stats_valid = 0; /* Force later code to update */
1169 av_type = link_connect_time;
1170 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1172 av_type = link_stats.bytes_out;
1173 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1175 av_type = link_stats.bytes_in;
1176 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1178 av_type = link_stats.pkts_out;
1179 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1181 av_type = link_stats.pkts_in;
1182 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1185 if (*remote_number) {
1186 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1187 remote_number, 0, VENDOR_NONE);
1189 rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1191 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1192 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1194 hisaddr = ho->hisaddr;
1195 av_type = htonl(hisaddr);
1196 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1198 /* Add user specified vp's */
1200 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1202 if (rstate.acctserver) {
1203 result = rc_acct_using_server(rstate.acctserver,
1204 rstate.client_port, send);
1206 result = rc_acct(rstate.client_port, send);
1209 if (result != OK_RC) {
1210 /* RADIUS server could be down so make this a warning */
1212 "Interim accounting failed for %s", rstate.user);
1214 rc_avpair_free(send);
1216 /* Schedule another one */
1217 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1220 /**********************************************************************
1221 * %FUNCTION: radius_ip_up
1228 * Called when IPCP is up. We'll do a start-accounting record.
1229 ***********************************************************************/
1231 radius_ip_up(void *opaque, int arg)
1233 radius_acct_start();
1236 /**********************************************************************
1237 * %FUNCTION: radius_ip_down
1244 * Called when IPCP is down. We'll do a stop-accounting record.
1245 ***********************************************************************/
1247 radius_ip_down(void *opaque, int arg)
1252 /**********************************************************************
1253 * %FUNCTION: radius_init
1255 * msg -- buffer of size BUF_LEN for error message
1257 * negative on failure; non-negative on success
1259 * Initializes radiusclient library
1260 ***********************************************************************/
1262 radius_init(char *msg)
1264 if (rstate.initialized) {
1268 if (config_file && *config_file) {
1269 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1272 rstate.initialized = 1;
1274 if (rc_read_config(rstate.config_file) != 0) {
1275 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1276 rstate.config_file);
1280 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1281 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1282 rc_conf_str("dictionary"));
1286 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1287 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1288 rc_conf_str("mapfile"));
1292 /* Add av pairs saved during option parsing */
1294 struct avpopt *n = avpopt->next;
1296 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1297 free(avpopt->vpstr);
1304 /**********************************************************************
1305 * %FUNCTION: get_client_port
1307 * ifname -- PPP interface name (e.g. "ppp7")
1309 * The NAS port number (e.g. 7)
1311 * Extracts the port number from the interface name
1312 ***********************************************************************/
1314 get_client_port(char *ifname)
1317 if (sscanf(ifname, "ppp%d", &port) == 1) {
1320 return rc_map2id(ifname);
1323 /**********************************************************************
1324 * %FUNCTION: radius_allowed_address
1326 * addr -- IP address
1328 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1330 ***********************************************************************/
1332 radius_allowed_address(u_int32_t addr)
1334 ipcp_options *wo = &ipcp_wantoptions[0];
1336 if (!rstate.choose_ip) {
1337 /* If RADIUS server said any address is OK, then fine... */
1338 if (rstate.any_ip_addr_ok) {
1342 /* Sigh... if an address was supplied for remote host in pppd
1343 options, it has to match that. */
1344 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1350 if (addr == rstate.ip_addr) return 1;
1354 /* Useful for other plugins */
1355 char *radius_logged_in_user(void)