Debian Codegraph @

modules/chanserv/antiflood.c

raw
1 /*
2 * Copyright (c) 2013 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 #include "atheme.h"
22
23 DECLARE_MODULE_V1
24 (
25 "chanserv/antiflood", false, _modinit, _moddeinit,
26 PACKAGE_STRING,
27 VENDOR_STRING
28 );
29
30 static time_t antiflood_msg_time = 60;
31 static size_t antiflood_msg_count = 10;
32
33 #define METADATA_KEY_ENFORCE_METHOD "private:antiflood:enforce-method"
34
35 typedef enum {
36 ANTIFLOOD_ENFORCE_QUIET = 0,
37 ANTIFLOOD_ENFORCE_KICKBAN,
38 ANTIFLOOD_ENFORCE_KLINE,
39 ANTIFLOOD_ENFORCE_COUNT
40 } antiflood_enforce_method_t;
41
42 static antiflood_enforce_method_t antiflood_enforce_method = ANTIFLOOD_ENFORCE_QUIET;
43
44 typedef enum {
45 MQ_ENFORCE_NONE = 0,
46 MQ_ENFORCE_MSG,
47 MQ_ENFORCE_LINE,
48 } mqueue_enforce_strategy_t;
49
50 typedef struct {
51 char *name;
52 size_t max;
53 time_t last_used;
54 mowgli_list_t entries;
55 } mqueue_t;
56
57 typedef struct {
58 stringref source;
59 char *message;
60 time_t time;
61 mowgli_node_t node;
62 } msg_t;
63
64 static mowgli_heap_t *msg_heap = NULL;
65
66 static void
67 msg_destroy(msg_t *msg, mqueue_t *mq)
68 {
69 free(msg->message);
70 strshare_unref(msg->source);
71 mowgli_node_delete(&msg->node, &mq->entries);
72
73 mowgli_heap_free(msg_heap, msg);
74 }
75
76 static msg_t *
77 msg_create(mqueue_t *mq, user_t *u, const char *message)
78 {
79 msg_t *msg;
80
81 msg = mowgli_heap_alloc(msg_heap);
82 msg->message = sstrdup(message);
83 msg->time = CURRTIME;
84 msg->source = u->uid != NULL ? strshare_ref(u->uid) : strshare_ref(u->nick);
85
86 if (MOWGLI_LIST_LENGTH(&mq->entries) > mq->max)
87 {
88 msg_t *head_msg = mq->entries.head->data;
89 msg_destroy(head_msg, mq);
90 }
91
92 mowgli_node_add(msg, &msg->node, &mq->entries);
93 mq->last_used = CURRTIME;
94
95 return msg;
96 }
97
98 static mowgli_patricia_t *mqueue_trie = NULL;
99 static mowgli_heap_t *mqueue_heap = NULL;
100 static mowgli_eventloop_timer_t *mqueue_gc_timer = NULL;
101
102 static mqueue_t *
103 mqueue_create(const char *name)
104 {
105 mqueue_t *mq;
106
107 mq = mowgli_heap_alloc(mqueue_heap);
108 mq->name = sstrdup(name);
109 mq->last_used = CURRTIME;
110 mq->max = antiflood_msg_count;
111
112 mowgli_patricia_add(mqueue_trie, mq->name, mq);
113
114 return mq;
115 }
116
117 static void
118 mqueue_free(mqueue_t *mq)
119 {
120 mowgli_node_t *n, *tn;
121
122 MOWGLI_ITER_FOREACH_SAFE(n, tn, mq->entries.head)
123 {
124 msg_t *msg = n->data;
125
126 msg_destroy(msg, mq);
127 }
128
129 free(mq->name);
130 mowgli_heap_free(mqueue_heap, mq);
131 }
132
133 static mqueue_t *
134 mqueue_get(mychan_t *mc)
135 {
136 mqueue_t *mq;
137
138 mq = mowgli_patricia_retrieve(mqueue_trie, mc->name);
139 if (mq == NULL)
140 mq = mqueue_create(mc->name);
141
142 return mq;
143 }
144
145 static void
146 mqueue_destroy(mqueue_t *mq)
147 {
148 mowgli_patricia_delete(mqueue_trie, mq->name);
149
150 mqueue_free(mq);
151 }
152
153 static void
154 mqueue_trie_destroy_cb(const char *key, void *data, void *privdata)
155 {
156 mqueue_t *mq = data;
157
158 mqueue_free(mq);
159 }
160
161 static void
162 mqueue_gc(void *unused)
163 {
164 mowgli_patricia_iteration_state_t iter;
165 mqueue_t *mq;
166
167 MOWGLI_PATRICIA_FOREACH(mq, &iter, mqueue_trie)
168 {
169 if ((mq->last_used + 3600) < CURRTIME)
170 mqueue_destroy(mq);
171 }
172 }
173
174 static mqueue_enforce_strategy_t
175 mqueue_should_enforce(mqueue_t *mq)
176 {
177 msg_t *oldest, *newest;
178 time_t age_delta;
179
180 if (MOWGLI_LIST_LENGTH(&mq->entries) < mq->max)
181 return MQ_ENFORCE_NONE;
182
183 oldest = mq->entries.head->data;
184 newest = mq->entries.tail->data;
185
186 if (oldest == NULL || newest == NULL || oldest == newest)
187 return MQ_ENFORCE_NONE;
188
189 age_delta = newest->time - oldest->time;
190
191 if (age_delta <= antiflood_msg_time)
192 {
193 mowgli_node_t *n;
194 size_t msg_matches = 0, usr_matches = 0;
195 time_t usr_first_seen = 0;
196
197 MOWGLI_ITER_FOREACH(n, mq->entries.head)
198 {
199 msg_t *msg = n->data;
200
201 if (!strcasecmp(msg->message, newest->message))
202 msg_matches++;
203
204 if (msg->source == newest->source)
205 {
206 usr_matches++;
207
208 if (!usr_first_seen)
209 usr_first_seen = msg->time;
210 }
211 }
212
213 if (msg_matches > (antiflood_msg_count / 2))
214 return MQ_ENFORCE_MSG;
215
216 if (usr_matches > (antiflood_msg_count / 2) &&
217 ((newest->time - usr_first_seen) < antiflood_msg_time / 4))
218 return MQ_ENFORCE_LINE;
219 }
220
221 return MQ_ENFORCE_NONE;
222 }
223
224 static chanban_t *(*place_quietmask)(channel_t *c, int dir, const char *hostbuf) = NULL;
225
226 /* this requires `chanserv/quiet` to be loaded. */
227 static void
228 antiflood_enforce_quiet(user_t *u, channel_t *c)
229 {
230 char hostbuf[BUFSIZE];
231
232 mowgli_strlcpy(hostbuf, "*!*@", sizeof hostbuf);
233 mowgli_strlcat(hostbuf, u->vhost, sizeof hostbuf);
234
235 if (place_quietmask != NULL)
236 {
237 chanban_t *cb;
238
239 cb = place_quietmask(c, MTYPE_ADD, hostbuf);
240 if (cb != NULL)
241 cb->flags |= CBAN_ANTIFLOOD;
242 slog(LG_INFO, "ANTIFLOOD:ENFORCE:QUIET: \2%s!%s@%s\2 on \2%s\2", u->nick, u->user, u->vhost, c->name);
243 }
244 }
245
246 static void
247 antiflood_unenforce_banlike(channel_t *c)
248 {
249 mowgli_node_t *n, *tn;
250
251 MOWGLI_ITER_FOREACH_SAFE(n, tn, c->bans.head)
252 {
253 chanban_t *cb = n->data;
254
255 if (!(cb->flags & CBAN_ANTIFLOOD))
256 continue;
257
258 modestack_mode_param(chansvs.nick, c, MTYPE_DEL, cb->type, cb->mask);
259 chanban_delete(cb);
260 }
261 }
262
263 static void
264 antiflood_enforce_kickban(user_t *u, channel_t *c)
265 {
266 chanban_t *cb;
267
268 ban(chansvs.me->me, c, u);
269 remove_ban_exceptions(chansvs.me->me, c, u);
270 try_kick(chansvs.me->me, c, u, "Flooding");
271
272 /* poison tail */
273 if (c->bans.tail != NULL)
274 {
275 cb = c->bans.tail->data;
276 cb->flags |= CBAN_ANTIFLOOD;
277 }
278 else if (c->bans.head != NULL)
279 {
280 cb = c->bans.head->data;
281 cb->flags |= CBAN_ANTIFLOOD;
282 }
283 slog(LG_INFO, "ANTIFLOOD:ENFORCE:KICKBAN: \2%s!%s@%s\2 from \2%s\2", u->nick, u->user, u->vhost, c->name);
284 }
285
286 static void
287 antiflood_enforce_kline(user_t *u, channel_t *c)
288 {
289 kline_add_user(u, "Flooding", 86400, chansvs.nick);
290 slog(LG_INFO, "ANTIFLOOD:ENFORCE:AKILL: \2%s!%s@%s\2 from \2%s\2", u->nick, u->user, u->vhost, c->name);
291 }
292
293 typedef struct {
294 void (*enforce)(user_t *u, channel_t *c);
295 void (*unenforce)(channel_t *c);
296 } antiflood_enforce_method_impl_t;
297
298 static antiflood_enforce_method_impl_t antiflood_enforce_methods[ANTIFLOOD_ENFORCE_COUNT] = {
299 [ANTIFLOOD_ENFORCE_QUIET] = { &antiflood_enforce_quiet, &antiflood_unenforce_banlike },
300 [ANTIFLOOD_ENFORCE_KICKBAN] = { &antiflood_enforce_kickban, &antiflood_unenforce_banlike },
301 [ANTIFLOOD_ENFORCE_KLINE] = { &antiflood_enforce_kline },
302 };
303
304 static inline antiflood_enforce_method_impl_t *
305 antiflood_enforce_method_impl_get(mychan_t *mc)
306 {
307 metadata_t *md;
308
309 md = metadata_find(mc, METADATA_KEY_ENFORCE_METHOD);
310 if (md != NULL)
311 {
312 if (!strcasecmp(md->value, "QUIET"))
313 return &antiflood_enforce_methods[ANTIFLOOD_ENFORCE_QUIET];
314 else if (!strcasecmp(md->value, "KICKBAN"))
315 return &antiflood_enforce_methods[ANTIFLOOD_ENFORCE_KICKBAN];
316 else if (!strcasecmp(md->value, "AKILL"))
317 return &antiflood_enforce_methods[ANTIFLOOD_ENFORCE_KLINE];
318 }
319
320 return &antiflood_enforce_methods[antiflood_enforce_method];
321 }
322
323 static void
324 antiflood_unenforce_timer_cb(void *unused)
325 {
326 mowgli_patricia_iteration_state_t state;
327 mychan_t *mc;
328
329 MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
330 {
331 antiflood_enforce_method_impl_t *enf = antiflood_enforce_method_impl_get(mc);
332
333 if (mc->chan == NULL)
334 continue;
335
336 if (enf->unenforce != NULL)
337 enf->unenforce(mc->chan);
338 }
339 }
340
341 static mowgli_eventloop_timer_t *antiflood_unenforce_timer = NULL;
342
343 static void
344 on_channel_message(hook_cmessage_data_t *data)
345 {
346 chanuser_t *cu;
347 mychan_t *mc;
348 mqueue_t *mq;
349 msg_t *msg;
350
351 return_if_fail(data != NULL);
352 return_if_fail(data->msg != NULL);
353 return_if_fail(data->u != NULL);
354 return_if_fail(data->c != NULL);
355
356 cu = chanuser_find(data->c, data->u);
357 if (cu == NULL)
358 return;
359
360 mc = mychan_from(data->c);
361 if (mc == NULL)
362 return;
363
364 mq = mqueue_get(mc);
365 return_if_fail(mq != NULL);
366
367 msg = msg_create(mq, data->u, data->msg);
368
369 /* never enforce against any user who has special CSTATUS flags. */
370 if (cu->modes)
371 return;
372
373 /* do not enforce unless enforcement is specifically enabled */
374 if (!(mc->flags & MC_ANTIFLOOD))
375 return;
376
377 if (mqueue_should_enforce(mq) != MQ_ENFORCE_NONE)
378 {
379 antiflood_enforce_method_impl_t *enf = antiflood_enforce_method_impl_get(mc);
380
381 if (enf == NULL || enf->enforce == NULL)
382 return;
383
384 enf->enforce(data->u, data->c);
385 }
386 }
387
388 static void
389 on_channel_drop(mychan_t *mc)
390 {
391 mqueue_t *mq;
392
393 mq = mqueue_get(mc);
394 return_if_fail(mq != NULL);
395
396 mqueue_destroy(mq);
397 }
398
399 static void
400 cs_set_cmd_antiflood(sourceinfo_t *si, int parc, char *parv[])
401 {
402 mychan_t *mc;
403
404 if (!(mc = mychan_find(parv[0])))
405 {
406 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), parv[0]);
407 return;
408 }
409
410 /* allow opers with PRIV_CHAN_ADMIN to override this setting since it has
411 oper-specific settings (i.e. AKILL action) */
412 if (!chanacs_source_has_flag(mc, si, CA_SET) && !has_priv(si, PRIV_CHAN_ADMIN))
413 {
414 command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
415 return;
416 }
417
418 if (parv[1] == NULL)
419 {
420 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET ANTIFLOOD");
421 return;
422 }
423
424 if (!strcasecmp(parv[1], "OFF"))
425 {
426 mc->flags &= ~MC_ANTIFLOOD;
427 metadata_delete(mc, METADATA_KEY_ENFORCE_METHOD);
428
429 logcommand(si, CMDLOG_SET, "ANTIFLOOD:NONE: \2%s\2", mc->name);
430 command_success_nodata(si, _("Flood protection turned off for \2%s\2."), mc->name);
431 return;
432 }
433 else if (!strcasecmp(parv[1], "ON"))
434 {
435 if (MC_ANTIFLOOD & mc->flags)
436 {
437 command_fail(si, fault_nochange, _("The \2%s\2 flag is already set for channel \2%s\2."), "ANTIFLOOD", mc->name);
438 return;
439 }
440 mc->flags |= MC_ANTIFLOOD;
441 metadata_delete(mc, METADATA_KEY_ENFORCE_METHOD);
442
443 logcommand(si, CMDLOG_SET, "ANTIFLOOD: %s (%s)", mc->name, "DEFAULT");
444 command_success_nodata(si, _("Flood protection turned on for \2%s\2 with default settings."), mc->name);
445 return;
446 }
447 else if (!strcasecmp(parv[1], "QUIET"))
448 {
449 mc->flags |= MC_ANTIFLOOD;
450 metadata_add(mc, METADATA_KEY_ENFORCE_METHOD, "QUIET");
451
452 logcommand(si, CMDLOG_SET, "ANTIFLOOD: %s (%s)", mc->name, "QUIET");
453 command_success_nodata(si, _("Flood protection turned on for \2%s\2 with \2%s\2 action."), mc->name, "QUIET");
454 return;
455 }
456 else if (!strcasecmp(parv[1], "KICKBAN"))
457 {
458 mc->flags |= MC_ANTIFLOOD;
459 metadata_add(mc, METADATA_KEY_ENFORCE_METHOD, "KICKBAN");
460
461 logcommand(si, CMDLOG_SET, "ANTIFLOOD: %s (%s)", mc->name, "KICKBAN");
462 command_success_nodata(si, _("Flood protection turned on for \2%s\2 with \2%s\2 action."), mc->name, "KICKBAN");
463 return;
464 }
465 else if (!strcasecmp(parv[1], "AKILL") || !strcasecmp(parv[1], "KLINE"))
466 {
467 if (has_priv(si, PRIV_AKILL))
468 {
469 mc->flags |= MC_ANTIFLOOD;
470 metadata_add(mc, METADATA_KEY_ENFORCE_METHOD, "AKILL");
471
472 logcommand(si, CMDLOG_SET, "ANTIFLOOD: %s (%s)", mc->name, "AKILL");
473 command_success_nodata(si, _("Flood protection turned on for \2%s\2 with \2%s\2 action."), mc->name, "AKILL");
474 return;
475 }
476 else
477 {
478 command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
479 return;
480 }
481 }
482 }
483
484 static command_t cs_set_antiflood = {
485 "ANTIFLOOD", N_("Set anti-flood action"), AC_NONE, 2,
486 cs_set_cmd_antiflood, { .path = "cservice/set_antiflood" }
487 };
488
489 mowgli_patricia_t **cs_set_cmdtree;
490
491 static int
492 c_ci_antiflood_enforce_method(mowgli_config_file_entry_t *ce)
493 {
494 if (ce->vardata == NULL)
495 {
496 conf_report_warning(ce, "no parameter for configuration option");
497 return 0;
498 }
499
500 if (!strcasecmp(ce->vardata, "QUIET"))
501 antiflood_enforce_method = ANTIFLOOD_ENFORCE_QUIET;
502 else if (!strcasecmp(ce->vardata, "KICKBAN"))
503 antiflood_enforce_method = ANTIFLOOD_ENFORCE_KICKBAN;
504 else if (!strcasecmp(ce->vardata, "AKILL") || !strcasecmp(ce->vardata, "KLINE"))
505 antiflood_enforce_method = ANTIFLOOD_ENFORCE_KLINE;
506
507 return 0;
508 }
509
510 void
511 _modinit(module_t *m)
512 {
513 MODULE_TRY_REQUEST_SYMBOL(m, cs_set_cmdtree, "chanserv/set_core", "cs_set_cmdtree");
514
515 /* attempt to pull in the place_quietmask() routine from chanserv/quiet,
516 we don't see it as a hardfail because we can run without QUIET support. */
517 if (module_request("chanserv/quiet"))
518 {
519 place_quietmask = module_locate_symbol("chanserv/quiet", "place_quietmask");
520 if (place_quietmask == NULL)
521 antiflood_enforce_method = ANTIFLOOD_ENFORCE_KICKBAN;
522 }
523
524 hook_add_event("channel_message");
525 hook_add_channel_message(on_channel_message);
526
527 hook_add_event("channel_drop");
528 hook_add_channel_drop(on_channel_drop);
529
530 msg_heap = sharedheap_get(sizeof(msg_t));
531
532 mqueue_heap = sharedheap_get(sizeof(mqueue_t));
533 mqueue_trie = mowgli_patricia_create(irccasecanon);
534 mqueue_gc_timer = mowgli_timer_add(base_eventloop, "mqueue_gc", mqueue_gc, NULL, 300);
535
536 antiflood_unenforce_timer = mowgli_timer_add(base_eventloop, "antiflood_unenforce", antiflood_unenforce_timer_cb, NULL, 3600);
537
538 command_add(&cs_set_antiflood, *cs_set_cmdtree);
539
540 add_conf_item("ANTIFLOOD_ENFORCE_METHOD", &chansvs.me->conf_table, c_ci_antiflood_enforce_method);
541 }
542
543 void
544 _moddeinit(module_unload_intent_t intent)
545 {
546 command_delete(&cs_set_antiflood, *cs_set_cmdtree);
547
548 hook_del_channel_message(on_channel_message);
549 hook_del_channel_drop(on_channel_drop);
550
551 mowgli_patricia_destroy(mqueue_trie, mqueue_trie_destroy_cb, NULL);
552 mowgli_timer_destroy(base_eventloop, mqueue_gc_timer);
553 mowgli_timer_destroy(base_eventloop, antiflood_unenforce_timer);
554
555 del_conf_item("ANTIFLOOD_ENFORCE_METHOD", &chansvs.me->conf_table);
556 }
557
558 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
559 * vim:ts=8
560 * vim:sw=8
561 * vim:noexpandtab
562 */