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