]> jfr.im git - solanum.git/blame - extensions/hurt.c
extensions/extb_extgecos: support CIDR masks in $x extbans (#414)
[solanum.git] / extensions / hurt.c
CommitLineData
212380e3 1/*
a6f63a82 2 * Solanum: a slightly advanced ircd
212380e3
AC
3 *
4 * Copyright (C) 2006 charybdis development team
5 * All rights reserved
212380e3
AC
6 */
7#include "stdinc.h"
8#include "modules.h"
9#include "hook.h"
10#include "client.h"
11#include "ircd.h"
12#include "send.h"
13#include "numeric.h"
14#include "hostmask.h"
212380e3
AC
15#include "s_conf.h"
16#include "s_newconf.h"
17#include "hash.h"
bd0d352f 18#include "messages.h"
77d3d2db 19#include "s_assert.h"
212380e3
AC
20
21/* {{{ Structures */
22#define HURT_CUTOFF (10) /* protocol messages. */
23#define HURT_DEFAULT_EXPIRE (7 * 24 * 60) /* minutes. */
24#define HURT_EXIT_REASON "Hurt: Failed to identify to services"
25
26enum {
27 HEAL_NICK = 0,
28 HEAL_IP
29};
30
31typedef struct _hurt_state {
32 time_t start_time;
33 uint32_t n_hurts;
ddcb223e 34 rb_dlink_list hurt_clients;
212380e3
AC
35 uint16_t cutoff;
36 time_t default_expire;
37 const char *exit_reason;
38} hurt_state_t;
39
40typedef struct _hurt {
19fcdbd5 41 char *ip;
212380e3
AC
42 struct sockaddr *saddr;
43 int saddr_bits;
19fcdbd5 44 char *reason;
212380e3
AC
45 time_t expire;
46} hurt_t;
47/* }}} */
48
49/* {{{ Prototypes */
3c7d6fcc
EM
50static void mo_hurt(struct MsgBuf *msgbuf_p, struct Client *, struct Client *, int, const char **);
51static void me_hurt(struct MsgBuf *msgbuf_p, struct Client *, struct Client *, int, const char **);
52static void mo_heal(struct MsgBuf *msgbuf_p, struct Client *, struct Client *, int, const char **);
53static void me_heal(struct MsgBuf *msgbuf_p, struct Client *, struct Client *, int, const char **);
212380e3
AC
54
55static int modinit(void);
56static void modfini(void);
57
82436efb
EM
58static void client_exit_hook(void *);
59static void new_local_user_hook(void *);
60static void doing_stats_hook(void *);
212380e3
AC
61
62static void hurt_check_event(void *);
63static void hurt_expire_event(void *);
64
65static hurt_t *hurt_new(time_t, const char *, const char *);
66static void hurt_add(hurt_t *);
67static void hurt_propagate(struct Client *, struct Client *, hurt_t *);
68static hurt_t *hurt_find(const char *ip);
69static hurt_t *hurt_find_exact(const char *ip);
70static void hurt_remove(const char *ip);
71static void hurt_destroy(void *hurt);
72
3c7d6fcc 73static void heal_nick(struct Client *, struct Client *);
212380e3 74
212380e3
AC
75/* }}} */
76
77/* {{{ State containers */
78
ddcb223e 79rb_dlink_list hurt_confs = { NULL, NULL, 0 };
212380e3
AC
80
81/* }}} */
82
83/* {{{ Messages */
84struct Message hurt_msgtab = {
7baa37a9 85 "HURT", 0, 0, 0, 0, {
212380e3
AC
86 mg_ignore, mg_ignore, mg_ignore,
87 mg_ignore, {me_hurt, 0}, {mo_hurt, 3}
88 }
89};
90
91struct Message heal_msgtab = {
7baa37a9 92 "HEAL", 0, 0, 0, 0, {
212380e3
AC
93 mg_ignore, mg_ignore, mg_ignore,
94 mg_ignore, {me_heal, 0}, {mo_heal, 2}
95 }
96};
97/* }}} */
98
99/* {{{ Misc module stuff */
100mapi_hfn_list_av1 hurt_hfnlist[] = {
82436efb
EM
101 {"client_exit", client_exit_hook},
102 {"new_local_user", new_local_user_hook},
103 {"doing_stats", doing_stats_hook},
212380e3
AC
104 {NULL, NULL},
105};
106
107mapi_clist_av1 hurt_clist[] = { &hurt_msgtab, &heal_msgtab, NULL };
108
dacd2aa8
EM
109static const char hurt_desc[] =
110 "Prevents \"hurt\" users from messaging anyone but operators or "
111 "services until they identify or are \"healed\"";
112
113DECLARE_MODULE_AV2(
212380e3
AC
114 hurt,
115 modinit,
116 modfini,
117 hurt_clist,
118 NULL,
119 hurt_hfnlist,
dacd2aa8
EM
120 NULL,
121 NULL,
122 hurt_desc
212380e3
AC
123);
124/* }}} */
125
126hurt_state_t hurt_state = {
127 .cutoff = HURT_CUTOFF,
128 .default_expire = HURT_DEFAULT_EXPIRE,
129 .exit_reason = HURT_EXIT_REASON,
130};
131
132/*
133 * Module constructor/destructor.
134 */
135
136/* {{{ static int modinit() */
036a10a9
AC
137
138struct ev_entry *hurt_expire_ev = NULL;
139struct ev_entry *hurt_check_ev = NULL;
140
212380e3
AC
141static int
142modinit(void)
143{
144 /* set-up hurt_state. */
954012d3 145 hurt_state.start_time = rb_current_time();
212380e3
AC
146
147 /* add our event handlers. */
036a10a9
AC
148 hurt_expire_ev = rb_event_add("hurt_expire", hurt_expire_event, NULL, 60);
149 hurt_check_ev = rb_event_add("hurt_check", hurt_check_event, NULL, 5);
212380e3
AC
150
151 return 0;
152}
153/* }}} */
154
155/* {{{ static void modfini() */
156static void
157modfini(void)
158{
ddcb223e 159 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
160
161 /* and delete our events. */
036a10a9
AC
162 rb_event_delete(hurt_expire_ev);
163 rb_event_delete(hurt_check_ev);
212380e3 164
ddcb223e 165 RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_state.hurt_clients.head)
212380e3 166 {
ddcb223e 167 rb_dlinkDestroy(ptr, &hurt_state.hurt_clients);
212380e3
AC
168 }
169}
170/* }}} */
171
172/*
173 * Message handlers.
174 */
175
3c7d6fcc 176/* {{{ static void mo_hurt()
212380e3
AC
177 *
178 * HURT [<expire>] <ip> <reason>
55abcbb2 179 *
212380e3
AC
180 * parv[1] - expire or ip
181 * parv[2] - ip or reason
182 * parv[3] - reason or NULL
183 */
3c7d6fcc 184static void
760bafda 185mo_hurt(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
186 int parc, const char **parv)
187{
188 const char *ip, *expire, *reason;
189 int expire_time;
190 hurt_t *hurt;
191 struct Client *target_p;
192
193 if (!IsOperK(source_p)) {
194 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
195 source_p->name, "kline");
3c7d6fcc 196 return;
212380e3
AC
197 }
198
199 if (parc == 3)
200 expire = NULL, ip = parv[1], reason = parv[2];
201 else
202 expire = parv[1], ip = parv[2], reason = parv[3];
203
204 if (!expire)
205 expire_time = HURT_DEFAULT_EXPIRE;
206 if (expire && (expire_time = valid_temp_time(expire)) < 1) {
5366977b 207 sendto_one_notice(source_p, ":Permanent HURTs are not supported");
3c7d6fcc 208 return;
212380e3
AC
209 }
210 if (EmptyString(reason)) {
5366977b 211 sendto_one_notice(source_p, ":Empty HURT reasons are bad for business");
3c7d6fcc 212 return;
212380e3
AC
213 }
214
215 /* Is this a client? */
216 if (strchr(ip, '.') == NULL && strchr(ip, ':') == NULL)
217 {
218 target_p = find_named_person(ip);
219 if (target_p == NULL)
220 {
221 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
222 form_str(ERR_NOSUCHNICK), ip);
3c7d6fcc 223 return;
212380e3
AC
224 }
225 ip = target_p->orighost;
226 }
227 else
228 {
229 if (!strncmp(ip, "*@", 2))
230 ip += 2;
231 if (strchr(ip, '!') || strchr(ip, '@'))
232 {
233 sendto_one_notice(source_p, ":Invalid HURT mask [%s]",
234 ip);
3c7d6fcc 235 return;
212380e3
AC
236 }
237 }
238
239 if (hurt_find(ip) != NULL) {
5366977b 240 sendto_one(source_p, ":[%s] already HURT", ip);
3c7d6fcc 241 return;
212380e3
AC
242 }
243
244 /*
245 * okay, we've got this far, now it's time to add the the HURT locally
246 * and propagate it to other servers on the network.
247 */
248 sendto_realops_snomask(SNO_GENERAL, L_ALL,
249 "%s added HURT on [%s] for %ld minutes with reason [%s]",
250 get_oper_name(source_p), ip, (long) expire_time / 60, reason);
251
252 hurt = hurt_new(expire_time, ip, reason);
253 hurt_add(hurt);
254 hurt_propagate(NULL, source_p, hurt);
212380e3
AC
255}
256/* }}} */
257
3c7d6fcc 258/* {{{ static void me_hurt()
212380e3
AC
259 *
260 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
261 *
262 * parv[1] - expire
263 * parv[2] - ip
264 * parv[3] - reason
265 */
3c7d6fcc 266static void
760bafda 267me_hurt(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
268 int parc, const char **parv)
269{
270 time_t expire_time;
271 hurt_t *hurt;
272
273 /*
274 * right... if we don't get enough arguments, or if we get any invalid
275 * arguments, just ignore this request - shit happens, and it's not worth
276 * dropping a server over.
277 */
278 if (parc < 4 || !IsPerson(source_p))
3c7d6fcc 279 return;
212380e3 280 if ((expire_time = atoi(parv[1])) < 1)
3c7d6fcc 281 return;
212380e3 282 if (hurt_find(parv[2]) != NULL)
3c7d6fcc 283 return;
212380e3 284 if (EmptyString(parv[3]))
3c7d6fcc 285 return;
212380e3
AC
286
287 sendto_realops_snomask(SNO_GENERAL, L_ALL,
288 "%s added HURT on [%s] for %ld minutes with reason [%s]",
289 get_oper_name(source_p), parv[2], (long) expire_time / 60, parv[3]);
290 hurt = hurt_new(expire_time, parv[2], parv[3]);
291 hurt_add(hurt);
212380e3
AC
292}
293/* }}} */
294
3c7d6fcc 295/* {{{ static void mo_heal()
212380e3
AC
296 *
297 * HURT <nick>|<ip>
298 *
299 * parv[1] - nick or ip
300 */
3c7d6fcc 301static void
760bafda 302mo_heal(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
303 int parc, const char **parv)
304{
305 struct Client *target_p;
306
307 if (!IsOperUnkline(source_p))
308 {
309 sendto_one(source_p, form_str(ERR_NOPRIVS),
310 me.name, source_p->name, "unkline");
3c7d6fcc 311 return;
212380e3
AC
312 }
313
2d28539c 314 if (clean_nick(parv[1], 0))
212380e3
AC
315 {
316 target_p = find_named_person(parv[1]);
317 if (target_p == NULL)
318 {
319 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
320 form_str(ERR_NOSUCHNICK), parv[1]);
3c7d6fcc 321 return;
212380e3
AC
322 }
323 if (MyConnect(target_p))
324 heal_nick(source_p, target_p);
325 else
326 sendto_one(target_p, ":%s ENCAP %s HEAL %s",
327 get_id(source_p, target_p),
328 target_p->servptr->name,
329 get_id(target_p, target_p));
330 }
331 else if (strchr(parv[1], '.'))
332 {
333 if (hurt_find_exact(parv[1]) == NULL)
334 {
5366977b 335 sendto_one_notice(source_p, ":Mask [%s] is not HURT", parv[1]);
3c7d6fcc 336 return;
212380e3
AC
337 }
338 hurt_remove(parv[1]);
339 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s removed HURT on %s",
340 get_oper_name(source_p), parv[1]);
341 sendto_server(NULL, NULL, NOCAPS, NOCAPS, ":%s ENCAP * HEAL %s",
342 source_p->name, parv[1]);
343 }
344 else
345 {
5366977b 346 sendto_one(source_p, ":[%s] is not a valid IP address/nick", parv[1]);
3c7d6fcc 347 return;
212380e3 348 }
212380e3
AC
349}
350/* }}} */
351
3c7d6fcc 352static void
760bafda 353me_heal(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
354 int parc, const char **parv)
355{
356 struct Client *target_p;
357
358 /* as noted in me_hurt(), if we don't get sufficient arguments...
359 * *poof*, it's dropped...
360 */
361 if (parc < 2)
3c7d6fcc 362 return;
212380e3 363
2d28539c 364 if (clean_nick(parv[1], 0))
212380e3
AC
365 {
366 target_p = find_person(parv[1]);
367 if (target_p != NULL && MyConnect(target_p))
368 heal_nick(source_p, target_p);
369 }
370 else if (strchr(parv[1], '.')) /* host or mask to remove ban for */
371 {
372 if (hurt_find_exact(parv[1]) == NULL)
3c7d6fcc 373 return;
212380e3
AC
374
375 hurt_remove(parv[1]);
376 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s removed HURT on %s",
377 get_oper_name(source_p), parv[1]);
378 }
212380e3
AC
379}
380
381/*
382 * Event handlers.
383 */
384
385/* {{{ static void hurt_check_event() */
386static void
387hurt_check_event(void *arg)
388{
ddcb223e 389 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
390 struct Client *client_p;
391
ddcb223e 392 RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_state.hurt_clients.head) {
212380e3
AC
393 client_p = ptr->data;
394 if (!EmptyString(client_p->user->suser))
395 {
ddcb223e 396 rb_dlinkDestroy(ptr, &hurt_state.hurt_clients);
212380e3 397 sendto_one_notice(client_p, ":HURT restriction removed for this session");
954012d3 398 client_p->localClient->target_last = rb_current_time(); /* don't ask --nenolod */
212380e3
AC
399 }
400 else if (client_p->localClient->receiveM > hurt_state.cutoff)
401 exit_client(NULL, client_p, &me, hurt_state.exit_reason);
402 }
403}
404/* }}} */
405
406/* {{{ static void hurt_expire_event() */
407static void
408hurt_expire_event(void *unused)
409{
ddcb223e 410 rb_dlink_node *ptr, *next_ptr;
212380e3
AC
411 hurt_t *hurt;
412
ddcb223e 413 RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_confs.head)
212380e3
AC
414 {
415 hurt = (hurt_t *) ptr->data;
416
954012d3 417 if (hurt->expire <= rb_current_time())
212380e3 418 {
ddcb223e 419 rb_dlinkFindDestroy(hurt, &hurt_confs);
212380e3
AC
420 hurt_destroy(hurt);
421 }
422 }
423}
424/* }}} */
425
426/*
427 * Hook functions.
428 */
429
430/* {{{ static void client_exit_hook() */
431static void
82436efb 432client_exit_hook(void *data_)
212380e3 433{
82436efb 434 hook_data_client_exit *data = data_;
212380e3
AC
435 s_assert(data != NULL);
436 s_assert(data->target != NULL);
437
ddcb223e 438 rb_dlinkFindDestroy(data->target, &hurt_state.hurt_clients);
212380e3
AC
439}
440/* }}} */
441
442/* {{{ static void new_local_user_hook() */
443static void
82436efb 444new_local_user_hook(void *data)
212380e3 445{
82436efb 446 struct Client *source_p = data;
212380e3
AC
447 if (IsAnyDead(source_p) || !EmptyString(source_p->user->suser) ||
448 IsExemptKline(source_p))
449 return;
450
451 if (hurt_find(source_p->sockhost) || hurt_find(source_p->orighost))
452 {
954012d3 453 source_p->localClient->target_last = rb_current_time() + 600; /* don't ask --nenolod */
212380e3 454 SetTGChange(source_p);
ddcb223e 455 rb_dlinkAddAlloc(source_p, &hurt_state.hurt_clients);
212380e3 456 sendto_one_notice(source_p, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
55abcbb2 457 }
212380e3
AC
458}
459/* }}} */
460
461/* {{{ static void doing_stats_hook() */
462static void
82436efb 463doing_stats_hook(void *data)
212380e3 464{
82436efb 465 hook_data_int *hdata = data;
ddcb223e 466 rb_dlink_node *ptr;
212380e3
AC
467 hurt_t *hurt;
468 struct Client *source_p;
469
470 s_assert(hdata);
471 s_assert(hdata->client);
472
473 source_p = hdata->client;
474 if(hdata->arg2 != (int) 's')
475 return;
d4f7eb4c 476 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOperGeneral(source_p))
212380e3 477 return;
d4f7eb4c 478 if ((ConfigFileEntry.stats_k_oper_only == 1) && !IsOperGeneral(source_p))
212380e3
AC
479 {
480 hurt = hurt_find(source_p->sockhost);
481 if (hurt != NULL)
482 {
483 sendto_one_numeric(source_p, RPL_STATSKLINE,
484 form_str(RPL_STATSKLINE), 's',
485 "*", hurt->ip, hurt->reason, "", "");
486 return;
487 }
488
489 hurt = hurt_find(source_p->orighost);
490 if (hurt != NULL)
491 {
492 sendto_one_numeric(source_p, RPL_STATSKLINE,
493 form_str(RPL_STATSKLINE), 's',
494 "*", hurt->ip, hurt->reason, "", "");
212380e3
AC
495 }
496 return;
497 }
498
ddcb223e 499 RB_DLINK_FOREACH(ptr, hurt_confs.head)
212380e3
AC
500 {
501 hurt = (hurt_t *) ptr->data;
502 sendto_one_numeric(source_p, RPL_STATSKLINE,
503 form_str(RPL_STATSKLINE), 's',
504 "*", hurt->ip, hurt->reason, "", "");
505 }
506}
507/* }}} */
508
509/* {{{ static void hurt_propagate()
510 *
511 * client_p - specific server to propagate HURT to, or NULL to propagate to all
512 * servers.
513 * source_p - source (oper who added the HURT)
514 * hurt - HURT to be propagated
515 */
516static void
517hurt_propagate(struct Client *client_p, struct Client *source_p, hurt_t *hurt)
518{
519 if (client_p)
520 sendto_one(client_p,
521 ":%s ENCAP %s HURT %ld %s :%s",
522 source_p->name, client_p->name,
954012d3 523 (long)(hurt->expire - rb_current_time()),
212380e3
AC
524 hurt->ip, hurt->reason);
525 else
526 sendto_server(&me, NULL, NOCAPS, NOCAPS,
527 ":%s ENCAP * HURT %ld %s :%s",
528 source_p->name,
954012d3 529 (long)(hurt->expire - rb_current_time()),
212380e3
AC
530 hurt->ip, hurt->reason);
531}
532/* }}} */
533
534/* {{{ static hurt_t *hurt_new() */
535static hurt_t *
536hurt_new(time_t expire, const char *ip, const char *reason)
537{
538 hurt_t *hurt;
539
b42e202d 540 hurt = rb_malloc(sizeof(hurt_t));
212380e3 541
b42e202d
VY
542 hurt->ip = rb_strdup(ip);
543 hurt->reason = rb_strdup(reason);
954012d3 544 hurt->expire = rb_current_time() + expire;
212380e3
AC
545
546 return hurt;
547}
548/* }}} */
549
550/* {{{ static void hurt_destroy() */
551static void
552hurt_destroy(void *hurt)
553{
554 hurt_t *h;
555
556 if (!hurt)
557 return;
558
559 h = (hurt_t *) hurt;
b42e202d
VY
560 rb_free(h->ip);
561 rb_free(h->reason);
562 rb_free(h);
212380e3
AC
563}
564/* }}} */
565
566static void
567hurt_add(hurt_t *hurt)
568{
ddcb223e 569 rb_dlinkAddAlloc(hurt, &hurt_confs);
212380e3
AC
570}
571
572static hurt_t *
573hurt_find_exact(const char *ip)
574{
ddcb223e 575 rb_dlink_node *ptr;
212380e3
AC
576 hurt_t *hurt;
577
ddcb223e 578 RB_DLINK_FOREACH(ptr, hurt_confs.head)
212380e3
AC
579 {
580 hurt = (hurt_t *) ptr->data;
581
f956cb0f 582 if (!rb_strcasecmp(ip, hurt->ip))
212380e3
AC
583 return hurt;
584 }
585
586 return NULL;
587}
588
589static hurt_t *
590hurt_find(const char *ip)
591{
ddcb223e 592 rb_dlink_node *ptr;
212380e3
AC
593 hurt_t *hurt;
594
ddcb223e 595 RB_DLINK_FOREACH(ptr, hurt_confs.head)
212380e3
AC
596 {
597 hurt = (hurt_t *) ptr->data;
598
599 if (match(hurt->ip, ip))
600 return hurt;
601 }
602
603 return NULL;
604}
605
606static void
607hurt_remove(const char *ip)
608{
609 hurt_t *hurt = hurt_find_exact(ip);
610
ddcb223e 611 rb_dlinkFindDestroy(hurt, &hurt_confs);
212380e3
AC
612 hurt_destroy(hurt);
613}
614
3c7d6fcc
EM
615/* {{{ static void heal_nick() */
616static void
212380e3
AC
617heal_nick(struct Client *source_p, struct Client *target_p)
618{
ddcb223e 619 if (rb_dlinkFindDestroy(target_p, &hurt_state.hurt_clients))
212380e3 620 {
a9227555 621 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s used HEAL on %s",
212380e3
AC
622 get_oper_name(source_p), get_client_name(target_p, HIDE_IP));
623 sendto_one_notice(target_p, ":HURT restriction temporarily removed by operator");
624 sendto_one_notice(source_p, ":HURT restriction on %s temporarily removed", target_p->name);
954012d3 625 target_p->localClient->target_last = rb_current_time(); /* don't ask --nenolod */
212380e3
AC
626 }
627 else
628 {
629 sendto_one_notice(source_p, ":%s was not hurt", target_p->name);
212380e3
AC
630 }
631}
632/* }}} */
633
212380e3
AC
634/*
635 * vim: ts=8 sw=8 noet fdm=marker tw=80
636 */