63aeec1d90122224a4ef19e0a8f3f802342973ea
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.17 2004-11-28 02:53:11 bodea Exp $";
4
5 #include <time.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <malloc.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <arpa/inet.h>
13 #include <ctype.h>
14 #include <netinet/in.h>
15 #include "md5.h"
16 #include "constants.h"
17 #include "l2tpns.h"
18 #include "plugin.h"
19 #include "util.h"
20
21 extern radiust *radius;
22 extern sessiont *session;
23 extern tunnelt *tunnel;
24 extern u32 sessionid;
25 extern configt *config;
26 extern int *radfds;
27 extern ip_filtert *ip_filters;
28
29 static const char *radius_state(int state)
30 {
31 static char *tmp = NULL;
32 int i;
33 for (i = 0; radius_states[i]; i++)
34 if (i == state) return radius_states[i];
35
36 if (tmp == NULL) tmp = (char *)calloc(64, 1);
37 sprintf(tmp, "%d", state);
38 return tmp;
39 }
40
41 // Set up socket for radius requests
42 void initrad(void)
43 {
44 int i;
45 LOG(3, 0, 0, 0, "Creating %d sockets for RADIUS queries\n", config->num_radfds);
46 radfds = calloc(sizeof(int), config->num_radfds);
47 for (i = 0; i < config->num_radfds; i++)
48 {
49 int flags;
50 radfds[i] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
51 flags = fcntl(radfds[i], F_GETFL, 0);
52 fcntl(radfds[i], F_SETFL, flags | O_NONBLOCK);
53 }
54 }
55
56 void radiusclear(u16 r, sessionidt s)
57 {
58 if (s) session[s].radius = 0;
59 memset(&radius[r], 0, sizeof(radius[r])); // radius[r].state = RADIUSNULL;
60 }
61
62
63 static u16 get_free_radius()
64 {
65 int count;
66 static u32 next_radius_id = 0;
67
68 for (count = MAXRADIUS; count > 0 ; --count)
69 {
70 ++next_radius_id; // Find the next ID to check.
71 if (next_radius_id >= MAXRADIUS)
72 next_radius_id = 1;
73
74 if (radius[next_radius_id].state == RADIUSNULL)
75 {
76 return next_radius_id;
77 }
78 }
79
80 LOG(0, 0, 0, 0, "Can't find a free radius session! This is very bad!\n");
81 return 0;
82 }
83
84 u16 radiusnew(sessionidt s)
85 {
86 u16 r = session[s].radius;
87
88 /* re-use */
89 if (r)
90 {
91 LOG(3, 0, s, session[s].tunnel, "Re-used radius %d\n", r);
92 return r;
93 }
94
95 if (!(r = get_free_radius()))
96 {
97 LOG(1, 0, s, session[s].tunnel, "No free RADIUS sessions\n");
98 STAT(radius_overflow);
99 return 0;
100 };
101
102 memset(&radius[r], 0, sizeof(radius[r]));
103 session[s].radius = r;
104 radius[r].session = s;
105 radius[r].state = RADIUSWAIT;
106 radius[r].retry = TIME + 1200; // Wait at least 120 seconds to re-claim this.
107
108 LOG(3,0,s, session[s].tunnel, "Allocated radius %d\n", r);
109 return r;
110 }
111
112 // Send a RADIUS request
113 void radiussend(u16 r, u8 state)
114 {
115 struct sockaddr_in addr;
116 u8 b[4096]; // RADIUS packet
117 char pass[129];
118 int pl;
119 u8 *p;
120 sessionidt s;
121
122 CSTAT(call_radiussend);
123
124 s = radius[r].session;
125 if (!config->numradiusservers)
126 {
127 LOG(0, 0, s, session[s].tunnel, "No RADIUS servers\n");
128 return;
129 }
130 if (!*config->radiussecret)
131 {
132 LOG(0, 0, s, session[s].tunnel, "No RADIUS secret\n");
133 return;
134 }
135
136 if (state != RADIUSAUTH && !config->radius_accounting)
137 {
138 // Radius accounting is turned off
139 radiusclear(r, s);
140 return;
141 }
142
143 if (radius[r].state != state)
144 radius[r].try = 0;
145 radius[r].state = state;
146 radius[r].retry = backoff(radius[r].try++);
147 LOG(4, 0, s, session[s].tunnel, "Send RADIUS id %d sock %d state %s try %d\n",
148 r >> RADIUS_SHIFT, r & RADIUS_MASK,
149 radius_state(radius[r].state), radius[r].try);
150 if (radius[r].try > config->numradiusservers * 2)
151 {
152 if (s)
153 {
154 if (state == RADIUSAUTH)
155 sessionshutdown(s, "RADIUS timeout");
156 else
157 {
158 LOG(1, 0, s, session[s].tunnel, "RADIUS timeout, but in state %s so don't timeout session\n",
159 radius_states[state]);
160 radiusclear(r, s);
161 }
162 STAT(radius_timeout);
163 }
164 else
165 {
166 STAT(radius_retries);
167 radius[r].state = RADIUSWAIT;
168 radius[r].retry = 100;
169 }
170 return ;
171 }
172 // contruct RADIUS access request
173 switch (state)
174 {
175 case RADIUSAUTH:
176 b[0] = 1; // access request
177 break;
178 case RADIUSSTART:
179 case RADIUSSTOP:
180 b[0] = 4; // accounting request
181 break;
182 default:
183 LOG(0, 0, 0, 0, "Unknown radius state %d\n", state);
184 }
185 b[1] = r >> RADIUS_SHIFT; // identifier
186 memcpy(b + 4, radius[r].auth, 16);
187 p = b + 20;
188 if (s)
189 {
190 *p = 1; // user name
191 p[1] = strlen(session[s].user) + 2;
192 strcpy(p + 2, session[s].user);
193 p += p[1];
194 }
195 if (state == RADIUSAUTH)
196 {
197 if (radius[r].chap)
198 {
199 *p = 3; // CHAP password
200 p[1] = 19; // length
201 p[2] = radius[r].id; // ID
202 memcpy(p + 3, radius[r].pass, 16); // response from CHAP request
203 p += p[1];
204 *p = 60; // CHAP Challenge
205 p[1] = 18; // length
206 memcpy(p + 2, radius[r].auth, 16);
207 p += p[1];
208 }
209 else
210 {
211 strcpy(pass, radius[r].pass);
212 pl = strlen(pass);
213 while (pl & 15)
214 pass[pl++] = 0; // pad
215 if (pl)
216 { // encrypt
217 hasht hash;
218 int p = 0;
219 while (p < pl)
220 {
221 MD5_CTX ctx;
222 MD5Init(&ctx);
223 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
224 if (p)
225 MD5Update(&ctx, pass + p - 16, 16);
226 else
227 MD5Update(&ctx, radius[r].auth, 16);
228 MD5Final(hash, &ctx);
229 do
230 {
231 pass[p] ^= hash[p & 15];
232 p++;
233 }
234 while (p & 15);
235 }
236 }
237 *p = 2; // password
238 p[1] = pl + 2;
239 if (pl)
240 memcpy(p + 2, pass, pl);
241 p += p[1];
242 }
243 }
244 else if (state == RADIUSSTART || state == RADIUSSTOP)
245 { // accounting
246 *p = 40; // accounting type
247 p[1] = 6;
248 *(u32 *) (p + 2) = htonl((state == RADIUSSTART) ? 1 : 2);
249 p += p[1];
250 if (s)
251 {
252 *p = 44; // session ID
253 p[1] = 18;
254 sprintf(p + 2, "%08X%08X", session[s].id, session[s].opened);
255 p += p[1];
256 if (state == RADIUSSTOP)
257 { // stop
258 *p = 42; // input octets
259 p[1] = 6;
260 *(u32 *) (p + 2) = htonl(session[s].cin);
261 p += p[1];
262 *p = 43; // output octets
263 p[1] = 6;
264 *(u32 *) (p + 2) = htonl(session[s].cout);
265 p += p[1];
266 *p = 46; // session time
267 p[1] = 6;
268 *(u32 *) (p + 2) = htonl(time(NULL) - session[s].opened);
269 p += p[1];
270 *p = 47; // input packets
271 p[1] = 6;
272 *(u32 *) (p + 2) = htonl(session[s].pin);
273 p += p[1];
274 *p = 48; // output spackets
275 p[1] = 6;
276 *(u32 *) (p + 2) = htonl(session[s].pout);
277 p += p[1];
278 }
279 else
280 { // start
281 *p = 41; // delay
282 p[1] = 6;
283 *(u32 *) (p + 2) = htonl(time(NULL) - session[s].opened);
284 p += p[1];
285 }
286 }
287 }
288 if (s)
289 {
290 *p = 5; // NAS-Port
291 p[1] = 6;
292 *(u32 *) (p + 2) = htonl(s);
293 p += p[1];
294 }
295 if (s && session[s].ip)
296 {
297 *p = 8; // Framed-IP-Address
298 p[1] = 6;
299 *(u32 *) (p + 2) = htonl(session[s].ip);
300 p += p[1];
301 }
302 if (*session[s].called)
303 {
304 *p = 30; // called
305 p[1] = strlen(session[s].called) + 2;
306 strcpy(p + 2, session[s].called);
307 p += p[1];
308 }
309 if (*radius[r].calling)
310 {
311 *p = 31; // calling
312 p[1] = strlen(radius[r].calling) + 2;
313 strcpy(p + 2, radius[r].calling);
314 p += p[1];
315 }
316 else if (*session[s].calling)
317 {
318 *p = 31; // calling
319 p[1] = strlen(session[s].calling) + 2;
320 strcpy(p + 2, session[s].calling);
321 p += p[1];
322 }
323 // NAS-IP-Address
324 *p = 4;
325 p[1] = 6;
326 *(u32 *)(p + 2) = config->bind_address;
327 p += p[1];
328
329 // All AVpairs added
330 *(u16 *) (b + 2) = htons(p - b);
331 if (state != RADIUSAUTH)
332 {
333 // Build auth for accounting packet
334 char z[16] = {0};
335 char hash[16] = {0};
336 MD5_CTX ctx;
337 MD5Init(&ctx);
338 MD5Update(&ctx, b, 4);
339 MD5Update(&ctx, z, 16);
340 MD5Update(&ctx, b + 20, (p - b) - 20);
341 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
342 MD5Final(hash, &ctx);
343 memcpy(b + 4, hash, 16);
344 memcpy(radius[r].auth, hash, 16);
345 }
346 memset(&addr, 0, sizeof(addr));
347 addr.sin_family = AF_INET;
348 *(u32 *) & addr.sin_addr = config->radiusserver[(radius[r].try - 1) % config->numradiusservers];
349 {
350 // get radius port
351 u16 port = config->radiusport[(radius[r].try - 1) % config->numradiusservers];
352 // no need to define the accounting port for itself:
353 // the accounting port is as far as I know always one more
354 // than the auth port JK 20040713
355 addr.sin_port = htons((state == RADIUSAUTH) ? port : port+1);
356 }
357
358 LOG_HEX(5, "RADIUS Send", b, (p - b));
359 sendto(radfds[r & RADIUS_MASK], b, p - b, 0, (void *) &addr, sizeof(addr));
360 }
361
362 // process RADIUS response
363 void processrad(u8 *buf, int len, char socket_index)
364 {
365 u8 b[MAXCONTROL];
366 MD5_CTX ctx;
367 u16 r;
368 sessionidt s;
369 tunnelidt t = 0;
370 hasht hash;
371 u8 routes = 0;
372
373 int r_code, r_id ; // Radius code.
374
375 r_code = buf[0]; // First byte in radius packet.
376 r_id = buf[1]; // radius reply indentifier.
377
378
379 CSTAT(call_processrad);
380
381 LOG_HEX(5, "RADIUS Response", buf, len);
382 if (len < 20 || len < ntohs(*(u16 *) (buf + 2)))
383 {
384 LOG(1, 0, 0, 0, "Duff RADIUS response length %d\n", len);
385 return ;
386 }
387 len = ntohs(*(u16 *) (buf + 2));
388 r = socket_index | (r_id << RADIUS_SHIFT);
389 s = radius[r].session;
390 LOG(3, 0, s, session[s].tunnel, "Received %s, radius %d response for session %u (code %d, id %d)\n",
391 radius_states[radius[r].state], r, s, r_code, r_id);
392 if (!s && radius[r].state != RADIUSSTOP)
393 {
394 LOG(1, 0, s, session[s].tunnel, " Unexpected RADIUS response\n");
395 return;
396 }
397 if (radius[r].state != RADIUSAUTH && radius[r].state != RADIUSSTART && radius[r].state != RADIUSSTOP)
398 {
399 LOG(1, 0, s, session[s].tunnel, " Unexpected RADIUS response\n");
400 return;
401 }
402 t = session[s].tunnel;
403 MD5Init(&ctx);
404 MD5Update(&ctx, buf, 4);
405 MD5Update(&ctx, radius[r].auth, 16);
406 MD5Update(&ctx, buf + 20, len - 20);
407 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
408 MD5Final(hash, &ctx);
409 do {
410 if (memcmp(hash, buf + 4, 16))
411 {
412 LOG(0, 0, s, session[s].tunnel, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
413 return; // Do nothing. On timeout, it will try the next radius server.
414 }
415 if ((radius[r].state == RADIUSAUTH && *buf != 2 && *buf != 3) ||
416 ((radius[r].state == RADIUSSTART || radius[r].state == RADIUSSTOP) && *buf != 5))
417 {
418 LOG(1, 0, s, session[s].tunnel, " Unexpected RADIUS response %d\n", *buf);
419 return; // We got something we didn't expect. Let the timeouts take
420 // care off finishing the radius session if that's really correct.
421 }
422 if (radius[r].state == RADIUSAUTH)
423 {
424 LOG(4, 0, s, session[s].tunnel, " Original response is \"%s\"\n", (*buf == 2) ? "accept" : "reject");
425 // process auth response
426 if (radius[r].chap)
427 {
428 // CHAP
429 u8 *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
430 if (!p) return; // Abort!
431
432 {
433 struct param_post_auth packet = { &tunnel[t], &session[s], session[s].user, (*buf == 2), PPPCHAP };
434 run_plugins(PLUGIN_POST_AUTH, &packet);
435 *buf = packet.auth_allowed ? 2 : 3;
436 }
437
438 LOG(3, 0, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
439 (*buf == 2) ? "allowed" : "denied");
440 *p = (*buf == 2) ? 3 : 4; // ack/nak
441 p[1] = radius[r].id;
442 *(u16 *) (p + 2) = ntohs(4); // no message
443 tunnelsend(b, (p - b) + 4, t); // send it
444 }
445 else
446 {
447 // PAP
448 u8 *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPPAP);
449 if (!p) return; // Abort!
450
451 {
452 struct param_post_auth packet = { &tunnel[t], &session[s], session[s].user, (*buf == 2), PPPPAP };
453 run_plugins(PLUGIN_POST_AUTH, &packet);
454 *buf = packet.auth_allowed ? 2 : 3;
455 }
456
457 LOG(3, 0, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
458 (*buf == 2) ? "allowed" : "denied");
459 // ack/nak
460 *p = *buf;
461 p[1] = radius[r].id;
462 *(u16 *) (p + 2) = ntohs(5);
463 p[4] = 0; // no message
464 tunnelsend(b, (p - b) + 5, t); // send it
465 }
466
467 if (*buf == 2)
468 {
469 // Login successful
470 // Extract IP, routes, etc
471 u8 *p = buf + 20;
472 u8 *e = buf + len;
473 for (p = buf + 20; p < e && p[1]; p += p[1])
474 {
475 if (*p == 8)
476 {
477 // Framed-IP-Address
478 LOG(3, 0, s, session[s].tunnel, " Radius reply contains IP address %s\n", inet_toa(*(u32 *) (p + 2)));
479 session[s].ip = ntohl(*(u32 *) (p + 2));
480 session[s].ip_pool_index = -1;
481 }
482 else if (*p == 135)
483 {
484 // DNS address
485 LOG(3, 0, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n", inet_toa(*(u32 *) (p + 2)));
486 session[s].dns1 = ntohl(*(u32 *) (p + 2));
487 }
488 else if (*p == 136)
489 {
490 // DNS address
491 LOG(3, 0, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n", inet_toa(*(u32 *) (p + 2)));
492 session[s].dns2 = ntohl(*(u32 *) (p + 2));
493 }
494 else if (*p == 22)
495 {
496 // Framed-Route
497 ipt ip = 0, mask = 0;
498 u8 u = 0;
499 u8 bits = 0;
500 u8 *n = p + 2;
501 u8 *e = p + p[1];
502 while (n < e && (isdigit(*n) || *n == '.'))
503 {
504 if (*n == '.')
505 {
506 ip = (ip << 8) + u;
507 u = 0;
508 }
509 else
510 u = u * 10 + *n - '0';
511 n++;
512 }
513 ip = (ip << 8) + u;
514 if (*n == '/')
515 {
516 n++;
517 while (n < e && isdigit(*n))
518 bits = bits * 10 + *n++ - '0';
519 mask = (( -1) << (32 - bits));
520 }
521 else if ((ip >> 24) < 128)
522 mask = 0xFF0000;
523 else if ((ip >> 24) < 192)
524 mask = 0xFFFF0000;
525 else
526 mask = 0xFFFFFF00;
527 if (routes == MAXROUTE)
528 {
529 LOG(1, 0, s, session[s].tunnel, " Too many routes\n");
530 }
531 else if (ip)
532 {
533 char *ips, *masks;
534 ips = strdup(inet_toa(htonl(ip)));
535 masks = strdup(inet_toa(htonl(mask)));
536 LOG(3, 0, s, session[s].tunnel, " Radius reply contains route for %s/%s\n", ips, masks);
537 free(ips);
538 free(masks);
539 session[s].route[routes].ip = ip;
540 session[s].route[routes].mask = mask;
541 routes++;
542 }
543 }
544 else if (*p == 11)
545 {
546 // Filter-Id
547 char *filter = p + 2;
548 int l = p[1] - 2;
549 char *suffix;
550 u8 *f = 0;
551 int i;
552
553 LOG(3, 0, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
554 if ((suffix = memchr(filter, '.', l)))
555 {
556 int b = suffix - filter;
557 if (l - b == 3 && !memcmp("in", suffix+1, 2))
558 f = &session[s].filter_in;
559 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
560 f = &session[s].filter_out;
561
562 l = b;
563 }
564
565 if (!f)
566 {
567 LOG(3, 0, s, session[s].tunnel, " Invalid filter\n");
568 continue;
569 }
570
571 for (*f = 0, i = 0; !*f && i < MAXFILTER; i++)
572 if (strlen(ip_filters[i].name) == l &&
573 !strncmp(ip_filters[i].name, filter, l))
574 *f = i + 1;
575
576 if (*f)
577 ip_filters[*f - 1].used++;
578 else
579 LOG(3, 0, s, session[s].tunnel, " Unknown filter\n");
580
581 }
582 else if (*p == 26)
583 {
584 // Vendor-Specific Attribute
585 int vendor = ntohl(*(int *)(p + 2));
586 char attrib = *(p + 6);
587 char attrib_length = *(p + 7) - 2;
588 LOG(3, 0, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor, attrib, attrib_length);
589 if (attrib_length == 0) continue;
590 if (attrib != 1)
591 LOG(3, 0, s, session[s].tunnel, " Unknown vendor-specific\n");
592 else
593 {
594 char *avpair, *value, *key, *newp;
595 avpair = key = calloc(attrib_length + 1, 1);
596 memcpy(avpair, p + 8, attrib_length);
597 LOG(3, 0, s, session[s].tunnel, " Cisco-Avpair value: %s\n", avpair);
598 do {
599 value = strchr(key, '=');
600 if (!value) break;
601 *value++ = 0;
602
603 // Trim quotes off reply string
604 if (*value == '\'' || *value == '\"')
605 {
606 char *x;
607 value++;
608 x = value + strlen(value) - 1;
609 if (*x == '\'' || *x == '\"')
610 *x = 0;
611 }
612
613 // Run hooks
614 newp = strchr(value, ',');
615 if (newp) *newp++ = 0;
616 {
617 struct param_radius_response p = { &tunnel[session[s].tunnel], &session[s], key, value };
618 run_plugins(PLUGIN_RADIUS_RESPONSE, &p);
619 }
620 key = newp;
621 } while (newp);
622 free(avpair);
623 }
624 }
625 }
626 }
627 else if (*buf == 3)
628 {
629 LOG(2, 0, s, session[s].tunnel, " Authentication denied for %s\n", session[s].user);
630 //FIXME: We should tear down the session here!
631 break;
632 }
633
634 if (!session[s].dns1 && config->default_dns1)
635 {
636 session[s].dns1 = htonl(config->default_dns1);
637 LOG(3, 0, s, t, " Sending dns1 = %s\n", inet_toa(config->default_dns1));
638 }
639 if (!session[s].dns2 && config->default_dns2)
640 {
641 session[s].dns2 = htonl(config->default_dns2);
642 LOG(3, 0, s, t, " Sending dns2 = %s\n", inet_toa(config->default_dns2));
643 }
644
645 // Valid Session, set it up
646 session[s].unique_id = 0;
647 sessionsetup(t, s);
648 }
649 else
650 {
651 // An ack for a stop or start record.
652 LOG(3, 0, s, t, " RADIUS accounting ack recv in state %s\n", radius_states[radius[r].state]);
653 break;
654 }
655 } while (0);
656
657 // finished with RADIUS
658 radiusclear(r, s);
659 }
660
661 // Send a retry for RADIUS/CHAP message
662 void radiusretry(u16 r)
663 {
664 sessionidt s = radius[r].session;
665 tunnelidt t = 0;
666
667 CSTAT(call_radiusretry);
668
669 if (s)
670 t = session[s].tunnel;
671 radius[r].retry = backoff(radius[r].try + 1);
672 switch (radius[r].state)
673 {
674 case RADIUSCHAP: // sending CHAP down PPP
675 sendchap(t, s);
676 break;
677 case RADIUSIPCP:
678 sendipcp(t, s); // send IPCP
679 break;
680 case RADIUSAUTH: // sending auth to RADIUS server
681 radiussend(r, RADIUSAUTH);
682 break;
683 case RADIUSSTART: // sending start accounting to RADIUS server
684 radiussend(r, RADIUSSTART);
685 break;
686 case RADIUSSTOP: // sending stop accounting to RADIUS server
687 radiussend(r, RADIUSSTOP);
688 break;
689 default:
690 case RADIUSNULL: // Not in use
691 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
692 // free up RADIUS task
693 radiusclear(r, s);
694 LOG(3, 0, s, session[s].tunnel, "Freeing up radius session %d\n", r);
695 break;
696 }
697 }