Debian Codegraph @

modules/operserv/clones.c

raw
1 /*
2 * Copyright (c) 2006-2007 Atheme Development Group
3 * Rights to this code are documented in doc/LICENCE.
4 *
5 * This file contains functionality implementing clone detection.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "operserv/clones", true, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 #define CLONESDB_VERSION 3
19 #define CLONES_GRACE_TIMEPERIOD 180
20
21 static void clones_newuser(hook_user_nick_t *data);
22 static void clones_userquit(user_t *u);
23 static void clones_configready(void *unused);
24
25 static void os_cmd_clones(sourceinfo_t *si, int parc, char *parv[]);
26 static void os_cmd_clones_kline(sourceinfo_t *si, int parc, char *parv[]);
27 static void os_cmd_clones_list(sourceinfo_t *si, int parc, char *parv[]);
28 static void os_cmd_clones_addexempt(sourceinfo_t *si, int parc, char *parv[]);
29 static void os_cmd_clones_delexempt(sourceinfo_t *si, int parc, char *parv[]);
30 static void os_cmd_clones_setexempt(sourceinfo_t *si, int parc, char *parv[]);
31 static void os_cmd_clones_listexempt(sourceinfo_t *si, int parc, char *parv[]);
32 static void os_cmd_clones_duration(sourceinfo_t *si, int parc, char *parv[]);
33
34 static void write_exemptdb(database_handle_t *db);
35
36
37 static void db_h_ck(database_handle_t *db, const char *type);
38 static void db_h_cd(database_handle_t *db, const char *type);
39 static void db_h_gr(database_handle_t *db, const char *type);
40 static void db_h_ex(database_handle_t *db, const char *type);
41 static void db_h_clonesdbv(database_handle_t *db, const char *type);
42
43 mowgli_patricia_t *os_clones_cmds;
44
45 service_t *serviceinfo;
46
47 static mowgli_list_t clone_exempts;
48 bool kline_enabled;
49 unsigned int grace_count;
50 mowgli_patricia_t *hostlist;
51 mowgli_heap_t *hostentry_heap;
52 static long kline_duration;
53 static int clones_allowed, clones_warn;
54 static unsigned int clones_dbversion = 1;
55
56 typedef struct cexcept_ cexcept_t;
57 struct cexcept_
58 {
59 char *ip;
60 int allowed;
61 int warn;
62 char *reason;
63 long expires;
64 };
65
66 typedef struct hostentry_ hostentry_t;
67 struct hostentry_
68 {
69 char ip[HOSTIPLEN];
70 mowgli_list_t clients;
71 time_t firstkill;
72 unsigned int gracekills;
73 };
74
75 static inline bool cexempt_expired(cexcept_t *c)
76 {
77 if (c && c->expires && CURRTIME > c->expires)
78 return true;
79
80 return false;
81 }
82
83 command_t os_clones = { "CLONES", N_("Manages network wide clones."), PRIV_AKILL, 5, os_cmd_clones, { .path = "oservice/clones" } };
84
85 command_t os_clones_kline = { "KLINE", N_("Enables/disables klines for excessive clones."), AC_NONE, 1, os_cmd_clones_kline, { .path = "" } };
86 command_t os_clones_list = { "LIST", N_("Lists clones on the network."), AC_NONE, 0, os_cmd_clones_list, { .path = "" } };
87 command_t os_clones_addexempt = { "ADDEXEMPT", N_("Adds a clones exemption."), AC_NONE, 3, os_cmd_clones_addexempt, { .path = "" } };
88 command_t os_clones_delexempt = { "DELEXEMPT", N_("Deletes a clones exemption."), AC_NONE, 1, os_cmd_clones_delexempt, { .path = "" } };
89 command_t os_clones_setexempt = { "SETEXEMPT", N_("Sets a clone exemption details."), AC_NONE, 1, os_cmd_clones_setexempt, { .path = "" } };
90 command_t os_clones_listexempt = { "LISTEXEMPT", N_("Lists clones exemptions."), AC_NONE, 0, os_cmd_clones_listexempt, { .path = "" } };
91 command_t os_clones_duration = { "DURATION", N_("Sets a custom duration to ban clones for."), AC_NONE, 1, os_cmd_clones_duration, { .path = "" } };
92
93 static void clones_configready(void *unused)
94 {
95 clones_allowed = config_options.default_clone_allowed;
96 clones_warn = config_options.default_clone_warn;
97 }
98
99 void _modinit(module_t *m)
100 {
101 user_t *u;
102 mowgli_patricia_iteration_state_t state;
103
104 if (!module_find_published("backend/opensex"))
105 {
106 slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
107 m->mflags = MODTYPE_FAIL;
108 return;
109 }
110
111 service_named_bind_command("operserv", &os_clones);
112
113 os_clones_cmds = mowgli_patricia_create(strcasecanon);
114
115 command_add(&os_clones_kline, os_clones_cmds);
116 command_add(&os_clones_list, os_clones_cmds);
117 command_add(&os_clones_addexempt, os_clones_cmds);
118 command_add(&os_clones_delexempt, os_clones_cmds);
119 command_add(&os_clones_setexempt, os_clones_cmds);
120 command_add(&os_clones_listexempt, os_clones_cmds);
121 command_add(&os_clones_duration, os_clones_cmds);
122
123 hook_add_event("config_ready");
124 hook_add_config_ready(clones_configready);
125
126 hook_add_event("user_add");
127 hook_add_user_add(clones_newuser);
128 hook_add_event("user_delete");
129 hook_add_user_delete(clones_userquit);
130 hook_add_db_write(write_exemptdb);
131
132 db_register_type_handler("CLONES-DBV", db_h_clonesdbv);
133 db_register_type_handler("CLONES-CK", db_h_ck);
134 db_register_type_handler("CLONES-CD", db_h_cd);
135 db_register_type_handler("CLONES-GR", db_h_gr);
136 db_register_type_handler("CLONES-EX", db_h_ex);
137
138 hostlist = mowgli_patricia_create(noopcanon);
139 hostentry_heap = mowgli_heap_create(sizeof(hostentry_t), HEAP_USER, BH_NOW);
140
141 kline_duration = 3600; /* set a default */
142
143 serviceinfo = service_find("operserv");
144
145
146 /* add everyone to host hash */
147 MOWGLI_PATRICIA_FOREACH(u, &state, userlist)
148 {
149 clones_newuser(&(hook_user_nick_t){ .u = u });
150 }
151 }
152
153 static void free_hostentry(const char *key, void *data, void *privdata)
154 {
155 mowgli_node_t *n, *tn;
156 hostentry_t *he = data;
157
158 MOWGLI_ITER_FOREACH_SAFE(n, tn, he->clients.head)
159 {
160 mowgli_node_delete(n, &he->clients);
161 mowgli_node_free(n);
162 }
163
164 mowgli_heap_free(hostentry_heap, he);
165 }
166
167 void _moddeinit(module_unload_intent_t intent)
168 {
169 mowgli_node_t *n, *tn;
170
171 mowgli_patricia_destroy(hostlist, free_hostentry, NULL);
172 mowgli_heap_destroy(hostentry_heap);
173
174 MOWGLI_ITER_FOREACH_SAFE(n, tn, clone_exempts.head)
175 {
176 cexcept_t *c = n->data;
177
178 free(c->ip);
179 free(c->reason);
180 free(c);
181
182 mowgli_node_delete(n, &clone_exempts);
183 mowgli_node_free(n);
184 }
185
186 service_named_unbind_command("operserv", &os_clones);
187
188 command_delete(&os_clones_kline, os_clones_cmds);
189 command_delete(&os_clones_list, os_clones_cmds);
190 command_delete(&os_clones_addexempt, os_clones_cmds);
191 command_delete(&os_clones_delexempt, os_clones_cmds);
192 command_delete(&os_clones_setexempt, os_clones_cmds);
193 command_delete(&os_clones_listexempt, os_clones_cmds);
194
195 command_delete(&os_clones_duration, os_clones_cmds);
196
197 hook_del_user_add(clones_newuser);
198 hook_del_user_delete(clones_userquit);
199 hook_del_db_write(write_exemptdb);
200 hook_del_config_ready(clones_configready);
201
202 db_unregister_type_handler("CLONES-DBV");
203 db_unregister_type_handler("CLONES-CK");
204 db_unregister_type_handler("CLONES-CD");
205 db_unregister_type_handler("CLONES-EX");
206
207 mowgli_patricia_destroy(os_clones_cmds, NULL, NULL);
208 }
209
210 static void write_exemptdb(database_handle_t *db)
211 {
212 mowgli_node_t *n, *tn;
213
214 db_start_row(db,"CLONES-DBV");
215 db_write_uint(db, CLONESDB_VERSION);
216 db_commit_row(db);
217
218 db_start_row(db, "CLONES-CK");
219 db_write_uint(db, kline_enabled);
220 db_commit_row(db);
221 db_start_row(db, "CLONES-CD");
222 db_write_uint(db, kline_duration);
223 db_commit_row(db);
224 db_start_row(db, "CLONES-GR");
225 db_write_uint(db, grace_count);
226 db_commit_row(db);
227
228 MOWGLI_ITER_FOREACH_SAFE(n, tn, clone_exempts.head)
229 {
230 cexcept_t *c = n->data;
231 if (cexempt_expired(c))
232 {
233 free(c->ip);
234 free(c->reason);
235 free(c);
236 mowgli_node_delete(n, &clone_exempts);
237 mowgli_node_free(n);
238 }
239 else
240 {
241 db_start_row(db, "CLONES-EX");
242 db_write_word(db, c->ip);
243 db_write_uint(db, c->allowed);
244 db_write_uint(db, c->warn);
245 db_write_time(db, c->expires);
246 db_write_str(db, c->reason);
247 db_commit_row(db);
248 }
249 }
250 }
251
252 static void db_h_clonesdbv(database_handle_t *db, const char *type)
253 {
254 clones_dbversion = db_sread_uint(db);
255 }
256 static void db_h_ck(database_handle_t *db, const char *type)
257 {
258 kline_enabled = db_sread_int(db) != 0;
259 }
260
261 static void db_h_cd(database_handle_t *db, const char *type)
262 {
263 kline_duration = db_sread_uint(db);
264 }
265
266 static void db_h_gr(database_handle_t *db, const char *type)
267 {
268 grace_count = db_sread_uint(db);
269 }
270
271 static void db_h_ex(database_handle_t *db, const char *type)
272 {
273 unsigned int allowed, warn;
274
275 const char *ip = db_sread_word(db);
276 allowed = db_sread_uint(db);
277
278 if (clones_dbversion == 3)
279 {
280 warn = db_sread_uint(db);
281 }
282 else if (clones_dbversion == 2)
283 {
284 warn = db_sread_uint(db);
285 db_sread_uint(db); /* trash the old KILL value */
286 }
287 else
288 {
289 warn = allowed;
290 }
291
292 time_t expires = db_sread_time(db);
293 const char *reason = db_sread_str(db);
294
295 cexcept_t *c = (cexcept_t *)smalloc(sizeof(cexcept_t));
296 c->ip = sstrdup(ip);
297 c->allowed = allowed;
298 c->warn = warn;
299 c->expires = expires;
300 c->reason = sstrdup(reason);
301 mowgli_node_add(c, mowgli_node_create(), &clone_exempts);
302 }
303
304 static cexcept_t * find_exempt(const char *ip)
305 {
306 mowgli_node_t *n;
307
308 /* first check for an exact match */
309 MOWGLI_ITER_FOREACH(n, clone_exempts.head)
310 {
311 cexcept_t *c = n->data;
312
313 if (!strcmp(ip, c->ip))
314 return c;
315 }
316
317 /* then look for cidr */
318 MOWGLI_ITER_FOREACH(n, clone_exempts.head)
319 {
320 cexcept_t *c = n->data;
321
322 if (!match_ips(c->ip, ip))
323 return c;
324 }
325
326 return 0;
327 }
328
329 static void os_cmd_clones(sourceinfo_t *si, int parc, char *parv[])
330 {
331 command_t *c;
332 char *cmd = parv[0];
333
334 /* Bad/missing arg */
335 if (!cmd)
336 {
337 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES");
338 command_fail(si, fault_needmoreparams, _("Syntax: CLONES KLINE|LIST|ADDEXEMPT|DELEXEMPT|LISTEXEMPT|SETEXEMPT|DURATION [parameters]"));
339 return;
340 }
341
342 c = command_find(os_clones_cmds, cmd);
343 if (c == NULL)
344 {
345 command_fail(si, fault_badparams, _("Invalid command. Use \2/%s%s help\2 for a command listing."), (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
346 return;
347 }
348
349 command_exec(si->service, si, c, parc + 1, parv + 1);
350 }
351
352 static void os_cmd_clones_kline(sourceinfo_t *si, int parc, char *parv[])
353 {
354 const char *arg = parv[0];
355
356 if (arg == NULL)
357 arg = "";
358
359 if (!strcasecmp(arg, "ON"))
360 {
361 if (kline_enabled && grace_count == 0)
362 {
363 command_fail(si, fault_nochange, _("CLONES klines are already enabled."));
364 return;
365 }
366 kline_enabled = true;
367 grace_count = 0;
368 command_success_nodata(si, _("Enabled CLONES klines."));
369 wallops("\2%s\2 enabled CLONES klines", get_oper_name(si));
370 logcommand(si, CMDLOG_ADMIN, "CLONES:KLINE:ON");
371 }
372 else if (!strcasecmp(arg, "OFF"))
373 {
374 if (!kline_enabled)
375 {
376 command_fail(si, fault_nochange, _("CLONES klines are already disabled."));
377 return;
378 }
379 kline_enabled = false;
380 command_success_nodata(si, _("Disabled CLONES klines."));
381 wallops("\2%s\2 disabled CLONES klines", get_oper_name(si));
382 logcommand(si, CMDLOG_ADMIN, "CLONES:KLINE:OFF");
383 }
384 else if (isdigit((unsigned char)arg[0]))
385 {
386 unsigned int newgrace = atol(arg);
387 if (kline_enabled && grace_count == newgrace)
388 {
389 command_fail(si, fault_nochange, _("CLONES kline grace is already enabled and set to %d kills."), grace_count);
390 }
391 kline_enabled = true;
392 grace_count = newgrace;
393 command_success_nodata(si, _("Enabled CLONES klines with a grace of %d kills"), grace_count);
394 wallops("\2%s\2 enabled CLONES klines with a grace of %d kills", get_oper_name(si), grace_count);
395 logcommand(si, CMDLOG_ADMIN, "CLONES:KLINE:ON grace %d", grace_count);
396 }
397 else
398 {
399 if (kline_enabled)
400 {
401 if (grace_count)
402 command_success_string(si, "ON", _("CLONES klines are currently enabled with a grace of %d kills."), grace_count);
403 else
404 command_success_string(si, "ON", _("CLONES klines are currently enabled."));
405 }
406 else
407 command_success_string(si, "OFF", _("CLONES klines are currently disabled."));
408 }
409 }
410
411 static void os_cmd_clones_list(sourceinfo_t *si, int parc, char *parv[])
412 {
413 hostentry_t *he;
414 int k = 0;
415 mowgli_patricia_iteration_state_t state;
416
417 MOWGLI_PATRICIA_FOREACH(he, &state, hostlist)
418 {
419 k = MOWGLI_LIST_LENGTH(&he->clients);
420
421 if (k > 3)
422 {
423 cexcept_t *c = find_exempt(he->ip);
424 if (c)
425 command_success_nodata(si, _("%d from %s (\2EXEMPT\2; allowed %d)"), k, he->ip, c->allowed);
426 else
427 command_success_nodata(si, _("%d from %s"), k, he->ip);
428 }
429 }
430 command_success_nodata(si, _("End of CLONES LIST"));
431 logcommand(si, CMDLOG_ADMIN, "CLONES:LIST");
432 }
433
434 static void os_cmd_clones_addexempt(sourceinfo_t *si, int parc, char *parv[])
435 {
436 mowgli_node_t *n;
437 char *ip = parv[0];
438 char *clonesstr = parv[1];
439 int clones;
440 char *expiry = parv[2];
441 char *reason = parv[3];
442 char rreason[BUFSIZE];
443 cexcept_t *c = NULL;
444 long duration;
445
446 if (!ip || !clonesstr || !expiry)
447 {
448 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
449 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
450 return;
451 }
452
453 if (!valid_ip_or_mask(ip))
454 {
455 command_fail(si, fault_badparams, _("Invalid IP/mask given."));
456 command_fail(si, fault_badparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
457 return;
458 }
459
460 clones = atoi(clonesstr);
461
462 if (expiry && !strcasecmp(expiry, "!P"))
463 {
464 if (!reason)
465 {
466 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
467 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
468 return;
469 }
470
471 duration = 0;
472 mowgli_strlcpy(rreason, reason, BUFSIZE);
473 }
474 else if (expiry && !strcasecmp(expiry, "!T"))
475 {
476 if (!reason)
477 {
478 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
479 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
480 return;
481 }
482
483 reason = strchr(reason, ' ');
484 if (reason)
485 *reason++ = '\0';
486 expiry += 3;
487
488 duration = (atol(expiry) * 60);
489 while (isdigit((unsigned char)*expiry))
490 ++expiry;
491 if (*expiry == 'h' || *expiry == 'H')
492 duration *= 60;
493 else if (*expiry == 'd' || *expiry == 'D')
494 duration *= 1440;
495 else if (*expiry == 'w' || *expiry == 'W')
496 duration *= 10080;
497 else if (*expiry == '\0')
498 ;
499 else
500 duration = 0;
501
502 if (duration == 0)
503 {
504 command_fail(si, fault_badparams, _("Invalid duration given."));
505 command_fail(si, fault_badparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
506 return;
507 }
508
509 if (!reason)
510 {
511 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
512 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
513 return;
514 }
515
516 mowgli_strlcpy(rreason, reason, BUFSIZE);
517 }
518 else if (expiry)
519 {
520 duration = config_options.clone_time;
521
522 mowgli_strlcpy(rreason, expiry, BUFSIZE);
523 if (reason)
524 {
525 mowgli_strlcat(rreason, " ", BUFSIZE);
526 mowgli_strlcat(rreason, reason, BUFSIZE);
527 }
528 }
529 else
530 {
531 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
532 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
533 return;
534 }
535
536 MOWGLI_ITER_FOREACH(n, clone_exempts.head)
537 {
538 cexcept_t *t = n->data;
539
540 if (!strcmp(ip, t->ip))
541 c = t;
542 }
543
544 if (c == NULL)
545 {
546 if (!*rreason)
547 {
548 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES ADDEXEMPT");
549 command_fail(si, fault_needmoreparams, _("Syntax: CLONES ADDEXEMPT <ip> <clones> [!P|!T <minutes>] <reason>"));
550 return;
551 }
552
553 c = smalloc(sizeof(cexcept_t));
554 c->ip = sstrdup(ip);
555 c->reason = sstrdup(rreason);
556 mowgli_node_add(c, mowgli_node_create(), &clone_exempts);
557 command_success_nodata(si, _("Added \2%s\2 to clone exempt list."), ip);
558 }
559 else
560 {
561 if (*rreason)
562 {
563 free(c->reason);
564 c->reason = sstrdup(rreason);
565 }
566 command_success_nodata(si, _("\2Warning\2: the syntax you are using to update this exemption has been deprecated and may be removed in a future version. Please use SETEXEMPT in the future instead."));
567 command_success_nodata(si, _("Updated \2%s\2 in clone exempt list."), ip);
568 }
569
570 c->allowed = clones;
571 c->warn = clones;
572 c->expires = duration ? (CURRTIME + duration) : 0;
573
574 logcommand(si, CMDLOG_ADMIN, "CLONES:ADDEXEMPT: \2%s\2 \2%d\2 (reason: \2%s\2) (duration: \2%s\2)", ip, clones, c->reason, timediff(duration));
575 }
576
577 static void os_cmd_clones_delexempt(sourceinfo_t *si, int parc, char *parv[])
578 {
579 mowgli_node_t *n, *tn;
580 char *arg = parv[0];
581
582 if (!arg)
583 {
584 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES DELEXEMPT");
585 command_fail(si, fault_needmoreparams, _("Syntax: CLONES DELEXEMPT <ip>"));
586 return;
587 }
588
589 MOWGLI_ITER_FOREACH_SAFE(n, tn, clone_exempts.head)
590 {
591 cexcept_t *c = n->data;
592
593 if (cexempt_expired(c))
594 {
595 free(c->ip);
596 free(c->reason);
597 free(c);
598 mowgli_node_delete(n, &clone_exempts);
599 mowgli_node_free(n);
600 }
601 else if (!strcmp(c->ip, arg))
602 {
603 free(c->ip);
604 free(c->reason);
605 free(c);
606 mowgli_node_delete(n, &clone_exempts);
607 mowgli_node_free(n);
608 command_success_nodata(si, _("Removed \2%s\2 from clone exempt list."), arg);
609 logcommand(si, CMDLOG_ADMIN, "CLONES:DELEXEMPT: \2%s\2", arg);
610 return;
611 }
612 }
613
614 command_fail(si, fault_nosuch_target, _("\2%s\2 not found in clone exempt list."), arg);
615 }
616
617
618 static void os_cmd_clones_setexempt(sourceinfo_t *si, int parc, char *parv[])
619 {
620 mowgli_node_t *n, *tn;
621 char *ip = parv[0];
622 char *subcmd = parv[1];
623 char *clonesstr = parv[2];
624 char *reason = parv[3];
625 char rreason[BUFSIZE];
626
627 long duration;
628
629
630 if (!ip || !subcmd || !clonesstr)
631 {
632 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLONES SETEXEMPT");
633 command_fail(si, fault_needmoreparams, _("Syntax: CLONES SETEXEMPT [DEFAULT | <ip>] <ALLOWED | WARN> <limit>"));
634 command_fail(si, fault_needmoreparams, _("Syntax: CLONES SETEXEMPT <ip> <REASON | DURATION> <value>"));
635 return;
636 }
637
638 int clones = atoi(clonesstr);
639
640 if (!strcasecmp(ip, "DEFAULT"))
641 {
642 if (!strcasecmp(subcmd, "ALLOWED"))
643 {
644 clones_allowed = clones;
645 command_success_nodata(si, _("Default allowed clone limit set to \2%d\2."), clones_allowed);
646 }
647 else if (!strcasecmp(subcmd,"WARN"))
648 {
649 if (clones == 0)
650 {
651 command_success_nodata(si, _("Default clone warning has been disabled."));
652 clones_warn = 0;
653 return;
654 }
655
656 clones_warn = clones;
657 command_success_nodata(si, _("Default warned clone limit set to \2%d\2"), clones_warn);
658 }
659 else
660 {
661 /* Invalid parameters */
662 command_fail(si, fault_badparams, _("Invalid syntax given."));
663 command_fail(si, fault_badparams, _("Syntax: CLONES SETEXEMPT DEFAULT <ALLOWED | WARN> <limit>"));
664 return;
665 }
666
667 logcommand(si, CMDLOG_ADMIN, "CLONES:SETEXEMPT:DEFAULT: \2%s\2 \2%d\2 allowed, \2%d\2 warn", ip, clones_allowed, clones_warn);
668 }
669 else if (ip) {
670 MOWGLI_ITER_FOREACH_SAFE(n, tn, clone_exempts.head)
671 {
672 cexcept_t *c = n->data;
673
674 if (cexempt_expired(c))
675 {
676 free(c->ip);
677 free(c->reason);
678 free(c);
679 mowgli_node_delete(n, &clone_exempts);
680 mowgli_node_free(n);
681 }
682 else if (!strcmp(c->ip, ip))
683 {
684 if (!strcasecmp(subcmd, "ALLOWED"))
685 {
686 if (clones < c->warn)
687 {
688 command_fail(si, fault_badparams, _("Allowed clones limit must be greater than or equal to the warned limit of %d"), c->warn);
689 return;
690 }
691
692 c->allowed = clones;
693 command_success_nodata(si, _("Allowed clones limit for host \2%s\2 set to \2%d\2"), ip, c->allowed);
694 }
695 else if (!strcasecmp(subcmd, "WARN"))
696 {
697 if (clones == 0)
698 {
699 command_success_nodata(si, _("Clone warning messages will be disabled for host \2%s\2"), ip);
700 c->warn = 0;
701 return;
702 }
703 else if (clones > c->allowed)
704 {
705 command_fail(si, fault_badparams, _("Warned clones limit must be lower than or equal to the allowed limit of %d"), c->allowed);
706 return;
707 }
708
709 c->warn = clones;
710 command_success_nodata(si, _("Warned clones limit for host \2%s\2 set to \2%d\2"), ip, c->warn);
711 }
712 else if (!strcasecmp(subcmd, "DURATION"))
713 {
714 char *expiry = clonesstr;
715 if (!strcmp(expiry, "0"))
716 {
717 c->expires = 0;
718 command_success_nodata(si, _("Clone exemption duration for host \2%s\2 set to \2permanent\2"), ip);
719 }
720 else
721 {
722 duration = (atol(expiry) * 60);
723 while (isdigit((unsigned char)*expiry))
724 ++expiry;
725 if (*expiry == 'h' || *expiry == 'H')
726 duration *= 60;
727 else if (*expiry == 'd' || *expiry == 'D')
728 duration *= 1440;
729 else if (*expiry == 'w' || *expiry == 'W')
730 duration *= 10080;
731 else if (*expiry == '\0')
732 ;
733 else
734 duration = 0;
735
736 if (duration == 0)
737 {
738 command_fail(si, fault_badparams, _("Invalid duration given."));
739 command_fail(si, fault_needmoreparams, _("Syntax: CLONES SETEXEMPT <ip> DURATION <value>"));
740 return;
741 }
742 c->expires = CURRTIME + duration;
743 command_success_nodata(si, _("Clone exemption duration for host \2%s\2 set to \2%s\2 (%ld seconds)"), ip, clonesstr, duration);
744 }
745
746 }
747 else if (!strcasecmp(subcmd, "REASON"))
748 {
749 mowgli_strlcpy(rreason, clonesstr, BUFSIZE);
750 if (reason)
751 {
752 mowgli_strlcat(rreason, " ", BUFSIZE);
753 mowgli_strlcat(rreason, reason, BUFSIZE);
754 }
755 /* mowgli_strlcat(rreason,clonesstr,BUFSIZE); */
756 free(c->reason);
757 c->reason = sstrdup(rreason);
758 command_success_nodata(si, _("Clone exemption reason for host \2%s\2 changed to \2%s\2"), ip, c->reason);
759 }
760 else
761 {
762 /* Invalid parameters */
763 command_fail(si, fault_badparams, _("Invalid syntax given."));
764 command_fail(si, fault_badparams, _("Syntax: CLONES SETEXEMPT <IP> <ALLOWED | WARN | DURATION | REASON> <value>"));
765 return;
766 }
767
768 logcommand(si, CMDLOG_ADMIN, "CLONES:SETEXEMPT: \2%s\2 \2%d\2 (reason: \2%s\2) (duration: \2%s\2)", ip, clones, c->reason, timediff((c->expires - CURRTIME)));
769
770 return;
771 }
772 }
773
774 command_fail(si, fault_nosuch_target, _("\2%s\2 not found in clone exempt list."), ip);
775 }
776 }
777
778 static void os_cmd_clones_duration(sourceinfo_t *si, int parc, char *parv[])
779 {
780 char *s = parv[0];
781 long duration;
782
783 if (!s)
784 {
785 command_success_nodata(si, _("Clone ban duration set to \2%ld\2 (%ld seconds)"), kline_duration / 60, kline_duration);
786 return;
787 }
788
789 duration = (atol(s) * 60);
790 while (isdigit((unsigned char)*s))
791 s++;
792 if (*s == 'h' || *s == 'H')
793 duration *= 60;
794 else if (*s == 'd' || *s == 'D')
795 duration *= 1440;
796 else if (*s == 'w' || *s == 'W')
797 duration *= 10080;
798 else if (*s == '\0' || *s == 'm' || *s == 'M')
799 ;
800 else
801 duration = 0;
802
803 if (duration <= 0)
804 {
805 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "CLONES DURATION");
806 return;
807 }
808
809 kline_duration = duration;
810 command_success_nodata(si, _("Clone ban duration set to \2%s\2 (%ld seconds)"), parv[0], kline_duration);
811 }
812
813 static void os_cmd_clones_listexempt(sourceinfo_t *si, int parc, char *parv[])
814 {
815
816 command_success_nodata(si, _("DEFAULT - allowed limit %d, warn on %d"), clones_allowed, clones_warn);
817 mowgli_node_t *n, *tn;
818
819 MOWGLI_ITER_FOREACH_SAFE(n, tn, clone_exempts.head)
820 {
821 cexcept_t *c = n->data;
822
823 if (cexempt_expired(c))
824 {
825 free(c->ip);
826 free(c->reason);
827 free(c);
828 mowgli_node_delete(n, &clone_exempts);
829 mowgli_node_free(n);
830 }
831 else if (c->expires)
832 command_success_nodata(si, _("%s - allowed limit %d, warn on %d - expires in %s - \2%s\2"), c->ip, c->allowed, c->warn, timediff(c->expires > CURRTIME ? c->expires - CURRTIME : 0), c->reason);
833 else
834 command_success_nodata(si, _("%s - allowed limit %d, warn on %d - \2permanent\2 - \2%s\2"), c->ip, c->allowed, c->warn, c->reason);
835 }
836 command_success_nodata(si, _("End of CLONES LISTEXEMPT"));
837 logcommand(si, CMDLOG_ADMIN, "CLONES:LISTEXEMPT");
838 }
839
840 static void clones_newuser(hook_user_nick_t *data)
841 {
842 user_t *u = data->u;
843 unsigned int i;
844 hostentry_t *he;
845 unsigned int allowed, warn;
846 mowgli_node_t *n;
847
848 /* If the user has been killed, don't do anything. */
849 if (!u)
850 return;
851
852 /* User has no IP, ignore them */
853 if (is_internal_client(u) || u->ip == NULL)
854 return;
855
856 he = mowgli_patricia_retrieve(hostlist, u->ip);
857 if (he == NULL)
858 {
859 he = mowgli_heap_alloc(hostentry_heap);
860 mowgli_strlcpy(he->ip, u->ip, sizeof he->ip);
861 mowgli_patricia_add(hostlist, he->ip, he);
862 }
863 mowgli_node_add(u, mowgli_node_create(), &he->clients);
864 i = MOWGLI_LIST_LENGTH(&he->clients);
865
866 cexcept_t *c = find_exempt(u->ip);
867 if (c == 0)
868 {
869 allowed = clones_allowed;
870 warn = clones_warn;
871 }
872 else
873 {
874 allowed = c->allowed;
875 warn = c->warn;
876 }
877
878 if (config_options.clone_increase)
879 {
880 unsigned int real_allowed = allowed;
881 unsigned int real_warn = warn;
882
883 MOWGLI_ITER_FOREACH(n, he->clients.head)
884 {
885 user_t *tu = n->data;
886
887 if (tu->myuser == NULL)
888 continue;
889 if (allowed != 0)
890 allowed++;
891 if (warn != 0)
892 warn++;
893 }
894
895 /* A hard limit of 2x the "real" limit sounds good IMO --jdhore */
896 if (allowed > (real_allowed * 2))
897 allowed = real_allowed * 2;
898 if (warn > (real_warn * 2))
899 warn = real_warn * 2;
900 }
901
902 if (i > allowed && allowed != 0)
903 {
904 /* User has exceeded the maximum number of allowed clones. */
905 if (is_autokline_exempt(u))
906 slog(LG_INFO, "CLONES: \2%d\2 clones on \2%s\2 (%s!%s@%s) (user is autokline exempt)", i, u->ip, u->nick, u->user, u->host);
907 else if (!kline_enabled || he->gracekills < grace_count || (grace_count > 0 && he->firstkill < time(NULL) - CLONES_GRACE_TIMEPERIOD))
908 {
909 if (he->firstkill < time(NULL) - CLONES_GRACE_TIMEPERIOD)
910 {
911 he->firstkill = time(NULL);
912 he->gracekills = 1;
913 }
914 else
915 {
916 he->gracekills++;
917 }
918
919 if (!kline_enabled)
920 slog(LG_INFO, "CLONES: \2%d\2 clones on \2%s\2 (%s!%s@%s) (TKLINE disabled, killing user)", i, u->ip, u->nick, u->user, u->host);
921 else
922 slog(LG_INFO, "CLONES: \2%d\2 clones on \2%s\2 (%s!%s@%s) (grace period, killing user, %d grace kills remaining)", i, u->ip, u->nick,
923 u->user, u->host, grace_count - he->gracekills);
924
925 kill_user(serviceinfo->me, u, "Too many connections from this host.");
926 data->u = NULL; /* Required due to kill_user being called during user_add hook. --mr_flea */
927 }
928 else
929 {
930 if (! (u->flags & UF_KLINESENT)) {
931 slog(LG_INFO, "CLONES: \2%d\2 clones on \2%s\2 (%s!%s@%s) (TKLINE due to excess clones)", i, u->ip, u->nick, u->user, u->host);
932 kline_sts("*", "*", u->ip, kline_duration, "Excessive clones");
933 u->flags |= UF_KLINESENT;
934 }
935 }
936
937 }
938 else if (i >= warn && warn != 0)
939 {
940 slog(LG_INFO, "CLONES: \2%d\2 clones on \2%s\2 (%s!%s@%s) (\2%d\2 allowed)", i, u->ip, u->nick, u->user, u->host, allowed);
941 msg(serviceinfo->nick, u->nick, _("\2WARNING\2: You may not have more than \2%d\2 clients connected to the network at once. Any further connections risks being removed."), allowed);
942 }
943 }
944
945 static void clones_userquit(user_t *u)
946 {
947 mowgli_node_t *n;
948 hostentry_t *he;
949
950 /* User has no IP, ignore them */
951 if (is_internal_client(u) || u->ip == NULL)
952 return;
953
954 he = mowgli_patricia_retrieve(hostlist, u->ip);
955 if (he == NULL)
956 {
957 slog(LG_DEBUG, "clones_userquit(): hostentry for %s not found??", u->ip);
958 return;
959 }
960 n = mowgli_node_find(u, &he->clients);
961 if (n)
962 {
963 mowgli_node_delete(n, &he->clients);
964 mowgli_node_free(n);
965 if (MOWGLI_LIST_LENGTH(&he->clients) == 0)
966 {
967 /* TODO: free later if he->firstkill > time(NULL) - CLONES_GRACE_TIMEPERIOD. */
968 mowgli_patricia_delete(hostlist, he->ip);
969 mowgli_heap_free(hostentry_heap, he);
970 }
971 }
972 }
973
974 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
975 * vim:ts=8
976 * vim:sw=8
977 * vim:noexpandtab
978 */