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