minidlna support now Samsung TV C550/C650 (thx amir909)
[tomato.git] / release / src / router / xl2tpd / file.c
blob3554cccbb28783ae509ded9c2540baa559f29ad2
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 * File format handling
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include "l2tp.h"
28 struct lns *lnslist;
29 struct lac *laclist;
30 struct lns *deflns;
31 struct lac *deflac;
32 struct global gconfig;
33 char filerr[STRLEN];
35 int parse_config (FILE *);
36 struct keyword words[];
38 int init_config ()
40 FILE *f;
41 int returnedValue;
43 gconfig.port = UDP_LISTEN_PORT;
44 gconfig.listenaddr = htonl(INADDR_ANY); /* Default is to bind (listen) to all interfaces */
45 gconfig.debug_avp = 0;
46 gconfig.debug_network = 0;
47 gconfig.packet_dump = 0;
48 gconfig.debug_tunnel = 0;
49 gconfig.debug_state = 0;
50 lnslist = NULL;
51 laclist = NULL;
52 deflac = (struct lac *) calloc (1, sizeof (struct lac));
54 f = fopen (gconfig.configfile, "r");
55 if (!f)
57 f = fopen (gconfig.altconfigfile, "r");
58 if (f)
60 l2tp_log (LOG_WARNING, "%s: Using old style config files %s and %s\n",
61 __FUNCTION__, gconfig.altconfigfile, gconfig.altauthfile);
62 strncpy (gconfig.authfile, gconfig.altauthfile,
63 sizeof (gconfig.authfile));
65 else
67 l2tp_log (LOG_CRIT, "%s: Unable to open config file %s or %s\n",
68 __FUNCTION__, gconfig.configfile, gconfig.altconfigfile);
69 return -1;
73 returnedValue = parse_config (f);
74 fclose (f);
75 return (returnedValue);
76 filerr[0] = 0;
79 struct lns *new_lns ()
81 struct lns *tmp;
82 tmp = (struct lns *) calloc (1, sizeof (struct lns));
83 if (!tmp)
85 l2tp_log (LOG_CRIT, "%s: Unable to allocate memory for new LNS\n",
86 __FUNCTION__);
87 return NULL;
89 tmp->next = NULL;
90 tmp->exclusive = 0;
91 tmp->localaddr = 0;
92 tmp->tun_rws = DEFAULT_RWS_SIZE;
93 tmp->call_rws = DEFAULT_RWS_SIZE;
94 tmp->rxspeed = DEFAULT_RX_BPS;
95 tmp->txspeed = DEFAULT_TX_BPS;
96 tmp->hbit = 0;
97 tmp->lbit = 0;
98 tmp->authpeer = 0;
99 tmp->authself = -1;
100 tmp->authname[0] = 0;
101 tmp->peername[0] = 0;
102 tmp->hostname[0] = 0;
103 tmp->entname[0] = 0;
104 tmp->range = NULL;
105 tmp->assign_ip = 1; /* default to 'yes' */
106 tmp->lacs = NULL;
107 tmp->passwdauth = 0;
108 tmp->pap_require = 0;
109 tmp->pap_refuse = 0;
110 tmp->chap_require = 0;
111 tmp->chap_refuse = 0;
112 tmp->idle = 0;
113 tmp->pridns = 0;
114 tmp->secdns = 0;
115 tmp->priwins = 0;
116 tmp->secwins = 0;
117 tmp->proxyarp = 0;
118 tmp->proxyauth = 0;
119 tmp->challenge = 0;
120 tmp->debug = 0;
121 tmp->pppoptfile[0] = 0;
122 tmp->t = NULL;
123 return tmp;
126 struct lac *new_lac ()
128 struct lac *tmp;
129 tmp = (struct lac *) calloc (1, sizeof (struct lac));
130 if (!tmp)
132 l2tp_log (LOG_CRIT, "%s: Unable to allocate memory for lac entry!\n",
133 __FUNCTION__);
134 return NULL;
136 tmp->next = NULL;
137 tmp->rsched = NULL;
138 tmp->localaddr = 0;
139 tmp->remoteaddr = 0;
140 tmp->lns = 0;
141 tmp->tun_rws = DEFAULT_RWS_SIZE;
142 tmp->call_rws = DEFAULT_RWS_SIZE;
143 tmp->hbit = 0;
144 tmp->lbit = 0;
145 tmp->authpeer = 0;
146 tmp->authself = -1;
147 tmp->authname[0] = 0;
148 tmp->peername[0] = 0;
149 tmp->hostname[0] = 0;
150 tmp->entname[0] = 0;
151 tmp->pap_require = 0;
152 tmp->pap_refuse = 0;
153 tmp->chap_require = 0;
154 tmp->chap_refuse = 0;
155 tmp->t = NULL;
156 tmp->redial = 0;
157 tmp->rtries = 0;
158 tmp->rmax = 0;
159 tmp->challenge = 0;
160 tmp->autodial = 0;
161 tmp->rtimeout = 30;
162 tmp->active = 0;
163 tmp->debug = 0;
164 tmp->pppoptfile[0] = 0;
165 tmp->defaultroute = 0;
166 return tmp;
169 int yesno (char *value)
171 if (!strcasecmp (value, "yes") || !strcasecmp (value, "y") ||
172 !strcasecmp (value, "true"))
173 return 1;
174 else if (!strcasecmp (value, "no") || !strcasecmp (value, "n") ||
175 !strcasecmp (value, "false"))
176 return 0;
177 else
178 return -1;
181 int set_boolean (char *word, char *value, int *ptr)
183 int val;
184 #ifdef DEBUG_FILE
185 l2tp_log (LOG_DEBUG, "set_%s: %s flag to '%s'\n", word, word, value);
186 #endif /* ; */
187 if ((val = yesno (value)) < 0)
189 snprintf (filerr, sizeof (filerr), "%s must be 'yes' or 'no'\n",
190 word);
191 return -1;
193 *ptr = val;
194 return 0;
197 int set_int (char *word, char *value, int *ptr)
199 int val;
200 #ifdef DEBUG_FILE
201 l2tp_log (LOG_DEBUG, "set_%s: %s flag to '%s'\n", word, word, value);
202 #endif /* ; */
203 if ((val = atoi (value)) < 0)
205 snprintf (filerr, sizeof (filerr), "%s must be a number\n", word);
206 return -1;
208 *ptr = val;
209 return 0;
212 int set_string (char *word, char *value, char *ptr, int len)
214 #ifdef DEBUG_FILE
215 l2tp_log (LOG_DEBUG, "set_%s: %s flag to '%s'\n", word, word, value);
216 #endif /* ; */
217 strncpy (ptr, value, len);
218 return 0;
221 int set_port (char *word, char *value, int context, void *item)
223 switch (context & ~CONTEXT_DEFAULT)
225 case CONTEXT_GLOBAL:
226 #ifdef DEBUG_FILE
227 l2tp_log (LOG_DEBUG, "set_port: Setting global port number to %s\n",
228 value);
229 #endif
230 set_int (word, value, &(((struct global *) item)->port));
231 break;
232 default:
233 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
234 word);
235 return -1;
237 return 0;
240 int set_rtimeout (char *word, char *value, int context, void *item)
242 if (atoi (value) < 1)
244 snprintf (filerr, sizeof (filerr),
245 "rtimeout value must be at least 1\n");
246 return -1;
248 switch (context & ~CONTEXT_DEFAULT)
250 case CONTEXT_LAC:
251 #ifdef DEBUG_FILE
252 l2tp_log (LOG_DEBUG, "set_rtimeout: Setting redial timeout to %s\n",
253 value);
254 #endif
255 set_int (word, value, &(((struct lac *) item)->rtimeout));
256 break;
257 default:
258 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
259 word);
260 return -1;
262 return 0;
265 int set_rws (char *word, char *value, int context, void *item)
267 if (atoi (value) < -1)
269 snprintf (filerr, sizeof (filerr),
270 "receive window size must be at least -1\n");
271 return -1;
273 switch (context & ~CONTEXT_DEFAULT)
275 case CONTEXT_LAC:
276 if (word[0] == 'c')
277 set_int (word, value, &(((struct lac *) item)->call_rws));
278 if (word[0] == 't')
280 set_int (word, value, &(((struct lac *) item)->tun_rws));
281 if (((struct lac *) item)->tun_rws < 1)
283 snprintf (filerr, sizeof (filerr),
284 "receive window size for tunnels must be at least 1\n");
285 return -1;
288 break;
289 case CONTEXT_LNS:
290 if (word[0] == 'c')
291 set_int (word, value, &(((struct lns *) item)->call_rws));
292 if (word[0] == 't')
294 set_int (word, value, &(((struct lns *) item)->tun_rws));
295 if (((struct lns *) item)->tun_rws < 1)
297 snprintf (filerr, sizeof (filerr),
298 "receive window size for tunnels must be at least 1\n");
299 return -1;
302 break;
303 default:
304 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
305 word);
306 return -1;
308 return 0;
311 int set_speed (char *word, char *value, int context, void *item)
313 if (atoi (value) < 1 )
315 snprintf (filerr, sizeof (filerr),
316 "bps must be greater than zero\n");
317 return -1;
319 switch (context & ~CONTEXT_DEFAULT)
321 case CONTEXT_LAC:
322 if (word[0] == 't')
323 set_int (word, value, &(((struct lac *) item)->txspeed));
324 else if (word[0] == 'r')
325 set_int (word, value, &(((struct lac *) item)->rxspeed));
326 else
328 set_int (word, value, &(((struct lac *) item)->rxspeed));
329 set_int (word, value, &(((struct lac *) item)->txspeed));
331 break;
332 case CONTEXT_LNS:
333 if (word[0] == 't')
334 set_int (word, value, &(((struct lns *) item)->txspeed));
335 else if (word[0] == 'r')
336 set_int (word, value, &(((struct lns *) item)->rxspeed));
337 else
339 set_int (word, value, &(((struct lns *) item)->rxspeed));
340 set_int (word, value, &(((struct lns *) item)->txspeed));
342 break;
343 default:
344 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
345 word);
346 return -1;
348 return 0;
351 int set_rmax (char *word, char *value, int context, void *item)
353 if (atoi (value) < 1)
355 snprintf (filerr, sizeof (filerr), "rmax value must be at least 1\n");
356 return -1;
358 switch (context & ~CONTEXT_DEFAULT)
360 case CONTEXT_LAC:
361 #ifdef DEBUG_FILE
362 l2tp_log (LOG_DEBUG, "set_rmax: Setting max redials to %s\n", value);
363 #endif
364 set_int (word, value, &(((struct lac *) item)->rmax));
365 break;
366 default:
367 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
368 word);
369 return -1;
371 return 0;
374 int set_authfile (char *word, char *value, int context, void *item)
376 if (!strlen (value))
378 snprintf (filerr, sizeof (filerr),
379 "no filename specified for authentication\n");
380 return -1;
382 switch (context & ~CONTEXT_DEFAULT)
384 case CONTEXT_GLOBAL:
385 #ifdef DEBUG_FILE
386 l2tp_log (LOG_DEBUG, "set_authfile: Setting global auth file to '%s'\n",
387 value);
388 #endif /* ; */
389 strncpy (((struct global *) item)->authfile, value,
390 sizeof (((struct global *)item)->authfile));
391 break;
392 default:
393 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
394 word);
395 return -1;
397 return 0;
400 int set_autodial (char *word, char *value, int context, void *item)
402 switch (context & ~CONTEXT_DEFAULT)
404 case CONTEXT_LAC:
405 if (set_boolean (word, value, &(((struct lac *) item)->autodial)))
406 return -1;
407 break;
408 default:
409 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
410 word);
411 return -1;
413 return 0;
416 int set_flow (char *word, char *value, int context, void *item)
418 int v;
419 set_boolean (word, value, &v);
420 if (v < 0)
421 return -1;
422 switch (context & ~CONTEXT_DEFAULT)
424 case CONTEXT_LAC:
425 if (v)
427 if (((struct lac *) item)->call_rws < 0)
428 ((struct lac *) item)->call_rws = 0;
430 else
432 ((struct lac *) item)->call_rws = -1;
434 break;
435 case CONTEXT_LNS:
436 if (v)
438 if (((struct lns *) item)->call_rws < 0)
439 ((struct lns *) item)->call_rws = 0;
441 else
443 ((struct lns *) item)->call_rws = -1;
445 break;
446 default:
447 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
448 word);
449 return -1;
451 return 0;
454 int set_defaultroute (char *word, char *value, int context, void *item)
456 switch (context & ~CONTEXT_DEFAULT)
458 case CONTEXT_LAC:
459 if (set_boolean (word, value, &(((struct lac *) item)->defaultroute)))
460 return -1;
461 break;
462 default:
463 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
464 word);
465 return -1;
467 return 0;
470 int set_authname (char *word, char *value, int context, void *item)
472 struct lac *l = (struct lac *) item;
473 struct lns *n = (struct lns *) item;
474 switch (context & ~CONTEXT_DEFAULT)
476 case CONTEXT_LNS:
477 if (set_string (word, value, n->authname, sizeof (n->authname)))
478 return -1;
479 break;
480 case CONTEXT_LAC:
481 if (set_string (word, value, l->authname, sizeof (l->authname)))
482 return -1;
483 break;
484 default:
485 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
486 word);
487 return -1;
489 return 0;
492 int set_hostname (char *word, char *value, int context, void *item)
494 struct lac *l = (struct lac *) item;
495 struct lns *n = (struct lns *) item;
496 switch (context & ~CONTEXT_DEFAULT)
498 case CONTEXT_LNS:
499 if (set_string (word, value, n->hostname, sizeof (n->hostname)))
500 return -1;
501 break;
502 case CONTEXT_LAC:
503 if (set_string (word, value, l->hostname, sizeof (l->hostname)))
504 return -1;
505 break;
506 default:
507 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
508 word);
509 return -1;
511 return 0;
514 int set_passwdauth (char *word, char *value, int context, void *item)
516 switch (context & ~CONTEXT_DEFAULT)
518 case CONTEXT_LNS:
519 if (set_boolean (word, value, &(((struct lns *) item)->passwdauth)))
520 return -1;
521 break;
522 default:
523 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
524 word);
525 return -1;
527 return 0;
530 int set_hbit (char *word, char *value, int context, void *item)
532 switch (context & ~CONTEXT_DEFAULT)
534 case CONTEXT_LAC:
535 if (set_boolean (word, value, &(((struct lac *) item)->hbit)))
536 return -1;
537 break;
538 case CONTEXT_LNS:
539 if (set_boolean (word, value, &(((struct lns *) item)->hbit)))
540 return -1;
541 break;
542 default:
543 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
544 word);
545 return -1;
547 return 0;
550 int set_challenge (char *word, char *value, int context, void *item)
552 switch (context & ~CONTEXT_DEFAULT)
554 case CONTEXT_LAC:
555 if (set_boolean (word, value, &(((struct lac *) item)->challenge)))
556 return -1;
557 break;
558 case CONTEXT_LNS:
559 if (set_boolean (word, value, &(((struct lns *) item)->challenge)))
560 return -1;
561 break;
562 default:
563 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
564 word);
565 return -1;
567 return 0;
570 int set_lbit (char *word, char *value, int context, void *item)
572 switch (context & ~CONTEXT_DEFAULT)
574 case CONTEXT_LAC:
575 if (set_boolean (word, value, &(((struct lac *) item)->lbit)))
576 return -1;
577 break;
578 case CONTEXT_LNS:
579 if (set_boolean (word, value, &(((struct lns *) item)->lbit)))
580 return -1;
581 break;
582 default:
583 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
584 word);
585 return -1;
587 return 0;
591 int set_debug (char *word, char *value, int context, void *item)
593 switch (context & ~CONTEXT_DEFAULT)
595 case CONTEXT_LAC:
596 if (set_boolean (word, value, &(((struct lac *) item)->debug)))
597 return -1;
598 break;
599 case CONTEXT_LNS:
600 if (set_boolean (word, value, &(((struct lns *) item)->debug)))
601 return -1;
602 break;
603 default:
604 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
605 word);
606 return -1;
608 return 0;
611 int set_pppoptfile (char *word, char *value, int context, void *item)
613 struct lac *l = (struct lac *) item;
614 struct lns *n = (struct lns *) item;
615 switch (context & ~CONTEXT_DEFAULT)
617 case CONTEXT_LNS:
618 if (set_string (word, value, n->pppoptfile, sizeof (n->pppoptfile)))
619 return -1;
620 break;
621 case CONTEXT_LAC:
622 if (set_string (word, value, l->pppoptfile, sizeof (l->pppoptfile)))
623 return -1;
624 break;
625 default:
626 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
627 word);
628 return -1;
630 return 0;
633 int set_papchap (char *word, char *value, int context, void *item)
635 int result;
636 char *c;
637 struct lac *l = (struct lac *) item;
638 struct lns *n = (struct lns *) item;
639 if (set_boolean (word, value, &result))
640 return -1;
641 c = strchr (word, ' ');
642 c++;
643 switch (context & ~CONTEXT_DEFAULT)
645 case CONTEXT_LAC:
646 if (c[0] == 'p') /* PAP */
647 if (word[2] == 'f')
648 l->pap_refuse = result;
649 else
650 l->pap_require = result;
651 else if (c[0] == 'a') /* Authentication */
652 if (word[2] == 'f')
653 l->authself = !result;
654 else
655 l->authpeer = result;
656 else /* CHAP */ if (word[2] == 'f')
657 l->chap_refuse = result;
658 else
659 l->chap_require = result;
660 break;
661 case CONTEXT_LNS:
662 if (c[0] == 'p') /* PAP */
663 if (word[2] == 'f')
664 n->pap_refuse = result;
665 else
666 n->pap_require = result;
667 else if (c[0] == 'a') /* Authentication */
668 if (word[2] == 'f')
669 n->authself = !result;
670 else
671 n->authpeer = result;
672 else /* CHAP */ if (word[2] == 'f')
673 n->chap_refuse = result;
674 else
675 n->chap_require = result;
676 break;
677 default:
678 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
679 word);
680 return -1;
682 return 0;
685 int set_redial (char *word, char *value, int context, void *item)
687 switch (context & ~CONTEXT_DEFAULT)
689 case CONTEXT_LAC:
690 if (set_boolean (word, value, &(((struct lac *) item)->redial)))
691 return -1;
692 break;
693 default:
694 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
695 word);
696 return -1;
698 return 0;
701 int set_accesscontrol (char *word, char *value, int context, void *item)
703 switch (context & ~CONTEXT_DEFAULT)
705 case CONTEXT_GLOBAL:
706 if (set_boolean
707 (word, value, &(((struct global *) item)->accesscontrol)))
708 return -1;
709 break;
710 default:
711 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
712 word);
713 return -1;
715 return 0;
718 int set_userspace (char *word, char *value, int context, void *item)
720 switch (context & ~CONTEXT_DEFAULT)
722 case CONTEXT_GLOBAL:
723 if (set_boolean
724 (word, value, &(((struct global *) item)->forceuserspace)))
725 return -1;
726 break;
727 default:
728 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
729 word);
730 return -1;
732 return 0;
735 int set_debugavp (char *word, char *value, int context, void *item)
737 switch (context & ~CONTEXT_DEFAULT)
739 case CONTEXT_GLOBAL:
740 if (set_boolean
741 (word, value, &(((struct global *) item)->debug_avp)))
742 return -1;
743 break;
744 default:
745 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
746 word);
747 return -1;
749 return 0;
752 int set_debugnetwork (char *word, char *value, int context, void *item)
754 switch (context & ~CONTEXT_DEFAULT)
756 case CONTEXT_GLOBAL:
757 if (set_boolean
758 (word, value, &(((struct global *) item)->debug_network)))
759 return -1;
760 break;
761 default:
762 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
763 word);
764 return -1;
766 return 0;
769 int set_debugpacket (char *word, char *value, int context, void *item)
771 switch (context & ~CONTEXT_DEFAULT)
773 case CONTEXT_GLOBAL:
774 if (set_boolean
775 (word, value, &(((struct global *) item)->packet_dump)))
776 return -1;
777 break;
778 default:
779 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
780 word);
781 return -1;
783 return 0;
786 int set_debugtunnel (char *word, char *value, int context, void *item)
788 switch (context & ~CONTEXT_DEFAULT)
790 case CONTEXT_GLOBAL:
791 if (set_boolean
792 (word, value, &(((struct global *) item)->debug_tunnel)))
793 return -1;
794 break;
795 default:
796 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
797 word);
798 return -1;
800 return 0;
803 int set_debugstate (char *word, char *value, int context, void *item)
805 switch (context & ~CONTEXT_DEFAULT)
807 case CONTEXT_GLOBAL:
808 if (set_boolean
809 (word, value, &(((struct global *) item)->debug_state)))
810 return -1;
811 break;
812 default:
813 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
814 word);
815 return -1;
817 return 0;
820 int set_assignip (char *word, char *value, int context, void *item)
822 switch (context & ~CONTEXT_DEFAULT)
824 case CONTEXT_LNS:
825 if (set_boolean (word, value, &(((struct lns *) item)->assign_ip)))
826 return -1;
827 break;
828 default:
829 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
830 word);
831 return -1;
833 return 0;
836 struct iprange *set_range (char *word, char *value, struct iprange *in)
838 char *c, *d = NULL, *e = NULL;
839 struct iprange *ipr, *p;
840 struct hostent *hp;
841 int count = 0;
842 c = strchr (value, '-');
843 if (c)
845 d = c + 1;
846 *c = 0;
847 while ((c >= value) && (*c < 33))
848 *(c--) = 0;
849 while (*d && (*d < 33))
850 d++;
852 if (!strlen (value) || (c && !strlen (d)))
854 snprintf (filerr, sizeof (filerr),
855 "format is '%s <host or ip> - <host or ip>'\n", word);
856 return NULL;
858 ipr = (struct iprange *) malloc (sizeof (struct iprange));
859 ipr->next = NULL;
860 hp = gethostbyname (value);
861 if (!hp)
863 snprintf (filerr, sizeof (filerr), "Unknown host %s\n", value);
864 free (ipr);
865 return NULL;
867 bcopy (hp->h_addr, &ipr->start, sizeof (unsigned int));
868 if (c)
870 e = d;
871 while(*e != '\0') {
872 if (*e++ == '.')
873 count++;
875 if (count != 3) {
876 char ip_hi[16];
878 strcpy(ip_hi, value);
879 e = strrchr(ip_hi, '.')+1;
880 /* Copy the last field + null terminator */
881 strcpy(e, d);
882 d = ip_hi;
884 hp = gethostbyname (d);
885 if (!hp)
887 snprintf (filerr, sizeof (filerr), "Unknown host %s\n", d);
888 free (ipr);
889 return NULL;
891 bcopy (hp->h_addr, &ipr->end, sizeof (unsigned int));
893 else
894 ipr->end = ipr->start;
895 if (ntohl (ipr->start) > ntohl (ipr->end))
897 snprintf (filerr, sizeof (filerr), "start is greater than end!\n");
898 free (ipr);
899 return NULL;
901 if (word[0] == 'n')
902 ipr->sense = SENSE_DENY;
903 else
904 ipr->sense = SENSE_ALLOW;
905 p = in;
906 if (p)
908 while (p->next)
909 p = p->next;
910 p->next = ipr;
911 return in;
913 else
914 return ipr;
917 int set_iprange (char *word, char *value, int context, void *item)
919 struct lns *lns = (struct lns *) item;
920 switch (context & ~CONTEXT_DEFAULT)
922 case CONTEXT_LNS:
923 break;
924 default:
925 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
926 word);
927 return -1;
929 lns->range = set_range (word, value, lns->range);
930 if (!lns->range)
931 return -1;
932 #ifdef DEBUG_FILE
933 l2tp_log (LOG_DEBUG, "range start = %x, end = %x, sense=%ud\n",
934 ntohl (lns->range->start), ntohl (lns->range->end), lns->range->sense);
935 #endif
936 return 0;
939 int set_lac (char *word, char *value, int context, void *item)
941 struct lns *lns = (struct lns *) item;
942 switch (context & ~CONTEXT_DEFAULT)
944 case CONTEXT_LNS:
945 break;
946 default:
947 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
948 word);
949 return -1;
951 lns->lacs = set_range (word, value, lns->lacs);
952 if (!lns->lacs)
953 return -1;
954 #ifdef DEBUG_FILE
955 l2tp_log (LOG_DEBUG, "lac start = %x, end = %x, sense=%ud\n",
956 ntohl (lns->lacs->start), ntohl (lns->lacs->end), lns->lacs->sense);
957 #endif
958 return 0;
961 int set_exclusive (char *word, char *value, int context, void *item)
963 switch (context & ~CONTEXT_DEFAULT)
965 case CONTEXT_LNS:
966 if (set_boolean (word, value, &(((struct lns *) item)->exclusive)))
967 return -1;
968 break;
969 default:
970 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
971 word);
972 return -1;
974 return 0;
977 int set_ip (char *word, char *value, unsigned int *addr)
979 struct hostent *hp;
980 hp = gethostbyname (value);
981 if (!hp)
983 snprintf (filerr, sizeof (filerr), "%s: host '%s' not found\n",
984 __FUNCTION__, value);
985 return -1;
987 bcopy (hp->h_addr, addr, sizeof (unsigned int));
988 return 0;
991 int set_listenaddr (char *word, char *value, int context, void *item)
993 switch (context & ~CONTEXT_DEFAULT)
995 case CONTEXT_GLOBAL:
996 #ifdef DEBUG_FILE
997 l2tp_log (LOG_DEBUG, "set_listenaddr: Setting listen address to %s\n",
998 value);
999 #endif
1000 if (set_ip (word, value, &(((struct global *) item)->listenaddr)))
1001 return -1;
1002 break;
1003 default:
1004 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
1005 word);
1006 return -1;
1008 return 0;
1011 int set_localaddr (char *word, char *value, int context, void *item)
1013 struct lac *l;
1014 struct lns *n;
1015 switch (context & ~CONTEXT_DEFAULT)
1017 case CONTEXT_LAC:
1018 l = (struct lac *) item;
1019 return set_ip (word, value, &(l->localaddr));
1020 case CONTEXT_LNS:
1021 n = (struct lns *) item;
1022 return set_ip (word, value, &(n->localaddr));
1023 default:
1024 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
1025 word);
1026 return -1;
1028 return 0;
1031 int set_remoteaddr (char *word, char *value, int context, void *item)
1033 struct lac *l;
1034 switch (context & ~CONTEXT_DEFAULT)
1036 case CONTEXT_LAC:
1037 l = (struct lac *) item;
1038 return set_ip (word, value, &(l->remoteaddr));
1039 default:
1040 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
1041 word);
1042 return -1;
1044 return 0;
1047 int set_lns (char *word, char *value, int context, void *item)
1049 #if 0
1050 struct hostent *hp;
1051 #endif
1052 struct lac *l;
1053 struct host *ipr, *pos;
1054 char *d;
1055 switch (context & ~CONTEXT_DEFAULT)
1057 case CONTEXT_LAC:
1058 #ifdef DEBUG_FILE
1059 l2tp_log (LOG_DEBUG, "set_lns: setting LNS to '%s'\n", value);
1060 #endif
1061 l = (struct lac *) item;
1062 d = strchr (value, ':');
1063 if (d)
1065 d[0] = 0;
1066 d++;
1068 #if 0
1069 // why would you want to lookup hostnames at this time?
1070 hp = gethostbyname (value);
1071 if (!hp)
1073 snprintf (filerr, sizeof (filerr), "no such host '%s'\n", value);
1074 return -1;
1076 #endif
1077 ipr = malloc (sizeof (struct host));
1078 ipr->next = NULL;
1079 pos = l->lns;
1080 if (!pos)
1082 l->lns = ipr;
1084 else
1086 while (pos->next)
1087 pos = pos->next;
1088 pos->next = ipr;
1090 strncpy (ipr->hostname, value, sizeof (ipr->hostname));
1091 if (d)
1092 ipr->port = atoi (d);
1093 else
1094 ipr->port = UDP_LISTEN_PORT;
1095 break;
1096 default:
1097 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
1098 word);
1099 return -1;
1101 return 0;
1104 int set_rand_sys ()
1106 l2tp_log(LOG_WARNING, "The \"rand()\" function call is not a very good source"
1107 "of randomness\n");
1108 rand_source = RAND_SYS;
1109 return 0;
1112 int set_ipsec_saref (char *word, char *value, int context, void *item)
1114 struct global *g = ((struct global *) item);
1115 switch (context & ~CONTEXT_DEFAULT)
1117 case CONTEXT_GLOBAL:
1118 if (set_boolean
1119 (word, value, &(g->ipsecsaref)))
1120 return -1;
1121 if(g->ipsecsaref) {
1122 l2tp_log(LOG_WARNING, "Enabling IPsec SAref processing for L2TP transport mode SAs\n");
1124 if(g->forceuserspace != 1) {
1125 l2tp_log(LOG_WARNING, "IPsec SAref does not work with L2TP kernel mode yet, enabling forceuserspace=yes\n");
1127 break;
1128 default:
1129 snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
1130 word);
1131 return -1;
1133 return 0;
1136 int set_rand_dev ()
1138 rand_source = RAND_DEV;
1139 return 0;
1142 int set_rand_egd (char *value)
1144 l2tp_log(LOG_WARNING, "%s: not yet implemented!\n", __FUNCTION__);
1145 rand_source = RAND_EGD;
1146 return -1;
1149 int set_rand_source (char *word, char *value, int context, void *item)
1151 time_t seconds;
1153 * We're going to go ahead and seed the rand() function with srand()
1154 * because even if we set the randomness source to dev or egd, they
1155 * can fall back to sys if they fail, so we want to make sure we at
1156 * least have *some* semblance of randomness available from the
1157 * rand() function
1160 * This is a sucky random number seed...just the result from the
1161 * time() call...but...the user requested to use the rand()
1162 * function, which is a pretty sucky source of randomness
1163 * regardless...at least we can get a almost sorta decent seed. If
1164 * you have any better suggestions for creating a seed...lemme know
1165 * :/
1167 seconds = time(NULL);
1168 srand(seconds);
1170 if (context != CONTEXT_GLOBAL)
1172 l2tp_log(LOG_WARNING, "%s: %s not valid in context %d\n",
1173 __FUNCTION__, word, context);
1174 return -1;
1176 /* WORKING HERE */
1177 if (strlen(value) == 0)
1179 snprintf(filerr, sizeof (filerr), "no randomness source specified\n");
1180 return -1;
1182 if (strncmp(value, "egd", 3) == 0)
1184 return set_rand_egd(value);
1186 else if (strncmp(value, "dev", 3) == 0)
1188 return set_rand_dev();
1190 else if (strncmp(value, "sys", 3) == 0)
1192 return set_rand_sys();
1194 else
1196 l2tp_log(LOG_WARNING, "%s: %s is not a valid randomness source\n",
1197 __FUNCTION__, value);
1198 return -1;
1203 int parse_config (FILE * f)
1205 /* Read in the configuration file handed to us */
1206 /* FIXME: I should check for incompatible options */
1207 int context = 0;
1208 char buf[STRLEN];
1209 char *s, *d, *t;
1210 int linenum = 0;
1211 int def = 0;
1212 struct keyword *kw;
1213 void *data = NULL;
1214 struct lns *tl;
1215 struct lac *tc;
1216 while (!feof (f))
1218 fgets (buf, sizeof (buf), f);
1219 if (feof (f))
1220 break;
1221 linenum++;
1222 s = buf;
1223 /* Strip comments */
1224 while (*s && *s != ';')
1225 s++;
1226 *s = 0;
1227 s = buf;
1228 if (!strlen (buf))
1229 continue;
1230 while ((*s < 33) && *s)
1231 s++; /* Skip over beginning white space */
1232 t = s + strlen (s);
1233 while ((t >= s) && (*t < 33))
1234 *(t--) = 0; /* Ditch trailing white space */
1235 if (!strlen (s))
1236 continue;
1237 if (s[0] == '[')
1239 /* We've got a context description */
1240 if (!(t = strchr (s, ']')))
1242 l2tp_log (LOG_CRIT, "parse_config: line %d: No closing bracket\n",
1243 linenum);
1244 return -1;
1246 t[0] = 0;
1247 s++;
1248 if ((d = strchr (s, ' ')))
1250 /* There's a parameter */
1251 d[0] = 0;
1252 d++;
1254 if (d && !strcasecmp (d, "default"))
1255 def = CONTEXT_DEFAULT;
1256 else
1257 def = 0;
1258 if (!strcasecmp (s, "global"))
1260 context = CONTEXT_GLOBAL;
1261 #ifdef DEBUG_FILE
1262 l2tp_log (LOG_DEBUG,
1263 "parse_config: global context descriptor %s\n",
1264 d ? d : "");
1265 #endif
1266 data = &gconfig;
1268 else if (!strcasecmp (s, "lns"))
1270 context = CONTEXT_LNS;
1271 if (def)
1273 if (!deflns)
1275 deflns = new_lns ();
1276 strncpy (deflns->entname, "default",
1277 sizeof (deflns->entname));
1279 data = deflns;
1280 continue;
1282 data = NULL;
1283 tl = lnslist;
1284 if (d)
1286 while (tl)
1288 if (!strcasecmp (d, tl->entname))
1289 break;
1290 tl = tl->next;
1292 if (tl)
1293 data = tl;
1295 if (!data)
1297 data = new_lns ();
1298 if (!data)
1299 return -1;
1300 ((struct lns *) data)->next = lnslist;
1301 lnslist = (struct lns *) data;
1303 if (d)
1304 strncpy (((struct lns *) data)->entname,
1305 d, sizeof (((struct lns *) data)->entname));
1306 #ifdef DEBUG_FILE
1307 l2tp_log (LOG_DEBUG, "parse_config: lns context descriptor %s\n",
1308 d ? d : "");
1309 #endif
1311 else if (!strcasecmp (s, "lac"))
1313 context = CONTEXT_LAC;
1314 if (def)
1316 if (!deflac)
1318 deflac = new_lac ();
1319 strncpy (deflac->entname, "default",
1320 sizeof (deflac->entname));
1322 data = deflac;
1323 continue;
1325 data = NULL;
1326 tc = laclist;
1327 if (d)
1329 while (tc)
1331 if (!strcasecmp (d, tc->entname))
1332 break;
1333 tc = tc->next;
1335 if (tc)
1336 data = tc;
1338 if (!data)
1340 data = new_lac ();
1341 if (!data)
1342 return -1;
1343 ((struct lac *) data)->next = laclist;
1344 laclist = (struct lac *) data;
1346 if (d)
1347 strncpy (((struct lac *) data)->entname,
1348 d, sizeof (((struct lac *) data)->entname));
1349 #ifdef DEBUG_FILE
1350 l2tp_log (LOG_DEBUG, "parse_config: lac context descriptor %s\n",
1351 d ? d : "");
1352 #endif
1354 else
1356 l2tp_log (LOG_WARNING,
1357 "parse_config: line %d: unknown context '%s'\n", linenum,
1359 return -1;
1362 else
1364 if (!context)
1366 l2tp_log (LOG_WARNING,
1367 "parse_config: line %d: data '%s' occurs with no context\n",
1368 linenum, s);
1369 return -1;
1371 if (!(t = strchr (s, '=')))
1373 l2tp_log (LOG_WARNING, "parse_config: line %d: no '=' in data\n",
1374 linenum);
1375 return -1;
1377 d = t;
1378 d--;
1379 t++;
1380 while ((d >= s) && (*d < 33))
1381 d--;
1382 d++;
1383 *d = 0;
1384 while (*t && (*t < 33))
1385 t++;
1386 #ifdef DEBUG_FILE
1387 l2tp_log (LOG_DEBUG, "parse_config: field is %s, value is %s\n", s, t);
1388 #endif
1389 /* Okay, bit twidling is done. Let's handle this */
1390 for (kw = words; kw->keyword; kw++)
1392 if (!strcasecmp (s, kw->keyword))
1394 if (kw->handler (s, t, context | def, data))
1396 l2tp_log (LOG_WARNING, "parse_config: line %d: %s", linenum,
1397 filerr);
1398 return -1;
1400 break;
1403 if (!kw->keyword)
1405 l2tp_log (LOG_CRIT, "parse_config: line %d: Unknown field '%s'\n",
1406 linenum, s);
1407 return -1;
1411 return 0;
1414 struct keyword words[] = {
1415 {"listen-addr", &set_listenaddr},
1416 {"port", &set_port},
1417 {"rand source", &set_rand_source},
1418 {"auth file", &set_authfile},
1419 {"exclusive", &set_exclusive},
1420 {"autodial", &set_autodial},
1421 {"redial", &set_redial},
1422 {"redial timeout", &set_rtimeout},
1423 {"lns", &set_lns},
1424 {"max redials", &set_rmax},
1425 {"access control", &set_accesscontrol},
1426 {"force userspace", &set_userspace},
1427 {"ip range", &set_iprange},
1428 {"no ip range", &set_iprange},
1429 {"debug avp", &set_debugavp},
1430 {"debug network", &set_debugnetwork},
1431 {"debug packet", &set_debugpacket},
1432 {"debug tunnel", &set_debugtunnel},
1433 {"debug state", &set_debugstate},
1434 {"ipsec saref", &set_ipsec_saref},
1435 {"lac", &set_lac},
1436 {"no lac", &set_lac},
1437 {"assign ip", &set_assignip},
1438 {"local ip", &set_localaddr},
1439 {"remote ip", &set_remoteaddr},
1440 {"defaultroute", &set_defaultroute},
1441 {"length bit", &set_lbit},
1442 {"hidden bit", &set_hbit},
1443 {"require pap", &set_papchap},
1444 {"require chap", &set_papchap},
1445 {"require authentication", &set_papchap},
1446 {"require auth", &set_papchap},
1447 {"refuse pap", &set_papchap},
1448 {"refuse chap", &set_papchap},
1449 {"refuse authentication", &set_papchap},
1450 {"refuse auth", &set_papchap},
1451 {"unix authentication", &set_passwdauth},
1452 {"unix auth", &set_passwdauth},
1453 {"name", &set_authname},
1454 {"hostname", &set_hostname},
1455 {"ppp debug", &set_debug},
1456 {"pppoptfile", &set_pppoptfile},
1457 {"call rws", &set_rws},
1458 {"tunnel rws", &set_rws},
1459 {"flow bit", &set_flow},
1460 {"challenge", &set_challenge},
1461 {"tx bps", &set_speed},
1462 {"rx bps", &set_speed},
1463 {"bps", &set_speed},
1464 {NULL, NULL}