2007-01-25 06:40:21 +00:00
|
|
|
/*
|
|
|
|
* charybdis: A slightly useful ircd.
|
|
|
|
* blacklist.c: Manages DNS blacklist entries and lookups
|
|
|
|
*
|
2008-01-02 21:34:29 +00:00
|
|
|
* Copyright (C) 2006-2008 charybdis development team
|
2007-01-25 06:40:21 +00: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.
|
|
|
|
*
|
|
|
|
* $Id: blacklist.c 2743 2006-11-10 15:15:00Z jilles $
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlink_list blacklist_list = { NULL, NULL, 0 };
|
2007-01-25 06:40:21 +00:00
|
|
|
|
|
|
|
/* private interfaces */
|
|
|
|
static struct Blacklist *find_blacklist(char *name)
|
|
|
|
{
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlink_node *nptr;
|
2007-01-25 06:40:21 +00:00
|
|
|
|
2008-04-01 20:38:40 +00:00
|
|
|
RB_DLINK_FOREACH(nptr, blacklist_list.head)
|
2007-01-25 06:40:21 +00:00
|
|
|
{
|
|
|
|
struct Blacklist *blptr = (struct Blacklist *) nptr->data;
|
|
|
|
|
|
|
|
if (!irccmp(blptr->host, name))
|
|
|
|
return blptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
|
|
|
|
{
|
|
|
|
struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
|
2008-01-02 21:32:09 +00:00
|
|
|
int listed = 0;
|
2007-01-25 06:40:21 +00: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));
|
|
|
|
MyFree(blcptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-02 21:32:09 +00:00
|
|
|
if (reply != NULL)
|
|
|
|
{
|
|
|
|
/* only accept 127.0.0.x as a listing */
|
2008-01-04 23:38:23 +00:00
|
|
|
if (reply->addr.ss_family == AF_INET &&
|
|
|
|
!memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177\0\0", 3))
|
|
|
|
listed = TRUE;
|
|
|
|
else if (blcptr->blacklist->lastwarning + 3600 < CurrentTime)
|
|
|
|
{
|
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"Garbage reply from blacklist %s",
|
|
|
|
blcptr->blacklist->host);
|
|
|
|
blcptr->blacklist->lastwarning = CurrentTime;
|
|
|
|
}
|
2008-01-02 21:32:09 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 06:40:21 +00:00
|
|
|
/* they have a blacklist entry for this client */
|
2008-01-02 21:32:09 +00:00
|
|
|
if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
|
2007-01-25 06:40:21 +00: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 20:38:40 +00:00
|
|
|
rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
|
2007-01-25 06:40:21 +00:00
|
|
|
|
|
|
|
/* yes, it can probably happen... */
|
2008-04-01 20:38:40 +00: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 06:40:21 +00:00
|
|
|
{
|
|
|
|
char buf[USERLEN + 1];
|
|
|
|
strlcpy(buf, blcptr->client_p->username, sizeof buf);
|
|
|
|
register_local_user(blcptr->client_p, blcptr->client_p, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFree(blcptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: no IPv6 implementation, not to concerned right now though. */
|
|
|
|
static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p)
|
|
|
|
{
|
|
|
|
struct BlacklistClient *blcptr = MyMalloc(sizeof(struct BlacklistClient));
|
|
|
|
char buf[IRCD_BUFSIZE];
|
|
|
|
int ip[4];
|
|
|
|
|
|
|
|
blcptr->blacklist = blptr;
|
|
|
|
blcptr->client_p = client_p;
|
|
|
|
|
|
|
|
blcptr->dns_query.ptr = blcptr;
|
|
|
|
blcptr->dns_query.callback = blacklist_dns_callback;
|
|
|
|
|
|
|
|
/* XXX: yes I know this is bad, I don't really care right now */
|
|
|
|
sscanf(client_p->sockhost, "%d.%d.%d.%d", &ip[3], &ip[2], &ip[1], &ip[0]);
|
|
|
|
|
|
|
|
/* becomes 2.0.0.127.torbl.ahbl.org or whatever */
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_snprintf(buf, IRCD_BUFSIZE, "%d.%d.%d.%d.%s", ip[0], ip[1], ip[2], ip[3], blptr->host);
|
2007-01-25 06:40:21 +00:00
|
|
|
|
|
|
|
gethost_byname_type(buf, &blcptr->dns_query, T_A);
|
|
|
|
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
|
2007-01-25 06:40:21 +00:00
|
|
|
blptr->refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public interfaces */
|
|
|
|
struct Blacklist *new_blacklist(char *name, char *reject_reason)
|
|
|
|
{
|
|
|
|
struct Blacklist *blptr;
|
|
|
|
|
|
|
|
if (name == NULL || reject_reason == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
blptr = find_blacklist(name);
|
|
|
|
if (blptr == NULL)
|
|
|
|
{
|
|
|
|
blptr = MyMalloc(sizeof(struct Blacklist));
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlinkAddAlloc(blptr, &blacklist_list);
|
2007-01-25 06:40:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
blptr->status &= ~CONF_ILLEGAL;
|
|
|
|
strlcpy(blptr->host, name, HOSTLEN);
|
|
|
|
strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
|
2008-01-04 23:38:23 +00:00
|
|
|
blptr->lastwarning = 0;
|
2007-01-25 06:40:21 +00:00
|
|
|
|
|
|
|
return blptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unref_blacklist(struct Blacklist *blptr)
|
|
|
|
{
|
|
|
|
blptr->refcount--;
|
|
|
|
if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
|
|
|
|
{
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlinkFindDestroy(blptr, &blacklist_list);
|
2007-01-25 06:40:21 +00:00
|
|
|
MyFree(blptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lookup_blacklists(struct Client *client_p)
|
|
|
|
{
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlink_node *nptr;
|
2007-01-25 06:40:21 +00:00
|
|
|
|
|
|
|
/* We don't do IPv6 right now, sorry! */
|
|
|
|
if (client_p->localClient->ip.ss_family == AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2008-04-01 20:38:40 +00:00
|
|
|
RB_DLINK_FOREACH(nptr, blacklist_list.head)
|
2007-01-25 06:40:21 +00: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-01 22:45:16 +00:00
|
|
|
rb_dlink_node *ptr, *rb_free(;
|
2007-01-25 06:40:21 +00:00
|
|
|
struct BlacklistClient *blcptr;
|
|
|
|
|
|
|
|
if (client_p->preClient == NULL)
|
|
|
|
return;
|
2008-04-01 22:45:16 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, rb_free(, client_p->preClient->dnsbl_queries.head)
|
2007-01-25 06:40:21 +00:00
|
|
|
{
|
|
|
|
blcptr = ptr->data;
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
|
2007-01-25 06:40:21 +00:00
|
|
|
unref_blacklist(blcptr->blacklist);
|
|
|
|
delete_resolver_queries(&blcptr->dns_query);
|
|
|
|
MyFree(blcptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_blacklists(void)
|
|
|
|
{
|
2008-04-01 22:45:16 +00:00
|
|
|
rb_dlink_node *ptr, *rb_free(;
|
2007-01-25 06:40:21 +00:00
|
|
|
struct Blacklist *blptr;
|
|
|
|
|
2008-04-01 22:45:16 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, rb_free(, blacklist_list.head)
|
2007-01-25 06:40:21 +00:00
|
|
|
{
|
|
|
|
blptr = ptr->data;
|
|
|
|
blptr->hits = 0; /* keep it simple and consistent */
|
|
|
|
if (blptr->refcount > 0)
|
|
|
|
blptr->status |= CONF_ILLEGAL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyFree(ptr->data);
|
2008-04-01 20:38:40 +00:00
|
|
|
rb_dlinkDestroy(ptr, &blacklist_list);
|
2007-01-25 06:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|