1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2006-2007 Pierre Habouzit
34 * Copyright © 2008 Florent Bruneau
41 #include "policy_tokens.h"
46 #define DAEMON_NAME "postlicyd"
47 #define DAEMON_VERSION "0.3"
48 #define DEFAULT_PORT 10000
49 #define RUNAS_USER "nobody"
50 #define RUNAS_GROUP "nogroup"
54 typedef struct query_context_t {
56 filter_context_t context;
60 static config_t *config = NULL;
61 static bool refresh = false;
62 static PA(client_t) busy = ARRAY_INIT;
64 static void *query_starter(listener_t* server)
66 query_context_t *context = p_new(query_context_t, 1);
67 filter_context_prepare(&context->context, context);
71 static void query_stopper(void *data)
73 query_context_t **context = data;
75 filter_context_wipe(&(*context)->context);
80 static bool config_refresh(void *mconfig)
83 if (filter_running > 0) {
86 bool ret = config_reload(mconfig);
87 foreach (client_t **server, busy) {
88 client_io_ro(*server);
95 static void policy_answer(client_t *pcy, const char *message)
97 query_context_t *context = client_data(pcy);
98 const query_t* query = &context->query;
99 buffer_t *buf = client_output_buffer(pcy);
101 /* Write reply "action=ACTION [text]" */
102 buffer_addstr(buf, "action=");
103 buffer_ensure(buf, m_strlen(message) + 64);
105 ssize_t size = array_size(*buf) - array_len(*buf);
106 ssize_t format_size = query_format(array_ptr(*buf, array_len(*buf)),
107 size, message, query);
108 if (format_size == -1) {
109 buffer_addstr(buf, message);
110 } else if (format_size > size) {
111 buffer_ensure(buf, format_size + 1);
112 query_format(array_ptr(*buf, array_len(*buf)),
113 array_size(*buf) - array_len(*buf),
115 array_len(*buf) += format_size;
117 array_len(*buf) += format_size;
119 buffer_addstr(buf, "\n\n");
121 /* Finalize query. */
122 buf = client_input_buffer(pcy);
123 buffer_consume(buf, query->eoq - buf->data);
127 static const filter_t *next_filter(client_t *pcy, const filter_t *filter,
128 const query_t *query, const filter_hook_t *hook, bool *ok) {
130 query_context_t *context = client_data(pcy);
131 if (hook->counter >= 0 && hook->counter < MAX_COUNTERS && hook->cost > 0) {
132 context->context.counters[hook->counter] += hook->cost;
133 debug("request client=%s, from=<%s>, to=<%s>: added %d to counter %d (now %u)",
135 query->sender == NULL ? "undefined" : query->sender,
136 query->recipient == NULL ? "undefined" : query->recipient,
137 hook->cost, hook->counter, context->context.counters[hook->counter]);
141 warn("request client=%s, from=<%s>, to=<%s>: aborted",
143 query->sender == NULL ? "undefined" : query->sender,
144 query->recipient == NULL ? "undefined" : query->recipient);
147 } else if (hook->async) {
148 debug("request client=%s, from=<%s>, to=<%s>: "
149 "asynchronous filter from filter %s",
151 query->sender == NULL ? "undefined" : query->sender,
152 query->recipient == NULL ? "undefined" : query->recipient,
156 } else if (hook->postfix) {
157 info("request client=%s, from=<%s>, to=<%s>: "
158 "awswer %s from filter %s: \"%s\"",
160 query->sender == NULL ? "undefined" : query->sender,
161 query->recipient == NULL ? "undefined" : query->recipient,
162 htokens[hook->type], filter->name, hook->value);
163 policy_answer(pcy, hook->value);
167 debug("request client=%s, from=<%s>, to=<%s>: "
168 "awswer %s from filter %s: next filter %s",
170 query->sender == NULL ? "undefined" : query->sender,
171 query->recipient == NULL ? "undefined" : query->recipient,
172 htokens[hook->type], filter->name,
173 (array_ptr(config->filters, hook->filter_id))->name);
174 return array_ptr(config->filters, hook->filter_id);
178 static bool policy_process(client_t *pcy, const config_t *mconfig)
180 query_context_t *context = client_data(pcy);
181 const query_t* query = &context->query;
182 const filter_t *filter;
183 if (mconfig->entry_points[query->state] == -1) {
184 warn("no filter defined for current protocol_state (%d)", query->state);
187 if (context->context.current_filter != NULL) {
188 filter = context->context.current_filter;
190 filter = array_ptr(mconfig->filters, mconfig->entry_points[query->state]);
192 context->context.current_filter = NULL;
195 const filter_hook_t *hook = filter_run(filter, query, &context->context);
196 filter = next_filter(pcy, filter, query, hook, &ok);
197 if (filter == NULL) {
203 static int policy_run(client_t *pcy, void* vconfig)
205 const config_t *mconfig = vconfig;
207 array_add(busy, pcy);
211 query_context_t *context = client_data(pcy);
212 query_t *query = &context->query;
213 context->client = pcy;
215 buffer_t *buf = client_input_buffer(pcy);
216 int search_offs = MAX(0, (int)(buf->len - 1));
217 int nb = client_read(pcy);
221 if (errno == EAGAIN || errno == EINTR)
228 err("unexpected end of data");
232 if (!(eoq = strstr(buf->data + search_offs, "\n\n"))) {
236 if (!query_parse(query, buf->data)) {
239 query->eoq = eoq + strlen("\n\n");
241 /* The instance changed => reset the static context */
242 if (query->instance == NULL || strcmp(context->context.instance, query->instance) != 0) {
243 filter_context_clean(&context->context);
244 m_strcat(context->context.instance, 64, query->instance);
247 return policy_process(pcy, mconfig) ? 0 : -1;
250 static void policy_async_handler(filter_context_t *context,
251 const filter_hook_t *hook)
254 const filter_t *filter = context->current_filter;
255 query_context_t *qctx = context->data;
256 query_t *query = &qctx->query;
257 client_t *server = qctx->client;
259 context->current_filter = next_filter(server, filter, query, hook, &ok);
260 if (context->current_filter != NULL) {
261 ok = policy_process(server, config);
264 client_release(server);
266 if (refresh && filter_running == 0) {
267 config_refresh(config);
271 static int postlicyd_init(void)
273 filter_async_handler_register(policy_async_handler);
277 static void postlicyd_shutdown(void)
279 array_deep_wipe(busy, client_delete);
281 module_init(postlicyd_init);
282 module_exit(postlicyd_shutdown);
284 /* administrivia {{{ */
288 fputs("usage: "DAEMON_NAME" [options] config\n"
291 " -l <port> port to listen to\n"
292 " -p <pidfile> file to write our pid to\n"
293 " -f stay in foreground\n"
294 " -d grow logging level\n"
295 " -u unsafe mode (don't drop privileges)\n"
301 int main(int argc, char *argv[])
304 const char *pidfile = NULL;
305 bool daemonize = true;
306 int port = DEFAULT_PORT;
307 bool port_from_cli = false;
309 for (int c = 0; (c = getopt(argc, argv, "ufd" "l:p:")) >= 0; ) {
319 port_from_cli = true;
337 if (argc - optind != 1) {
342 info("starting %s v%s...", DAEMON_NAME, DAEMON_VERSION);
344 if (pidfile_open(pidfile) < 0) {
345 crit("unable to write pidfile %s", pidfile);
349 if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
350 crit("unable to drop privileges");
354 config = config_read(argv[optind]);
355 if (config == NULL) {
358 if (port_from_cli || config->port == 0) {
362 if (daemonize && daemon_detach() < 0) {
363 crit("unable to fork");
369 if (start_listener(config->port) == NULL) {
372 return server_loop(query_starter, query_stopper,
373 policy_run, config_refresh, config);