shadowircd / extensions / hurt.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
 *
 * Copyright (C) 2006 charybdis development team
 * All rights reserved
 *
 * $Id: hurt.c 3161 2007-01-25 07:23:01Z nenolod $
 */
#include "stdinc.h"
#include "modules.h"
#include "hook.h"
#include "client.h"
#include "ircd.h"
#include "send.h"
#include "numeric.h"
#include "hostmask.h"
#include "s_conf.h"
#include "s_newconf.h"
#include "hash.h"

/* {{{ Structures */
#define HURT_CUTOFF             (10)            /* protocol messages. */
#define HURT_DEFAULT_EXPIRE     (7 * 24 * 60)   /* minutes. */
#define HURT_EXIT_REASON        "Hurt: Failed to identify to services"

enum {
        HEAL_NICK = 0,
        HEAL_IP
};

typedef struct _hurt_state {
        time_t start_time;
        uint32_t n_hurts;
        rb_dlink_list hurt_clients;
        uint16_t cutoff;
        time_t default_expire;
        const char *exit_reason;
} hurt_state_t;

typedef struct _hurt {
        char *ip;
        struct sockaddr *saddr;
        int saddr_bits;
        char *reason;
        time_t expire;
} hurt_t;
/* }}} */

/* {{{ Prototypes */
static int mo_hurt(struct Client *, struct Client *, int, const char **);
static int me_hurt(struct Client *, struct Client *, int, const char **);
static int mo_heal(struct Client *, struct Client *, int, const char **);
static int me_heal(struct Client *, struct Client *, int, const char **);

static int modinit(void);
static void modfini(void);

static void client_exit_hook(hook_data_client_exit *);
static void new_local_user_hook(struct Client *);
static void doing_stats_hook(hook_data_int *hdata);

static void hurt_check_event(void *);
static void hurt_expire_event(void *);

static hurt_t *hurt_new(time_t, const char *, const char *);
static void hurt_add(hurt_t *);
static void hurt_propagate(struct Client *, struct Client *, hurt_t *);
static hurt_t *hurt_find(const char *ip);
static hurt_t *hurt_find_exact(const char *ip);
static void hurt_remove(const char *ip);
static void hurt_destroy(void *hurt);

static int heal_nick(struct Client *, struct Client *);

static int nick_is_valid(const char *);
/* }}} */

/* {{{ State containers */

rb_dlink_list hurt_confs = { NULL, NULL, 0 };

/* }}} */

/* {{{ Messages */
struct Message hurt_msgtab = {
	"HURT", 0, 0, 0, MFLG_SLOW, {
		mg_ignore, mg_ignore, mg_ignore,
		mg_ignore, {me_hurt, 0}, {mo_hurt, 3}
	}
};

struct Message heal_msgtab = {
	"HEAL", 0, 0, 0, MFLG_SLOW, {
		mg_ignore, mg_ignore, mg_ignore,
		mg_ignore, {me_heal, 0}, {mo_heal, 2}
	}
};
/* }}} */

/* {{{ Misc module stuff */
mapi_hfn_list_av1 hurt_hfnlist[] = {
	{"client_exit",		(hookfn) client_exit_hook},
	{"new_local_user",	(hookfn) new_local_user_hook},
	{"doing_stats",		(hookfn) doing_stats_hook},
	{NULL, 			NULL},
};

mapi_clist_av1 hurt_clist[] = { &hurt_msgtab, &heal_msgtab, NULL };

DECLARE_MODULE_AV1(
	hurt,
	modinit,
	modfini,
	hurt_clist,
	NULL,
	hurt_hfnlist,
	"$Revision: 3161 $"
);
/* }}} */

hurt_state_t hurt_state = {
	.cutoff = HURT_CUTOFF,
	.default_expire = HURT_DEFAULT_EXPIRE,
	.exit_reason = HURT_EXIT_REASON,
};

/*
 * Module constructor/destructor.
 */

/* {{{ static int modinit() */

struct ev_entry *hurt_expire_ev = NULL;
struct ev_entry *hurt_check_ev = NULL;

static int
modinit(void)
{
	/* set-up hurt_state. */
	hurt_state.start_time = rb_current_time();

	/* add our event handlers. */
	hurt_expire_ev = rb_event_add("hurt_expire", hurt_expire_event, NULL, 60);
	hurt_check_ev = rb_event_add("hurt_check", hurt_check_event, NULL, 5);

	return 0;
}
/* }}} */

/* {{{ static void modfini() */
static void
modfini(void)
{
	rb_dlink_node	*ptr, *next_ptr;

	/* and delete our events. */
	rb_event_delete(hurt_expire_ev);
	rb_event_delete(hurt_check_ev);

	RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_state.hurt_clients.head)
	{
		rb_dlinkDestroy(ptr, &hurt_state.hurt_clients);
	}
}
/* }}} */

/*
 * Message handlers.
 */

/* {{{ static int mo_hurt()
 *
 * HURT [<expire>] <ip> <reason>
 * 
 * parv[1] - expire or ip
 * parv[2] - ip or reason
 * parv[3] - reason or NULL
 */
static int
mo_hurt(struct Client *client_p, struct Client *source_p,
		int parc, const char **parv)
{
	const char			*ip, *expire, *reason;
	int				expire_time;
	hurt_t				*hurt;
	struct Client			*target_p;

	if (!IsOperK(source_p)) {
		sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
				source_p->name, "kline");
		return 0;
	}

	if (parc == 3)
		expire = NULL, ip = parv[1], reason = parv[2];
	else
		expire = parv[1], ip = parv[2], reason = parv[3];

	if (!expire)
		expire_time = HURT_DEFAULT_EXPIRE;
	if (expire && (expire_time = valid_temp_time(expire)) < 1) {
		sendto_one_notice(source_p, ":Permanent HURTs are not supported");
		return 0;
	}
	if (EmptyString(reason)) {
		sendto_one_notice(source_p, ":Empty HURT reasons are bad for business");
		return 0;
	}

	/* Is this a client? */
	if (strchr(ip, '.') == NULL && strchr(ip, ':') == NULL)
	{
		target_p = find_named_person(ip);
		if (target_p == NULL)
		{
			sendto_one_numeric(source_p, ERR_NOSUCHNICK,
					   form_str(ERR_NOSUCHNICK), ip);
			return 0;
		}
		ip = target_p->orighost;
	}
	else
	{
		if (!strncmp(ip, "*@", 2))
			ip += 2;
		if (strchr(ip, '!') || strchr(ip, '@'))
		{
			sendto_one_notice(source_p, ":Invalid HURT mask [%s]",
					ip);
			return 0;
		}
	}

	if (hurt_find(ip) != NULL) {
		sendto_one(source_p, ":[%s] already HURT", ip);
		return 0;
	}

	/*
	 * okay, we've got this far, now it's time to add the the HURT locally
	 * and propagate it to other servers on the network.
	 */
	sendto_realops_snomask(SNO_GENERAL, L_ALL,
			"%s added HURT on [%s] for %ld minutes with reason [%s]",
			get_oper_name(source_p), ip, (long) expire_time / 60, reason);

	hurt = hurt_new(expire_time, ip, reason);
	hurt_add(hurt);
	hurt_propagate(NULL, source_p, hurt);

	return 0;
}
/* }}} */

/* {{{ static int me_hurt()
 *
 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
 *
 * parv[1] - expire
 * parv[2] - ip
 * parv[3] - reason
 */
static int
me_hurt(struct Client *client_p, struct Client *source_p,
		int parc, const char **parv)
{
	time_t				expire_time;
	hurt_t				*hurt;

	/*
	 * right... if we don't get enough arguments, or if we get any invalid
	 * arguments, just ignore this request - shit happens, and it's not worth
	 * dropping a server over.
	 */
	if (parc < 4 || !IsPerson(source_p))
		return 0;
	if ((expire_time = atoi(parv[1])) < 1)
		return 0;
	if (hurt_find(parv[2]) != NULL)
		return 0;
	if (EmptyString(parv[3]))
		return 0;

	sendto_realops_snomask(SNO_GENERAL, L_ALL,
			"%s added HURT on [%s] for %ld minutes with reason [%s]",
			get_oper_name(source_p), parv[2], (long) expire_time / 60, parv[3]);
	hurt = hurt_new(expire_time, parv[2], parv[3]);
	hurt_add(hurt);

	return 0;
}
/* }}} */

/* {{{ static int mo_heal()
 *
 * HURT <nick>|<ip>
 *
 * parv[1] - nick or ip
 */
static int
mo_heal(struct Client *client_p, struct Client *source_p,
		int parc, const char **parv)
{
	struct Client *target_p;

	if (!IsOperUnkline(source_p))
	{
		sendto_one(source_p, form_str(ERR_NOPRIVS),
				me.name, source_p->name, "unkline");
		return 0;
	}

	if (nick_is_valid(parv[1]))
	{
		target_p = find_named_person(parv[1]);
		if (target_p == NULL)
		{
			sendto_one_numeric(source_p, ERR_NOSUCHNICK,
					form_str(ERR_NOSUCHNICK), parv[1]);
			return 0;
		}
		if (MyConnect(target_p))
			heal_nick(source_p, target_p);
		else
			sendto_one(target_p, ":%s ENCAP %s HEAL %s",
					get_id(source_p, target_p),
					target_p->servptr->name,
					get_id(target_p, target_p));
	}
	else if (strchr(parv[1], '.'))
	{
		if (hurt_find_exact(parv[1]) == NULL)
		{
			sendto_one_notice(source_p, ":Mask [%s] is not HURT", parv[1]);
			return 0;
		}
		hurt_remove(parv[1]);
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s removed HURT on %s",
				get_oper_name(source_p), parv[1]);
		sendto_server(NULL, NULL, NOCAPS, NOCAPS, ":%s ENCAP * HEAL %s",
			source_p->name, parv[1]);
	}
	else
	{
		sendto_one(source_p, ":[%s] is not a valid IP address/nick", parv[1]);
		return 0;
	}

	return 0;
}
/* }}} */

static int
me_heal(struct Client *client_p, struct Client *source_p,
		int parc, const char **parv)
{
	struct Client *target_p;

	/* as noted in me_hurt(), if we don't get sufficient arguments...
	 * *poof*, it's dropped...
	 */
	if (parc < 2)
		return 0;

	if (nick_is_valid(parv[1]))
	{
		target_p = find_person(parv[1]);
		if (target_p != NULL && MyConnect(target_p))
			heal_nick(source_p, target_p);
	}
	else if (strchr(parv[1], '.'))	/* host or mask to remove ban for */
	{
		if (hurt_find_exact(parv[1]) == NULL)
			return 0;

		hurt_remove(parv[1]);
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s removed HURT on %s",
				get_oper_name(source_p), parv[1]);
	}
	else
		return 0;

	return 0;
}

/*
 * Event handlers.
 */

/* {{{ static void hurt_check_event() */
static void
hurt_check_event(void *arg)
{
	rb_dlink_node	*ptr, *next_ptr;
	struct Client	*client_p;

	RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_state.hurt_clients.head) {
		client_p = ptr->data;
		if (!EmptyString(client_p->user->suser))
		{
			rb_dlinkDestroy(ptr, &hurt_state.hurt_clients);
			sendto_one_notice(client_p, ":HURT restriction removed for this session");
			USED_TARGETS(client_p) = 0;
			client_p->localClient->target_last = rb_current_time();		/* don't ask --nenolod */
		}
		else if (client_p->localClient->receiveM > hurt_state.cutoff)
			exit_client(NULL, client_p, &me, hurt_state.exit_reason);
	}
}
/* }}} */

/* {{{ static void hurt_expire_event() */
static void
hurt_expire_event(void *unused)
{
	rb_dlink_node	*ptr, *next_ptr;
	hurt_t		*hurt;

	RB_DLINK_FOREACH_SAFE (ptr, next_ptr, hurt_confs.head)
	{
		hurt = (hurt_t *) ptr->data;

		if (hurt->expire <= rb_current_time())
		{
			rb_dlinkFindDestroy(hurt, &hurt_confs);
			hurt_destroy(hurt);
		}
	}
}
/* }}} */

/*
 * Hook functions.
 */

/* {{{ static void client_exit_hook() */
static void
client_exit_hook(hook_data_client_exit *data)
{
	s_assert(data != NULL);
	s_assert(data->target != NULL);

	rb_dlinkFindDestroy(data->target, &hurt_state.hurt_clients);
}
/* }}} */

/* {{{ static void new_local_user_hook() */
static void
new_local_user_hook(struct Client *source_p)
{
	if (IsAnyDead(source_p) || !EmptyString(source_p->user->suser) ||
			IsExemptKline(source_p))
		return;

	if (hurt_find(source_p->sockhost) || hurt_find(source_p->orighost))
	{
		USED_TARGETS(source_p) = 10;
		source_p->localClient->target_last = rb_current_time() + 600;		/* don't ask --nenolod */
		SetTGChange(source_p);
		rb_dlinkAddAlloc(source_p, &hurt_state.hurt_clients);
		sendto_one_notice(source_p, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
	}	
}
/* }}} */

/* {{{ static void doing_stats_hook() */
static void
doing_stats_hook(hook_data_int *hdata)
{
	rb_dlink_node	*ptr;
	hurt_t		*hurt;
	struct Client	*source_p;

	s_assert(hdata);
	s_assert(hdata->client);

	source_p = hdata->client;
	if(hdata->arg2 != (int) 's')
		return;
	if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper(source_p))
		return;
	if ((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper(source_p))
	{
		hurt = hurt_find(source_p->sockhost);
		if (hurt != NULL)
		{
			sendto_one_numeric(source_p, RPL_STATSKLINE,
					form_str(RPL_STATSKLINE), 's',
					"*", hurt->ip, hurt->reason, "", "");
			return;
		}

		hurt = hurt_find(source_p->orighost);
		if (hurt != NULL)
		{
			sendto_one_numeric(source_p, RPL_STATSKLINE,
					form_str(RPL_STATSKLINE), 's',
					"*", hurt->ip, hurt->reason, "", "");
			return;
		}
		return;
	}

	RB_DLINK_FOREACH(ptr, hurt_confs.head)
	{
		hurt = (hurt_t *) ptr->data;
		sendto_one_numeric(source_p, RPL_STATSKLINE,
				form_str(RPL_STATSKLINE), 's',
				"*", hurt->ip, hurt->reason, "", "");
	}
}
/* }}} */

/* {{{ static void hurt_propagate()
 *
 * client_p - specific server to propagate HURT to, or NULL to propagate to all
 *      servers.
 * source_p - source (oper who added the HURT)
 * hurt   - HURT to be propagated
 */
static void
hurt_propagate(struct Client *client_p, struct Client *source_p, hurt_t *hurt)
{
	if (client_p)
		sendto_one(client_p,
				":%s ENCAP %s HURT %ld %s :%s",
				source_p->name, client_p->name,
				(long)(hurt->expire - rb_current_time()),
				hurt->ip, hurt->reason);
	else
		sendto_server(&me, NULL, NOCAPS, NOCAPS,
				":%s ENCAP * HURT %ld %s :%s",
				source_p->name,
				(long)(hurt->expire - rb_current_time()),
				hurt->ip, hurt->reason);
}
/* }}} */

/* {{{ static hurt_t *hurt_new() */
static hurt_t *
hurt_new(time_t expire, const char *ip, const char *reason)
{
	hurt_t *hurt;

	hurt = rb_malloc(sizeof(hurt_t));

	hurt->ip = rb_strdup(ip);
	hurt->reason = rb_strdup(reason);
	hurt->expire = rb_current_time() + expire;

	return hurt;
}
/* }}} */

/* {{{ static void hurt_destroy() */
static void
hurt_destroy(void *hurt)
{
	hurt_t *h;

	if (!hurt)
		return;

	h = (hurt_t *) hurt;
	rb_free(h->ip);
	rb_free(h->reason);
	rb_free(h);
}
/* }}} */

static void
hurt_add(hurt_t *hurt)
{
	rb_dlinkAddAlloc(hurt, &hurt_confs);
}

static hurt_t *
hurt_find_exact(const char *ip)
{
	rb_dlink_node *ptr;
	hurt_t *hurt;

	RB_DLINK_FOREACH(ptr, hurt_confs.head)
	{
		hurt = (hurt_t *) ptr->data;

		if (!strcasecmp(ip, hurt->ip))
			return hurt;
	}

	return NULL;
}

static hurt_t *
hurt_find(const char *ip)
{
	rb_dlink_node *ptr;
	hurt_t *hurt;

	RB_DLINK_FOREACH(ptr, hurt_confs.head)
	{
		hurt = (hurt_t *) ptr->data;

		if (match(hurt->ip, ip))
			return hurt;
	}

	return NULL;
}

static void
hurt_remove(const char *ip)
{
	hurt_t *hurt = hurt_find_exact(ip);

	rb_dlinkFindDestroy(hurt, &hurt_confs);
	hurt_destroy(hurt);
}

/* {{{ static int heal_nick() */
static int
heal_nick(struct Client *source_p, struct Client *target_p)
{
	if (rb_dlinkFindDestroy(target_p, &hurt_state.hurt_clients))
	{
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s used HEAL on %s",
				get_oper_name(source_p), get_client_name(target_p, HIDE_IP));
		sendto_one_notice(target_p, ":HURT restriction temporarily removed by operator");
		sendto_one_notice(source_p, ":HURT restriction on %s temporarily removed", target_p->name);
		USED_TARGETS(target_p) = 0;
		target_p->localClient->target_last = rb_current_time();		/* don't ask --nenolod */
		return 1;
	}
	else
	{
		sendto_one_notice(source_p, ":%s was not hurt", target_p->name);
		return 0;
	}
}
/* }}} */

/*
 * Anything else...
 */

/* {{{ static int nick_is_valid() */
static int
nick_is_valid(const char *nick)
{
	const char *s = nick;

	for (; *s != '\0'; s++) {
		if (!IsNickChar(*s))
			return 0;
	}

	return 1;
}
/* }}} */

/*
 * vim: ts=8 sw=8 noet fdm=marker tw=80
 */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.