X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Fmain-postlicyd.c;h=5f46e76e2377171cd1d242f6e3976fcccdb1c4f1;hb=2520de08ed80b941f28c9ccb97a3785a507a831d;hp=7d6970de1e37c482f2cb14cb511e6fcf613f3e84;hpb=9b993811fe1fd208afd954e86c0bfc81a78af04f;p=apps%2Fpfixtools.git diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 7d6970d..5f46e76 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -42,6 +42,7 @@ #include "policy_tokens.h" #include "server.h" #include "query.h" +#include "config.h" #define DAEMON_NAME "postlicyd" #define DEFAULT_PORT 10000 @@ -55,6 +56,11 @@ static void *query_starter(server_t* server) return query_new(); } +static bool config_refresh(void *config) +{ + return config_reload(config); +} + static int postfix_parsejob(query_t *query, char *p) { #define PARSE_CHECK(expr, error, ...) \ @@ -66,6 +72,7 @@ static int postfix_parsejob(query_t *query, char *p) } while (0) p_clear(query, 1); + query->state = SMTP_UNKNOWN; while (*p != '\n') { char *k, *v; int klen, vlen, vtk; @@ -86,7 +93,7 @@ static int postfix_parsejob(query_t *query, char *p) vtk = policy_tokenize(v, vlen); switch (policy_tokenize(k, klen)) { -#define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; break; +#define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; syslog(LOG_DEBUG, "%s = %s", ptokens[PTK_##up], query->low); break; CASE(HELO_NAME, helo_name); CASE(QUEUE_ID, queue_id); CASE(SENDER, sender); @@ -165,25 +172,36 @@ static void policy_answer(server_t *pcy, const char *fmt, ...) epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy); } -static bool policy_run_filter(const query_t* query, void* filter, void* conf) -{ - return false; -} - -static void policy_process(server_t *pcy) +static bool policy_process(server_t *pcy, const config_t *config) { const query_t* query = pcy->data; - if (!policy_run_filter(query, NULL, NULL)) { - policy_answer(pcy, "DUNNO"); + const filter_t *filter; + if (config->entry_points[query->state] == -1) { + syslog(LOG_WARNING, "no filter defined for current protocol_state (%d)", query->state); + return false; + } + filter = array_ptr(config->filters, config->entry_points[query->state]); + while (true) { + const filter_hook_t *hook = filter_run(filter, query); + if (hook == NULL) { + syslog(LOG_WARNING, "request aborted"); + return false; + } else if (hook->postfix) { + policy_answer(pcy, "%s", hook->value); + return true; + } else { + filter = array_ptr(config->filters, hook->filter_id); + } } } -static int policy_run(server_t *pcy, void* config) +static int policy_run(server_t *pcy, void* vconfig) { - ssize_t search_offs = MAX(0, pcy->ibuf.len - 1); + ssize_t search_offs = MAX(0, (ssize_t)(pcy->ibuf.len - 1)); int nb = buffer_read(&pcy->ibuf, pcy->fd, -1); const char *eoq; - query_t* query = pcy->data; + query_t *query = pcy->data; + const config_t *config = vconfig; if (nb < 0) { if (errno == EAGAIN || errno == EINTR) @@ -204,8 +222,7 @@ static int policy_run(server_t *pcy, void* config) return -1; query->eoq = eoq + strlen("\n\n"); epoll_modify(pcy->fd, 0, pcy); - policy_process(pcy); - return 0; + return policy_process(pcy, config) ? 0 : -1; } int start_listener(int port) @@ -234,6 +251,7 @@ int main(int argc, char *argv[]) const char *pidfile = NULL; bool daemonize = true; int port = DEFAULT_PORT; + bool port_from_cli = false; for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) { switch (c) { @@ -245,6 +263,7 @@ int main(int argc, char *argv[]) break; case 'l': port = atoi(optarg); + port_from_cli = true; break; case 'f': daemonize = false; @@ -260,11 +279,24 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + config_t *config = config_read(argv[optind]); + if (config == NULL) { + return EXIT_FAILURE; + } + if (port_from_cli || config->port == 0) { + config->port = port; + } + if (common_setup(pidfile, false, RUNAS_USER, RUNAS_GROUP, daemonize) != EXIT_SUCCESS - || start_listener(port) < 0) { + || start_listener(config->port) < 0) { + config_delete(&config); return EXIT_FAILURE; } - return server_loop(query_starter, (delete_client_t)query_delete, - policy_run, NULL); + { + int res = server_loop(query_starter, (delete_client_t)query_delete, + policy_run, config_refresh, config); + config_delete(&config); + return res; + } }