summaryrefslogtreecommitdiff
path: root/release/src/router/rp-l2tp/options.c
blob: 44a778399290bbe89294e10c5c6a747536ff9fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/***********************************************************************
*
* options.c
*
* Code for parsing options out of configuration file.
*
* Copyright (C) 2002 by Roaring Penguin Software Inc.
*
* This software may be distributed under the terms of the GNU General
* Public License, Version 2, or (at your option) any later version.
*
* LIC: GPL
*
***********************************************************************/

static char const RCSID[] =
"$Id: options.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";

#include "l2tp.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>

l2tp_settings Settings;

static option_handler *option_handlers = NULL;

/* Function for currently-active option context */
static int (*option_context_fn)(EventSelector *es,
				char const *name, char const *value);
static int do_load_handler(EventSelector *es,
			   l2tp_opt_descriptor *desc, char const *value);
static int set_option(EventSelector *es,
		      l2tp_opt_descriptor *desc, char const *value);

/* Global options */
static l2tp_opt_descriptor global_opts[] = {
    /*  name               type                 addr */
    { "load-handler",      OPT_TYPE_CALLFUNC,   (void *) do_load_handler },
    { "listen-port",       OPT_TYPE_PORT,       &Settings.listen_port },
    { "listen-addr",       OPT_TYPE_IPADDR,     &Settings.listen_addr },
    { NULL,                OPT_TYPE_BOOL,       NULL }
};

/**********************************************************************
* %FUNCTION: do_load_handler
* %ARGUMENTS:
*  es -- event selector
*  desc -- option descriptor
*  value -- name of handler to load
* %RETURNS:
*  0 on success, -1 on failure
* %DESCRIPTION:
*  Loads a DLL as a handler
***********************************************************************/
static int
do_load_handler(EventSelector *es,
		l2tp_opt_descriptor *desc,
		char const *value)
{
    return l2tp_load_handler(es, value);
}

/**********************************************************************
* %FUNCTION: set_option
* %ARGUMENTS:
*  es -- event selector
*  desc -- option descriptor
*  value -- value string parsed from config file
* %RETURNS:
*  -1 on error, 0 if all is OK
* %DESCRIPTION:
*  Sets an option value.
***********************************************************************/
static int
set_option(EventSelector *es,
	   l2tp_opt_descriptor *desc,
	   char const *value)
{
    long x;
    char *end;
    struct hostent *he;
    int (*fn)(EventSelector *, l2tp_opt_descriptor *, char const *);

    switch(desc->type) {
    case OPT_TYPE_BOOL:
	if (!strcasecmp(value, "true") ||
	    !strcasecmp(value, "yes") ||
	    !strcasecmp(value, "on") ||
	    !strcasecmp(value, "1")) {
	    * (int *) (desc->addr) = 1;
	    return 0;
	}
	if (!strcasecmp(value, "false") ||
	    !strcasecmp(value, "no") ||
	    !strcasecmp(value, "off") ||
	    !strcasecmp(value, "0")) {
	    * (int *) (desc->addr) = 0;
	    return 0;
	}
	l2tp_set_errmsg("Expecting boolean value, found '%s'", value);
	return -1;

    case OPT_TYPE_INT:
    case OPT_TYPE_PORT:
	x = strtol(value, &end, 0);
	if (*end) {
	    l2tp_set_errmsg("Expecting integer value, found '%s'", value);
	    return -1;
	}
	if (desc->type == OPT_TYPE_PORT) {
	    if (x < 1 || x > 65535) {
		l2tp_set_errmsg("Port values must range from 1 to 65535");
		return -1;
	    }
	}

	* (int *) desc->addr = (int) x;
	return 0;

    case OPT_TYPE_IPADDR:
	he = gethostbyname(value);
	if (!he) {
	    l2tp_set_errmsg("Could not resolve %s as IP address: %s",
		       value, strerror(errno));
	    return -1;
	}

	memcpy(desc->addr, he->h_addr, sizeof(he->h_addr));
	return 0;

    case OPT_TYPE_STRING:
	if (* (char **) desc->addr) {
	    free(* (char **) desc->addr);
	}
	* (char **) desc->addr = strdup(value);
	if (! * (char *) desc->addr) {
	    l2tp_set_errmsg("Out of memory");
	    return -1;
	}
	return 0;

    case OPT_TYPE_CALLFUNC:
	fn = (int (*)(EventSelector *, l2tp_opt_descriptor *, char const *)) desc->addr;
	return fn(es, desc, value);
    }
    l2tp_set_errmsg("Unknown value type %d", desc->type);
    return -1;
}

/**********************************************************************
* %FUNCTION: chomp_word
* %ARGUMENTS:
*  line -- the input line
*  word -- buffer for storing word
* %RETURNS:
*  Updated value of line
* %DESCRIPTION:
*  Chomps a word from line
***********************************************************************/
char const *
l2tp_chomp_word(char const *line, char *word)
{
    *word = 0;

    /* Chew up whitespace */
    while(*line && isspace(*line)) line++;

    if (*line != '"') {
	/* Not quoted string */
	while (*line && !isspace(*line)) {
	    *word++ = *line++;
	}
	*word = 0;
	return line;
    }

    /* Quoted string */
    line++;
    while(*line) {
	if (*line != '\\') {
	    if (*line == '"') {
		line++;
		*word = 0;
		return line;
	    }
	    *word++ = *line++;
	    continue;
	}
	line++;
	if (*line) *word++ = *line++;
    }
    *word = 0;
    return line;
}

/**********************************************************************
* %FUNCTION: split_line_into_words
* %ARGUMENTS:
*  line -- the input line
*  name, value -- buffers which are large enough to contain all chars in line
* %RETURNS:
*  Nothing
* %DESCRIPTION:
*  Splits line into two words.  A word is:
*    - Non-whitespace chars:  foobarbazblech_3
*    - Quoted text:           "Here is text \"with embedded quotes\""
***********************************************************************/
static void
split_line_into_words(char const *line, char *name, char *value)
{
    line = l2tp_chomp_word(line, name);
    line = l2tp_chomp_word(line, value);
}

/**********************************************************************
* %FUNCTION: parser_switch_context
* %ARGUMENTS:
*  name, value -- words read from line.  Either "global ..ignored.."
*                 or "section context"
* %RETURNS:
*  0 if context-switch proceeded OK, -1 if not.
* %DESCRIPTION:
*  Switches configuration contexts
***********************************************************************/
static int
parser_switch_context(EventSelector *es,
		      char const *name,
		      char const *value)
{
    int r;
    option_handler *handler;

    /* Switch out of old context */
    if (option_context_fn) {
	r = option_context_fn(es, "*end*", "*end*");
	option_context_fn = NULL;
	if (r < 0) return r;
    }

    if (!strcasecmp(name, "global")) {
	return 0;
    }

    /* Must be "section foo" */
    handler = option_handlers;
    while(handler) {
	if (!strcasecmp(value, handler->section)) {
	    option_context_fn = handler->process_option;
	    option_context_fn(es, "*begin*", "*begin*");
	    return 0;
	}
	handler = handler->next;
    }
    l2tp_set_errmsg("No handler for section %s", value);
    return -1;
}

/**********************************************************************
* %FUNCTION: option_set
* %ARGUMENTS:
*  es -- event selector
*  name -- name of option
*  value -- value of option
*  descriptors -- array of option descriptors for this context
* %RETURNS:
*  0 on success, -1 on failure
* %DESCRIPTION:
*  Sets an option
***********************************************************************/
int
l2tp_option_set(EventSelector *es,
		char const *name,
		char const *value,
		l2tp_opt_descriptor descriptors[])
{
    int i;

    for (i=0; descriptors[i].name; i++) {
	if (!strcasecmp(descriptors[i].name, name)) {
	    return set_option(es, &descriptors[i], value);
	}
    }
    l2tp_set_errmsg("Option %s is not known in this context",
	       name);
    return -1;
}

/**********************************************************************
* %FUNCTION: option_register_section
* %ARGUMENTS:
*  handler -- an option-handler
* %RETURNS:
*  Nothing
* %DESCRIPTION:
*  Adds handler to linked-list of sections.
***********************************************************************/
void
l2tp_option_register_section(option_handler *h)
{
    h->next = option_handlers;
    option_handlers = h;
}

/**********************************************************************
* %FUNCTION: handle_option
* %ARGUMENTS:
*  es -- event selector
*  name -- name of option
*  value -- option's value
* %RETURNS:
*  0 on success, -1 on failure
* %DESCRIPTION:
*  Handles an option
***********************************************************************/
static int
handle_option(EventSelector *es,
	      char const *name,
	      char const *value)
{
    if (option_context_fn) {
	return option_context_fn(es, name, value);
    }

    return l2tp_option_set(es, name, value, global_opts);
}

/**********************************************************************
* %FUNCTION: parse_config_file
* %ARGUMENTS:
*  es -- event selector
*  fname -- filename to parse
* %RETURNS:
*  -1 on error, 0 if all is OK
* %DESCRIPTION:
*  Parses configuration file.
***********************************************************************/
int
l2tp_parse_config_file(EventSelector *es,
		       char const *fname)
{
    char buf[512];
    char name[512];
    char value[512];
    int r = 0;
    size_t l;
    char *line;
    FILE *fp;

    /* Defaults */
    Settings.listen_port = 1701;
    Settings.listen_addr.s_addr = htonl(INADDR_ANY);

    fp = fopen(fname, "r");
    if (!fp) {
	l2tp_set_errmsg("Could not open '%s' for reading: %s",
		   fname, strerror(errno));
	return -1;
    }

    /* Start in global context */
    option_context_fn = NULL;
    while (fgets(buf, sizeof(buf), fp) != NULL) {
	l = strlen(buf);
	if (l && (buf[l] == '\n')) {
	    buf[l--] = 0;
	}

	/* Skip leading whitespace */
	line = buf;
	while(*line && isspace(*line)) line++;

	/* Ignore blank lines and comments */
	if (!*line || *line == '#') {
	    continue;
	}

	/* Split line into two words */
	split_line_into_words(line, name, value);

	/* Check for context switch */
	if (!strcasecmp(name, "global") ||
	    !strcasecmp(name, "section")) {
	    r = parser_switch_context(es, name, value);
	    if (r < 0) break;
	    continue;
	}

	r = handle_option(es, name, value);
	if (r < 0) break;
    }
    fclose(fp);
    if (r >= 0) {
	if (option_context_fn) {
	    r = option_context_fn(es, "*end*", "*end*");
	    option_context_fn = NULL;
	}
    }

    return r;
}