Asynchronous DNS queries on iplist.
[apps/pfixtools.git] / postlicyd / filter.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 © 2008 Florent Bruneau
34  */
35
36 #include "str.h"
37 #include "buffer.h"
38 #include "filter.h"
39
40 static filter_runner_t      runners[FTK_count];
41 static filter_constructor_t constructors[FTK_count];
42 static filter_destructor_t  destructors[FTK_count];
43 static bool                 hooks[FTK_count][HTK_count];
44 static bool                 params[FTK_count][ATK_count];
45
46 static filter_context_constructor_t ctx_constructors[FTK_count];
47 static filter_context_destructor_t  ctx_destructors[FTK_count];
48 static filter_async_handler_t       async_handler = NULL;
49
50 static const filter_hook_t default_hook = {
51     .type      = 0,
52     .value     = (char*)"DUNNO",
53     .postfix   = true,
54     .async     = false,
55     .filter_id = 0
56 };
57
58 static const filter_hook_t async_hook = {
59     .type      = 0,
60     .value     = NULL,
61     .postfix   = false,
62     .async     = true,
63     .filter_id = 0
64 };
65
66 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
67                               filter_destructor_t destructor, filter_runner_t runner,
68                               filter_context_constructor_t context_constructor,
69                               filter_context_destructor_t context_destructor)
70 {
71     filter_token tok = filter_tokenize(type, m_strlen(type));
72     CHECK_FILTER(tok);
73
74     runners[tok] = runner;
75     constructors[tok] = constructor;
76     destructors[tok] = destructor;
77
78     ctx_constructors[tok] = context_constructor;
79     ctx_destructors[tok]  = context_destructor;
80     return tok;
81 }
82
83 filter_result_t filter_hook_register(filter_type_t filter,
84                                      const char *name)
85 {
86     filter_result_t tok = hook_tokenize(name, m_strlen(name));
87     CHECK_FILTER(filter);
88     CHECK_HOOK(tok);
89
90     hooks[filter][tok] = true;
91     return tok;
92 }
93
94 filter_param_id_t filter_param_register(filter_type_t filter,
95                                         const char *name)
96 {
97     filter_param_id_t tok = param_tokenize(name, m_strlen(name));
98     CHECK_FILTER(filter);
99     CHECK_PARAM(tok);
100
101     params[filter][tok] = true;
102     return tok;
103 }
104
105 void filter_async_handler_register(filter_async_handler_t handler)
106 {
107     async_handler = handler;
108 }
109
110 bool filter_build(filter_t *filter)
111 {
112     bool ret = true;
113     if (filter->type == FTK_UNKNOWN || filter->name == NULL) {
114         return false;
115     }
116     if (filter->hooks.len > 0) {
117 #       define QSORT_TYPE filter_hook_t
118 #       define QSORT_BASE filter->hooks.data
119 #       define QSORT_NELT filter->hooks.len
120 #       define QSORT_LT(a,b) a->type < b->type
121 #       include "qsort.c"
122     }
123     filter_constructor_t constructor = constructors[filter->type];
124     if (constructor) {
125         ret = constructor(filter);
126     }
127     array_deep_wipe(filter->params, filter_params_wipe);
128     return ret;
129 }
130
131 bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
132 {
133     foreach (filter_hook_t *hook, filter->hooks) {
134         if (!hook->postfix) {
135             hook->filter_id = filter_find_with_name(filter_list, hook->value);
136             if (hook->filter_id == -1) {
137                 err("invalid filter name %s for hook %s",
138                     hook->value, htokens[hook->type]);
139                 return false;
140             }
141             p_delete(&hook->value);
142         }
143     }}
144     return true;
145 }
146
147 static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level)
148 {
149     if (filter->last_seen == level) {
150         return true;
151     }
152     filter->last_seen = level;
153     foreach (filter_hook_t *hook, filter->hooks) {
154         if (hook->postfix) {
155             continue;
156         }
157         if (hook->filter_id == level) {
158             return false;
159         }
160         if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) {
161             return false;
162         }
163     }}
164     return true;
165 }
166
167 bool filter_check_safety(A(filter_t) *array)
168 {
169     foreach (filter_t *filter, *array) {
170         if (!filter_check_loop(filter, array, __Ai)) {
171             err("the filter tree contains a loop");
172             return false;
173         }
174     }}
175     return true;
176 }
177
178 void filter_wipe(filter_t *filter)
179 {
180     filter_destructor_t destructor = destructors[filter->type];
181     if (destructor) {
182         destructor(filter);
183     }
184     array_deep_wipe(filter->hooks, filter_hook_wipe);
185     array_deep_wipe(filter->params, filter_params_wipe);
186     p_delete(&filter->name);
187 }
188
189 static inline const filter_hook_t *filter_hook_for_result(const filter_t *filter,
190                                                           filter_result_t res)
191 {
192     int start = 0;
193     int end   = filter->hooks.len;
194
195     if (res == HTK_ABORT) {
196         return NULL;
197     }
198     if (res == HTK_ASYNC) {
199         return &async_hook;
200     }
201
202     while (start < end) {
203         int mid = (start + end) / 2;
204         filter_hook_t *hook = array_ptr(filter->hooks, mid);
205         if (hook->type == res) {
206             debug("return hook of type %s, value %s",
207                   htokens[hook->type], hook->value);
208             return hook;
209         } else if (res < hook->type) {
210             end = mid;
211         } else {
212             start = mid + 1;
213         }
214     }
215     warn("missing hook %s for filter %s", htokens[res], filter->name);
216     return &default_hook;
217 }
218
219 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
220                                 filter_context_t *context)
221 {
222     debug("running filter %s (%s)", filter->name, ftokens[filter->type]);
223     filter_result_t res = runners[filter->type](filter, query, context);
224
225     if (res == HTK_ASYNC) {
226         context->current_filter = filter;
227     } else {
228         context->current_filter = NULL;
229     }
230
231     debug("filter run, result is %s", htokens[res]);
232     return filter_hook_for_result(filter, res);
233 }
234
235 bool filter_test(const filter_t *filter, const query_t *query,
236                  filter_context_t *context, filter_result_t result)
237 {
238     return !!(runners[filter->type](filter, query, context) == result);
239 }
240
241 void filter_set_name(filter_t *filter, const char *name, int len)
242 {
243     filter->name = p_dupstr(name, len);
244 }
245
246 bool filter_set_type(filter_t *filter, const char *type, int len)
247 {
248     filter->type = filter_tokenize(type, len);
249     return filter->type != FTK_UNKNOWN;
250 }
251
252 bool filter_add_param(filter_t *filter, const char *name, int name_len,
253                       const char *value, int value_len)
254 {
255     filter_param_t param;
256     param.type = param_tokenize(name, name_len);
257     if (param.type == ATK_UNKNOWN) {
258         err("unknown parameter %.*s", name_len, name);
259         return false;
260     }
261     if (!params[filter->type][param.type]) {
262         err("hook %s is not valid for filter %s",
263             atokens[param.type], ftokens[filter->type]);
264         return false;
265     }
266     param.value     = p_dupstr(value, value_len);
267     param.value_len = value_len;
268     array_add(filter->params, param);
269     return true;
270 }
271
272 bool filter_add_hook(filter_t *filter, const char *name, int name_len,
273                      const char *value, int value_len)
274 {
275     filter_hook_t hook;
276     hook.type  = hook_tokenize(name, name_len);
277     if (hook.type == HTK_UNKNOWN) {
278         err("unknown hook type %.*s", name_len, name);
279         return false;
280     }
281     if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) {
282         err("hook %s not is valid for filter %s",
283             htokens[hook.type], ftokens[filter->type]);
284         return false;
285     }
286     hook.async   = false;
287     hook.postfix = (strncmp(value, "postfix:", 8) == 0);
288     hook.value = m_strdup(hook.postfix ? value + 8 : value);
289     hook.filter_id = -1;
290     array_add(filter->hooks, hook);
291     return true;
292 }
293
294 void filter_context_prepare(filter_context_t *context, void *qctx)
295 {
296     for (int i = 0 ; i < FTK_count ; ++i) {
297         if (ctx_constructors[i] != NULL) {
298             context->contexts[i] = ctx_constructors[i]();
299         }
300     }
301     context->current_filter = NULL;
302     context->data = qctx;
303 }
304
305 void filter_context_wipe(filter_context_t *context)
306 {
307     for (int i = 0 ; i < FTK_count ; ++i) {
308         if (ctx_destructors[i] != NULL) {
309             ctx_destructors[i](context->contexts[i]);
310         }
311     }
312 }
313
314 void filter_post_async_result(filter_context_t *context, filter_result_t result)
315 {
316     const filter_t *filter = context->current_filter;
317     const filter_hook_t *hook = NULL;
318
319     if (result == HTK_ASYNC) {
320         return;
321     }
322     hook = filter_hook_for_result(filter, result);
323     async_handler(context, hook);
324 }