| 1 |
/* |
| 2 |
* Copyright (c) 2010 William Pitcock <[email protected]> |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Looks for users and performs actions on them. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
#include <limits.h> |
| 11 |
|
| 12 |
struct trace_query_constructor |
| 13 |
{ |
| 14 |
void *(*prepare)(char **); |
| 15 |
bool (*exec)(user_t *, void *); |
| 16 |
void (*cleanup)(void *); |
| 17 |
}; |
| 18 |
|
| 19 |
struct trace_query_domain |
| 20 |
{ |
| 21 |
struct trace_query_constructor *cons; |
| 22 |
mowgli_node_t node; |
| 23 |
}; |
| 24 |
|
| 25 |
struct trace_query_regexp_domain |
| 26 |
{ |
| 27 |
struct trace_query_domain domain; |
| 28 |
atheme_regex_t * regex; |
| 29 |
char * pattern; |
| 30 |
int flags; |
| 31 |
}; |
| 32 |
|
| 33 |
struct trace_query_server_domain |
| 34 |
{ |
| 35 |
struct trace_query_domain domain; |
| 36 |
server_t * server; |
| 37 |
}; |
| 38 |
|
| 39 |
struct trace_query_glob_domain |
| 40 |
{ |
| 41 |
struct trace_query_domain domain; |
| 42 |
char * pattern; |
| 43 |
}; |
| 44 |
|
| 45 |
struct trace_query_channel_domain |
| 46 |
{ |
| 47 |
struct trace_query_domain domain; |
| 48 |
channel_t * channel; |
| 49 |
}; |
| 50 |
|
| 51 |
struct trace_query_nickage_domain |
| 52 |
{ |
| 53 |
struct trace_query_domain domain; |
| 54 |
int nickage; |
| 55 |
int comparison; |
| 56 |
}; |
| 57 |
|
| 58 |
struct trace_query_numchan_domain |
| 59 |
{ |
| 60 |
struct trace_query_domain domain; |
| 61 |
unsigned int numchan; |
| 62 |
int comparison; |
| 63 |
}; |
| 64 |
|
| 65 |
struct trace_query_identified_domain |
| 66 |
{ |
| 67 |
struct trace_query_domain domain; |
| 68 |
bool identified; |
| 69 |
}; |
| 70 |
|
| 71 |
struct trace_action |
| 72 |
{ |
| 73 |
sourceinfo_t * si; |
| 74 |
bool matched; |
| 75 |
}; |
| 76 |
|
| 77 |
struct trace_action_constructor |
| 78 |
{ |
| 79 |
struct trace_action * (*prepare)(sourceinfo_t *, char **); |
| 80 |
void (*exec)(user_t *, struct trace_action *); |
| 81 |
void (*cleanup)(struct trace_action *, bool); |
| 82 |
}; |
| 83 |
|
| 84 |
struct trace_action_kill |
| 85 |
{ |
| 86 |
struct trace_action base; |
| 87 |
char * reason; |
| 88 |
}; |
| 89 |
|
| 90 |
struct trace_action_akill |
| 91 |
{ |
| 92 |
struct trace_action base; |
| 93 |
long duration; |
| 94 |
char * reason; |
| 95 |
}; |
| 96 |
|
| 97 |
struct trace_action_count |
| 98 |
{ |
| 99 |
struct trace_action base; |
| 100 |
unsigned int matches; |
| 101 |
}; |
| 102 |
|
| 103 |
/* |
| 104 |
* Add-on interface. |
| 105 |
* |
| 106 |
* This allows third-party module writers to extend the trace API. |
| 107 |
* Just copy the prototypes out of trace.c, and add the trace_cmdtree |
| 108 |
* symbol to your module with MODULE_USE_SYMBOL(). |
| 109 |
* |
| 110 |
* Then add your criteria to the tree with mowgli_patricia_add(). |
| 111 |
*/ |
| 112 |
mowgli_patricia_t *trace_cmdtree = NULL; |
| 113 |
mowgli_patricia_t *trace_acttree = NULL; |
| 114 |
|
| 115 |
static int |
| 116 |
read_comparison_operator(char **string, int default_comparison) |
| 117 |
{ |
| 118 |
if (**string == '<') |
| 119 |
{ |
| 120 |
if (*((*string)+1) == '=') |
| 121 |
{ |
| 122 |
*string += 2; |
| 123 |
return 2; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
*string += 1; |
| 128 |
return 1; |
| 129 |
} |
| 130 |
} |
| 131 |
else if (**string == '>') |
| 132 |
{ |
| 133 |
if (*((*string)+1) == '=') |
| 134 |
{ |
| 135 |
*string += 2; |
| 136 |
return 4; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
*string += 1; |
| 141 |
return 3; |
| 142 |
} |
| 143 |
} |
| 144 |
else if (**string == '=') |
| 145 |
{ |
| 146 |
*string += 1; |
| 147 |
return 0; |
| 148 |
} |
| 149 |
else |
| 150 |
return default_comparison; |
| 151 |
} |
| 152 |
|
| 153 |
static char * |
| 154 |
reason_extract(char **args) |
| 155 |
{ |
| 156 |
char *start = *args; |
| 157 |
bool quotes = false; |
| 158 |
|
| 159 |
while (**args == ' ') |
| 160 |
{ |
| 161 |
(*args)++; |
| 162 |
} |
| 163 |
if (**args == '"') |
| 164 |
{ |
| 165 |
start = ++(*args); |
| 166 |
quotes = true; |
| 167 |
} |
| 168 |
else |
| 169 |
start = *args; |
| 170 |
|
| 171 |
while (**args) |
| 172 |
{ |
| 173 |
if (quotes && **args == '"') |
| 174 |
{ |
| 175 |
quotes = false; |
| 176 |
**args = 0; |
| 177 |
(*args)++; |
| 178 |
break; |
| 179 |
} |
| 180 |
else if (!quotes && **args == ' ') |
| 181 |
{ |
| 182 |
**args = 0; |
| 183 |
(*args)++; |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
(*args)++; |
| 188 |
} |
| 189 |
|
| 190 |
if (!(**args)) |
| 191 |
*args = NULL; |
| 192 |
|
| 193 |
if (start == *args) |
| 194 |
return NULL; /* No reason found. */ |
| 195 |
if (quotes) |
| 196 |
return NULL; /* Unclosed quotes. */ |
| 197 |
|
| 198 |
return start; |
| 199 |
} |
| 200 |
|
| 201 |
static void * |
| 202 |
trace_regexp_prepare(char **args) |
| 203 |
{ |
| 204 |
struct trace_query_regexp_domain *domain; |
| 205 |
|
| 206 |
return_val_if_fail(args != NULL, NULL); |
| 207 |
return_val_if_fail(*args != NULL, NULL); |
| 208 |
|
| 209 |
domain = scalloc(sizeof(struct trace_query_regexp_domain), 1); |
| 210 |
domain->pattern = regex_extract(*args, &(*args), &domain->flags); |
| 211 |
domain->regex = regex_create(domain->pattern, domain->flags); |
| 212 |
|
| 213 |
return domain; |
| 214 |
} |
| 215 |
|
| 216 |
static bool |
| 217 |
trace_regexp_exec(user_t *u, void *q) |
| 218 |
{ |
| 219 |
char usermask[512]; |
| 220 |
struct trace_query_regexp_domain *domain = (struct trace_query_regexp_domain *) q; |
| 221 |
|
| 222 |
return_val_if_fail(domain != NULL, false); |
| 223 |
return_val_if_fail(u != NULL, false); |
| 224 |
|
| 225 |
if (domain->regex == NULL) |
| 226 |
return false; |
| 227 |
|
| 228 |
snprintf(usermask, 512, "%s!%s@%s %s", u->nick, u->user, u->host, u->gecos); |
| 229 |
|
| 230 |
return regex_match(domain->regex, usermask); |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
trace_regexp_cleanup(void *q) |
| 235 |
{ |
| 236 |
struct trace_query_regexp_domain *domain = (struct trace_query_regexp_domain *) q; |
| 237 |
|
| 238 |
return_if_fail(domain != NULL); |
| 239 |
|
| 240 |
if (domain->regex != NULL) |
| 241 |
regex_destroy(domain->regex); |
| 242 |
|
| 243 |
sfree(domain); |
| 244 |
} |
| 245 |
|
| 246 |
static void * |
| 247 |
trace_server_prepare(char **args) |
| 248 |
{ |
| 249 |
char *server; |
| 250 |
struct trace_query_server_domain *domain; |
| 251 |
|
| 252 |
return_val_if_fail(args != NULL, NULL); |
| 253 |
return_val_if_fail(*args != NULL, NULL); |
| 254 |
|
| 255 |
/* split out the next space */ |
| 256 |
server = strtok(*args, " "); |
| 257 |
|
| 258 |
domain = scalloc(sizeof(struct trace_query_server_domain), 1); |
| 259 |
domain->server = server_find(server); |
| 260 |
|
| 261 |
/* advance *args to next token */ |
| 262 |
*args = strtok(NULL, ""); |
| 263 |
|
| 264 |
return domain; |
| 265 |
} |
| 266 |
|
| 267 |
static bool |
| 268 |
trace_server_exec(user_t *u, void *q) |
| 269 |
{ |
| 270 |
struct trace_query_server_domain *domain = (struct trace_query_server_domain *) q; |
| 271 |
|
| 272 |
return_val_if_fail(domain != NULL, false); |
| 273 |
return_val_if_fail(u != NULL, false); |
| 274 |
|
| 275 |
return (domain->server == u->server); |
| 276 |
} |
| 277 |
|
| 278 |
static void |
| 279 |
trace_server_cleanup(void *q) |
| 280 |
{ |
| 281 |
struct trace_query_server_domain *domain = (struct trace_query_server_domain *) q; |
| 282 |
|
| 283 |
return_if_fail(domain != NULL); |
| 284 |
|
| 285 |
sfree(domain); |
| 286 |
} |
| 287 |
|
| 288 |
static void * |
| 289 |
trace_glob_prepare(char **args) |
| 290 |
{ |
| 291 |
char *pattern; |
| 292 |
struct trace_query_glob_domain *domain; |
| 293 |
|
| 294 |
return_val_if_fail(args != NULL, NULL); |
| 295 |
return_val_if_fail(*args != NULL, NULL); |
| 296 |
|
| 297 |
/* split out the next space */ |
| 298 |
pattern = strtok(*args, " "); |
| 299 |
|
| 300 |
domain = scalloc(sizeof(struct trace_query_glob_domain), 1); |
| 301 |
domain->pattern = sstrdup(pattern); |
| 302 |
|
| 303 |
*args = strtok(NULL, ""); |
| 304 |
|
| 305 |
return domain; |
| 306 |
} |
| 307 |
|
| 308 |
static bool |
| 309 |
trace_glob_exec(user_t *u, void *q) |
| 310 |
{ |
| 311 |
char usermask[512]; |
| 312 |
struct trace_query_glob_domain *domain = (struct trace_query_glob_domain *) q; |
| 313 |
|
| 314 |
return_val_if_fail(domain != NULL, false); |
| 315 |
return_val_if_fail(u != NULL, false); |
| 316 |
|
| 317 |
if (domain->pattern == NULL) |
| 318 |
return false; |
| 319 |
|
| 320 |
snprintf(usermask, 512, "%s!%s@%s", u->nick, u->user, u->host); |
| 321 |
|
| 322 |
return !match(domain->pattern, usermask); |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
trace_glob_cleanup(void *q) |
| 327 |
{ |
| 328 |
struct trace_query_glob_domain *domain = (struct trace_query_glob_domain *) q; |
| 329 |
|
| 330 |
return_if_fail(domain != NULL); |
| 331 |
|
| 332 |
sfree(domain); |
| 333 |
} |
| 334 |
|
| 335 |
static void * |
| 336 |
trace_channel_prepare(char **args) |
| 337 |
{ |
| 338 |
char *channel; |
| 339 |
struct trace_query_channel_domain *domain; |
| 340 |
|
| 341 |
return_val_if_fail(args != NULL, NULL); |
| 342 |
return_val_if_fail(*args != NULL, NULL); |
| 343 |
|
| 344 |
/* split out the next space */ |
| 345 |
channel = strtok(*args, " "); |
| 346 |
|
| 347 |
domain = scalloc(sizeof(struct trace_query_channel_domain), 1); |
| 348 |
domain->channel = channel_find(channel); |
| 349 |
|
| 350 |
/* advance *args to next token */ |
| 351 |
*args = strtok(NULL, ""); |
| 352 |
|
| 353 |
return domain; |
| 354 |
} |
| 355 |
|
| 356 |
static bool |
| 357 |
trace_channel_exec(user_t *u, void *q) |
| 358 |
{ |
| 359 |
struct trace_query_channel_domain *domain = (struct trace_query_channel_domain *) q; |
| 360 |
|
| 361 |
return_val_if_fail(domain != NULL, false); |
| 362 |
return_val_if_fail(u != NULL, false); |
| 363 |
|
| 364 |
if (domain->channel == NULL) |
| 365 |
return false; |
| 366 |
|
| 367 |
return (chanuser_find(domain->channel, u) != NULL); |
| 368 |
} |
| 369 |
|
| 370 |
static void |
| 371 |
trace_channel_cleanup(void *q) |
| 372 |
{ |
| 373 |
struct trace_query_channel_domain *domain = (struct trace_query_channel_domain *) q; |
| 374 |
|
| 375 |
return_if_fail(domain != NULL); |
| 376 |
|
| 377 |
sfree(domain); |
| 378 |
} |
| 379 |
|
| 380 |
static void * |
| 381 |
trace_nickage_prepare(char **args) |
| 382 |
{ |
| 383 |
char *nickage_string; |
| 384 |
struct trace_query_nickage_domain *domain; |
| 385 |
|
| 386 |
return_val_if_fail(args != NULL, NULL); |
| 387 |
return_val_if_fail(*args != NULL, NULL); |
| 388 |
|
| 389 |
/* split out the next space */ |
| 390 |
nickage_string = strtok(*args, " "); |
| 391 |
|
| 392 |
domain = scalloc(sizeof(struct trace_query_nickage_domain), 1); |
| 393 |
domain->comparison = read_comparison_operator(&nickage_string, 2); |
| 394 |
domain->nickage = atoi(nickage_string); |
| 395 |
|
| 396 |
/* advance *args to next token */ |
| 397 |
*args = strtok(NULL, ""); |
| 398 |
|
| 399 |
return domain; |
| 400 |
} |
| 401 |
|
| 402 |
static bool |
| 403 |
trace_nickage_exec(user_t *u, void *q) |
| 404 |
{ |
| 405 |
struct trace_query_nickage_domain *domain = (struct trace_query_nickage_domain *) q; |
| 406 |
|
| 407 |
return_val_if_fail(domain != NULL, false); |
| 408 |
return_val_if_fail(u != NULL, false); |
| 409 |
|
| 410 |
int nickage = CURRTIME - u->ts; |
| 411 |
if (domain->comparison == 1) |
| 412 |
return (nickage < domain->nickage); |
| 413 |
else if (domain->comparison == 2) |
| 414 |
return (nickage <= domain->nickage); |
| 415 |
else if (domain->comparison == 3) |
| 416 |
return (nickage > domain->nickage); |
| 417 |
else if (domain->comparison == 4) |
| 418 |
return (nickage >= domain->nickage); |
| 419 |
else |
| 420 |
return (nickage == domain->nickage); |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
trace_nickage_cleanup(void *q) |
| 425 |
{ |
| 426 |
struct trace_query_nickage_domain *domain = (struct trace_query_nickage_domain *) q; |
| 427 |
|
| 428 |
return_if_fail(domain != NULL); |
| 429 |
|
| 430 |
sfree(domain); |
| 431 |
} |
| 432 |
|
| 433 |
static void * |
| 434 |
trace_numchan_prepare(char **args) |
| 435 |
{ |
| 436 |
char *numchan_string; |
| 437 |
struct trace_query_numchan_domain *domain; |
| 438 |
|
| 439 |
return_val_if_fail(args != NULL, NULL); |
| 440 |
return_val_if_fail(*args != NULL, NULL); |
| 441 |
|
| 442 |
/* split out the next space */ |
| 443 |
numchan_string = strtok(*args, " "); |
| 444 |
|
| 445 |
domain = scalloc(sizeof(struct trace_query_numchan_domain), 1); |
| 446 |
domain->comparison = read_comparison_operator(&numchan_string, 0); |
| 447 |
domain->numchan = atoi(numchan_string); |
| 448 |
|
| 449 |
/* advance *args to next token */ |
| 450 |
*args = strtok(NULL, ""); |
| 451 |
|
| 452 |
return domain; |
| 453 |
} |
| 454 |
|
| 455 |
static bool |
| 456 |
trace_numchan_exec(user_t *u, void *q) |
| 457 |
{ |
| 458 |
struct trace_query_numchan_domain *domain = (struct trace_query_numchan_domain *) q; |
| 459 |
|
| 460 |
return_val_if_fail(domain != NULL, false); |
| 461 |
return_val_if_fail(u != NULL, false); |
| 462 |
|
| 463 |
unsigned int numchan = u->channels.count; |
| 464 |
if (domain->comparison == 1) |
| 465 |
return (numchan < domain->numchan); |
| 466 |
else if (domain->comparison == 2) |
| 467 |
return (numchan <= domain->numchan); |
| 468 |
else if (domain->comparison == 3) |
| 469 |
return (numchan > domain->numchan); |
| 470 |
else if (domain->comparison == 4) |
| 471 |
return (numchan >= domain->numchan); |
| 472 |
else |
| 473 |
return (numchan == domain->numchan); |
| 474 |
} |
| 475 |
|
| 476 |
static void |
| 477 |
trace_numchan_cleanup(void *q) |
| 478 |
{ |
| 479 |
struct trace_query_numchan_domain *domain = (struct trace_query_numchan_domain *) q; |
| 480 |
|
| 481 |
return_if_fail(domain != NULL); |
| 482 |
|
| 483 |
sfree(domain); |
| 484 |
} |
| 485 |
|
| 486 |
static void * |
| 487 |
trace_identified_prepare(char **args) |
| 488 |
{ |
| 489 |
char *yesno; |
| 490 |
bool identified; |
| 491 |
struct trace_query_identified_domain *domain; |
| 492 |
|
| 493 |
return_val_if_fail(args != NULL, NULL); |
| 494 |
return_val_if_fail(*args != NULL, NULL); |
| 495 |
|
| 496 |
yesno = strtok(*args, " "); |
| 497 |
|
| 498 |
if (!strcasecmp(yesno, "yes")) |
| 499 |
identified = true; |
| 500 |
else if (!strcasecmp(yesno, "no")) |
| 501 |
identified = false; |
| 502 |
else |
| 503 |
return NULL; |
| 504 |
|
| 505 |
domain = scalloc(sizeof(struct trace_query_identified_domain), 1); |
| 506 |
domain->identified = identified; |
| 507 |
|
| 508 |
/* advance *args to next token */ |
| 509 |
*args = strtok(NULL, ""); |
| 510 |
|
| 511 |
return domain; |
| 512 |
} |
| 513 |
|
| 514 |
static bool |
| 515 |
trace_identified_exec(user_t *u, void *q) |
| 516 |
{ |
| 517 |
struct trace_query_identified_domain *domain = (struct trace_query_identified_domain *) q; |
| 518 |
|
| 519 |
return_val_if_fail(domain != NULL, false); |
| 520 |
return_val_if_fail(u != NULL, false); |
| 521 |
|
| 522 |
return (domain->identified == (u->myuser != NULL)); |
| 523 |
} |
| 524 |
|
| 525 |
static void |
| 526 |
trace_identified_cleanup(void *q) |
| 527 |
{ |
| 528 |
struct trace_query_identified_domain *domain = (struct trace_query_identified_domain *) q; |
| 529 |
|
| 530 |
return_if_fail(domain != NULL); |
| 531 |
|
| 532 |
sfree(domain); |
| 533 |
} |
| 534 |
|
| 535 |
static void |
| 536 |
trace_action_init(struct trace_action *a, sourceinfo_t *si) |
| 537 |
{ |
| 538 |
return_if_fail(a != NULL); |
| 539 |
return_if_fail(si != NULL); |
| 540 |
|
| 541 |
a->si = si; |
| 542 |
a->matched = false; |
| 543 |
} |
| 544 |
|
| 545 |
static struct trace_action * |
| 546 |
trace_print_prepare(sourceinfo_t *si, char **args) |
| 547 |
{ |
| 548 |
struct trace_action *a; |
| 549 |
|
| 550 |
return_val_if_fail(si != NULL, NULL); |
| 551 |
|
| 552 |
a = scalloc(sizeof(struct trace_action), 1); |
| 553 |
trace_action_init(a, si); |
| 554 |
|
| 555 |
return a; |
| 556 |
} |
| 557 |
|
| 558 |
static void |
| 559 |
trace_print_exec(user_t *u, struct trace_action *a) |
| 560 |
{ |
| 561 |
return_if_fail(u != NULL); |
| 562 |
return_if_fail(a != NULL); |
| 563 |
if(is_internal_client(u)) |
| 564 |
return; |
| 565 |
|
| 566 |
a->matched = true; |
| 567 |
command_success_nodata(a->si, _("\2Match:\2 %s!%s@%s %s {%s}"), u->nick, u->user, u->host, u->gecos, u->server->name); |
| 568 |
} |
| 569 |
|
| 570 |
static void |
| 571 |
trace_print_cleanup(struct trace_action *a, bool succeeded) |
| 572 |
{ |
| 573 |
return_if_fail(a != NULL); |
| 574 |
|
| 575 |
if (!a->matched && succeeded) |
| 576 |
command_success_nodata(a->si, _("No matches.")); |
| 577 |
|
| 578 |
sfree(a); |
| 579 |
} |
| 580 |
|
| 581 |
static struct trace_action * |
| 582 |
trace_kill_prepare(sourceinfo_t *si, char **args) |
| 583 |
{ |
| 584 |
struct trace_action_kill *a; |
| 585 |
char *reason; |
| 586 |
|
| 587 |
return_val_if_fail(si != NULL, NULL); |
| 588 |
return_val_if_fail(args != NULL, NULL); |
| 589 |
if (*args == NULL) |
| 590 |
return NULL; |
| 591 |
|
| 592 |
reason = reason_extract(args); |
| 593 |
if (reason == NULL) |
| 594 |
return NULL; |
| 595 |
|
| 596 |
a = scalloc(sizeof(struct trace_action_kill), 1); |
| 597 |
trace_action_init(&a->base, si); |
| 598 |
a->reason = reason; |
| 599 |
|
| 600 |
return (struct trace_action*) a; |
| 601 |
} |
| 602 |
|
| 603 |
static void |
| 604 |
trace_kill_exec(user_t *u, struct trace_action *act) |
| 605 |
{ |
| 606 |
service_t *svs; |
| 607 |
struct trace_action_kill *a = (struct trace_action_kill *) act; |
| 608 |
|
| 609 |
return_if_fail(u != NULL); |
| 610 |
return_if_fail(a != NULL); |
| 611 |
if (is_internal_client(u)) |
| 612 |
return; |
| 613 |
if (is_ircop(u)) |
| 614 |
return; |
| 615 |
if (u->myuser && is_soper(u->myuser)) |
| 616 |
return; |
| 617 |
if ((svs = service_find("operserv")) == NULL) |
| 618 |
return; |
| 619 |
|
| 620 |
act->matched = true; |
| 621 |
kill_user(svs->me, u, "%s", a->reason); |
| 622 |
command_success_nodata(act->si, _("\2%s\2 has been killed."), u->nick); |
| 623 |
} |
| 624 |
|
| 625 |
static void |
| 626 |
trace_kill_cleanup(struct trace_action *a, bool succeeded) |
| 627 |
{ |
| 628 |
return_if_fail(a != NULL); |
| 629 |
|
| 630 |
if (!a->matched && succeeded) |
| 631 |
command_success_nodata(a->si, _("No matches.")); |
| 632 |
|
| 633 |
sfree(a); |
| 634 |
} |
| 635 |
|
| 636 |
static struct trace_action * |
| 637 |
trace_akill_prepare(sourceinfo_t *si, char **args) |
| 638 |
{ |
| 639 |
struct trace_action_akill *a; |
| 640 |
char *s, *reason; |
| 641 |
long duration = config_options.kline_time; |
| 642 |
char token; |
| 643 |
|
| 644 |
return_val_if_fail(si != NULL, NULL); |
| 645 |
return_val_if_fail(args != NULL, NULL); |
| 646 |
if (*args == NULL) |
| 647 |
return NULL; |
| 648 |
|
| 649 |
while (**args == ' ') |
| 650 |
(*args)++; |
| 651 |
|
| 652 |
/* Extract a token, but only if there's one to remove. |
| 653 |
* Otherwise, this would clip a word off the reason. */ |
| 654 |
token = 0; |
| 655 |
s = *args; |
| 656 |
if (!strncasecmp(s, "!T", 2) || !strncasecmp(s, "!P", 2)) |
| 657 |
{ |
| 658 |
if (s[2] == ' ') |
| 659 |
{ |
| 660 |
token = tolower(s[1]); |
| 661 |
s[2] = '\0'; |
| 662 |
*args += 3; |
| 663 |
} |
| 664 |
else if (s[2] == '\0') |
| 665 |
{ |
| 666 |
token = tolower(s[1]); |
| 667 |
*args += 2; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
if (token == 't') |
| 672 |
{ |
| 673 |
s = strtok(*args, " "); |
| 674 |
*args = strtok(NULL, ""); |
| 675 |
if (*args == NULL) |
| 676 |
return NULL; |
| 677 |
|
| 678 |
duration = (atol(s) * 60); |
| 679 |
while (isdigit((unsigned char)*s)) |
| 680 |
s++; |
| 681 |
if (*s == 'h' || *s == 'H') |
| 682 |
duration *= 60; |
| 683 |
else if (*s == 'd' || *s == 'D') |
| 684 |
duration *= 1440; |
| 685 |
else if (*s == 'w' || *s == 'W') |
| 686 |
duration *= 10080; |
| 687 |
else if (*s == '\0') |
| 688 |
; |
| 689 |
else |
| 690 |
duration = 0; |
| 691 |
|
| 692 |
if (duration == 0) |
| 693 |
return NULL; |
| 694 |
} |
| 695 |
else if (token == 'p') |
| 696 |
duration = 0; |
| 697 |
|
| 698 |
reason = reason_extract(args); |
| 699 |
if (reason == NULL) |
| 700 |
return NULL; |
| 701 |
|
| 702 |
a = scalloc(sizeof(struct trace_action_akill), 1); |
| 703 |
trace_action_init(&a->base, si); |
| 704 |
a->duration = duration; |
| 705 |
a->reason = reason; |
| 706 |
|
| 707 |
|
| 708 |
return (struct trace_action*) a; |
| 709 |
} |
| 710 |
|
| 711 |
static void |
| 712 |
trace_akill_exec(user_t *u, struct trace_action *act) |
| 713 |
{ |
| 714 |
const char *kuser, *khost; |
| 715 |
struct trace_action_akill *a = (struct trace_action_akill *) act; |
| 716 |
|
| 717 |
return_if_fail(u != NULL); |
| 718 |
return_if_fail(a != NULL); |
| 719 |
if (is_internal_client(u)) |
| 720 |
return; |
| 721 |
if (is_ircop(u)) |
| 722 |
return; |
| 723 |
if (u->myuser && is_soper(u->myuser)) |
| 724 |
return; |
| 725 |
|
| 726 |
kuser = "*"; |
| 727 |
khost = u->host; |
| 728 |
|
| 729 |
if (!match(khost, "127.0.0.1") || !match_ips(khost, "127.0.0.1")) |
| 730 |
return; |
| 731 |
if (me.vhost != NULL && (!match(khost, me.vhost) || !match_ips(khost, me.vhost))) |
| 732 |
return; |
| 733 |
if (kline_find(kuser, khost)) |
| 734 |
return; |
| 735 |
|
| 736 |
act->matched = true; |
| 737 |
kline_add(kuser, khost, a->reason, a->duration, get_storage_oper_name(act->si)); |
| 738 |
command_success_nodata(act->si, _("\2%s\2 has been akilled."), u->nick); |
| 739 |
} |
| 740 |
|
| 741 |
static void |
| 742 |
trace_akill_cleanup(struct trace_action *a, bool succeeded) |
| 743 |
{ |
| 744 |
return_if_fail(a != NULL); |
| 745 |
|
| 746 |
if (!a->matched && succeeded) |
| 747 |
command_success_nodata(a->si, _("No matches.")); |
| 748 |
|
| 749 |
sfree(a); |
| 750 |
} |
| 751 |
|
| 752 |
static struct trace_action * |
| 753 |
trace_count_prepare(sourceinfo_t *si, char **args) |
| 754 |
{ |
| 755 |
struct trace_action_count *a; |
| 756 |
|
| 757 |
return_val_if_fail(si != NULL, NULL); |
| 758 |
|
| 759 |
a = scalloc(sizeof(struct trace_action_count), 1); |
| 760 |
trace_action_init(&a->base, si); |
| 761 |
|
| 762 |
return (struct trace_action *) a; |
| 763 |
} |
| 764 |
|
| 765 |
static void |
| 766 |
trace_count_exec(user_t *u, struct trace_action *act) |
| 767 |
{ |
| 768 |
struct trace_action_count *a = (struct trace_action_count *) act; |
| 769 |
|
| 770 |
return_if_fail(u != NULL); |
| 771 |
return_if_fail(a != NULL); |
| 772 |
if (is_internal_client(u)) |
| 773 |
return; |
| 774 |
|
| 775 |
act->matched = true; |
| 776 |
a->matches++; |
| 777 |
} |
| 778 |
|
| 779 |
static void |
| 780 |
trace_count_cleanup(struct trace_action *act, bool succeeded) |
| 781 |
{ |
| 782 |
struct trace_action_count *a = (struct trace_action_count *) act; |
| 783 |
|
| 784 |
return_if_fail(a != NULL); |
| 785 |
|
| 786 |
if (succeeded) |
| 787 |
command_success_nodata(act->si, _("\2%u\2 matches"), a->matches); |
| 788 |
|
| 789 |
sfree(a); |
| 790 |
} |
| 791 |
|
| 792 |
static struct trace_query_constructor trace_regexp = { |
| 793 |
.prepare = &trace_regexp_prepare, |
| 794 |
.exec = &trace_regexp_exec, |
| 795 |
.cleanup = &trace_regexp_cleanup, |
| 796 |
}; |
| 797 |
|
| 798 |
static struct trace_query_constructor trace_server = { |
| 799 |
.prepare = &trace_server_prepare, |
| 800 |
.exec = &trace_server_exec, |
| 801 |
.cleanup = &trace_server_cleanup, |
| 802 |
}; |
| 803 |
|
| 804 |
static struct trace_query_constructor trace_glob = { |
| 805 |
.prepare = &trace_glob_prepare, |
| 806 |
.exec = &trace_glob_exec, |
| 807 |
.cleanup = &trace_glob_cleanup, |
| 808 |
}; |
| 809 |
|
| 810 |
static struct trace_query_constructor trace_channel = { |
| 811 |
.prepare = &trace_channel_prepare, |
| 812 |
.exec = &trace_channel_exec, |
| 813 |
.cleanup = &trace_channel_cleanup, |
| 814 |
}; |
| 815 |
|
| 816 |
static struct trace_query_constructor trace_nickage = { |
| 817 |
.prepare = &trace_nickage_prepare, |
| 818 |
.exec = &trace_nickage_exec, |
| 819 |
.cleanup = &trace_nickage_cleanup, |
| 820 |
}; |
| 821 |
|
| 822 |
static struct trace_query_constructor trace_numchan = { |
| 823 |
.prepare = &trace_numchan_prepare, |
| 824 |
.exec = &trace_numchan_exec, |
| 825 |
.cleanup = &trace_numchan_cleanup, |
| 826 |
}; |
| 827 |
|
| 828 |
static struct trace_query_constructor trace_identified = { |
| 829 |
.prepare = &trace_identified_prepare, |
| 830 |
.exec = &trace_identified_exec, |
| 831 |
.cleanup = &trace_identified_cleanup, |
| 832 |
}; |
| 833 |
|
| 834 |
static struct trace_action_constructor trace_print = { |
| 835 |
.prepare = &trace_print_prepare, |
| 836 |
.exec = &trace_print_exec, |
| 837 |
.cleanup = &trace_print_cleanup, |
| 838 |
}; |
| 839 |
|
| 840 |
static struct trace_action_constructor trace_kill = { |
| 841 |
.prepare = &trace_kill_prepare, |
| 842 |
.exec = &trace_kill_exec, |
| 843 |
.cleanup = &trace_kill_cleanup, |
| 844 |
}; |
| 845 |
|
| 846 |
static struct trace_action_constructor trace_akill = { |
| 847 |
.prepare = &trace_akill_prepare, |
| 848 |
.exec = &trace_akill_exec, |
| 849 |
.cleanup = &trace_akill_cleanup, |
| 850 |
}; |
| 851 |
|
| 852 |
static struct trace_action_constructor trace_count = { |
| 853 |
.prepare = &trace_count_prepare, |
| 854 |
.exec = &trace_count_exec, |
| 855 |
.cleanup = &trace_count_cleanup, |
| 856 |
}; |
| 857 |
|
| 858 |
static bool |
| 859 |
os_cmd_trace_run(sourceinfo_t *si, struct trace_action_constructor *actcons, struct trace_action* act, mowgli_list_t *crit, char *args) |
| 860 |
{ |
| 861 |
user_t *u; |
| 862 |
mowgli_patricia_iteration_state_t state; |
| 863 |
mowgli_node_t *n; |
| 864 |
|
| 865 |
if (args == NULL) |
| 866 |
{ |
| 867 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TRACE"); |
| 868 |
command_fail(si, fault_needmoreparams, _("Syntax: TRACE <action> <params>")); |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
while (true) |
| 873 |
{ |
| 874 |
struct trace_query_constructor *cons; |
| 875 |
struct trace_query_domain *q; |
| 876 |
char *cmd = strtok(args, " "); |
| 877 |
|
| 878 |
if (cmd == NULL) |
| 879 |
break; |
| 880 |
|
| 881 |
cons = mowgli_patricia_retrieve(trace_cmdtree, cmd); |
| 882 |
if (cons == NULL) |
| 883 |
{ |
| 884 |
command_fail(si, fault_nosuch_target, _("Invalid criteria specified.")); |
| 885 |
return false; |
| 886 |
} |
| 887 |
|
| 888 |
args = strtok(NULL, ""); |
| 889 |
if (args == NULL) |
| 890 |
{ |
| 891 |
command_fail(si, fault_nosuch_target, _("Invalid criteria specified.")); |
| 892 |
return false; |
| 893 |
} |
| 894 |
|
| 895 |
q = cons->prepare(&args); |
| 896 |
slog(LG_DEBUG, "operserv/trace: adding criteria %p(%s) to list [remain: %s]", q, cmd, args); |
| 897 |
if (q == NULL) |
| 898 |
{ |
| 899 |
command_fail(si, fault_nosuch_target, _("Invalid criteria specified.")); |
| 900 |
return false; |
| 901 |
} |
| 902 |
slog(LG_DEBUG, "operserv/trace: new args position [%s]", args); |
| 903 |
|
| 904 |
q->cons = cons; |
| 905 |
mowgli_node_add(q, &q->node, crit); |
| 906 |
} |
| 907 |
|
| 908 |
MOWGLI_PATRICIA_FOREACH(u, &state, userlist) |
| 909 |
{ |
| 910 |
bool doit = true; |
| 911 |
|
| 912 |
MOWGLI_ITER_FOREACH(n, crit->head) |
| 913 |
{ |
| 914 |
struct trace_query_domain *q = (struct trace_query_domain *) n->data; |
| 915 |
|
| 916 |
if (!q->cons->exec(u, q)) |
| 917 |
{ |
| 918 |
doit = false; |
| 919 |
break; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
if (doit) |
| 924 |
actcons->exec(u, act); |
| 925 |
} |
| 926 |
|
| 927 |
return true; |
| 928 |
} |
| 929 |
|
| 930 |
static void |
| 931 |
os_cmd_trace(sourceinfo_t *si, int parc, char *parv[]) |
| 932 |
{ |
| 933 |
mowgli_list_t crit = { NULL, NULL, 0 }; |
| 934 |
struct trace_action_constructor *actcons; |
| 935 |
struct trace_action* act; |
| 936 |
char *args = parv[1]; |
| 937 |
mowgli_node_t *n, *tn; |
| 938 |
char *params; |
| 939 |
bool succeeded; |
| 940 |
|
| 941 |
if (!parv[0]) |
| 942 |
{ |
| 943 |
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "TRACE"); |
| 944 |
command_fail(si, fault_badparams, _("Syntax: TRACE <action> <params>")); |
| 945 |
return; |
| 946 |
} |
| 947 |
|
| 948 |
actcons = mowgli_patricia_retrieve(trace_acttree, parv[0]); |
| 949 |
if (actcons == NULL) |
| 950 |
{ |
| 951 |
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "TRACE"); |
| 952 |
command_fail(si, fault_badparams, _("Syntax: TRACE <action> <params>")); |
| 953 |
return; |
| 954 |
} |
| 955 |
|
| 956 |
act = actcons->prepare(si, &args); |
| 957 |
if (act == NULL) |
| 958 |
{ |
| 959 |
command_fail(si, fault_nosuch_target, _("Action compilation failed.")); |
| 960 |
return; |
| 961 |
} |
| 962 |
|
| 963 |
params = sstrdup(args); |
| 964 |
succeeded = os_cmd_trace_run(si, actcons, act, &crit, args); |
| 965 |
|
| 966 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, crit.head) |
| 967 |
{ |
| 968 |
struct trace_query_domain *q = (struct trace_query_domain *) n->data; |
| 969 |
q->cons->cleanup(q); |
| 970 |
} |
| 971 |
actcons->cleanup(act, succeeded); |
| 972 |
|
| 973 |
if (succeeded) |
| 974 |
logcommand(si, CMDLOG_ADMIN, "TRACE: \2%s\2 \2%s\2", parv[0], params); |
| 975 |
|
| 976 |
sfree(params); |
| 977 |
} |
| 978 |
|
| 979 |
static command_t os_trace = { |
| 980 |
.name = "TRACE", |
| 981 |
.desc = N_("Looks for users and performs actions on them."), |
| 982 |
.access = PRIV_USER_AUSPEX, |
| 983 |
.maxparc = 2, |
| 984 |
.cmd = &os_cmd_trace, |
| 985 |
.help = { .path = "contrib/trace" }, |
| 986 |
}; |
| 987 |
|
| 988 |
static void |
| 989 |
mod_init(module_t *const restrict m) |
| 990 |
{ |
| 991 |
if (! (trace_cmdtree = mowgli_patricia_create(&strcasecanon))) |
| 992 |
{ |
| 993 |
(void) slog(LG_ERROR, "%s: mowgli_patricia_create() failed", m->name); |
| 994 |
|
| 995 |
m->mflags |= MODFLAG_FAIL; |
| 996 |
return; |
| 997 |
} |
| 998 |
if (! (trace_acttree = mowgli_patricia_create(&strcasecanon))) |
| 999 |
{ |
| 1000 |
(void) slog(LG_ERROR, "%s: mowgli_patricia_create() failed", m->name); |
| 1001 |
|
| 1002 |
(void) mowgli_patricia_destroy(trace_cmdtree, NULL, NULL); |
| 1003 |
|
| 1004 |
m->mflags |= MODFLAG_FAIL; |
| 1005 |
return; |
| 1006 |
} |
| 1007 |
|
| 1008 |
mowgli_patricia_add(trace_cmdtree, "REGEXP", &trace_regexp); |
| 1009 |
mowgli_patricia_add(trace_cmdtree, "SERVER", &trace_server); |
| 1010 |
mowgli_patricia_add(trace_cmdtree, "GLOB", &trace_glob); |
| 1011 |
mowgli_patricia_add(trace_cmdtree, "CHANNEL", &trace_channel); |
| 1012 |
mowgli_patricia_add(trace_cmdtree, "NICKAGE", &trace_nickage); |
| 1013 |
mowgli_patricia_add(trace_cmdtree, "NUMCHAN", &trace_numchan); |
| 1014 |
mowgli_patricia_add(trace_cmdtree, "IDENTIFIED", &trace_identified); |
| 1015 |
|
| 1016 |
mowgli_patricia_add(trace_acttree, "PRINT", &trace_print); |
| 1017 |
mowgli_patricia_add(trace_acttree, "KILL", &trace_kill); |
| 1018 |
mowgli_patricia_add(trace_acttree, "AKILL", &trace_akill); |
| 1019 |
mowgli_patricia_add(trace_acttree, "COUNT", &trace_count); |
| 1020 |
|
| 1021 |
service_named_bind_command("operserv", &os_trace); |
| 1022 |
} |
| 1023 |
|
| 1024 |
static void |
| 1025 |
mod_deinit(const module_unload_intent_t intent) |
| 1026 |
{ |
| 1027 |
service_named_unbind_command("operserv", &os_trace); |
| 1028 |
|
| 1029 |
mowgli_patricia_delete(trace_cmdtree, "REGEXP"); |
| 1030 |
mowgli_patricia_delete(trace_cmdtree, "SERVER"); |
| 1031 |
mowgli_patricia_delete(trace_cmdtree, "GLOB"); |
| 1032 |
mowgli_patricia_delete(trace_cmdtree, "CHANNEL"); |
| 1033 |
mowgli_patricia_delete(trace_cmdtree, "NICKAGE"); |
| 1034 |
mowgli_patricia_delete(trace_cmdtree, "NUMCHAN"); |
| 1035 |
mowgli_patricia_delete(trace_cmdtree, "IDENTIFIED"); |
| 1036 |
|
| 1037 |
mowgli_patricia_delete(trace_acttree, "PRINT"); |
| 1038 |
mowgli_patricia_delete(trace_acttree, "KILL"); |
| 1039 |
mowgli_patricia_delete(trace_acttree, "AKILL"); |
| 1040 |
mowgli_patricia_delete(trace_acttree, "COUNT"); |
| 1041 |
|
| 1042 |
mowgli_patricia_destroy(trace_cmdtree, NULL, NULL); |
| 1043 |
mowgli_patricia_destroy(trace_acttree, NULL, NULL); |
| 1044 |
} |
| 1045 |
|
| 1046 |
SIMPLE_DECLARE_MODULE_V1("contrib/os_trace", MODULE_UNLOAD_CAPABILITY_OK) |