pfix-srsd: add a -I option
[apps/pfixtools.git] / postlicyd / config.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS   */
20 /*  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED         */
21 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE    */
22 /*  DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY         */
23 /*  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
24 /*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   */
25 /*  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)     */
26 /*  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       */
27 /*  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  */
28 /*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
29 /*  POSSIBILITY OF SUCH DAMAGE.                                               */
30 /*                                                                            */
31 /*   Copyright (c) 2006-2008 the Authors                                      */
32 /*   see AUTHORS and source files for details                                 */
33 /******************************************************************************/
34
35 /*
36  * Copyright © 2008 Florent Bruneau
37  */
38
39 #include "file.h"
40 #include "config.h"
41 #include "str.h"
42 #include "resources.h"
43
44 #define config_param_register(Param)
45
46 /* Filter to execute on "CONNECT"
47  */
48 config_param_register("client_filter");
49
50 /* Filter to execute on "MAIL FROM"
51  */
52 config_param_register("sender_filter");
53
54 /* Filter to execute on "RCPT TO"
55  */
56 config_param_register("recipient_filter");
57
58 /* Filter to execute on "DATA"
59  */
60 config_param_register("data_filter");
61
62 /* Filter to execute on "END-OF-DATA"
63  */
64 config_param_register("end_of_data_filter");
65
66 /* Filter to execute on "ETRN"
67  */
68 config_param_register("etrn_filter");
69
70 /* Filter to execute on "HELO"
71  */
72 config_param_register("helo_filter");
73 config_param_register("ehlo_filter");
74
75 /* Filter to execute on "VRFY"
76  */
77 config_param_register("verify_filter");
78
79
80 /* Where to bind the server.
81  */
82 config_param_register("port");
83
84
85 /* Format of the log message.
86  * The message exact format is $log: "reply"
87  */
88 config_param_register("log_format");
89
90
91 static config_t *global_config = NULL;
92
93 static inline config_t *config_new(void)
94 {
95     config_t *config = p_new(config_t, 1);
96     global_config = config;
97     return config;
98 }
99
100 static void config_close(config_t *config)
101 {
102     for (int i = 0 ; i < SMTP_count ; ++i) {
103         config->entry_points[i] = -1;
104     }
105     array_deep_wipe(config->filters, filter_wipe);
106     array_deep_wipe(config->params, filter_params_wipe);
107     p_delete(&config->log_format);
108 }
109
110 void config_delete(config_t **config)
111 {
112     if (*config) {
113         config_close(*config);
114         p_delete(config);
115         global_config = NULL;
116     }
117 }
118
119 static void config_exit()
120 {
121     if (global_config) {
122         config_delete(&global_config);
123     }
124 }
125 module_exit(config_exit);
126
127
128 static bool config_parse(config_t *config)
129 {
130     filter_t filter;
131     file_map_t map;
132     const char *p;
133     int line = 0;
134     const char *linep;
135     bool in_section = false;
136     bool end_of_section = false;
137
138     char key[BUFSIZ];
139     char value[BUFSIZ];
140     int key_len, value_len;
141
142     if (!file_map_open(&map, config->filename, false)) {
143         return false;
144     }
145
146     filter_init(&filter);
147     linep = p = map.map;
148
149 #define READ_LOG(Lev, Fmt, ...)                                                \
150     __log(LOG_ ## Lev, "config file %s:%d:%d: " Fmt, config->filename,         \
151            line + 1, (int)(p - linep + 1), ##__VA_ARGS__)
152 #define READ_ERROR(Fmt, ...)                                                   \
153     do {                                                                       \
154         READ_LOG(ERR, Fmt, ##__VA_ARGS__);                                     \
155         goto error;                                                            \
156     } while (0)
157 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
158     do {                                                                       \
159         if ((Len) >= BUFSIZ - 1) {                                             \
160             READ_ERROR("unreasonnable long line");                             \
161         }                                                                      \
162         (Buffer)[(Len)++] = (Char);                                            \
163         (Buffer)[(Len)]   = '\0';                                              \
164     } while (0)
165 #define READ_NEXT                                                              \
166     do {                                                                       \
167         if (*p == '\n') {                                                      \
168             ++line;                                                            \
169             linep = p + 1;                                                     \
170         }                                                                      \
171         if (++p >= map.end) {                                                  \
172             if (!end_of_section) {                                             \
173                 if (in_section) {                                              \
174                     goto badeof;                                               \
175                 } else {                                                       \
176                     goto ok;                                                   \
177                 }                                                              \
178             }                                                                  \
179         }                                                                      \
180     } while (0)
181 #define READ_BLANK                                                             \
182     do {                                                                       \
183         bool in_comment = false;                                               \
184         while (in_comment || isspace(*p) || *p == '#') {                       \
185             if (*p == '\n') {                                                  \
186                 in_comment = false;                                            \
187             } else if (*p == '#') {                                            \
188                 in_comment = true;                                             \
189             }                                                                  \
190             READ_NEXT;                                                         \
191         }                                                                      \
192     } while (0)
193 #define READ_TOKEN(Name, Buffer, Len)                                          \
194     do {                                                                       \
195         (Len) = 0;                                                             \
196         (Buffer)[0] = '\0';                                                    \
197         if (!isalpha(*p)) {                                                    \
198             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
199         }                                                                      \
200         do {                                                                   \
201             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
202             READ_NEXT;                                                         \
203         } while (isalnum(*p) || *p == '_');                                    \
204     } while (0)
205 #define READ_STRING(Name, Buffer, Len, Ignore)                                 \
206     do {                                                                       \
207         (Len) = 0;                                                             \
208         (Buffer)[0] = '\0';                                                    \
209         if (*p == '"') {                                                       \
210             bool escaped = false;                                              \
211             while (*p == '"') {                                                \
212                 READ_NEXT;                                                     \
213                 while (true) {                                                 \
214                     if (*p == '\n') {                                          \
215                         READ_ERROR("string must not contain EOL");             \
216                     } else if (escaped) {                                      \
217                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
218                         escaped = false;                                       \
219                     } else if (*p == '\\') {                                   \
220                         escaped = true;                                        \
221                     } else if (*p == '"') {                                    \
222                         READ_NEXT;                                \
223                         break;                                                 \
224                     } else {                                                   \
225                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
226                     }                                                          \
227                     READ_NEXT;                                                 \
228                 }                                                              \
229                 READ_BLANK;                                                    \
230             }                                                                  \
231             if (*p != ';') {                                                   \
232                 READ_ERROR("%s must end with a ';'", Name);                    \
233             }                                                                  \
234         } else {                                                               \
235             bool escaped = false;                                              \
236             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
237                 if (escaped) {                                                 \
238                     if (*p == '\r' || *p == '\n') {                            \
239                         READ_BLANK;                                            \
240                     } else {                                                   \
241                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
242                     }                                                          \
243                     escaped = false;                                           \
244                 }                                                              \
245                 if (*p == '\\') {                                              \
246                     escaped = true;                                            \
247                 } else if (*p == '\r' || *p == '\n') {                         \
248                     READ_ERROR("%s must not contain EOL", Name);               \
249                 } else {                                                       \
250                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
251                 }                                                              \
252                 READ_NEXT;                                                     \
253             }                                                                  \
254             if (escaped) {                                                     \
255                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
256             }                                                                  \
257             while ((Len) > 0 && isspace((Buffer)[(Len) - 1])) {                \
258                 (Buffer)[--(Len)] = '\0';                                      \
259             }                                                                  \
260         }                                                                      \
261         end_of_section = Ignore;                                               \
262         READ_NEXT;                                                             \
263     } while(0)
264
265
266 read_section:
267     if (p >= map.end) {
268         goto ok;
269     }
270
271     value[0] = key[0] = '\0';
272     value_len = key_len = 0;
273
274     in_section = end_of_section = false;
275     READ_BLANK;
276     in_section = true;
277     READ_TOKEN("section name", key, key_len);
278     READ_BLANK;
279     switch (*p) {
280       case '=':
281         READ_NEXT;
282         goto read_param_value;
283       case '{':
284         READ_NEXT;
285         goto read_filter;
286       default:
287         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
288     }
289
290 read_param_value:
291     READ_BLANK;
292     READ_STRING("parameter value", value, value_len, true);
293     {
294         filter_param_t param;
295         param.type  = param_tokenize(key, key_len);
296         if (param.type != ATK_UNKNOWN) {
297             param.value     = p_dupstr(value, value_len);
298             param.value_len = value_len;
299             array_add(config->params, param);
300         } else {
301             READ_LOG(INFO, "unknown parameter %.*s", key_len, key);
302         }
303     }
304     goto read_section;
305
306 read_filter:
307     filter_set_name(&filter, key, key_len);
308     READ_BLANK;
309     while (*p != '}') {
310         READ_TOKEN("filter parameter name", key, key_len);
311         READ_BLANK;
312         if (*p != '=') {
313             READ_ERROR("invalid character '%c', expected '='", *p);
314         }
315         READ_NEXT;
316         READ_BLANK;
317         READ_STRING("filter parameter value", value, value_len, false);
318         READ_BLANK;
319         if (strcmp(key, "type") == 0) {
320             if (!filter_set_type(&filter, value, value_len)) {
321                 READ_ERROR("unknow filter type (%s) for filter %s",
322                            value, filter.name);
323             }
324         } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
325             if (!filter_add_hook(&filter, key + 3, key_len - 3,
326                                  value, value_len)) {
327                 READ_ERROR("hook %s not supported by filter %s",
328                            key + 3, filter.name);
329             }
330         } else {
331             /* filter_add_param failure mean unknown type or unsupported type.
332              * this are non-fatal errors.
333              */
334             (void)filter_add_param(&filter, key, key_len, value, value_len);
335         }
336     }
337     end_of_section = true;
338     READ_NEXT;
339     array_add(config->filters, filter);
340     filter_init(&filter);
341     goto read_section;
342
343 ok:
344     file_map_close(&map);
345     return true;
346
347 badeof:
348     err("Unexpected end of file");
349
350 error:
351     if (filter.name) {
352         filter_wipe(&filter);
353     }
354     file_map_close(&map);
355     return false;
356 }
357
358 static bool config_build_structure(config_t *config)
359 {
360     bool ok = true;
361     if (config->filters.len > 0) {
362 #       define QSORT_TYPE filter_t
363 #       define QSORT_BASE config->filters.data
364 #       define QSORT_NELT config->filters.len
365 #       define QSORT_LT(a,b) strcmp(a->name, b->name) < 0
366 #       include "qsort.c"
367     }
368
369     foreach (filter_t *filter, config->filters) {
370         if (!filter_update_references(filter, &config->filters)) {
371             ok = false;
372             break;
373         }
374     }}
375     if (!ok) {
376         return false;
377     }
378     if (!filter_check_safety(&config->filters)) {
379         return false;
380     }
381
382     ok = false;
383 #define PARSE_CHECK(Expr, Fmt, ...)                                            \
384     if (!(Expr)) {                                                             \
385         err(Fmt, ##__VA_ARGS__);                                               \
386         return false;                                                          \
387     }
388     foreach (filter_param_t *param, config->params) {
389         switch (param->type) {
390 #define   CASE(Param, State)                                                   \
391             case ATK_ ## Param ## _FILTER:                                     \
392               ok = true;                                                       \
393               config->entry_points[SMTP_ ## State]                             \
394                   = filter_find_with_name(&config->filters, param->value);     \
395               PARSE_CHECK(config->entry_points[SMTP_ ## State] >= 0,           \
396                           "invalid filter name %s", param->value);             \
397               break;
398           CASE(CLIENT,      CONNECT)
399           CASE(EHLO,        EHLO)
400           CASE(HELO,        HELO)
401           CASE(SENDER,      MAIL)
402           CASE(RECIPIENT,   RCPT)
403           CASE(DATA,        DATA)
404           CASE(END_OF_DATA, END_OF_MESSAGE)
405           CASE(VERIFY,      VRFY)
406           CASE(ETRN,        ETRN)
407 #undef    CASE
408           FILTER_PARAM_PARSE_INT(PORT, config->port);
409           FILTER_PARAM_PARSE_STRING(LOG_FORMAT, config->log_format, true);
410           default: break;
411         }
412     }}
413     array_deep_wipe(config->params, filter_params_wipe);
414
415     if (config->log_format && !query_format_check(config->log_format)) {
416         err("invalid log format: \"%s\"", config->log_format);
417         return false;
418     }
419
420     if (!ok) {
421         err("no entry point defined");
422     }
423     return ok;
424 }
425
426 static bool config_build_filters(config_t *config)
427 {
428     foreach (filter_t *filter, config->filters) {
429         if (!filter_build(filter)) {
430             return false;
431         }
432     }}
433
434     return true;
435 }
436
437 static bool config_load(config_t *config) {
438     config_close(config);
439
440     if (!config_parse(config)) {
441         err("Invalid configuration: cannot parse configuration file \"%s\"", config->filename);
442         return false;
443     }
444     if (!config_build_structure(config)) {
445         err("Invalid configuration: inconsistent filter structure");
446         return false;
447     }
448     if (!config_build_filters(config)) {
449         err("Invalid configuration: invalid filter");
450         return false;
451     }
452
453     resource_garbage_collect();
454     return true;
455 }
456
457 bool config_reload(config_t *config)
458 {
459     return config_load(config);
460 }
461
462 config_t *config_read(const char *file)
463 {
464     config_t *config = config_new();
465     config->filename = file;
466     if (!config_reload(config)) {
467         config_delete(&config);
468         return NULL;
469     }
470     return config;
471 }
472
473 bool config_check(const char *file)
474 {
475     config_t *config = config_new();
476     config->filename = file;
477
478     bool ret = config_parse(config) && config_build_structure(config);
479
480     config_delete(&config);
481     return ret;
482 }