]> jfr.im git - solanum.git/blame - authd/providers/blacklist.c
authd/provider: some fixes
[solanum.git] / authd / providers / blacklist.c
CommitLineData
add80afd
EM
1/*
2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
4 *
5 * Copyright (C) 2006-2011 charybdis development team
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 */
33
34/* Originally written for charybdis circa 2006 (by nenolod?).
35 * Tweaked for authd. Some functions and structs renamed. Public/private
36 * interfaces have been shifted around. Some code has been cleaned up too.
37 * -- Elizafox 24 March 2016
38 */
39
40#include "authd.h"
41#include "provider.h"
db821ee9 42#include "notice.h"
add80afd
EM
43#include "stdinc.h"
44#include "dns.h"
45
46typedef enum filter_t
47{
48 FILTER_ALL = 1,
49 FILTER_LAST = 2,
50} filter_t;
51
52/* Blacklist accepted IP types */
53#define IPTYPE_IPV4 1
54#define IPTYPE_IPV6 2
55
56/* A configured DNSBL */
57struct blacklist
58{
59 char host[IRCD_RES_HOSTLEN + 1];
60 char reason[BUFSIZE]; /* Reason template (ircd fills in the blanks) */
61 unsigned char iptype; /* IP types supported */
62 rb_dlink_list filters; /* Filters for queries */
63
64 bool delete; /* If true delete when no clients */
65 int refcount; /* When 0 and delete is set, remove this blacklist */
66
67 time_t lastwarning; /* Last warning about garbage replies sent */
68};
69
70/* A lookup in progress for a particular DNSBL for a particular client */
71struct blacklist_lookup
72{
73 struct blacklist *bl; /* Blacklist we're checking */
74 struct auth_client *auth; /* Client */
75 struct dns_query *query; /* DNS query pointer */
76
77 rb_dlink_node node;
78};
79
80/* A blacklist filter */
81struct blacklist_filter
82{
83 filter_t type; /* Type of filter */
3f2695ac 84 char filter[HOSTIPLEN]; /* The filter itself */
add80afd
EM
85
86 rb_dlink_node node;
87};
88
89/* Blacklist user data attached to auth_client instance */
90struct blacklist_user
91{
92 rb_dlink_list queries; /* Blacklist queries in flight */
93 time_t timeout; /* When this times out */
94};
95
96/* public interfaces */
97static bool blacklists_init(void);
98static void blacklists_destroy(void);
99
100static bool blacklists_start(struct auth_client *);
101static void blacklists_cancel(struct auth_client *);
102
103/* private interfaces */
104static void unref_blacklist(struct blacklist *);
3f2695ac
EM
105static struct blacklist *new_blacklist(const char *, const char *, unsigned char, rb_dlink_list *);
106static struct blacklist *find_blacklist(const char *);
add80afd
EM
107static bool blacklist_check_reply(struct blacklist_lookup *, const char *);
108static void blacklist_dns_callback(const char *, bool, query_type, void *);
109static void initiate_blacklist_dnsquery(struct blacklist *, struct auth_client *);
110static void timeout_blacklist_queries_event(void *);
111
112/* Variables */
113static rb_dlink_list blacklist_list = { NULL, NULL, 0 };
114static struct ev_entry *timeout_ev;
115static EVH timeout_blacklists;
116static int blacklist_timeout = 15;
117
118/* private interfaces */
119
120static void
121unref_blacklist(struct blacklist *bl)
122{
123 rb_dlink_node *ptr, *nptr;
124
125 bl->refcount--;
126 if (bl->delete && bl->refcount <= 0)
127 {
128 RB_DLINK_FOREACH_SAFE(ptr, nptr, bl->filters.head)
129 {
130 rb_dlinkDelete(ptr, &bl->filters);
131 rb_free(ptr);
132 }
133
134 rb_dlinkFindDestroy(bl, &blacklist_list);
135 rb_free(bl);
136 }
137}
138
139static struct blacklist *
3f2695ac 140new_blacklist(const char *name, const char *reason, unsigned char iptype, rb_dlink_list *filters)
add80afd
EM
141{
142 struct blacklist *bl;
143
3f2695ac 144 if (name == NULL || reason == NULL || iptype == 0)
add80afd
EM
145 return NULL;
146
147 if((bl = find_blacklist(name)) == NULL)
148 {
149 bl = rb_malloc(sizeof(struct blacklist));
150 rb_dlinkAddAlloc(bl, &blacklist_list);
151 }
152 else
153 bl->delete = false;
154
155 rb_strlcpy(bl->host, name, IRCD_RES_HOSTLEN + 1);
156 rb_strlcpy(bl->reason, reason, BUFSIZE);
3f2695ac 157 bl->iptype = iptype;
add80afd
EM
158
159 rb_dlinkMoveList(filters, &bl->filters);
160
161 bl->lastwarning = 0;
162
163 return bl;
164}
165
166static struct blacklist *
3f2695ac 167find_blacklist(const char *name)
add80afd
EM
168{
169 rb_dlink_node *ptr;
170
171 RB_DLINK_FOREACH(ptr, blacklist_list.head)
172 {
173 struct blacklist *bl = (struct blacklist *)ptr->data;
174
175 if (!strcasecmp(bl->host, name))
176 return bl;
177 }
178
179 return NULL;
180}
181
182static inline bool
183blacklist_check_reply(struct blacklist_lookup *bllookup, const char *ipaddr)
184{
185 struct blacklist *bl = bllookup->bl;
186 const char *lastoctet;
187 rb_dlink_node *ptr;
188
189 /* No filters and entry found - thus positive match */
190 if (!rb_dlink_list_length(&bl->filters))
191 return true;
192
193 /* Below will prolly have to change if IPv6 address replies are sent back */
194 if ((lastoctet = strrchr(ipaddr, '.')) == NULL || *(++lastoctet) == '\0')
195 goto blwarn;
196
197 RB_DLINK_FOREACH(ptr, bl->filters.head)
198 {
199 struct blacklist_filter *filter = ptr->data;
200 const char *cmpstr;
201
202 if (filter->type == FILTER_ALL)
203 cmpstr = ipaddr;
204 else if (filter->type == FILTER_LAST)
205 cmpstr = lastoctet;
206 else
207 {
208 warn_opers(L_CRIT, "BUG: Unknown blacklist filter type on blacklist %s: %d",
209 bl->host, filter->type);
210 continue;
211 }
212
3f2695ac 213 if (strcmp(cmpstr, filter->filter) == 0)
add80afd
EM
214 /* Match! */
215 return true;
216 }
217
218 return false;
219blwarn:
220 if (bl->lastwarning + 3600 < rb_current_time())
221 {
222 warn_opers(L_WARN, "Garbage/undecipherable reply received from blacklist %s (reply %s)",
223 bl->host, ipaddr);
224 bl->lastwarning = rb_current_time();
225 }
226 return false;
227}
228
229static void
230blacklist_dns_callback(const char *result, bool status, query_type type, void *data)
231{
232 struct blacklist_lookup *bllookup = (struct blacklist_lookup *)data;
233 struct blacklist_user *bluser;
234 struct auth_client *auth;
235
236 if (bllookup == NULL || bllookup->auth == NULL)
237 return;
238
239 auth = bllookup->auth;
240 bluser = auth->data[PROVIDER_BLACKLIST];
241 if(bluser == NULL)
242 return;
243
244 if (result != NULL && status && blacklist_check_reply(bllookup, result))
245 {
246 /* Match found, so proceed no further */
247 blacklists_cancel(auth);
248 reject_client(auth, PROVIDER_BLACKLIST, bllookup->bl->reason);
249 return;
250 }
251
252 unref_blacklist(bllookup->bl);
253 rb_dlinkDelete(&bllookup->node, &bluser->queries);
254 rb_free(bllookup);
255
256 if(!rb_dlink_list_length(&bluser->queries))
257 {
258 /* Done here */
a7d5aea1 259 provider_done(auth, PROVIDER_BLACKLIST);
add80afd
EM
260 rb_free(bluser);
261 auth->data[PROVIDER_BLACKLIST] = NULL;
262 }
263}
264
265static void
266initiate_blacklist_dnsquery(struct blacklist *bl, struct auth_client *auth)
267{
268 struct blacklist_lookup *bllookup = rb_malloc(sizeof(struct blacklist_lookup));
269 struct blacklist_user *bluser = auth->data[PROVIDER_BLACKLIST];
270 char buf[IRCD_RES_HOSTLEN + 1];
271 int aftype;
272
273 bllookup->bl = bl;
274 bllookup->auth = auth;
275
276 aftype = GET_SS_FAMILY(&auth->c_addr);
277 if((aftype == AF_INET && (bl->iptype & IPTYPE_IPV4) == 0) ||
278 (aftype == AF_INET6 && (bl->iptype & IPTYPE_IPV6) == 0))
279 /* Incorrect blacklist type for this IP... */
280 return;
281
282 build_rdns(buf, sizeof(buf), &auth->c_addr, bl->host);
283
284 bllookup->query = lookup_ip(buf, AF_INET, blacklist_dns_callback, bllookup);
285
286 rb_dlinkAdd(bllookup, &bllookup->node, &bluser->queries);
287 bl->refcount++;
288}
289
290/* Timeout outstanding queries */
291static void
292timeout_blacklist_queries_event(void *notused)
293{
294 struct auth_client *auth;
295 rb_dictionary_iter iter;
296
297 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
298 {
299 struct blacklist_user *bluser = auth->data[PROVIDER_BLACKLIST];
300
301 if(bluser != NULL && bluser->timeout < rb_current_time())
302 {
303 blacklists_cancel(auth);
304 provider_done(auth, PROVIDER_BLACKLIST);
305 }
306 }
307}
308
a7d5aea1
EM
309static inline void
310lookup_all_blacklists(struct auth_client *auth)
311{
312 struct blacklist_user *bluser = auth->data[PROVIDER_BLACKLIST];
313 rb_dlink_node *ptr;
314
315 RB_DLINK_FOREACH(ptr, blacklist_list.head)
316 {
317 struct blacklist *bl = (struct blacklist *)ptr->data;
318
319 if (!bl->delete)
320 initiate_blacklist_dnsquery(bl, auth);
321 }
322
323 bluser->timeout = rb_current_time() + blacklist_timeout;
324}
325
a0a218ba
EM
326static inline void
327delete_blacklist(struct blacklist *bl)
328{
329 if (bl->refcount > 0)
330 bl->delete = true;
331 else
332 {
333 rb_dlinkFindDestroy(bl, &blacklist_list);
334 rb_free(bl);
335 }
336}
337
338static void
339delete_all_blacklists(void)
340{
341 rb_dlink_node *ptr, *nptr;
342
343 RB_DLINK_FOREACH_SAFE(ptr, nptr, blacklist_list.head)
344 {
345 delete_blacklist(ptr->data);
346 }
347}
348
add80afd
EM
349/* public interfaces */
350static bool
351blacklists_start(struct auth_client *auth)
352{
add80afd
EM
353 if(auth->data[PROVIDER_BLACKLIST] != NULL)
354 return true;
355
356 if(!rb_dlink_list_length(&blacklist_list))
357 /* Nothing to do... */
358 return true;
359
a7d5aea1 360 auth->data[PROVIDER_BLACKLIST] = rb_malloc(sizeof(struct blacklist_user));
add80afd 361
a7d5aea1
EM
362 if(is_provider_done(auth, PROVIDER_RDNS) && is_provider_done(auth, PROVIDER_IDENT))
363 /* This probably can't happen but let's handle this case anyway */
364 lookup_all_blacklists(auth);
add80afd 365
a7d5aea1
EM
366 set_provider_on(auth, PROVIDER_BLACKLIST);
367 return true;
368}
add80afd 369
6c88869f 370/* This is called every time a provider is completed as long as we are marked not done */
a7d5aea1
EM
371static void
372blacklists_initiate(struct auth_client *auth, provider_t provider)
373{
374 struct blacklist_user *bluser = auth->data[PROVIDER_BLACKLIST];
add80afd 375
6c88869f
EM
376 lrb_assert(provider != PROVIDER_BLACKLIST);
377 lrb_assert(!is_provider_done(auth, PROVIDER_BLACKLIST));
378 lrb_assert(rb_dlink_list_length(&blacklist_list) > 0);
379
380 if(bluser == NULL || rb_dlink_list_length(&bluser->queries))
a7d5aea1
EM
381 /* Nothing to do */
382 return;
6c88869f 383 else if(!(is_provider_done(auth, PROVIDER_RDNS) && is_provider_done(auth, PROVIDER_IDENT)))
a7d5aea1
EM
384 /* Don't start until we've completed these */
385 return;
386 else
387 lookup_all_blacklists(auth);
add80afd
EM
388}
389
390static void
391blacklists_cancel(struct auth_client *auth)
392{
393 rb_dlink_node *ptr, *nptr;
394 struct blacklist_user *bluser = auth->data[PROVIDER_BLACKLIST];
395
396 if(bluser == NULL)
397 return;
398
399 RB_DLINK_FOREACH_SAFE(ptr, nptr, bluser->queries.head)
400 {
401 struct blacklist_lookup *bllookup = ptr->data;
402 rb_dlinkDelete(&bllookup->node, &bluser->queries);
403 unref_blacklist(bllookup->bl);
404 cancel_query(bllookup->query);
405 rb_free(bllookup);
406 }
407
408 rb_free(bluser);
409 auth->data[PROVIDER_BLACKLIST] = NULL;
410}
411
412static bool
413blacklists_init(void)
414{
415 timeout_ev = rb_event_addish("timeout_blacklist_queries_event", timeout_blacklist_queries_event, NULL, 1);
416 return (timeout_ev != NULL);
417}
418
419static void
420blacklists_destroy(void)
421{
422 rb_dlink_node *ptr, *nptr;
423 rb_dictionary_iter iter;
424 struct blacklist *bl;
425 struct auth_client *auth;
426
427 RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
428 {
429 blacklists_cancel(auth);
430 }
431
a0a218ba
EM
432 delete_all_blacklists();
433 rb_event_delete(timeout_ev);
add80afd
EM
434}
435
3f2695ac
EM
436static void
437add_conf_blacklist(const char *key, int parc, const char **parv)
438{
439 rb_dlink_list filters;
440 char *tmp, *elemlist = rb_strdup(parv[2]);
441 unsigned char iptype;
442
443 for(char *elem = rb_strtok_r(elemlist, ",", &tmp); elem; elem = rb_strtok_r(NULL, ",", &tmp))
444 {
445 struct blacklist_filter *filter = rb_malloc(sizeof(struct blacklist_filter));
446 int dot_c = 0;
447 filter_t type = FILTER_LAST;
448 bool valid = true;
449
450 /* Check blacklist filter type and for validity */
451 for(char *c = elem; *c != '\0'; c++)
452 {
453 if(*c == '.')
454 {
455 if(++dot_c > 3)
456 {
457 warn_opers(L_CRIT, "addr_conf_blacklist got a bad filter (too many octets)");
458 valid = false;
459 break;
460 }
461
462 type = FILTER_ALL;
463 }
464 else if(!isdigit(*c))
465 {
466 warn_opers(L_CRIT, "addr_conf_blacklist got a bad filter (invalid character in blacklist filter: %c)", *c);
467 valid = false;
468 break;
469 }
470 }
471
472 if(valid && dot_c > 0 && dot_c < 3)
473 {
474 warn_opers(L_CRIT, "addr_conf_blacklist got a bad filter (insufficient octets)");
475 valid = false;
476 }
477
478 if(!valid)
479 {
480 rb_free(filter);
481 continue;
482 }
483
484 filter->type = type;
485 rb_strlcpy(filter->filter, elem, sizeof(filter->filter));
486 rb_dlinkAdd(filter, &filter->node, &filters);
487 }
488
489 rb_free(elemlist);
490
491 iptype = atoi(parv[1]) & 0x3;
492 if(new_blacklist(parv[0], parv[3], iptype, &filters) == NULL)
493 {
494 rb_dlink_node *ptr, *nptr;
495
496 warn_opers(L_CRIT, "addr_conf_blacklist got a malformed blacklist");
497
498 RB_DLINK_FOREACH_SAFE(ptr, nptr, filters.head)
499 {
500 rb_free(ptr->data);
501 rb_dlinkDelete(ptr, &filters);
502 }
503 }
504}
505
a0a218ba
EM
506static void
507del_conf_blacklist(const char *key, int parc, const char **parv)
508{
509 struct blacklist *bl = find_blacklist(parv[0]);
510 if(bl == NULL)
511 {
512 warn_opers(L_CRIT, "BUG: tried to remove nonexistent blacklist %s", parv[0]);
513 return;
514 }
515
516 delete_blacklist(bl);
517}
518
519static void
520del_conf_blacklist_all(const char *key, int parc, const char **parv)
521{
522 delete_all_blacklists();
523}
524
3f2695ac
EM
525static void
526add_conf_blacklist_timeout(const char *key, int parc, const char **parv)
527{
528 int timeout = atoi(parv[0]);
529
530 if(timeout < 0)
531 {
646e6567 532 warn_opers(L_CRIT, "BUG: blacklist timeout < 0 (value: %d)", timeout);
3f2695ac
EM
533 return;
534 }
535
536 blacklist_timeout = timeout;
537}
538
539struct auth_opts_handler blacklist_options[] =
540{
541 { "rbl", 4, add_conf_blacklist },
a0a218ba
EM
542 { "rbl_del", 1, del_conf_blacklist },
543 { "rbl_del_all", 0, del_conf_blacklist_all },
3f2695ac
EM
544 { "rbl_timeout", 1, add_conf_blacklist_timeout },
545 { NULL, 0, NULL },
546};
547
add80afd
EM
548struct auth_provider blacklist_provider =
549{
550 .id = PROVIDER_BLACKLIST,
551 .init = blacklists_init,
552 .destroy = blacklists_destroy,
553 .start = blacklists_start,
554 .cancel = blacklists_cancel,
a7d5aea1 555 .completed = blacklists_initiate,
3f2695ac 556 .opt_handlers = blacklist_options,
add80afd 557};