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