1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
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 */
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 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
40 #define config_param_register(Param)
42 /* Filter to execute on "CONNECT"
44 config_param_register("client_filter");
46 /* Filter to execute on "MAIL FROM"
48 config_param_register("sender_filter");
50 /* Filter to execute on "RCPT TO"
52 config_param_register("recipient_filter");
54 /* Filter to execute on "DATA"
56 config_param_register("data_filter");
58 /* Filter to execute on "END-OF-DATA"
60 config_param_register("end_of_data_filter");
62 /* Filter to execute on "ETRN"
64 config_param_register("etrn_filter");
66 /* Filter to execute on "HELO"
68 config_param_register("helo_filter");
69 config_param_register("ehlo_filter");
71 /* Filter to execute on "VRFY"
73 config_param_register("verify_filter");
75 static inline config_t *config_new(void)
77 config_t *config = p_new(config_t, 1);
81 static void config_close(config_t *config)
83 for (int i = 0 ; i < SMTP_count ; ++i) {
84 config->entry_points[i] = -1;
86 array_deep_wipe(config->filters, filter_wipe);
87 array_deep_wipe(config->params, filter_params_wipe);
90 void config_delete(config_t **config)
93 config_close(*config);
99 static bool config_second_pass(config_t *config)
102 if (config->filters.len > 0) {
103 # define QSORT_TYPE filter_t
104 # define QSORT_BASE config->filters.data
105 # define QSORT_NELT config->filters.len
106 # define QSORT_LT(a,b) strcmp(a->name, b->name) < 0
110 foreach (filter_t *filter, config->filters) {
111 if (!filter_update_references(filter, &config->filters)) {
119 if (!filter_check_safety(&config->filters)) {
124 foreach (filter_param_t *param, config->params) {
125 switch (param->type) {
126 #define CASE(Param, State) \
127 case ATK_ ## Param ## _FILTER: \
129 config->entry_points[SMTP_ ## State] \
130 = filter_find_with_name(&config->filters, param->value); \
132 CASE(CLIENT, CONNECT)
136 CASE(RECIPIENT, RCPT)
138 CASE(END_OF_DATA, END_OF_MESSAGE)
145 array_deep_wipe(config->params, filter_params_wipe);
148 syslog(LOG_ERR, "no entry point defined");
154 static bool config_load(config_t *config)
164 ssize_t key_len, value_len;
166 if (!file_map_open(&map, config->filename, false)) {
170 config_close(config);
171 filter_init(&filter);
174 #define READ_ERROR(Fmt, ...) \
176 syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, config->filename, \
177 line + 1, p - linep + 1, ##__VA_ARGS__); \
180 #define ADD_IN_BUFFER(Buffer, Len, Char) \
182 if ((Len) >= BUFSIZ - 1) { \
183 READ_ERROR("unreasonnable long line"); \
185 (Buffer)[(Len)++] = (Char); \
186 (Buffer)[(Len)] = '\0'; \
188 #define READ_NEXT(OnEOF) \
194 if (++p >= map.end) { \
198 #define READ_BLANK(OnEOF) \
200 bool in_comment = false; \
201 while (in_comment || isspace(*p) || *p == '#') { \
203 in_comment = false; \
204 } else if (*p == '#') { \
210 #define READ_TOKEN(Name, Buffer, Len) \
213 (Buffer)[0] = '\0'; \
214 if (!isalpha(*p)) { \
215 READ_ERROR("invalid %s, unexpected character '%c'", Name, *p); \
218 ADD_IN_BUFFER(Buffer, Len, *p); \
219 READ_NEXT(goto badeof); \
220 } while (isalnum(*p) || *p == '_'); \
222 #define READ_STRING(Name, Buffer, Len, OnEOF) \
225 (Buffer)[0] = '\0'; \
227 bool escaped = false; \
228 while (*p == '"') { \
229 READ_NEXT(goto badeof); \
232 READ_ERROR("string must not contain EOL"); \
233 } else if (escaped) { \
234 ADD_IN_BUFFER(Buffer, Len, *p); \
236 } else if (*p == '\\') { \
238 } else if (*p == '"') { \
239 READ_NEXT(goto badeof); \
242 ADD_IN_BUFFER(Buffer, Len, *p); \
244 READ_NEXT(goto badeof); \
246 READ_BLANK(goto badeof); \
249 READ_ERROR("%s must end with a ';'", Name); \
252 bool escaped = false; \
253 while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
255 if (*p == '\r' || *p == '\n') { \
256 READ_BLANK(goto badeof); \
258 ADD_IN_BUFFER(Buffer, Len, '\\'); \
264 } else if (*p == '\r' || *p == '\n') { \
265 READ_ERROR("%s must not contain EOL", Name); \
267 ADD_IN_BUFFER(Buffer, Len, *p); \
269 READ_NEXT(goto badeof); \
272 ADD_IN_BUFFER(Buffer, Len, '\\'); \
274 while ((Len) > 0 && isspace((Buffer)[(Len) - 1])) { \
275 (Buffer)[--(Len)] = '\0'; \
287 value[0] = key[0] = '\0';
288 value_len = key_len = 0;
291 READ_TOKEN("section name", key, key_len);
292 READ_BLANK(goto badeof);
295 READ_NEXT(goto badeof);
296 goto read_param_value;
298 READ_NEXT(goto badeof);
301 READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
305 READ_BLANK(goto badeof);
306 READ_STRING("parameter value", value, value_len, ;);
308 filter_param_t param;
309 param.type = param_tokenize(key, key_len);
310 if (param.type != ATK_UNKNOWN) {
311 param.value = p_dupstr(value, value_len);
312 param.value_len = value_len;
313 array_add(config->params, param);
319 filter_set_name(&filter, key, key_len);
320 READ_BLANK(goto badeof);
322 READ_TOKEN("filter parameter name", key, key_len);
323 READ_BLANK(goto badeof);
325 READ_ERROR("invalid character '%c', expected '='", *p);
327 READ_NEXT(goto badeof);
328 READ_BLANK(goto badeof);
329 READ_STRING("filter parameter value", value, value_len, goto badeof);
330 READ_BLANK(goto badeof);
331 if (strcmp(key, "type") == 0) {
332 if (!filter_set_type(&filter, value, value_len)) {
333 READ_ERROR("unknow filter type (%s) for filter %s",
336 } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
337 if (!filter_add_hook(&filter, key + 3, key_len - 3,
339 READ_ERROR("hook %s not supported by filter %s",
340 key + 3, filter.name);
343 /* filter_add_param failure mean unknown type or unsupported type.
344 * this are non-fatal errors.
346 (void)filter_add_param(&filter, key, key_len, value, value_len);
350 if (!filter_build(&filter)) {
351 READ_ERROR("invalid filter %s", filter.name);
353 array_add(config->filters, filter);
354 filter_init(&filter);
358 if (!config_second_pass(config)) {
361 file_map_close(&map);
365 syslog(LOG_ERR, "Unexpected end of file");
369 filter_wipe(&filter);
371 file_map_close(&map);
375 bool config_reload(config_t *config)
377 return config_load(config);
380 config_t *config_read(const char *file)
382 config_t *config = config_new();
383 config->filename = file;
384 if (!config_reload(config)) {