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"
47 #define DAEMON_NAME "postlicyd"
48 #define DEFAULT_PORT 10000
49 #define RUNAS_USER "nobody"
50 #define RUNAS_GROUP "nogroup"
54 static void *query_starter(server_t* server)
59 static bool config_refresh(void *config)
61 return config_reload(config);
64 __attribute__((format(printf,2,0)))
65 static void policy_answer(server_t *pcy, const char *fmt, ...)
68 const query_t* query = pcy->data;
70 buffer_addstr(&pcy->obuf, "action=");
72 buffer_addvf(&pcy->obuf, fmt, args);
74 buffer_addstr(&pcy->obuf, "\n\n");
75 buffer_consume(&pcy->ibuf, query->eoq - pcy->ibuf.data);
76 epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy);
79 static bool policy_process(server_t *pcy, const config_t *config)
81 const query_t* query = pcy->data;
82 const filter_t *filter;
83 if (config->entry_points[query->state] == -1) {
84 syslog(LOG_WARNING, "no filter defined for current protocol_state (%d)", query->state);
87 filter = array_ptr(config->filters, config->entry_points[query->state]);
89 const filter_hook_t *hook = filter_run(filter, query);
91 syslog(LOG_WARNING, "request aborted");
93 } else if (hook->postfix) {
94 policy_answer(pcy, "%s", hook->value);
97 filter = array_ptr(config->filters, hook->filter_id);
102 static int policy_run(server_t *pcy, void* vconfig)
104 ssize_t search_offs = MAX(0, (ssize_t)(pcy->ibuf.len - 1));
105 int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
107 query_t *query = pcy->data;
108 const config_t *config = vconfig;
111 if (errno == EAGAIN || errno == EINTR)
118 syslog(LOG_ERR, "unexpected end of data");
122 if (!(eoq = strstr(pcy->ibuf.data + search_offs, "\n\n")))
125 if (!query_parse(pcy->data, pcy->ibuf.data))
127 query->eoq = eoq + strlen("\n\n");
128 epoll_modify(pcy->fd, 0, pcy);
129 return policy_process(pcy, config) ? 0 : -1;
132 int start_listener(int port)
134 return start_server(port, NULL, NULL);
137 /* administrivia {{{ */
141 fputs("usage: "DAEMON_NAME" [options] config\n"
144 " -l <port> port to listen to\n"
145 " -p <pidfile> file to write our pid to\n"
146 " -f stay in foreground\n"
152 int main(int argc, char *argv[])
155 const char *pidfile = NULL;
156 bool daemonize = true;
157 int port = DEFAULT_PORT;
158 bool port_from_cli = false;
160 for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) {
170 port_from_cli = true;
181 if (argc - optind != 1) {
186 config_t *config = config_read(argv[optind]);
187 if (config == NULL) {
190 if (port_from_cli || config->port == 0) {
194 if (common_setup(pidfile, false, RUNAS_USER, RUNAS_GROUP,
195 daemonize) != EXIT_SUCCESS
196 || start_listener(config->port) < 0) {
197 config_delete(&config);
201 int res = server_loop(query_starter, (delete_client_t)query_delete,
202 policy_run, config_refresh, config);
203 config_delete(&config);