logging tweak
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.20.2.3 2005/05/30 06:35:19 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 configt *config;
25 extern int *radfds;
26 extern ip_filtert *ip_filters;
27
28 static 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, "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 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(uint16_t 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 uint16_t get_free_radius()
63 {
64 int count;
65 static uint32_t 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, "Can't find a free radius session! This is very bad!\n");
80 return 0;
81 }
82
83 uint16_t radiusnew(sessionidt s)
84 {
85 uint16_t r = session[s].radius;
86
87 /* re-use */
88 if (r)
89 {
90 LOG(3, 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, 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, s, session[s].tunnel, "Allocated radius %d\n", r);
108 return r;
109 }
110
111 // Send a RADIUS request
112 void radiussend(uint16_t r, uint8_t state)
113 {
114 struct sockaddr_in addr;
115 uint8_t b[4096]; // RADIUS packet
116 char pass[129];
117 int pl;
118 uint8_t *p;
119 sessionidt s;
120
121 CSTAT(call_radiussend);
122
123 s = radius[r].session;
124 if (!config->numradiusservers)
125 {
126 LOG(0, s, session[s].tunnel, "No RADIUS servers\n");
127 return;
128 }
129 if (!*config->radiussecret)
130 {
131 LOG(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++) + 20; // 3s, 4s, 6s, 10s...
146 LOG(4, 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
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, 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, "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 *(uint32_t *) (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 *(uint32_t *) (p + 2) = htonl(session[s].cin);
261 p += p[1];
262 *p = 43; // output octets
263 p[1] = 6;
264 *(uint32_t *) (p + 2) = htonl(session[s].cout);
265 p += p[1];
266 *p = 46; // session time
267 p[1] = 6;
268 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
269 p += p[1];
270 *p = 47; // input packets
271 p[1] = 6;
272 *(uint32_t *) (p + 2) = htonl(session[s].pin);
273 p += p[1];
274 *p = 48; // output spackets
275 p[1] = 6;
276 *(uint32_t *) (p + 2) = htonl(session[s].pout);
277 p += p[1];
278 }
279 else
280 { // start
281 *p = 41; // delay
282 p[1] = 6;
283 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
284 p += p[1];
285 }
286
287 if (session[s].snoop_ip && session[s].snoop_port)
288 {
289 *p = 26; // vendor-specific
290 *(uint32_t *) (p + 2) = htonl(9); // Cisco
291 p[6] = 1; // Cisco-Avpair
292 p[7] = 2 + sprintf(p + 8, "intercept=%s:%d",
293 fmtaddr(session[s].snoop_ip, 0), session[s].snoop_port);
294
295 p[1] = p[7] + 6;
296 p += p[1];
297 }
298 }
299 }
300 if (s)
301 {
302 *p = 5; // NAS-Port
303 p[1] = 6;
304 *(uint32_t *) (p + 2) = htonl(s);
305 p += p[1];
306 }
307 if (s && session[s].ip)
308 {
309 *p = 8; // Framed-IP-Address
310 p[1] = 6;
311 *(uint32_t *) (p + 2) = htonl(session[s].ip);
312 p += p[1];
313 }
314 if (*session[s].called)
315 {
316 *p = 30; // called
317 p[1] = strlen(session[s].called) + 2;
318 strcpy(p + 2, session[s].called);
319 p += p[1];
320 }
321 if (*radius[r].calling)
322 {
323 *p = 31; // calling
324 p[1] = strlen(radius[r].calling) + 2;
325 strcpy(p + 2, radius[r].calling);
326 p += p[1];
327 }
328 else if (*session[s].calling)
329 {
330 *p = 31; // calling
331 p[1] = strlen(session[s].calling) + 2;
332 strcpy(p + 2, session[s].calling);
333 p += p[1];
334 }
335 // NAS-IP-Address
336 *p = 4;
337 p[1] = 6;
338 *(uint32_t *)(p + 2) = config->bind_address;
339 p += p[1];
340
341 // All AVpairs added
342 *(uint16_t *) (b + 2) = htons(p - b);
343 if (state != RADIUSAUTH)
344 {
345 // Build auth for accounting packet
346 char z[16] = {0};
347 char hash[16] = {0};
348 MD5_CTX ctx;
349 MD5Init(&ctx);
350 MD5Update(&ctx, b, 4);
351 MD5Update(&ctx, z, 16);
352 MD5Update(&ctx, b + 20, (p - b) - 20);
353 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
354 MD5Final(hash, &ctx);
355 memcpy(b + 4, hash, 16);
356 memcpy(radius[r].auth, hash, 16);
357 }
358 memset(&addr, 0, sizeof(addr));
359 addr.sin_family = AF_INET;
360 *(uint32_t *) & addr.sin_addr = config->radiusserver[(radius[r].try - 1) % config->numradiusservers];
361 {
362 // get radius port
363 uint16_t port = config->radiusport[(radius[r].try - 1) % config->numradiusservers];
364 // no need to define the accounting port for itself:
365 // the accounting port is as far as I know always one more
366 // than the auth port JK 20040713
367 addr.sin_port = htons((state == RADIUSAUTH) ? port : port+1);
368 }
369
370 LOG_HEX(5, "RADIUS Send", b, (p - b));
371 sendto(radfds[r & RADIUS_MASK], b, p - b, 0, (void *) &addr, sizeof(addr));
372 }
373
374 // process RADIUS response
375 void processrad(uint8_t *buf, int len, char socket_index)
376 {
377 uint8_t b[MAXCONTROL];
378 MD5_CTX ctx;
379 uint16_t r;
380 sessionidt s;
381 tunnelidt t = 0;
382 hasht hash;
383 uint8_t routes = 0;
384
385 int r_code, r_id ; // Radius code.
386
387 r_code = buf[0]; // First byte in radius packet.
388 r_id = buf[1]; // radius reply indentifier.
389
390
391 CSTAT(call_processrad);
392
393 LOG_HEX(5, "RADIUS Response", buf, len);
394 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
395 {
396 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len);
397 return ;
398 }
399 len = ntohs(*(uint16_t *) (buf + 2));
400 r = socket_index | (r_id << RADIUS_SHIFT);
401 s = radius[r].session;
402 LOG(3, s, session[s].tunnel, "Received %s, radius %d response for session %u (code %d, id %d)\n",
403 radius_states[radius[r].state], r, s, r_code, r_id);
404 if (!s && radius[r].state != RADIUSSTOP)
405 {
406 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
407 return;
408 }
409 if (radius[r].state != RADIUSAUTH && radius[r].state != RADIUSSTART && radius[r].state != RADIUSSTOP)
410 {
411 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
412 return;
413 }
414 t = session[s].tunnel;
415 MD5Init(&ctx);
416 MD5Update(&ctx, buf, 4);
417 MD5Update(&ctx, radius[r].auth, 16);
418 MD5Update(&ctx, buf + 20, len - 20);
419 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
420 MD5Final(hash, &ctx);
421 do {
422 if (memcmp(hash, buf + 4, 16))
423 {
424 LOG(0, s, session[s].tunnel, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
425 return; // Do nothing. On timeout, it will try the next radius server.
426 }
427 if ((radius[r].state == RADIUSAUTH && *buf != 2 && *buf != 3) ||
428 ((radius[r].state == RADIUSSTART || radius[r].state == RADIUSSTOP) && *buf != 5))
429 {
430 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response %d\n", *buf);
431 return; // We got something we didn't expect. Let the timeouts take
432 // care off finishing the radius session if that's really correct.
433 }
434 if (radius[r].state == RADIUSAUTH)
435 {
436 LOG(4, s, session[s].tunnel, " Original response is \"%s\"\n", (*buf == 2) ? "accept" : "reject");
437 // process auth response
438 if (radius[r].chap)
439 {
440 // CHAP
441 uint8_t *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
442 if (!p) return; // Abort!
443
444 {
445 struct param_post_auth packet = { &tunnel[t], &session[s], session[s].user, (*buf == 2), PPPCHAP };
446 run_plugins(PLUGIN_POST_AUTH, &packet);
447 *buf = packet.auth_allowed ? 2 : 3;
448 }
449
450 LOG(3, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
451 (*buf == 2) ? "allowed" : "denied");
452 *p = (*buf == 2) ? 3 : 4; // ack/nak
453 p[1] = radius[r].id;
454 *(uint16_t *) (p + 2) = ntohs(4); // no message
455 tunnelsend(b, (p - b) + 4, t); // send it
456 }
457 else
458 {
459 // PAP
460 uint8_t *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPPAP);
461 if (!p) return; // Abort!
462
463 {
464 struct param_post_auth packet = { &tunnel[t], &session[s], session[s].user, (*buf == 2), PPPPAP };
465 run_plugins(PLUGIN_POST_AUTH, &packet);
466 *buf = packet.auth_allowed ? 2 : 3;
467 }
468
469 LOG(3, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
470 (*buf == 2) ? "allowed" : "denied");
471 // ack/nak
472 *p = *buf;
473 p[1] = radius[r].id;
474 *(uint16_t *) (p + 2) = ntohs(5);
475 p[4] = 0; // no message
476 tunnelsend(b, (p - b) + 5, t); // send it
477 }
478
479 if (*buf == 2)
480 {
481 // Login successful
482 // Extract IP, routes, etc
483 uint8_t *p = buf + 20;
484 uint8_t *e = buf + len;
485 for (; p + 2 <= e && p[1] && p + p[1] <= e; p += p[1])
486 {
487 if (*p == 8)
488 {
489 // Framed-IP-Address
490 if (p[1] < 6) continue;
491 session[s].ip = ntohl(*(uint32_t *) (p + 2));
492 session[s].ip_pool_index = -1;
493 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
494 fmtaddr(htonl(session[s].ip), 0));
495
496 if (session[s].ip == 0xFFFFFFFE)
497 session[s].ip = 0; // assign from pool
498 }
499 else if (*p == 135)
500 {
501 // DNS address
502 if (p[1] < 6) continue;
503 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
504 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
505 fmtaddr(htonl(session[s].dns1), 0));
506 }
507 else if (*p == 136)
508 {
509 // DNS address
510 if (p[1] < 6) continue;
511 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
512 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
513 fmtaddr(htonl(session[s].dns2), 0));
514 }
515 else if (*p == 22)
516 {
517 // Framed-Route
518 in_addr_t ip = 0, mask = 0;
519 uint8_t u = 0;
520 uint8_t bits = 0;
521 uint8_t *n = p + 2;
522 uint8_t *e = p + p[1];
523 while (n < e && (isdigit(*n) || *n == '.'))
524 {
525 if (*n == '.')
526 {
527 ip = (ip << 8) + u;
528 u = 0;
529 }
530 else
531 u = u * 10 + *n - '0';
532 n++;
533 }
534 ip = (ip << 8) + u;
535 if (*n == '/')
536 {
537 n++;
538 while (n < e && isdigit(*n))
539 bits = bits * 10 + *n++ - '0';
540 mask = (( -1) << (32 - bits));
541 }
542 else if ((ip >> 24) < 128)
543 mask = 0xFF0000;
544 else if ((ip >> 24) < 192)
545 mask = 0xFFFF0000;
546 else
547 mask = 0xFFFFFF00;
548
549 if (routes == MAXROUTE)
550 {
551 LOG(1, s, session[s].tunnel, " Too many routes\n");
552 }
553 else if (ip)
554 {
555 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%s\n",
556 fmtaddr(htonl(ip), 0), fmtaddr(htonl(mask), 1));
557
558 session[s].route[routes].ip = ip;
559 session[s].route[routes].mask = mask;
560 routes++;
561 }
562 }
563 else if (*p == 11)
564 {
565 // Filter-Id
566 char *filter = p + 2;
567 int l = p[1] - 2;
568 char *suffix;
569 uint8_t *f = 0;
570 int i;
571
572 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
573 if ((suffix = memchr(filter, '.', l)))
574 {
575 int b = suffix - filter;
576 if (l - b == 3 && !memcmp("in", suffix+1, 2))
577 f = &session[s].filter_in;
578 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
579 f = &session[s].filter_out;
580
581 l = b;
582 }
583
584 if (!f)
585 {
586 LOG(3, s, session[s].tunnel, " Invalid filter\n");
587 continue;
588 }
589
590 for (*f = 0, i = 0; !*f && i < MAXFILTER; i++)
591 if (strlen(ip_filters[i].name) == l &&
592 !strncmp(ip_filters[i].name, filter, l))
593 *f = i + 1;
594
595 if (*f)
596 ip_filters[*f - 1].used++;
597 else
598 LOG(3, s, session[s].tunnel, " Unknown filter\n");
599
600 }
601 else if (*p == 26 && p[1] >= 7)
602 {
603 // Vendor-Specific Attribute
604 int vendor = ntohl(*(int *)(p + 2));
605 char attrib = *(p + 6);
606 char attrib_length = *(p + 7) - 2;
607 char *avpair, *value, *key, *newp;
608
609 LOG(3, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor, attrib, attrib_length);
610 if (vendor != 9 || attrib != 1)
611 {
612 LOG(3, s, session[s].tunnel, " Unknown vendor-specific\n");
613 continue;
614 }
615
616 if (attrib_length < 0) continue;
617
618 avpair = key = calloc(attrib_length + 1, 1);
619 memcpy(avpair, p + 8, attrib_length);
620 LOG(3, s, session[s].tunnel, " Cisco-Avpair value: %s\n", avpair);
621 do {
622 value = strchr(key, '=');
623 if (!value) break;
624 *value++ = 0;
625
626 // Trim quotes off reply string
627 if (*value == '\'' || *value == '\"')
628 {
629 char *x;
630 value++;
631 x = value + strlen(value) - 1;
632 if (*x == '\'' || *x == '\"')
633 *x = 0;
634 }
635
636 // Run hooks
637 newp = strchr(value, ',');
638 if (newp) *newp++ = 0;
639 {
640 struct param_radius_response p = { &tunnel[session[s].tunnel], &session[s], key, value };
641 run_plugins(PLUGIN_RADIUS_RESPONSE, &p);
642 }
643 key = newp;
644 } while (newp);
645 free(avpair);
646 }
647 }
648 }
649 else if (*buf == 3)
650 {
651 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
652 sessionkill(s, "Authentication rejected");
653 break;
654 }
655
656 if (!session[s].dns1 && config->default_dns1)
657 {
658 session[s].dns1 = htonl(config->default_dns1);
659 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
660 }
661 if (!session[s].dns2 && config->default_dns2)
662 {
663 session[s].dns2 = htonl(config->default_dns2);
664 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
665 }
666
667 // Valid Session, set it up
668 session[s].unique_id = 0;
669 sessionsetup(t, s);
670 }
671 else
672 {
673 // An ack for a stop or start record.
674 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_states[radius[r].state]);
675 break;
676 }
677 } while (0);
678
679 // finished with RADIUS
680 radiusclear(r, s);
681 }
682
683 // Send a retry for RADIUS/CHAP message
684 void radiusretry(uint16_t r)
685 {
686 sessionidt s = radius[r].session;
687 tunnelidt t = 0;
688
689 CSTAT(call_radiusretry);
690
691 if (s)
692 t = session[s].tunnel;
693
694 switch (radius[r].state)
695 {
696 case RADIUSCHAP: // sending CHAP down PPP
697 sendchap(t, s);
698 break;
699 case RADIUSIPCP:
700 sendipcp(t, s); // send IPCP
701 break;
702 case RADIUSAUTH: // sending auth to RADIUS server
703 radiussend(r, RADIUSAUTH);
704 break;
705 case RADIUSSTART: // sending start accounting to RADIUS server
706 radiussend(r, RADIUSSTART);
707 break;
708 case RADIUSSTOP: // sending stop accounting to RADIUS server
709 radiussend(r, RADIUSSTOP);
710 break;
711 default:
712 case RADIUSNULL: // Not in use
713 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
714 // free up RADIUS task
715 radiusclear(r, s);
716 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
717 break;
718 }
719 }