]> jfr.im git - solanum.git/blame - src/blacklist.c
src/ircd: Die if the configuration file does not exist
[solanum.git] / src / blacklist.c
CommitLineData
212380e3
AC
1/*
2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
4 *
0a1e77c2 5 * Copyright (C) 2006-2011 charybdis development team
212380e3
AC
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
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.
17 *
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.
20 *
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.
32 *
212380e3
AC
33 */
34
35#include "stdinc.h"
36#include "client.h"
37#include "res.h"
212380e3
AC
38#include "numeric.h"
39#include "reject.h"
40#include "s_conf.h"
41#include "s_user.h"
42#include "blacklist.h"
43
b2f0da88 44rb_dlink_list blacklist_list = { NULL, NULL, 0 };
212380e3
AC
45
46/* private interfaces */
47static struct Blacklist *find_blacklist(char *name)
48{
b2f0da88 49 rb_dlink_node *nptr;
212380e3 50
b2f0da88 51 RB_DLINK_FOREACH(nptr, blacklist_list.head)
212380e3
AC
52 {
53 struct Blacklist *blptr = (struct Blacklist *) nptr->data;
54
55 if (!irccmp(blptr->host, name))
56 return blptr;
57 }
58
59 return NULL;
60}
61
3c93d380
EM
62static inline int blacklist_check_reply(struct BlacklistClient *blcptr, struct rb_sockaddr_storage *addr)
63{
64 struct Blacklist *blptr = blcptr->blacklist;
65 char ipaddr[HOSTIPLEN];
66 char *lastoctet;
67 rb_dlink_node *ptr;
68
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
71 * --Elizabeth
72 */
73 if (addr->ss_family != AF_INET ||
74 memcmp(&((struct sockaddr_in *)addr)->sin_addr, "\177", 1))
75 goto blwarn;
76
77 /* No filters and entry found - thus positive match */
78 if (!rb_dlink_list_length(&blptr->filters))
79 return 1;
80
81 rb_inet_ntop_sock((struct sockaddr *)addr, ipaddr, sizeof(ipaddr));
82
83 /* Below will prolly have to change too if the above changes */
84 if ((lastoctet = strrchr(ipaddr, '.')) == NULL || *(++lastoctet) == '\0')
85 goto blwarn;
86
87 RB_DLINK_FOREACH(ptr, blcptr->blacklist->filters.head)
88 {
89 struct BlacklistFilter *filter = ptr->data;
90 char *cmpstr;
91
92 if (filter->type == BLACKLIST_FILTER_ALL)
93 cmpstr = ipaddr;
94 else if (filter->type == BLACKLIST_FILTER_LAST)
95 cmpstr = lastoctet;
96 else
97 {
98 sendto_realops_snomask(SNO_GENERAL, L_ALL,
99 "blacklist_check_reply(): Unknown filtertype (BUG!)");
100 continue;
101 }
102
103 if (strcmp(cmpstr, filter->filterstr) == 0)
104 /* Match! */
105 return 1;
106 }
107
108 return 0;
109blwarn:
110 if (blcptr->blacklist->lastwarning + 3600 < rb_current_time())
111 {
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();
116 }
117 return 0;
118}
119
212380e3
AC
120static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
121{
122 struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
137d856d 123 int listed = 0;
212380e3
AC
124
125 if (blcptr == NULL || blcptr->client_p == NULL)
126 return;
127
128 if (blcptr->client_p->preClient == NULL)
129 {
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));
637c4932 132 rb_free(blcptr);
212380e3
AC
133 return;
134 }
135
137d856d
JT
136 if (reply != NULL)
137 {
3c93d380 138 if (blacklist_check_reply(blcptr, &reply->addr))
96274734 139 listed = TRUE;
137d856d
JT
140 }
141
212380e3 142 /* they have a blacklist entry for this client */
137d856d 143 if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
212380e3
AC
144 {
145 blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist;
146 /* reference to blacklist moves from blcptr to client_p->preClient... */
147 }
148 else
149 unref_blacklist(blcptr->blacklist);
150
b2f0da88 151 rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
212380e3
AC
152
153 /* yes, it can probably happen... */
b2f0da88 154 if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name))
212380e3
AC
155 {
156 char buf[USERLEN + 1];
f427c8b0 157 rb_strlcpy(buf, blcptr->client_p->username, sizeof buf);
212380e3
AC
158 register_local_user(blcptr->client_p, blcptr->client_p, buf);
159 }
160
637c4932 161 rb_free(blcptr);
212380e3
AC
162}
163
212380e3
AC
164static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p)
165{
eddc2ab6 166 struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient));
db3efb7a 167 char buf[IRCD_RES_HOSTLEN + 1];
5d21ef50 168 uint8_t *ip;
212380e3
AC
169
170 blcptr->blacklist = blptr;
171 blcptr->client_p = client_p;
172
173 blcptr->dns_query.ptr = blcptr;
174 blcptr->dns_query.callback = blacklist_dns_callback;
175
0a1e77c2
EM
176 if ((client_p->localClient->ip.ss_family == AF_INET) && blptr->ipv4)
177 {
178 ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr;
179
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],
186 blptr->host);
187 }
188 /* IPv6 is supported now. --Elizabeth */
189 else if ((client_p->localClient->ip.ss_family == AF_INET6) && blptr->ipv6)
190 {
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;
194 char *bufptr = buf;
195 int i;
196
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
199 */
200
201 /* Going backwards */
202 for (i = 15; i >= 0; i--)
203 {
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;
208
209 /* One part... (why 5? rb_snprintf adds \0) */
210 rb_snprintf(bufptr, 5, "%1x.%1x.",
211 (unsigned int) lo, /* Remember, backwards */
212 (unsigned int) hi);
213
214 /* Lurch forward to next position */
215 bufptr += 4;
216 }
212380e3 217
0a1e77c2
EM
218 /* Tack host on */
219 strcpy(bufptr, blptr->host);
220 }
221 /* This shouldn't happen... */
222 else
223 return;
212380e3
AC
224
225 gethost_byname_type(buf, &blcptr->dns_query, T_A);
226
b2f0da88 227 rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
212380e3
AC
228 blptr->refcount++;
229}
230
231/* public interfaces */
3c93d380 232struct Blacklist *new_blacklist(char *name, char *reject_reason, int ipv4, int ipv6, rb_dlink_list *filters)
212380e3
AC
233{
234 struct Blacklist *blptr;
235
236 if (name == NULL || reject_reason == NULL)
237 return NULL;
238
239 blptr = find_blacklist(name);
240 if (blptr == NULL)
241 {
eddc2ab6 242 blptr = rb_malloc(sizeof(struct Blacklist));
b2f0da88 243 rb_dlinkAddAlloc(blptr, &blacklist_list);
212380e3
AC
244 }
245 else
246 blptr->status &= ~CONF_ILLEGAL;
3c93d380 247
db3efb7a 248 rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
f427c8b0 249 rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
0a1e77c2
EM
250 blptr->ipv4 = ipv4;
251 blptr->ipv6 = ipv6;
3c93d380
EM
252
253 rb_dlinkMoveList(filters, &blptr->filters);
254
96274734 255 blptr->lastwarning = 0;
212380e3
AC
256
257 return blptr;
258}
259
260void unref_blacklist(struct Blacklist *blptr)
261{
3c93d380
EM
262 rb_dlink_node *ptr, *next_ptr;
263
212380e3
AC
264 blptr->refcount--;
265 if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
266 {
3c93d380
EM
267 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blptr->filters.head)
268 {
269 rb_free(ptr);
270 rb_dlinkDelete(ptr, &blptr->filters);
271 }
272
b2f0da88 273 rb_dlinkFindDestroy(blptr, &blacklist_list);
637c4932 274 rb_free(blptr);
212380e3
AC
275 }
276}
277
278void lookup_blacklists(struct Client *client_p)
279{
b2f0da88 280 rb_dlink_node *nptr;
212380e3 281
b2f0da88 282 RB_DLINK_FOREACH(nptr, blacklist_list.head)
212380e3
AC
283 {
284 struct Blacklist *blptr = (struct Blacklist *) nptr->data;
285
286 if (!(blptr->status & CONF_ILLEGAL))
287 initiate_blacklist_dnsquery(blptr, client_p);
288 }
289}
290
291void abort_blacklist_queries(struct Client *client_p)
292{
637c4932 293 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
294 struct BlacklistClient *blcptr;
295
296 if (client_p->preClient == NULL)
297 return;
637c4932 298 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
212380e3
AC
299 {
300 blcptr = ptr->data;
b2f0da88 301 rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
212380e3
AC
302 unref_blacklist(blcptr->blacklist);
303 delete_resolver_queries(&blcptr->dns_query);
637c4932 304 rb_free(blcptr);
212380e3
AC
305 }
306}
307
308void destroy_blacklists(void)
309{
637c4932 310 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
311 struct Blacklist *blptr;
312
637c4932 313 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
212380e3
AC
314 {
315 blptr = ptr->data;
316 blptr->hits = 0; /* keep it simple and consistent */
317 if (blptr->refcount > 0)
318 blptr->status |= CONF_ILLEGAL;
319 else
320 {
637c4932 321 rb_free(ptr->data);
b2f0da88 322 rb_dlinkDestroy(ptr, &blacklist_list);
212380e3
AC
323 }
324 }
325}