+ config_close(*config);
+ p_delete(config);
+ }
+}
+
+
+static bool config_second_pass(config_t *config)
+{
+ bool ok = true;
+ if (config->filters.len > 0) {
+# define QSORT_TYPE filter_t
+# define QSORT_BASE config->filters.data
+# define QSORT_NELT config->filters.len
+# define QSORT_LT(a,b) strcmp(a->name, b->name) < 0
+# include "qsort.c"
+ }
+
+ foreach (filter_t *filter, config->filters) {
+ if (!filter_update_references(filter, &config->filters)) {
+ ok = false;
+ break;
+ }
+ }}
+ if (!ok) {
+ return false;
+ }
+ if (!filter_check_safety(&config->filters)) {
+ return false;
+ }
+
+ ok = false;
+ foreach (filter_param_t *param, config->params) {
+ switch (param->type) {
+#define CASE(Param, State) \
+ case ATK_ ## Param ## _FILTER: \
+ ok = true; \
+ config->entry_points[SMTP_ ## State] \
+ = filter_find_with_name(&config->filters, param->value); \
+ break;
+ CASE(CLIENT, CONNECT)
+ CASE(EHLO, EHLO)
+ CASE(HELO, HELO)
+ CASE(SENDER, MAIL)
+ CASE(RECIPIENT, RCPT)
+ CASE(DATA, DATA)
+ CASE(END_OF_DATA, END_OF_MESSAGE)
+ CASE(VERIFY, VRFY)
+ CASE(ETRN, ETRN)
+#undef CASE
+ default: break;
+ }
+ }}
+ array_deep_wipe(config->params, filter_params_wipe);
+
+ if (!ok) {
+ syslog(LOG_ERR, "no entry point defined");
+ }
+
+ return ok;
+}
+
+static bool config_load(config_t *config)
+{
+ filter_t filter;
+ file_map_t map;
+ const char *p;
+ int line = 0;
+ const char *linep;
+ bool in_section = false;
+ bool end_of_section = false;
+
+ char key[BUFSIZ];
+ char value[BUFSIZ];
+ ssize_t key_len, value_len;
+
+ if (!file_map_open(&map, config->filename, false)) {
+ return false;
+ }
+
+ config_close(config);
+ filter_init(&filter);
+ linep = p = map.map;
+
+#define READ_LOG(Lev, Fmt, ...) \
+ syslog(LOG_ ## Lev, "config file %s:%d:%d: " Fmt, config->filename, \
+ line + 1, p - linep + 1, ##__VA_ARGS__)
+#define READ_ERROR(Fmt, ...) \
+ do { \
+ READ_LOG(ERR, Fmt, ##__VA_ARGS__); \
+ goto error; \
+ } while (0)
+#define ADD_IN_BUFFER(Buffer, Len, Char) \
+ do { \
+ if ((Len) >= BUFSIZ - 1) { \
+ READ_ERROR("unreasonnable long line"); \
+ } \
+ (Buffer)[(Len)++] = (Char); \
+ (Buffer)[(Len)] = '\0'; \
+ } while (0)
+#define READ_NEXT \
+ do { \
+ if (*p == '\n') { \
+ ++line; \
+ linep = p + 1; \
+ } \
+ if (++p >= map.end) { \
+ if (!end_of_section) { \
+ if (in_section) { \
+ goto badeof; \
+ } else { \
+ goto ok; \
+ } \
+ } \
+ } \
+ } while (0)
+#define READ_BLANK \
+ do { \
+ bool in_comment = false; \
+ while (in_comment || isspace(*p) || *p == '#') { \
+ if (*p == '\n') { \
+ in_comment = false; \
+ } else if (*p == '#') { \
+ in_comment = true; \
+ } \
+ READ_NEXT; \
+ } \
+ } while (0)
+#define READ_TOKEN(Name, Buffer, Len) \
+ do { \
+ (Len) = 0; \
+ (Buffer)[0] = '\0'; \
+ if (!isalpha(*p)) { \
+ READ_ERROR("invalid %s, unexpected character '%c'", Name, *p); \
+ } \
+ do { \
+ ADD_IN_BUFFER(Buffer, Len, *p); \
+ READ_NEXT; \
+ } while (isalnum(*p) || *p == '_'); \
+ } while (0)
+#define READ_STRING(Name, Buffer, Len, Ignore) \
+ do { \
+ (Len) = 0; \
+ (Buffer)[0] = '\0'; \
+ if (*p == '"') { \
+ bool escaped = false; \
+ while (*p == '"') { \
+ READ_NEXT; \
+ while (true) { \
+ if (*p == '\n') { \
+ READ_ERROR("string must not contain EOL"); \
+ } else if (escaped) { \
+ ADD_IN_BUFFER(Buffer, Len, *p); \
+ escaped = false; \
+ } else if (*p == '\\') { \
+ escaped = true; \
+ } else if (*p == '"') { \
+ READ_NEXT; \
+ break; \
+ } else { \
+ ADD_IN_BUFFER(Buffer, Len, *p); \
+ } \
+ READ_NEXT; \
+ } \
+ READ_BLANK; \
+ } \
+ if (*p != ';') { \
+ READ_ERROR("%s must end with a ';'", Name); \
+ } \
+ } else { \
+ bool escaped = false; \
+ while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
+ if (escaped) { \
+ if (*p == '\r' || *p == '\n') { \
+ READ_BLANK; \
+ } else { \
+ ADD_IN_BUFFER(Buffer, Len, '\\'); \
+ } \
+ escaped = false; \
+ } \
+ if (*p == '\\') { \
+ escaped = true; \
+ } else if (*p == '\r' || *p == '\n') { \
+ READ_ERROR("%s must not contain EOL", Name); \
+ } else { \
+ ADD_IN_BUFFER(Buffer, Len, *p); \
+ } \
+ READ_NEXT; \
+ } \
+ if (escaped) { \
+ ADD_IN_BUFFER(Buffer, Len, '\\'); \
+ } \
+ while ((Len) > 0 && isspace((Buffer)[(Len) - 1])) { \
+ (Buffer)[--(Len)] = '\0'; \
+ } \
+ } \
+ end_of_section = Ignore; \
+ READ_NEXT; \
+ } while(0)
+
+
+read_section:
+ if (p >= map.end) {
+ goto ok;
+ }
+
+ value[0] = key[0] = '\0';
+ value_len = key_len = 0;
+
+ in_section = end_of_section = false;
+ READ_BLANK;
+ in_section = true;
+ READ_TOKEN("section name", key, key_len);
+ READ_BLANK;
+ switch (*p) {
+ case '=':
+ READ_NEXT;
+ goto read_param_value;
+ case '{':
+ READ_NEXT;
+ goto read_filter;
+ default:
+ READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
+ }
+
+read_param_value:
+ READ_BLANK;
+ READ_STRING("parameter value", value, value_len, true);
+ {
+ filter_param_t param;
+ param.type = param_tokenize(key, key_len);
+ if (param.type != ATK_UNKNOWN) {
+ param.value = p_dupstr(value, value_len);
+ param.value_len = value_len;
+ array_add(config->params, param);
+ } else {
+ READ_LOG(INFO, "unknown parameter %.*s", key_len, key);
+ }
+ }
+ goto read_section;
+
+read_filter:
+ filter_set_name(&filter, key, key_len);
+ READ_BLANK;
+ while (*p != '}') {
+ READ_TOKEN("filter parameter name", key, key_len);
+ READ_BLANK;
+ if (*p != '=') {
+ READ_ERROR("invalid character '%c', expected '='", *p);