filter_params_t -> filter_param_t
[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 config_param_register("first_filter");
43
44 static inline config_t *config_new(void)
45 {
46     config_t *config = p_new(config_t, 1);
47     config->entry_point = -1;
48     return config;
49 }
50
51 void config_delete(config_t **config)
52 {
53     if (*config) {
54         array_deep_wipe((*config)->filters, filter_wipe);
55         array_deep_wipe((*config)->params, filter_params_wipe);
56         p_delete(config);
57     }
58 }
59
60
61 static bool config_second_pass(config_t *config)
62 {
63     bool ok = true;
64     if (config->filters.len > 0) {
65 #       define QSORT_TYPE filter_t
66 #       define QSORT_BASE config->filters.data
67 #       define QSORT_NELT config->filters.len
68 #       define QSORT_LT(a,b) strcmp(a->name, b->name) < 0
69 #       include "qsort.c"
70     }
71
72     foreach (filter_t *filter, config->filters) {
73         if (!filter_update_references(filter, &config->filters)) {
74             ok = false;
75             break;
76         }
77     }}
78
79     config->entry_point = -1;
80     foreach (filter_param_t *param, config->params) {
81         switch (param->type) {
82           case ATK_FIRST_FILTER:
83             config->entry_point = filter_find_with_name(&config->filters,
84                                                         param->value);
85             break;
86           default: break;
87         }
88     }}
89     array_deep_wipe(config->params, filter_params_wipe);
90
91     if (config->entry_point == -1) {
92         ok = false;
93         syslog(LOG_ERR, "no entry point defined");
94     }
95
96     return ok;
97 }
98
99 config_t *config_read(const char *file)
100 {
101     config_t *config;
102     filter_t filter;
103     file_map_t map;
104     const char *p;
105     int line = 0;
106     const char *linep;
107
108     char key[BUFSIZ];
109     char value[BUFSIZ];
110     ssize_t key_len, value_len;
111
112     if (!file_map_open(&map, file, false)) {
113         return false;
114     }
115
116     config = config_new();
117     filter_init(&filter);
118     linep = p = map.map;
119
120 #define READ_ERROR(Fmt, ...)                                                   \
121     do {                                                                       \
122         syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1,          \
123                p - linep + 1, ##__VA_ARGS__);                                  \
124         goto error;                                                            \
125     } while (0)
126 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
127     do {                                                                       \
128         if ((Len) >= BUFSIZ - 1) {                                             \
129             READ_ERROR("unreasonnable long line");                             \
130         }                                                                      \
131         (Buffer)[(Len)++] = (Char);                                            \
132         (Buffer)[(Len)]   = '\0';                                              \
133     } while (0)
134 #define READ_NEXT(OnEOF)                                                       \
135     do {                                                                       \
136         if (*p == '\n') {                                                      \
137             ++line;                                                            \
138             linep = p + 1;                                                     \
139         }                                                                      \
140         if (++p >= map.end) {                                                  \
141             OnEOF;                                                             \
142         }                                                                      \
143     } while (0)
144 #define READ_BLANK(OnEOF)                                                      \
145     do {                                                                       \
146         bool in_comment = false;                                               \
147         while (in_comment || isspace(*p) || *p == '#') {                       \
148             if (*p == '\n') {                                                  \
149                 in_comment = false;                                            \
150             } else if (*p == '#') {                                            \
151                 in_comment = true;                                             \
152             }                                                                  \
153             READ_NEXT(OnEOF);                                                  \
154         }                                                                      \
155     } while (0)
156 #define READ_TOKEN(Name, Buffer, Len)                                          \
157     do {                                                                       \
158         (Len) = 0;                                                             \
159         (Buffer)[0] = '\0';                                                    \
160         if (!isalpha(*p)) {                                                    \
161             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
162         }                                                                      \
163         do {                                                                   \
164             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
165             READ_NEXT(goto badeof);                                            \
166         } while (isalnum(*p) || *p == '_');                                    \
167     } while (0)
168 #define READ_STRING(Name, Buffer, Len, OnEOF)                                  \
169     do {                                                                       \
170         (Len) = 0;                                                             \
171         (Buffer)[0] = '\0';                                                    \
172         if (*p == '"') {                                                       \
173             bool escaped = false;                                              \
174             while (*p == '"') {                                                \
175                 READ_NEXT(goto badeof);                                        \
176                 while (true) {                                                 \
177                     if (*p == '\n') {                                          \
178                         READ_ERROR("string must not contain EOL");             \
179                     } else if (escaped) {                                      \
180                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
181                         escaped = false;                                       \
182                     } else if (*p == '\\') {                                   \
183                         escaped = true;                                        \
184                     } else if (*p == '"') {                                    \
185                         READ_NEXT(goto badeof);                                \
186                         break;                                                 \
187                     } else {                                                   \
188                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
189                     }                                                          \
190                     READ_NEXT(goto badeof);                                    \
191                 }                                                              \
192                 READ_BLANK(goto badeof);                                       \
193             }                                                                  \
194             if (*p != ';') {                                                   \
195                 READ_ERROR("%s must end with a ';'", Name);                    \
196             }                                                                  \
197         } else {                                                               \
198             bool escaped = false;                                              \
199             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
200                 if (escaped) {                                                 \
201                     if (*p == '\r' || *p == '\n') {                            \
202                         READ_BLANK(goto badeof);                               \
203                     } else {                                                   \
204                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
205                     }                                                          \
206                     escaped = false;                                           \
207                 }                                                              \
208                 if (*p == '\\') {                                              \
209                     escaped = true;                                            \
210                 } else if (*p == '\r' || *p == '\n') {                         \
211                     READ_ERROR("%s must not contain EOL", Name);               \
212                 } else {                                                       \
213                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
214                 }                                                              \
215                 READ_NEXT(goto badeof);                                        \
216             }                                                                  \
217             if (escaped) {                                                     \
218                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
219             }                                                                  \
220         }                                                                      \
221         READ_NEXT(OnEOF);                                                      \
222     } while(0)
223
224
225 read_section:
226     if (p >= map.end) {
227         goto ok;
228     }
229
230     value[0] = key[0] = '\0';
231     value_len = key_len = 0;
232
233     READ_BLANK(goto ok);
234     READ_TOKEN("section name", key, key_len);
235     READ_BLANK(goto badeof);
236     switch (*p) {
237       case '=':
238         READ_NEXT(goto badeof);
239         goto read_param_value;
240       case '{':
241         READ_NEXT(goto badeof);
242         goto read_filter;
243       default:
244         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
245     }
246
247 read_param_value:
248     READ_BLANK(goto badeof);
249     READ_STRING("parameter value", value, value_len, ;);
250     {
251         filter_param_t param;
252         param.type  = param_tokenize(key, key_len);
253         if (param.type != ATK_UNKNOWN) {
254             param.value = m_strdup(value);
255             array_add(config->params, param);
256         }
257     }
258     goto read_section;
259
260 read_filter:
261     filter_set_name(&filter, key, key_len);
262     READ_BLANK(goto badeof);
263     while (*p != '}') {
264         READ_TOKEN("filter parameter name", key, key_len);
265         READ_BLANK(goto badeof);
266         if (*p != '=') {
267             READ_ERROR("invalid character '%c', expected '='", *p);
268         }
269         READ_NEXT(goto badeof);
270         READ_BLANK(goto badeof);
271         READ_STRING("filter parameter value", value, value_len, goto badeof);
272         READ_BLANK(goto badeof);
273         if (strcmp(key, "type") == 0) {
274             if (!filter_set_type(&filter, value, value_len)) {
275                 READ_ERROR("unknow filter type (%s) for filter %s",
276                            value, filter.name);
277             }
278         } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
279             if (!filter_add_hook(&filter, key + 3, key_len - 3,
280                                  value, value_len)) {
281                 READ_ERROR("hook %s not supported by filter %s",
282                            key + 3, filter.name);
283             }
284         } else {
285             /* filter_add_param failure mean unknown type or unsupported type.
286              * this are non-fatal errors.
287              */
288             (void)filter_add_param(&filter, key, key_len, value, value_len);
289         }
290     }
291     READ_NEXT(;);
292     if (!filter_build(&filter)) {
293         READ_ERROR("invalid filter %s", filter.name);
294     }
295     array_add(config->filters, filter);
296     filter_init(&filter);
297     goto read_section;
298
299 ok:
300     if (!config_second_pass(config)) {
301         goto error;
302     }
303     file_map_close(&map);
304     return config;
305
306 badeof:
307     syslog(LOG_ERR, "Unexpected end of file");
308
309 error:
310     filter_wipe(&filter);
311     config_delete(&config);
312     file_map_close(&map);
313     return NULL;
314 }