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