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