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.2"
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 return config_reload(mconfig);
79 __attribute__((format(printf,2,0)))
80 static void policy_answer(server_t *pcy, const char *fmt, ...)
83 query_context_t *context = pcy->data;
84 const query_t* query = &context->query;
86 buffer_addstr(&pcy->obuf, "action=");
88 buffer_addvf(&pcy->obuf, fmt, args);
90 buffer_addstr(&pcy->obuf, "\n\n");
91 buffer_consume(&pcy->ibuf, query->eoq - pcy->ibuf.data);
92 epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy);
95 static bool policy_process(server_t *pcy, const config_t *mconfig)
97 query_context_t *context = pcy->data;
98 const query_t* query = &context->query;
99 const filter_t *filter;
100 if (mconfig->entry_points[query->state] == -1) {
101 warn("no filter defined for current protocol_state (%d)", query->state);
104 if (context->context.current_filter != NULL) {
105 filter = context->context.current_filter;
107 filter = array_ptr(mconfig->filters, mconfig->entry_points[query->state]);
109 context->context.current_filter = NULL;
111 const filter_hook_t *hook = filter_run(filter, query, &context->context);
113 warn("request client=%s, from=<%s>, to=<%s>: aborted",
115 query->sender == NULL ? "undefined" : query->sender,
116 query->recipient == NULL ? "undefined" : query->recipient);
118 } else if (hook->async) {
119 debug("request client=%s, from=<%s>, to=<%s>: "
120 "asynchronous filter from filter %s",
122 query->sender == NULL ? "undefined" : query->sender,
123 query->recipient == NULL ? "undefined" : query->recipient,
126 } else if (hook->postfix) {
127 info("request client=%s, from=<%s>, to=<%s>: "
128 "awswer %s from filter %s: \"%s\"",
130 query->sender == NULL ? "undefined" : query->sender,
131 query->recipient == NULL ? "undefined" : query->recipient,
132 htokens[hook->type], filter->name, hook->value);
133 policy_answer(pcy, "%s", hook->value);
136 debug("request client=%s, from=<%s>, to=<%s>: "
137 "awswer %s from filter %s: next filter %s",
139 query->sender == NULL ? "undefined" : query->sender,
140 query->recipient == NULL ? "undefined" : query->recipient,
141 htokens[hook->type], filter->name,
142 (array_ptr(mconfig->filters, hook->filter_id))->name);
143 filter = array_ptr(mconfig->filters, hook->filter_id);
148 static int policy_run(server_t *pcy, void* vconfig)
150 int search_offs = MAX(0, (int)(pcy->ibuf.len - 1));
151 int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
153 query_context_t *context = pcy->data;
154 query_t *query = &context->query;
155 context->server = pcy;
156 const config_t *mconfig = vconfig;
159 if (errno == EAGAIN || errno == EINTR)
166 err("unexpected end of data");
170 if (!(eoq = strstr(pcy->ibuf.data + search_offs, "\n\n")))
173 if (!query_parse(pcy->data, pcy->ibuf.data))
175 query->eoq = eoq + strlen("\n\n");
176 epoll_modify(pcy->fd, 0, pcy);
177 return policy_process(pcy, mconfig) ? 0 : -1;
180 static void policy_async_handler(filter_context_t *context,
181 const filter_hook_t *hook)
183 const filter_t *filter = context->current_filter;
184 query_context_t *qctx = context->data;
185 query_t *query = &qctx->query;
186 server_t *server = qctx->server;
188 debug("request client=%s, from=<%s>, to=<%s>: "
189 "awswer %s from filter %s: next filter %s",
191 query->sender == NULL ? "undefined" : query->sender,
192 query->recipient == NULL ? "undefined" : query->recipient,
193 htokens[hook->type], filter->name,
194 (array_ptr(config->filters, hook->filter_id))->name);
195 context->current_filter = array_ptr(config->filters, hook->filter_id);
197 if (!policy_process(server, config)) {
198 server_release(server);
202 static int postlicyd_init(void)
204 filter_async_handler_register(policy_async_handler);
207 module_init(postlicyd_init);
209 int start_listener(int port)
211 return start_server(port, NULL, NULL);
214 /* administrivia {{{ */
218 fputs("usage: "DAEMON_NAME" [options] config\n"
221 " -l <port> port to listen to\n"
222 " -p <pidfile> file to write our pid to\n"
223 " -f stay in foreground\n"
224 " -d grow logging level\n"
225 " -u unsafe mode (don't drop privileges)\n"
231 int main(int argc, char *argv[])
234 const char *pidfile = NULL;
235 bool daemonize = true;
236 int port = DEFAULT_PORT;
237 bool port_from_cli = false;
239 for (int c = 0; (c = getopt(argc, argv, "ufd" "l:p:")) >= 0; ) {
249 port_from_cli = true;
267 if (argc - optind != 1) {
272 info("starting %s v%s...", DAEMON_NAME, DAEMON_VERSION);
274 if (pidfile_open(pidfile) < 0) {
275 crit("unable to write pidfile %s", pidfile);
279 if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
280 crit("unable to drop privileges");
284 config = config_read(argv[optind]);
285 if (config == NULL) {
288 if (port_from_cli || config->port == 0) {
292 if (daemonize && daemon_detach() < 0) {
293 crit("unable to fork");
299 if (start_listener(config->port) < 0) {
302 return server_loop(query_starter, query_stopper,
303 policy_run, config_refresh, config);