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