Log filter result.
[apps/pfixtools.git] / postlicyd / main-postlicyd.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
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     */
17 /*     permission.                                                            */
18 /*                                                                            */
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 /******************************************************************************/
31
32 /*
33  * Copyright © 2006-2007 Pierre Habouzit
34  * Copyright © 2008 Florent Bruneau
35  */
36
37 #include <getopt.h>
38
39 #include "buffer.h"
40 #include "common.h"
41 #include "epoll.h"
42 #include "policy_tokens.h"
43 #include "server.h"
44 #include "query.h"
45 #include "config.h"
46
47 #define DAEMON_NAME             "postlicyd"
48 #define DEFAULT_PORT            10000
49 #define RUNAS_USER              "nobody"
50 #define RUNAS_GROUP             "nogroup"
51
52 DECLARE_MAIN
53
54 static void *query_starter(server_t* server)
55 {
56     return query_new();
57 }
58
59 static bool config_refresh(void *config)
60 {
61     return config_reload(config);
62 }
63
64 __attribute__((format(printf,2,0)))
65 static void policy_answer(server_t *pcy, const char *fmt, ...)
66 {
67     va_list args;
68     const query_t* query = pcy->data;
69
70     buffer_addstr(&pcy->obuf, "action=");
71     va_start(args, fmt);
72     buffer_addvf(&pcy->obuf, fmt, args);
73     va_end(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);
77 }
78
79 static bool policy_process(server_t *pcy, const config_t *config)
80 {
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);
85         return false;
86     }
87     filter = array_ptr(config->filters, config->entry_points[query->state]);
88     while (true) {
89         const filter_hook_t *hook = filter_run(filter, query);
90         if (hook == NULL) {
91             syslog(LOG_WARNING, "request client=%s, from=<%s>, to=<%s>: aborted",
92                    query->client_name,
93                    query->sender == NULL ? "undefined" : query->sender,
94                    query->recipient == NULL ? "undefined" : query->recipient);
95             return false;
96         } else if (hook->postfix) {
97             syslog(LOG_INFO, "request client=%s, from=<%s>, to=<%s>: "
98                   "awswer %s from filter %s",
99                    query->client_name,
100                    query->sender == NULL ? "undefined" : query->sender,
101                    query->recipient == NULL ? "undefined" : query->recipient,
102                    htokens[hook->type], filter->name);
103             policy_answer(pcy, "%s", hook->value);
104             return true;
105         } else {
106             filter = array_ptr(config->filters, hook->filter_id);
107         }
108     }
109 }
110
111 static int policy_run(server_t *pcy, void* vconfig)
112 {
113     ssize_t search_offs = MAX(0, (ssize_t)(pcy->ibuf.len - 1));
114     int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
115     const char *eoq;
116     query_t  *query  = pcy->data;
117     const config_t *config = vconfig;
118
119     if (nb < 0) {
120         if (errno == EAGAIN || errno == EINTR)
121             return 0;
122         UNIXERR("read");
123         return -1;
124     }
125     if (nb == 0) {
126         if (pcy->ibuf.len)
127             syslog(LOG_ERR, "unexpected end of data");
128         return -1;
129     }
130
131     if (!(eoq = strstr(pcy->ibuf.data + search_offs, "\n\n")))
132         return 0;
133
134     if (!query_parse(pcy->data, pcy->ibuf.data))
135         return -1;
136     query->eoq = eoq + strlen("\n\n");
137     epoll_modify(pcy->fd, 0, pcy);
138     return policy_process(pcy, config) ? 0 : -1;
139 }
140
141 int start_listener(int port)
142 {
143     return start_server(port, NULL, NULL);
144 }
145
146 /* administrivia {{{ */
147
148 void usage(void)
149 {
150     fputs("usage: "DAEMON_NAME" [options] config\n"
151           "\n"
152           "Options:\n"
153           "    -l <port>    port to listen to\n"
154           "    -p <pidfile> file to write our pid to\n"
155           "    -f           stay in foreground\n"
156          , stderr);
157 }
158
159 /* }}} */
160
161 int main(int argc, char *argv[])
162 {
163     bool unsafe = false;
164     const char *pidfile = NULL;
165     bool daemonize = true;
166     int port = DEFAULT_PORT;
167     bool port_from_cli = false;
168
169     for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) {
170         switch (c) {
171           case 'p':
172             pidfile = optarg;
173             break;
174           case 'u':
175             unsafe = true;
176             break;
177           case 'l':
178             port = atoi(optarg);
179             port_from_cli = true;
180             break;
181           case 'f':
182             daemonize = false;
183             break;
184           default:
185             usage();
186             return EXIT_FAILURE;
187         }
188     }
189
190     if (argc - optind != 1) {
191         usage();
192         return EXIT_FAILURE;
193     }
194
195     if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
196         syslog(LOG_CRIT, "unable to drop privileges");
197         return EXIT_FAILURE;
198     }
199
200     config_t *config = config_read(argv[optind]);
201     if (config == NULL) {
202         return EXIT_FAILURE;
203     }
204     if (port_from_cli || config->port == 0) {
205         config->port = port;
206     }
207
208     if (common_setup(pidfile, true, NULL, NULL, daemonize) != EXIT_SUCCESS
209         || start_listener(config->port) < 0) {
210         config_delete(&config);
211         return EXIT_FAILURE;
212     } else {
213         int res = server_loop(query_starter, (delete_client_t)query_delete,
214                               policy_run, config_refresh, config);
215         config_delete(&config);
216         return res;
217     }
218 }