e82f7154 |
1 | /* -*-c-*- |
2 | * |
82793759 |
3 | * $Id: conf.c,v 1.5 1999/10/22 22:46:44 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Configuration parsing |
6 | * |
61e3dbdf |
7 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
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 $ |
82793759 |
32 | * Revision 1.5 1999/10/22 22:46:44 mdw |
33 | * Improve documentation for conf_enum. |
34 | * |
a6eddf54 |
35 | * Revision 1.4 1999/10/15 21:12:36 mdw |
36 | * Remove redundant debugging code. |
37 | * |
e73034b0 |
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 | * |
61e3dbdf |
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. |
e82f7154 |
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 | |
e82f7154 |
61 | #include <mLib/dstr.h> |
62 | #include <mLib/quis.h> |
63 | #include <mLib/report.h> |
64 | |
61e3dbdf |
65 | #include "conf.h" |
e82f7154 |
66 | #include "scan.h" |
61e3dbdf |
67 | #include "source.h" |
68 | #include "target.h" |
e82f7154 |
69 | |
61e3dbdf |
70 | #include "exec.h" |
71 | #include "file.h" |
72 | #include "socket.h" |
e82f7154 |
73 | |
61e3dbdf |
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 }; |
e82f7154 |
78 | |
e73034b0 |
79 | static const char *notword = 0; |
80 | static const char *notdelim = 0; |
81 | |
e82f7154 |
82 | /*----- Main code ---------------------------------------------------------*/ |
83 | |
e73034b0 |
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 | |
e82f7154 |
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 | |
61e3dbdf |
106 | int token(scanner *sc) |
e82f7154 |
107 | { |
61e3dbdf |
108 | #define SELFDELIM \ |
109 | '{': case '}': case '/': case ',': \ |
110 | case '=': case ':': case ';': \ |
111 | case '.': case '[': case ']' |
e82f7154 |
112 | |
113 | int ch; |
114 | |
61e3dbdf |
115 | DRESET(&sc->d); |
116 | |
117 | /* --- Main tokenization --- */ |
118 | |
e82f7154 |
119 | for (;;) { |
61e3dbdf |
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 | |
e82f7154 |
132 | else if (isspace((unsigned char)ch)) |
133 | ; |
134 | else switch (ch) { |
61e3dbdf |
135 | |
136 | /* --- End of file --- */ |
137 | |
e82f7154 |
138 | case EOF: |
61e3dbdf |
139 | sc->t = CTOK_EOF; |
140 | goto done; |
141 | |
142 | /* --- Comment character --- */ |
143 | |
e82f7154 |
144 | case '#': |
61e3dbdf |
145 | do ch = scan(sc); while (ch != EOF && ch != '\n'); |
e82f7154 |
146 | break; |
61e3dbdf |
147 | |
148 | /* --- Various self-delimiting characters --- */ |
149 | |
150 | case SELFDELIM: |
e73034b0 |
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 | } |
61e3dbdf |
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: |
e73034b0 |
181 | if (q || (notdelim && strchr(notdelim, ch))) |
61e3dbdf |
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); |
e82f7154 |
195 | DPUTZ(&sc->d); |
61e3dbdf |
196 | sc->t = CTOK_WORD; |
197 | goto done; |
198 | } |
e82f7154 |
199 | } |
200 | } |
201 | |
61e3dbdf |
202 | done: |
61e3dbdf |
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; |
e82f7154 |
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 | |
61e3dbdf |
233 | void error(scanner *sc, const char *msg, ...) |
e82f7154 |
234 | { |
235 | va_list ap; |
236 | va_start(ap, msg); |
61e3dbdf |
237 | fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line); |
e82f7154 |
238 | vfprintf(stderr, msg, ap); |
239 | fputc('\n', stderr); |
240 | exit(1); |
241 | } |
242 | |
61e3dbdf |
243 | /* --- @conf_enum@ --- * |
e82f7154 |
244 | * |
61e3dbdf |
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 |
e82f7154 |
249 | * |
61e3dbdf |
250 | * Returns: Index into list, zero-based, or @-1@. |
e82f7154 |
251 | * |
61e3dbdf |
252 | * Use: Checks whether the current token is a string which matches |
82793759 |
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. |
e82f7154 |
262 | */ |
263 | |
61e3dbdf |
264 | int conf_enum(scanner *sc, const char *list, unsigned f, const char *err) |
e82f7154 |
265 | { |
61e3dbdf |
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 | } |
e82f7154 |
323 | } |
324 | |
61e3dbdf |
325 | /* --- @conf_prefix@ --- * |
e82f7154 |
326 | * |
61e3dbdf |
327 | * Arguments: @scanner *sc@ = pointer to a scanner object |
328 | * @const char *p@ = pointer to prefix string to check |
e82f7154 |
329 | * |
61e3dbdf |
330 | * Returns: Nonzero if the prefix matches. |
e82f7154 |
331 | * |
61e3dbdf |
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. |
e82f7154 |
342 | */ |
343 | |
61e3dbdf |
344 | int conf_prefix(scanner *sc, const char *p) |
e82f7154 |
345 | { |
61e3dbdf |
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 | } |
e82f7154 |
354 | |
61e3dbdf |
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 | */ |
e82f7154 |
371 | |
61e3dbdf |
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 | }; |
e82f7154 |
379 | |
61e3dbdf |
380 | /* --- Read an optional opening bracket --- */ |
e82f7154 |
381 | |
61e3dbdf |
382 | if (sc->t == '[') { |
e82f7154 |
383 | token(sc); |
61e3dbdf |
384 | f |= f_bra; |
385 | } |
e82f7154 |
386 | |
61e3dbdf |
387 | /* --- Do the main reading sequence --- */ |
e82f7154 |
388 | |
61e3dbdf |
389 | do { |
390 | if (sc->t == delim) { |
391 | DPUTC(d, delim); |
392 | f |= f_ok; |
e82f7154 |
393 | token(sc); |
61e3dbdf |
394 | } |
395 | if (sc->t == CTOK_WORD) { |
396 | DPUTD(d, &sc->d); |
397 | f |= f_ok; |
e82f7154 |
398 | token(sc); |
399 | } |
61e3dbdf |
400 | } while (sc->t == delim); |
e82f7154 |
401 | |
61e3dbdf |
402 | /* --- Check that the string was OK --- */ |
e82f7154 |
403 | |
61e3dbdf |
404 | if (!(f & f_ok)) |
405 | error(sc, "parse error, name expected"); |
e82f7154 |
406 | |
61e3dbdf |
407 | /* --- Read a closing bracket --- */ |
e82f7154 |
408 | |
61e3dbdf |
409 | if (f & f_bra) { |
410 | if (sc->t == ']') |
411 | token(sc); |
412 | else |
413 | error(sc, "parse error, missing `]'"); |
414 | } |
415 | DPUTZ(d); |
e82f7154 |
416 | } |
417 | |
418 | /* --- @conf_parse@ --- * |
419 | * |
61e3dbdf |
420 | * Arguments: @scanner *sc@ = pointer to scanner definition |
e82f7154 |
421 | * |
422 | * Returns: --- |
423 | * |
424 | * Use: Parses a configuration file from the scanner. |
425 | */ |
426 | |
61e3dbdf |
427 | void conf_parse(scanner *sc) |
e82f7154 |
428 | { |
e82f7154 |
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 || |
61e3dbdf |
440 | strcmp(sc->d.buf, "fw") == 0 || |
441 | strcmp(sc->d.buf, "from") == 0) { |
442 | source *s; |
443 | target *t; |
e82f7154 |
444 | |
445 | token(sc); |
e82f7154 |
446 | |
61e3dbdf |
447 | /* --- Read a source description --- */ |
e82f7154 |
448 | |
61e3dbdf |
449 | { |
450 | source_ops **sops; |
e82f7154 |
451 | |
61e3dbdf |
452 | /* --- Try to find a source type which understands --- */ |
e82f7154 |
453 | |
61e3dbdf |
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 | } |
e82f7154 |
479 | |
61e3dbdf |
480 | /* --- Read a destination description --- */ |
e82f7154 |
481 | |
61e3dbdf |
482 | if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "to") == 0 || |
483 | strcmp(sc->d.buf, "->") == 0)) |
484 | token(sc); |
e82f7154 |
485 | |
486 | { |
61e3dbdf |
487 | target_ops **tops; |
e82f7154 |
488 | |
61e3dbdf |
489 | /* --- Try to find a target which understands --- */ |
e82f7154 |
490 | |
61e3dbdf |
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 | } |
e82f7154 |
515 | } |
516 | |
61e3dbdf |
517 | /* --- Combine the source and target --- */ |
e82f7154 |
518 | |
61e3dbdf |
519 | s->ops->attach(s, sc, t); |
520 | } |
e82f7154 |
521 | |
61e3dbdf |
522 | /* --- Include configuration from a file --- * |
523 | * |
524 | * Slightly tricky. Scan the optional semicolon from the including |
525 | * stream, not the included one. |
526 | */ |
e82f7154 |
527 | |
61e3dbdf |
528 | else if (strcmp(sc->d.buf, "include") == 0) { |
529 | FILE *fp; |
530 | dstr d = DSTR_INIT; |
e82f7154 |
531 | |
532 | token(sc); |
61e3dbdf |
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 == ';') |
e82f7154 |
537 | token(sc); |
61e3dbdf |
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 `;' */ |
e82f7154 |
543 | } |
544 | |
545 | /* --- Other configuration is handled elsewhere --- */ |
546 | |
61e3dbdf |
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 | } |
e82f7154 |
577 | |
578 | if (sc->t == ';') |
579 | token(sc); |
580 | } |
581 | } |
582 | |
583 | /*----- That's all, folks -------------------------------------------------*/ |