X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Fconfig.c;h=0bd69e2b115c2a9742f77eb8add360d9b3191a2c;hb=9fc48ba2176a71585c13cd346968bdba499e80de;hp=2d818f22ce7b0484c6ca1fc925c465569315a703;hpb=150de5c8a15de6797f2844293891621efefff40b;p=apps%2Fpfixtools.git diff --git a/postlicyd/config.c b/postlicyd/config.c index 2d818f2..0bd69e2 100644 --- a/postlicyd/config.c +++ b/postlicyd/config.c @@ -34,19 +34,50 @@ */ #include "file.h" -#include "filter.h" #include "config.h" +#include "str.h" -struct config_t { - A(filter_t) filters; - A(filter_params_t) params; - int entry_point; -}; +#define config_param_register(Param) + +/* Filter to execute on "CONNECT" + */ +config_param_register("client_filter"); + +/* Filter to execute on "MAIL FROM" + */ +config_param_register("sender_filter"); + +/* Filter to execute on "RCPT TO" + */ +config_param_register("recipient_filter"); + +/* Filter to execute on "DATA" + */ +config_param_register("data_filter"); + +/* Filter to execute on "END-OF-DATA" + */ +config_param_register("end_of_data_filter"); + +/* Filter to execute on "ETRN" + */ +config_param_register("etrn_filter"); + +/* Filter to execute on "HELO" + */ +config_param_register("helo_filter"); +config_param_register("ehlo_filter"); + +/* Filter to execute on "VRFY" + */ +config_param_register("verify_filter"); static inline config_t *config_new(void) { config_t *config = p_new(config_t, 1); - config->entry_point = -1; + for (int i = 0 ; i < SMTP_count ; ++i) { + config->entry_points[i] = -1; + } return config; } @@ -59,6 +90,59 @@ void config_delete(config_t **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; + } + + 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; +} + config_t *config_read(const char *file) { config_t *config; @@ -77,6 +161,7 @@ config_t *config_read(const char *file) } config = config_new(); + filter_init(&filter); linep = p = map.map; #define READ_ERROR(Fmt, ...) \ @@ -209,12 +294,17 @@ read_section: read_param_value: READ_BLANK(goto badeof); READ_STRING("parameter value", value, value_len, ;); - /* TODO: Insert parameter in the configuration. - */ + { + filter_param_t param; + param.type = param_tokenize(key, key_len); + if (param.type != ATK_UNKNOWN) { + param.value = m_strdup(value); + array_add(config->params, param); + } + } goto read_section; read_filter: - filter_init(&filter); filter_set_name(&filter, key, key_len); READ_BLANK(goto badeof); while (*p != '}') { @@ -239,9 +329,10 @@ read_filter: key + 3, filter.name); } } else { - if (!filter_add_param(&filter, key, key_len, value, value_len)) { - goto error; - } + /* filter_add_param failure mean unknown type or unsupported type. + * this are non-fatal errors. + */ + (void)filter_add_param(&filter, key, key_len, value, value_len); } } READ_NEXT(;); @@ -249,15 +340,24 @@ read_filter: READ_ERROR("invalid filter %s", filter.name); } array_add(config->filters, filter); + filter_init(&filter); goto read_section; ok: + if (!config_second_pass(config)) { + goto error; + } + file_map_close(&map); return config; badeof: syslog(LOG_ERR, "Unexpected end of file"); error: + if (filter.name) { + filter_wipe(&filter); + } config_delete(&config); + file_map_close(&map); return NULL; }