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