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"
24 #define HURT_CUTOFF (10) /* protocol messages. */
25 #define HURT_DEFAULT_EXPIRE (7 * 24 * 60) /* minutes. */
26 #define HURT_EXIT_REASON "Hurt: Failed to identify to services"
33 typedef struct _hurt_state
{
36 rb_dlink_list hurt_clients
;
38 time_t default_expire
;
39 const char *exit_reason
;
42 typedef struct _hurt
{
44 struct sockaddr
*saddr
;
52 static int mo_hurt(struct MsgBuf
*msgbuf_p
, struct Client
*, struct Client
*, int, const char **);
53 static int me_hurt(struct MsgBuf
*msgbuf_p
, struct Client
*, struct Client
*, int, const char **);
54 static int mo_heal(struct MsgBuf
*msgbuf_p
, struct Client
*, struct Client
*, int, const char **);
55 static int me_heal(struct MsgBuf
*msgbuf_p
, struct Client
*, struct Client
*, int, const char **);
57 static int modinit(void);
58 static void modfini(void);
60 static void client_exit_hook(hook_data_client_exit
*);
61 static void new_local_user_hook(struct Client
*);
62 static void doing_stats_hook(hook_data_int
*hdata
);
64 static void hurt_check_event(void *);
65 static void hurt_expire_event(void *);
67 static hurt_t
*hurt_new(time_t, const char *, const char *);
68 static void hurt_add(hurt_t
*);
69 static void hurt_propagate(struct Client
*, struct Client
*, hurt_t
*);
70 static hurt_t
*hurt_find(const char *ip
);
71 static hurt_t
*hurt_find_exact(const char *ip
);
72 static void hurt_remove(const char *ip
);
73 static void hurt_destroy(void *hurt
);
75 static int heal_nick(struct Client
*, struct Client
*);
79 /* {{{ State containers */
81 rb_dlink_list hurt_confs
= { NULL
, NULL
, 0 };
86 struct Message hurt_msgtab
= {
88 mg_ignore
, mg_ignore
, mg_ignore
,
89 mg_ignore
, {me_hurt
, 0}, {mo_hurt
, 3}
93 struct Message heal_msgtab
= {
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
= rb_current_time();
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 rb_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 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
)
163 rb_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 MsgBuf
*msgbuf_p
, 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 MsgBuf
*msgbuf_p
, 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 MsgBuf
*msgbuf_p
, 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 (clean_nick(parv
[1], 0))
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 MsgBuf
*msgbuf_p
, 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 (clean_nick(parv
[1], 0))
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 rb_dlink_node
*ptr
, *next_ptr
;
396 struct Client
*client_p
;
398 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
) {
399 client_p
= ptr
->data
;
400 if (!EmptyString(client_p
->user
->suser
))
402 rb_dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
403 sendto_one_notice(client_p
, ":HURT restriction removed for this session");
404 client_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
406 else if (client_p
->localClient
->receiveM
> hurt_state
.cutoff
)
407 exit_client(NULL
, client_p
, &me
, hurt_state
.exit_reason
);
412 /* {{{ static void hurt_expire_event() */
414 hurt_expire_event(void *unused
)
416 rb_dlink_node
*ptr
, *next_ptr
;
419 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_confs
.head
)
421 hurt
= (hurt_t
*) ptr
->data
;
423 if (hurt
->expire
<= rb_current_time())
425 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
436 /* {{{ static void client_exit_hook() */
438 client_exit_hook(hook_data_client_exit
*data
)
440 s_assert(data
!= NULL
);
441 s_assert(data
->target
!= NULL
);
443 rb_dlinkFindDestroy(data
->target
, &hurt_state
.hurt_clients
);
447 /* {{{ static void new_local_user_hook() */
449 new_local_user_hook(struct Client
*source_p
)
451 if (IsAnyDead(source_p
) || !EmptyString(source_p
->user
->suser
) ||
452 IsExemptKline(source_p
))
455 if (hurt_find(source_p
->sockhost
) || hurt_find(source_p
->orighost
))
457 source_p
->localClient
->target_last
= rb_current_time() + 600; /* don't ask --nenolod */
458 SetTGChange(source_p
);
459 rb_dlinkAddAlloc(source_p
, &hurt_state
.hurt_clients
);
460 sendto_one_notice(source_p
, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
465 /* {{{ static void doing_stats_hook() */
467 doing_stats_hook(hook_data_int
*hdata
)
471 struct Client
*source_p
;
474 s_assert(hdata
->client
);
476 source_p
= hdata
->client
;
477 if(hdata
->arg2
!= (int) 's')
479 if((ConfigFileEntry
.stats_k_oper_only
== 2) && !IsOper(source_p
))
481 if ((ConfigFileEntry
.stats_k_oper_only
== 1) && !IsOper(source_p
))
483 hurt
= hurt_find(source_p
->sockhost
);
486 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
487 form_str(RPL_STATSKLINE
), 's',
488 "*", hurt
->ip
, hurt
->reason
, "", "");
492 hurt
= hurt_find(source_p
->orighost
);
495 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
496 form_str(RPL_STATSKLINE
), 's',
497 "*", hurt
->ip
, hurt
->reason
, "", "");
503 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
505 hurt
= (hurt_t
*) ptr
->data
;
506 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
507 form_str(RPL_STATSKLINE
), 's',
508 "*", hurt
->ip
, hurt
->reason
, "", "");
513 /* {{{ static void hurt_propagate()
515 * client_p - specific server to propagate HURT to, or NULL to propagate to all
517 * source_p - source (oper who added the HURT)
518 * hurt - HURT to be propagated
521 hurt_propagate(struct Client
*client_p
, struct Client
*source_p
, hurt_t
*hurt
)
525 ":%s ENCAP %s HURT %ld %s :%s",
526 source_p
->name
, client_p
->name
,
527 (long)(hurt
->expire
- rb_current_time()),
528 hurt
->ip
, hurt
->reason
);
530 sendto_server(&me
, NULL
, NOCAPS
, NOCAPS
,
531 ":%s ENCAP * HURT %ld %s :%s",
533 (long)(hurt
->expire
- rb_current_time()),
534 hurt
->ip
, hurt
->reason
);
538 /* {{{ static hurt_t *hurt_new() */
540 hurt_new(time_t expire
, const char *ip
, const char *reason
)
544 hurt
= rb_malloc(sizeof(hurt_t
));
546 hurt
->ip
= rb_strdup(ip
);
547 hurt
->reason
= rb_strdup(reason
);
548 hurt
->expire
= rb_current_time() + expire
;
554 /* {{{ static void hurt_destroy() */
556 hurt_destroy(void *hurt
)
571 hurt_add(hurt_t
*hurt
)
573 rb_dlinkAddAlloc(hurt
, &hurt_confs
);
577 hurt_find_exact(const char *ip
)
582 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
584 hurt
= (hurt_t
*) ptr
->data
;
586 if (!strcasecmp(ip
, hurt
->ip
))
594 hurt_find(const char *ip
)
599 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
601 hurt
= (hurt_t
*) ptr
->data
;
603 if (match(hurt
->ip
, ip
))
611 hurt_remove(const char *ip
)
613 hurt_t
*hurt
= hurt_find_exact(ip
);
615 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
619 /* {{{ static int heal_nick() */
621 heal_nick(struct Client
*source_p
, struct Client
*target_p
)
623 if (rb_dlinkFindDestroy(target_p
, &hurt_state
.hurt_clients
))
625 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s used HEAL on %s",
626 get_oper_name(source_p
), get_client_name(target_p
, HIDE_IP
));
627 sendto_one_notice(target_p
, ":HURT restriction temporarily removed by operator");
628 sendto_one_notice(source_p
, ":HURT restriction on %s temporarily removed", target_p
->name
);
629 target_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
634 sendto_one_notice(source_p
, ":%s was not hurt", target_p
->name
);
641 * vim: ts=8 sw=8 noet fdm=marker tw=80