]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | #include <assert.h> | |
4 | #include "../lib/version.h" | |
5 | #include "../control/control.h" | |
6 | #include "../lib/irc_string.h" | |
7 | #include "../lib/splitline.h" | |
8 | #include "../lib/strlfunc.h" | |
9 | #include "../core/nsmalloc.h" | |
10 | #include "../irc/irc.h" | |
11 | #include "../localuser/localuser.h" | |
12 | #include "../localuser/localuserchannel.h" | |
13 | #include "glines.h" | |
14 | #include "../trusts/trusts.h" | |
15 | ||
16 | #define BLOCK_CHASE_MIN 5 /* seconds to chase nicknames in block without -c */ | |
17 | #define BLOCK_CHASE_MAX 1800 /* seconds to chase nicknames in block with -c */ | |
18 | ||
19 | MODULE_VERSION(""); | |
20 | ||
21 | static void registercommands(int, void *); | |
22 | static void deregistercommands(int, void *); | |
23 | ||
24 | static int parse_gline_flags(nick *sender, const char *flagparam, int *overridesanity, int *overridelimit, int *simulate, int *chase, int *coff) { | |
25 | const char *pos; | |
26 | ||
27 | *coff = 0; | |
28 | *overridesanity = 0; | |
29 | *overridelimit = 0; | |
30 | *simulate = 0; | |
31 | ||
32 | if (chase) | |
33 | *chase = 0; | |
34 | ||
35 | if (flagparam[0] == '-') { | |
36 | *coff = 1; | |
37 | ||
38 | for (pos = flagparam + 1; *pos; pos++) { | |
39 | switch (*pos) { | |
40 | case 'f': | |
41 | *overridesanity = 1; | |
42 | break; | |
43 | case 'l': | |
44 | *overridelimit = 1; | |
45 | break; | |
46 | case 'S': | |
47 | *simulate = 1; | |
48 | break; | |
49 | case 'c': | |
50 | if (!chase) | |
51 | goto invalid; | |
52 | ||
53 | *chase = 1; | |
54 | break; | |
55 | default: | |
56 | goto invalid; | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | return 1; | |
62 | ||
63 | invalid: | |
64 | controlreply(sender, "Invalid flag specified: %c", *pos); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | static int glines_cmdblock(void *source, int cargc, char **cargv) { | |
69 | nick *sender = source; | |
70 | nick *target, *wnp; | |
71 | whowas *ww; | |
72 | int hits, duration, id; | |
73 | int coff, overridesanity, overridelimit, simulate, chase; | |
74 | char *reason; | |
75 | char creator[128]; | |
76 | glinebuf gbuf; | |
77 | int ownww; | |
78 | ||
79 | if (cargc < 1) | |
80 | return CMD_USAGE; | |
81 | ||
82 | if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &chase, &coff)) | |
83 | return CMD_ERROR; | |
84 | ||
85 | if (cargc < 3 + coff) | |
86 | return CMD_USAGE; | |
87 | ||
88 | duration = durationtolong(cargv[coff + 1]); | |
89 | ||
90 | if (duration <= 0) { | |
91 | controlreply(sender, "Invalid duration specified."); | |
92 | return CMD_ERROR; | |
93 | } | |
94 | ||
95 | target = getnickbynick(cargv[coff]); | |
96 | ||
97 | if (!target) { | |
98 | ww = whowas_chase(cargv[coff], chase ? BLOCK_CHASE_MAX : BLOCK_CHASE_MIN); | |
99 | ||
100 | if (!ww) { | |
101 | controlreply(sender, "Sorry, couldn't find that user.%s", chase ? "" : " Use -c to chase whowas entries."); | |
102 | return CMD_ERROR; | |
103 | } | |
104 | ||
105 | ownww = 0; | |
106 | ||
107 | controlreply(sender, "Found matching whowas record:"); | |
108 | controlreply(sender, "%s", whowas_format(ww)); | |
109 | } else { | |
110 | ww = whowas_fromnick(target, 1); | |
111 | ownww = 1; | |
112 | } | |
113 | ||
114 | wnp = &ww->nick; | |
115 | ||
116 | if (sender != target && (IsService(wnp) || IsOper(wnp) || NickOnServiceServer(wnp))) { | |
117 | controlreply(sender, "Target user '%s' is an oper or a service. Not setting G-Lines.", wnp->nick); | |
118 | return CMD_ERROR; | |
119 | } | |
120 | ||
121 | rejoinline(cargv[coff + 2], cargc - coff - 2); | |
122 | reason = cargv[coff + 2]; | |
123 | ||
124 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
125 | ||
126 | glinebufinit(&gbuf, 0); | |
127 | glinebufcommentv(&gbuf, "BLOCK", cargc + coff - 1, cargv); | |
128 | glinebufaddbywhowas(&gbuf, ww, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration); | |
129 | ||
130 | glinebufspew(&gbuf, sender); | |
131 | ||
132 | if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) { | |
133 | glinebufabort(&gbuf); | |
134 | if (ownww) | |
135 | whowas_free(ww); | |
136 | controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines."); | |
137 | return CMD_ERROR; | |
138 | } | |
139 | ||
140 | if (simulate) { | |
141 | glinebufabort(&gbuf); | |
142 | if (ownww) | |
143 | whowas_free(ww); | |
144 | controlreply(sender, "Simulation complete. Not setting G-Lines."); | |
145 | return CMD_ERROR; | |
146 | } | |
147 | ||
148 | glinebufcounthits(&gbuf, &hits, NULL); | |
149 | id = glinebufcommit(&gbuf, 1); | |
150 | ||
151 | controlwall(NO_OPER, NL_GLINES, "%s BLOCK'ed user '%s!%s@%s' for %s with reason '%s' (%d hits)", controlid(sender), | |
152 | wnp->nick, wnp->ident, wnp->host->name->content, | |
153 | longtoduration(duration, 0), reason, hits); | |
154 | ||
155 | if (ownww) | |
156 | whowas_free(ww); | |
157 | ||
158 | controlreply(sender, "Done. G-Line transaction ID: %d", id); | |
159 | ||
160 | return CMD_OK; | |
161 | } | |
162 | ||
163 | static int glines_cmdgline(void *source, int cargc, char **cargv) { | |
164 | nick *sender = source; | |
165 | int duration, users, channels, id; | |
166 | char *mask, *reason; | |
167 | char creator[128]; | |
168 | int coff, overridesanity, overridelimit, simulate; | |
169 | glinebuf gbuf; | |
170 | #if SNIRCD_VERSION < 140 | |
171 | gline *gl; | |
172 | #endif /* SNIRCD_VERSION */ | |
173 | ||
174 | if (cargc < 1) | |
175 | return CMD_USAGE; | |
176 | ||
177 | if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, NULL, &coff)) | |
178 | return CMD_ERROR; | |
179 | ||
180 | if (cargc < 3 + coff) | |
181 | return CMD_USAGE; | |
182 | ||
183 | mask = cargv[coff]; | |
184 | ||
185 | duration = durationtolong(cargv[coff + 1]); | |
186 | ||
187 | if (duration <= 0) { | |
188 | controlreply(sender, "Invalid duration specified."); | |
189 | return CMD_ERROR; | |
190 | } | |
191 | ||
192 | rejoinline(cargv[coff + 2], cargc - coff - 2); | |
193 | reason = cargv[coff + 2]; | |
194 | ||
195 | #if SNIRCD_VERSION < 140 | |
196 | gl = findgline(mask); | |
197 | ||
198 | if (gl) { | |
199 | /* warn opers that they can't modify this gline */ | |
200 | if (gl->flags & GLINE_ACTIVE) { | |
201 | controlreply(sender, "Active G-Line already exists on %s - unable to modify", mask); | |
202 | return CMD_ERROR; | |
203 | } | |
204 | ||
205 | controlreply(sender, "Reactivating existing gline on %s", mask); | |
206 | } | |
207 | #endif /* SNIRCD_VERSION */ | |
208 | ||
209 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
210 | ||
211 | glinebufinit(&gbuf, 0); | |
212 | glinebufcommentv(&gbuf, "GLINE", cargc + coff - 1, cargv); | |
213 | ||
214 | if (!glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration)) { | |
215 | controlreply(sender, "Invalid G-Line mask."); | |
216 | return CMD_ERROR; | |
217 | } | |
218 | ||
219 | glinebufspew(&gbuf, sender); | |
220 | ||
221 | if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) { | |
222 | glinebufabort(&gbuf); | |
223 | controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines."); | |
224 | return CMD_ERROR; | |
225 | } | |
226 | ||
227 | if (simulate) { | |
228 | glinebufabort(&gbuf); | |
229 | controlreply(sender, "Simulation complete. Not setting G-Lines."); | |
230 | return CMD_ERROR; | |
231 | } | |
232 | ||
233 | glinebufcounthits(&gbuf, &users, &channels); | |
234 | id = glinebufcommit(&gbuf, 1); | |
235 | ||
236 | controlwall(NO_OPER, NL_GLINES, "%s GLINE'd mask '%s' for %s with reason '%s' (hits %d users/%d channels)", | |
237 | controlid(sender), mask, longtoduration(duration, 0), reason, users, channels); | |
238 | ||
239 | controlreply(sender, "Done. G-Line transaction ID: %d", id); | |
240 | ||
241 | return CMD_OK; | |
242 | } | |
243 | ||
244 | static int glines_cmdsmartgline(void *source, int cargc, char **cargv) { | |
245 | nick *sender = source; | |
246 | char *origmask; | |
247 | char mask[512]; | |
248 | char *p, *user, *host; | |
249 | struct irc_in_addr ip; | |
250 | unsigned char bits; | |
251 | int hits, duration; | |
252 | int coff, overridesanity, overridelimit, simulate, id; | |
253 | char *reason; | |
254 | char creator[128]; | |
255 | glinebuf gbuf; | |
256 | ||
257 | if (cargc < 1) | |
258 | return CMD_USAGE; | |
259 | ||
260 | if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, NULL, &coff)) | |
261 | return CMD_ERROR; | |
262 | ||
263 | if (cargc < 3 + coff) | |
264 | return CMD_USAGE; | |
265 | ||
266 | origmask = cargv[coff]; | |
267 | ||
268 | if (origmask[0] == '#' || origmask[0] == '&' || origmask[0] == '$') { | |
269 | controlreply(sender, "Please use \"gline\" for badchan or realname glines."); | |
270 | return CMD_ERROR; | |
271 | } | |
272 | ||
273 | duration = durationtolong(cargv[coff + 1]); | |
274 | ||
275 | if (duration <= 0) { | |
276 | controlreply(sender, "Invalid duration specified."); | |
277 | return CMD_ERROR; | |
278 | } | |
279 | ||
280 | rejoinline(cargv[coff + 2], cargc - coff - 2); | |
281 | reason = cargv[coff + 2]; | |
282 | ||
283 | strncpy(mask, origmask, sizeof(mask)); | |
284 | ||
285 | if (strchr(mask, '!')) { | |
286 | controlreply(sender, "Use \"gline\" to place nick glines."); | |
287 | return CMD_ERROR; | |
288 | } | |
289 | ||
290 | p = strchr(mask, '@'); | |
291 | ||
292 | if (!p) { | |
293 | controlreply(sender, "Mask must contain a username (e.g. user@ip)."); | |
294 | return CMD_ERROR; | |
295 | } | |
296 | ||
297 | user = mask; | |
298 | host = p + 1; | |
299 | *p = '\0'; | |
300 | ||
301 | if (strchr(user, '*') || strchr(user, '?')) { | |
302 | controlreply(sender, "Usernames may not contain wildcards."); | |
303 | return CMD_ERROR; | |
304 | } | |
305 | ||
306 | if (!ipmask_parse(host, &ip, &bits)) { | |
307 | controlreply(sender, "Invalid CIDR mask."); | |
308 | return CMD_ERROR; | |
309 | } | |
310 | ||
311 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
312 | ||
313 | glinebufinit(&gbuf, 0); | |
314 | glinebufcommentv(&gbuf, "SMARTGLINE", cargc + coff - 1, cargv); | |
315 | glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration); | |
316 | ||
317 | glinebufspew(&gbuf, sender); | |
318 | ||
319 | if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) { | |
320 | glinebufabort(&gbuf); | |
321 | controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines."); | |
322 | return CMD_ERROR; | |
323 | } | |
324 | ||
325 | if (simulate) { | |
326 | glinebufabort(&gbuf); | |
327 | controlreply(sender, "Simulation complete. Not setting G-Lines."); | |
328 | return CMD_ERROR; | |
329 | } | |
330 | ||
331 | glinebufcounthits(&gbuf, &hits, NULL); | |
332 | id = glinebufcommit(&gbuf, 1); | |
333 | ||
334 | controlwall(NO_OPER, NL_GLINES, "%s SMARTGLINE'd mask '%s' for %s with reason '%s' (%d hits)", | |
335 | controlid(sender), cargv[0], longtoduration(duration, 0), reason, hits); | |
336 | ||
337 | controlreply(sender, "Done. G-Line transaction ID: %d", id); | |
338 | ||
339 | return CMD_OK; | |
340 | } | |
341 | ||
342 | static int glines_cmdungline(void *source, int cargc, char **cargv) { | |
343 | nick *sender = source; | |
344 | gline *gl; | |
345 | ||
346 | if (cargc < 1) | |
347 | return CMD_USAGE; | |
348 | ||
349 | gl = findgline(cargv[0]); | |
350 | ||
351 | if (!gl) { | |
352 | controlreply(sender, "No such G-Line."); | |
353 | return CMD_ERROR; | |
354 | } | |
355 | ||
356 | if (!(gl->flags & GLINE_ACTIVE)) { | |
357 | controlreply(sender, "G-Line was already deactivated."); | |
358 | return CMD_ERROR; | |
359 | } | |
360 | ||
361 | gline_deactivate(gl, 0, 1); | |
362 | ||
363 | controlwall(NO_OPER, NL_GLINES, "%s UNGLINE'd mask '%s'", controlid(sender), cargv[0]); | |
364 | ||
365 | controlreply(sender, "G-Line deactivated."); | |
366 | ||
367 | return CMD_OK; | |
368 | } | |
369 | ||
370 | static int glines_cmddestroygline(void *source, int cargc, char **cargv) { | |
371 | nick *sender = source; | |
372 | gline *gl; | |
373 | ||
374 | if (cargc < 1) | |
375 | return CMD_USAGE; | |
376 | ||
377 | gl = findgline(cargv[0]); | |
378 | ||
379 | if (!gl) { | |
380 | controlreply(sender, "No such G-Line."); | |
381 | return CMD_ERROR; | |
382 | } | |
383 | ||
384 | gline_destroy(gl, 0, 1); | |
385 | ||
386 | controlwall(NO_OPER, NL_GLINES, "%s DESTROYGLINE'd mask '%s'", controlid(sender), cargv[0]); | |
387 | ||
388 | controlreply(sender, "G-Line destroyed."); | |
389 | ||
390 | return CMD_OK; | |
391 | } | |
392 | ||
393 | static int glines_cmdclearchan(void *source, int cargc, char **cargv) { | |
394 | nick *sender = source; | |
395 | channel *cp; | |
396 | nick *np; | |
397 | char *reason = "Clearing channel."; | |
398 | int mode, duration, i, slot, hits, id; | |
399 | int coff, overridesanity, overridelimit, simulate; | |
400 | array victims; | |
401 | char creator[128]; | |
402 | glinebuf gbuf; | |
403 | ||
404 | if (cargc < 1) | |
405 | return CMD_USAGE; | |
406 | ||
407 | if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, NULL, &coff)) | |
408 | return CMD_ERROR; | |
409 | ||
410 | if (cargc < 2 + coff) | |
411 | return CMD_USAGE; | |
412 | ||
413 | cp = findchannel(cargv[coff]); | |
414 | ||
415 | if (!cp) { | |
416 | controlreply(sender, "Couldn't find that channel."); | |
417 | return CMD_ERROR; | |
418 | } | |
419 | ||
420 | if (strcmp(cargv[coff + 1], "kick") == 0) | |
421 | mode = 0; | |
422 | else if (strcmp(cargv[coff + 1], "kill") == 0) | |
423 | mode = 1; | |
424 | else if (strcmp(cargv[coff + 1], "gline") == 0) | |
425 | mode = 2; | |
426 | else if (strcmp(cargv[coff + 1], "glineall") == 0) | |
427 | mode = 3; | |
428 | else | |
429 | return CMD_USAGE; | |
430 | ||
431 | if (mode == 0 || mode == 1) { | |
432 | if (cargc >= 3) { | |
433 | rejoinline(cargv[coff + 2], cargc - coff - 2); | |
434 | reason = cargv[coff + 2]; | |
435 | } | |
436 | } else { | |
437 | if (cargc < 3 + coff) | |
438 | return CMD_USAGE; | |
439 | ||
440 | duration = durationtolong(cargv[coff + 2]); | |
441 | ||
442 | if (duration <= 0) { | |
443 | controlreply(sender, "Invalid duration specified."); | |
444 | return CMD_ERROR; | |
445 | } | |
446 | ||
447 | if (cargc >= 4 + coff) { | |
448 | rejoinline(cargv[coff + 3], cargc - coff - 3); | |
449 | reason = cargv[coff + 3]; | |
450 | } | |
451 | } | |
452 | ||
453 | array_init(&victims, sizeof(nick *)); | |
454 | ||
455 | /* we need to make a list of the channel users here because | |
456 | * kicking/killing them will affect their position in the channel's | |
457 | * user list. */ | |
458 | for (i = 0; i < cp->users->hashsize; i++) { | |
459 | if (cp->users->content[i] == nouser) | |
460 | continue; | |
461 | ||
462 | np = getnickbynumeric(cp->users->content[i]); | |
463 | ||
464 | if (!np) | |
465 | continue; | |
466 | ||
467 | if (IsService(np) || IsOper(np) || NickOnServiceServer(np)) | |
468 | continue; | |
469 | ||
470 | slot = array_getfreeslot(&victims); | |
471 | (((nick **)victims.content)[slot]) = np; | |
472 | } | |
473 | ||
474 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
475 | ||
476 | glinebufinit(&gbuf, 0); | |
477 | glinebufcommentv(&gbuf, "CLEARCHAN", cargc + coff - 1, cargv); | |
478 | ||
479 | for (i = 0; i < victims.cursi; i++) { | |
480 | np = ((nick **)victims.content)[i]; | |
481 | ||
482 | switch (mode) { | |
483 | case 0: | |
484 | if (simulate) | |
485 | controlreply(sender, "user: %s!%s@%s r(%s)", np->nick, np->ident, np->host->name->content, np->realname->name->content); | |
486 | else | |
487 | localkickuser(NULL, cp, np, reason); | |
488 | ||
489 | break; | |
490 | case 1: | |
491 | if (simulate) | |
492 | controlreply(sender, "user: %s!%s@%s r(%s)", np->nick, np->ident, np->host->name->content, np->realname->name->content); | |
493 | else | |
494 | killuser(NULL, np, "%s", reason); | |
495 | ||
496 | break; | |
497 | case 2: | |
498 | if (IsAccount(np)) | |
499 | break; | |
500 | /* fall through */ | |
501 | case 3: | |
502 | glinebufaddbynick(&gbuf, np, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration); | |
503 | break; | |
504 | default: | |
505 | assert(0); | |
506 | } | |
507 | } | |
508 | ||
509 | if (mode != 0 && mode != 1) { | |
510 | glinebufspew(&gbuf, sender); | |
511 | ||
512 | if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) { | |
513 | glinebufabort(&gbuf); | |
514 | controlreply(sender, "G-Line failed sanity checks. Not setting G-Line."); | |
515 | return CMD_ERROR; | |
516 | } | |
517 | } | |
518 | ||
519 | if (simulate) { | |
520 | glinebufabort(&gbuf); | |
521 | controlreply(sender, "Simulation complete. Not clearing channel."); | |
522 | return CMD_ERROR; | |
523 | } | |
524 | ||
525 | glinebufmerge(&gbuf); | |
526 | glinebufcounthits(&gbuf, &hits, NULL); | |
527 | id = glinebufcommit(&gbuf, 1); | |
528 | ||
529 | array_free(&victims); | |
530 | ||
531 | if (mode == 0 || mode == 1) { | |
532 | controlwall(NO_OPER, NL_GLINES, "%s CLEARCHAN'd channel '%s' with mode '%s' and reason '%s'", | |
533 | controlid(sender), cp->index->name->content, cargv[1], reason); | |
534 | controlreply(sender, "Done."); | |
535 | } else { | |
536 | controlwall(NO_OPER, NL_GLINES, "%s CLEARCHAN'd channel '%s' with mode '%s', duration %s and reason '%s' (%d hits)", | |
537 | controlid(sender), cp->index->name->content, cargv[1], longtoduration(duration, 0), reason, hits); | |
538 | controlreply(sender, "Done. G-Line transaction ID: %d", id); | |
539 | } | |
540 | ||
541 | return CMD_OK; | |
542 | } | |
543 | ||
544 | static int glines_cmdtrustgline(void *source, int cargc, char **cargv) { | |
545 | nick *sender = source; | |
546 | trustgroup *tg; | |
547 | trusthost *th; | |
548 | int duration, hits; | |
549 | int coff, overridesanity, overridelimit, simulate, id; | |
550 | char *reason; | |
551 | char mask[512]; | |
552 | char creator[128]; | |
553 | glinebuf gbuf; | |
554 | ||
555 | if (cargc < 1) | |
556 | return CMD_USAGE; | |
557 | ||
558 | if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, NULL, &coff)) | |
559 | return CMD_ERROR; | |
560 | ||
561 | if (cargc < 4 + coff) | |
562 | return CMD_USAGE; | |
563 | ||
564 | tg = tg_strtotg(cargv[coff]); | |
565 | ||
566 | if (!(tg->flags & TRUST_RELIABLE_USERNAME)) { | |
567 | controlreply(sender, "Sorry, that trust group does not have the \"reliable username\" flag."); | |
568 | return CMD_ERROR; | |
569 | } | |
570 | ||
571 | duration = durationtolong(cargv[coff + 2]); | |
572 | ||
573 | if (duration <= 0) { | |
574 | controlreply(sender, "Invalid duration specified."); | |
575 | return CMD_ERROR; | |
576 | } | |
577 | ||
578 | rejoinline(cargv[coff + 3], cargc - coff - 3); | |
579 | reason = cargv[coff + 3]; | |
580 | ||
581 | snprintf(creator, sizeof(creator), "#%s", sender->authname); | |
582 | ||
583 | glinebufinit(&gbuf, 0); | |
584 | glinebufcommentv(&gbuf, "TRUSTGLINE", cargc + coff - 1, cargv); | |
585 | ||
586 | for(th = tg->hosts; th; th = th->next) { | |
587 | snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], CIDRtostr(th->ip, th->bits)); | |
588 | glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration); | |
589 | } | |
590 | ||
591 | glinebufspew(&gbuf, sender); | |
592 | ||
593 | if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) { | |
594 | glinebufabort(&gbuf); | |
595 | controlreply(sender, "G-Line failed sanity checks. Not setting G-Line."); | |
596 | return CMD_ERROR; | |
597 | } | |
598 | ||
599 | if (simulate) { | |
600 | glinebufabort(&gbuf); | |
601 | controlreply(sender, "Simulation complete. Not setting G-Lines."); | |
602 | return CMD_ERROR; | |
603 | } | |
604 | ||
605 | glinebufcounthits(&gbuf, &hits, NULL); | |
606 | id = glinebufcommit(&gbuf, 1); | |
607 | ||
608 | controlwall(NO_OPER, NL_GLINES, "%s TRUSTGLINE'd user '%s' on trust group '%s' for %s with reason '%s' (%d hits)", | |
609 | controlid(sender), cargv[1], tg->name->content, longtoduration(duration, 0), reason, hits); | |
610 | ||
611 | controlreply(sender, "Done. G-Line transaction ID: %d", id); | |
612 | ||
613 | return CMD_OK; | |
614 | } | |
615 | ||
616 | static int glines_cmdtrustungline(void *source, int cargc, char **cargv) { | |
617 | nick *sender = source; | |
618 | trustgroup *tg; | |
619 | trusthost *th; | |
620 | char mask[512]; | |
621 | gline *gl; | |
622 | int count; | |
623 | ||
624 | if (cargc < 2) | |
625 | return CMD_USAGE; | |
626 | ||
627 | tg = tg_strtotg(cargv[0]); | |
628 | ||
629 | if (!(tg->flags & TRUST_RELIABLE_USERNAME)) { | |
630 | controlreply(sender, "Sorry, that trust group does not have the \"reliable username\" flag."); | |
631 | return CMD_ERROR; | |
632 | } | |
633 | ||
634 | count = 0; | |
635 | ||
636 | for (th = tg->hosts; th; th = th->next) { | |
637 | snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], CIDRtostr(th->ip, th->bits)); | |
638 | ||
639 | gl = findgline(mask); | |
640 | ||
641 | if (gl && (gl->flags & GLINE_ACTIVE)) { | |
642 | gline_deactivate(gl, 0, 1); | |
643 | count++; | |
644 | } | |
645 | } | |
646 | ||
647 | controlwall(NO_OPER, NL_GLINES, "%s TRUSTUNGLINE'd user '%s' on trust group '%s' (%d G-Lines deactivated)", | |
648 | controlid(sender), cargv[1], tg->name->content, count); | |
649 | ||
650 | controlreply(sender, "Done."); | |
651 | ||
652 | return CMD_OK; | |
653 | } | |
654 | ||
655 | static int glines_cmdglstats(void *source, int cargc, char **cargv) { | |
656 | nick *sender = (nick*)source; | |
657 | gline *gl, *next; | |
658 | time_t curtime = getnettime(); | |
659 | int glinecount = 0, hostglinecount = 0, ipglinecount = 0, badchancount = 0, rnglinecount = 0; | |
660 | int deactivecount = 0, activecount = 0; | |
661 | ||
662 | for (gl = glinelist; gl; gl = next) { | |
663 | next = gl->next; | |
664 | ||
665 | if (gl->lifetime <= curtime) { | |
666 | removegline(gl); | |
667 | continue; | |
668 | } else if (gl->expire <= curtime) { | |
669 | gl->flags &= ~GLINE_ACTIVE; | |
670 | } | |
671 | ||
672 | if (gl->flags & GLINE_ACTIVE) { | |
673 | activecount++; | |
674 | } else { | |
675 | deactivecount++; | |
676 | } | |
677 | ||
678 | if (gl->flags & GLINE_IPMASK) | |
679 | ipglinecount++; | |
680 | else if (gl->flags & GLINE_HOSTMASK) | |
681 | hostglinecount++; | |
682 | else if (gl->flags & GLINE_REALNAME) | |
683 | rnglinecount++; | |
684 | else if (gl->flags & GLINE_BADCHAN) | |
685 | badchancount++; | |
686 | glinecount++; | |
687 | } | |
688 | ||
689 | controlreply(sender, "Total G-Lines set: %d", glinecount); | |
690 | controlreply(sender, "Hostmask G-Lines: %d", hostglinecount); | |
691 | controlreply(sender, "IPMask G-Lines: %d", ipglinecount); | |
692 | controlreply(sender, "Channel G-Lines: %d", badchancount); | |
693 | controlreply(sender, "Realname G-Lines: %d", rnglinecount); | |
694 | ||
695 | controlreply(sender, "Active G-Lines: %d", activecount); | |
696 | controlreply(sender, "Inactive G-Lines: %d", deactivecount); | |
697 | ||
698 | /* TODO show top 10 creators here */ | |
699 | /* TODO show unique creators count */ | |
700 | /* TODO show glines per create %8.1f", ccount?((float)gcount/(float)ccount):0 */ | |
701 | return CMD_OK; | |
702 | } | |
703 | ||
704 | static int glines_cmdglist(void *source, int cargc, char **cargv) { | |
705 | nick *sender = (nick *)source; | |
706 | gline *gl, *next; | |
707 | time_t curtime = time(NULL); | |
708 | int flags = 0; | |
709 | char *mask; | |
710 | int count = 0; | |
711 | int limit = 500; | |
712 | char expirestr[250], idstr[250]; | |
713 | ||
714 | if (cargc < 1 || (cargc == 1 && cargv[0][0] == '-')) { | |
715 | controlreply(sender, "Syntax: glist [-flags] <mask>"); | |
716 | controlreply(sender, "Valid flags are:"); | |
717 | controlreply(sender, "-c: Count G-Lines."); | |
718 | controlreply(sender, "-f: Find G-Lines active on <mask>."); | |
719 | controlreply(sender, "-x: Find G-Lines matching <mask> exactly."); | |
720 | controlreply(sender, "-R: Find G-lines on realnames."); | |
721 | controlreply(sender, "-o: Search for glines by owner."); | |
722 | controlreply(sender, "-r: Search for glines by reason."); | |
723 | controlreply(sender, "-i: Include inactive glines."); | |
724 | return CMD_ERROR; | |
725 | } | |
726 | ||
727 | if (cargc > 1) { | |
728 | char* ch = cargv[0]; | |
729 | ||
730 | for (; *ch; ch++) | |
731 | switch (*ch) { | |
732 | case '-': | |
733 | break; | |
734 | ||
735 | case 'c': | |
736 | flags |= GLIST_COUNT; | |
737 | break; | |
738 | ||
739 | case 'f': | |
740 | flags |= GLIST_FIND; | |
741 | break; | |
742 | ||
743 | case 'x': | |
744 | flags |= GLIST_EXACT; | |
745 | break; | |
746 | ||
747 | case 'r': | |
748 | flags |= GLIST_REASON; | |
749 | break; | |
750 | case 'o': | |
751 | flags |= GLIST_OWNER; | |
752 | break; | |
753 | ||
754 | case 'R': | |
755 | flags |= GLIST_REALNAME; | |
756 | break; | |
757 | ||
758 | case 'i': | |
759 | flags |= GLIST_INACTIVE; | |
760 | break; | |
761 | ||
762 | default: | |
763 | controlreply(sender, "Invalid flag '%c'.", *ch); | |
764 | return CMD_ERROR; | |
765 | } | |
766 | ||
767 | mask = cargv[1]; | |
768 | } else { | |
769 | mask = cargv[0]; | |
770 | } | |
771 | ||
772 | if ((flags & (GLIST_EXACT|GLIST_FIND)) == (GLIST_EXACT|GLIST_FIND)) { | |
773 | controlreply(sender, "You cannot use -x and -f flags together."); | |
774 | return CMD_ERROR; | |
775 | } | |
776 | ||
777 | if (!(flags & GLIST_COUNT)) | |
778 | controlreply(sender, "%-50s %-19s %-15s %-25s %s", "Mask:", "Expires in:", "Transaction ID:", "Creator:", "Reason:"); | |
779 | ||
780 | gline *searchgl = makegline(mask); | |
781 | ||
782 | if (!searchgl) { | |
783 | controlreply(sender, "Invalid G-line mask specified."); | |
784 | return CMD_ERROR; | |
785 | } | |
786 | ||
787 | for (gl = glinelist; gl; gl = next) { | |
788 | next = gl->next; | |
789 | ||
790 | if (gl->lifetime <= curtime) { | |
791 | removegline(gl); | |
792 | continue; | |
793 | } else if (gl->expire <= curtime) { | |
794 | gl->flags &= ~GLINE_ACTIVE; | |
795 | } | |
796 | ||
797 | if (!(gl->flags & GLINE_ACTIVE)) { | |
798 | if (!(flags & GLIST_INACTIVE)) { | |
799 | continue; | |
800 | } | |
801 | } | |
802 | ||
803 | if (flags & GLIST_REALNAME) { | |
804 | if (!(gl->flags & GLINE_REALNAME)) | |
805 | continue; | |
806 | if (flags & GLIST_EXACT) { | |
807 | if (!glineequal(searchgl, gl)) | |
808 | continue; | |
809 | } else if (flags & GLIST_FIND) { | |
810 | if (!gline_match_mask(gl, searchgl)) | |
811 | continue; | |
812 | } else { | |
813 | if (!match2strings(mask, glinetostring(gl))) | |
814 | continue; | |
815 | } | |
816 | } else { | |
817 | if (gl->flags & GLINE_REALNAME) | |
818 | continue; | |
819 | ||
820 | if (flags & GLIST_REASON) { | |
821 | if (flags & GLIST_EXACT) { | |
822 | if (!gl->reason || ircd_strcmp(mask, gl->reason->content) != 0) | |
823 | continue; | |
824 | } else if (flags & GLIST_FIND) { | |
825 | if (!gl->reason || !match2strings(gl->reason->content, mask)) | |
826 | continue; | |
827 | } else if (!gl->reason || !match2strings(mask, gl->reason->content)) | |
828 | continue; | |
829 | } else if (flags & GLIST_OWNER) { | |
830 | if (flags & GLIST_EXACT) { | |
831 | if (!gl->creator || ircd_strcmp(mask, gl->creator->content) != 0) | |
832 | continue; | |
833 | } else if (flags & GLIST_FIND) { | |
834 | if (!gl->creator || !match2strings(gl->creator->content, mask)) | |
835 | continue; | |
836 | } else if (!gl->creator || !match2strings(mask, gl->creator->content)) | |
837 | continue; | |
838 | } else { | |
839 | if (flags & GLIST_EXACT) { | |
840 | if (!glineequal(searchgl, gl)) | |
841 | continue; | |
842 | } else if (flags & GLIST_FIND) { | |
843 | if (!gline_match_mask(gl, searchgl)) | |
844 | continue; | |
845 | } else { | |
846 | if (!match2strings(mask, glinetostring(gl))) | |
847 | continue; | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | if (count == limit && !(flags & GLIST_COUNT)) | |
853 | controlreply(sender, "More than %d matches, list truncated.", limit); | |
854 | ||
855 | count++; | |
856 | ||
857 | if (!(flags & GLIST_COUNT) && count < limit) { | |
858 | snprintf(expirestr, sizeof(expirestr), "%s", glinetostring(gl)); | |
859 | snprintf(idstr, sizeof(idstr), "%d", gl->glinebufid); | |
860 | controlreply(sender, "%s%-49s %-19s %-15s %-25s %s", | |
861 | (gl->flags & GLINE_ACTIVE) ? "+" : "-", | |
862 | expirestr, | |
863 | (gl->flags & GLINE_ACTIVE) ? (char*)longtoduration(gl->expire - curtime, 0) : "<inactive>", | |
864 | gl->glinebufid ? idstr : "", | |
865 | gl->creator ? gl->creator->content : "", | |
866 | gl->reason ? gl->reason->content : ""); | |
867 | } | |
868 | } | |
869 | ||
870 | controlreply(sender, "%s%d G-Line%s found.", (flags & GLIST_COUNT) ? "" : "End of list - ", count, count == 1 ? "" : "s"); | |
871 | ||
872 | return CMD_OK; | |
873 | } | |
874 | ||
875 | static int glines_cmdglinelog(void *source, int cargc, char **cargv) { | |
876 | nick *sender = source; | |
877 | glinebuf *gbl; | |
878 | gline *gl; | |
879 | int i, id, count; | |
880 | char timebuf[30]; | |
881 | ||
882 | id = 0; | |
883 | ||
884 | if (cargc > 0) { | |
885 | id = atoi(cargv[0]); | |
886 | ||
887 | if (id == 0) { | |
888 | controlreply(sender, "Invalid log ID."); | |
889 | return CMD_ERROR; | |
890 | } | |
891 | } | |
892 | ||
893 | controlreply(sender, "Time ID G-Lines User Hits Channel Hits Comment"); | |
894 | ||
895 | for (i = 0; i < MAXGLINELOG; i++) { | |
896 | gbl = glinebuflog[(i + glinebuflogoffset + 1) % MAXGLINELOG]; | |
897 | ||
898 | if (!gbl) | |
899 | continue; | |
900 | ||
901 | if (id == 0 || gbl->id == id) { | |
902 | count = 0; | |
903 | ||
904 | for (gl = gbl->glines; gl; gl = gl->next) | |
905 | count++; | |
906 | ||
907 | strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime((gbl->amend) ? &gbl->amend : &gbl->commit)); | |
908 | strncat(timebuf, (gbl->amend) ? "*" : " ", sizeof(timebuf)); | |
909 | controlreply(sender, "%-20s %-10d %-10d %-15d %-15d %s", timebuf, gbl->id, count, gbl->userhits, gbl->channelhits, gbl->comment ? gbl->comment->content : "(no comment)"); | |
910 | } | |
911 | ||
912 | if (id != 0 && gbl->id == id) { | |
913 | glinebufspew(gbl, sender); | |
914 | controlreply(sender, "Done."); | |
915 | return CMD_OK; | |
916 | } | |
917 | } | |
918 | ||
919 | if (id == 0) { | |
920 | controlreply(sender, "Done."); | |
921 | } else { | |
922 | controlreply(sender, "Log entry for ID %d not found.", id); | |
923 | } | |
924 | ||
925 | return CMD_OK; | |
926 | } | |
927 | ||
928 | static int glines_cmdglineundo(void *source, int cargc, char **cargv) { | |
929 | nick *sender = source; | |
930 | int id; | |
931 | ||
932 | if (cargc < 1) | |
933 | return CMD_USAGE; | |
934 | ||
935 | id = atoi(cargv[0]); | |
936 | ||
937 | if (id == 0 || !glinebufundo(id)) { | |
938 | controlreply(sender, "Invalid log ID."); | |
939 | return CMD_ERROR; | |
940 | } | |
941 | ||
942 | controlreply(sender, "Done."); | |
943 | ||
944 | return CMD_OK; | |
945 | } | |
946 | ||
947 | static int glines_cmdsyncglines(void *source, int cargc, char **cargv) { | |
948 | nick *sender = source; | |
949 | gline *gl; | |
950 | int count; | |
951 | ||
952 | count = 0; | |
953 | ||
954 | for (gl = glinelist; gl; gl = gl->next) { | |
955 | gline_propagate(gl); | |
956 | count++; | |
957 | } | |
958 | ||
959 | controlwall(NO_OPER, NL_GLINES, "%s SYNCGLINE'd %d G-Lines.", | |
960 | controlid(sender), count); | |
961 | ||
962 | controlreply(sender, "Done."); | |
963 | ||
964 | return CMD_OK; | |
965 | } | |
966 | ||
967 | static int glines_cmdcleanupglines(void *source, int cargc, char **cargv) { | |
968 | nick *sender = source; | |
969 | gline **pnext, *gl; | |
970 | int count; | |
971 | time_t now; | |
972 | ||
973 | count = 0; | |
974 | time(&now); | |
975 | ||
976 | for (pnext = &glinelist; *pnext;) { | |
977 | gl = *pnext; | |
978 | ||
979 | /* Remove inactivate glines that have been last changed more than a week ago */ | |
980 | if (!(gl->flags & GLINE_ACTIVE) && gl->lastmod < now - 7 * 24 * 60 * 60) { | |
981 | gline_destroy(gl, 0, 1); | |
982 | count++; | |
983 | } else { | |
984 | pnext = &((*pnext)->next); | |
985 | } | |
986 | ||
987 | if (!*pnext) | |
988 | break; | |
989 | } | |
990 | ||
991 | controlwall(NO_OPER, NL_GLINES, "%s CLEANUPGLINES'd %d G-Lines.", | |
992 | controlid(sender), count); | |
993 | ||
994 | controlreply(sender, "Done."); | |
995 | ||
996 | return CMD_OK; | |
997 | } | |
998 | ||
999 | static int commandsregistered; | |
1000 | ||
1001 | static void registercommands(int hooknum, void *arg) { | |
1002 | if (commandsregistered) | |
1003 | return; | |
1004 | commandsregistered = 1; | |
1005 | ||
1006 | registercontrolhelpcmd("block", NO_OPER, 4, glines_cmdblock, "Usage: block ?flags? <nick> <duration> <reason>\nSets a gline using an appropriate mask given the user's nickname.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit\n-c - chase nick across quits/kills/nick changes"); | |
1007 | registercontrolhelpcmd("gline", NO_OPER, 4, glines_cmdgline, "Usage: gline ?flags? <mask> <duration> <reason>\nSets a gline.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit"); | |
1008 | registercontrolhelpcmd("smartgline", NO_OPER, 4, glines_cmdsmartgline, "Usage: smartgline ?flags? <user@host> <duration> <reason>\nSets a gline. Automatically adjusts the mask depending on whether the specified mask is trusted.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit"); | |
1009 | registercontrolhelpcmd("ungline", NO_OPER, 1, glines_cmdungline, "Usage: ungline <mask>\nDeactivates a gline."); | |
1010 | registercontrolhelpcmd("destroygline", NO_DEVELOPER, 1, glines_cmddestroygline, "Usage: destroygline <mask>\nDestroys a gline."); | |
1011 | registercontrolhelpcmd("clearchan", NO_OPER, 5, glines_cmdclearchan, "Usage: clearchan ?flags? <#channel> <how> <duration> ?reason?\nClears a channel.\nhow can be one of:\nkick - Kicks users.\nkill - Kills users.\ngline - Glines non-authed users (using an appropriate mask).\nglineall - Glines users.\nDuration is only valid when glining users. Reason defaults to \"Clearing channel.\".\nFlags (for glines) can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit"); | |
1012 | registercontrolhelpcmd("trustgline", NO_OPER, 5, glines_cmdtrustgline, "Usage: trustgline ?flags? <#id|name> <user> <duration> <reason>\nSets a gline on the specified username for each host in the specified trust group. The username may contain wildcards.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit"); | |
1013 | registercontrolhelpcmd("trustungline", NO_OPER, 2, glines_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nRemoves a gline that was previously set with trustgline."); | |
1014 | registercontrolhelpcmd("glstats", NO_OPER, 0, glines_cmdglstats, "Usage: glstat\nShows statistics about G-Lines."); | |
1015 | registercontrolhelpcmd("glist", NO_OPER, 2, glines_cmdglist, "Usage: glist [-flags] <mask>\nLists matching G-Lines.\nValid flags are:\n-c: Count G-Lines.\n-f: Find G-Lines active on <mask>.\n-x: Find G-Lines matching <mask> exactly.\n-R: Find G-lines on realnames.\n-o: Search for glines by owner.\n-r: Search for glines by reason.\n-i: Include inactive glines."); | |
1016 | registercontrolhelpcmd("glinelog", NO_OPER, 1, glines_cmdglinelog, "Usage: glinelog ?id?\nShows information about previous gline transactions."); | |
1017 | registercontrolhelpcmd("glineundo", NO_OPER, 1, glines_cmdglineundo, "Usage: glineundo ?id?\nUndoes a gline transaction."); | |
1018 | registercontrolhelpcmd("syncglines", NO_DEVELOPER, 0, glines_cmdsyncglines, "Usage: syncglines\nSends all G-Lines to all other servers."); | |
1019 | registercontrolhelpcmd("cleanupglines", NO_DEVELOPER, 0, glines_cmdcleanupglines, "Usage: cleanupglines\nDestroys all deactivated G-Lines."); | |
1020 | } | |
1021 | ||
1022 | static void deregistercommands(int hooknum, void *arg) { | |
1023 | if (!commandsregistered) | |
1024 | return; | |
1025 | commandsregistered = 0; | |
1026 | ||
1027 | deregistercontrolcmd("block", glines_cmdblock); | |
1028 | deregistercontrolcmd("gline", glines_cmdgline); | |
1029 | deregistercontrolcmd("smartgline", glines_cmdsmartgline); | |
1030 | deregistercontrolcmd("ungline", glines_cmdungline); | |
1031 | deregistercontrolcmd("destroygline", glines_cmddestroygline); | |
1032 | deregistercontrolcmd("clearchan", glines_cmdclearchan); | |
1033 | deregistercontrolcmd("trustgline", glines_cmdtrustgline); | |
1034 | deregistercontrolcmd("trustungline", glines_cmdtrustungline); | |
1035 | deregistercontrolcmd("glstats", glines_cmdglstats); | |
1036 | deregistercontrolcmd("glist", glines_cmdglist); | |
1037 | deregistercontrolcmd("glinelog", glines_cmdglinelog); | |
1038 | deregistercontrolcmd("glineundo", glines_cmdglineundo); | |
1039 | deregistercontrolcmd("syncglines", glines_cmdsyncglines); | |
1040 | deregistercontrolcmd("cleanupglines", glines_cmdcleanupglines); | |
1041 | } | |
1042 | ||
1043 | void _init(void) { | |
1044 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); | |
1045 | registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); | |
1046 | ||
1047 | if (trustsdbloaded) | |
1048 | registercommands(0, NULL); | |
1049 | } | |
1050 | ||
1051 | void _fini(void) { | |
1052 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); | |
1053 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); | |
1054 | ||
1055 | deregistercommands(0, NULL); | |
1056 | } |