Switch to array_t and feed the filters from config.
[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 config_t *config_read(const char *file)
63 {
64     config_t *config;
65     filter_t filter;
66     file_map_t map;
67     const char *p;
68     int line = 0;
69     const char *linep;
70
71     char key[BUFSIZ];
72     char value[BUFSIZ];
73     ssize_t key_len, value_len;
74
75     if (!file_map_open(&map, file, false)) {
76         return false;
77     }
78
79     config = config_new();
80     linep = p = map.map;
81
82 #define READ_ERROR(Fmt, ...)                                                   \
83     do {                                                                       \
84         syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1,          \
85                p - linep + 1, ##__VA_ARGS__);                                  \
86         goto error;                                                            \
87     } while (0)
88 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
89     do {                                                                       \
90         if ((Len) >= BUFSIZ - 1) {                                             \
91             READ_ERROR("unreasonnable long line");                             \
92         }                                                                      \
93         (Buffer)[(Len)++] = (Char);                                            \
94         (Buffer)[(Len)]   = '\0';                                              \
95     } while (0)
96 #define READ_NEXT(OnEOF)                                                       \
97     do {                                                                       \
98         if (*p == '\n') {                                                      \
99             ++line;                                                            \
100             linep = p + 1;                                                     \
101         }                                                                      \
102         if (++p >= map.end) {                                                  \
103             OnEOF;                                                             \
104         }                                                                      \
105     } while (0)
106 #define READ_BLANK(OnEOF)                                                      \
107     do {                                                                       \
108         bool in_comment = false;                                               \
109         while (in_comment || isspace(*p) || *p == '#') {                       \
110             if (*p == '\n') {                                                  \
111                 in_comment = false;                                            \
112             } else if (*p == '#') {                                            \
113                 in_comment = true;                                             \
114             }                                                                  \
115             READ_NEXT(OnEOF);                                                  \
116         }                                                                      \
117     } while (0)
118 #define READ_TOKEN(Name, Buffer, Len)                                          \
119     do {                                                                       \
120         (Len) = 0;                                                             \
121         (Buffer)[0] = '\0';                                                    \
122         if (!isalpha(*p)) {                                                    \
123             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
124         }                                                                      \
125         do {                                                                   \
126             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
127             READ_NEXT(goto badeof);                                            \
128         } while (isalnum(*p) || *p == '_');                                    \
129     } while (0)
130 #define READ_STRING(Name, Buffer, Len, OnEOF)                                  \
131     do {                                                                       \
132         (Len) = 0;                                                             \
133         (Buffer)[0] = '\0';                                                    \
134         if (*p == '"') {                                                       \
135             bool escaped = false;                                              \
136             while (*p == '"') {                                                \
137                 READ_NEXT(goto badeof);                                        \
138                 while (true) {                                                 \
139                     if (*p == '\n') {                                          \
140                         READ_ERROR("string must not contain EOL");             \
141                     } else if (escaped) {                                      \
142                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
143                         escaped = false;                                       \
144                     } else if (*p == '\\') {                                   \
145                         escaped = true;                                        \
146                     } else if (*p == '"') {                                    \
147                         READ_NEXT(goto badeof);                                \
148                         break;                                                 \
149                     } else {                                                   \
150                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
151                     }                                                          \
152                     READ_NEXT(goto badeof);                                    \
153                 }                                                              \
154                 READ_BLANK(goto badeof);                                       \
155             }                                                                  \
156             if (*p != ';') {                                                   \
157                 READ_ERROR("%s must end with a ';'", Name);                    \
158             }                                                                  \
159         } else {                                                               \
160             bool escaped = false;                                              \
161             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
162                 if (escaped) {                                                 \
163                     if (*p == '\r' || *p == '\n') {                            \
164                         READ_BLANK(goto badeof);                               \
165                     } else {                                                   \
166                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
167                     }                                                          \
168                     escaped = false;                                           \
169                 }                                                              \
170                 if (*p == '\\') {                                              \
171                     escaped = true;                                            \
172                 } else if (*p == '\r' || *p == '\n') {                         \
173                     READ_ERROR("%s must not contain EOL", Name);               \
174                 } else {                                                       \
175                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
176                 }                                                              \
177                 READ_NEXT(goto badeof);                                        \
178             }                                                                  \
179             if (escaped) {                                                     \
180                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
181             }                                                                  \
182         }                                                                      \
183         READ_NEXT(OnEOF);                                                      \
184     } while(0)
185
186
187 read_section:
188     if (p >= map.end) {
189         goto ok;
190     }
191
192     value[0] = key[0] = '\0';
193     value_len = key_len = 0;
194
195     READ_BLANK(goto ok);
196     READ_TOKEN("section name", key, key_len);
197     READ_BLANK(goto badeof);
198     switch (*p) {
199       case '=':
200         READ_NEXT(goto badeof);
201         goto read_param_value;
202       case '{':
203         READ_NEXT(goto badeof);
204         goto read_filter;
205       default:
206         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
207     }
208
209 read_param_value:
210     READ_BLANK(goto badeof);
211     READ_STRING("parameter value", value, value_len, ;);
212     /* TODO: Insert parameter in the configuration.
213      */
214     goto read_section;
215
216 read_filter:
217     filter_init(&filter);
218     filter_set_name(&filter, key, key_len);
219     READ_BLANK(goto badeof);
220     while (*p != '}') {
221         READ_TOKEN("filter parameter name", key, key_len);
222         READ_BLANK(goto badeof);
223         if (*p != '=') {
224             READ_ERROR("invalid character '%c', expected '='", *p);
225         }
226         READ_NEXT(goto badeof);
227         READ_BLANK(goto badeof);
228         READ_STRING("filter parameter value", value, value_len, goto badeof);
229         READ_BLANK(goto badeof);
230         if (strcmp(key, "type") == 0) {
231             if (!filter_set_type(&filter, value, value_len)) {
232                 READ_ERROR("unknow filter type (%s) for filter %s",
233                            value, filter.name);
234             }
235         } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
236             if (!filter_add_hook(&filter, key + 3, key_len - 3,
237                                  value, value_len)) {
238                 READ_ERROR("hook %s not supported by filter %s",
239                            key + 3, filter.name);
240             }
241         } else {
242             if (!filter_add_param(&filter, key, key_len, value, value_len)) {
243                 goto error;
244             }
245         }
246     }
247     READ_NEXT(;);
248     if (!filter_build(&filter)) {
249         READ_ERROR("invalid filter %s", filter.name);
250     }
251     array_add(config->filters, filter);
252     goto read_section;
253
254 ok:
255     return config;
256
257 badeof:
258     syslog(LOG_ERR, "Unexpected end of file");
259
260 error:
261     config_delete(&config);
262     return NULL;
263 }