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 $
19 #include "s_newconf.h"
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"
32 typedef struct _hurt_state
{
35 dlink_list hurt_clients
;
37 time_t default_expire
;
38 const char *exit_reason
;
41 typedef struct _hurt
{
43 struct sockaddr
*saddr
;
51 static int mo_hurt(struct Client
*, struct Client
*, int, const char **);
52 static int me_hurt(struct Client
*, struct Client
*, int, const char **);
53 static int mo_heal(struct Client
*, struct Client
*, int, const char **);
54 static int me_heal(struct Client
*, struct Client
*, int, const char **);
56 static int modinit(void);
57 static void modfini(void);
59 static void client_exit_hook(hook_data_client_exit
*);
60 static void new_local_user_hook(struct Client
*);
61 static void doing_stats_hook(hook_data_int
*hdata
);
63 static void hurt_check_event(void *);
64 static void hurt_expire_event(void *);
66 static hurt_t
*hurt_new(time_t, const char *, const char *);
67 static void hurt_add(hurt_t
*);
68 static void hurt_propagate(struct Client
*, struct Client
*, hurt_t
*);
69 static hurt_t
*hurt_find(const char *ip
);
70 static hurt_t
*hurt_find_exact(const char *ip
);
71 static void hurt_remove(const char *ip
);
72 static void hurt_destroy(void *hurt
);
74 static int heal_nick(struct Client
*, struct Client
*);
76 static int nick_is_valid(const char *);
79 /* {{{ State containers */
81 dlink_list hurt_confs
= { NULL
, NULL
, 0 };
86 struct 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}
93 struct 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}
101 /* {{{ Misc module stuff */
102 mapi_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
},
109 mapi_clist_av1 hurt_clist
[] = { &hurt_msgtab
, &heal_msgtab
, NULL
};
122 hurt_state_t hurt_state
= {
123 .cutoff
= HURT_CUTOFF
,
124 .default_expire
= HURT_DEFAULT_EXPIRE
,
125 .exit_reason
= HURT_EXIT_REASON
,
129 * Module constructor/destructor.
132 /* {{{ static int modinit() */
134 struct ev_entry
*hurt_expire_ev
= NULL
;
135 struct ev_entry
*hurt_check_ev
= NULL
;
140 /* set-up hurt_state. */
141 hurt_state
.start_time
= CurrentTime
;
143 /* add our event handlers. */
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);
151 /* {{{ static void modfini() */
155 dlink_node
*ptr
, *next_ptr
;
157 /* and delete our events. */
158 rb_event_delete(hurt_expire_ev
);
159 rb_event_delete(hurt_check_ev
);
161 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
)
163 dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
172 /* {{{ static int mo_hurt()
174 * HURT [<expire>] <ip> <reason>
176 * parv[1] - expire or ip
177 * parv[2] - ip or reason
178 * parv[3] - reason or NULL
181 mo_hurt(struct Client
*client_p
, struct Client
*source_p
,
182 int parc
, const char **parv
)
184 const char *ip
, *expire
, *reason
;
187 struct Client
*target_p
;
189 if (!IsOperK(source_p
)) {
190 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
191 source_p
->name
, "kline");
196 expire
= NULL
, ip
= parv
[1], reason
= parv
[2];
198 expire
= parv
[1], ip
= parv
[2], reason
= parv
[3];
201 expire_time
= HURT_DEFAULT_EXPIRE
;
202 if (expire
&& (expire_time
= valid_temp_time(expire
)) < 1) {
203 sendto_one_notice(source_p
, ":Permanent HURTs are not supported");
206 if (EmptyString(reason
)) {
207 sendto_one_notice(source_p
, ":Empty HURT reasons are bad for business");
211 /* Is this a client? */
212 if (strchr(ip
, '.') == NULL
&& strchr(ip
, ':') == NULL
)
214 target_p
= find_named_person(ip
);
215 if (target_p
== NULL
)
217 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
218 form_str(ERR_NOSUCHNICK
), ip
);
221 ip
= target_p
->orighost
;
225 if (!strncmp(ip
, "*@", 2))
227 if (strchr(ip
, '!') || strchr(ip
, '@'))
229 sendto_one_notice(source_p
, ":Invalid HURT mask [%s]",
235 if (hurt_find(ip
) != NULL
) {
236 sendto_one(source_p
, ":[%s] already HURT", ip
);
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.
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
);
248 hurt
= hurt_new(expire_time
, ip
, reason
);
250 hurt_propagate(NULL
, source_p
, hurt
);
256 /* {{{ static int me_hurt()
258 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
265 me_hurt(struct Client
*client_p
, struct Client
*source_p
,
266 int parc
, const char **parv
)
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.
276 if (parc
< 4 || !IsPerson(source_p
))
278 if ((expire_time
= atoi(parv
[1])) < 1)
280 if (hurt_find(parv
[2]) != NULL
)
282 if (EmptyString(parv
[3]))
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]);
295 /* {{{ static int mo_heal()
299 * parv[1] - nick or ip
302 mo_heal(struct Client
*client_p
, struct Client
*source_p
,
303 int parc
, const char **parv
)
305 struct Client
*target_p
;
307 if (!IsOperUnkline(source_p
))
309 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
310 me
.name
, source_p
->name
, "unkline");
314 if (nick_is_valid(parv
[1]))
316 target_p
= find_named_person(parv
[1]);
317 if (target_p
== NULL
)
319 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
320 form_str(ERR_NOSUCHNICK
), parv
[1]);
323 if (MyConnect(target_p
))
324 heal_nick(source_p
, target_p
);
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
));
331 else if (strchr(parv
[1], '.'))
333 if (hurt_find_exact(parv
[1]) == NULL
)
335 sendto_one_notice(source_p
, ":Mask [%s] is not HURT", parv
[1]);
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]);
346 sendto_one(source_p
, ":[%s] is not a valid IP address/nick", parv
[1]);
355 me_heal(struct Client
*client_p
, struct Client
*source_p
,
356 int parc
, const char **parv
)
358 struct Client
*target_p
;
360 /* as noted in me_hurt(), if we don't get sufficient arguments...
361 * *poof*, it's dropped...
366 if (nick_is_valid(parv
[1]))
368 target_p
= find_person(parv
[1]);
369 if (target_p
!= NULL
&& MyConnect(target_p
))
370 heal_nick(source_p
, target_p
);
372 else if (strchr(parv
[1], '.')) /* host or mask to remove ban for */
374 if (hurt_find_exact(parv
[1]) == NULL
)
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]);
391 /* {{{ static void hurt_check_event() */
393 hurt_check_event(void *arg
)
395 dlink_node
*ptr
, *next_ptr
;
396 struct Client
*client_p
;
398 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
) {
399 client_p
= ptr
->data
;
400 if (!EmptyString(client_p
->user
->suser
))
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 */
407 else if (client_p
->localClient
->receiveM
> hurt_state
.cutoff
)
408 exit_client(NULL
, client_p
, &me
, hurt_state
.exit_reason
);
413 /* {{{ static void hurt_expire_event() */
415 hurt_expire_event(void *unused
)
417 dlink_node
*ptr
, *next_ptr
;
420 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_confs
.head
)
422 hurt
= (hurt_t
*) ptr
->data
;
424 if (hurt
->expire
<= CurrentTime
)
426 dlinkFindDestroy(hurt
, &hurt_confs
);
437 /* {{{ static void client_exit_hook() */
439 client_exit_hook(hook_data_client_exit
*data
)
441 s_assert(data
!= NULL
);
442 s_assert(data
->target
!= NULL
);
444 dlinkFindDestroy(data
->target
, &hurt_state
.hurt_clients
);
448 /* {{{ static void new_local_user_hook() */
450 new_local_user_hook(struct Client
*source_p
)
452 if (IsAnyDead(source_p
) || !EmptyString(source_p
->user
->suser
) ||
453 IsExemptKline(source_p
))
456 if (hurt_find(source_p
->sockhost
) || hurt_find(source_p
->orighost
))
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.");
467 /* {{{ static void doing_stats_hook() */
469 doing_stats_hook(hook_data_int
*hdata
)
473 struct Client
*source_p
;
476 s_assert(hdata
->client
);
478 source_p
= hdata
->client
;
479 if(hdata
->arg2
!= (int) 's')
481 if((ConfigFileEntry
.stats_k_oper_only
== 2) && !IsOper(source_p
))
483 if ((ConfigFileEntry
.stats_k_oper_only
== 1) && !IsOper(source_p
))
485 hurt
= hurt_find(source_p
->sockhost
);
488 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
489 form_str(RPL_STATSKLINE
), 's',
490 "*", hurt
->ip
, hurt
->reason
, "", "");
494 hurt
= hurt_find(source_p
->orighost
);
497 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
498 form_str(RPL_STATSKLINE
), 's',
499 "*", hurt
->ip
, hurt
->reason
, "", "");
505 DLINK_FOREACH(ptr
, hurt_confs
.head
)
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
, "", "");
515 /* {{{ static void hurt_propagate()
517 * client_p - specific server to propagate HURT to, or NULL to propagate to all
519 * source_p - source (oper who added the HURT)
520 * hurt - HURT to be propagated
523 hurt_propagate(struct Client
*client_p
, struct Client
*source_p
, hurt_t
*hurt
)
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
);
532 sendto_server(&me
, NULL
, NOCAPS
, NOCAPS
,
533 ":%s ENCAP * HURT %ld %s :%s",
535 (long)(hurt
->expire
- CurrentTime
),
536 hurt
->ip
, hurt
->reason
);
540 /* {{{ static hurt_t *hurt_new() */
542 hurt_new(time_t expire
, const char *ip
, const char *reason
)
546 hurt
= MyMalloc(sizeof(hurt_t
));
548 DupString(hurt
->ip
, ip
);
549 DupString(hurt
->reason
, reason
);
550 hurt
->expire
= CurrentTime
+ expire
;
556 /* {{{ static void hurt_destroy() */
558 hurt_destroy(void *hurt
)
573 hurt_add(hurt_t
*hurt
)
575 dlinkAddAlloc(hurt
, &hurt_confs
);
579 hurt_find_exact(const char *ip
)
584 DLINK_FOREACH(ptr
, hurt_confs
.head
)
586 hurt
= (hurt_t
*) ptr
->data
;
588 if (!strcasecmp(ip
, hurt
->ip
))
596 hurt_find(const char *ip
)
601 DLINK_FOREACH(ptr
, hurt_confs
.head
)
603 hurt
= (hurt_t
*) ptr
->data
;
605 if (match(hurt
->ip
, ip
))
613 hurt_remove(const char *ip
)
615 hurt_t
*hurt
= hurt_find_exact(ip
);
617 dlinkFindDestroy(hurt
, &hurt_confs
);
621 /* {{{ static int heal_nick() */
623 heal_nick(struct Client
*source_p
, struct Client
*target_p
)
625 if (dlinkFindDestroy(target_p
, &hurt_state
.hurt_clients
))
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 */
637 sendto_one_notice(source_p
, ":%s was not hurt", target_p
->name
);
647 /* {{{ static int nick_is_valid() */
649 nick_is_valid(const char *nick
)
651 const char *s
= nick
;
653 for (; *s
!= '\0'; s
++) {
663 * vim: ts=8 sw=8 noet fdm=marker tw=80