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