Cleanup.
[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     do {                                                                       \
87         syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1,          \
88                p - linep + 1, ##__VA_ARGS__);                                  \
89         goto error;                                                            \
90     } while (0)
91 #define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
92     do {                                                                       \
93         if ((Len) >= BUFSIZ - 1) {                                             \
94             READ_ERROR("unreasonnable long line");                             \
95         }                                                                      \
96         (Buffer)[(Len)++] = (Char);                                            \
97         (Buffer)[(Len)]   = '\0';                                              \
98     } while (0)
99 #define READ_NEXT(OnEOF)                                                       \
100     do {                                                                       \
101         if (*p == '\n') {                                                      \
102             ++line;                                                            \
103             linep = p + 1;                                                     \
104         }                                                                      \
105         if (++p >= map.end) {                                                  \
106             OnEOF;                                                             \
107         }                                                                      \
108     } while (0)
109 #define READ_BLANK(OnEOF)                                                      \
110     do {                                                                       \
111         bool in_comment = false;                                               \
112         while (in_comment || isspace(*p) || *p == '#') {                       \
113             if (*p == '\n') {                                                  \
114                 in_comment = false;                                            \
115             } else if (*p == '#') {                                            \
116                 in_comment = true;                                             \
117             }                                                                  \
118             READ_NEXT(OnEOF);                                                  \
119         }                                                                      \
120     } while (0)
121 #define READ_TOKEN(Name, Buffer, Len)                                          \
122     do {                                                                       \
123         (Len) = 0;                                                             \
124         (Buffer)[0] = '\0';                                                    \
125         if (!isalpha(*p)) {                                                    \
126             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
127         }                                                                      \
128         do {                                                                   \
129             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
130             READ_NEXT(goto badeof);                                            \
131         } while (isalnum(*p) || *p == '_');                                    \
132     } while (0)
133 #define READ_STRING(Name, Buffer, Len, OnEOF)                                  \
134     do {                                                                       \
135         (Len) = 0;                                                             \
136         (Buffer)[0] = '\0';                                                    \
137         if (*p == '"') {                                                       \
138             bool escaped = false;                                              \
139             while (*p == '"') {                                                \
140                 READ_NEXT(goto badeof);                                        \
141                 while (true) {                                                 \
142                     if (*p == '\n') {                                          \
143                         READ_ERROR("string must not contain EOL");             \
144                     } else if (escaped) {                                      \
145                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
146                         escaped = false;                                       \
147                     } else if (*p == '\\') {                                   \
148                         escaped = true;                                        \
149                     } else if (*p == '"') {                                    \
150                         READ_NEXT(goto badeof);                                \
151                         break;                                                 \
152                     } else {                                                   \
153                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
154                     }                                                          \
155                     READ_NEXT(goto badeof);                                    \
156                 }                                                              \
157                 READ_BLANK(goto badeof);                                       \
158             }                                                                  \
159             if (*p != ';') {                                                   \
160                 READ_ERROR("%s must end with a ';'", Name);                    \
161             }                                                                  \
162         } else {                                                               \
163             bool escaped = false;                                              \
164             while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
165                 if (escaped) {                                                 \
166                     if (*p == '\r' || *p == '\n') {                            \
167                         READ_BLANK(goto badeof);                               \
168                     } else {                                                   \
169                         ADD_IN_BUFFER(Buffer, Len, '\\');                      \
170                     }                                                          \
171                     escaped = false;                                           \
172                 }                                                              \
173                 if (*p == '\\') {                                              \
174                     escaped = true;                                            \
175                 } else if (*p == '\r' || *p == '\n') {                         \
176                     READ_ERROR("%s must not contain EOL", Name);               \
177                 } else {                                                       \
178                     ADD_IN_BUFFER(Buffer, Len, *p);                            \
179                 }                                                              \
180                 READ_NEXT(goto badeof);                                        \
181             }                                                                  \
182             if (escaped) {                                                     \
183                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
184             }                                                                  \
185         }                                                                      \
186         READ_NEXT(OnEOF);                                                      \
187     } while(0)
188
189
190 read_section:
191     if (p >= map.end) {
192         goto ok;
193     }
194
195     value[0] = key[0] = '\0';
196     value_len = key_len = 0;
197
198     READ_BLANK(goto ok);
199     READ_TOKEN("section name", key, key_len);
200     READ_BLANK(goto badeof);
201     switch (*p) {
202       case '=':
203         READ_NEXT(goto badeof);
204         goto read_param_value;
205       case '{':
206         READ_NEXT(goto badeof);
207         goto read_filter;
208       default:
209         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
210     }
211
212 read_param_value:
213     READ_BLANK(goto badeof);
214     READ_STRING("parameter value", value, value_len, ;);
215     /* TODO: Insert parameter in the configuration.
216      */
217     goto read_section;
218
219 read_filter:
220     /* TODO: Create a filter with the given name.
221      */
222     READ_BLANK(goto badeof);
223     while (*p != '}') {
224         READ_TOKEN("filter parameter name", key, key_len);
225         READ_BLANK(goto badeof);
226         if (*p != '=') {
227             READ_ERROR("invalid character '%c', expected '='", *p);
228         }
229         READ_NEXT(goto badeof);
230         READ_BLANK(goto badeof);
231         READ_STRING("filter parameter value", value, value_len, goto badeof);
232         READ_BLANK(goto badeof);
233         /* TODO: Insert parameter in the filter.
234          */
235     }
236     READ_NEXT(;);
237     /* TODO: Check the filter.
238      */
239     goto read_section;
240
241 ok:
242     return config;
243
244 badeof:
245     syslog(LOG_ERR, "Unexpected end of file");
246
247 error:
248     config_delete(&config);
249     return NULL;
250 }