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