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() */
136 /* set-up hurt_state. */
137 hurt_state
.start_time
= CurrentTime
;
139 /* add our event handlers. */
140 eventAdd("hurt_expire", hurt_expire_event
, NULL
, 60);
141 eventAdd("hurt_check", hurt_check_event
, NULL
, 5);
147 /* {{{ static void modfini() */
151 dlink_node
*ptr
, *next_ptr
;
153 /* and delete our events. */
154 eventDelete(hurt_expire_event
, NULL
);
155 eventDelete(hurt_check_event
, NULL
);
157 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
)
159 dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
168 /* {{{ static int mo_hurt()
170 * HURT [<expire>] <ip> <reason>
172 * parv[1] - expire or ip
173 * parv[2] - ip or reason
174 * parv[3] - reason or NULL
177 mo_hurt(struct Client
*client_p
, struct Client
*source_p
,
178 int parc
, const char **parv
)
180 const char *ip
, *expire
, *reason
;
183 struct Client
*target_p
;
185 if (!IsOperK(source_p
)) {
186 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
187 source_p
->name
, "kline");
192 expire
= NULL
, ip
= parv
[1], reason
= parv
[2];
194 expire
= parv
[1], ip
= parv
[2], reason
= parv
[3];
197 expire_time
= HURT_DEFAULT_EXPIRE
;
198 if (expire
&& (expire_time
= valid_temp_time(expire
)) < 1) {
199 sendto_one_notice(source_p
, ":Permanent HURTs are not supported");
202 if (EmptyString(reason
)) {
203 sendto_one_notice(source_p
, ":Empty HURT reasons are bad for business");
207 /* Is this a client? */
208 if (strchr(ip
, '.') == NULL
&& strchr(ip
, ':') == NULL
)
210 target_p
= find_named_person(ip
);
211 if (target_p
== NULL
)
213 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
214 form_str(ERR_NOSUCHNICK
), ip
);
217 ip
= target_p
->orighost
;
221 if (!strncmp(ip
, "*@", 2))
223 if (strchr(ip
, '!') || strchr(ip
, '@'))
225 sendto_one_notice(source_p
, ":Invalid HURT mask [%s]",
231 if (hurt_find(ip
) != NULL
) {
232 sendto_one(source_p
, ":[%s] already HURT", ip
);
237 * okay, we've got this far, now it's time to add the the HURT locally
238 * and propagate it to other servers on the network.
240 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
241 "%s added HURT on [%s] for %ld minutes with reason [%s]",
242 get_oper_name(source_p
), ip
, (long) expire_time
/ 60, reason
);
244 hurt
= hurt_new(expire_time
, ip
, reason
);
246 hurt_propagate(NULL
, source_p
, hurt
);
252 /* {{{ static int me_hurt()
254 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
261 me_hurt(struct Client
*client_p
, struct Client
*source_p
,
262 int parc
, const char **parv
)
268 * right... if we don't get enough arguments, or if we get any invalid
269 * arguments, just ignore this request - shit happens, and it's not worth
270 * dropping a server over.
272 if (parc
< 4 || !IsPerson(source_p
))
274 if ((expire_time
= atoi(parv
[1])) < 1)
276 if (hurt_find(parv
[2]) != NULL
)
278 if (EmptyString(parv
[3]))
281 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
282 "%s added HURT on [%s] for %ld minutes with reason [%s]",
283 get_oper_name(source_p
), parv
[2], (long) expire_time
/ 60, parv
[3]);
284 hurt
= hurt_new(expire_time
, parv
[2], parv
[3]);
291 /* {{{ static int mo_heal()
295 * parv[1] - nick or ip
298 mo_heal(struct Client
*client_p
, struct Client
*source_p
,
299 int parc
, const char **parv
)
301 struct Client
*target_p
;
303 if (!IsOperUnkline(source_p
))
305 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
306 me
.name
, source_p
->name
, "unkline");
310 if (nick_is_valid(parv
[1]))
312 target_p
= find_named_person(parv
[1]);
313 if (target_p
== NULL
)
315 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
316 form_str(ERR_NOSUCHNICK
), parv
[1]);
319 if (MyConnect(target_p
))
320 heal_nick(source_p
, target_p
);
322 sendto_one(target_p
, ":%s ENCAP %s HEAL %s",
323 get_id(source_p
, target_p
),
324 target_p
->servptr
->name
,
325 get_id(target_p
, target_p
));
327 else if (strchr(parv
[1], '.'))
329 if (hurt_find_exact(parv
[1]) == NULL
)
331 sendto_one_notice(source_p
, ":Mask [%s] is not HURT", parv
[1]);
334 hurt_remove(parv
[1]);
335 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
336 get_oper_name(source_p
), parv
[1]);
337 sendto_server(NULL
, NULL
, NOCAPS
, NOCAPS
, ":%s ENCAP * HEAL %s",
338 source_p
->name
, parv
[1]);
342 sendto_one(source_p
, ":[%s] is not a valid IP address/nick", parv
[1]);
351 me_heal(struct Client
*client_p
, struct Client
*source_p
,
352 int parc
, const char **parv
)
354 struct Client
*target_p
;
356 /* as noted in me_hurt(), if we don't get sufficient arguments...
357 * *poof*, it's dropped...
362 if (nick_is_valid(parv
[1]))
364 target_p
= find_person(parv
[1]);
365 if (target_p
!= NULL
&& MyConnect(target_p
))
366 heal_nick(source_p
, target_p
);
368 else if (strchr(parv
[1], '.')) /* host or mask to remove ban for */
370 if (hurt_find_exact(parv
[1]) == NULL
)
373 hurt_remove(parv
[1]);
374 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
375 get_oper_name(source_p
), parv
[1]);
387 /* {{{ static void hurt_check_event() */
389 hurt_check_event(void *arg
)
391 dlink_node
*ptr
, *next_ptr
;
392 struct Client
*client_p
;
394 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
) {
395 client_p
= ptr
->data
;
396 if (!EmptyString(client_p
->user
->suser
))
398 dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
399 sendto_one_notice(client_p
, ":HURT restriction removed for this session");
400 USED_TARGETS(client_p
) = 0;
401 client_p
->localClient
->target_last
= CurrentTime
; /* don't ask --nenolod */
403 else if (client_p
->localClient
->receiveM
> hurt_state
.cutoff
)
404 exit_client(NULL
, client_p
, &me
, hurt_state
.exit_reason
);
409 /* {{{ static void hurt_expire_event() */
411 hurt_expire_event(void *unused
)
413 dlink_node
*ptr
, *next_ptr
;
416 DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_confs
.head
)
418 hurt
= (hurt_t
*) ptr
->data
;
420 if (hurt
->expire
<= CurrentTime
)
422 dlinkFindDestroy(hurt
, &hurt_confs
);
433 /* {{{ static void client_exit_hook() */
435 client_exit_hook(hook_data_client_exit
*data
)
437 s_assert(data
!= NULL
);
438 s_assert(data
->target
!= NULL
);
440 dlinkFindDestroy(data
->target
, &hurt_state
.hurt_clients
);
444 /* {{{ static void new_local_user_hook() */
446 new_local_user_hook(struct Client
*source_p
)
448 if (IsAnyDead(source_p
) || !EmptyString(source_p
->user
->suser
) ||
449 IsExemptKline(source_p
))
452 if (hurt_find(source_p
->sockhost
) || hurt_find(source_p
->orighost
))
454 USED_TARGETS(source_p
) = 10;
455 source_p
->localClient
->target_last
= CurrentTime
+ 600; /* don't ask --nenolod */
456 SetTGChange(source_p
);
457 dlinkAddAlloc(source_p
, &hurt_state
.hurt_clients
);
458 sendto_one_notice(source_p
, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
463 /* {{{ static void doing_stats_hook() */
465 doing_stats_hook(hook_data_int
*hdata
)
469 struct Client
*source_p
;
472 s_assert(hdata
->client
);
474 source_p
= hdata
->client
;
475 if(hdata
->arg2
!= (int) 's')
477 if((ConfigFileEntry
.stats_k_oper_only
== 2) && !IsOper(source_p
))
479 if ((ConfigFileEntry
.stats_k_oper_only
== 1) && !IsOper(source_p
))
481 hurt
= hurt_find(source_p
->sockhost
);
484 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
485 form_str(RPL_STATSKLINE
), 's',
486 "*", hurt
->ip
, hurt
->reason
, "", "");
490 hurt
= hurt_find(source_p
->orighost
);
493 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
494 form_str(RPL_STATSKLINE
), 's',
495 "*", hurt
->ip
, hurt
->reason
, "", "");
501 DLINK_FOREACH(ptr
, hurt_confs
.head
)
503 hurt
= (hurt_t
*) ptr
->data
;
504 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
505 form_str(RPL_STATSKLINE
), 's',
506 "*", hurt
->ip
, hurt
->reason
, "", "");
511 /* {{{ static void hurt_propagate()
513 * client_p - specific server to propagate HURT to, or NULL to propagate to all
515 * source_p - source (oper who added the HURT)
516 * hurt - HURT to be propagated
519 hurt_propagate(struct Client
*client_p
, struct Client
*source_p
, hurt_t
*hurt
)
523 ":%s ENCAP %s HURT %ld %s :%s",
524 source_p
->name
, client_p
->name
,
525 (long)(hurt
->expire
- CurrentTime
),
526 hurt
->ip
, hurt
->reason
);
528 sendto_server(&me
, NULL
, NOCAPS
, NOCAPS
,
529 ":%s ENCAP * HURT %ld %s :%s",
531 (long)(hurt
->expire
- CurrentTime
),
532 hurt
->ip
, hurt
->reason
);
536 /* {{{ static hurt_t *hurt_new() */
538 hurt_new(time_t expire
, const char *ip
, const char *reason
)
542 hurt
= MyMalloc(sizeof(hurt_t
));
544 DupString(hurt
->ip
, ip
);
545 DupString(hurt
->reason
, reason
);
546 hurt
->expire
= CurrentTime
+ expire
;
552 /* {{{ static void hurt_destroy() */
554 hurt_destroy(void *hurt
)
569 hurt_add(hurt_t
*hurt
)
571 dlinkAddAlloc(hurt
, &hurt_confs
);
575 hurt_find_exact(const char *ip
)
580 DLINK_FOREACH(ptr
, hurt_confs
.head
)
582 hurt
= (hurt_t
*) ptr
->data
;
584 if (!strcasecmp(ip
, hurt
->ip
))
592 hurt_find(const char *ip
)
597 DLINK_FOREACH(ptr
, hurt_confs
.head
)
599 hurt
= (hurt_t
*) ptr
->data
;
601 if (match(hurt
->ip
, ip
))
609 hurt_remove(const char *ip
)
611 hurt_t
*hurt
= hurt_find_exact(ip
);
613 dlinkFindDestroy(hurt
, &hurt_confs
);
617 /* {{{ static int heal_nick() */
619 heal_nick(struct Client
*source_p
, struct Client
*target_p
)
621 if (dlinkFindDestroy(target_p
, &hurt_state
.hurt_clients
))
623 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s used HEAL on %s",
624 get_oper_name(source_p
), get_client_name(target_p
, HIDE_IP
));
625 sendto_one_notice(target_p
, ":HURT restriction temporarily removed by operator");
626 sendto_one_notice(source_p
, ":HURT restriction on %s temporarily removed", target_p
->name
);
627 USED_TARGETS(target_p
) = 0;
628 target_p
->localClient
->target_last
= CurrentTime
; /* 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