Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / xl2tpd / aaa.c
blob556e3345471528b32da6578cb223ad4ae015e9de
1 /*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
6 * Mark Spencer
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Authorization, Accounting, and Access control
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <errno.h>
22 #include "l2tp.h"
24 extern void bufferDump (char *, int);
26 /* FIXME: Accounting? */
28 struct addr_ent *uaddr[ADDR_HASH_SIZE];
30 void init_addr ()
32 int x;
33 for (x = 0; x < ADDR_HASH_SIZE; x++)
34 uaddr[x] = NULL;
37 static int ip_used (unsigned int addr)
39 struct addr_ent *tmp;
40 tmp = uaddr[addr % ADDR_HASH_SIZE];
41 while (tmp)
43 if (tmp->addr == addr)
44 return -1;
45 tmp = tmp->next;
47 return 0;
50 void mk_challenge (unsigned char *c, int length)
52 get_entropy(c, length);
54 /* int x;
55 int *s = (int *) c;
56 for (x = 0; x < length / sizeof (int); x++)
57 s[x] = rand (); */
60 void reserve_addr (unsigned int addr)
62 /* Mark this address as in use */
63 struct addr_ent *tmp, *tmp2;
64 addr = ntohl (addr);
65 if (ip_used (addr))
66 return;
67 tmp = uaddr[addr % ADDR_HASH_SIZE];
68 tmp2 = (struct addr_ent *) malloc (sizeof (struct addr_ent));
69 uaddr[addr % ADDR_HASH_SIZE] = tmp2;
70 tmp2->next = tmp;
71 tmp2->addr = addr;
74 void unreserve_addr (unsigned int addr)
76 struct addr_ent *tmp, *last = NULL, *z;
77 addr = ntohl (addr);
78 tmp = uaddr[addr % ADDR_HASH_SIZE];
79 while (tmp)
81 if (tmp->addr == addr)
83 if (last)
85 last->next = tmp->next;
87 else
89 uaddr[addr % ADDR_HASH_SIZE] = tmp->next;
91 z = tmp;
92 tmp = tmp->next;
93 free (z);
95 else
97 last = tmp;
98 tmp = tmp->next;
103 unsigned int get_addr (struct iprange *ipr)
105 unsigned int x, y;
106 int status;
107 struct iprange *ipr2;
108 while (ipr)
110 if (ipr->sense == SENSE_ALLOW)
111 for (x = ntohl (ipr->start); x <= ntohl (ipr->end); x++)
113 /* Found an IP in an ALLOW range, check to be sure it is
114 consistant through the remaining regions */
115 if (!ip_used (x))
117 status = SENSE_ALLOW;
118 ipr2 = ipr->next;
119 while (ipr2)
121 if ((x >= ntohl (ipr2->start))
122 && (x <= ntohl (ipr2->end)))
123 status = ipr2->sense;
124 ipr2 = ipr2->next;
126 y = htonl (x);
127 if (status == SENSE_ALLOW)
128 return y;
131 ipr = ipr->next;
133 return 0;
136 static int get_secret (char *us, char *them, unsigned char *secret, int size)
138 FILE *f;
139 char buf[STRLEN];
140 char *u, *t, *s;
141 int num = 0;
142 f = fopen (gconfig.authfile, "r");
143 if (!f)
145 l2tp_log (LOG_WARNING, "%s : Unable to open '%s' for authentication\n",
146 __FUNCTION__, gconfig.authfile);
147 return 0;
149 while (!feof (f))
151 num++;
152 if (NULL == fgets (buf, sizeof (buf), f))
154 /* Error or EOF */
155 break;
157 /* Strip comments */
158 for (t = buf; *t; t++)
159 *t = ((*t == '#') || (*t == ';')) ? 0 : *t;
160 /* Strip trailing whitespace */
161 for (t = buf + strlen (buf) - 1; (t >= buf) && (*t < 33); t--)
162 *t = 0;
163 if (!strlen (buf))
164 continue; /* Empty line */
165 u = buf;
166 while (*u && (*u < 33))
167 u++;
168 /* us */
169 if (!*u)
171 l2tp_log (LOG_WARNING,
172 "%s: Invalid authentication info (no us), line %d\n",
173 __FUNCTION__, num);
174 continue;
176 t = u;
177 while (*t > 32)
178 t++;
179 *(t++) = 0;
180 while (*t && (*t < 33))
181 t++;
182 /* them */
183 if (!*t)
185 l2tp_log (LOG_WARNING,
186 "%s: Invalid authentication info (nothem), line %d\n",
187 __FUNCTION__, num);
188 continue;
190 s = t;
191 while (*s > 33)
192 s++;
193 *(s++) = 0;
194 while (*s && (*s < 33))
195 s++;
196 if (!*s)
198 l2tp_log (LOG_WARNING,
199 "%s: Invalid authentication info (no secret), line %d\n",
200 __FUNCTION__, num);
201 continue;
203 if ((!strcasecmp (u, us) || !strcasecmp (u, "*")) &&
204 (!strcasecmp (t, them) || !strcasecmp (t, "*")))
206 #ifdef DEBUG_AUTH
207 l2tp_log (LOG_DEBUG,
208 "%s: we are '%s', they are '%s', secret is '%s'\n",
209 __FUNCTION__, u, t, s);
210 #endif
211 strncpy ((char *)secret, s, size);
212 fclose(f);
213 return -1;
216 fclose(f);
217 return 0;
220 int handle_challenge (struct tunnel *t, struct challenge *chal)
222 char *us;
223 char *them;
224 if (!t->lns && !t->lac)
226 l2tp_log (LOG_DEBUG, "%s: No LNS or LAC to handle challenge!\n",
227 __FUNCTION__);
228 return -1;
230 #ifdef DEBUG_AUTH
231 l2tp_log (LOG_DEBUG, "%s: making response for tunnel: %d\n", __FUNCTION__,
232 t->ourtid);
233 #endif
234 if (t->lns)
236 if (t->lns->hostname[0])
237 us = t->lns->hostname;
238 else
239 us = hostname;
240 if (t->lns->peername[0])
241 them = t->lns->peername;
242 else
243 them = t->hostname;
245 else
247 if (t->lac->hostname[0])
248 us = t->lac->hostname;
249 else
250 us = hostname;
251 if (t->lac->peername[0])
252 them = t->lac->peername;
253 else
254 them = t->hostname;
257 if (!get_secret (us, them, chal->secret, sizeof (chal->secret)))
259 l2tp_log (LOG_DEBUG, "%s: no secret found for us='%s' and them='%s'\n",
260 __FUNCTION__, us, them);
261 return -1;
264 #if DEBUG_AUTH
265 l2tp_log (LOG_DEBUG, "*%s: Here comes the chal->ss:\n", __FUNCTION__);
266 bufferDump (&chal->ss, 1);
268 l2tp_log (LOG_DEBUG, "%s: Here comes the secret\n", __FUNCTION__);
269 bufferDump (chal->secret, strlen (chal->secret));
271 l2tp_log (LOG_DEBUG, "%s: Here comes the challenge\n", __FUNCTION__);
272 bufferDump (chal->challenge, chal->chal_len);
273 #endif
275 memset (chal->response, 0, MD_SIG_SIZE);
276 MD5Init (&chal->md5);
277 MD5Update (&chal->md5, &chal->ss, 1);
278 MD5Update (&chal->md5, chal->secret, strlen ((char *)chal->secret));
279 MD5Update (&chal->md5, chal->challenge, chal->chal_len);
280 MD5Final (chal->response, &chal->md5);
281 #ifdef DEBUG_AUTH
282 l2tp_log (LOG_DEBUG, "response is %X%X%X%X to '%s' and %X%X%X%X, %d\n",
283 *((int *) &chal->response[0]),
284 *((int *) &chal->response[4]),
285 *((int *) &chal->response[8]),
286 *((int *) &chal->response[12]),
287 chal->secret,
288 *((int *) &chal->challenge[0]),
289 *((int *) &chal->challenge[4]),
290 *((int *) &chal->challenge[8]),
291 *((int *) &chal->challenge[12]), chal->ss);
292 #endif
293 chal->state = STATE_CHALLENGED;
294 return 0;
297 struct lns *get_lns (struct tunnel *t)
300 * Look through our list of LNS's and
301 * find a reasonable LNS for this call
302 * if one is available
304 struct lns *lns;
305 struct iprange *ipr;
306 int allow, checkdefault = 0;
307 /* If access control is disabled, we give the default
308 otherwise, we give nothing */
309 allow = 0;
310 lns = lnslist;
311 if (!lns)
313 lns = deflns;
314 checkdefault = -1;
316 while (lns)
318 ipr = lns->lacs;
319 while (ipr)
321 if ((ntohl (t->peer.sin_addr.s_addr) >= ntohl (ipr->start)) &&
322 (ntohl (t->peer.sin_addr.s_addr) <= ntohl (ipr->end)))
324 #ifdef DEBUG_AAA
325 l2tp_log (LOG_DEBUG,
326 "$s: Rule %s to %s, sense %s matched %s\n", __FUNCTION__,
327 IPADDY (ipr->start), IPADDY (ipr->end),
328 (ipr->sense ? "allow" : "deny"), IPADDY (t->peer.sin_addr.s_addr));
329 #endif
330 allow = ipr->sense;
332 ipr = ipr->next;
334 if (allow)
335 return lns;
336 lns = lns->next;
337 if (!lns && !checkdefault)
339 lns = deflns;
340 checkdefault = -1;
343 if (gconfig.accesscontrol)
344 return NULL;
345 else
346 return deflns;
349 #ifdef DEBUG_HIDDEN
350 static void print_md5 (void * const md5)
352 int *i = (int *) md5;
353 l2tp_log (LOG_DEBUG, "%X%X%X%X\n", i[0], i[1], i[2], i[3], i[4]);
356 static inline void print_challenge (struct challenge *chal)
358 l2tp_log (LOG_DEBUG, "vector: ");
359 print_md5 (chal->vector);
360 l2tp_log (LOG_DEBUG, "secret: %s\n", chal->secret);
362 #endif
363 void encrypt_avp (struct buffer *buf, _u16 len, struct tunnel *t)
365 /* Encrypts an AVP of len, at data. We assume there
366 are two "spare bytes" before the data pointer,l but otherwise
367 this is just a normal AVP that is about to be returned from
368 an avpsend routine */
369 struct avp_hdr *new_hdr =
370 (struct avp_hdr *) (buf->start + buf->len - len);
371 struct avp_hdr *old_hdr =
372 (struct avp_hdr *) (buf->start + buf->len - len + 2);
373 _u16 length, flags, attr; /* New length, old flags */
374 unsigned char *ptr, *end;
375 int cnt;
376 unsigned char digest[MD_SIG_SIZE];
377 unsigned char *previous_segment;
379 /* FIXME: Should I pad more randomly? Right now I pad to nearest 16 bytes */
380 length =
381 ((len - sizeof (struct avp_hdr) + 1) / 16 + 1) * 16 +
382 sizeof (struct avp_hdr);
383 flags = htons (old_hdr->length) & 0xF000;
384 new_hdr->length = htons (length | flags | HBIT);
385 new_hdr->vendorid = old_hdr->vendorid;
386 new_hdr->attr = attr = old_hdr->attr;
387 /* This is really the length field of the hidden sub-format */
388 old_hdr->attr = htons (len - sizeof (struct avp_hdr));
389 /* Okay, now we've rewritten the header, as it should be. Let's start
390 encrypting the actual data now */
391 buf->len -= len;
392 buf->len += length;
393 /* Back to the beginning of real data, including the original length AVP */
395 MD5Init (&t->chal_them.md5);
396 MD5Update (&t->chal_them.md5, (void *) &attr, 2);
397 MD5Update (&t->chal_them.md5, t->chal_them.secret,
398 strlen ((char *)t->chal_them.secret));
399 MD5Update (&t->chal_them.md5, t->chal_them.vector, VECTOR_SIZE);
400 MD5Final (digest, &t->chal_them.md5);
402 /* Though not a "MUST" in the spec, our subformat length is always a multiple of 16 */
403 ptr = ((unsigned char *) new_hdr) + sizeof (struct avp_hdr);
404 end = ((unsigned char *) new_hdr) + length;
405 previous_segment = ptr;
406 while (ptr < end)
408 #if DEBUG_HIDDEN
409 l2tp_log (LOG_DEBUG, "%s: The digest to be XOR'ed\n", __FUNCTION__);
410 bufferDump (digest, MD_SIG_SIZE);
411 l2tp_log (LOG_DEBUG, "%s: The plaintext to be XOR'ed\n", __FUNCTION__);
412 bufferDump (ptr, MD_SIG_SIZE);
413 #endif
414 for (cnt = 0; cnt < MD_SIG_SIZE; cnt++, ptr++)
416 *ptr = *ptr ^ digest[cnt];
418 #if DEBUG_HIDDEN
419 l2tp_log (LOG_DEBUG, "%s: The result of XOR\n", __FUNCTION__);
420 bufferDump (previous_segment, MD_SIG_SIZE);
421 #endif
422 if (ptr < end)
424 MD5Init (&t->chal_them.md5);
425 MD5Update (&t->chal_them.md5, t->chal_them.secret,
426 strlen ((char *)t->chal_them.secret));
427 MD5Update (&t->chal_them.md5, previous_segment, MD_SIG_SIZE);
428 MD5Final (digest, &t->chal_them.md5);
430 previous_segment = ptr;
434 int decrypt_avp (char *buf, struct tunnel *t)
436 /* Decrypts a hidden AVP pointed to by buf. The
437 new header will be exptected to be two characters
438 offset from the old */
439 int cnt = 0;
440 int len, olen, flags;
441 unsigned char digest[MD_SIG_SIZE];
442 char *ptr, *end;
443 _u16 attr;
444 struct avp_hdr *old_hdr = (struct avp_hdr *) buf;
445 struct avp_hdr *new_hdr = (struct avp_hdr *) (buf + 2);
446 int saved_segment_len; /* maybe less 16; may be used if the cipher is longer than 16 octets */
447 unsigned char saved_segment[MD_SIG_SIZE];
448 ptr = ((char *) old_hdr) + sizeof (struct avp_hdr);
449 olen = old_hdr->length & 0x0FFF;
450 end = buf + olen;
451 if (!t->chal_us.vector)
453 l2tp_log (LOG_DEBUG,
454 "%s: Hidden bit set, but no random vector specified!\n", __FUNCTION__);
455 return -EINVAL;
457 /* First, let's decrypt all the data. We're not guaranteed
458 that it will be padded to a 16 byte boundary, so we
459 have to be more careful than when encrypting */
460 attr = ntohs (old_hdr->attr);
461 MD5Init (&t->chal_us.md5);
462 MD5Update (&t->chal_us.md5, (void *) &attr, 2);
463 MD5Update (&t->chal_us.md5, t->chal_us.secret,
464 strlen ((char *)t->chal_us.secret));
465 MD5Update (&t->chal_us.md5, t->chal_us.vector, t->chal_us.vector_len);
466 MD5Final (digest, &t->chal_us.md5);
467 #ifdef DEBUG_HIDDEN
468 l2tp_log (LOG_DEBUG, "attribute is %d and challenge is: ", attr);
469 print_challenge (&t->chal_us);
470 l2tp_log (LOG_DEBUG, "md5 is: ");
471 print_md5 (digest);
472 #endif
473 while (ptr < end)
475 if (cnt >= MD_SIG_SIZE)
477 MD5Init (&t->chal_us.md5);
478 MD5Update (&t->chal_us.md5, t->chal_us.secret,
479 strlen ((char *)t->chal_us.secret));
480 MD5Update (&t->chal_us.md5, saved_segment, MD_SIG_SIZE);
481 MD5Final (digest, &t->chal_us.md5);
482 cnt = 0;
484 /* at the beginning of each segment, we save the current segment (16 octets or less) of cipher
485 * so that the next round of MD5 (if there is a next round) hash could use it
487 if (cnt == 0)
489 saved_segment_len =
490 (end - ptr < MD_SIG_SIZE) ? (end - ptr) : MD_SIG_SIZE;
491 memcpy (saved_segment, ptr, saved_segment_len);
493 *ptr = *ptr ^ digest[cnt++];
494 ptr++;
496 /* Hopefully we're all nice and decrypted now. Let's rewrite the header.
497 First save the old flags, and get the new stuff */
498 flags = old_hdr->length & 0xF000 & ~HBIT;
499 len = ntohs (new_hdr->attr) + sizeof (struct avp_hdr);
500 if (len > olen - 2)
502 l2tp_log (LOG_DEBUG,
503 "%s: Decrypted length is too long (%d > %d)\n", __FUNCTION__, len,
504 olen - 2);
505 return -EINVAL;
507 new_hdr->attr = old_hdr->attr;
508 new_hdr->vendorid = old_hdr->vendorid;
509 new_hdr->length = len | flags;
510 return 0;