d277542dec496de225419693f944b9f94b33715d
[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 const filter_hook_t default_hook = {
47     .type      = 0,
48     .value     = (char*)"DUNNO",
49     .postfix   = true,
50     .filter_id = 0
51 };
52
53 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
54                               filter_destructor_t destructor, filter_runner_t runner)
55 {
56     filter_token tok = filter_tokenize(type, m_strlen(type));
57     CHECK_FILTER(tok);
58
59     runners[tok] = runner;
60     constructors[tok] = constructor;
61     destructors[tok] = destructor;
62     return tok;
63 }
64
65 filter_result_t filter_hook_register(filter_type_t filter,
66                                      const char *name)
67 {
68     filter_result_t tok = hook_tokenize(name, m_strlen(name));
69     CHECK_FILTER(filter);
70     CHECK_HOOK(tok);
71
72     hooks[filter][tok] = true;
73     return tok;
74 }
75
76 filter_param_id_t filter_param_register(filter_type_t filter,
77                                         const char *name)
78 {
79     filter_param_id_t tok = param_tokenize(name, m_strlen(name));
80     CHECK_FILTER(filter);
81     CHECK_PARAM(tok);
82
83     params[filter][tok] = true;
84     return tok;
85 }
86
87 bool filter_build(filter_t *filter)
88 {
89     bool ret = true;
90     if (filter->type == FTK_UNKNOWN || filter->name == NULL) {
91         return false;
92     }
93     if (filter->hooks.len > 0) {
94 #       define QSORT_TYPE filter_hook_t
95 #       define QSORT_BASE filter->hooks.data
96 #       define QSORT_NELT filter->hooks.len
97 #       define QSORT_LT(a,b) a->type < b->type
98 #       include "qsort.c"
99     }
100     filter_constructor_t constructor = constructors[filter->type];
101     if (constructor) {
102         ret = constructor(filter);
103     }
104     array_deep_wipe(filter->params, filter_params_wipe);
105     return ret;
106 }
107
108 bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
109 {
110     foreach (filter_hook_t *hook, filter->hooks) {
111         if (!hook->postfix) {
112             hook->filter_id = filter_find_with_name(filter_list, hook->value);
113             if (hook->filter_id == -1) {
114                 err("invalid filter name %s for hook %s",
115                     hook->value, htokens[hook->type]);
116                 return false;
117             }
118             p_delete(&hook->value);
119         }
120     }}
121     return true;
122 }
123
124 static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level)
125 {
126     if (filter->last_seen == level) {
127         return true;
128     }
129     filter->last_seen = level;
130     foreach (filter_hook_t *hook, filter->hooks) {
131         if (hook->postfix) {
132             continue;
133         }
134         if (hook->filter_id == level) {
135             return false;
136         }
137         if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) {
138             return false;
139         }
140     }}
141     return true;
142 }
143
144 bool filter_check_safety(A(filter_t) *array)
145 {
146     foreach (filter_t *filter, *array) {
147         if (!filter_check_loop(filter, array, __Ai)) {
148             err("the filter tree contains a loop");
149             return false;
150         }
151     }}
152     return true;
153 }
154
155 void filter_wipe(filter_t *filter)
156 {
157     filter_destructor_t destructor = destructors[filter->type];
158     if (destructor) {
159         destructor(filter);
160     }
161     array_deep_wipe(filter->hooks, filter_hook_wipe);
162     array_deep_wipe(filter->params, filter_params_wipe);
163     p_delete(&filter->name);
164 }
165
166 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query)
167 {
168     int start = 0;
169     int end   = filter->hooks.len;
170     debug("running filter %s (%s)", filter->name, ftokens[filter->type]);
171     filter_result_t res = runners[filter->type](filter, query);
172
173     if (res == HTK_ABORT) {
174         return NULL;
175     }
176     debug("filter run, result is %s", htokens[res]);
177
178     while (start < end) {
179         int mid = (start + end) / 2;
180         filter_hook_t *hook = array_ptr(filter->hooks, mid);
181         if (hook->type == res) {
182             debug("return hook of type %s, value %s",
183                   htokens[hook->type], hook->value);
184             return hook;
185         } else if (res < hook->type) {
186             end = mid;
187         } else {
188             start = mid + 1;
189         }
190     }
191     warn("missing hook %s for filter %s", htokens[res], filter->name);
192     return &default_hook;
193 }
194
195 bool filter_test(const filter_t *filter, const query_t *query, filter_result_t result)
196 {
197     return !!(runners[filter->type](filter, query) == result);
198 }
199
200 void filter_set_name(filter_t *filter, const char *name, ssize_t len)
201 {
202     filter->name = p_dupstr(name, len);
203 }
204
205 bool filter_set_type(filter_t *filter, const char *type, ssize_t len)
206 {
207     filter->type = filter_tokenize(type, len);
208     return filter->type != FTK_UNKNOWN;
209 }
210
211 bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len,
212                       const char *value, ssize_t value_len)
213 {
214     filter_param_t param;
215     param.type = param_tokenize(name, name_len);
216     if (param.type == ATK_UNKNOWN) {
217         err("unknown parameter %.*s", name_len, name);
218         return false;
219     }
220     if (!params[filter->type][param.type]) {
221         err("hook %s is not valid for filter %s",
222             atokens[param.type], ftokens[filter->type]);
223         return false;
224     }
225     param.value     = p_dupstr(value, value_len);
226     param.value_len = value_len;
227     array_add(filter->params, param);
228     return true;
229 }
230
231 bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len,
232                      const char *value, ssize_t value_len)
233 {
234     filter_hook_t hook;
235     hook.type  = hook_tokenize(name, name_len);
236     if (hook.type == HTK_UNKNOWN) {
237         err("unknown hook type %.*s", name_len, name);
238         return false;
239     }
240     if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) {
241         err("hook %s not is valid for filter %s",
242             htokens[hook.type], ftokens[filter->type]);
243         return false;
244     }
245     hook.postfix = (strncmp(value, "postfix:", 8) == 0);
246     hook.value = m_strdup(hook.postfix ? value + 8 : value);
247     hook.filter_id = -1;
248     array_add(filter->hooks, hook);
249     return true;
250 }