d4fbce3f73b9683a93d68855f1b20ef391e6ce8b
[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     filter_t *filters;
42     int filters_len;
43     int filters_size;
44
45     int entry_point;
46 };
47
48 static inline config_t *config_new(void)
49 {
50     config_t *config = p_new(config_t, 1);
51     config->entry_point = -1;
52     return config;
53 }
54
55 void config_delete(config_t **config)
56 {
57     if (*config) {
58         for (int i = 0 ; i < (*config)->filters_len ; ++i) {
59             filter_wipe((*config)->filters + i);
60         }
61         p_delete(&(*config)->filters);
62     }
63 }
64
65 config_t *config_read(const char *file)
66 {
67     config_t *config;
68     //filter_t *filter = NULL;
69     file_map_t map;
70     const char *p;
71     int line = 0;
72     const char *linep;
73
74     char key[BUFSIZ];
75     char value[BUFSIZ];
76     ssize_t key_len, value_len;
77
78     if (!file_map_open(&map, file, false)) {
79         return false;
80     }
81
82     config = config_new();
83     linep = p = map.map;
84
85 #define READ_ERROR(Fmt, ...)                                                   \
86     syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1,              \
87            p - linep + 1, ##__VA_ARGS__)
88 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
89     if ((Len) >= BUFSIZ - 1) {                                                 \
90         READ_ERROR("unreasonnable long line");                                 \
91         goto error;                                                            \
92     }                                                                          \
93     (Buffer)[(Len)++] = (Char);                                                \
94     (Buffer)[(Len)]   = '\0';
95
96 #define READ_NEXT(OnEOF)                                                       \
97     if (*p == '\n') {                                                          \
98         ++line;                                                                \
99         linep = p + 1;                                                         \
100     }                                                                          \
101     if (++p >= map.end) {                                                      \
102         OnEOF;                                                                 \
103     }                                                                          \
104     syslog(LOG_ERR, "Read char '%c' at %d", *p, __LINE__);
105 #define READ_BLANK(OnEOF)                                                      \
106     do {                                                                       \
107         bool in_comment = false;                                               \
108         while (in_comment || isspace(*p) || *p == '#') {                       \
109             if (*p == '\n') {                                                  \
110                 in_comment = false;                                            \
111             } else if (*p == '#') {                                            \
112                 in_comment = true;                                             \
113             }                                                                  \
114             READ_NEXT(OnEOF);                                                  \
115         }                                                                      \
116     } while (0)
117 #define READ_TOKEN(Name, Buffer, Len)                                          \
118     do {                                                                       \
119         (Len) = 0;                                                             \
120         (Buffer)[0] = '\0';                                                    \
121         if (!isalpha(*p)) {                                                    \
122             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
123             goto error;                                                        \
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                         goto error;                                            \
142                     } else if (escaped) {                                      \
143                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
144                         escaped = false;                                       \
145                     } else if (*p == '\\') {                                   \
146                         escaped = true;                                        \
147                     } else if (*p == '"') {                                    \
148                         READ_NEXT(goto badeof);                                \
149                         break;                                                 \
150                     } else {                                                   \
151                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
152                     }                                                          \
153                     READ_NEXT(goto badeof);                                    \
154                 }                                                              \
155                 READ_BLANK(goto badeof);                                       \
156             }                                                                  \
157             if (*p != ';') {                                                   \
158                 READ_ERROR("%s must end with a ';'", Name);                    \
159                 goto error;                                                    \
160             }                                                                  \
161         } else {                                                               \
162             bool escaped = false;                                              \
163             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
164                 if (escaped) {                                                 \
165                     if (*p == '\r' || *p == '\n') {                            \
166                         READ_BLANK(goto badeof);                               \
167                     } else {                                                   \
168                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
169                     }                                                          \
170                     escaped = false;                                           \
171                 }                                                              \
172                 if (*p == '\\') {                                              \
173                     escaped = true;                                            \
174                 } else if (*p == '\r' || *p == '\n') {                         \
175                     READ_ERROR("%s must not contain EOL", Name);               \
176                 } else {                                                       \
177                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
178                 }                                                              \
179                 READ_NEXT(goto badeof);                                        \
180             }                                                                  \
181             if (escaped) {                                                     \
182                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
183             }                                                                  \
184         }                                                                      \
185         READ_NEXT(OnEOF)                                                       \
186         syslog(LOG_ERR, "string read: %s", Buffer);                            \
187     } while(0)
188
189
190 read_section:
191     syslog(LOG_ERR, "read_section");
192     if (p >= map.end) {
193         goto ok;
194     }
195
196     value[0] = key[0] = '\0';
197     value_len = key_len = 0;
198
199     READ_BLANK(goto ok);
200     READ_TOKEN("section name", key, key_len);
201     READ_BLANK(goto badeof);
202     switch (*p) {
203       case '=':
204         READ_NEXT(goto badeof)
205         goto read_param_value;
206       case '{':
207         READ_NEXT(goto badeof)
208         goto read_filter;
209       default:
210         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
211         goto error;
212     }
213
214 read_param_value:
215     syslog(LOG_ERR, "read_param_value: key=%s", key);
216     READ_BLANK(goto badeof);
217     READ_STRING("parameter value", value, value_len, ;);
218     /* TODO: Insert parameter in the configuration.
219      */
220     goto read_section;
221
222 read_filter:
223     syslog(LOG_ERR, "read_filter: key=%s", key);
224     /* TODO: Create a filter with the given name.
225      */
226     READ_BLANK(goto badeof);
227     while (*p != '}') {
228         READ_TOKEN("filter parameter name", key, key_len);
229         syslog(LOG_ERR, "read parameter: key=%s", key);
230         READ_BLANK(goto badeof);
231         if (*p != '=') {
232             READ_ERROR("invalid character '%c', expected '='", *p);
233             goto error;
234         }
235         READ_NEXT(goto badeof);
236         READ_BLANK(goto badeof);
237         READ_STRING("filter parameter value", value, value_len, goto badeof);
238         READ_BLANK(goto badeof);
239         /* TODO: Insert parameter in the filter.
240          */
241     }
242     READ_NEXT(;)
243     /* TODO: Check the filter.
244      */
245     goto read_section;
246
247 ok:
248     return config;
249
250 badeof:
251     syslog(LOG_ERR, "Unexpected end of file");
252
253 error:
254     config_delete(&config);
255     return NULL;
256 }