Debian Codegraph @

modules/contrib/src/dnsbl.c

raw
1 /*
2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
4 *
5 * Copyright (C) 2016 Atheme Development Group <http://atheme.github.io>
6 * Copyright (C) 2006-2008 charybdis development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* To configure/use, add a block to the general{} section of your atheme.conf
36 * like this:
37 *
38 * blacklists {
39 * "dnsbl.dronebl.org";
40 * "rbl.efnetrbl.org";
41 * };
42 */
43
44 #include "atheme-compat.h"
45
46 #if (CURRENT_ABI_REVISION < 730000)
47
48 #include "conf.h"
49
50 /* A configured DNSBL */
51 struct Blacklist {
52 unsigned int status; /* If CONF_ILLEGAL, delete when no clients */
53 int refcount;
54 char host[IRCD_RES_HOSTLEN + 1];
55 unsigned int hits;
56 time_t lastwarning;
57 };
58
59 /* A lookup in progress for a particular DNSBL for a particular client */
60 struct BlacklistClient {
61 struct Blacklist *blacklist;
62 user_t *u;
63 mowgli_dns_query_t dns_query;
64 mowgli_node_t node;
65 };
66
67 struct dnsbl_exempt_ {
68 char *ip;
69 time_t exempt_ts;
70 char *creator;
71 char *reason;
72 };
73
74 typedef struct dnsbl_exempt_ dnsbl_exempt_t;
75
76 static mowgli_list_t blacklist_list = { NULL, NULL, 0 };
77 static mowgli_list_t dnsbl_elist = { NULL, NULL, 0 };
78
79 static mowgli_patricia_t **os_set_cmdtree = NULL;
80 static mowgli_dns_t *dns_base = NULL;
81 static char *action = NULL;
82
83 static inline mowgli_list_t *
84 dnsbl_queries(user_t *u)
85 {
86 mowgli_list_t *l;
87
88 return_val_if_fail(u != NULL, NULL);
89
90 l = privatedata_get(u, "dnsbl:queries");
91 if (l != NULL)
92 return l;
93
94 l = mowgli_list_create();
95 privatedata_set(u, "dnsbl:queries", l);
96
97 return l;
98 }
99
100 static void
101 os_cmd_set_dnsblaction(sourceinfo_t *si, int parc, char *parv[])
102 {
103 char *act = parv[0];
104
105 if (!act)
106 {
107 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET DNSBLACTION");
108 command_fail(si, fault_needmoreparams, _("Syntax: SET DNSBLACTION <action> <SNOOP|KLINE|NOTIFY|NONE>"));
109 return;
110 }
111
112 if (!strcasecmp("SNOOP", act) || !strcasecmp("KLINE", act) || !strcasecmp("NOTIFY", act))
113 {
114 action = sstrdup(act);
115 command_success_nodata(si, _("DNSBLACTION successfully set to \2%s\2"), act);
116 logcommand(si, CMDLOG_ADMIN, "SET:DNSBLACTION: \2%s\2", act);
117 }
118 else if (!strcasecmp("NONE", act))
119 {
120 action = NULL;
121 command_success_nodata(si, _("DNSBLACTION successfully set to \2%s\2"), act);
122 logcommand(si, CMDLOG_ADMIN, "SET:DNSBLACTION: \2%s\2", act);
123 }
124 else
125 command_fail(si, fault_badparams, _("Invalid action given."));
126 }
127
128 static void
129 os_cmd_dnsblexempt(sourceinfo_t *si, int parc, char *parv[])
130 {
131 char *command = parv[0];
132 char *ip = parv[1];
133 char *reason = parv[2];
134 mowgli_node_t *n, *tn;
135 dnsbl_exempt_t *de;
136
137 if (!command)
138 {
139 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DNSBLEXEMPT");
140 command_fail(si, fault_needmoreparams, _("Syntax: DNSBLEXEMPT ADD|DEL|LIST [ip] [reason]"));
141 return;
142 }
143
144 if (!strcasecmp("ADD", command))
145 {
146
147 if (!ip || !reason)
148 {
149 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DNSBLEXEMPT ADD");
150 command_fail(si, fault_needmoreparams, _("Syntax: DNSBLEXEMPT ADD <ip> <reason>"));
151 return;
152 }
153
154 MOWGLI_ITER_FOREACH(n, dnsbl_elist.head)
155 {
156 de = n->data;
157
158 if (!irccasecmp(de->ip, ip))
159 {
160 command_success_nodata(si, _("\2%s\2 has already been entered into "
161 "the DNSBL exempts list."), ip);
162 return;
163 }
164 }
165
166 de = smalloc(sizeof(dnsbl_exempt_t));
167 de->exempt_ts = CURRTIME;;
168 de->creator = sstrdup(get_source_name(si));
169 de->reason = sstrdup(reason);
170 de->ip = sstrdup(ip);
171 mowgli_node_add(de, mowgli_node_create(), &dnsbl_elist);
172
173 command_success_nodata(si, _("You have added \2%s\2 to the DNSBL exempts list."), ip);
174 logcommand(si, CMDLOG_ADMIN, "DNSBL:EXEMPT:ADD: \2%s\2 \2%s\2", ip, reason);
175 }
176 else if (!strcasecmp("DEL", command))
177 {
178
179 if (!ip)
180 {
181 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DNSBLEXEMPT DEL");
182 command_fail(si, fault_needmoreparams, _("Syntax: DNSBLEXEMPT DEL <ip>"));
183 return;
184 }
185
186 MOWGLI_ITER_FOREACH_SAFE(n, tn, dnsbl_elist.head)
187 {
188 de = n->data;
189
190 if (!irccasecmp(de->ip, ip))
191 {
192 logcommand(si, CMDLOG_SET, "DNSBL:EXEMPT:DEL: \2%s\2", de->ip);
193 command_success_nodata(si, _("DNSBL Exempt IP \2%s\2 has been deleted."), de->ip);
194
195 mowgli_node_delete(n, &dnsbl_elist);
196 sfree(de->creator);
197 sfree(de->reason);
198 sfree(de->ip);
199 sfree(de);
200
201 return;
202 }
203 }
204
205 command_success_nodata(si, _("IP \2%s\2 not found in DNSBL Exempt database."), ip);
206 }
207 else if (!strcasecmp("LIST", command))
208 {
209 char buf[BUFSIZE];
210 struct tm tm;
211
212 MOWGLI_ITER_FOREACH(n, dnsbl_elist.head)
213 {
214 de = n->data;
215
216 tm = *localtime(&de->exempt_ts);
217 strftime(buf, BUFSIZE, TIME_FORMAT, &tm);
218 command_success_nodata(si, _("IP: \2%s\2, Reason: \2%s\2 (%s - %s)"),
219 de->ip, de->reason, de->creator, buf);
220 }
221
222 command_success_nodata(si, _("End of list."));
223 logcommand(si, CMDLOG_GET, "DNSBL:EXEMPT:LIST");
224 }
225 else
226 {
227 command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "DNSBLEXEMPT");
228 command_fail(si, fault_needmoreparams, _("Syntax: DNSBLEXEMPT ADD|DEL|LIST [ip] [reason]"));
229 }
230 }
231
232 static void
233 dnsbl_hit(user_t *u, struct Blacklist *blptr)
234 {
235 service_t *const svs = service_find("operserv");
236
237 if (!strcasecmp("SNOOP", action))
238 {
239 slog(LG_INFO, "DNSBL: \2%s\2!%s@%s [%s] is listed in DNS Blacklist %s.",
240 u->nick, u->user, u->host, u->gecos, blptr->host);
241
242 /* abort_blacklist_queries(u); */
243 }
244 else if (!strcasecmp("NOTIFY", action))
245 {
246 slog(LG_INFO, "DNSBL: \2%s\2!%s@%s [%s] is listed in DNS Blacklist %s.",
247 u->nick, u->user, u->host, u->gecos, blptr->host);
248
249 notice(svs->nick, u->nick, "Your IP address %s is listed in DNS Blacklist %s", u->ip, blptr->host);
250
251 /* abort_blacklist_queries(u); */
252 }
253 else if (!strcasecmp("KLINE", action))
254 {
255 if (! (u->flags & UF_KLINESENT)) {
256 slog(LG_INFO, "DNSBL: k-lining \2%s\2!%s@%s [%s] who is listed in DNS Blacklist %s.",
257 u->nick, u->user, u->host, u->gecos, blptr->host);
258
259 /* abort_blacklist_queries(u); */
260 notice(svs->nick, u->nick, "Your IP address %s is listed in DNS Blacklist %s", u->ip, blptr->host);
261 kline_add(u->user, u->host, "Banned (DNS Blacklist)", 86400, "*");
262 u->flags |= UF_KLINESENT;
263 }
264 }
265 }
266
267 static void
268 blacklist_dns_callback(mowgli_dns_reply_t *reply, int result, void *vptr)
269 {
270 struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
271 int listed = 0;
272 mowgli_list_t *l;
273
274 if (blcptr == NULL)
275 return;
276
277 if (blcptr->u == NULL)
278 {
279 sfree(blcptr);
280 return;
281 }
282
283 if (reply != NULL)
284 {
285 /* only accept 127.x.y.z as a listing */
286 if (reply->addr.addr.ss_family == AF_INET &&
287 !memcmp(&((struct sockaddr_in *)&reply->addr.addr)->sin_addr, "\177", 1))
288 listed++;
289 else if (blcptr->blacklist->lastwarning + 3600 < CURRTIME)
290 {
291 slog(LG_DEBUG, "Garbage reply from blacklist %s", blcptr->blacklist->host);
292 blcptr->blacklist->lastwarning = CURRTIME;
293 }
294 }
295
296 /* they have a blacklist entry for this client */
297 if (listed)
298 {
299 dnsbl_hit(blcptr->u, blcptr->blacklist);
300 return;
301 }
302
303 l = dnsbl_queries(blcptr->u);
304 mowgli_node_delete(&blcptr->node, l);
305
306 sfree(blcptr);
307 }
308
309 /* XXX: no IPv6 implementation, not to concerned right now though. */
310 static void
311 initiate_blacklist_dnsquery(struct Blacklist *blptr, user_t *u)
312 {
313 struct BlacklistClient *blcptr = smalloc(sizeof(struct BlacklistClient));
314 char buf[IRCD_RES_HOSTLEN + 1];
315 unsigned int ip[4];
316 mowgli_list_t *l;
317
318 blcptr->blacklist = blptr;
319 blcptr->u = u;
320
321 blcptr->dns_query.ptr = blcptr;
322 blcptr->dns_query.callback = blacklist_dns_callback;
323
324 /* A sscanf worked fine for chary for many years, it'll be fine here */
325 sscanf(u->ip, "%u.%u.%u.%u", &ip[3], &ip[2], &ip[1], &ip[0]);
326
327 /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
328 snprintf(buf, sizeof buf, "%u.%u.%u.%u.%s", ip[0], ip[1], ip[2], ip[3], blptr->host);
329
330 mowgli_dns_gethost_byname(dns_base, buf, &blcptr->dns_query, MOWGLI_DNS_T_A);
331
332 l = dnsbl_queries(u);
333 mowgli_node_add(blcptr, &blcptr->node, l);
334 blptr->refcount++;
335 }
336
337 static void
338 lookup_blacklists(user_t *u)
339 {
340 mowgli_node_t *n;
341
342 MOWGLI_ITER_FOREACH(n, blacklist_list.head)
343 {
344 struct Blacklist *blptr = (struct Blacklist *) n->data;
345 blptr->status = 0;
346
347 if (u == NULL)
348 return;
349
350 initiate_blacklist_dnsquery(blptr, u);
351 }
352 }
353
354 static void
355 os_cmd_dnsblscan(sourceinfo_t *si, int parc, char *parv[])
356 {
357 char *user = parv[0];
358 user_t *u;
359
360 if (!user)
361 {
362 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DNSBLSCAN");
363 command_fail(si, fault_needmoreparams, _("Syntax: DNSBLSCAN <nickname>"));
364 return;
365 }
366
367 if ((u = user_find_named(user)))
368 {
369 lookup_blacklists(u);
370 logcommand(si, CMDLOG_ADMIN, "DNSBLSCAN: %s", user);
371 command_success_nodata(si, _("%s has been scanned."), user);
372 }
373 else
374 command_fail(si, fault_badparams, _("User %s is not on the network, you cannot scan them."), user);
375 }
376
377 /* private interfaces */
378 static struct Blacklist *
379 find_blacklist(char *name)
380 {
381 mowgli_node_t *n;
382
383 MOWGLI_ITER_FOREACH(n, blacklist_list.head)
384 {
385 struct Blacklist *blptr = (struct Blacklist *) n->data;
386
387 if (!strcasecmp(blptr->host, name))
388 return blptr;
389 }
390
391 return NULL;
392 }
393
394 /* public interfaces */
395 static struct Blacklist *
396 new_blacklist(char *name)
397 {
398 struct Blacklist *blptr;
399
400 if (name == NULL)
401 return NULL;
402
403 blptr = find_blacklist(name);
404
405 if (blptr == NULL)
406 {
407 blptr = smalloc(sizeof(struct Blacklist));
408 mowgli_node_add(blptr, mowgli_node_create(), &blacklist_list);
409 }
410
411 mowgli_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
412 blptr->lastwarning = 0;
413
414 return blptr;
415 }
416
417 /* This appears to be unnecessary on Atheme and only causes crashes so #if 0
418 * it out, at least for now. --jdhore
419 */
420 #if 0
421 static void
422 abort_blacklist_queries(user_t *u)
423 {
424 mowgli_node_t *n, *tn;
425 mowgli_list_t *l;
426 struct BlacklistClient *blcptr;
427
428 if (u == NULL)
429 return;
430
431 l = dnsbl_queries(u);
432
433 MOWGLI_ITER_FOREACH_SAFE(n, tn, l->head)
434 {
435 blcptr = n->data;
436 mowgli_node_delete(&blcptr->node, l);
437 unref_blacklist(blcptr->blacklist);
438 mowgli_dns_delete_query(dns_base, &blcptr->dns_query);
439 sfree(blcptr);
440 }
441 }
442 #endif
443
444 static void
445 destroy_blacklists(void)
446 {
447 mowgli_node_t *n, *tn;
448 struct Blacklist *blptr;
449
450 MOWGLI_ITER_FOREACH_SAFE(n, tn, blacklist_list.head)
451 {
452 blptr = n->data;
453 blptr->hits = 0; /* keep it simple and consistent */
454 sfree(n->data);
455 mowgli_node_delete(n, &blacklist_list);
456 mowgli_node_free(n);
457 }
458 }
459
460 static int
461 dnsbl_config_handler(mowgli_config_file_entry_t *ce)
462 {
463 mowgli_config_file_entry_t *cce;
464
465 MOWGLI_ITER_FOREACH(cce, ce->entries)
466 {
467 char *line = sstrdup(cce->varname);
468 new_blacklist(line);
469 sfree(line);
470 }
471
472 return 0;
473 }
474
475 static void
476 dnsbl_config_purge(void *unused)
477 {
478 destroy_blacklists();
479 }
480
481 static void
482 check_dnsbls(hook_user_nick_t *data)
483 {
484 user_t *u = data->u;
485 mowgli_node_t *n;
486
487 if (!u)
488 return;
489
490 if (is_internal_client(u))
491 return;
492
493 if (!action)
494 return;
495
496 MOWGLI_ITER_FOREACH(n, dnsbl_elist.head)
497 {
498 dnsbl_exempt_t *de = n->data;
499
500 if (!irccasecmp(de->ip, u->ip))
501 return;
502 }
503
504 lookup_blacklists(u);
505 }
506
507 static void
508 osinfo_hook(sourceinfo_t *si)
509 {
510 mowgli_node_t *n;
511
512 if (action)
513 command_success_nodata(si, _("Action taken when a user is an a DNSBL: %s"), action);
514 else
515 command_success_nodata(si, _("Action taken when a user is an a DNSBL: %s"), _("None"));
516
517 MOWGLI_ITER_FOREACH(n, blacklist_list.head)
518 {
519 struct Blacklist *blptr = (struct Blacklist *) n->data;
520
521 command_success_nodata(si, _("Using Blacklist: %s"), blptr->host);
522 }
523 }
524
525 static void
526 write_dnsbl_exempt_db(database_handle_t *db)
527 {
528 mowgli_node_t *n;
529
530 MOWGLI_ITER_FOREACH(n, dnsbl_elist.head)
531 {
532 dnsbl_exempt_t *de = n->data;
533
534 db_start_row(db, "BW");
535 db_write_word(db, de->ip);
536 db_write_time(db, de->exempt_ts);
537 db_write_word(db, de->creator);
538 db_write_word(db, de->reason);
539 db_commit_row(db);
540 }
541 }
542
543 static void
544 db_h_ble(database_handle_t *db, const char *type)
545 {
546 const char *ip = db_sread_word(db);
547 time_t exempt_ts = db_sread_time(db);
548 const char *creator = db_sread_word(db);
549 const char *reason = db_sread_word(db);
550
551 dnsbl_exempt_t *de = smalloc(sizeof(dnsbl_exempt_t));
552
553 de->ip = sstrdup(ip);
554 de->exempt_ts = exempt_ts;
555 de->creator = sstrdup(creator);
556 de->reason = sstrdup(reason);
557
558 mowgli_node_add(de, mowgli_node_create(), &dnsbl_elist);
559 }
560
561 static command_t os_set_dnsblaction = {
562 "DNSBLACTION",
563 N_("Changes what happens to a user when they hit a DNSBL."),
564 PRIV_USER_ADMIN,
565 1,
566 &os_cmd_set_dnsblaction,
567 { .path = "contrib/set_dnsblaction" },
568 };
569
570 static command_t os_dnsblexempt = {
571 "DNSBLEXEMPT",
572 N_("Manage the list of IP's exempt from DNSBL checking."),
573 PRIV_USER_ADMIN,
574 3,
575 &os_cmd_dnsblexempt,
576 { .path = "contrib/dnsblexempt" },
577 };
578
579 static command_t os_dnsblscan = {
580 "DNSBLSCAN",
581 N_("Manually scan if a user is in a DNSBL."),
582 PRIV_USER_ADMIN,
583 1,
584 &os_cmd_dnsblscan,
585 { .path = "contrib/dnsblscan" },
586 };
587
588 static void
589 mod_init(module_t *m)
590 {
591 MODULE_CONFLICT(m, "proxyscan/dnsbl");
592
593 MODULE_TRY_REQUEST_SYMBOL(m, os_set_cmdtree, "operserv/set", "os_set_cmdtree");
594
595 if (!module_find_published("backend/opensex"))
596 {
597 (void) slog(LG_ERROR, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
598 m->mflags |= MODFLAG_FAIL;
599 return;
600 }
601
602 if (! (dns_base = mowgli_dns_create(base_eventloop, MOWGLI_DNS_TYPE_ASYNC)))
603 {
604 (void) slog(LG_ERROR, "%s: failed to create Mowgli DNS resolver object", m->name);
605 m->mflags |= MODFLAG_FAIL;
606 return;
607 }
608
609 hook_add_db_write(write_dnsbl_exempt_db);
610
611 db_register_type_handler("BLE", db_h_ble);
612
613 service_named_bind_command("operserv", &os_dnsblexempt);
614 service_named_bind_command("operserv", &os_dnsblscan);
615
616 hook_add_event("config_purge");
617 hook_add_config_purge(dnsbl_config_purge);
618
619 hook_add_event("user_add");
620 hook_add_user_add(check_dnsbls);
621
622 hook_add_event("operserv_info");
623 hook_add_operserv_info(osinfo_hook);
624
625 add_dupstr_conf_item("dnsbl_action", &conf_gi_table, 0, &action, NULL);
626 add_conf_item("BLACKLISTS", &conf_gi_table, dnsbl_config_handler);
627 command_add(&os_set_dnsblaction, *os_set_cmdtree);
628 }
629
630 static void
631 mod_deinit(module_unload_intent_t intent)
632 {
633 (void) mowgli_dns_destroy(dns_base);
634
635 hook_del_db_write(write_dnsbl_exempt_db);
636 hook_del_user_add(check_dnsbls);
637 hook_del_config_purge(dnsbl_config_purge);
638 hook_del_operserv_info(osinfo_hook);
639
640 db_unregister_type_handler("BLE");
641
642 del_conf_item("dnsbl_action", &conf_gi_table);
643 del_conf_item("BLACKLISTS", &conf_gi_table);
644 command_delete(&os_set_dnsblaction, *os_set_cmdtree);
645 service_named_unbind_command("operserv", &os_dnsblexempt);
646 service_named_unbind_command("operserv", &os_dnsblscan);
647 }
648
649 #else /* (CURRENT_ABI_REVISION < 730000) */
650
651 static void
652 mod_init(module_t *m)
653 {
654 (void) slog(LG_INFO, "%s: this module has been replaced with modules/proxyscan/dnsbl on v7.3", m->name);
655 (void) slog(LG_INFO, "%s: loading that module instead. Please update your configuration file.", m->name);
656
657 MODULE_TRY_REQUEST_DEPENDENCY(m, "proxyscan/dnsbl");
658 }
659
660 static void
661 mod_deinit(module_unload_intent_t intent)
662 {
663
664 }
665
666 #endif /* (CURRENT_ABI_REVISION >= 730000) */
667
668 SIMPLE_DECLARE_MODULE_V1("contrib/dnsbl", MODULE_UNLOAD_CAPABILITY_OK)