2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
5 * Copyright (C) 2006-2011 charybdis development team
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
42 #include "blacklist.h"
45 rb_dlink_list blacklist_list
= { NULL
, NULL
, 0 };
47 /* private interfaces */
48 static struct Blacklist
*find_blacklist(char *name
)
52 RB_DLINK_FOREACH(nptr
, blacklist_list
.head
)
54 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
56 if (!irccmp(blptr
->host
, name
))
63 static inline int blacklist_check_reply(struct BlacklistClient
*blcptr
, const char *ipaddr
)
65 struct Blacklist
*blptr
= blcptr
->blacklist
;
66 const char *lastoctet
;
69 /* No filters and entry found - thus positive match */
70 if (!rb_dlink_list_length(&blptr
->filters
))
73 /* Below will prolly have to change too if the above changes */
74 if ((lastoctet
= strrchr(ipaddr
, '.')) == NULL
|| *(++lastoctet
) == '\0')
77 RB_DLINK_FOREACH(ptr
, blcptr
->blacklist
->filters
.head
)
79 struct BlacklistFilter
*filter
= ptr
->data
;
82 if (filter
->type
== BLACKLIST_FILTER_ALL
)
84 else if (filter
->type
== BLACKLIST_FILTER_LAST
)
88 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
89 "blacklist_check_reply(): Unknown filtertype (BUG!)");
93 if (strcmp(cmpstr
, filter
->filterstr
) == 0)
100 if (blcptr
->blacklist
->lastwarning
+ 3600 < rb_current_time())
102 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
103 "Garbage reply from blacklist %s",
104 blcptr
->blacklist
->host
);
105 blcptr
->blacklist
->lastwarning
= rb_current_time();
110 static void blacklist_dns_callback(const char *result
, int status
, int aftype
, void *vptr
)
112 struct BlacklistClient
*blcptr
= (struct BlacklistClient
*) vptr
;
115 if (blcptr
== NULL
|| blcptr
->client_p
== NULL
)
118 if (blcptr
->client_p
->preClient
== NULL
)
120 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
121 "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr
->client_p
, HIDE_IP
));
126 if (result
!= NULL
&& status
)
128 if (blacklist_check_reply(blcptr
, result
))
132 /* they have a blacklist entry for this client */
133 if (listed
&& blcptr
->client_p
->preClient
->dnsbl_listed
== NULL
)
135 blcptr
->client_p
->preClient
->dnsbl_listed
= blcptr
->blacklist
;
136 /* reference to blacklist moves from blcptr to client_p->preClient... */
139 unref_blacklist(blcptr
->blacklist
);
141 rb_dlinkDelete(&blcptr
->node
, &blcptr
->client_p
->preClient
->dnsbl_queries
);
143 /* yes, it can probably happen... */
144 if (rb_dlink_list_length(&blcptr
->client_p
->preClient
->dnsbl_queries
) == 0 && blcptr
->client_p
->flags
& FLAGS_SENTUSER
&& !EmptyString(blcptr
->client_p
->name
))
145 register_local_user(blcptr
->client_p
, blcptr
->client_p
);
150 static void initiate_blacklist_dnsquery(struct Blacklist
*blptr
, struct Client
*client_p
)
152 struct BlacklistClient
*blcptr
= rb_malloc(sizeof(struct BlacklistClient
));
153 char buf
[IRCD_RES_HOSTLEN
+ 1];
156 blcptr
->blacklist
= blptr
;
157 blcptr
->client_p
= client_p
;
159 if ((client_p
->localClient
->ip
.ss_family
== AF_INET
) && blptr
->ipv4
)
161 ip
= (uint8_t *)&((struct sockaddr_in
*)&client_p
->localClient
->ip
)->sin_addr
.s_addr
;
163 /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
164 snprintf(buf
, sizeof buf
, "%d.%d.%d.%d.%s",
165 (unsigned int) ip
[3],
166 (unsigned int) ip
[2],
167 (unsigned int) ip
[1],
168 (unsigned int) ip
[0],
171 /* IPv6 is supported now. --Elizabeth */
172 else if ((client_p
->localClient
->ip
.ss_family
== AF_INET6
) && blptr
->ipv6
)
174 /* Breaks it into ip[0] = 0x00, ip[1] = 0x00... ip[16] = 0x01 for localhost
175 * I wish there was a uint4_t for C, this would make the below cleaner, but... */
176 ip
= (uint8_t *)&((struct sockaddr_in6
*)&client_p
->localClient
->ip
)->sin6_addr
.s6_addr
;
180 /* The below will give us something like
181 * 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
184 /* Going backwards */
185 for (i
= 15; i
>= 0; i
--)
187 /* Get upper and lower nibbles (yes this works fine on
188 * both big and little endian) */
189 uint8_t hi
= (ip
[i
] >> 4) & 0x0F;
190 uint8_t lo
= ip
[i
] & 0x0F;
192 /* One part... (why 5? rb_snprintf adds \0) */
193 snprintf(bufptr
, 5, "%1x.%1x.",
194 (unsigned int) lo
, /* Remember, backwards */
197 /* Lurch forward to next position */
202 strcpy(bufptr
, blptr
->host
);
204 /* This shouldn't happen... */
208 blcptr
->dns_id
= lookup_hostname(buf
, AF_INET
, blacklist_dns_callback
, blcptr
);
210 rb_dlinkAdd(blcptr
, &blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
214 /* public interfaces */
215 struct Blacklist
*new_blacklist(char *name
, char *reject_reason
, int ipv4
, int ipv6
, rb_dlink_list
*filters
)
217 struct Blacklist
*blptr
;
219 if (name
== NULL
|| reject_reason
== NULL
)
222 blptr
= find_blacklist(name
);
225 blptr
= rb_malloc(sizeof(struct Blacklist
));
226 rb_dlinkAddAlloc(blptr
, &blacklist_list
);
229 blptr
->status
&= ~CONF_ILLEGAL
;
231 rb_strlcpy(blptr
->host
, name
, IRCD_RES_HOSTLEN
+ 1);
232 rb_strlcpy(blptr
->reject_reason
, reject_reason
, IRCD_BUFSIZE
);
236 rb_dlinkMoveList(filters
, &blptr
->filters
);
238 blptr
->lastwarning
= 0;
243 void unref_blacklist(struct Blacklist
*blptr
)
245 rb_dlink_node
*ptr
, *next_ptr
;
248 if (blptr
->status
& CONF_ILLEGAL
&& blptr
->refcount
<= 0)
250 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, blptr
->filters
.head
)
252 rb_dlinkDelete(ptr
, &blptr
->filters
);
256 rb_dlinkFindDestroy(blptr
, &blacklist_list
);
261 void lookup_blacklists(struct Client
*client_p
)
265 RB_DLINK_FOREACH(nptr
, blacklist_list
.head
)
267 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
269 if (!(blptr
->status
& CONF_ILLEGAL
))
270 initiate_blacklist_dnsquery(blptr
, client_p
);
274 void abort_blacklist_queries(struct Client
*client_p
)
276 rb_dlink_node
*ptr
, *next_ptr
;
277 struct BlacklistClient
*blcptr
;
279 if (client_p
->preClient
== NULL
)
281 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->preClient
->dnsbl_queries
.head
)
284 rb_dlinkDelete(&blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
285 unref_blacklist(blcptr
->blacklist
);
286 cancel_lookup(blcptr
->dns_id
);
291 void destroy_blacklists(void)
293 rb_dlink_node
*ptr
, *next_ptr
;
294 struct Blacklist
*blptr
;
296 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, blacklist_list
.head
)
299 blptr
->hits
= 0; /* keep it simple and consistent */
300 if (blptr
->refcount
> 0)
301 blptr
->status
|= CONF_ILLEGAL
;
305 rb_dlinkDestroy(ptr
, &blacklist_list
);