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