64bits fixes.
[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
40 #define config_param_register(Param)
41
42 /* Filter to execute on "CONNECT"
43  */
44 config_param_register("client_filter");
45
46 /* Filter to execute on "MAIL FROM"
47  */
48 config_param_register("sender_filter");
49
50 /* Filter to execute on "RCPT TO"
51  */
52 config_param_register("recipient_filter");
53
54 /* Filter to execute on "DATA"
55  */
56 config_param_register("data_filter");
57
58 /* Filter to execute on "END-OF-DATA"
59  */
60 config_param_register("end_of_data_filter");
61
62 /* Filter to execute on "ETRN"
63  */
64 config_param_register("etrn_filter");
65
66 /* Filter to execute on "HELO"
67  */
68 config_param_register("helo_filter");
69 config_param_register("ehlo_filter");
70
71 /* Filter to execute on "VRFY"
72  */
73 config_param_register("verify_filter");
74
75
76 /* Where to bind the server.
77  */
78 config_param_register("port");
79
80
81 static config_t *global_config = NULL;
82
83 static inline config_t *config_new(void)
84 {
85     config_t *config = p_new(config_t, 1);
86     global_config = config;
87     return config;
88 }
89
90 static void config_close(config_t *config)
91 {
92     for (int i = 0 ; i < SMTP_count ; ++i) {
93         config->entry_points[i] = -1;
94     }
95     array_deep_wipe(config->filters, filter_wipe);
96     array_deep_wipe(config->params, filter_params_wipe);
97 }
98
99 void config_delete(config_t **config)
100 {
101     if (*config) {
102         config_close(*config);
103         p_delete(config);
104         global_config = NULL;
105     }
106 }
107
108 static void config_exit()
109 {
110     if (global_config) {
111         config_delete(&global_config);
112     }
113 }
114 module_exit(config_exit);
115
116 static bool config_second_pass(config_t *config)
117 {
118     bool ok = true;
119     if (config->filters.len > 0) {
120 #       define QSORT_TYPE filter_t
121 #       define QSORT_BASE config->filters.data
122 #       define QSORT_NELT config->filters.len
123 #       define QSORT_LT(a,b) strcmp(a->name, b->name) < 0
124 #       include "qsort.c"
125     }
126
127     foreach (filter_t *filter, config->filters) {
128         if (!filter_update_references(filter, &config->filters)) {
129             ok = false;
130             break;
131         }
132     }}
133     if (!ok) {
134         return false;
135     }
136     if (!filter_check_safety(&config->filters)) {
137         return false;
138     }
139
140     ok = false;
141 #define PARSE_CHECK(Expr, Fmt, ...)                                            \
142     if (!(Expr)) {                                                             \
143         err(Fmt, ##__VA_ARGS__);                                               \
144         return false;                                                          \
145     }
146     foreach (filter_param_t *param, config->params) {
147         switch (param->type) {
148 #define   CASE(Param, State)                                                   \
149             case ATK_ ## Param ## _FILTER:                                     \
150               ok = true;                                                       \
151               config->entry_points[SMTP_ ## State]                             \
152                   = filter_find_with_name(&config->filters, param->value);     \
153               PARSE_CHECK(config->entry_points[SMTP_ ## State] >= 0,           \
154                           "invalid filter name %s", param->value);             \
155               break;
156           CASE(CLIENT,      CONNECT)
157           CASE(EHLO,        EHLO)
158           CASE(HELO,        HELO)
159           CASE(SENDER,      MAIL)
160           CASE(RECIPIENT,   RCPT)
161           CASE(DATA,        DATA)
162           CASE(END_OF_DATA, END_OF_MESSAGE)
163           CASE(VERIFY,      VRFY)
164           CASE(ETRN,        ETRN)
165 #undef    CASE
166           FILTER_PARAM_PARSE_INT(PORT, config->port);
167           default: break;
168         }
169     }}
170     array_deep_wipe(config->params, filter_params_wipe);
171
172     if (!ok) {
173         err("no entry point defined");
174     }
175
176     return ok;
177 }
178
179 static bool config_load(config_t *config)
180 {
181     filter_t filter;
182     file_map_t map;
183     const char *p;
184     int line = 0;
185     const char *linep;
186     bool in_section = false;
187     bool end_of_section = false;
188
189     char key[BUFSIZ];
190     char value[BUFSIZ];
191     int key_len, value_len;
192
193     if (!file_map_open(&map, config->filename, false)) {
194         return false;
195     }
196
197     config_close(config);
198     filter_init(&filter);
199     linep = p = map.map;
200
201 #define READ_LOG(Lev, Fmt, ...)                                                \
202     __log(LOG_ ## Lev, "config file %s:%d:%d: " Fmt, config->filename,         \
203            line + 1, (int)(p - linep + 1), ##__VA_ARGS__)
204 #define READ_ERROR(Fmt, ...)                                                   \
205     do {                                                                       \
206         READ_LOG(ERR, Fmt, ##__VA_ARGS__);                                     \
207         goto error;                                                            \
208     } while (0)
209 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
210     do {                                                                       \
211         if ((Len) >= BUFSIZ - 1) {                                             \
212             READ_ERROR("unreasonnable long line");                             \
213         }                                                                      \
214         (Buffer)[(Len)++] = (Char);                                            \
215         (Buffer)[(Len)]   = '\0';                                              \
216     } while (0)
217 #define READ_NEXT                                                              \
218     do {                                                                       \
219         if (*p == '\n') {                                                      \
220             ++line;                                                            \
221             linep = p + 1;                                                     \
222         }                                                                      \
223         if (++p >= map.end) {                                                  \
224             if (!end_of_section) {                                             \
225                 if (in_section) {                                              \
226                     goto badeof;                                               \
227                 } else {                                                       \
228                     goto ok;                                                   \
229                 }                                                              \
230             }                                                                  \
231         }                                                                      \
232     } while (0)
233 #define READ_BLANK                                                             \
234     do {                                                                       \
235         bool in_comment = false;                                               \
236         while (in_comment || isspace(*p) || *p == '#') {                       \
237             if (*p == '\n') {                                                  \
238                 in_comment = false;                                            \
239             } else if (*p == '#') {                                            \
240                 in_comment = true;                                             \
241             }                                                                  \
242             READ_NEXT;                                                         \
243         }                                                                      \
244     } while (0)
245 #define READ_TOKEN(Name, Buffer, Len)                                          \
246     do {                                                                       \
247         (Len) = 0;                                                             \
248         (Buffer)[0] = '\0';                                                    \
249         if (!isalpha(*p)) {                                                    \
250             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
251         }                                                                      \
252         do {                                                                   \
253             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
254             READ_NEXT;                                                         \
255         } while (isalnum(*p) || *p == '_');                                    \
256     } while (0)
257 #define READ_STRING(Name, Buffer, Len, Ignore)                                 \
258     do {                                                                       \
259         (Len) = 0;                                                             \
260         (Buffer)[0] = '\0';                                                    \
261         if (*p == '"') {                                                       \
262             bool escaped = false;                                              \
263             while (*p == '"') {                                                \
264                 READ_NEXT;                                                     \
265                 while (true) {                                                 \
266                     if (*p == '\n') {                                          \
267                         READ_ERROR("string must not contain EOL");             \
268                     } else if (escaped) {                                      \
269                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
270                         escaped = false;                                       \
271                     } else if (*p == '\\') {                                   \
272                         escaped = true;                                        \
273                     } else if (*p == '"') {                                    \
274                         READ_NEXT;                                \
275                         break;                                                 \
276                     } else {                                                   \
277                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
278                     }                                                          \
279                     READ_NEXT;                                                 \
280                 }                                                              \
281                 READ_BLANK;                                                    \
282             }                                                                  \
283             if (*p != ';') {                                                   \
284                 READ_ERROR("%s must end with a ';'", Name);                    \
285             }                                                                  \
286         } else {                                                               \
287             bool escaped = false;                                              \
288             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
289                 if (escaped) {                                                 \
290                     if (*p == '\r' || *p == '\n') {                            \
291                         READ_BLANK;                                            \
292                     } else {                                                   \
293                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
294                     }                                                          \
295                     escaped = false;                                           \
296                 }                                                              \
297                 if (*p == '\\') {                                              \
298                     escaped = true;                                            \
299                 } else if (*p == '\r' || *p == '\n') {                         \
300                     READ_ERROR("%s must not contain EOL", Name);               \
301                 } else {                                                       \
302                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
303                 }                                                              \
304                 READ_NEXT;                                                     \
305             }                                                                  \
306             if (escaped) {                                                     \
307                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
308             }                                                                  \
309             while ((Len) > 0 && isspace((Buffer)[(Len) - 1])) {                \
310                 (Buffer)[--(Len)] = '\0';                                      \
311             }                                                                  \
312         }                                                                      \
313         end_of_section = Ignore;                                               \
314         READ_NEXT;                                                             \
315     } while(0)
316
317
318 read_section:
319     if (p >= map.end) {
320         goto ok;
321     }
322
323     value[0] = key[0] = '\0';
324     value_len = key_len = 0;
325
326     in_section = end_of_section = false;
327     READ_BLANK;
328     in_section = true;
329     READ_TOKEN("section name", key, key_len);
330     READ_BLANK;
331     switch (*p) {
332       case '=':
333         READ_NEXT;
334         goto read_param_value;
335       case '{':
336         READ_NEXT;
337         goto read_filter;
338       default:
339         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
340     }
341
342 read_param_value:
343     READ_BLANK;
344     READ_STRING("parameter value", value, value_len, true);
345     {
346         filter_param_t param;
347         param.type  = param_tokenize(key, key_len);
348         if (param.type != ATK_UNKNOWN) {
349             param.value     = p_dupstr(value, value_len);
350             param.value_len = value_len;
351             array_add(config->params, param);
352         } else {
353             READ_LOG(INFO, "unknown parameter %.*s", key_len, key);
354         }
355     }
356     goto read_section;
357
358 read_filter:
359     filter_set_name(&filter, key, key_len);
360     READ_BLANK;
361     while (*p != '}') {
362         READ_TOKEN("filter parameter name", key, key_len);
363         READ_BLANK;
364         if (*p != '=') {
365             READ_ERROR("invalid character '%c', expected '='", *p);
366         }
367         READ_NEXT;
368         READ_BLANK;
369         READ_STRING("filter parameter value", value, value_len, false);
370         READ_BLANK;
371         if (strcmp(key, "type") == 0) {
372             if (!filter_set_type(&filter, value, value_len)) {
373                 READ_ERROR("unknow filter type (%s) for filter %s",
374                            value, filter.name);
375             }
376         } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
377             if (!filter_add_hook(&filter, key + 3, key_len - 3,
378                                  value, value_len)) {
379                 READ_ERROR("hook %s not supported by filter %s",
380                            key + 3, filter.name);
381             }
382         } else {
383             /* filter_add_param failure mean unknown type or unsupported type.
384              * this are non-fatal errors.
385              */
386             (void)filter_add_param(&filter, key, key_len, value, value_len);
387         }
388     }
389     end_of_section = true;
390     READ_NEXT;
391     if (!filter_build(&filter)) {
392         READ_ERROR("invalid filter %s", filter.name);
393     }
394     array_add(config->filters, filter);
395     filter_init(&filter);
396     goto read_section;
397
398 ok:
399     if (!config_second_pass(config)) {
400         goto error;
401     }
402     file_map_close(&map);
403     return true;
404
405 badeof:
406     err("Unexpected end of file");
407
408 error:
409     if (filter.name) {
410         filter_wipe(&filter);
411     }
412     file_map_close(&map);
413     return false;
414 }
415
416 bool config_reload(config_t *config)
417 {
418     return config_load(config);
419 }
420
421 config_t *config_read(const char *file)
422 {
423     config_t *config = config_new();
424     config->filename = file;
425     if (!config_reload(config)) {
426         config_delete(&config);
427         return NULL;
428     }
429     return config;
430 }