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"
44 rb_dlink_list blacklist_list
= { NULL
, NULL
, 0 };
46 /* private interfaces */
47 static struct Blacklist
*find_blacklist(char *name
)
51 RB_DLINK_FOREACH(nptr
, blacklist_list
.head
)
53 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
55 if (!irccmp(blptr
->host
, name
))
62 static inline int blacklist_check_reply(struct BlacklistClient
*blcptr
, struct rb_sockaddr_storage
*addr
)
64 struct Blacklist
*blptr
= blcptr
->blacklist
;
65 char ipaddr
[HOSTIPLEN
];
69 /* XXX the below two checks might want to change at some point
70 * e.g. if IPv6 blacklists don't use 127.x.y.z or A records anymore
73 if (addr
->ss_family
!= AF_INET
||
74 memcmp(&((struct sockaddr_in
*)addr
)->sin_addr
, "\177", 1))
77 /* No filters and entry found - thus positive match */
78 if (!rb_dlink_list_length(&blptr
->filters
))
81 rb_inet_ntop_sock((struct sockaddr
*)addr
, ipaddr
, sizeof(ipaddr
));
83 /* Below will prolly have to change too if the above changes */
84 if ((lastoctet
= strrchr(ipaddr
, '.')) == NULL
|| *(++lastoctet
) == '\0')
87 RB_DLINK_FOREACH(ptr
, blcptr
->blacklist
->filters
.head
)
89 struct BlacklistFilter
*filter
= ptr
->data
;
92 if (filter
->type
== BLACKLIST_FILTER_ALL
)
94 else if (filter
->type
== BLACKLIST_FILTER_LAST
)
98 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
99 "blacklist_check_reply(): Unknown filtertype (BUG!)");
103 if (strcmp(cmpstr
, filter
->filterstr
) == 0)
110 if (blcptr
->blacklist
->lastwarning
+ 3600 < rb_current_time())
112 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
113 "Garbage reply from blacklist %s",
114 blcptr
->blacklist
->host
);
115 blcptr
->blacklist
->lastwarning
= rb_current_time();
120 static void blacklist_dns_callback(void *vptr
, struct DNSReply
*reply
)
122 struct BlacklistClient
*blcptr
= (struct BlacklistClient
*) vptr
;
125 if (blcptr
== NULL
|| blcptr
->client_p
== NULL
)
128 if (blcptr
->client_p
->preClient
== NULL
)
130 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
131 "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr
->client_p
, HIDE_IP
));
138 if (blacklist_check_reply(blcptr
, &reply
->addr
))
142 /* they have a blacklist entry for this client */
143 if (listed
&& blcptr
->client_p
->preClient
->dnsbl_listed
== NULL
)
145 blcptr
->client_p
->preClient
->dnsbl_listed
= blcptr
->blacklist
;
146 /* reference to blacklist moves from blcptr to client_p->preClient... */
149 unref_blacklist(blcptr
->blacklist
);
151 rb_dlinkDelete(&blcptr
->node
, &blcptr
->client_p
->preClient
->dnsbl_queries
);
153 /* yes, it can probably happen... */
154 if (rb_dlink_list_length(&blcptr
->client_p
->preClient
->dnsbl_queries
) == 0 && blcptr
->client_p
->flags
& FLAGS_SENTUSER
&& !EmptyString(blcptr
->client_p
->name
))
156 char buf
[USERLEN
+ 1];
157 rb_strlcpy(buf
, blcptr
->client_p
->username
, sizeof buf
);
158 register_local_user(blcptr
->client_p
, blcptr
->client_p
, buf
);
164 static void initiate_blacklist_dnsquery(struct Blacklist
*blptr
, struct Client
*client_p
)
166 struct BlacklistClient
*blcptr
= rb_malloc(sizeof(struct BlacklistClient
));
167 char buf
[IRCD_RES_HOSTLEN
+ 1];
170 blcptr
->blacklist
= blptr
;
171 blcptr
->client_p
= client_p
;
173 blcptr
->dns_query
.ptr
= blcptr
;
174 blcptr
->dns_query
.callback
= blacklist_dns_callback
;
176 if ((client_p
->localClient
->ip
.ss_family
== AF_INET
) && blptr
->ipv4
)
178 ip
= (uint8_t *)&((struct sockaddr_in
*)&client_p
->localClient
->ip
)->sin_addr
.s_addr
;
180 /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
181 rb_snprintf(buf
, sizeof buf
, "%d.%d.%d.%d.%s",
182 (unsigned int) ip
[3],
183 (unsigned int) ip
[2],
184 (unsigned int) ip
[1],
185 (unsigned int) ip
[0],
188 /* IPv6 is supported now. --Elizabeth */
189 else if ((client_p
->localClient
->ip
.ss_family
== AF_INET6
) && blptr
->ipv6
)
191 /* Breaks it into ip[0] = 0x00, ip[1] = 0x00... ip[16] = 0x01 for localhost
192 * I wish there was a uint4_t for C, this would make the below cleaner, but... */
193 ip
= (uint8_t *)&((struct sockaddr_in6
*)&client_p
->localClient
->ip
)->sin6_addr
.s6_addr
;
197 /* The below will give us something like
198 * 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
201 /* Going backwards */
202 for (i
= 15; i
>= 0; i
--)
204 /* Get upper and lower nibbles (yes this works fine on
205 * both big and little endian) */
206 uint8_t hi
= (ip
[i
] >> 4) & 0x0F;
207 uint8_t lo
= ip
[i
] & 0x0F;
209 /* One part... (why 5? rb_snprintf adds \0) */
210 rb_snprintf(bufptr
, 5, "%1x.%1x.",
211 (unsigned int) lo
, /* Remember, backwards */
214 /* Lurch forward to next position */
219 strcpy(bufptr
, blptr
->host
);
221 /* This shouldn't happen... */
225 gethost_byname_type(buf
, &blcptr
->dns_query
, T_A
);
227 rb_dlinkAdd(blcptr
, &blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
231 /* public interfaces */
232 struct Blacklist
*new_blacklist(char *name
, char *reject_reason
, int ipv4
, int ipv6
, rb_dlink_list
*filters
)
234 struct Blacklist
*blptr
;
236 if (name
== NULL
|| reject_reason
== NULL
)
239 blptr
= find_blacklist(name
);
242 blptr
= rb_malloc(sizeof(struct Blacklist
));
243 rb_dlinkAddAlloc(blptr
, &blacklist_list
);
246 blptr
->status
&= ~CONF_ILLEGAL
;
248 rb_strlcpy(blptr
->host
, name
, IRCD_RES_HOSTLEN
+ 1);
249 rb_strlcpy(blptr
->reject_reason
, reject_reason
, IRCD_BUFSIZE
);
253 rb_dlinkMoveList(filters
, &blptr
->filters
);
255 blptr
->lastwarning
= 0;
260 void unref_blacklist(struct Blacklist
*blptr
)
262 rb_dlink_node
*ptr
, *next_ptr
;
265 if (blptr
->status
& CONF_ILLEGAL
&& blptr
->refcount
<= 0)
267 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, blptr
->filters
.head
)
270 rb_dlinkDelete(ptr
, &blptr
->filters
);
273 rb_dlinkFindDestroy(blptr
, &blacklist_list
);
278 void lookup_blacklists(struct Client
*client_p
)
282 RB_DLINK_FOREACH(nptr
, blacklist_list
.head
)
284 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
286 if (!(blptr
->status
& CONF_ILLEGAL
))
287 initiate_blacklist_dnsquery(blptr
, client_p
);
291 void abort_blacklist_queries(struct Client
*client_p
)
293 rb_dlink_node
*ptr
, *next_ptr
;
294 struct BlacklistClient
*blcptr
;
296 if (client_p
->preClient
== NULL
)
298 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->preClient
->dnsbl_queries
.head
)
301 rb_dlinkDelete(&blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
302 unref_blacklist(blcptr
->blacklist
);
303 delete_resolver_queries(&blcptr
->dns_query
);
308 void destroy_blacklists(void)
310 rb_dlink_node
*ptr
, *next_ptr
;
311 struct Blacklist
*blptr
;
313 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, blacklist_list
.head
)
316 blptr
->hits
= 0; /* keep it simple and consistent */
317 if (blptr
->refcount
> 0)
318 blptr
->status
|= CONF_ILLEGAL
;
322 rb_dlinkDestroy(ptr
, &blacklist_list
);