2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
4 * Copyright (C) 2006 charybdis development team
7 * $Id: hurt.c 3161 2007-01-25 07:23:01Z nenolod $
18 #include "s_newconf.h"
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"
31 typedef struct _hurt_state
{
34 rb_dlink_list hurt_clients
;
36 time_t default_expire
;
37 const char *exit_reason
;
40 typedef struct _hurt
{
42 struct sockaddr
*saddr
;
50 static int mo_hurt(struct Client
*, struct Client
*, int, const char **);
51 static int me_hurt(struct Client
*, struct Client
*, int, const char **);
52 static int mo_heal(struct Client
*, struct Client
*, int, const char **);
53 static int me_heal(struct Client
*, struct Client
*, int, const char **);
55 static int modinit(void);
56 static void modfini(void);
58 static void client_exit_hook(hook_data_client_exit
*);
59 static void new_local_user_hook(struct Client
*);
60 static void doing_stats_hook(hook_data_int
*hdata
);
62 static void hurt_check_event(void *);
63 static void hurt_expire_event(void *);
65 static hurt_t
*hurt_new(time_t, const char *, const char *);
66 static void hurt_add(hurt_t
*);
67 static void hurt_propagate(struct Client
*, struct Client
*, hurt_t
*);
68 static hurt_t
*hurt_find(const char *ip
);
69 static hurt_t
*hurt_find_exact(const char *ip
);
70 static void hurt_remove(const char *ip
);
71 static void hurt_destroy(void *hurt
);
73 static int heal_nick(struct Client
*, struct Client
*);
75 static int nick_is_valid(const char *);
78 /* {{{ State containers */
80 rb_dlink_list hurt_confs
= { NULL
, NULL
, 0 };
85 struct Message hurt_msgtab
= {
86 "HURT", 0, 0, 0, MFLG_SLOW
, {
87 mg_ignore
, mg_ignore
, mg_ignore
,
88 mg_ignore
, {me_hurt
, 0}, {mo_hurt
, 3}
92 struct Message heal_msgtab
= {
93 "HEAL", 0, 0, 0, MFLG_SLOW
, {
94 mg_ignore
, mg_ignore
, mg_ignore
,
95 mg_ignore
, {me_heal
, 0}, {mo_heal
, 2}
100 /* {{{ Misc module stuff */
101 mapi_hfn_list_av1 hurt_hfnlist
[] = {
102 {"client_exit", (hookfn
) client_exit_hook
},
103 {"new_local_user", (hookfn
) new_local_user_hook
},
104 {"doing_stats", (hookfn
) doing_stats_hook
},
108 mapi_clist_av1 hurt_clist
[] = { &hurt_msgtab
, &heal_msgtab
, NULL
};
121 hurt_state_t hurt_state
= {
122 .cutoff
= HURT_CUTOFF
,
123 .default_expire
= HURT_DEFAULT_EXPIRE
,
124 .exit_reason
= HURT_EXIT_REASON
,
128 * Module constructor/destructor.
131 /* {{{ static int modinit() */
133 struct ev_entry
*hurt_expire_ev
= NULL
;
134 struct ev_entry
*hurt_check_ev
= NULL
;
139 /* set-up hurt_state. */
140 hurt_state
.start_time
= rb_current_time();
142 /* add our event handlers. */
143 hurt_expire_ev
= rb_event_add("hurt_expire", hurt_expire_event
, NULL
, 60);
144 hurt_check_ev
= rb_event_add("hurt_check", hurt_check_event
, NULL
, 5);
150 /* {{{ static void modfini() */
154 rb_dlink_node
*ptr
, *next_ptr
;
156 /* and delete our events. */
157 rb_event_delete(hurt_expire_ev
);
158 rb_event_delete(hurt_check_ev
);
160 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
)
162 rb_dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
171 /* {{{ static int mo_hurt()
173 * HURT [<expire>] <ip> <reason>
175 * parv[1] - expire or ip
176 * parv[2] - ip or reason
177 * parv[3] - reason or NULL
180 mo_hurt(struct Client
*client_p
, struct Client
*source_p
,
181 int parc
, const char **parv
)
183 const char *ip
, *expire
, *reason
;
186 struct Client
*target_p
;
188 if (!IsOperK(source_p
)) {
189 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
190 source_p
->name
, "kline");
195 expire
= NULL
, ip
= parv
[1], reason
= parv
[2];
197 expire
= parv
[1], ip
= parv
[2], reason
= parv
[3];
200 expire_time
= HURT_DEFAULT_EXPIRE
;
201 if (expire
&& (expire_time
= valid_temp_time(expire
)) < 1) {
202 sendto_one_notice(source_p
, ":Permanent HURTs are not supported");
205 if (EmptyString(reason
)) {
206 sendto_one_notice(source_p
, ":Empty HURT reasons are bad for business");
210 /* Is this a client? */
211 if (strchr(ip
, '.') == NULL
&& strchr(ip
, ':') == NULL
)
213 target_p
= find_named_person(ip
);
214 if (target_p
== NULL
)
216 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
217 form_str(ERR_NOSUCHNICK
), ip
);
220 ip
= target_p
->orighost
;
224 if (!strncmp(ip
, "*@", 2))
226 if (strchr(ip
, '!') || strchr(ip
, '@'))
228 sendto_one_notice(source_p
, ":Invalid HURT mask [%s]",
234 if (hurt_find(ip
) != NULL
) {
235 sendto_one(source_p
, ":[%s] already HURT", ip
);
240 * okay, we've got this far, now it's time to add the the HURT locally
241 * and propagate it to other servers on the network.
243 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
244 "%s added HURT on [%s] for %ld minutes with reason [%s]",
245 get_oper_name(source_p
), ip
, (long) expire_time
/ 60, reason
);
247 hurt
= hurt_new(expire_time
, ip
, reason
);
249 hurt_propagate(NULL
, source_p
, hurt
);
255 /* {{{ static int me_hurt()
257 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
264 me_hurt(struct Client
*client_p
, struct Client
*source_p
,
265 int parc
, const char **parv
)
271 * right... if we don't get enough arguments, or if we get any invalid
272 * arguments, just ignore this request - shit happens, and it's not worth
273 * dropping a server over.
275 if (parc
< 4 || !IsPerson(source_p
))
277 if ((expire_time
= atoi(parv
[1])) < 1)
279 if (hurt_find(parv
[2]) != NULL
)
281 if (EmptyString(parv
[3]))
284 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
285 "%s added HURT on [%s] for %ld minutes with reason [%s]",
286 get_oper_name(source_p
), parv
[2], (long) expire_time
/ 60, parv
[3]);
287 hurt
= hurt_new(expire_time
, parv
[2], parv
[3]);
294 /* {{{ static int mo_heal()
298 * parv[1] - nick or ip
301 mo_heal(struct Client
*client_p
, struct Client
*source_p
,
302 int parc
, const char **parv
)
304 struct Client
*target_p
;
306 if (!IsOperUnkline(source_p
))
308 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
309 me
.name
, source_p
->name
, "unkline");
313 if (nick_is_valid(parv
[1]))
315 target_p
= find_named_person(parv
[1]);
316 if (target_p
== NULL
)
318 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
319 form_str(ERR_NOSUCHNICK
), parv
[1]);
322 if (MyConnect(target_p
))
323 heal_nick(source_p
, target_p
);
325 sendto_one(target_p
, ":%s ENCAP %s HEAL %s",
326 get_id(source_p
, target_p
),
327 target_p
->servptr
->name
,
328 get_id(target_p
, target_p
));
330 else if (strchr(parv
[1], '.'))
332 if (hurt_find_exact(parv
[1]) == NULL
)
334 sendto_one_notice(source_p
, ":Mask [%s] is not HURT", parv
[1]);
337 hurt_remove(parv
[1]);
338 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
339 get_oper_name(source_p
), parv
[1]);
340 sendto_server(NULL
, NULL
, NOCAPS
, NOCAPS
, ":%s ENCAP * HEAL %s",
341 source_p
->name
, parv
[1]);
345 sendto_one(source_p
, ":[%s] is not a valid IP address/nick", parv
[1]);
354 me_heal(struct Client
*client_p
, struct Client
*source_p
,
355 int parc
, const char **parv
)
357 struct Client
*target_p
;
359 /* as noted in me_hurt(), if we don't get sufficient arguments...
360 * *poof*, it's dropped...
365 if (nick_is_valid(parv
[1]))
367 target_p
= find_person(parv
[1]);
368 if (target_p
!= NULL
&& MyConnect(target_p
))
369 heal_nick(source_p
, target_p
);
371 else if (strchr(parv
[1], '.')) /* host or mask to remove ban for */
373 if (hurt_find_exact(parv
[1]) == NULL
)
376 hurt_remove(parv
[1]);
377 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
378 get_oper_name(source_p
), parv
[1]);
390 /* {{{ static void hurt_check_event() */
392 hurt_check_event(void *arg
)
394 rb_dlink_node
*ptr
, *next_ptr
;
395 struct Client
*client_p
;
397 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
) {
398 client_p
= ptr
->data
;
399 if (!EmptyString(client_p
->user
->suser
))
401 rb_dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
402 sendto_one_notice(client_p
, ":HURT restriction removed for this session");
403 client_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
405 else if (client_p
->localClient
->receiveM
> hurt_state
.cutoff
)
406 exit_client(NULL
, client_p
, &me
, hurt_state
.exit_reason
);
411 /* {{{ static void hurt_expire_event() */
413 hurt_expire_event(void *unused
)
415 rb_dlink_node
*ptr
, *next_ptr
;
418 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_confs
.head
)
420 hurt
= (hurt_t
*) ptr
->data
;
422 if (hurt
->expire
<= rb_current_time())
424 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
435 /* {{{ static void client_exit_hook() */
437 client_exit_hook(hook_data_client_exit
*data
)
439 s_assert(data
!= NULL
);
440 s_assert(data
->target
!= NULL
);
442 rb_dlinkFindDestroy(data
->target
, &hurt_state
.hurt_clients
);
446 /* {{{ static void new_local_user_hook() */
448 new_local_user_hook(struct Client
*source_p
)
450 if (IsAnyDead(source_p
) || !EmptyString(source_p
->user
->suser
) ||
451 IsExemptKline(source_p
))
454 if (hurt_find(source_p
->sockhost
) || hurt_find(source_p
->orighost
))
456 source_p
->localClient
->target_last
= rb_current_time() + 600; /* don't ask --nenolod */
457 SetTGChange(source_p
);
458 rb_dlinkAddAlloc(source_p
, &hurt_state
.hurt_clients
);
459 sendto_one_notice(source_p
, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
464 /* {{{ static void doing_stats_hook() */
466 doing_stats_hook(hook_data_int
*hdata
)
470 struct Client
*source_p
;
473 s_assert(hdata
->client
);
475 source_p
= hdata
->client
;
476 if(hdata
->arg2
!= (int) 's')
478 if((ConfigFileEntry
.stats_k_oper_only
== 2) && !IsOper(source_p
))
480 if ((ConfigFileEntry
.stats_k_oper_only
== 1) && !IsOper(source_p
))
482 hurt
= hurt_find(source_p
->sockhost
);
485 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
486 form_str(RPL_STATSKLINE
), 's',
487 "*", hurt
->ip
, hurt
->reason
, "", "");
491 hurt
= hurt_find(source_p
->orighost
);
494 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
495 form_str(RPL_STATSKLINE
), 's',
496 "*", hurt
->ip
, hurt
->reason
, "", "");
502 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
504 hurt
= (hurt_t
*) ptr
->data
;
505 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
506 form_str(RPL_STATSKLINE
), 's',
507 "*", hurt
->ip
, hurt
->reason
, "", "");
512 /* {{{ static void hurt_propagate()
514 * client_p - specific server to propagate HURT to, or NULL to propagate to all
516 * source_p - source (oper who added the HURT)
517 * hurt - HURT to be propagated
520 hurt_propagate(struct Client
*client_p
, struct Client
*source_p
, hurt_t
*hurt
)
524 ":%s ENCAP %s HURT %ld %s :%s",
525 source_p
->name
, client_p
->name
,
526 (long)(hurt
->expire
- rb_current_time()),
527 hurt
->ip
, hurt
->reason
);
529 sendto_server(&me
, NULL
, NOCAPS
, NOCAPS
,
530 ":%s ENCAP * HURT %ld %s :%s",
532 (long)(hurt
->expire
- rb_current_time()),
533 hurt
->ip
, hurt
->reason
);
537 /* {{{ static hurt_t *hurt_new() */
539 hurt_new(time_t expire
, const char *ip
, const char *reason
)
543 hurt
= rb_malloc(sizeof(hurt_t
));
545 hurt
->ip
= rb_strdup(ip
);
546 hurt
->reason
= rb_strdup(reason
);
547 hurt
->expire
= rb_current_time() + expire
;
553 /* {{{ static void hurt_destroy() */
555 hurt_destroy(void *hurt
)
570 hurt_add(hurt_t
*hurt
)
572 rb_dlinkAddAlloc(hurt
, &hurt_confs
);
576 hurt_find_exact(const char *ip
)
581 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
583 hurt
= (hurt_t
*) ptr
->data
;
585 if (!strcasecmp(ip
, hurt
->ip
))
593 hurt_find(const char *ip
)
598 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
600 hurt
= (hurt_t
*) ptr
->data
;
602 if (match(hurt
->ip
, ip
))
610 hurt_remove(const char *ip
)
612 hurt_t
*hurt
= hurt_find_exact(ip
);
614 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
618 /* {{{ static int heal_nick() */
620 heal_nick(struct Client
*source_p
, struct Client
*target_p
)
622 if (rb_dlinkFindDestroy(target_p
, &hurt_state
.hurt_clients
))
624 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s used HEAL on %s",
625 get_oper_name(source_p
), get_client_name(target_p
, HIDE_IP
));
626 sendto_one_notice(target_p
, ":HURT restriction temporarily removed by operator");
627 sendto_one_notice(source_p
, ":HURT restriction on %s temporarily removed", target_p
->name
);
628 target_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
633 sendto_one_notice(source_p
, ":%s was not hurt", target_p
->name
);
643 /* {{{ static int nick_is_valid() */
645 nick_is_valid(const char *nick
)
647 const char *s
= nick
;
649 for (; *s
!= '\0'; s
++) {
659 * vim: ts=8 sw=8 noet fdm=marker tw=80