]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/blacklist.c
blacklist: Remove the sscanf() for the IPv4 blacklist check.
[irc/rqf/shadowircd.git] / src / blacklist.c
index 382ea9139b0e448f7df44e4d94f7f9bc2293d4aa..d468a8f37dd22b7a11b0a87df7e3d1b4d7289bd8 100644 (file)
@@ -2,7 +2,7 @@
  * charybdis: A slightly useful ircd.
  * blacklist.c: Manages DNS blacklist entries and lookups
  *
- * Copyright (C) 2006 charybdis development team
+ * Copyright (C) 2006-2008 charybdis development team
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * 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 "tools.h"
-#include "memory.h"
 #include "numeric.h"
 #include "reject.h"
 #include "s_conf.h"
 #include "s_user.h"
 #include "blacklist.h"
 
-dlink_list blacklist_list = { NULL, NULL, 0 };
+rb_dlink_list blacklist_list = { NULL, NULL, 0 };
 
 /* private interfaces */
 static struct Blacklist *find_blacklist(char *name)
 {
-       dlink_node *nptr;
+       rb_dlink_node *nptr;
 
-       DLINK_FOREACH(nptr, blacklist_list.head)
+       RB_DLINK_FOREACH(nptr, blacklist_list.head)
        {
                struct Blacklist *blptr = (struct Blacklist *) nptr->data;
 
@@ -65,6 +62,7 @@ static struct Blacklist *find_blacklist(char *name)
 static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
 {
        struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
+       int listed = 0;
 
        if (blcptr == NULL || blcptr->client_p == NULL)
                return;
@@ -73,12 +71,27 @@ static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
        {
                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);
+               rb_free(blcptr);
                return;
        }
 
+       if (reply != NULL)
+       {
+               /* only accept 127.x.y.z as a listing */
+               if (reply->addr.ss_family == AF_INET &&
+                               !memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177", 1))
+                       listed = TRUE;
+               else 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();
+               }
+       }
+
        /* they have a blacklist entry for this client */
-       if (reply != NULL && blcptr->client_p->preClient->dnsbl_listed == NULL)
+       if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
        {
                blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist;
                /* reference to blacklist moves from blcptr to client_p->preClient... */
@@ -86,25 +99,25 @@ static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
        else
                unref_blacklist(blcptr->blacklist);
 
-       dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
+       rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
 
        /* yes, it can probably happen... */
-       if (dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name))
+       if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name))
        {
                char buf[USERLEN + 1];
-               strlcpy(buf, blcptr->client_p->username, sizeof buf);
+               rb_strlcpy(buf, blcptr->client_p->username, sizeof buf);
                register_local_user(blcptr->client_p, blcptr->client_p, buf);
        }
 
-       MyFree(blcptr);
+       rb_free(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];
+       struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient));
+       char buf[IRCD_RES_HOSTLEN + 1];
+       uint8_t *ip;
 
        blcptr->blacklist = blptr;
        blcptr->client_p = client_p;
@@ -112,15 +125,19 @@ static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *
        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]);
+       ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr;
 
        /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
-       ircsnprintf(buf, IRCD_BUFSIZE, "%d.%d.%d.%d.%s", ip[0], ip[1], ip[2], ip[3], blptr->host);
+       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);
 
        gethost_byname_type(buf, &blcptr->dns_query, T_A);
 
-       dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
+       rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
        blptr->refcount++;
 }
 
@@ -135,13 +152,14 @@ struct Blacklist *new_blacklist(char *name, char *reject_reason)
        blptr = find_blacklist(name);
        if (blptr == NULL)
        {
-               blptr = MyMalloc(sizeof(struct Blacklist));
-               dlinkAddAlloc(blptr, &blacklist_list);
+               blptr = rb_malloc(sizeof(struct Blacklist));
+               rb_dlinkAddAlloc(blptr, &blacklist_list);
        }
        else
                blptr->status &= ~CONF_ILLEGAL;
-       strlcpy(blptr->host, name, HOSTLEN);
-       strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
+       rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
+       rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
+       blptr->lastwarning = 0;
 
        return blptr;
 }
@@ -151,20 +169,20 @@ void unref_blacklist(struct Blacklist *blptr)
        blptr->refcount--;
        if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
        {
-               dlinkFindDestroy(blptr, &blacklist_list);
-               MyFree(blptr);
+               rb_dlinkFindDestroy(blptr, &blacklist_list);
+               rb_free(blptr);
        }
 }
 
 void lookup_blacklists(struct Client *client_p)
 {
-       dlink_node *nptr;
+       rb_dlink_node *nptr;
 
        /* We don't do IPv6 right now, sorry! */
        if (client_p->localClient->ip.ss_family == AF_INET6)
                return;
 
-       DLINK_FOREACH(nptr, blacklist_list.head)
+       RB_DLINK_FOREACH(nptr, blacklist_list.head)
        {
                struct Blacklist *blptr = (struct Blacklist *) nptr->data;
 
@@ -175,27 +193,27 @@ void lookup_blacklists(struct Client *client_p)
 
 void abort_blacklist_queries(struct Client *client_p)
 {
-       dlink_node *ptr, *next_ptr;
+       rb_dlink_node *ptr, *next_ptr;
        struct BlacklistClient *blcptr;
 
        if (client_p->preClient == NULL)
                return;
-       DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
        {
                blcptr = ptr->data;
-               dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
+               rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
                unref_blacklist(blcptr->blacklist);
                delete_resolver_queries(&blcptr->dns_query);
-               MyFree(blcptr);
+               rb_free(blcptr);
        }
 }
 
 void destroy_blacklists(void)
 {
-       dlink_node *ptr, *next_ptr;
+       rb_dlink_node *ptr, *next_ptr;
        struct Blacklist *blptr;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
        {
                blptr = ptr->data;
                blptr->hits = 0; /* keep it simple and consistent */
@@ -203,8 +221,8 @@ void destroy_blacklists(void)
                        blptr->status |= CONF_ILLEGAL;
                else
                {
-                       MyFree(ptr->data);
-                       dlinkDestroy(ptr, &blacklist_list);
+                       rb_free(ptr->data);
+                       rb_dlinkDestroy(ptr, &blacklist_list);
                }
        }
 }