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 rbl_filter_t {
235 int32_t hard_threshold;
236 int32_t soft_threshold;
239 static rbl_filter_t *rbl_filter_new(void)
241 return p_new(rbl_filter_t, 1);
244 static void rbl_filter_delete(rbl_filter_t **rbl)
247 array_deep_wipe((*rbl)->rbls, rbldb_delete);
248 array_wipe((*rbl)->weights);
249 array_wipe((*rbl)->hosts);
250 array_wipe((*rbl)->host_offsets);
251 array_wipe((*rbl)->host_weights);
257 static bool rbl_filter_constructor(filter_t *filter)
259 rbl_filter_t *data = rbl_filter_new();
261 #define PARSE_CHECK(Expr, Str, ...) \
263 err(Str, ##__VA_ARGS__); \
264 rbl_filter_delete(&data); \
268 data->hard_threshold = 1;
269 data->soft_threshold = 1;
270 foreach (filter_param_t *param, filter->params) {
271 switch (param->type) {
272 /* file parameter is:
273 * [no]lock:weight:filename
275 * - lock: memlock the database in memory.
276 * - nolock: don't memlock the database in memory [default].
277 * - \d+: a number describing the weight to give to the match
278 * the given list [mandatory]
279 * the file pointed by filename MUST be a valid ip list issued from
280 * the rsync (or equivalent) service of a (r)bl.
282 case ATK_FILE: case ATK_RBLDNS: {
286 const char *current = param->value;
287 const char *p = m_strchrnul(param->value, ':');
289 for (int i = 0 ; i < 3 ; ++i) {
290 PARSE_CHECK(i == 2 || *p,
291 "file parameter must contains a locking state "
292 "and a weight option");
295 if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
297 } else if ((p - current) == 6
298 && strncmp(current, "nolock", 6) == 0) {
301 PARSE_CHECK(false, "illegal locking state %.*s",
302 (int)(p - current), current);
307 weight = strtol(current, &next, 10);
308 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
309 "illegal weight value %.*s",
310 (int)(p - current), current);
314 rbl = rbldb_create(current, lock);
315 PARSE_CHECK(rbl != NULL,
316 "cannot load rbl db from %s", current);
317 array_add(data->rbls, rbl);
318 array_add(data->weights, weight);
323 p = m_strchrnul(current, ':');
330 * define a RBL to use through DNS resolution.
334 const char *current = param->value;
335 const char *p = m_strchrnul(param->value, ':');
337 for (int i = 0 ; i < 2 ; ++i) {
338 PARSE_CHECK(i == 1 || *p,
339 "host parameter must contains a weight option");
342 weight = strtol(current, &next, 10);
343 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
344 "illegal weight value %.*s",
345 (int)(p - current), current);
349 array_add(data->host_offsets, array_len(data->hosts));
350 array_append(data->hosts, current, strlen(current) + 1);
351 array_add(data->host_weights, weight);
356 p = m_strchrnul(current, ':');
361 /* hard_threshold parameter is an integer.
362 * If the matching score is greater or equal than this threshold,
363 * the hook "hard_match" is called.
364 * hard_threshold = 1 means, that all matches are hard matches.
367 FILTER_PARAM_PARSE_INT(HARD_THRESHOLD, data->hard_threshold);
369 /* soft_threshold parameter is an integer.
370 * if the matching score is greater or equal than this threshold
371 * and smaller or equal than the hard_threshold, the hook "soft_match"
375 FILTER_PARAM_PARSE_INT(SOFT_THRESHOLD, data->soft_threshold);
381 PARSE_CHECK(data->rbls.len || data->host_offsets.len,
382 "no file parameter in the filter %s", filter->name);
387 static void rbl_filter_destructor(filter_t *filter)
389 rbl_filter_t *data = filter->data;
390 rbl_filter_delete(&data);
394 static filter_result_t rbl_filter(const filter_t *filter, const query_t *query)
398 const char *end = NULL;
399 const rbl_filter_t *data = filter->data;
402 if (parse_ipv4(query->client_address, &end, &ip) != 0) {
403 warn("invalid client address: %s, expected ipv4",
404 query->client_address);
407 for (uint32_t i = 0 ; i < data->rbls.len ; ++i) {
408 const rbldb_t *rbl = array_elt(data->rbls, i);
409 int weight = array_elt(data->weights, i);
410 if (rbldb_ipv4_lookup(rbl, ip)) {
412 if (sum >= data->hard_threshold) {
413 return HTK_HARD_MATCH;
418 for (uint32_t i = 0 ; i < data->host_offsets.len ; ++i) {
419 const char *rbl = array_ptr(data->hosts, array_elt(data->host_offsets, i));
420 int weight = array_elt(data->host_weights, i);
421 switch (rbl_check(rbl, ip)) {
425 if (sum >= data->hard_threshold) {
426 return HTK_HARD_MATCH;
433 warn("rbl %s unavailable", rbl);
438 err("filter %s: all the rbl returned an error", filter->name);
441 if (sum >= data->hard_threshold) {
442 return HTK_HARD_MATCH;
443 } else if (sum >= data->soft_threshold) {
444 return HTK_SOFT_MATCH;
450 static int rbl_init(void)
452 filter_type_t type = filter_register("iplist", rbl_filter_constructor,
453 rbl_filter_destructor, rbl_filter);
456 (void)filter_hook_register(type, "abort");
457 (void)filter_hook_register(type, "error");
458 (void)filter_hook_register(type, "fail");
459 (void)filter_hook_register(type, "hard_match");
460 (void)filter_hook_register(type, "soft_match");
464 (void)filter_param_register(type, "file");
465 (void)filter_param_register(type, "rbldns");
466 (void)filter_param_register(type, "dns");
467 (void)filter_param_register(type, "hard_threshold");
468 (void)filter_param_register(type, "soft_threshold");
471 module_init(rbl_init);