Debian Codegraph @

modules/operserv/akill.c

raw
1 /*
2 * Copyright (c) 2003-2004 E. Will et al.
3 * Copyright (c) 2005-2006 Atheme Development Group
4 * Rights to this code are documented in doc/LICENSE.
5 *
6 * This file contains functionality which implements
7 * the OperServ AKILL command.
8 *
9 */
10
11 #include "atheme.h"
12
13 DECLARE_MODULE_V1
14 (
15 "operserv/akill", false, _modinit, _moddeinit,
16 PACKAGE_STRING,
17 VENDOR_STRING
18 );
19
20 static void os_akill_newuser(hook_user_nick_t *data);
21
22 static void os_cmd_akill(sourceinfo_t *si, int parc, char *parv[]);
23 static void os_cmd_akill_add(sourceinfo_t *si, int parc, char *parv[]);
24 static void os_cmd_akill_del(sourceinfo_t *si, int parc, char *parv[]);
25 static void os_cmd_akill_list(sourceinfo_t *si, int parc, char *parv[]);
26 static void os_cmd_akill_sync(sourceinfo_t *si, int parc, char *parv[]);
27
28
29 command_t os_akill = { "AKILL", N_("Manages network bans."), PRIV_AKILL, 3, os_cmd_akill, { .path = "oservice/akill" } };
30
31 command_t os_akill_add = { "ADD", N_("Adds a network ban"), AC_NONE, 2, os_cmd_akill_add, { .path = "" } };
32 command_t os_akill_del = { "DEL", N_("Deletes a network ban"), AC_NONE, 1, os_cmd_akill_del, { .path = "" } };
33 command_t os_akill_list = { "LIST", N_("Lists all network bans"), AC_NONE, 1, os_cmd_akill_list, { .path = "" } };
34 command_t os_akill_sync = { "SYNC", N_("Synchronises network bans to servers"), AC_NONE, 0, os_cmd_akill_sync, { .path = "" } };
35
36 mowgli_patricia_t *os_akill_cmds;
37
38 void _modinit(module_t *m)
39 {
40 service_named_bind_command("operserv", &os_akill);
41
42 os_akill_cmds = mowgli_patricia_create(strcasecanon);
43
44 /* Add sub-commands */
45 command_add(&os_akill_add, os_akill_cmds);
46 command_add(&os_akill_del, os_akill_cmds);
47 command_add(&os_akill_list, os_akill_cmds);
48 command_add(&os_akill_sync, os_akill_cmds);
49
50 hook_add_event("user_add");
51 hook_add_user_add(os_akill_newuser);
52 }
53
54 void _moddeinit(module_unload_intent_t intent)
55 {
56 service_named_unbind_command("operserv", &os_akill);
57
58 /* Delete sub-commands */
59 command_delete(&os_akill_add, os_akill_cmds);
60 command_delete(&os_akill_del, os_akill_cmds);
61 command_delete(&os_akill_list, os_akill_cmds);
62 command_delete(&os_akill_sync, os_akill_cmds);
63
64 hook_del_user_add(os_akill_newuser);
65
66 mowgli_patricia_destroy(os_akill_cmds, NULL, NULL);
67 }
68
69 static void os_akill_newuser(hook_user_nick_t *data)
70 {
71 user_t *u = data->u;
72 kline_t *k;
73
74 /* If the user has been killed, don't do anything. */
75 if (!u)
76 return;
77
78 if (is_internal_client(u))
79 return;
80 k = kline_find_user(u);
81 if (k != NULL)
82 {
83 /* Server didn't have that kline, send it again.
84 * To ensure kline exempt works on akills too, do
85 * not send a KILL. -- jilles */
86 char reason[BUFSIZE];
87 snprintf(reason, sizeof(reason), "[#%lu] %s", k->number, k->reason);
88 if (! (u->flags & UF_KLINESENT)) {
89 kline_sts("*", k->user, k->host, k->duration ? k->expires - CURRTIME : 0, reason);
90 u->flags |= UF_KLINESENT;
91 }
92 }
93 }
94
95 static void os_cmd_akill(sourceinfo_t *si, int parc, char *parv[])
96 {
97 /* Grab args */
98 char *cmd = parv[0];
99 command_t *c;
100
101 /* Bad/missing arg */
102 if (!cmd)
103 {
104 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "AKILL");
105 command_fail(si, fault_needmoreparams, _("Syntax: AKILL ADD|DEL|LIST"));
106 return;
107 }
108
109 c = command_find(os_akill_cmds, cmd);
110 if (c == NULL)
111 {
112 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);
113 return;
114 }
115
116 command_exec(si->service, si, c, parc - 1, parv + 1);
117 }
118
119 static void os_cmd_akill_add(sourceinfo_t *si, int parc, char *parv[])
120 {
121 user_t *u;
122 char *target = parv[0];
123 char *token = strtok(parv[1], " ");
124 char star[] = "*";
125 const char *kuser, *khost;
126 char *treason, reason[BUFSIZE];
127 long duration;
128 char *s;
129 kline_t *k;
130
131 if (!target || !token)
132 {
133 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "AKILL ADD");
134 command_fail(si, fault_needmoreparams, _("Syntax: AKILL ADD <nick|hostmask> [!P|!T <minutes>] <reason>"));
135 return;
136 }
137
138 if (!strcasecmp(token, "!P"))
139 {
140 duration = 0;
141 treason = strtok(NULL, "");
142
143 if (treason)
144 mowgli_strlcpy(reason, treason, BUFSIZE);
145 else
146 mowgli_strlcpy(reason, "No reason given", BUFSIZE);
147 }
148 else if (!strcasecmp(token, "!T"))
149 {
150 s = strtok(NULL, " ");
151 treason = strtok(NULL, "");
152 if (treason)
153 mowgli_strlcpy(reason, treason, BUFSIZE);
154 else
155 mowgli_strlcpy(reason, "No reason given", BUFSIZE);
156 if (s)
157 {
158 duration = (atol(s) * 60);
159 while (isdigit((unsigned char)*s))
160 s++;
161 if (*s == 'h' || *s == 'H')
162 duration *= 60;
163 else if (*s == 'd' || *s == 'D')
164 duration *= 1440;
165 else if (*s == 'w' || *s == 'W')
166 duration *= 10080;
167 else if (*s == '\0')
168 ;
169 else
170 duration = 0;
171 if (duration == 0)
172 {
173 command_fail(si, fault_badparams, _("Invalid duration given."));
174 command_fail(si, fault_badparams, _("Syntax: AKILL ADD <nick|hostmask> [!P|!T <minutes>] <reason>"));
175 return;
176 }
177 }
178 else {
179 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "AKILL ADD");
180 command_fail(si, fault_needmoreparams, _("Syntax: AKILL ADD <nick|hostmask> [!P|!T <minutes>] <reason>"));
181 return;
182 }
183
184 }
185 else
186 {
187 duration = config_options.kline_time;
188 mowgli_strlcpy(reason, token, BUFSIZE);
189 treason = strtok(NULL, "");
190
191 if (treason)
192 {
193 mowgli_strlcat(reason, " ", BUFSIZE);
194 mowgli_strlcat(reason, treason, BUFSIZE);
195 }
196 }
197
198 if (strchr(target,'!'))
199 {
200 command_fail(si, fault_badparams, _("Invalid character '%c' in user@host."), '!');
201 return;
202 }
203
204 if (!(strchr(target, '@')))
205 {
206 if (!(u = user_find_named(target)))
207 {
208 command_fail(si, fault_nosuch_target, _("\2%s\2 is not on IRC."), target);
209 return;
210 }
211
212 if (is_internal_client(u) || u == si->su)
213 return;
214
215 kuser = star;
216 khost = u->host;
217 }
218 else
219 {
220 const char *p;
221 int i = 0;
222
223 kuser = collapse(strtok(target, "@"));
224 khost = collapse(strtok(NULL, ""));
225
226 if (!kuser || !khost)
227 {
228 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "AKILL ADD");
229 command_fail(si, fault_needmoreparams, _("Syntax: AKILL ADD <user>@<host> [options] <reason>"));
230 return;
231 }
232
233 if (strchr(khost,'@'))
234 {
235 command_fail(si, fault_badparams, _("Too many '%c' in user@host."), '@');
236 command_fail(si, fault_badparams, _("Syntax: AKILL ADD <user>@<host> [options] <reason>"));
237 return;
238 }
239
240 /* make sure there's at least 4 non-wildcards */
241 /* except if the user has no wildcards */
242 for (p = kuser; *p; p++)
243 {
244 if (*p != '*' && *p != '?' && *p != '.')
245 i++;
246 }
247 for (p = khost; *p; p++)
248 {
249 if (*p != '*' && *p != '?' && *p != '.')
250 i++;
251 }
252
253 if (i < 4 && (strchr(kuser, '*') || strchr(kuser, '?')) && !has_priv(si, PRIV_AKILL_ANYMASK))
254 {
255 command_fail(si, fault_badparams, _("Invalid user@host: \2%s@%s\2. At least four non-wildcard characters are required."), kuser, khost);
256 return;
257 }
258 }
259
260 if (!strcmp(kuser, "*"))
261 {
262 bool unsafe = false;
263 char *p;
264
265 if (!match(khost, "127.0.0.1") || !match_ips(khost, "127.0.0.1"))
266 unsafe = true;
267 else if (me.vhost != NULL && (!match(khost, me.vhost) || !match_ips(khost, me.vhost)))
268 unsafe = true;
269 else if ((p = strrchr(khost, '/')) != NULL && IsDigit(p[1]) && atoi(p + 1) < 4)
270 unsafe = true;
271 if (unsafe)
272 {
273 command_fail(si, fault_badparams, _("Invalid user@host: \2%s@%s\2. This mask is unsafe."), kuser, khost);
274 logcommand(si, CMDLOG_ADMIN, "failed AKILL ADD \2%s@%s\2 (unsafe mask)", kuser, khost);
275 return;
276 }
277 }
278
279 if (kline_find(kuser, khost))
280 {
281 command_fail(si, fault_nochange, _("AKILL \2%s@%s\2 is already matched in the database."), kuser, khost);
282 return;
283 }
284
285 k = kline_add(kuser, khost, reason, duration, get_storage_oper_name(si));
286
287 if (duration)
288 command_success_nodata(si, _("Timed AKILL on \2%s@%s\2 was successfully added and will expire in %s."), k->user, k->host, timediff(duration));
289 else
290 command_success_nodata(si, _("AKILL on \2%s@%s\2 was successfully added."), k->user, k->host);
291
292 verbose_wallops("\2%s\2 is \2adding\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2", get_oper_name(si), k->user, k->host,
293 k->reason);
294
295 if (duration)
296 logcommand(si, CMDLOG_ADMIN, "AKILL:ADD: \2%s@%s\2 (reason: \2%s\2) (duration: \2%s\2)", k->user, k->host, k->reason, timediff(k->duration));
297 else
298 logcommand(si, CMDLOG_ADMIN, "AKILL:ADD: \2%s@%s\2 (reason: \2%s\2) (duration: \2Permanent\2)", k->user, k->host, k->reason);
299 }
300
301 static void os_cmd_akill_del(sourceinfo_t *si, int parc, char *parv[])
302 {
303 char *target = parv[0];
304 char *userbuf, *hostbuf;
305 unsigned int number;
306 char *s;
307 kline_t *k;
308
309 if (!target)
310 {
311 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "AKILL DEL");
312 command_fail(si, fault_needmoreparams, _("Syntax: AKILL DEL <hostmask>"));
313 return;
314 }
315
316 if (strchr(target, ','))
317 {
318 unsigned int start = 0, end = 0, i;
319 char t[16];
320
321 s = strtok(target, ",");
322
323 do
324 {
325 if (strchr(s, ':'))
326 {
327 for (i = 0; *s != ':'; s++, i++)
328 t[i] = *s;
329
330 t[++i] = '\0';
331 start = atoi(t);
332
333 s++; /* skip past the : */
334
335 for (i = 0; *s != '\0'; s++, i++)
336 t[i] = *s;
337
338 t[++i] = '\0';
339 end = atoi(t);
340
341 for (i = start; i <= end; i++)
342 {
343 if (!(k = kline_find_num(i)))
344 {
345 command_fail(si, fault_nosuch_target, _("No such AKILL with number \2%d\2."), i);
346 continue;
347 }
348
349 command_success_nodata(si, _("AKILL on \2%s@%s\2 has been successfully removed."), k->user, k->host);
350 verbose_wallops("\2%s\2 is \2removing\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2",
351 get_oper_name(si), k->user, k->host, k->reason);
352
353 logcommand(si, CMDLOG_ADMIN, "AKILL:DEL: \2%s@%s\2", k->user, k->host);
354 kline_delete(k);
355 }
356
357 continue;
358 }
359
360 number = atoi(s);
361
362 if (!(k = kline_find_num(number)))
363 {
364 command_fail(si, fault_nosuch_target, _("No such AKILL with number \2%d\2."), number);
365 return;
366 }
367
368 command_success_nodata(si, _("AKILL on \2%s@%s\2 has been successfully removed."), k->user, k->host);
369 verbose_wallops("\2%s\2 is \2removing\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2",
370 get_oper_name(si), k->user, k->host, k->reason);
371
372 logcommand(si, CMDLOG_ADMIN, "AKILL:DEL: \2%s@%s\2", k->user, k->host);
373 kline_delete(k);
374 } while ((s = strtok(NULL, ",")));
375
376 return;
377 }
378
379 if (!strchr(target, '@'))
380 {
381 unsigned int start = 0, end = 0, i;
382 char t[16];
383
384 if (strchr(target, ':'))
385 {
386 for (i = 0; *target != ':'; target++, i++)
387 t[i] = *target;
388
389 t[++i] = '\0';
390 start = atoi(t);
391
392 target++; /* skip past the : */
393
394 for (i = 0; *target != '\0'; target++, i++)
395 t[i] = *target;
396
397 t[++i] = '\0';
398 end = atoi(t);
399
400 for (i = start; i <= end; i++)
401 {
402 if (!(k = kline_find_num(i)))
403 {
404 command_fail(si, fault_nosuch_target, _("No such AKILL with number \2%d\2."), i);
405 continue;
406 }
407
408 command_success_nodata(si, _("AKILL on \2%s@%s\2 has been successfully removed."), k->user, k->host);
409 verbose_wallops("\2%s\2 is \2removing\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2",
410 get_oper_name(si), k->user, k->host, k->reason);
411
412 logcommand(si, CMDLOG_ADMIN, "AKILL:DEL: \2%s@%s\2", k->user, k->host);
413 kline_delete(k);
414 }
415
416 return;
417 }
418
419 number = atoi(target);
420
421 if (!(k = kline_find_num(number)))
422 {
423 command_fail(si, fault_nosuch_target, _("No such AKILL with number \2%d\2."), number);
424 return;
425 }
426
427 command_success_nodata(si, _("AKILL on \2%s@%s\2 has been successfully removed."), k->user, k->host);
428
429 verbose_wallops("\2%s\2 is \2removing\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2",
430 get_oper_name(si), k->user, k->host, k->reason);
431
432 logcommand(si, CMDLOG_ADMIN, "AKILL:DEL: \2%s@%s\2", k->user, k->host);
433 kline_delete(k);
434 return;
435 }
436
437 userbuf = strtok(target, "@");
438 hostbuf = strtok(NULL, "");
439
440 if (!(k = kline_find(userbuf, hostbuf)))
441 {
442 command_fail(si, fault_nosuch_target, _("No such AKILL: \2%s@%s\2."), userbuf, hostbuf);
443 return;
444 }
445
446 command_success_nodata(si, _("AKILL on \2%s@%s\2 has been successfully removed."), userbuf, hostbuf);
447
448 verbose_wallops("\2%s\2 is \2removing\2 an \2AKILL\2 for \2%s@%s\2 -- reason: \2%s\2",
449 get_oper_name(si), k->user, k->host, k->reason);
450
451 logcommand(si, CMDLOG_ADMIN, "AKILL:DEL: \2%s@%s\2", k->user, k->host);
452 kline_delete(k);
453 }
454
455 static void os_cmd_akill_list(sourceinfo_t *si, int parc, char *parv[])
456 {
457 char *param = parv[0];
458 char *user = NULL, *host = NULL;
459 unsigned long num = 0;
460 bool full = false;
461 mowgli_node_t *n;
462 kline_t *k;
463
464 if (param != NULL)
465 {
466 if (!strcasecmp(param, "FULL"))
467 full = true;
468 else if ((host = strchr(param, '@')) != NULL)
469 {
470 *host++ = '\0';
471 user = param;
472 full = true;
473 }
474 else if (strchr(param, '.') || strchr(param, ':'))
475 {
476 host = param;
477 full = true;
478 }
479 else if (isdigit((unsigned char)param[0]) &&
480 (num = strtoul(param, NULL, 10)) != 0)
481 full = true;
482 else
483 {
484 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "AKILL LIST");
485 return;
486 }
487 }
488
489 if (user || host || num)
490 command_success_nodata(si, _("AKILL list matching given criteria (with reasons):"));
491 else if (full)
492 command_success_nodata(si, _("AKILL list (with reasons):"));
493 else
494 command_success_nodata(si, _("AKILL list:"));
495
496 MOWGLI_ITER_FOREACH(n, klnlist.head)
497 {
498 struct tm tm;
499 char settime[64];
500
501 k = (kline_t *)n->data;
502
503 tm = *localtime(&k->settime);
504 strftime(settime, sizeof settime, TIME_FORMAT, &tm);
505
506 if (num != 0 && k->number != num)
507 continue;
508 if (user != NULL && match(k->user, user))
509 continue;
510 if (host != NULL && match(k->host, host) && match_ips(k->host, host))
511 continue;
512
513 if (k->duration && full)
514 command_success_nodata(si, _("%lu: %s@%s - by \2%s\2 on %s - expires in \2%s\2 - (%s)"), k->number, k->user, k->host, k->setby, settime, timediff(k->expires > CURRTIME ? k->expires - CURRTIME : 0), k->reason);
515 else if (k->duration && !full)
516 command_success_nodata(si, _("%lu: %s@%s - by \2%s\2 on %s - expires in \2%s\2"), k->number, k->user, k->host, k->setby, settime, timediff(k->expires > CURRTIME ? k->expires - CURRTIME : 0));
517 else if (!k->duration && full)
518 command_success_nodata(si, _("%lu: %s@%s - by \2%s\2 on %s - \2permanent\2 - (%s)"), k->number, k->user, k->host, k->setby, settime, k->reason);
519 else
520 command_success_nodata(si, _("%lu: %s@%s - by \2%s\2 on %s - \2permanent\2"), k->number, k->user, k->host, k->setby, settime);
521 }
522
523 if (user || host || num)
524 command_success_nodata(si, _("End of AKILL list."));
525 else
526 command_success_nodata(si, _("Total of \2%zu\2 %s in AKILL list."), klnlist.count, (klnlist.count == 1) ? "entry" : "entries");
527 if (user || host)
528 logcommand(si, CMDLOG_GET, "AKILL:LIST: \2%s@%s\2", user ? user : "*", host ? host : "*");
529 else if (num)
530 logcommand(si, CMDLOG_GET, "AKILL:LIST: \2%lu\2", num);
531 else
532 logcommand(si, CMDLOG_GET, "AKILL:LIST: \2%s\2", full ? " FULL" : "");
533 }
534
535 static void os_cmd_akill_sync(sourceinfo_t *si, int parc, char *parv[])
536 {
537 mowgli_node_t *n;
538 kline_t *k;
539
540 logcommand(si, CMDLOG_DO, "AKILL:SYNC");
541
542 MOWGLI_ITER_FOREACH(n, klnlist.head)
543 {
544 k = (kline_t *)n->data;
545
546
547 char reason[BUFSIZE];
548 snprintf(reason, sizeof(reason), "[#%lu] %s", k->number, k->reason);
549
550 if (k->duration == 0)
551 kline_sts("*", k->user, k->host, 0, reason);
552 else if (k->expires > CURRTIME)
553 kline_sts("*", k->user, k->host, k->expires - CURRTIME, reason);
554 }
555
556 command_success_nodata(si, _("AKILL list synchronized to servers."));
557 }
558
559 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
560 * vim:ts=8
561 * vim:sw=8
562 * vim:noexpandtab
563 */