2016-03-26 02:07:36 +01:00
|
|
|
/*
|
|
|
|
* charybdis: A slightly useful ircd.
|
|
|
|
* blacklist.c: Manages DNS blacklist entries and lookups
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2011 charybdis development team
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Originally written for charybdis circa 2006 (by nenolod?).
|
|
|
|
* Tweaked for authd. Some functions and structs renamed. Public/private
|
|
|
|
* interfaces have been shifted around. Some code has been cleaned up too.
|
|
|
|
* -- Elizafox 24 March 2016
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "authd.h"
|
2016-04-07 11:47:48 +02:00
|
|
|
#include "defaults.h"
|
2016-03-26 02:07:36 +01:00
|
|
|
#include "provider.h"
|
2016-03-26 03:29:44 +01:00
|
|
|
#include "notice.h"
|
2016-03-26 02:07:36 +01:00
|
|
|
#include "stdinc.h"
|
|
|
|
#include "dns.h"
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
#define SELF_PID (blacklist_provider.id)
|
|
|
|
|
2016-03-26 02:07:36 +01:00
|
|
|
typedef enum filter_t
|
|
|
|
{
|
|
|
|
FILTER_ALL = 1,
|
|
|
|
FILTER_LAST = 2,
|
|
|
|
} filter_t;
|
|
|
|
|
|
|
|
/* Blacklist accepted IP types */
|
|
|
|
#define IPTYPE_IPV4 1
|
|
|
|
#define IPTYPE_IPV6 2
|
|
|
|
|
|
|
|
/* A configured DNSBL */
|
|
|
|
struct blacklist
|
|
|
|
{
|
|
|
|
char host[IRCD_RES_HOSTLEN + 1];
|
|
|
|
char reason[BUFSIZE]; /* Reason template (ircd fills in the blanks) */
|
2016-03-27 21:41:50 +02:00
|
|
|
uint8_t iptype; /* IP types supported */
|
2016-03-26 02:07:36 +01:00
|
|
|
rb_dlink_list filters; /* Filters for queries */
|
|
|
|
|
|
|
|
bool delete; /* If true delete when no clients */
|
|
|
|
int refcount; /* When 0 and delete is set, remove this blacklist */
|
2016-03-28 08:05:19 +02:00
|
|
|
unsigned int hits;
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
time_t lastwarning; /* Last warning about garbage replies sent */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A lookup in progress for a particular DNSBL for a particular client */
|
|
|
|
struct blacklist_lookup
|
|
|
|
{
|
|
|
|
struct blacklist *bl; /* Blacklist we're checking */
|
|
|
|
struct auth_client *auth; /* Client */
|
|
|
|
struct dns_query *query; /* DNS query pointer */
|
2016-03-27 06:15:28 +02:00
|
|
|
|
2016-03-26 02:07:36 +01:00
|
|
|
rb_dlink_node node;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A blacklist filter */
|
|
|
|
struct blacklist_filter
|
|
|
|
{
|
|
|
|
filter_t type; /* Type of filter */
|
2016-03-28 08:05:19 +02:00
|
|
|
char filter[HOSTIPLEN]; /* The filter itself */
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
rb_dlink_node node;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Blacklist user data attached to auth_client instance */
|
|
|
|
struct blacklist_user
|
|
|
|
{
|
|
|
|
rb_dlink_list queries; /* Blacklist queries in flight */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* public interfaces */
|
|
|
|
static void blacklists_destroy(void);
|
|
|
|
|
|
|
|
static bool blacklists_start(struct auth_client *);
|
|
|
|
static void blacklists_cancel(struct auth_client *);
|
|
|
|
|
|
|
|
/* private interfaces */
|
|
|
|
static void unref_blacklist(struct blacklist *);
|
2016-03-27 21:41:50 +02:00
|
|
|
static struct blacklist *new_blacklist(const char *, const char *, uint8_t, rb_dlink_list *);
|
2016-03-26 21:27:57 +01:00
|
|
|
static struct blacklist *find_blacklist(const char *);
|
2016-03-26 02:07:36 +01:00
|
|
|
static bool blacklist_check_reply(struct blacklist_lookup *, const char *);
|
|
|
|
static void blacklist_dns_callback(const char *, bool, query_type, void *);
|
|
|
|
static void initiate_blacklist_dnsquery(struct blacklist *, struct auth_client *);
|
|
|
|
|
|
|
|
/* Variables */
|
|
|
|
static rb_dlink_list blacklist_list = { NULL, NULL, 0 };
|
2016-04-07 11:47:48 +02:00
|
|
|
static int blacklist_timeout = BLACKLIST_TIMEOUT_DEFAULT;
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
/* private interfaces */
|
|
|
|
|
|
|
|
static void
|
|
|
|
unref_blacklist(struct blacklist *bl)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
|
|
|
|
|
|
|
bl->refcount--;
|
|
|
|
if (bl->delete && bl->refcount <= 0)
|
|
|
|
{
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, bl->filters.head)
|
|
|
|
{
|
|
|
|
rb_dlinkDelete(ptr, &bl->filters);
|
|
|
|
rb_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_dlinkFindDestroy(bl, &blacklist_list);
|
|
|
|
rb_free(bl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct blacklist *
|
2016-03-27 21:41:50 +02:00
|
|
|
new_blacklist(const char *name, const char *reason, uint8_t iptype, rb_dlink_list *filters)
|
2016-03-26 02:07:36 +01:00
|
|
|
{
|
|
|
|
struct blacklist *bl;
|
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
if (name == NULL || reason == NULL || iptype == 0)
|
2016-03-26 02:07:36 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if((bl = find_blacklist(name)) == NULL)
|
|
|
|
{
|
|
|
|
bl = rb_malloc(sizeof(struct blacklist));
|
|
|
|
rb_dlinkAddAlloc(bl, &blacklist_list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bl->delete = false;
|
|
|
|
|
|
|
|
rb_strlcpy(bl->host, name, IRCD_RES_HOSTLEN + 1);
|
|
|
|
rb_strlcpy(bl->reason, reason, BUFSIZE);
|
2016-03-26 21:27:57 +01:00
|
|
|
bl->iptype = iptype;
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
rb_dlinkMoveList(filters, &bl->filters);
|
|
|
|
|
|
|
|
bl->lastwarning = 0;
|
|
|
|
|
|
|
|
return bl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct blacklist *
|
2016-03-26 21:27:57 +01:00
|
|
|
find_blacklist(const char *name)
|
2016-03-26 02:07:36 +01:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, blacklist_list.head)
|
|
|
|
{
|
|
|
|
struct blacklist *bl = (struct blacklist *)ptr->data;
|
|
|
|
|
|
|
|
if (!strcasecmp(bl->host, name))
|
|
|
|
return bl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
blacklist_check_reply(struct blacklist_lookup *bllookup, const char *ipaddr)
|
|
|
|
{
|
|
|
|
struct blacklist *bl = bllookup->bl;
|
|
|
|
const char *lastoctet;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
/* No filters and entry found - thus positive match */
|
|
|
|
if (!rb_dlink_list_length(&bl->filters))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Below will prolly have to change if IPv6 address replies are sent back */
|
|
|
|
if ((lastoctet = strrchr(ipaddr, '.')) == NULL || *(++lastoctet) == '\0')
|
|
|
|
goto blwarn;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, bl->filters.head)
|
|
|
|
{
|
|
|
|
struct blacklist_filter *filter = ptr->data;
|
|
|
|
const char *cmpstr;
|
|
|
|
|
|
|
|
if (filter->type == FILTER_ALL)
|
|
|
|
cmpstr = ipaddr;
|
|
|
|
else if (filter->type == FILTER_LAST)
|
|
|
|
cmpstr = lastoctet;
|
|
|
|
else
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: Unknown blacklist filter type (host %s): %d",
|
2016-03-26 02:07:36 +01:00
|
|
|
bl->host, filter->type);
|
2016-03-29 02:22:02 +02:00
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
if (strcmp(cmpstr, filter->filter) == 0)
|
2016-03-26 02:07:36 +01:00
|
|
|
/* Match! */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
blwarn:
|
|
|
|
if (bl->lastwarning + 3600 < rb_current_time())
|
|
|
|
{
|
|
|
|
warn_opers(L_WARN, "Garbage/undecipherable reply received from blacklist %s (reply %s)",
|
|
|
|
bl->host, ipaddr);
|
|
|
|
bl->lastwarning = rb_current_time();
|
|
|
|
}
|
2016-03-30 06:22:13 +02:00
|
|
|
|
2016-03-26 02:07:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blacklist_dns_callback(const char *result, bool status, query_type type, void *data)
|
|
|
|
{
|
|
|
|
struct blacklist_lookup *bllookup = (struct blacklist_lookup *)data;
|
|
|
|
struct blacklist_user *bluser;
|
2016-03-27 06:15:28 +02:00
|
|
|
struct blacklist *bl;
|
2016-03-26 02:07:36 +01:00
|
|
|
struct auth_client *auth;
|
|
|
|
|
2016-04-04 10:49:34 +02:00
|
|
|
lrb_assert(bllookup != NULL);
|
|
|
|
lrb_assert(bllookup->auth != NULL);
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-03-27 06:15:28 +02:00
|
|
|
bl = bllookup->bl;
|
2016-03-26 02:07:36 +01:00
|
|
|
auth = bllookup->auth;
|
2016-04-04 10:49:34 +02:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
if((bluser = get_provider_data(auth, SELF_PID)) == NULL)
|
2016-03-26 02:07:36 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (result != NULL && status && blacklist_check_reply(bllookup, result))
|
|
|
|
{
|
|
|
|
/* Match found, so proceed no further */
|
2016-03-28 08:05:19 +02:00
|
|
|
bl->hits++;
|
2016-03-26 02:07:36 +01:00
|
|
|
blacklists_cancel(auth);
|
2016-04-05 11:31:22 +02:00
|
|
|
reject_client(auth, SELF_PID, bl->host, bl->reason);
|
2016-03-26 02:07:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-27 06:15:28 +02:00
|
|
|
unref_blacklist(bl);
|
|
|
|
cancel_query(bllookup->query); /* Ignore future responses */
|
2016-03-26 02:07:36 +01:00
|
|
|
rb_dlinkDelete(&bllookup->node, &bluser->queries);
|
|
|
|
rb_free(bllookup);
|
|
|
|
|
|
|
|
if(!rb_dlink_list_length(&bluser->queries))
|
|
|
|
{
|
|
|
|
/* Done here */
|
2016-03-30 06:22:13 +02:00
|
|
|
notice_client(auth->cid, "*** IP not found in DNS blacklist%s",
|
2016-03-30 06:33:13 +02:00
|
|
|
rb_dlink_list_length(&blacklist_list) > 1 ? "s" : "");
|
2016-03-26 02:07:36 +01:00
|
|
|
rb_free(bluser);
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
|
|
|
provider_done(auth, SELF_PID);
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initiate_blacklist_dnsquery(struct blacklist *bl, struct auth_client *auth)
|
|
|
|
{
|
|
|
|
struct blacklist_lookup *bllookup = rb_malloc(sizeof(struct blacklist_lookup));
|
2016-04-05 11:31:22 +02:00
|
|
|
struct blacklist_user *bluser = get_provider_data(auth, SELF_PID);
|
2016-03-26 02:07:36 +01:00
|
|
|
char buf[IRCD_RES_HOSTLEN + 1];
|
|
|
|
int aftype;
|
|
|
|
|
|
|
|
bllookup->bl = bl;
|
|
|
|
bllookup->auth = auth;
|
|
|
|
|
|
|
|
aftype = GET_SS_FAMILY(&auth->c_addr);
|
|
|
|
if((aftype == AF_INET && (bl->iptype & IPTYPE_IPV4) == 0) ||
|
|
|
|
(aftype == AF_INET6 && (bl->iptype & IPTYPE_IPV6) == 0))
|
|
|
|
/* Incorrect blacklist type for this IP... */
|
2016-04-04 10:49:34 +02:00
|
|
|
{
|
|
|
|
rb_free(bllookup);
|
2016-03-26 02:07:36 +01:00
|
|
|
return;
|
2016-04-04 10:49:34 +02:00
|
|
|
}
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
build_rdns(buf, sizeof(buf), &auth->c_addr, bl->host);
|
|
|
|
|
|
|
|
bllookup->query = lookup_ip(buf, AF_INET, blacklist_dns_callback, bllookup);
|
|
|
|
|
|
|
|
rb_dlinkAdd(bllookup, &bllookup->node, &bluser->queries);
|
|
|
|
bl->refcount++;
|
|
|
|
}
|
|
|
|
|
2016-03-26 02:46:58 +01:00
|
|
|
static inline void
|
|
|
|
lookup_all_blacklists(struct auth_client *auth)
|
|
|
|
{
|
2016-04-05 11:31:22 +02:00
|
|
|
struct blacklist_user *bluser = get_provider_data(auth, SELF_PID);
|
2016-03-26 02:46:58 +01:00
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
2016-03-30 06:22:13 +02:00
|
|
|
notice_client(auth->cid, "*** Checking your IP against DNS blacklist%s",
|
2016-03-30 06:29:58 +02:00
|
|
|
rb_dlink_list_length(&blacklist_list) > 1 ? "s" : "");
|
2016-03-30 06:22:13 +02:00
|
|
|
|
2016-03-26 02:46:58 +01:00
|
|
|
RB_DLINK_FOREACH(ptr, blacklist_list.head)
|
|
|
|
{
|
|
|
|
struct blacklist *bl = (struct blacklist *)ptr->data;
|
|
|
|
|
|
|
|
if (!bl->delete)
|
|
|
|
initiate_blacklist_dnsquery(bl, auth);
|
|
|
|
}
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_timeout_relative(auth, SELF_PID, blacklist_timeout);
|
2016-03-26 02:46:58 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 22:36:14 +01:00
|
|
|
static inline void
|
|
|
|
delete_blacklist(struct blacklist *bl)
|
|
|
|
{
|
|
|
|
if (bl->refcount > 0)
|
|
|
|
bl->delete = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_dlinkFindDestroy(bl, &blacklist_list);
|
|
|
|
rb_free(bl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_all_blacklists(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, blacklist_list.head)
|
|
|
|
{
|
|
|
|
delete_blacklist(ptr->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 02:07:36 +01:00
|
|
|
/* public interfaces */
|
|
|
|
static bool
|
|
|
|
blacklists_start(struct auth_client *auth)
|
|
|
|
{
|
2016-04-05 11:31:22 +02:00
|
|
|
uint32_t rdns_pid, ident_pid;
|
|
|
|
|
|
|
|
lrb_assert(get_provider_data(auth, SELF_PID) == NULL);
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
if(!rb_dlink_list_length(&blacklist_list))
|
|
|
|
/* Nothing to do... */
|
|
|
|
return true;
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, rb_malloc(sizeof(struct blacklist_user)));
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
if((!get_provider_id("rdns", &rdns_pid) || is_provider_done(auth, rdns_pid)) &&
|
|
|
|
(!get_provider_id("ident", &ident_pid) || is_provider_done(auth, ident_pid)))
|
|
|
|
{
|
|
|
|
/* Start the lookup if ident and rdns are finished, or not loaded. */
|
2016-03-26 02:46:58 +01:00
|
|
|
lookup_all_blacklists(auth);
|
2016-04-05 11:31:22 +02:00
|
|
|
}
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_running(auth, SELF_PID);
|
2016-03-26 02:46:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-03-26 02:55:10 +01:00
|
|
|
/* This is called every time a provider is completed as long as we are marked not done */
|
2016-03-26 02:46:58 +01:00
|
|
|
static void
|
2016-04-05 11:31:22 +02:00
|
|
|
blacklists_initiate(struct auth_client *auth, uint32_t provider)
|
2016-03-26 02:46:58 +01:00
|
|
|
{
|
2016-04-05 11:31:22 +02:00
|
|
|
struct blacklist_user *bluser = get_provider_data(auth, SELF_PID);
|
|
|
|
uint32_t rdns_pid, ident_pid;
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
lrb_assert(provider != SELF_PID);
|
|
|
|
lrb_assert(!is_provider_done(auth, SELF_PID));
|
2016-03-26 02:55:10 +01:00
|
|
|
lrb_assert(rb_dlink_list_length(&blacklist_list) > 0);
|
|
|
|
|
|
|
|
if(bluser == NULL || rb_dlink_list_length(&bluser->queries))
|
2016-03-26 02:46:58 +01:00
|
|
|
/* Nothing to do */
|
|
|
|
return;
|
2016-04-05 11:31:22 +02:00
|
|
|
else if((!get_provider_id("rdns", &rdns_pid) || is_provider_done(auth, rdns_pid)) &&
|
|
|
|
(!get_provider_id("ident", &ident_pid) || is_provider_done(auth, ident_pid)))
|
|
|
|
/* Don't start until ident and rdns are finished (or not loaded) */
|
2016-03-26 02:46:58 +01:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
lookup_all_blacklists(auth);
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blacklists_cancel(struct auth_client *auth)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
2016-04-05 11:31:22 +02:00
|
|
|
struct blacklist_user *bluser = get_provider_data(auth, SELF_PID);
|
2016-03-26 02:07:36 +01:00
|
|
|
|
|
|
|
if(bluser == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-03-30 06:29:58 +02:00
|
|
|
if(rb_dlink_list_length(&bluser->queries))
|
2016-03-26 02:07:36 +01:00
|
|
|
{
|
2016-03-30 06:22:13 +02:00
|
|
|
notice_client(auth->cid, "*** Aborting DNS blacklist queries");
|
2016-03-27 06:15:28 +02:00
|
|
|
|
2016-03-30 06:22:13 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, bluser->queries.head)
|
|
|
|
{
|
|
|
|
struct blacklist_lookup *bllookup = ptr->data;
|
|
|
|
|
|
|
|
cancel_query(bllookup->query);
|
|
|
|
unref_blacklist(bllookup->bl);
|
2016-03-27 06:15:28 +02:00
|
|
|
|
2016-03-30 06:22:13 +02:00
|
|
|
rb_dlinkDelete(&bllookup->node, &bluser->queries);
|
|
|
|
rb_free(bllookup);
|
|
|
|
}
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_free(bluser);
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
|
|
|
provider_done(auth, SELF_PID);
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blacklists_destroy(void)
|
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dictionary_iter iter;
|
|
|
|
struct auth_client *auth;
|
2016-03-26 02:07:36 +01:00
|
|
|
|
2016-05-01 02:20:12 +02:00
|
|
|
RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
|
2016-03-26 02:07:36 +01:00
|
|
|
{
|
|
|
|
blacklists_cancel(auth);
|
|
|
|
}
|
|
|
|
|
2016-03-26 22:36:14 +01:00
|
|
|
delete_all_blacklists();
|
2016-03-26 02:07:36 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
static void
|
|
|
|
add_conf_blacklist(const char *key, int parc, const char **parv)
|
|
|
|
{
|
2016-03-27 01:50:09 +01:00
|
|
|
rb_dlink_list filters = { NULL, NULL, 0 };
|
2016-03-26 21:27:57 +01:00
|
|
|
char *tmp, *elemlist = rb_strdup(parv[2]);
|
2016-03-27 21:41:50 +02:00
|
|
|
uint8_t iptype;
|
2016-03-26 21:27:57 +01:00
|
|
|
|
2016-03-27 01:50:09 +01:00
|
|
|
if(*elemlist == '*')
|
|
|
|
goto end;
|
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
for(char *elem = rb_strtok_r(elemlist, ",", &tmp); elem; elem = rb_strtok_r(NULL, ",", &tmp))
|
|
|
|
{
|
|
|
|
struct blacklist_filter *filter = rb_malloc(sizeof(struct blacklist_filter));
|
|
|
|
int dot_c = 0;
|
|
|
|
filter_t type = FILTER_LAST;
|
|
|
|
|
|
|
|
/* Check blacklist filter type and for validity */
|
|
|
|
for(char *c = elem; *c != '\0'; c++)
|
|
|
|
{
|
|
|
|
if(*c == '.')
|
|
|
|
{
|
|
|
|
if(++dot_c > 3)
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: addr_conf_blacklist got a bad filter (too many octets)");
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type = FILTER_ALL;
|
|
|
|
}
|
|
|
|
else if(!isdigit(*c))
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: addr_conf_blacklist got a bad filter (invalid character in blacklist filter: %c)",
|
|
|
|
*c);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 02:22:02 +02:00
|
|
|
if(dot_c > 0 && dot_c < 3)
|
2016-03-26 21:27:57 +01:00
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: addr_conf_blacklist got a bad filter (insufficient octets)");
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
filter->type = type;
|
|
|
|
rb_strlcpy(filter->filter, elem, sizeof(filter->filter));
|
|
|
|
rb_dlinkAdd(filter, &filter->node, &filters);
|
|
|
|
}
|
|
|
|
|
2016-03-27 01:50:09 +01:00
|
|
|
end:
|
2016-03-26 21:27:57 +01:00
|
|
|
rb_free(elemlist);
|
|
|
|
|
|
|
|
iptype = atoi(parv[1]) & 0x3;
|
|
|
|
if(new_blacklist(parv[0], parv[3], iptype, &filters) == NULL)
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: addr_conf_blacklist got a malformed blacklist");
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 22:36:14 +01:00
|
|
|
static void
|
|
|
|
del_conf_blacklist(const char *key, int parc, const char **parv)
|
|
|
|
{
|
|
|
|
struct blacklist *bl = find_blacklist(parv[0]);
|
|
|
|
if(bl == NULL)
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
/* Not fatal for now... */
|
|
|
|
warn_opers(L_WARN, "Blacklist: tried to remove nonexistent blacklist %s", parv[0]);
|
2016-03-26 22:36:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_blacklist(bl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
del_conf_blacklist_all(const char *key, int parc, const char **parv)
|
|
|
|
{
|
|
|
|
delete_all_blacklists();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
static void
|
|
|
|
add_conf_blacklist_timeout(const char *key, int parc, const char **parv)
|
|
|
|
{
|
|
|
|
int timeout = atoi(parv[0]);
|
|
|
|
|
|
|
|
if(timeout < 0)
|
|
|
|
{
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "Blacklist: blacklist timeout < 0 (value: %d)", timeout);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
blacklist_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:49:26 +02:00
|
|
|
#if 0
|
2016-03-28 08:05:19 +02:00
|
|
|
static void
|
|
|
|
blacklist_stats(uint32_t rid, char letter)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, blacklist_list.head)
|
|
|
|
{
|
|
|
|
struct blacklist *bl = ptr->data;
|
|
|
|
|
|
|
|
if(bl->delete)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
stats_result(rid, letter, "%s %hhu %u", bl->host, bl->iptype, bl->hits);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats_done(rid, letter);
|
|
|
|
}
|
2016-03-28 23:49:26 +02:00
|
|
|
#endif
|
2016-03-28 08:05:19 +02:00
|
|
|
|
2016-03-26 21:27:57 +01:00
|
|
|
struct auth_opts_handler blacklist_options[] =
|
|
|
|
{
|
|
|
|
{ "rbl", 4, add_conf_blacklist },
|
2016-03-26 22:36:14 +01:00
|
|
|
{ "rbl_del", 1, del_conf_blacklist },
|
|
|
|
{ "rbl_del_all", 0, del_conf_blacklist_all },
|
2016-03-26 21:27:57 +01:00
|
|
|
{ "rbl_timeout", 1, add_conf_blacklist_timeout },
|
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2016-03-26 02:07:36 +01:00
|
|
|
struct auth_provider blacklist_provider =
|
|
|
|
{
|
2016-04-05 11:31:22 +02:00
|
|
|
.name = "blacklist",
|
|
|
|
.letter = 'B',
|
2016-03-26 02:07:36 +01:00
|
|
|
.destroy = blacklists_destroy,
|
|
|
|
.start = blacklists_start,
|
|
|
|
.cancel = blacklists_cancel,
|
2016-03-31 00:20:27 +02:00
|
|
|
.timeout = blacklists_cancel,
|
2016-03-26 02:46:58 +01:00
|
|
|
.completed = blacklists_initiate,
|
2016-03-26 21:27:57 +01:00
|
|
|
.opt_handlers = blacklist_options,
|
2016-03-28 23:49:26 +02:00
|
|
|
/* .stats_handler = { 'B', blacklist_stats }, */
|
2016-03-26 02:07:36 +01:00
|
|
|
};
|