+
+
+/* postlicyd filter declaration */
+
+#include "filter.h"
+
+typedef struct rbl_filter_t {
+ PA(rbldb_t) rbls;
+ A(int) weights;
+
+ int32_t hard_threshold;
+ int32_t soft_threshold;
+} rbl_filter_t;
+
+static rbl_filter_t *rbl_filter_new(void)
+{
+ return p_new(rbl_filter_t, 1);
+}
+
+static void rbl_filter_delete(rbl_filter_t **rbl)
+{
+ if (*rbl) {
+ array_deep_wipe((*rbl)->rbls, rbldb_delete);
+ array_wipe((*rbl)->weights);
+ p_delete(rbl);
+ }
+}
+
+
+static bool rbl_filter_constructor(filter_t *filter)
+{
+ rbl_filter_t *data = rbl_filter_new();
+
+#define PARSE_CHECK(Expr, Str, ...) \
+ if (!(Expr)) { \
+ err(Str, ##__VA_ARGS__); \
+ rbl_filter_delete(&data); \
+ return false; \
+ }
+
+ data->hard_threshold = 1;
+ data->soft_threshold = 1;
+ foreach (filter_param_t *param, filter->params) {
+ switch (param->type) {
+ /* file parameter is:
+ * [no]lock:weight:filename
+ * valid options are:
+ * - lock: memlock the database in memory.
+ * - nolock: don't memlock the database in memory [default].
+ * - \d+: a number describing the weight to give to the match
+ * the given list [mandatory]
+ * the file pointed by filename MUST be a valid ip list issued from
+ * the rsync (or equivalent) service of a (r)bl.
+ */
+ case ATK_FILE: case ATK_RBLDNS: {
+ bool lock = false;
+ int weight = 0;
+ rbldb_t *rbl = NULL;
+ const char *current = param->value;
+ const char *p = m_strchrnul(param->value, ':');
+ char *next = NULL;
+ for (int i = 0 ; i < 3 ; ++i) {
+ PARSE_CHECK(i == 2 || *p,
+ "file parameter must contains a locking state "
+ "and a weight option");
+ switch (i) {
+ case 0:
+ if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
+ lock = true;
+ } else if ((p - current) == 6
+ && strncmp(current, "nolock", 6) == 0) {
+ lock = false;
+ } else {
+ PARSE_CHECK(false, "illegal locking state %.*s",
+ p - current, current);
+ }
+ break;
+
+ case 1:
+ weight = strtol(current, &next, 10);
+ PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
+ "illegal weight value %.*s",
+ (p - current), current);
+ break;
+
+ case 2:
+ rbl = rbldb_create(current, lock);
+ PARSE_CHECK(rbl != NULL,
+ "cannot load rbl db from %s", current);
+ array_add(data->rbls, rbl);
+ array_add(data->weights, weight);
+ break;
+ }
+ if (i != 2) {
+ current = p + 1;
+ p = m_strchrnul(current, ':');
+ }
+ }
+ } break;
+
+ /* hard_threshold parameter is an integer.
+ * If the matching score is greater or equal than this threshold,
+ * the hook "hard_match" is called.
+ * hard_threshold = 1 means, that all matches are hard matches.
+ * default is 1;
+ */
+ FILTER_PARAM_PARSE_INT(HARD_THRESHOLD, data->hard_threshold);
+
+ /* soft_threshold parameter is an integer.
+ * if the matching score is greater or equal than this threshold
+ * and smaller or equal than the hard_threshold, the hook "soft_match"
+ * is called.
+ * default is 1;
+ */
+ FILTER_PARAM_PARSE_INT(SOFT_THRESHOLD, data->soft_threshold);
+
+ default: break;
+ }
+ }}
+
+ PARSE_CHECK(data->rbls.len,
+ "no file parameter in the filter %s", filter->name);
+ filter->data = data;
+ return true;
+}
+
+static void rbl_filter_destructor(filter_t *filter)
+{
+ rbl_filter_t *data = filter->data;
+ rbl_filter_delete(&data);
+ filter->data = data;
+}
+
+static filter_result_t rbl_filter(const filter_t *filter, const query_t *query)
+{
+ uint32_t ip;
+ int32_t sum = 0;
+ const char *end = NULL;
+ const rbl_filter_t *data = filter->data;
+
+ if (parse_ipv4(query->client_address, &end, &ip) != 0) {
+ warn("invalid client address: %s, expected ipv4",
+ query->client_address);
+ return HTK_ERROR;
+ }
+ for (uint32_t i = 0 ; i < data->rbls.len ; ++i) {
+ const rbldb_t *rbl = array_elt(data->rbls, i);
+ int weight = array_elt(data->weights, i);
+ if (rbldb_ipv4_lookup(rbl, ip)) {
+ sum += weight;
+ }
+ }
+ if (sum >= data->hard_threshold) {
+ return HTK_HARD_MATCH;
+ } else if (sum >= data->soft_threshold) {
+ return HTK_SOFT_MATCH;
+ } else {
+ return HTK_FAIL;
+ }
+}
+
+static int rbl_init(void)
+{
+ filter_type_t type = filter_register("iplist", rbl_filter_constructor,
+ rbl_filter_destructor, rbl_filter);
+ /* Hooks.
+ */
+ (void)filter_hook_register(type, "abort");
+ (void)filter_hook_register(type, "error");
+ (void)filter_hook_register(type, "fail");
+ (void)filter_hook_register(type, "hard_match");
+ (void)filter_hook_register(type, "soft_match");
+
+ /* Parameters.
+ */
+ (void)filter_param_register(type, "file");
+ (void)filter_param_register(type, "rbldns");
+ (void)filter_param_register(type, "hard_threshold");
+ (void)filter_param_register(type, "soft_threshold");
+ return 0;
+}
+module_init(rbl_init);