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 © 2007 Pierre Habouzit
34 * Copyright © 2008 Florent Bruneau
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
49 #define IPv4_PREFIX(ip) ((uint32_t)(ip) >> IPv4_BITS)
50 #define IPv4_SUFFIX(ip) ((uint32_t)(ip) & ((1 << IPv4_BITS) - 1))
51 #define NODE(db, i) ((db)->tree + (i))
65 A(uint16_t) ips[1 << 16];
69 static int get_o(const char *s, const char **out)
73 if (*s < '0' || *s > '9')
77 if (*s < '0' || *s > '9')
80 res = res * 10 + *s++ - '0';
81 if (*s < '0' || *s > '9')
84 res = res * 10 + *s++ - '0';
85 if (!(*s < '0' || *s > '9') || res < 100)
93 static int parse_ipv4(const char *s, const char **out, uint32_t *ip)
98 if ((o & ~0xff) || *s++ != '.')
103 if ((o & ~0xff) || *s++ != '.')
108 if ((o & ~0xff) || *s++ != '.')
121 rbldb_t *rbldb_create(const char *file, bool lock)
128 if (!file_map_open(&map, file, false)) {
134 while (end > p && end[-1] != '\n') {
137 if (end != map.end) {
138 warn("file %s miss a final \\n, ignoring last line",
142 db = p_new(rbldb_t, 1);
146 while (*p == ' ' || *p == '\t' || *p == '\r')
149 if (parse_ipv4(p, &p, &ip) < 0) {
150 p = (char *)memchr(p, '\n', end - p) + 1;
152 array_add(db->ips[ip >> 16], ip & 0xffff);
156 file_map_close(&map);
158 /* Lookup may perform serveral I/O, so avoid swap.
160 for (int i = 0 ; i < 1 << 16 ; ++i) {
161 array_adjust(db->ips[i]);
162 if (lock && !array_lock(db->ips[i])) {
165 if (db->ips[i].len) {
166 # define QSORT_TYPE uint16_t
167 # define QSORT_BASE db->ips[i].data
168 # define QSORT_NELT db->ips[i].len
169 # define QSORT_LT(a,b) *a < *b
174 info("rbl %s loaded, %d IPs", file, ips);
178 static void rbldb_wipe(rbldb_t *db)
180 for (int i = 0 ; i < 1 << 16 ; ++i) {
181 array_wipe(db->ips[i]);
185 void rbldb_delete(rbldb_t **db)
193 uint32_t rbldb_stats(const rbldb_t *rbl)
196 for (int i = 0 ; i < 1 << 16 ; ++i) {
197 ips += array_len(rbl->ips[i]);
202 bool rbldb_ipv4_lookup(const rbldb_t *db, uint32_t ip)
204 const uint16_t hip = ip >> 16;
205 const uint16_t lip = ip & 0xffff;
206 int l = 0, r = db->ips[hip].len;
211 if (array_elt(db->ips[hip], i) == lip)
214 if (lip < array_elt(db->ips[hip], i)) {
224 /* postlicyd filter declaration */
228 typedef struct iplist_filter_t {
235 int32_t hard_threshold;
236 int32_t soft_threshold;
239 typedef struct iplist_async_data_t {
240 A(rbl_result_t) results;
244 } iplist_async_data_t;
246 static filter_type_t filter_type = FTK_UNKNOWN;
248 static iplist_filter_t *iplist_filter_new(void)
250 return p_new(iplist_filter_t, 1);
253 static void iplist_filter_delete(iplist_filter_t **rbl)
256 array_deep_wipe((*rbl)->rbls, rbldb_delete);
257 array_wipe((*rbl)->weights);
258 array_wipe((*rbl)->hosts);
259 array_wipe((*rbl)->host_offsets);
260 array_wipe((*rbl)->host_weights);
266 static bool iplist_filter_constructor(filter_t *filter)
268 iplist_filter_t *data = iplist_filter_new();
270 #define PARSE_CHECK(Expr, Str, ...) \
272 err(Str, ##__VA_ARGS__); \
273 iplist_filter_delete(&data); \
277 data->hard_threshold = 1;
278 data->soft_threshold = 1;
279 foreach (filter_param_t *param, filter->params) {
280 switch (param->type) {
281 /* file parameter is:
282 * [no]lock:weight:filename
284 * - lock: memlock the database in memory.
285 * - nolock: don't memlock the database in memory [default].
286 * - \d+: a number describing the weight to give to the match
287 * the given list [mandatory]
288 * the file pointed by filename MUST be a valid ip list issued from
289 * the rsync (or equivalent) service of a (r)bl.
291 case ATK_FILE: case ATK_RBLDNS: {
295 const char *current = param->value;
296 const char *p = m_strchrnul(param->value, ':');
298 for (int i = 0 ; i < 3 ; ++i) {
299 PARSE_CHECK(i == 2 || *p,
300 "file parameter must contains a locking state "
301 "and a weight option");
304 if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
306 } else if ((p - current) == 6
307 && strncmp(current, "nolock", 6) == 0) {
310 PARSE_CHECK(false, "illegal locking state %.*s",
311 (int)(p - current), current);
316 weight = strtol(current, &next, 10);
317 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
318 "illegal weight value %.*s",
319 (int)(p - current), current);
323 rbl = rbldb_create(current, lock);
324 PARSE_CHECK(rbl != NULL,
325 "cannot load rbl db from %s", current);
326 array_add(data->rbls, rbl);
327 array_add(data->weights, weight);
332 p = m_strchrnul(current, ':');
339 * define a RBL to use through DNS resolution.
343 const char *current = param->value;
344 const char *p = m_strchrnul(param->value, ':');
346 for (int i = 0 ; i < 2 ; ++i) {
347 PARSE_CHECK(i == 1 || *p,
348 "host parameter must contains a weight option");
351 weight = strtol(current, &next, 10);
352 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
353 "illegal weight value %.*s",
354 (int)(p - current), current);
358 array_add(data->host_offsets, array_len(data->hosts));
359 array_append(data->hosts, current, strlen(current) + 1);
360 array_add(data->host_weights, weight);
365 p = m_strchrnul(current, ':');
370 /* hard_threshold parameter is an integer.
371 * If the matching score is greater or equal than this threshold,
372 * the hook "hard_match" is called.
373 * hard_threshold = 1 means, that all matches are hard matches.
376 FILTER_PARAM_PARSE_INT(HARD_THRESHOLD, data->hard_threshold);
378 /* soft_threshold parameter is an integer.
379 * if the matching score is greater or equal than this threshold
380 * and smaller or equal than the hard_threshold, the hook "soft_match"
384 FILTER_PARAM_PARSE_INT(SOFT_THRESHOLD, data->soft_threshold);
390 PARSE_CHECK(data->rbls.len || data->host_offsets.len,
391 "no file parameter in the filter %s", filter->name);
396 static void iplist_filter_destructor(filter_t *filter)
398 iplist_filter_t *data = filter->data;
399 iplist_filter_delete(&data);
403 static void iplist_filter_async(rbl_result_t *result, void *arg)
405 filter_context_t *context = arg;
406 const filter_t *filter = context->current_filter;
407 const iplist_filter_t *data = filter->data;
408 iplist_async_data_t *async = context->contexts[filter_type];
411 if (*result != RBL_ERROR) {
412 async->error = false;
416 debug("got asynchronous request result for filter %s, rbl %d, still awaiting %d answers",
417 filter->name, (int)(result - array_ptr(async->results, 0)), async->awaited);
419 if (async->awaited == 0) {
420 filter_result_t res = HTK_FAIL;
424 for (uint32_t i = 0 ; i < array_len(data->host_offsets) ; ++i) {
425 int weight = array_elt(data->host_weights, i);
427 switch (array_elt(async->results, i)) {
429 crit("no more awaited answer but result is ASYNC");
432 async->sum += weight;
438 if (async->sum >= (uint32_t)data->hard_threshold) {
439 res = HTK_HARD_MATCH;
440 } else if (async->sum >= (uint32_t)data->soft_threshold) {
441 res = HTK_SOFT_MATCH;
444 debug("answering to filter %s", filter->name);
445 filter_post_async_result(context, res);
449 static filter_result_t iplist_filter(const filter_t *filter, const query_t *query,
450 filter_context_t *context)
454 const char *end = NULL;
455 const iplist_filter_t *data = filter->data;
458 if (parse_ipv4(query->client_address, &end, &ip) != 0) {
459 warn("invalid client address: %s, expected ipv4",
460 query->client_address);
463 for (uint32_t i = 0 ; i < data->rbls.len ; ++i) {
464 const rbldb_t *rbl = array_elt(data->rbls, i);
465 int weight = array_elt(data->weights, i);
466 if (rbldb_ipv4_lookup(rbl, ip)) {
468 if (sum >= data->hard_threshold) {
469 return HTK_HARD_MATCH;
474 if (array_len(data->host_offsets) > 0) {
475 iplist_async_data_t* async = context->contexts[filter_type];
476 array_ensure_exact_capacity(async->results, array_len(data->host_offsets));
479 for (uint32_t i = 0 ; i < data->host_offsets.len ; ++i) {
480 const char *rbl = array_ptr(data->hosts, array_elt(data->host_offsets, i));
481 if (rbl_check(rbl, ip, array_ptr(async->results, i),
482 iplist_filter_async, context)) {
487 debug("filter %s awaiting %d asynchronous queries", filter->name, async->awaited);
488 async->error = error;
492 err("filter %s: all the rbl returned an error", filter->name);
495 if (sum >= data->hard_threshold) {
496 return HTK_HARD_MATCH;
497 } else if (sum >= data->soft_threshold) {
498 return HTK_SOFT_MATCH;
504 static void *iplist_context_constructor(void)
506 return p_new(iplist_async_data_t, 1);
509 static void iplist_context_destructor(void *data)
511 iplist_async_data_t *ctx = data;
512 array_wipe(ctx->results);
516 static int iplist_init(void)
518 filter_type = filter_register("iplist", iplist_filter_constructor,
519 iplist_filter_destructor, iplist_filter,
520 iplist_context_constructor,
521 iplist_context_destructor);
524 (void)filter_hook_register(filter_type, "abort");
525 (void)filter_hook_register(filter_type, "error");
526 (void)filter_hook_register(filter_type, "fail");
527 (void)filter_hook_register(filter_type, "hard_match");
528 (void)filter_hook_register(filter_type, "soft_match");
529 (void)filter_hook_register(filter_type, "async");
533 (void)filter_param_register(filter_type, "file");
534 (void)filter_param_register(filter_type, "rbldns");
535 (void)filter_param_register(filter_type, "dns");
536 (void)filter_param_register(filter_type, "hard_threshold");
537 (void)filter_param_register(filter_type, "soft_threshold");
540 module_init(iplist_init);