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
42 #include "policy_tokens.h"
45 #include "postlicyd.h"
47 #define DAEMON_NAME "postlicyd"
48 #define DAEMON_VERSION "0.3"
49 #define DEFAULT_PORT 10000
50 #define RUNAS_USER "nobody"
51 #define RUNAS_GROUP "nogroup"
55 static config_t *config = NULL;
58 static void *query_starter(server_t* server)
60 query_context_t *context = p_new(query_context_t, 1);
61 filter_context_prepare(&context->context, context);
65 static void query_stopper(void *data)
67 query_context_t **context = data;
69 filter_context_wipe(&(*context)->context);
74 static bool config_refresh(void *mconfig)
76 if (filter_running > 0) {
81 return config_reload(mconfig);
84 static void policy_answer(server_t *pcy, const char *message)
86 query_context_t *context = pcy->data;
87 const query_t* query = &context->query;
89 buffer_addstr(&pcy->obuf, "action=");
90 buffer_ensure(&pcy->obuf, m_strlen(message) + 64);
92 ssize_t size = array_size(pcy->obuf) - array_len(pcy->obuf);
93 ssize_t format_size = query_format(array_ptr(pcy->obuf, array_len(pcy->obuf)),
94 size, message, query);
95 if (format_size == -1) {
96 buffer_addstr(&pcy->obuf, message);
97 } else if (format_size > size) {
98 buffer_ensure(&pcy->obuf, format_size + 1);
99 query_format(array_ptr(pcy->obuf, array_len(pcy->obuf)),
100 array_size(pcy->obuf) - array_len(pcy->obuf),
102 array_len(pcy->obuf) += format_size;
104 array_len(pcy->obuf) += format_size;
106 buffer_addstr(&pcy->obuf, "\n\n");
107 buffer_consume(&pcy->ibuf, query->eoq - pcy->ibuf.data);
108 epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy);
111 static const filter_t *next_filter(server_t *pcy, const filter_t *filter,
112 const query_t *query, const filter_hook_t *hook, bool *ok) {
114 query_context_t *context = pcy->data;
115 if (hook->counter >= 0 && hook->counter < MAX_COUNTERS && hook->cost > 0) {
116 context->context.counters[hook->counter] += hook->cost;
117 debug("request client=%s, from=<%s>, to=<%s>: added %d to counter %d (now %u)",
119 query->sender == NULL ? "undefined" : query->sender,
120 query->recipient == NULL ? "undefined" : query->recipient,
121 hook->cost, hook->counter, context->context.counters[hook->counter]);
125 warn("request client=%s, from=<%s>, to=<%s>: aborted",
127 query->sender == NULL ? "undefined" : query->sender,
128 query->recipient == NULL ? "undefined" : query->recipient);
131 } else if (hook->async) {
132 debug("request client=%s, from=<%s>, to=<%s>: "
133 "asynchronous filter from filter %s",
135 query->sender == NULL ? "undefined" : query->sender,
136 query->recipient == NULL ? "undefined" : query->recipient,
140 } else if (hook->postfix) {
141 info("request client=%s, from=<%s>, to=<%s>: "
142 "awswer %s from filter %s: \"%s\"",
144 query->sender == NULL ? "undefined" : query->sender,
145 query->recipient == NULL ? "undefined" : query->recipient,
146 htokens[hook->type], filter->name, hook->value);
147 policy_answer(pcy, hook->value);
151 debug("request client=%s, from=<%s>, to=<%s>: "
152 "awswer %s from filter %s: next filter %s",
154 query->sender == NULL ? "undefined" : query->sender,
155 query->recipient == NULL ? "undefined" : query->recipient,
156 htokens[hook->type], filter->name,
157 (array_ptr(config->filters, hook->filter_id))->name);
158 return array_ptr(config->filters, hook->filter_id);
162 static bool policy_process(server_t *pcy, const config_t *mconfig)
164 query_context_t *context = pcy->data;
165 const query_t* query = &context->query;
166 const filter_t *filter;
167 if (mconfig->entry_points[query->state] == -1) {
168 warn("no filter defined for current protocol_state (%d)", query->state);
171 if (context->context.current_filter != NULL) {
172 filter = context->context.current_filter;
174 filter = array_ptr(mconfig->filters, mconfig->entry_points[query->state]);
176 context->context.current_filter = NULL;
179 const filter_hook_t *hook = filter_run(filter, query, &context->context);
180 filter = next_filter(pcy, filter, query, hook, &ok);
181 if (filter == NULL) {
187 static int policy_run(server_t *pcy, void* vconfig)
193 int search_offs = MAX(0, (int)(pcy->ibuf.len - 1));
194 int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
196 query_context_t *context = pcy->data;
197 query_t *query = &context->query;
198 context->server = pcy;
199 const config_t *mconfig = vconfig;
202 if (errno == EAGAIN || errno == EINTR)
209 err("unexpected end of data");
213 if (!(eoq = strstr(pcy->ibuf.data + search_offs, "\n\n")))
216 if (!query_parse(pcy->data, pcy->ibuf.data))
218 query->eoq = eoq + strlen("\n\n");
219 if (query->instance == NULL || strcmp(context->context.instance, query->instance) != 0) {
220 filter_context_clean(&context->context);
221 m_strcat(context->context.instance, 64, query->instance);
223 epoll_modify(pcy->fd, 0, pcy);
224 return policy_process(pcy, mconfig) ? 0 : -1;
227 static void policy_async_handler(filter_context_t *context,
228 const filter_hook_t *hook)
231 const filter_t *filter = context->current_filter;
232 query_context_t *qctx = context->data;
233 query_t *query = &qctx->query;
234 server_t *server = qctx->server;
236 context->current_filter = next_filter(server, filter, query, hook, &ok);
237 if (context->current_filter != NULL) {
238 ok = policy_process(server, config);
241 server_release(server);
245 static int postlicyd_init(void)
247 filter_async_handler_register(policy_async_handler);
250 module_init(postlicyd_init);
252 int start_listener(int port)
254 return start_server(port, NULL, NULL);
257 /* administrivia {{{ */
261 fputs("usage: "DAEMON_NAME" [options] config\n"
264 " -l <port> port to listen to\n"
265 " -p <pidfile> file to write our pid to\n"
266 " -f stay in foreground\n"
267 " -d grow logging level\n"
268 " -u unsafe mode (don't drop privileges)\n"
274 int main(int argc, char *argv[])
277 const char *pidfile = NULL;
278 bool daemonize = true;
279 int port = DEFAULT_PORT;
280 bool port_from_cli = false;
282 for (int c = 0; (c = getopt(argc, argv, "ufd" "l:p:")) >= 0; ) {
292 port_from_cli = true;
310 if (argc - optind != 1) {
315 info("starting %s v%s...", DAEMON_NAME, DAEMON_VERSION);
317 if (pidfile_open(pidfile) < 0) {
318 crit("unable to write pidfile %s", pidfile);
322 if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
323 crit("unable to drop privileges");
327 config = config_read(argv[optind]);
328 if (config == NULL) {
331 if (port_from_cli || config->port == 0) {
335 if (daemonize && daemon_detach() < 0) {
336 crit("unable to fork");
342 if (start_listener(config->port) < 0) {
345 return server_loop(query_starter, query_stopper,
346 policy_run, config_refresh, config);