2007-01-25 07:40:21 +01:00
|
|
|
/*
|
|
|
|
* charybdis: A slightly useful ircd.
|
|
|
|
* blacklist.c: Manages DNS blacklist entries and lookups
|
|
|
|
*
|
2011-02-27 22:38:05 +01:00
|
|
|
* Copyright (C) 2006-2011 charybdis development team
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "res.h"
|
|
|
|
#include "numeric.h"
|
|
|
|
#include "reject.h"
|
|
|
|
#include "s_conf.h"
|
|
|
|
#include "s_user.h"
|
|
|
|
#include "blacklist.h"
|
2013-09-10 07:35:56 +02:00
|
|
|
#include "send.h"
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlink_list blacklist_list = { NULL, NULL, 0 };
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
/* private interfaces */
|
|
|
|
static struct Blacklist *find_blacklist(char *name)
|
|
|
|
{
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlink_node *nptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
RB_DLINK_FOREACH(nptr, blacklist_list.head)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
struct Blacklist *blptr = (struct Blacklist *) nptr->data;
|
|
|
|
|
|
|
|
if (!irccmp(blptr->host, name))
|
|
|
|
return blptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-20 23:17:29 +02:00
|
|
|
static inline int blacklist_check_reply(struct BlacklistClient *blcptr, struct rb_sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
struct Blacklist *blptr = blcptr->blacklist;
|
|
|
|
char ipaddr[HOSTIPLEN];
|
|
|
|
char *lastoctet;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
/* XXX the below two checks might want to change at some point
|
|
|
|
* e.g. if IPv6 blacklists don't use 127.x.y.z or A records anymore
|
|
|
|
* --Elizabeth
|
|
|
|
*/
|
|
|
|
if (addr->ss_family != AF_INET ||
|
|
|
|
memcmp(&((struct sockaddr_in *)addr)->sin_addr, "\177", 1))
|
|
|
|
goto blwarn;
|
|
|
|
|
|
|
|
/* No filters and entry found - thus positive match */
|
|
|
|
if (!rb_dlink_list_length(&blptr->filters))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
rb_inet_ntop_sock((struct sockaddr *)addr, ipaddr, sizeof(ipaddr));
|
|
|
|
|
|
|
|
/* Below will prolly have to change too if the above changes */
|
|
|
|
if ((lastoctet = strrchr(ipaddr, '.')) == NULL || *(++lastoctet) == '\0')
|
|
|
|
goto blwarn;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, blcptr->blacklist->filters.head)
|
|
|
|
{
|
|
|
|
struct BlacklistFilter *filter = ptr->data;
|
|
|
|
char *cmpstr;
|
|
|
|
|
|
|
|
if (filter->type == BLACKLIST_FILTER_ALL)
|
|
|
|
cmpstr = ipaddr;
|
|
|
|
else if (filter->type == BLACKLIST_FILTER_LAST)
|
|
|
|
cmpstr = lastoctet;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"blacklist_check_reply(): Unknown filtertype (BUG!)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cmpstr, filter->filterstr) == 0)
|
|
|
|
/* Match! */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
blwarn:
|
|
|
|
if (blcptr->blacklist->lastwarning + 3600 < rb_current_time())
|
|
|
|
{
|
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"Garbage reply from blacklist %s",
|
|
|
|
blcptr->blacklist->host);
|
|
|
|
blcptr->blacklist->lastwarning = rb_current_time();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
|
|
|
|
{
|
|
|
|
struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
|
2008-01-02 22:32:09 +01:00
|
|
|
int listed = 0;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
if (blcptr == NULL || blcptr->client_p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (blcptr->client_p->preClient == NULL)
|
|
|
|
{
|
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr->client_p, HIDE_IP));
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_free(blcptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-02 22:32:09 +01:00
|
|
|
if (reply != NULL)
|
|
|
|
{
|
2013-04-20 23:17:29 +02:00
|
|
|
if (blacklist_check_reply(blcptr, &reply->addr))
|
2008-01-05 00:38:23 +01:00
|
|
|
listed = TRUE;
|
2008-01-02 22:32:09 +01:00
|
|
|
}
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
/* they have a blacklist entry for this client */
|
2008-01-02 22:32:09 +01:00
|
|
|
if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist;
|
|
|
|
/* reference to blacklist moves from blcptr to client_p->preClient... */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
unref_blacklist(blcptr->blacklist);
|
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
/* yes, it can probably happen... */
|
2008-04-01 22:38:40 +02:00
|
|
|
if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name))
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
char buf[USERLEN + 1];
|
2008-04-20 06:40:40 +02:00
|
|
|
rb_strlcpy(buf, blcptr->client_p->username, sizeof buf);
|
2007-01-25 07:40:21 +01:00
|
|
|
register_local_user(blcptr->client_p, blcptr->client_p, buf);
|
|
|
|
}
|
|
|
|
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_free(blcptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p)
|
|
|
|
{
|
2008-04-02 01:07:29 +02:00
|
|
|
struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient));
|
2008-11-15 16:13:39 +01:00
|
|
|
char buf[IRCD_RES_HOSTLEN + 1];
|
2010-12-05 06:11:04 +01:00
|
|
|
uint8_t *ip;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
blcptr->blacklist = blptr;
|
|
|
|
blcptr->client_p = client_p;
|
|
|
|
|
|
|
|
blcptr->dns_query.ptr = blcptr;
|
|
|
|
blcptr->dns_query.callback = blacklist_dns_callback;
|
|
|
|
|
2011-02-27 22:38:05 +01:00
|
|
|
if ((client_p->localClient->ip.ss_family == AF_INET) && blptr->ipv4)
|
|
|
|
{
|
|
|
|
ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr;
|
|
|
|
|
|
|
|
/* becomes 2.0.0.127.torbl.ahbl.org or whatever */
|
|
|
|
rb_snprintf(buf, sizeof buf, "%d.%d.%d.%d.%s",
|
|
|
|
(unsigned int) ip[3],
|
|
|
|
(unsigned int) ip[2],
|
|
|
|
(unsigned int) ip[1],
|
|
|
|
(unsigned int) ip[0],
|
|
|
|
blptr->host);
|
|
|
|
}
|
|
|
|
/* IPv6 is supported now. --Elizabeth */
|
|
|
|
else if ((client_p->localClient->ip.ss_family == AF_INET6) && blptr->ipv6)
|
|
|
|
{
|
|
|
|
/* Breaks it into ip[0] = 0x00, ip[1] = 0x00... ip[16] = 0x01 for localhost
|
|
|
|
* I wish there was a uint4_t for C, this would make the below cleaner, but... */
|
|
|
|
ip = (uint8_t *)&((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_addr.s6_addr;
|
|
|
|
char *bufptr = buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* The below will give us something like
|
|
|
|
* 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.foobl.invalid
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Going backwards */
|
|
|
|
for (i = 15; i >= 0; i--)
|
|
|
|
{
|
|
|
|
/* Get upper and lower nibbles (yes this works fine on
|
|
|
|
* both big and little endian) */
|
|
|
|
uint8_t hi = (ip[i] >> 4) & 0x0F;
|
|
|
|
uint8_t lo = ip[i] & 0x0F;
|
|
|
|
|
|
|
|
/* One part... (why 5? rb_snprintf adds \0) */
|
|
|
|
rb_snprintf(bufptr, 5, "%1x.%1x.",
|
|
|
|
(unsigned int) lo, /* Remember, backwards */
|
|
|
|
(unsigned int) hi);
|
|
|
|
|
|
|
|
/* Lurch forward to next position */
|
|
|
|
bufptr += 4;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2011-02-27 22:38:05 +01:00
|
|
|
/* Tack host on */
|
|
|
|
strcpy(bufptr, blptr->host);
|
|
|
|
}
|
|
|
|
/* This shouldn't happen... */
|
|
|
|
else
|
|
|
|
return;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
gethost_byname_type(buf, &blcptr->dns_query, T_A);
|
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
|
2007-01-25 07:40:21 +01:00
|
|
|
blptr->refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public interfaces */
|
2013-04-20 23:17:29 +02:00
|
|
|
struct Blacklist *new_blacklist(char *name, char *reject_reason, int ipv4, int ipv6, rb_dlink_list *filters)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
struct Blacklist *blptr;
|
|
|
|
|
|
|
|
if (name == NULL || reject_reason == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
blptr = find_blacklist(name);
|
|
|
|
if (blptr == NULL)
|
|
|
|
{
|
2008-04-02 01:07:29 +02:00
|
|
|
blptr = rb_malloc(sizeof(struct Blacklist));
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkAddAlloc(blptr, &blacklist_list);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
blptr->status &= ~CONF_ILLEGAL;
|
2013-04-20 23:17:29 +02:00
|
|
|
|
2008-11-15 16:13:39 +01:00
|
|
|
rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
|
2008-04-20 06:40:40 +02:00
|
|
|
rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
|
2011-02-27 22:38:05 +01:00
|
|
|
blptr->ipv4 = ipv4;
|
|
|
|
blptr->ipv6 = ipv6;
|
2013-04-20 23:17:29 +02:00
|
|
|
|
|
|
|
rb_dlinkMoveList(filters, &blptr->filters);
|
|
|
|
|
2008-01-05 00:38:23 +01:00
|
|
|
blptr->lastwarning = 0;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
return blptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unref_blacklist(struct Blacklist *blptr)
|
|
|
|
{
|
2013-04-20 23:17:29 +02:00
|
|
|
rb_dlink_node *ptr, *next_ptr;
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
blptr->refcount--;
|
|
|
|
if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
|
|
|
|
{
|
2013-04-20 23:17:29 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blptr->filters.head)
|
|
|
|
{
|
|
|
|
rb_free(ptr);
|
|
|
|
rb_dlinkDelete(ptr, &blptr->filters);
|
|
|
|
}
|
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkFindDestroy(blptr, &blacklist_list);
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_free(blptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lookup_blacklists(struct Client *client_p)
|
|
|
|
{
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlink_node *nptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-01 22:38:40 +02:00
|
|
|
RB_DLINK_FOREACH(nptr, blacklist_list.head)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
struct Blacklist *blptr = (struct Blacklist *) nptr->data;
|
|
|
|
|
|
|
|
if (!(blptr->status & CONF_ILLEGAL))
|
|
|
|
initiate_blacklist_dnsquery(blptr, client_p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void abort_blacklist_queries(struct Client *client_p)
|
|
|
|
{
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_dlink_node *ptr, *next_ptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
struct BlacklistClient *blcptr;
|
|
|
|
|
|
|
|
if (client_p->preClient == NULL)
|
|
|
|
return;
|
2008-04-02 00:47:17 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
blcptr = ptr->data;
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
|
2007-01-25 07:40:21 +01:00
|
|
|
unref_blacklist(blcptr->blacklist);
|
|
|
|
delete_resolver_queries(&blcptr->dns_query);
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_free(blcptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_blacklists(void)
|
|
|
|
{
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_dlink_node *ptr, *next_ptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
struct Blacklist *blptr;
|
|
|
|
|
2008-04-02 00:47:17 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
|
|
|
blptr = ptr->data;
|
|
|
|
blptr->hits = 0; /* keep it simple and consistent */
|
|
|
|
if (blptr->refcount > 0)
|
|
|
|
blptr->status |= CONF_ILLEGAL;
|
|
|
|
else
|
|
|
|
{
|
2008-04-02 00:47:17 +02:00
|
|
|
rb_free(ptr->data);
|
2008-04-01 22:38:40 +02:00
|
|
|
rb_dlinkDestroy(ptr, &blacklist_list);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|