X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Fmain-postlicyd.c;h=a4543ec3be629b0afb575c6f8c9b962a76ac905f;hb=5c6356faa58d3109101e88ecfce207326d89ceab;hp=08c6a2b114bdeacb5bc8dd88210bf8cd0487a1b3;hpb=d622febc1fb702194f7ac3f95bfa6cba448dcc5b;p=apps%2Fpfixtools.git diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 08c6a2b..a4543ec 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -83,7 +83,10 @@ static bool config_refresh(void *mconfig) if (filter_running > 0) { return true; } + log_state = "refreshing "; + info("reloading configuration"); bool ret = config_reload(mconfig); + log_state = ""; foreach (client_t **server, busy) { client_io_ro(*server); }} @@ -100,21 +103,8 @@ static void policy_answer(client_t *pcy, const char *message) /* Write reply "action=ACTION [text]" */ buffer_addstr(buf, "action="); - buffer_ensure(buf, m_strlen(message) + 64); - - ssize_t size = array_size(*buf) - array_len(*buf); - ssize_t format_size = query_format(array_ptr(*buf, array_len(*buf)), - size, message, query); - if (format_size == -1) { + if (!query_format_buffer(buf, message, query)) { buffer_addstr(buf, message); - } else if (format_size > size) { - buffer_ensure(buf, format_size + 1); - query_format(array_ptr(*buf, array_len(*buf)), - array_size(*buf) - array_len(*buf), - message, query); - array_len(*buf) += format_size; - } else { - array_len(*buf) += format_size; } buffer_addstr(buf, "\n\n"); @@ -126,43 +116,49 @@ static void policy_answer(client_t *pcy, const char *message) static const filter_t *next_filter(client_t *pcy, const filter_t *filter, const query_t *query, const filter_hook_t *hook, bool *ok) { -#define MESSAGE_FORMAT "request client=%s from=<%s> to=<%s> at %s: " -#define MESSAGE_PARAMS query->client_name, \ - query->sender == NULL ? "undefined" : query->sender, \ - query->recipient == NULL ? "undefined" : query->recipient, \ - smtp_state_names[query->state] + char log_prefix[BUFSIZ]; + log_prefix[0] = '\0'; + +#define log_reply(Level, Msg, ...) \ + if (log_level >= LOG_ ## Level) { \ + if (log_prefix[0] == '\0') { \ + query_format(log_prefix, BUFSIZ, \ + config->log_format && config->log_format[0] ? \ + config->log_format : DEFAULT_LOG_FORMAT, query); \ + } \ + __log(LOG_ ## Level, "%s: " Msg, log_prefix, ##__VA_ARGS__); \ + } if (hook != NULL) { query_context_t *context = client_data(pcy); if (hook->counter >= 0 && hook->counter < MAX_COUNTERS && hook->cost > 0) { context->context.counters[hook->counter] += hook->cost; - debug(MESSAGE_FORMAT "added %d to counter %d (now %u)", MESSAGE_PARAMS, - hook->cost, hook->counter, context->context.counters[hook->counter]); + log_reply(DEBUG, "added %d to counter %d (now %u)", + hook->cost, hook->counter, + context->context.counters[hook->counter]); } } if (hook == NULL) { - warn(MESSAGE_FORMAT "aborted", MESSAGE_PARAMS); + log_reply(WARNING, "aborted"); *ok = false; return NULL; } else if (hook->async) { - debug(MESSAGE_FORMAT "asynchronous filter from filter %s", - MESSAGE_PARAMS, filter->name); + log_reply(WARNING, "asynchronous filter from filter %s", filter->name); *ok = true; return NULL; } else if (hook->postfix) { - info(MESSAGE_FORMAT "awswer %s from filter %s: \"%s\"", MESSAGE_PARAMS, - htokens[hook->type], filter->name, hook->value); + log_reply(INFO, "awswer %s from filter %s: \"%s\"", + htokens[hook->type], filter->name, hook->value); policy_answer(pcy, hook->value); *ok = true; return NULL; } else { - debug(MESSAGE_FORMAT "awswer %s from filter %s: next filter %s", - MESSAGE_PARAMS, htokens[hook->type], filter->name, - (array_ptr(config->filters, hook->filter_id))->name); + log_reply(DEBUG, "awswer %s from filter %s: next filter %s", + htokens[hook->type], filter->name, + (array_ptr(config->filters, hook->filter_id))->name); return array_ptr(config->filters, hook->filter_id); } -#undef MESSAGE_PARAMS -#undef MESSAGE_FORMAT +#undef log_reply } static bool policy_process(client_t *pcy, const config_t *mconfig) @@ -283,6 +279,7 @@ void usage(void) " -f stay in foreground\n" " -d grow logging level\n" " -u unsafe mode (don't drop privileges)\n" + " -c check-conf\n" , stderr); } @@ -295,8 +292,9 @@ int main(int argc, char *argv[]) bool daemonize = true; int port = DEFAULT_PORT; bool port_from_cli = false; + bool check_conf = false; - for (int c = 0; (c = getopt(argc, argv, "ufd" "l:p:")) >= 0; ) { + for (int c = 0; (c = getopt(argc, argv, "ufdc" "l:p:")) >= 0; ) { switch (c) { case 'p': pidfile = optarg; @@ -314,6 +312,11 @@ int main(int argc, char *argv[]) case 'd': ++log_level; break; + case 'c': + check_conf = true; + daemonize = false; + unsafe = true; + break; default: usage(); return EXIT_FAILURE; @@ -329,6 +332,9 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (check_conf) { + return config_check(argv[optind]) ? EXIT_SUCCESS : EXIT_FAILURE; + } info("%s v%s...", DAEMON_NAME, DAEMON_VERSION); if (pidfile_open(pidfile) < 0) {