X-Git-Url: http://git.madism.org/?a=blobdiff_plain;ds=sidebyside;f=postlicyd%2Fmain-postlicyd.c;h=a4241ffffb65c38806fdc1c8f4933a4467954450;hb=5fc57f7c291b99db643dc22a814087b34b9f4b59;hp=950e5818a8a83a691c2165814955f8d075eb0945;hpb=6aafe01d02acf2c2688ba7047b6f2f738afff50a;p=apps%2Fpfixtools.git diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 950e581..a4241ff 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -41,6 +41,8 @@ #include "epoll.h" #include "policy_tokens.h" #include "server.h" +#include "query.h" +#include "config.h" #define DAEMON_NAME "postlicyd" #define DEFAULT_PORT 10000 @@ -49,67 +51,6 @@ DECLARE_MAIN -enum smtp_state { - SMTP_UNKNOWN, - SMTP_CONNECT, - SMTP_EHLO, - SMTP_HELO = SMTP_EHLO, - SMTP_MAIL, - SMTP_RCPT, - SMTP_DATA, - SMTP_END_OF_MESSAGE, - SMTP_VRFY, - SMTP_ETRN, -}; - -/* \see http://www.postfix.org/SMTPD_POLICY_README.html */ -typedef struct query_t { - unsigned state : 4; - unsigned esmtp : 1; - - const char *helo_name; - const char *queue_id; - const char *sender; - const char *recipient; - const char *recipient_count; - const char *client_address; - const char *client_name; - const char *reverse_client_name; - const char *instance; - - /* postfix 2.2+ */ - const char *sasl_method; - const char *sasl_username; - const char *sasl_sender; - const char *size; - const char *ccert_subject; - const char *ccert_issuer; - const char *ccert_fingerprint; - - /* postfix 2.3+ */ - const char *encryption_protocol; - const char *encryption_cipher; - const char *encryption_keysize; - const char *etrn_domain; - - /* postfix 2.5+ */ - const char *stress; - - const char *eoq; -} query_t; - -static query_t *query_new(void) -{ - return p_new(query_t, 1); -} - -static void query_delete(query_t **query) -{ - if (*query) { - p_delete(query); - } -} - static void *query_starter(server_t* server) { return query_new(); @@ -225,25 +166,31 @@ 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 void policy_process(server_t *pcy, config_t *config) { const query_t* query = pcy->data; - if (!policy_run_filter(query, NULL, NULL)) { - policy_answer(pcy, "DUNNO"); + filter_t *filter = array_ptr(config->filters, config->entry_point); + while (true) { + filter_hook_t *hook = filter_run(filter, query); + if (hook == NULL) { + policy_answer(pcy, "DUNNO"); + return; + } else if (hook->postfix) { + policy_answer(pcy, "%s", hook->value); + return; + } 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); int nb = buffer_read(&pcy->ibuf, pcy->fd, -1); const char *eoq; - query_t* query = pcy->data; + query_t *query = pcy->data; + config_t *config = vconfig; if (nb < 0) { if (errno == EAGAIN || errno == EINTR) @@ -264,7 +211,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); + policy_process(pcy, config); return 0; } @@ -320,11 +267,20 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + config_t *config = config_read(argv[optind]); + if (config == NULL) { + return EXIT_FAILURE; + } + if (common_setup(pidfile, false, RUNAS_USER, RUNAS_GROUP, daemonize) != EXIT_SUCCESS || start_listener(port) < 0) { 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); + config_delete(&config); + return res; + } }