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 © 2008 Florent Bruneau
40 #include "policy_tokens.h"
42 typedef struct strlist_config_t {
52 unsigned is_hostname :1;
54 unsigned match_sender :1;
55 unsigned match_recipient :1;
57 unsigned match_helo :1;
58 unsigned match_client :1;
59 unsigned match_reverse :1;
63 static strlist_config_t *strlist_config_new(void)
65 return p_new(strlist_config_t, 1);
68 static void strlist_config_delete(strlist_config_t **config)
71 array_deep_wipe((*config)->tries, trie_delete);
72 array_wipe((*config)->weights);
73 array_wipe((*config)->reverses);
74 array_wipe((*config)->partiales);
79 static inline void strlist_copy(char *dest, const char *str, ssize_t str_len,
84 for (const char *src = str + str_len - 1 ; src >= str ; --src) {
85 *dest = ascii_tolower(*src);
89 for (int i = 0 ; i < str_len ; ++i) {
90 *dest = ascii_tolower(str[i]);
99 static trie_t *strlist_create(const char *file, bool reverse, bool lock)
106 if (!file_map_open(&map, file, false)) {
111 while (end > p && end[-1] != '\n') {
114 if (end != map.end) {
115 warn("file %s miss a final \\n, ignoring last line",
120 while (p < end && p != NULL) {
121 const char *eol = (char *)memchr(p, '\n', end - p);
125 if (eol - p >= BUFSIZ) {
126 err("unreasonnable long line");
127 file_map_close(&map);
132 const char *eos = eol;
133 while (p < eos && isspace(*p)) {
136 while (p < eos && isspace(eos[-1])) {
140 strlist_copy(line, p, eos - p, reverse);
141 trie_insert(db, line);
146 file_map_close(&map);
147 trie_compile(db, lock);
152 static bool strlist_filter_constructor(filter_t *filter)
154 strlist_config_t *config = strlist_config_new();
156 #define PARSE_CHECK(Expr, Str, ...) \
158 err(Str, ##__VA_ARGS__); \
159 strlist_config_delete(&config); \
163 config->hard_threshold = 1;
164 config->soft_threshold = 1;
165 foreach (filter_param_t *param, filter->params) {
166 switch (param->type) {
167 /* file parameter is:
168 * [no]lock:(prefix|suffix):weight:filename
170 * - lock: memlock the database in memory.
171 * - nolock: don't memlock the database in memory.
172 * - prefix: perform "prefix" compression on storage.
173 * - suffix perform "suffix" compression on storage.
174 * - \d+: a number describing the weight to give to the match
175 * the given list [mandatory]
176 * the file pointed by filename MUST be a valid string list (one string per
177 * line, empty lines and lines beginning with a '#' are ignored).
182 bool reverse = false;
183 bool partial = false;
185 const char *current = param->value;
186 const char *p = m_strchrnul(param->value, ':');
188 for (int i = 0 ; i < 4 ; ++i) {
189 PARSE_CHECK(i == 3 || *p,
190 "file parameter must contains a locking state "
191 "and a weight option");
194 if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
196 } else if ((p - current) == 6 && strncmp(current, "nolock", 6) == 0) {
199 PARSE_CHECK(false, "illegal locking state %.*s",
200 p - current, current);
205 if (p - current > (ssize_t)strlen("partial-")
206 && strncmp(current, "partial-", strlen("partial-")) == 0) {
208 current += strlen("partial-");
210 if ((p - current) == 6 && strncmp(current, "suffix", 6) == 0) {
212 } else if ((p - current) == 6 && strncmp(current, "prefix", 6) == 0) {
215 PARSE_CHECK(false, "illegal character order value %.*s",
216 p - current, current);
221 weight = strtol(current, &next, 10);
222 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
223 "illegal weight value %.*s",
224 (p - current), current);
228 trie = strlist_create(current, reverse, lock);
229 PARSE_CHECK(trie != NULL,
230 "cannot load string list from %s", current);
231 array_add(config->tries, trie);
232 array_add(config->weights, weight);
233 array_add(config->reverses, reverse);
234 array_add(config->partiales, partial);
239 p = m_strchrnul(current, ':');
244 /* hard_threshold parameter is an integer.
245 * If the matching score is greater or equal than this threshold,
246 * the hook "hard_match" is called.
247 * hard_threshold = 1 means, that all matches are hard matches.
250 FILTER_PARAM_PARSE_INT(HARD_THRESHOLD, config->hard_threshold);
252 /* soft_threshold parameter is an integer.
253 * if the matching score is greater or equal than this threshold
254 * and smaller or equal than the hard_threshold, the hook "soft_match"
258 FILTER_PARAM_PARSE_INT(SOFT_THRESHOLD, config->soft_threshold);
260 /* fields to match againes:
261 * fields = field_name(,field_name)*
263 * - hostname: helo_name,client_name,reverse_client_name
264 * - email: sender,recipient
267 const char *current = param->value;
268 const char *p = m_strchrnul(param->value, ',');
270 postlicyd_token tok = policy_tokenize(current, p - current);
272 #define CASE(Up, Low, Type) \
274 config->match_ ## Low = true; \
275 config->is_ ## Type = true; \
277 CASE(HELO_NAME, helo, hostname);
278 CASE(CLIENT_NAME, client, hostname);
279 CASE(REVERSE_CLIENT_NAME, reverse, hostname);
280 CASE(SENDER_DOMAIN, sender, hostname);
281 CASE(RECIPIENT_DOMAIN, recipient, hostname);
282 CASE(SENDER, sender, email);
283 CASE(RECIPIENT, recipient, email);
286 PARSE_CHECK(false, "unknown field %.*s", p - current, current);
293 p = m_strchrnul(current, ',');
301 PARSE_CHECK(config->is_email != config->is_hostname,
302 "matched field MUST be emails XOR hostnames");
303 PARSE_CHECK(config->tries.len,
304 "no file parameter in the filter %s", filter->name);
305 filter->data = config;
309 static void strlist_filter_destructor(filter_t *filter)
311 strlist_config_t *config = filter->data;
312 strlist_config_delete(&config);
313 filter->data = config;
316 static filter_result_t strlist_filter(const filter_t *filter, const query_t *query)
318 char reverse[BUFSIZ];
320 const strlist_config_t *config = filter->data;
322 if (config->is_email &&
323 ((config->match_sender && query->state < SMTP_MAIL)
324 || (config->match_recipient && query->state != SMTP_RCPT))) {
325 warn("trying to match an email against a field that is not "
326 "available in current protocol state");
328 } else if (config->is_hostname && config->match_helo && query->state < SMTP_HELO) {
329 warn("trying to match hostname against helo before helo is received");
332 #define LOOKUP(Flag, Field) \
333 if (config->match_ ## Flag) { \
334 const int len = m_strlen(query->Field); \
335 strlist_copy(normal, query->Field, len, false); \
336 strlist_copy(reverse, query->Field, len, true); \
337 for (uint32_t i = 0 ; i < config->tries.len ; ++i) { \
338 const int weight = array_elt(config->weights, i); \
339 const trie_t *trie = array_elt(config->tries, i); \
340 const bool rev = array_elt(config->reverses, i); \
341 const bool part = array_elt(config->partiales, i); \
342 if ((!part && trie_lookup(trie, rev ? reverse : normal)) \
343 || (part && trie_prefix(trie, rev ? reverse : normal))) { \
345 if (sum >= config->hard_threshold) { \
346 return HTK_HARD_MATCH; \
351 if (config->is_email) {
352 LOOKUP(sender, sender);
353 LOOKUP(recipient, recipient);
354 } else if (config->is_hostname) {
355 LOOKUP(helo, helo_name);
356 LOOKUP(client, client_name);
357 LOOKUP(reverse, reverse_client_name);
358 LOOKUP(recipient, recipient_domain);
359 LOOKUP(sender, sender_domain);
362 if (sum >= config->hard_threshold) {
363 return HTK_HARD_MATCH;
364 } else if (sum >= config->soft_threshold) {
365 return HTK_SOFT_MATCH;
371 static int strlist_init(void)
373 filter_type_t type = filter_register("strlist", strlist_filter_constructor,
374 strlist_filter_destructor, strlist_filter);
377 (void)filter_hook_register(type, "abort");
378 (void)filter_hook_register(type, "error");
379 (void)filter_hook_register(type, "fail");
380 (void)filter_hook_register(type, "hard_match");
381 (void)filter_hook_register(type, "soft_match");
385 (void)filter_param_register(type, "file");
386 (void)filter_param_register(type, "hard_threshold");
387 (void)filter_param_register(type, "soft_threshold");
388 (void)filter_param_register(type, "fields");
391 module_init(strlist_init);