Make syslog a separate option, and do it better.
[fwd] / conf.c
1 /* -*-c-*-
2 *
3 * $Id: conf.c,v 1.5 1999/10/22 22:46:44 mdw Exp $
4 *
5 * Configuration parsing
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: conf.c,v $
32 * Revision 1.5 1999/10/22 22:46:44 mdw
33 * Improve documentation for conf_enum.
34 *
35 * Revision 1.4 1999/10/15 21:12:36 mdw
36 * Remove redundant debugging code.
37 *
38 * Revision 1.3 1999/08/19 18:32:48 mdw
39 * Improve lexical analysis. In particular, `chmod' patterns don't have to
40 * be quoted any more.
41 *
42 * Revision 1.2 1999/07/26 23:28:39 mdw
43 * Major reconstruction work for new design.
44 *
45 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
46 * Initial revision.
47 *
48 */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include "config.h"
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 #include <mLib/dstr.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
64
65 #include "conf.h"
66 #include "scan.h"
67 #include "source.h"
68 #include "target.h"
69
70 #include "exec.h"
71 #include "file.h"
72 #include "socket.h"
73
74 /*----- Source and target tables ------------------------------------------*/
75
76 static source_ops *sources[] = { &xsource_ops, &fsource_ops, &ssource_ops };
77 static target_ops *targets[] = { &xtarget_ops, &ftarget_ops, &starget_ops };
78
79 static const char *notword = 0;
80 static const char *notdelim = 0;
81
82 /*----- Main code ---------------------------------------------------------*/
83
84 /* --- @undelim@ --- *
85 *
86 * Arguments: @const char *d, dd@ = pointer to characters to escape
87 *
88 * Returns: ---
89 *
90 * Use: Modifies the tokenizer. Characters in the first list will
91 * always be considered to begin a word. Characters in the
92 * second list will always be allowed to continue a word.
93 */
94
95 void undelim(const char *d, const char *dd) { notword = d; notdelim = dd; }
96
97 /* --- @token@ --- *
98 *
99 * Arguments: @scanner *sc@ = pointer to scanner definition
100 *
101 * Returns: Type of token scanned.
102 *
103 * Use: Reads the next token from the character scanner.
104 */
105
106 int token(scanner *sc)
107 {
108 #define SELFDELIM \
109 '{': case '}': case '/': case ',': \
110 case '=': case ':': case ';': \
111 case '.': case '[': case ']'
112
113 int ch;
114
115 DRESET(&sc->d);
116
117 /* --- Main tokenization --- */
118
119 for (;;) {
120 ch = scan(sc);
121
122 /* --- Deal with pushed-back tokens --- */
123
124 if (sc->head->tok) {
125 dstr_puts(&sc->d, sc->head->tok);
126 free(sc->head->tok);
127 sc->head->tok = 0;
128 sc->t = sc->head->t;
129 goto done;
130 }
131
132 else if (isspace((unsigned char)ch))
133 ;
134 else switch (ch) {
135
136 /* --- End of file --- */
137
138 case EOF:
139 sc->t = CTOK_EOF;
140 goto done;
141
142 /* --- Comment character --- */
143
144 case '#':
145 do ch = scan(sc); while (ch != EOF && ch != '\n');
146 break;
147
148 /* --- Various self-delimiting characters --- */
149
150 case SELFDELIM:
151 if (!notword || strchr(notword, ch) == 0) {
152 dstr_putc(&sc->d, ch);
153 dstr_putz(&sc->d);
154 sc->t = ch;
155 goto done;
156 }
157
158 /* --- Bare words --- *
159 *
160 * These aren't as bare any more. You can now backslash-escape
161 * individual characters, and enclose sections in double-quotes.
162 */
163
164 default: {
165 int q = 0;
166
167 for (;;) {
168 switch (ch) {
169 case EOF:
170 goto word;
171 case '\\':
172 ch = scan(sc);
173 if (ch == EOF)
174 goto word;
175 DPUTC(&sc->d, ch);
176 break;
177 case '\"':
178 q = !q;
179 break;
180 case SELFDELIM:
181 if (q || (notdelim && strchr(notdelim, ch)))
182 goto insert;
183 goto word;
184 default:
185 if (!q && isspace((unsigned char)(ch)))
186 goto word;
187 insert:
188 DPUTC(&sc->d, ch);
189 break;
190 }
191 ch = scan(sc);
192 }
193 word:
194 unscan(sc, ch);
195 DPUTZ(&sc->d);
196 sc->t = CTOK_WORD;
197 goto done;
198 }
199 }
200 }
201
202 done:
203 return (sc->t);
204 }
205
206 /* --- @pushback@ --- *
207 *
208 * Arguments: @scanner *sc@ = pointer to scanner definition
209 *
210 * Returns: ---
211 *
212 * Use: Pushes the current token back. This is normally a precursor
213 * to pushing a new scanner source.
214 */
215
216 static void pushback(scanner *sc)
217 {
218 sc->head->tok = xstrdup(sc->d.buf);
219 sc->head->t = sc->t;
220 }
221
222 /* --- @error@ --- *
223 *
224 * Arguments: @scanner *sc@ = pointer to scanner definition
225 * @const char *msg@ = message skeleton string
226 * @...@ = extra arguments for the skeleton
227 *
228 * Returns: Doesn't
229 *
230 * Use: Reports an error at the current scanner location.
231 */
232
233 void error(scanner *sc, const char *msg, ...)
234 {
235 va_list ap;
236 va_start(ap, msg);
237 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
238 vfprintf(stderr, msg, ap);
239 fputc('\n', stderr);
240 exit(1);
241 }
242
243 /* --- @conf_enum@ --- *
244 *
245 * Arguments: @scanner *sc@ = pointer to a scanner object
246 * @const char *list@ = comma-separated things to allow
247 * @unsigned f@ = flags for the search
248 * @const char *err@ = error message if not found
249 *
250 * Returns: Index into list, zero-based, or @-1@.
251 *
252 * Use: Checks whether the current token is a string which matches
253 * one of the comma-separated items given. The return value is
254 * the index (zero-based) of the matched string in the list.
255 *
256 * The flags control the behaviour if no exact match is found.
257 * If @ENUM_ABBREV@ is set, and the current token is a left
258 * substring of exactly one of the possibilities, then that one
259 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
260 * returned; otherwise an error is reported and the program is
261 * terminated.
262 */
263
264 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
265 {
266 const char *p, *q;
267 int chosen = -1;
268 int ok;
269 int index;
270
271 /* --- Make sure it's a string --- */
272
273 if (sc->t != CTOK_WORD)
274 error(sc, "parse error, expected %s", err);
275
276 /* --- Grind through the list --- */
277
278 q = sc->d.buf;
279 ok = 1;
280 index = 0;
281 p = list;
282 for (;;) {
283 switch (*p) {
284 case 0:
285 if (ok && !*q) {
286 token(sc);
287 return (index);
288 } else if (chosen != -1) {
289 token(sc);
290 return (chosen);
291 }
292 else if (f & ENUM_NONE)
293 return (-1);
294 else
295 error(sc, "unknown %s `%s'", err, sc->d.buf);
296 break;
297 case ',':
298 if (ok && !*q) {
299 token(sc);
300 return (index);
301 }
302 ok = 1;
303 q = sc->d.buf;
304 index++;
305 break;
306 default:
307 if (!ok)
308 break;
309 if ((f & ENUM_ABBREV) && !*q) {
310 if (chosen != -1)
311 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
312 chosen = index;
313 ok = 0;
314 }
315 if (*p == *q)
316 q++;
317 else
318 ok = 0;
319 break;
320 }
321 p++;
322 }
323 }
324
325 /* --- @conf_prefix@ --- *
326 *
327 * Arguments: @scanner *sc@ = pointer to a scanner object
328 * @const char *p@ = pointer to prefix string to check
329 *
330 * Returns: Nonzero if the prefix matches.
331 *
332 * Use: If the current token is a word matching the given prefix
333 * string, then it and an optional `.' character are removed and
334 * a nonzero result is returned. Otherwise the current token is
335 * left as it is, and zero is returned.
336 *
337 * Typical options parsing code would remove an expected prefix,
338 * scan an option anyway (since qualifying prefixes are
339 * optional) and if a match is found, claim the option. If no
340 * match is found, and a prefix was stripped, then an error
341 * should be reported.
342 */
343
344 int conf_prefix(scanner *sc, const char *p)
345 {
346 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
347 token(sc);
348 if (sc->t == '.')
349 token(sc);
350 return (1);
351 }
352 return (0);
353 }
354
355 /* --- @conf_name@ --- *
356 *
357 * Arguments: @scanner *sc@ = pointer to scanner
358 * @char delim@ = delimiter character to look for
359 * @dstr *d@ = pointer to dynamic string for output
360 *
361 * Returns: ---
362 *
363 * Use: Reads in a compound name consisting of words separated by
364 * delimiters. Leading and trailing delimiters are permitted,
365 * although they'll probably cause confusion if used. The name
366 * may be enclosed in square brackets if that helps at all.
367 *
368 * Examples of compound names are filenames (delimited by `/')
369 * and IP addresses (delimited by `.').
370 */
371
372 void conf_name(scanner *sc, char delim, dstr *d)
373 {
374 unsigned f = 0;
375 enum {
376 f_ok = 1,
377 f_bra = 2
378 };
379
380 /* --- Read an optional opening bracket --- */
381
382 if (sc->t == '[') {
383 token(sc);
384 f |= f_bra;
385 }
386
387 /* --- Do the main reading sequence --- */
388
389 do {
390 if (sc->t == delim) {
391 DPUTC(d, delim);
392 f |= f_ok;
393 token(sc);
394 }
395 if (sc->t == CTOK_WORD) {
396 DPUTD(d, &sc->d);
397 f |= f_ok;
398 token(sc);
399 }
400 } while (sc->t == delim);
401
402 /* --- Check that the string was OK --- */
403
404 if (!(f & f_ok))
405 error(sc, "parse error, name expected");
406
407 /* --- Read a closing bracket --- */
408
409 if (f & f_bra) {
410 if (sc->t == ']')
411 token(sc);
412 else
413 error(sc, "parse error, missing `]'");
414 }
415 DPUTZ(d);
416 }
417
418 /* --- @conf_parse@ --- *
419 *
420 * Arguments: @scanner *sc@ = pointer to scanner definition
421 *
422 * Returns: ---
423 *
424 * Use: Parses a configuration file from the scanner.
425 */
426
427 void conf_parse(scanner *sc)
428 {
429 token(sc);
430
431 for (;;) {
432 if (sc->t == CTOK_EOF)
433 break;
434 if (sc->t != CTOK_WORD)
435 error(sc, "parse error, keyword expected");
436
437 /* --- Handle a forwarding request --- */
438
439 if (strcmp(sc->d.buf, "forward") == 0 ||
440 strcmp(sc->d.buf, "fw") == 0 ||
441 strcmp(sc->d.buf, "from") == 0) {
442 source *s;
443 target *t;
444
445 token(sc);
446
447 /* --- Read a source description --- */
448
449 {
450 source_ops **sops;
451
452 /* --- Try to find a source type which understands --- */
453
454 s = 0;
455 for (sops = sources; *sops; sops++) {
456 if ((s = (*sops)->read(sc)) != 0)
457 goto found_source;
458 }
459 error(sc, "unknown source name `%s'", sc->d.buf);
460
461 /* --- Read any source-specific options --- */
462
463 found_source:
464 if (sc->t == '{') {
465 token(sc);
466 while (sc->t == CTOK_WORD) {
467 if (!s->ops->option || !s->ops->option(s, sc)) {
468 error(sc, "unknown %s source option `%s'",
469 s->ops->name, sc->d.buf);
470 }
471 if (sc->t == ';')
472 token(sc);
473 }
474 if (sc->t != '}')
475 error(sc, "parse error, missing `}'");
476 token(sc);
477 }
478 }
479
480 /* --- Read a destination description --- */
481
482 if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "to") == 0 ||
483 strcmp(sc->d.buf, "->") == 0))
484 token(sc);
485
486 {
487 target_ops **tops;
488
489 /* --- Try to find a target which understands --- */
490
491 t = 0;
492 for (tops = targets; *tops; tops++) {
493 if ((t = (*tops)->read(sc)) != 0)
494 goto found_target;
495 }
496 error(sc, "unknown target name `%s'", sc->d.buf);
497
498 /* --- Read any target-specific options --- */
499
500 found_target:
501 if (sc->t == '{') {
502 token(sc);
503 while (sc->t == CTOK_WORD) {
504 if (!t->ops->option || !t->ops->option(t, sc)) {
505 error(sc, "unknown %s target option `%s'",
506 t->ops->name, sc->d.buf);
507 }
508 if (sc->t == ';')
509 token(sc);
510 }
511 if (sc->t != '}')
512 error(sc, "parse error, `}' expected");
513 token(sc);
514 }
515 }
516
517 /* --- Combine the source and target --- */
518
519 s->ops->attach(s, sc, t);
520 }
521
522 /* --- Include configuration from a file --- *
523 *
524 * Slightly tricky. Scan the optional semicolon from the including
525 * stream, not the included one.
526 */
527
528 else if (strcmp(sc->d.buf, "include") == 0) {
529 FILE *fp;
530 dstr d = DSTR_INIT;
531
532 token(sc);
533 conf_name(sc, '/', &d);
534 if ((fp = fopen(d.buf, "r")) == 0)
535 error(sc, "can't include `%s': %s", d.buf, strerror(errno));
536 if (sc->t == ';')
537 token(sc);
538 pushback(sc);
539 scan_push(sc, scan_file(fp, xstrdup(d.buf), SCF_FREENAME));
540 token(sc);
541 dstr_destroy(&d);
542 continue; /* Don't parse a trailing `;' */
543 }
544
545 /* --- Other configuration is handled elsewhere --- */
546
547 else {
548
549 /* --- First try among the sources --- */
550
551 {
552 source_ops **sops;
553
554 for (sops = sources; *sops; sops++) {
555 if ((*sops)->option && (*sops)->option(0, sc))
556 goto found_option;
557 }
558 }
559
560 /* --- Then try among the targets --- */
561
562 {
563 target_ops **tops;
564
565 for (tops = targets; *tops; tops++) {
566 if ((*tops)->option && (*tops)->option(0, sc))
567 goto found_option;
568 }
569 }
570
571 /* --- Nobody wants the option --- */
572
573 error(sc, "unknown global option or prefix `%s'", sc->d.buf);
574
575 found_option:;
576 }
577
578 if (sc->t == ';')
579 token(sc);
580 }
581 }
582
583 /*----- That's all, folks -------------------------------------------------*/