3 char const *cvs_id_radius
= "$Id: radius.c,v 1.5 2004/06/28 02:43:13 fred_nerk Exp $";
8 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <netinet/in.h>
16 #include "constants.h"
21 extern radiust
*radius
;
22 extern sessiont
*session
;
23 extern tunnelt
*tunnel
;
25 extern struct configt
*config
;
28 const char *radius_state(int state
)
30 static char *tmp
= NULL
;
32 for (i
= 0; radius_states
[i
]; i
++)
33 if (i
== state
) return radius_states
[i
];
35 if (tmp
== NULL
) tmp
= (char *)calloc(64, 1);
36 sprintf(tmp
, "%d", state
);
40 // Set up socket for radius requests
44 log(3, 0, 0, 0, "Creating %d sockets for RADIUS queries\n", config
->num_radfds
);
45 radfds
= calloc(sizeof(int), config
->num_radfds
);
46 for (i
= 0; i
< config
->num_radfds
; i
++)
49 if (!radfds
[i
]) radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, UDP
);
50 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
51 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
55 void radiusclear(u16 r
, sessionidt s
)
57 if (s
) session
[s
].radius
= 0;
58 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
62 static u16
new_radius()
65 static u32 next_radius_id
= 0;
67 for (count
= MAXRADIUS
; count
> 0 ; --count
)
69 ++next_radius_id
; // Find the next ID to check.
70 if (next_radius_id
>= MAXRADIUS
)
73 if (radius
[next_radius_id
].state
== RADIUSNULL
)
75 return next_radius_id
;
79 log(0, 0, 0, 0, "Can't find a free radius session! This is very bad!\n");
83 u16
radiusnew(sessionidt s
)
86 if (!(r
= new_radius()))
88 log(1, 0, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
89 STAT(radius_overflow
);
92 memset(&radius
[r
], 0, sizeof(radius
[r
]));
93 session
[s
].radius
= r
;
94 radius
[r
].session
= s
;
95 radius
[r
].state
= RADIUSWAIT
;
96 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
98 log(3,0,s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
102 // Send a RADIUS request
103 void radiussend(u16 r
, u8 state
)
105 struct sockaddr_in addr
;
106 u8 b
[4096]; // RADIUS packet
112 CSTAT(call_radiussend
);
114 s
= radius
[r
].session
;
115 if (!config
->numradiusservers
)
117 log(0, 0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
120 if (!*config
->radiussecret
)
122 log(0, 0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
126 if (state
!= RADIUSAUTH
&& !config
->radius_accounting
)
128 // Radius accounting is turned off
133 if (radius
[r
].state
!= state
)
135 radius
[r
].state
= state
;
136 radius
[r
].retry
= backoff(radius
[r
].try++);
137 log(4, 0, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
138 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
139 radius_state(radius
[r
].state
), radius
[r
].try);
140 if (radius
[r
].try > config
->numradiusservers
* 2)
144 if (state
== RADIUSAUTH
)
145 sessionshutdown(s
, "RADIUS timeout");
148 log(1, 0, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
149 radius_states
[state
]);
152 STAT(radius_timeout
);
156 STAT(radius_retries
);
157 radius
[r
].state
= RADIUSWAIT
;
158 radius
[r
].retry
= 100;
162 // contruct RADIUS access request
166 b
[0] = 1; // access request
170 b
[0] = 4; // accounting request
173 log(0, 0, 0, 0, "Unknown radius state %d\n", state
);
175 b
[1] = r
>> RADIUS_SHIFT
; // identifier
176 memcpy(b
+ 4, radius
[r
].auth
, 16);
181 p
[1] = strlen(session
[s
].user
) + 2;
182 strcpy(p
+ 2, session
[s
].user
);
185 if (state
== RADIUSAUTH
)
189 *p
= 3; // CHAP password
191 p
[2] = radius
[r
].id
; // ID
192 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
194 *p
= 60; // CHAP Challenge
196 memcpy(p
+ 2, radius
[r
].auth
, 16);
201 strcpy(pass
, radius
[r
].pass
);
204 pass
[pl
++] = 0; // pad
213 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
215 MD5Update(&ctx
, pass
+ p
- 16, 16);
217 MD5Update(&ctx
, radius
[r
].auth
, 16);
218 MD5Final(hash
, &ctx
);
221 pass
[p
] ^= hash
[p
& 15];
230 memcpy(p
+ 2, pass
, pl
);
234 else if (state
== RADIUSSTART
|| state
== RADIUSSTOP
)
236 *p
= 40; // accounting type
238 *(u32
*) (p
+ 2) = htonl((state
== RADIUSSTART
) ? 1 : 2);
242 *p
= 44; // session ID
244 sprintf(p
+ 2, "%08X%08X", session
[s
].id
, session
[s
].opened
);
246 if (state
== RADIUSSTOP
)
248 *p
= 42; // input octets
250 *(u32
*) (p
+ 2) = htonl(session
[s
].cin
);
252 *p
= 43; // output octets
254 *(u32
*) (p
+ 2) = htonl(session
[s
].cout
);
256 *p
= 46; // session time
258 *(u32
*) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
260 *p
= 47; // input packets
262 *(u32
*) (p
+ 2) = htonl(session
[s
].pin
);
264 *p
= 48; // output spackets
266 *(u32
*) (p
+ 2) = htonl(session
[s
].pout
);
273 *(u32
*) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
282 *(u32
*) (p
+ 2) = htonl(s
);
285 if (s
&& session
[s
].ip
)
287 *p
= 8; // Framed-IP-Address
289 *(u32
*) (p
+ 2) = htonl(session
[s
].ip
);
292 if (*session
[s
].called
)
295 p
[1] = strlen(session
[s
].called
) + 2;
296 strcpy(p
+ 2, session
[s
].called
);
299 if (*radius
[r
].calling
)
302 p
[1] = strlen(radius
[r
].calling
) + 2;
303 strcpy(p
+ 2, radius
[r
].calling
);
306 else if (*session
[s
].calling
)
309 p
[1] = strlen(session
[s
].calling
) + 2;
310 strcpy(p
+ 2, session
[s
].calling
);
316 *(u32
*)(p
+ 2) = config
->bind_address
;
320 *(u16
*) (b
+ 2) = htons(p
- b
);
321 if (state
!= RADIUSAUTH
)
323 // Build auth for accounting packet
328 MD5Update(&ctx
, b
, 4);
329 MD5Update(&ctx
, z
, 16);
330 MD5Update(&ctx
, b
+ 20, (p
- b
) - 20);
331 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
332 MD5Final(hash
, &ctx
);
333 memcpy(b
+ 4, hash
, 16);
334 memcpy(radius
[r
].auth
, hash
, 16);
336 memset(&addr
, 0, sizeof(addr
));
337 addr
.sin_family
= AF_INET
;
338 *(u32
*) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
339 addr
.sin_port
= htons((state
== RADIUSAUTH
) ? RADPORT
: RADAPORT
);
341 log_hex(5, "RADIUS Send", b
, (p
- b
));
342 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
345 // process RADIUS response
346 void processrad(u8
*buf
, int len
, char socket_index
)
356 int r_code
, r_id
; // Radius code.
358 r_code
= buf
[0]; // First byte in radius packet.
359 r_id
= buf
[1]; // radius reply indentifier.
362 CSTAT(call_processrad
);
364 log_hex(5, "RADIUS Response", buf
, len
);
365 if (len
< 20 || len
< ntohs(*(u16
*) (buf
+ 2)))
367 log(1, 0, 0, 0, "Duff RADIUS response length %d\n", len
);
370 len
= ntohs(*(u16
*) (buf
+ 2));
371 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
372 s
= radius
[r
].session
;
373 log(3, 0, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (code %d, id %d)\n",
374 radius_states
[radius
[r
].state
], r
, s
, r_code
, r_id
);
375 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
377 log(1, 0, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
380 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSSTART
&& radius
[r
].state
!= RADIUSSTOP
)
382 log(1, 0, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
385 t
= session
[s
].tunnel
;
387 MD5Update(&ctx
, buf
, 4);
388 MD5Update(&ctx
, radius
[r
].auth
, 16);
389 MD5Update(&ctx
, buf
+ 20, len
- 20);
390 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
391 MD5Final(hash
, &ctx
);
393 if (memcmp(hash
, buf
+ 4, 16))
395 log(0, 0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
396 // radius[r].state = RADIUSWAIT;
398 return; // Do nothing. On timeout, it will try the next radius server.
400 if ((radius
[r
].state
== RADIUSAUTH
&& *buf
!= 2 && *buf
!= 3) ||
401 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
) && *buf
!= 5))
403 log(1, 0, s
, session
[s
].tunnel
, " Unexpected RADIUS response %d\n", *buf
);
405 return; // We got something we didn't expect. Let the timeouts take
406 // care off finishing the radius session if that's really correct.
407 // old code. I think incorrect. --mo
408 // radius[r].state = RADIUSWAIT;
409 // break; // Finish the radius sesssion.
411 if (radius
[r
].state
== RADIUSAUTH
)
413 log(4, 0, s
, session
[s
].tunnel
, " Original response is \"%s\"\n", (*buf
== 2) ? "accept" : "reject");
414 // process auth response
418 u8
*p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
423 struct param_post_auth packet
= { &tunnel
[t
], &session
[s
], session
[s
].user
, (*buf
== 2), PPPCHAP
};
424 run_plugins(PLUGIN_POST_AUTH
, &packet
);
425 *buf
= packet
.auth_allowed
? 2 : 3;
428 log(3, 0, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
429 (*buf
== 2) ? "allowed" : "denied");
430 *p
= (*buf
== 2) ? 3 : 4; // ack/nak
432 *(u16
*) (p
+ 2) = ntohs(4); // no message
433 tunnelsend(b
, (p
- b
) + 4, t
); // send it
438 u8
*p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
443 struct param_post_auth packet
= { &tunnel
[t
], &session
[s
], session
[s
].user
, (*buf
== 2), PPPPAP
};
444 run_plugins(PLUGIN_POST_AUTH
, &packet
);
445 *buf
= packet
.auth_allowed
? 2 : 3;
448 log(3, 0, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
449 (*buf
== 2) ? "allowed" : "denied");
453 *(u16
*) (p
+ 2) = ntohs(5);
454 p
[4] = 0; // no message
455 tunnelsend(b
, (p
- b
) + 5, t
); // send it
461 // Extract IP, routes, etc
464 for (p
= buf
+ 20; p
< e
&& p
[1]; p
+= p
[1])
468 // Statically assigned address
469 log(3, 0, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n", inet_toa(*(u32
*) (p
+ 2)));
470 session
[s
].ip
= ntohl(*(u32
*) (p
+ 2));
471 session
[s
].ip_pool_index
= -1;
476 log(3, 0, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n", inet_toa(*(u32
*) (p
+ 2)));
477 session
[s
].dns1
= ntohl(*(u32
*) (p
+ 2));
482 log(3, 0, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n", inet_toa(*(u32
*) (p
+ 2)));
483 session
[s
].dns2
= ntohl(*(u32
*) (p
+ 2));
488 ipt ip
= 0, mask
= 0;
493 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
501 u
= u
* 10 + *n
- '0';
508 while (n
< e
&& isdigit(*n
))
509 bits
= bits
* 10 + *n
++ - '0';
510 mask
= (( -1) << (32 - bits
));
512 else if ((ip
>> 24) < 128)
514 else if ((ip
>> 24) < 192)
518 if (routes
== MAXROUTE
)
520 log(1, 0, s
, session
[s
].tunnel
, " Too many routes\n");
525 ips
= strdup(inet_toa(htonl(ip
)));
526 masks
= strdup(inet_toa(htonl(mask
)));
527 log(3, 0, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n", ips
, masks
);
530 session
[s
].route
[routes
].ip
= ip
;
531 session
[s
].route
[routes
].mask
= mask
;
537 // Vendor-Specific Attribute
538 int vendor
= ntohl(*(int *)(p
+ 2));
539 char attrib
= *(p
+ 6);
540 char attrib_length
= *(p
+ 7) - 2;
541 log(3, 0, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor
, attrib
, attrib_length
);
542 if (attrib_length
== 0) continue;
544 log(3, 0, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
547 char *avpair
, *value
, *key
, *newp
;
548 avpair
= key
= calloc(attrib_length
+ 1, 1);
549 memcpy(avpair
, p
+ 8, attrib_length
);
550 log(3, 0, s
, session
[s
].tunnel
, " Cisco-Avpair value: %s\n", avpair
);
552 value
= strchr(key
, '=');
556 // Trim quotes off reply string
557 if (*value
== '\'' || *value
== '\"')
561 x
= value
+ strlen(value
) - 1;
562 if (*x
== '\'' || *x
== '\"')
567 newp
= strchr(value
, ',');
568 if (newp
) *newp
++ = 0;
570 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], key
, value
};
571 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
582 log(2, 0, s
, session
[s
].tunnel
, " Authentication denied for %s\n", session
[s
].user
);
583 //FIXME: We should tear down the session here!
587 if (!session
[s
].dns1
&& config
->default_dns1
)
589 session
[s
].dns1
= htonl(config
->default_dns1
);
590 log(3, 0, s
, t
, " Sending dns1 = %s\n", inet_toa(config
->default_dns1
));
592 if (!session
[s
].dns2
&& config
->default_dns2
)
594 session
[s
].dns2
= htonl(config
->default_dns2
);
595 log(3, 0, s
, t
, " Sending dns2 = %s\n", inet_toa(config
->default_dns2
));
598 // Valid Session, set it up
604 // An ack for a stop or start record.
605 log(3, 0, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_states
[radius
[r
].state
]);
610 // finished with RADIUS
614 // Send a retry for RADIUS/CHAP message
615 void radiusretry(u16 r
)
617 sessionidt s
= radius
[r
].session
;
620 CSTAT(call_radiusretry
);
623 t
= session
[s
].tunnel
;
624 radius
[r
].retry
= backoff(radius
[r
].try + 1);
625 switch (radius
[r
].state
)
627 case RADIUSCHAP
: // sending CHAP down PPP
631 sendipcp(t
, s
); // send IPCP
633 case RADIUSAUTH
: // sending auth to RADIUS server
634 radiussend(r
, RADIUSAUTH
);
636 case RADIUSSTART
: // sending start accounting to RADIUS server
637 radiussend(r
, RADIUSSTART
);
639 case RADIUSSTOP
: // sending stop accounting to RADIUS server
640 radiussend(r
, RADIUSSTOP
);
643 case RADIUSNULL
: // Not in use
644 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
645 // free up RADIUS task
647 log(3, 0, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
656 log(1, 0, 0, 0, "Cleaning radius session array\n");
658 for (i
= 1; i
< MAXRADIUS
; i
++)
660 if (radius
[i
].retry
== 0
661 || !session
[radius
[i
].session
].opened
662 || session
[radius
[i
].session
].die
663 || session
[radius
[i
].session
].tunnel
== 0)