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.27 2004/11/14 07:26:26 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,
289 /* Add user specified vp's */
291 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
293 if (rstate.authserver) {
294 result = rc_auth_using_server(rstate.authserver,
295 rstate.client_port, send,
296 &received, radius_msg, NULL);
298 result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
301 if (result == OK_RC) {
302 if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
307 /* free value pairs */
308 rc_avpair_free(received);
309 rc_avpair_free(send);
311 return (result == OK_RC) ? 1 : 0;
314 /**********************************************************************
315 * %FUNCTION: radius_chap_verify
317 * user -- name of the peer
318 * ourname -- name for this machine
319 * id -- the ID byte in the challenge
320 * digest -- points to the structure representing the digest type
321 * challenge -- the challenge string we sent (length in first byte)
322 * response -- the response (hash) the peer sent back (length in 1st byte)
323 * message -- space for a message to be returned to the peer
324 * message_space -- number of bytes available at *message.
326 * 1 if the response is good, 0 if it is bad
328 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
329 ***********************************************************************/
331 radius_chap_verify(char *user, char *ourname, int id,
332 struct chap_digest_type *digest,
333 unsigned char *challenge, unsigned char *response,
334 char *message, int message_space)
336 VALUE_PAIR *send, *received;
338 static char radius_msg[BUF_LEN];
340 int challenge_len, response_len;
341 u_char cpassword[MAX_RESPONSE_LEN + 1];
343 /* Need the RADIUS secret and Request Authenticator to decode MPPE */
344 REQUEST_INFO request_info, *req_info = &request_info;
346 REQUEST_INFO *req_info = NULL;
349 challenge_len = *challenge++;
350 response_len = *response++;
354 if (radius_init(radius_msg) < 0) {
355 error("%s", radius_msg);
359 /* return error for types we can't handle */
360 if ((digest->code != CHAP_MD5)
362 && (digest->code != CHAP_MICROSOFT)
363 && (digest->code != CHAP_MICROSOFT_V2)
366 error("RADIUS: Challenge type %u unsupported", digest->code);
370 /* Put user with potentially realm added in rstate.user */
371 if (!rstate.done_chap_once) {
372 make_username_realm(user);
373 rstate.client_port = get_client_port (portnummap ? devnam : ifname);
374 if (radius_pre_auth_hook) {
375 radius_pre_auth_hook(rstate.user,
381 send = received = NULL;
384 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
387 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
389 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
392 * add the challenge and response fields
394 switch (digest->code) {
396 /* CHAP-Challenge and CHAP-Password */
397 if (response_len != MD5_HASH_SIZE)
400 memcpy(&cpassword[1], response, MD5_HASH_SIZE);
402 rc_avpair_add(&send, PW_CHAP_CHALLENGE,
403 challenge, challenge_len, VENDOR_NONE);
404 rc_avpair_add(&send, PW_CHAP_PASSWORD,
405 cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
411 /* MS-CHAP-Challenge and MS-CHAP-Response */
412 MS_ChapResponse *rmd = (MS_ChapResponse *) 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 memcpy(p, rmd->UseNT, sizeof(rmd->UseNT));
420 p += sizeof(rmd->UseNT);
421 memcpy(p, rmd->LANManResp, sizeof(rmd->LANManResp));
422 p += sizeof(rmd->LANManResp);
423 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
425 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
426 challenge, challenge_len, VENDOR_MICROSOFT);
427 rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
428 cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
432 case CHAP_MICROSOFT_V2:
434 /* MS-CHAP-Challenge and MS-CHAP2-Response */
435 MS_Chap2Response *rmd = (MS_Chap2Response *) response;
436 u_char *p = cpassword;
438 if (response_len != MS_CHAP2_RESPONSE_LEN)
441 /* The idiots use a different field order in RADIUS than PPP */
442 memcpy(p, rmd->Flags, sizeof(rmd->Flags));
443 p += sizeof(rmd->Flags);
444 memcpy(p, rmd->PeerChallenge, sizeof(rmd->PeerChallenge));
445 p += sizeof(rmd->PeerChallenge);
446 memcpy(p, rmd->Reserved, sizeof(rmd->Reserved));
447 p += sizeof(rmd->Reserved);
448 memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
450 rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
451 challenge, challenge_len, VENDOR_MICROSOFT);
452 rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
453 cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
459 if (*remote_number) {
460 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
464 /* Add user specified vp's */
466 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
469 * make authentication with RADIUS server
472 if (rstate.authserver) {
473 result = rc_auth_using_server(rstate.authserver,
474 rstate.client_port, send,
475 &received, radius_msg, req_info);
477 result = rc_auth(rstate.client_port, send, &received, radius_msg,
481 if (result == OK_RC) {
482 if (!rstate.done_chap_once) {
483 if (radius_setparams(received, radius_msg, req_info, digest,
484 challenge, message, message_space) < 0) {
485 error("%s", radius_msg);
488 rstate.done_chap_once = 1;
493 rc_avpair_free(received);
494 rc_avpair_free (send);
495 return (result == OK_RC);
498 /**********************************************************************
499 * %FUNCTION: make_username_realm
501 * user -- the user given to pppd
505 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
506 * then the default realm from the radiusclient config file is added.
507 ***********************************************************************/
509 make_username_realm(char *user)
513 if ( user != NULL ) {
514 strlcpy(rstate.user, user, sizeof(rstate.user));
519 default_realm = rc_conf_str("default_realm");
521 if (!strchr(rstate.user, '@') &&
523 (*default_realm != '\0')) {
524 strlcat(rstate.user, "@", sizeof(rstate.user));
525 strlcat(rstate.user, default_realm, sizeof(rstate.user));
529 /**********************************************************************
530 * %FUNCTION: radius_setparams
532 * vp -- received value-pairs
533 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
535 * >= 0 on success; -1 on failure
537 * Parses attributes sent by RADIUS server and sets them in pppd.
538 ***********************************************************************/
540 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
541 struct chap_digest_type *digest, unsigned char *challenge,
542 char *message, int message_space)
545 int ms_chap2_success = 0;
547 int mppe_enc_keys = 0; /* whether or not these were received */
548 int mppe_enc_policy = 0;
549 int mppe_enc_types = 0;
552 /* Send RADIUS attributes to anyone else who might be interested */
553 if (radius_attributes_hook) {
554 (*radius_attributes_hook)(vp);
558 * service type (if not framed then quit),
559 * new IP address (RADIUS can define static IP for some users),
563 if (vp->vendorcode == VENDOR_NONE) {
564 switch (vp->attribute) {
565 case PW_SERVICE_TYPE:
566 /* check for service type */
567 /* if not FRAMED then exit */
568 if (vp->lvalue != PW_FRAMED) {
569 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
570 vp->lvalue, rstate.user);
575 case PW_FRAMED_PROTOCOL:
576 /* check for framed protocol type */
577 /* if not PPP then also exit */
578 if (vp->lvalue != PW_PPP) {
579 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
580 vp->lvalue, rstate.user);
585 case PW_SESSION_TIMEOUT:
586 /* Session timeout */
587 maxconnect = vp->lvalue;
590 case PW_SESSION_OCTETS_LIMIT:
591 /* Session traffic limit */
592 maxoctets = vp->lvalue;
594 case PW_OCTETS_DIRECTION:
595 /* Session traffic limit direction check */
596 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
599 case PW_ACCT_INTERIM_INTERVAL:
600 /* Send accounting updates every few seconds */
601 rstate.acct_interim_interval = vp->lvalue;
602 /* RFC says it MUST NOT be less than 60 seconds */
603 /* We use "0" to signify not sending updates */
604 if (rstate.acct_interim_interval &&
605 rstate.acct_interim_interval < 60) {
606 rstate.acct_interim_interval = 60;
609 case PW_FRAMED_IP_ADDRESS:
610 /* seting up remote IP addresses */
612 if (remote == 0xffffffff) {
613 /* 0xffffffff means user should be allowed to select one */
614 rstate.any_ip_addr_ok = 1;
615 } else if (remote != 0xfffffffe) {
616 /* 0xfffffffe means NAS should select an ip address */
617 remote = htonl(vp->lvalue);
618 if (bad_ip_adrs (remote)) {
619 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
620 remote, rstate.user);
623 rstate.choose_ip = 1;
624 rstate.ip_addr = remote;
628 /* Save Class attribute to pass it in accounting request */
629 if (vp->lvalue <= MAXCLASSLEN) {
630 rstate.class_len=vp->lvalue;
631 memcpy(rstate.class, vp->strvalue, rstate.class_len);
632 } /* else too big for our buffer - ignore it */
638 } else if (vp->vendorcode == VENDOR_MICROSOFT) {
639 switch (vp->attribute) {
640 case PW_MS_CHAP2_SUCCESS:
641 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
642 slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
646 strlcpy(message, vp->strvalue + 1, message_space);
647 ms_chap2_success = 1;
651 case PW_MS_CHAP_MPPE_KEYS:
652 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
653 slprintf(msg, BUF_LEN,
654 "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
660 case PW_MS_MPPE_SEND_KEY:
661 case PW_MS_MPPE_RECV_KEY:
662 if (radius_setmppekeys2(vp, req_info) < 0) {
663 slprintf(msg, BUF_LEN,
664 "RADIUS: bad MS-MPPE-%s-Key attribute",
665 (vp->attribute == PW_MS_MPPE_SEND_KEY)?
672 case PW_MS_MPPE_ENCRYPTION_POLICY:
673 mppe_enc_policy = vp->lvalue; /* save for later */
676 case PW_MS_MPPE_ENCRYPTION_TYPES:
677 mppe_enc_types = vp->lvalue; /* save for later */
682 case PW_MS_PRIMARY_DNS_SERVER:
683 case PW_MS_SECONDARY_DNS_SERVER:
684 case PW_MS_PRIMARY_NBNS_SERVER:
685 case PW_MS_SECONDARY_NBNS_SERVER:
694 /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
695 if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
700 * Require both policy and key attributes to indicate a valid key.
701 * Note that if the policy value was '0' we don't set the key!
703 if (mppe_enc_policy && mppe_enc_keys) {
705 /* Set/modify allowed encryption types. */
707 set_mppe_enc_types(mppe_enc_policy, mppe_enc_types);
715 /**********************************************************************
716 * %FUNCTION: radius_setmppekeys
718 * vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
719 * req_info -- radius request information used for encryption
721 * >= 0 on success; -1 on failure
723 * Decrypt the "key" provided by the RADIUS server for MPPE encryption.
725 ***********************************************************************/
727 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
728 unsigned char *challenge)
735 if (vp->lvalue != 32) {
736 error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
741 memcpy(plain, vp->strvalue, sizeof(plain));
744 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
745 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
746 MD5_Final(buf, &Context);
748 for (i = 0; i < 16; i++)
752 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
753 MD5_Update(&Context, vp->strvalue, 16);
754 MD5_Final(buf, &Context);
756 for(i = 0; i < 16; i++)
757 plain[i + 16] ^= buf[i];
760 * Annoying. The "key" returned is just the NTPasswordHashHash, which
761 * the NAS (us) doesn't need; we only need the start key. So we have
762 * to generate the start key, sigh. NB: We do not support the LM-Key.
764 mppe_set_keys(challenge, &plain[8]);
769 /**********************************************************************
770 * %FUNCTION: radius_setmppekeys2
772 * vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
773 * req_info -- radius request information used for encryption
775 * >= 0 on success; -1 on failure
777 * Decrypt the key provided by the RADIUS server for MPPE encryption.
779 ***********************************************************************/
781 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
785 u_char *salt = vp->strvalue;
786 u_char *crypt = vp->strvalue + 2;
788 u_char buf[MD5_HASH_SIZE];
791 if (vp->attribute == PW_MS_MPPE_RECV_KEY)
794 if (vp->lvalue != 34) {
795 error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
800 if ((salt[0] & 0x80) == 0) {
801 error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
805 memcpy(plain, crypt, 32);
808 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
809 MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
810 MD5_Update(&Context, salt, 2);
811 MD5_Final(buf, &Context);
813 for (i = 0; i < 16; i++)
816 if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
817 error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
818 (int) plain[0], type);
823 MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
824 MD5_Update(&Context, crypt, 16);
825 MD5_Final(buf, &Context);
827 plain[16] ^= buf[0]; /* only need the first byte */
829 if (vp->attribute == PW_MS_MPPE_SEND_KEY)
830 memcpy(mppe_send_key, plain + 1, 16);
832 memcpy(mppe_recv_key, plain + 1, 16);
838 /**********************************************************************
839 * %FUNCTION: radius_acct_start
845 * Sends a "start" accounting message to the RADIUS server.
846 ***********************************************************************/
848 radius_acct_start(void)
852 VALUE_PAIR *send = NULL;
853 ipcp_options *ho = &ipcp_hisoptions[0];
856 if (!rstate.initialized) {
860 rstate.start_time = time(NULL);
862 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
864 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
865 rstate.session_id, 0, VENDOR_NONE);
866 rc_avpair_add(&send, PW_USER_NAME,
867 rstate.user, 0, VENDOR_NONE);
869 if (rstate.class_len > 0)
870 rc_avpair_add(&send, PW_CLASS,
871 rstate.class, rstate.class_len, VENDOR_NONE);
873 av_type = PW_STATUS_START;
874 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
877 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
880 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
882 if (*remote_number) {
883 rc_avpair_add(&send, PW_CALLING_STATION_ID,
884 remote_number, 0, VENDOR_NONE);
888 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
891 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
892 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
894 hisaddr = ho->hisaddr;
895 av_type = htonl(hisaddr);
896 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
898 /* Add user specified vp's */
900 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
902 if (rstate.acctserver) {
903 result = rc_acct_using_server(rstate.acctserver,
904 rstate.client_port, send);
906 result = rc_acct(rstate.client_port, send);
909 rc_avpair_free(send);
911 if (result != OK_RC) {
912 /* RADIUS server could be down so make this a warning */
914 "Accounting START failed for %s", rstate.user);
916 rstate.accounting_started = 1;
917 /* Kick off periodic accounting reports */
918 if (rstate.acct_interim_interval) {
919 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
924 /**********************************************************************
925 * %FUNCTION: radius_acct_stop
931 * Sends a "stop" accounting message to the RADIUS server.
932 ***********************************************************************/
934 radius_acct_stop(void)
937 VALUE_PAIR *send = NULL;
938 ipcp_options *ho = &ipcp_hisoptions[0];
942 if (!rstate.initialized) {
946 if (!rstate.accounting_started) {
950 rstate.accounting_started = 0;
951 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
954 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
956 av_type = PW_STATUS_STOP;
957 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
960 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
963 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
966 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
969 if (link_stats_valid) {
970 av_type = link_connect_time;
971 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
973 av_type = link_stats.bytes_out;
974 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
976 av_type = link_stats.bytes_in;
977 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
979 av_type = link_stats.pkts_out;
980 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
982 av_type = link_stats.pkts_in;
983 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
986 if (*remote_number) {
987 rc_avpair_add(&send, PW_CALLING_STATION_ID,
988 remote_number, 0, VENDOR_NONE);
991 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
992 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
994 av_type = PW_NAS_ERROR;
997 case EXIT_USER_REQUEST:
998 av_type = PW_USER_REQUEST;
1002 case EXIT_PEER_DEAD:
1003 case EXIT_CONNECT_FAILED:
1004 av_type = PW_LOST_CARRIER;
1007 case EXIT_INIT_FAILED:
1008 case EXIT_OPEN_FAILED:
1009 case EXIT_LOCK_FAILED:
1010 case EXIT_PTYCMD_FAILED:
1011 av_type = PW_PORT_ERROR;
1014 case EXIT_PEER_AUTH_FAILED:
1015 case EXIT_AUTH_TOPEER_FAILED:
1016 case EXIT_NEGOTIATION_FAILED:
1017 case EXIT_CNID_AUTH_FAILED:
1018 av_type = PW_SERVICE_UNAVAILABLE;
1021 case EXIT_IDLE_TIMEOUT:
1022 av_type = PW_ACCT_IDLE_TIMEOUT;
1025 case EXIT_CONNECT_TIME:
1026 av_type = PW_ACCT_SESSION_TIMEOUT;
1030 case EXIT_TRAFFIC_LIMIT:
1031 av_type = PW_NAS_REQUEST;
1036 av_type = PW_NAS_ERROR;
1039 rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1041 hisaddr = ho->hisaddr;
1042 av_type = htonl(hisaddr);
1043 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1045 /* Add user specified vp's */
1047 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1049 if (rstate.acctserver) {
1050 result = rc_acct_using_server(rstate.acctserver,
1051 rstate.client_port, send);
1053 result = rc_acct(rstate.client_port, send);
1056 if (result != OK_RC) {
1057 /* RADIUS server could be down so make this a warning */
1059 "Accounting STOP failed for %s", rstate.user);
1061 rc_avpair_free(send);
1064 /**********************************************************************
1065 * %FUNCTION: radius_acct_interim
1071 * Sends an interim accounting message to the RADIUS server
1072 ***********************************************************************/
1074 radius_acct_interim(void *ignored)
1077 VALUE_PAIR *send = NULL;
1078 ipcp_options *ho = &ipcp_hisoptions[0];
1082 if (!rstate.initialized) {
1086 if (!rstate.accounting_started) {
1090 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1093 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1095 av_type = PW_STATUS_ALIVE;
1096 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1098 av_type = PW_FRAMED;
1099 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1102 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1104 av_type = PW_RADIUS;
1105 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1107 /* Update link stats */
1108 update_link_stats(0);
1110 if (link_stats_valid) {
1111 link_stats_valid = 0; /* Force later code to update */
1113 av_type = link_connect_time;
1114 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1116 av_type = link_stats.bytes_out;
1117 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1119 av_type = link_stats.bytes_in;
1120 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1122 av_type = link_stats.pkts_out;
1123 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1125 av_type = link_stats.pkts_in;
1126 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1129 if (*remote_number) {
1130 rc_avpair_add(&send, PW_CALLING_STATION_ID,
1131 remote_number, 0, VENDOR_NONE);
1134 av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1135 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1137 hisaddr = ho->hisaddr;
1138 av_type = htonl(hisaddr);
1139 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1141 /* Add user specified vp's */
1143 rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1145 if (rstate.acctserver) {
1146 result = rc_acct_using_server(rstate.acctserver,
1147 rstate.client_port, send);
1149 result = rc_acct(rstate.client_port, send);
1152 if (result != OK_RC) {
1153 /* RADIUS server could be down so make this a warning */
1155 "Interim accounting failed for %s", rstate.user);
1157 rc_avpair_free(send);
1159 /* Schedule another one */
1160 TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1163 /**********************************************************************
1164 * %FUNCTION: radius_ip_up
1171 * Called when IPCP is up. We'll do a start-accounting record.
1172 ***********************************************************************/
1174 radius_ip_up(void *opaque, int arg)
1176 radius_acct_start();
1179 /**********************************************************************
1180 * %FUNCTION: radius_ip_down
1187 * Called when IPCP is down. We'll do a stop-accounting record.
1188 ***********************************************************************/
1190 radius_ip_down(void *opaque, int arg)
1195 /**********************************************************************
1196 * %FUNCTION: radius_init
1198 * msg -- buffer of size BUF_LEN for error message
1200 * negative on failure; non-negative on success
1202 * Initializes radiusclient library
1203 ***********************************************************************/
1205 radius_init(char *msg)
1207 if (rstate.initialized) {
1211 if (config_file && *config_file) {
1212 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1215 rstate.initialized = 1;
1217 if (rc_read_config(rstate.config_file) != 0) {
1218 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1219 rstate.config_file);
1223 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1224 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1225 rc_conf_str("dictionary"));
1229 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
1230 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1231 rc_conf_str("mapfile"));
1235 /* Add av pairs saved during option parsing */
1237 struct avpopt *n = avpopt->next;
1239 rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1240 free(avpopt->vpstr);
1247 /**********************************************************************
1248 * %FUNCTION: get_client_port
1250 * ifname -- PPP interface name (e.g. "ppp7")
1252 * The NAS port number (e.g. 7)
1254 * Extracts the port number from the interface name
1255 ***********************************************************************/
1257 get_client_port(char *ifname)
1260 if (sscanf(ifname, "ppp%d", &port) == 1) {
1263 return rc_map2id(ifname);
1266 /**********************************************************************
1267 * %FUNCTION: radius_allowed_address
1269 * addr -- IP address
1271 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
1273 ***********************************************************************/
1275 radius_allowed_address(u_int32_t addr)
1277 ipcp_options *wo = &ipcp_wantoptions[0];
1279 if (!rstate.choose_ip) {
1280 /* If RADIUS server said any address is OK, then fine... */
1281 if (rstate.any_ip_addr_ok) {
1285 /* Sigh... if an address was supplied for remote host in pppd
1286 options, it has to match that. */
1287 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1293 if (addr == rstate.ip_addr) return 1;
1297 /* Useful for other plugins */
1298 char *radius_logged_in_user(void)