2 * Copyright 1999 Internet Business Solutions Ltd., Switzerland
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.sbin/ppp/radius.c,v 1.11.2.5 2002/09/01 02:12:32 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/radius.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
31 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <arpa/inet.h>
39 #include <net/route.h>
43 #include "radlib_vs.h"
46 #include <radlib_vs.h>
65 #include "descriptor.h"
70 #include "slcompress.h"
71 #include "throughput.h"
99 struct mschap_response {
102 u_char lm_response[24];
103 u_char nt_response[24];
106 struct mschap2_response {
109 u_char pchallenge[16];
119 radius_policyname(int policy)
122 case MPPE_POLICY_ALLOWED:
124 case MPPE_POLICY_REQUIRED:
127 return NumStr(policy, NULL, 0);
131 radius_typesname(int types)
134 case MPPE_TYPE_40BIT:
136 case MPPE_TYPE_128BIT:
138 case MPPE_TYPE_40BIT|MPPE_TYPE_128BIT:
139 return "40 or 128 bit";
141 return NumStr(types, NULL, 0);
146 demangle(struct radius *r, const void *mangled, size_t mlen,
147 char **buf, size_t *len)
149 char R[AUTH_LEN]; /* variable names as per rfc2548 */
154 int Slen, i, Clen, Ppos;
157 if (mlen % 16 != SALT_LEN) {
158 log_Printf(LogWARN, "Cannot interpret mangled data of length %ld\n",
165 /* We need the RADIUS Request-Authenticator */
166 if (rad_request_authenticator(r->cx.rad, R, sizeof R) != AUTH_LEN) {
167 log_Printf(LogWARN, "Cannot obtain the RADIUS request authenticator\n");
173 A = (const u_char *)mangled; /* Salt comes first */
174 C = (const u_char *)mangled + SALT_LEN; /* Then the ciphertext */
175 Clen = mlen - SALT_LEN;
176 S = rad_server_secret(r->cx.rad); /* We need the RADIUS secret */
178 P = alloca(Clen); /* We derive our plaintext */
181 MD5Update(&Context, S, Slen);
182 MD5Update(&Context, R, AUTH_LEN);
183 MD5Update(&Context, A, SALT_LEN);
184 MD5Final(b, &Context);
190 for (i = 0; i < 16; i++)
191 P[Ppos++] = C[i] ^ b[i];
195 MD5Update(&Context, S, Slen);
196 MD5Update(&Context, C, 16);
197 MD5Final(b, &Context);
204 * The resulting plain text consists of a one-byte length, the text and
205 * maybe some padding.
208 if (*len > mlen - 1) {
209 log_Printf(LogWARN, "Mangled data seems to be garbage\n");
216 memcpy(*buf, P + 1, *len);
221 * rad_continue_send_request() has given us `got' (non-zero). Deal with it.
224 radius_Process(struct radius *r, int got)
226 char *argv[MAXARGS], *nuke;
227 struct bundle *bundle;
228 int argc, addrs, res, width;
230 struct ncprange dest;
234 u_int32_t ipaddr, vendor;
237 r->cx.fd = -1; /* Stop select()ing */
238 stype = r->cx.auth ? "auth" : "acct";
241 case RAD_ACCESS_ACCEPT:
242 log_Printf(LogPHASE, "Radius(%s): ACCEPT received\n", stype);
244 rad_close(r->cx.rad);
249 case RAD_ACCESS_REJECT:
250 log_Printf(LogPHASE, "Radius(%s): REJECT received\n", stype);
252 rad_close(r->cx.rad);
257 case RAD_ACCESS_CHALLENGE:
258 /* we can't deal with this (for now) ! */
259 log_Printf(LogPHASE, "Radius: CHALLENGE received (can't handle yet)\n");
261 auth_Failure(r->cx.auth);
262 rad_close(r->cx.rad);
265 case RAD_ACCOUNTING_RESPONSE:
266 log_Printf(LogPHASE, "Radius(%s): Accounting response received\n", stype);
268 auth_Failure(r->cx.auth); /* unexpected !!! */
270 /* No further processing for accounting requests, please */
271 rad_close(r->cx.rad);
275 log_Printf(LogPHASE, "radius(%s): %s\n", stype, rad_strerror(r->cx.rad));
277 auth_Failure(r->cx.auth);
278 rad_close(r->cx.rad);
282 log_Printf(LogERROR, "rad_send_request(%s): Failed %d: %s\n", stype,
283 got, rad_strerror(r->cx.rad));
285 auth_Failure(r->cx.auth);
286 rad_close(r->cx.rad);
290 /* Let's see what we've got in our reply */
291 r->ip.s_addr = r->mask.s_addr = INADDR_NONE;
294 while ((res = rad_get_attr(r->cx.rad, &data, &len)) > 0) {
296 case RAD_FRAMED_IP_ADDRESS:
297 r->ip = rad_cvt_addr(data);
298 log_Printf(LogPHASE, " IP %s\n", inet_ntoa(r->ip));
303 if ((r->filterid = rad_cvt_string(data, len)) == NULL) {
304 log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad));
305 auth_Failure(r->cx.auth);
306 rad_close(r->cx.rad);
309 log_Printf(LogPHASE, " Filter \"%s\"\n", r->filterid);
312 case RAD_SESSION_TIMEOUT:
313 r->sessiontime = rad_cvt_int(data);
314 log_Printf(LogPHASE, " Session-Timeout %lu\n", r->sessiontime);
317 case RAD_FRAMED_IP_NETMASK:
318 r->mask = rad_cvt_addr(data);
319 log_Printf(LogPHASE, " Netmask %s\n", inet_ntoa(r->mask));
323 r->mtu = rad_cvt_int(data);
324 log_Printf(LogPHASE, " MTU %lu\n", r->mtu);
327 case RAD_FRAMED_ROUTING:
328 /* Disabled for now - should we automatically set up some filters ? */
329 /* rad_cvt_int(data); */
330 /* bit 1 = Send routing packets */
331 /* bit 2 = Receive routing packets */
334 case RAD_FRAMED_COMPRESSION:
335 r->vj = rad_cvt_int(data) == 1 ? 1 : 0;
336 log_Printf(LogPHASE, " VJ %sabled\n", r->vj ? "en" : "dis");
339 case RAD_FRAMED_ROUTE:
341 * We expect a string of the format ``dest[/bits] gw [metrics]''
342 * Any specified metrics are ignored. MYADDR and HISADDR are
343 * understood for ``dest'' and ``gw'' and ``0.0.0.0'' is the same
347 if ((nuke = rad_cvt_string(data, len)) == NULL) {
348 log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad));
349 auth_Failure(r->cx.auth);
350 rad_close(r->cx.rad);
354 log_Printf(LogPHASE, " Route: %s\n", nuke);
355 bundle = r->cx.auth->physical->dl->bundle;
356 ip.s_addr = INADDR_ANY;
357 ncprange_setip4host(&dest, ip);
358 argc = command_Interpret(nuke, strlen(nuke), argv);
360 log_Printf(LogWARN, "radius: %s: Syntax error\n",
361 argc == 1 ? argv[0] : "\"\"");
363 log_Printf(LogWARN, "radius: %s: Invalid route\n",
364 argc == 1 ? argv[0] : "\"\"");
365 else if ((strcasecmp(argv[0], "default") != 0 &&
366 !ncprange_aton(&dest, &bundle->ncp, argv[0])) ||
367 !ncpaddr_aton(&gw, &bundle->ncp, argv[1]))
368 log_Printf(LogWARN, "radius: %s %s: Invalid route\n",
371 ncprange_getwidth(&dest, &width);
372 if (width == 32 && strchr(argv[0], '/') == NULL) {
373 /* No mask specified - use the natural mask */
374 ncprange_getip4addr(&dest, &ip);
375 ncprange_setip4mask(&dest, addr2mask(ip));
379 if (!strncasecmp(argv[0], "HISADDR", 7))
380 addrs = ROUTE_DSTHISADDR;
381 else if (!strncasecmp(argv[0], "MYADDR", 6))
382 addrs = ROUTE_DSTMYADDR;
384 if (ncpaddr_getip4addr(&gw, &ipaddr) && ipaddr == INADDR_ANY) {
385 addrs |= ROUTE_GWHISADDR;
386 ncpaddr_setip4(&gw, bundle->ncp.ipcp.peer_ip);
387 } else if (strcasecmp(argv[1], "HISADDR") == 0)
388 addrs |= ROUTE_GWHISADDR;
390 route_Add(&r->routes, addrs, &dest, &gw);
395 case RAD_REPLY_MESSAGE:
397 if ((r->repstr = rad_cvt_string(data, len)) == NULL) {
398 log_Printf(LogERROR, "rad_cvt_string: %s\n", rad_strerror(r->cx.rad));
399 auth_Failure(r->cx.auth);
400 rad_close(r->cx.rad);
403 log_Printf(LogPHASE, " Reply-Message \"%s\"\n", r->repstr);
406 case RAD_VENDOR_SPECIFIC:
407 if ((res = rad_get_vendor_attr(&vendor, &data, &len)) <= 0) {
408 log_Printf(LogERROR, "rad_get_vendor_attr: %s (failing!)\n",
409 rad_strerror(r->cx.rad));
410 auth_Failure(r->cx.auth);
411 rad_close(r->cx.rad);
416 case RAD_VENDOR_MICROSOFT:
419 case RAD_MICROSOFT_MS_CHAP_ERROR:
424 if (len < 3 || ((const char *)data)[1] != '=') {
426 * Only point at the String field if we don't think the
427 * peer has misformatted the response.
429 ((const char *)data)++;
432 log_Printf(LogWARN, "Warning: The MS-CHAP-Error "
433 "attribute is mis-formatted. Compensating\n");
434 if ((r->errstr = rad_cvt_string((const char *)data,
436 log_Printf(LogERROR, "rad_cvt_string: %s\n",
437 rad_strerror(r->cx.rad));
438 auth_Failure(r->cx.auth);
439 rad_close(r->cx.rad);
442 log_Printf(LogPHASE, " MS-CHAP-Error \"%s\"\n", r->errstr);
446 case RAD_MICROSOFT_MS_CHAP2_SUCCESS:
451 if (len < 3 || ((const char *)data)[1] != '=') {
453 * Only point at the String field if we don't think the
454 * peer has misformatted the response.
456 ((const char *)data)++;
459 log_Printf(LogWARN, "Warning: The MS-CHAP2-Success "
460 "attribute is mis-formatted. Compensating\n");
461 if ((r->msrepstr = rad_cvt_string((const char *)data,
463 log_Printf(LogERROR, "rad_cvt_string: %s\n",
464 rad_strerror(r->cx.rad));
465 auth_Failure(r->cx.auth);
466 rad_close(r->cx.rad);
469 log_Printf(LogPHASE, " MS-CHAP2-Success \"%s\"\n",
474 case RAD_MICROSOFT_MS_MPPE_ENCRYPTION_POLICY:
475 r->mppe.policy = rad_cvt_int(data);
476 log_Printf(LogPHASE, " MS-MPPE-Encryption-Policy %s\n",
477 radius_policyname(r->mppe.policy));
480 case RAD_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES:
481 r->mppe.types = rad_cvt_int(data);
482 log_Printf(LogPHASE, " MS-MPPE-Encryption-Types %s\n",
483 radius_typesname(r->mppe.types));
486 case RAD_MICROSOFT_MS_MPPE_RECV_KEY:
487 free(r->mppe.recvkey);
488 demangle(r, data, len, &r->mppe.recvkey, &r->mppe.recvkeylen);
489 log_Printf(LogPHASE, " MS-MPPE-Recv-Key ********\n");
492 case RAD_MICROSOFT_MS_MPPE_SEND_KEY:
493 demangle(r, data, len, &r->mppe.sendkey, &r->mppe.sendkeylen);
494 log_Printf(LogPHASE, " MS-MPPE-Send-Key ********\n");
499 log_Printf(LogDEBUG, "Dropping MICROSOFT vendor specific "
500 "RADIUS attribute %d\n", res);
506 log_Printf(LogDEBUG, "Dropping vendor %lu RADIUS attribute %d\n",
507 (unsigned long)vendor, res);
513 log_Printf(LogDEBUG, "Dropping RADIUS attribute %d\n", res);
519 log_Printf(LogERROR, "rad_get_attr: %s (failing!)\n",
520 rad_strerror(r->cx.rad));
521 auth_Failure(r->cx.auth);
522 } else if (got == RAD_ACCESS_REJECT)
523 auth_Failure(r->cx.auth);
526 auth_Success(r->cx.auth);
528 rad_close(r->cx.rad);
532 * We've either timed out or select()ed on the read descriptor
535 radius_Continue(struct radius *r, int sel)
540 timer_Stop(&r->cx.timer);
541 if ((got = rad_continue_send_request(r->cx.rad, sel, &r->cx.fd, &tv)) == 0) {
542 log_Printf(LogPHASE, "Radius: Request re-sent\n");
543 r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS;
544 timer_Start(&r->cx.timer);
548 radius_Process(r, got);
552 * Time to call rad_continue_send_request() - timed out.
555 radius_Timeout(void *v)
557 radius_Continue((struct radius *)v, 0);
561 * Time to call rad_continue_send_request() - something to read.
564 radius_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
566 radius_Continue(descriptor2radius(d), 1);
570 * Behave as a struct fdescriptor (descriptor.h)
573 radius_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
575 struct radius *rad = descriptor2radius(d);
577 if (r && rad->cx.fd != -1) {
578 FD_SET(rad->cx.fd, r);
579 if (*n < rad->cx.fd + 1)
581 log_Printf(LogTIMER, "Radius: fdset(r) %d\n", rad->cx.fd);
589 * Behave as a struct fdescriptor (descriptor.h)
592 radius_IsSet(struct fdescriptor *d, const fd_set *fdset)
594 struct radius *r = descriptor2radius(d);
596 return r && r->cx.fd != -1 && FD_ISSET(r->cx.fd, fdset);
600 * Behave as a struct fdescriptor (descriptor.h)
603 radius_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
605 /* We never want to write here ! */
606 log_Printf(LogALERT, "radius_Write: Internal error: Bad call !\n");
611 * Initialise ourselves
614 radius_Init(struct radius *r)
616 r->desc.type = RADIUS_DESCRIPTOR;
617 r->desc.UpdateSet = radius_UpdateSet;
618 r->desc.IsSet = radius_IsSet;
619 r->desc.Read = radius_Read;
620 r->desc.Write = radius_Write;
623 memset(&r->cx.timer, '\0', sizeof r->cx.timer);
627 r->ip.s_addr = INADDR_ANY;
628 r->mask.s_addr = INADDR_NONE;
636 r->mppe.recvkey = NULL;
637 r->mppe.recvkeylen = 0;
638 r->mppe.sendkey = NULL;
639 r->mppe.sendkeylen = 0;
640 *r->cfg.file = '\0';;
641 log_Printf(LogDEBUG, "Radius: radius_Init\n");
645 * Forget everything and go back to initialised state.
648 radius_Destroy(struct radius *r)
651 log_Printf(LogDEBUG, "Radius: radius_Destroy\n");
652 timer_Stop(&r->cx.timer);
653 route_DeleteAll(&r->routes);
662 free(r->mppe.recvkey);
663 r->mppe.recvkey = NULL;
664 r->mppe.recvkeylen = 0;
665 free(r->mppe.sendkey);
666 r->mppe.sendkey = NULL;
667 r->mppe.sendkeylen = 0;
668 if (r->cx.fd != -1) {
670 rad_close(r->cx.rad);
675 radius_put_physical_details(struct rad_handle *rad, struct physical *p)
681 switch (p->handler->type) {
683 type = RAD_ISDN_SYNC;
703 if (rad_put_int(rad, RAD_NAS_PORT_TYPE, type) != 0) {
704 log_Printf(LogERROR, "rad_put: rad_put_int: %s\n", rad_strerror(rad));
709 if ((slot = physical_Slot(p)) >= 0)
710 if (rad_put_int(rad, RAD_NAS_PORT, slot) != 0) {
711 log_Printf(LogERROR, "rad_put: rad_put_int: %s\n", rad_strerror(rad));
720 * Start an authentication request to the RADIUS server.
723 radius_Authenticate(struct radius *r, struct authinfo *authp, const char *name,
724 const char *key, int klen, const char *nchallenge,
729 char hostname[MAXHOSTNAMELEN];
732 struct in_addr hostaddr;
735 struct mschap_response msresp;
736 struct mschap2_response msresp2;
737 const struct MSCHAPv2_resp *keyv2;
745 * We assume that our name/key/challenge is the same as last time,
746 * and just continue to wait for the RADIUS server(s).
752 if ((r->cx.rad = rad_auth_open()) == NULL) {
753 log_Printf(LogERROR, "rad_auth_open: %s\n", strerror(errno));
757 if (rad_config(r->cx.rad, r->cfg.file) != 0) {
758 log_Printf(LogERROR, "rad_config: %s\n", rad_strerror(r->cx.rad));
759 rad_close(r->cx.rad);
763 if (rad_create_request(r->cx.rad, RAD_ACCESS_REQUEST) != 0) {
764 log_Printf(LogERROR, "rad_create_request: %s\n", rad_strerror(r->cx.rad));
765 rad_close(r->cx.rad);
769 if (rad_put_string(r->cx.rad, RAD_USER_NAME, name) != 0 ||
770 rad_put_int(r->cx.rad, RAD_SERVICE_TYPE, RAD_FRAMED) != 0 ||
771 rad_put_int(r->cx.rad, RAD_FRAMED_PROTOCOL, RAD_PPP) != 0) {
772 log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad));
773 rad_close(r->cx.rad);
777 switch (authp->physical->link.lcp.want_auth) {
779 /* We're talking PAP */
780 if (rad_put_attr(r->cx.rad, RAD_USER_PASSWORD, key, klen) != 0) {
781 log_Printf(LogERROR, "PAP: rad_put_string: %s\n",
782 rad_strerror(r->cx.rad));
783 rad_close(r->cx.rad);
789 switch (authp->physical->link.lcp.want_authtype) {
791 if (rad_put_attr(r->cx.rad, RAD_CHAP_PASSWORD, key, klen) != 0 ||
792 rad_put_attr(r->cx.rad, RAD_CHAP_CHALLENGE, nchallenge, nclen) != 0) {
793 log_Printf(LogERROR, "CHAP: rad_put_string: %s\n",
794 rad_strerror(r->cx.rad));
795 rad_close(r->cx.rad);
803 log_Printf(LogERROR, "CHAP80: Unrecognised key length %d\n", klen);
804 rad_close(r->cx.rad);
808 rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT,
809 RAD_MICROSOFT_MS_CHAP_CHALLENGE, nchallenge, nclen);
812 memcpy(msresp.lm_response, key + 1, 24);
813 memcpy(msresp.nt_response, key + 25, 24);
814 rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT,
815 RAD_MICROSOFT_MS_CHAP_RESPONSE, &msresp,
820 if (klen != sizeof(*keyv2) + 1) {
821 log_Printf(LogERROR, "CHAP81: Unrecognised key length %d\n", klen);
822 rad_close(r->cx.rad);
826 keyv2 = (const struct MSCHAPv2_resp *)(key + 1);
827 rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT,
828 RAD_MICROSOFT_MS_CHAP_CHALLENGE, nchallenge, nclen);
829 msresp2.ident = *key;
830 msresp2.flags = keyv2->Flags;
831 memcpy(msresp2.response, keyv2->NTResponse, sizeof msresp2.response);
832 memset(msresp2.reserved, '\0', sizeof msresp2.reserved);
833 memcpy(msresp2.pchallenge, keyv2->PeerChallenge,
834 sizeof msresp2.pchallenge);
835 rad_put_vendor_attr(r->cx.rad, RAD_VENDOR_MICROSOFT,
836 RAD_MICROSOFT_MS_CHAP2_RESPONSE, &msresp2,
841 log_Printf(LogERROR, "CHAP: Unrecognised type 0x%02x\n",
842 authp->physical->link.lcp.want_authtype);
843 rad_close(r->cx.rad);
848 if (gethostname(hostname, sizeof hostname) != 0)
849 log_Printf(LogERROR, "rad_put: gethostname(): %s\n", strerror(errno));
852 if ((hp = gethostbyname(hostname)) != NULL) {
853 hostaddr.s_addr = *(u_long *)hp->h_addr;
854 if (rad_put_addr(r->cx.rad, RAD_NAS_IP_ADDRESS, hostaddr) != 0) {
855 log_Printf(LogERROR, "rad_put: rad_put_string: %s\n",
856 rad_strerror(r->cx.rad));
857 rad_close(r->cx.rad);
862 if (rad_put_string(r->cx.rad, RAD_NAS_IDENTIFIER, hostname) != 0) {
863 log_Printf(LogERROR, "rad_put: rad_put_string: %s\n",
864 rad_strerror(r->cx.rad));
865 rad_close(r->cx.rad);
870 radius_put_physical_details(r->cx.rad, authp->physical);
873 if ((got = rad_init_send_request(r->cx.rad, &r->cx.fd, &tv)))
874 radius_Process(r, got);
876 log_Printf(LogPHASE, "Radius: Request sent\n");
877 log_Printf(LogDEBUG, "Using radius_Timeout [%p]\n", radius_Timeout);
878 r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS;
879 r->cx.timer.func = radius_Timeout;
880 r->cx.timer.name = "radius auth";
882 timer_Start(&r->cx.timer);
889 * Send an accounting request to the RADIUS server
892 radius_Account(struct radius *r, struct radacct *ac, struct datalink *dl,
893 int acct_type, struct in_addr *peer_ip, struct in_addr *netmask,
894 struct pppThroughput *stats)
898 char hostname[MAXHOSTNAMELEN];
901 struct in_addr hostaddr;
909 * We assume that our name/key/challenge is the same as last time,
910 * and just continue to wait for the RADIUS server(s).
914 timer_Stop(&r->cx.timer);
916 if ((r->cx.rad = rad_acct_open()) == NULL) {
917 log_Printf(LogERROR, "rad_auth_open: %s\n", strerror(errno));
921 if (rad_config(r->cx.rad, r->cfg.file) != 0) {
922 log_Printf(LogERROR, "rad_config: %s\n", rad_strerror(r->cx.rad));
923 rad_close(r->cx.rad);
927 if (rad_create_request(r->cx.rad, RAD_ACCOUNTING_REQUEST) != 0) {
928 log_Printf(LogERROR, "rad_create_request: %s\n", rad_strerror(r->cx.rad));
929 rad_close(r->cx.rad);
933 /* Grab some accounting data and initialize structure */
934 if (acct_type == RAD_START) {
936 /* Fetch username from datalink */
937 strncpy(ac->user_name, dl->peer.authname, sizeof ac->user_name);
938 ac->user_name[AUTHLEN-1] = '\0';
940 ac->authentic = 2; /* Assume RADIUS verified auth data */
942 /* Generate a session ID */
943 snprintf(ac->session_id, sizeof ac->session_id, "%s%ld-%s%lu",
944 dl->bundle->cfg.auth.name, (long)getpid(),
945 dl->peer.authname, (unsigned long)stats->uptime);
947 /* And grab our MP socket name */
948 snprintf(ac->multi_session_id, sizeof ac->multi_session_id, "%s",
949 dl->bundle->ncp.mp.active ?
950 dl->bundle->ncp.mp.server.socket.sun_path : "");
952 /* Fetch IP, netmask from IPCP */
953 memcpy(&ac->ip, peer_ip, sizeof(ac->ip));
954 memcpy(&ac->mask, netmask, sizeof(ac->mask));
957 if (rad_put_string(r->cx.rad, RAD_USER_NAME, ac->user_name) != 0 ||
958 rad_put_int(r->cx.rad, RAD_SERVICE_TYPE, RAD_FRAMED) != 0 ||
959 rad_put_int(r->cx.rad, RAD_FRAMED_PROTOCOL, RAD_PPP) != 0 ||
960 rad_put_addr(r->cx.rad, RAD_FRAMED_IP_ADDRESS, ac->ip) != 0 ||
961 rad_put_addr(r->cx.rad, RAD_FRAMED_IP_NETMASK, ac->mask) != 0) {
962 log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad));
963 rad_close(r->cx.rad);
967 if (gethostname(hostname, sizeof hostname) != 0)
968 log_Printf(LogERROR, "rad_put: gethostname(): %s\n", strerror(errno));
971 if ((hp = gethostbyname(hostname)) != NULL) {
972 hostaddr.s_addr = *(u_long *)hp->h_addr;
973 if (rad_put_addr(r->cx.rad, RAD_NAS_IP_ADDRESS, hostaddr) != 0) {
974 log_Printf(LogERROR, "rad_put: rad_put_string: %s\n",
975 rad_strerror(r->cx.rad));
976 rad_close(r->cx.rad);
981 if (rad_put_string(r->cx.rad, RAD_NAS_IDENTIFIER, hostname) != 0) {
982 log_Printf(LogERROR, "rad_put: rad_put_string: %s\n",
983 rad_strerror(r->cx.rad));
984 rad_close(r->cx.rad);
989 radius_put_physical_details(r->cx.rad, dl->physical);
991 if (rad_put_int(r->cx.rad, RAD_ACCT_STATUS_TYPE, acct_type) != 0 ||
992 rad_put_string(r->cx.rad, RAD_ACCT_SESSION_ID, ac->session_id) != 0 ||
993 rad_put_string(r->cx.rad, RAD_ACCT_MULTI_SESSION_ID,
994 ac->multi_session_id) != 0 ||
995 rad_put_int(r->cx.rad, RAD_ACCT_DELAY_TIME, 0) != 0) {
996 /* XXX ACCT_DELAY_TIME should be increased each time a packet is waiting */
997 log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad));
998 rad_close(r->cx.rad);
1002 if (acct_type == RAD_STOP)
1003 /* Show some statistics */
1004 if (rad_put_int(r->cx.rad, RAD_ACCT_INPUT_OCTETS, stats->OctetsIn) != 0 ||
1005 rad_put_int(r->cx.rad, RAD_ACCT_INPUT_PACKETS, stats->PacketsIn) != 0 ||
1006 rad_put_int(r->cx.rad, RAD_ACCT_OUTPUT_OCTETS, stats->OctetsOut) != 0 ||
1007 rad_put_int(r->cx.rad, RAD_ACCT_OUTPUT_PACKETS, stats->PacketsOut)
1009 rad_put_int(r->cx.rad, RAD_ACCT_SESSION_TIME, throughput_uptime(stats))
1011 log_Printf(LogERROR, "rad_put: %s\n", rad_strerror(r->cx.rad));
1012 rad_close(r->cx.rad);
1016 r->cx.auth = NULL; /* Not valid for accounting requests */
1017 if ((got = rad_init_send_request(r->cx.rad, &r->cx.fd, &tv)))
1018 radius_Process(r, got);
1020 log_Printf(LogDEBUG, "Using radius_Timeout [%p]\n", radius_Timeout);
1021 r->cx.timer.load = tv.tv_usec / TICKUNIT + tv.tv_sec * SECTICKS;
1022 r->cx.timer.func = radius_Timeout;
1023 r->cx.timer.name = "radius acct";
1024 r->cx.timer.arg = r;
1025 timer_Start(&r->cx.timer);
1030 * How do things look at the moment ?
1033 radius_Show(struct radius *r, struct prompt *p)
1035 prompt_Printf(p, " Radius config: %s",
1036 *r->cfg.file ? r->cfg.file : "none");
1038 prompt_Printf(p, "\n IP: %s\n", inet_ntoa(r->ip));
1039 prompt_Printf(p, " Netmask: %s\n", inet_ntoa(r->mask));
1040 prompt_Printf(p, " MTU: %lu\n", r->mtu);
1041 prompt_Printf(p, " VJ: %sabled\n", r->vj ? "en" : "dis");
1042 prompt_Printf(p, " Message: %s\n", r->repstr ? r->repstr : "");
1043 prompt_Printf(p, " MPPE Enc Policy: %s\n",
1044 radius_policyname(r->mppe.policy));
1045 prompt_Printf(p, " MPPE Enc Types: %s\n",
1046 radius_typesname(r->mppe.types));
1047 prompt_Printf(p, " MPPE Recv Key: %seceived\n",
1048 r->mppe.recvkey ? "R" : "Not r");
1049 prompt_Printf(p, " MPPE Send Key: %seceived\n",
1050 r->mppe.sendkey ? "R" : "Not r");
1051 prompt_Printf(p, " MS-CHAP2-Response: %s\n",
1052 r->msrepstr ? r->msrepstr : "");
1053 prompt_Printf(p, " Error Message: %s\n", r->errstr ? r->errstr : "");
1055 route_ShowSticky(p, r->routes, " Routes", 16);
1057 prompt_Printf(p, " (not authenticated)\n");