This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kline.c
342 lines (286 loc) · 9.07 KB
/
kline.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
/*
* syn: a utility bot to manage IRC network access
* Copyright (C) 2009-2016 Stephen Bennett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "atheme.h"
#include "pmodule.h"
#include "syn.h"
mowgli_patricia_t *ircd_klines;
mowgli_list_t ircd_wildcard_klines;
mowgli_heap_t *ircd_kline_heap;
mowgli_eventloop_timer_t *expire_timer = 0;
static void syn_m_kline(sourceinfo_t *si, int parc, char **parv);
static void syn_m_ban(sourceinfo_t *si, int parc, char **parv);
static void syn_m_unkline(sourceinfo_t *si, int parc, char **parv);
static void expire_klines(void *unused);
char *kline_kill_reason = 0;
static void mod_init(module_t *m)
{
use_syn_main_symbols(m);
ircd_klines = mowgli_patricia_create(noopcanon);
ircd_kline_heap = mowgli_heap_create(sizeof(kline_t), 512, BH_NOW);
hook_add_event("syn_kline_added");
add_dupstr_conf_item("KLINE_KILL_REASON", &syn->conf_table, 0, &kline_kill_reason, "Banned");
pcommand_add("KLINE", syn_m_kline, 5, MSRC_USER);
pcommand_add("BAN", syn_m_ban, 8, MSRC_USER | MSRC_SERVER);
pcommand_add("UNKLINE", syn_m_unkline, 3, MSRC_USER);
expire_timer = mowgli_timer_add(base_eventloop, "expire_ircd_klines", expire_klines, NULL, 120);
}
static void mod_deinit(module_unload_intent_t intent)
{
pcommand_delete("KLINE");
pcommand_delete("BAN");
pcommand_delete("UNKLINE");
mowgli_timer_destroy(base_eventloop, expire_timer);
}
kline_t* _syn_find_kline(const char *user, const char *host)
{
kline_t *k;
if ((k = mowgli_patricia_retrieve(ircd_klines, host)))
return k;
mowgli_node_t *n;
MOWGLI_LIST_FOREACH(n, ircd_wildcard_klines.head)
{
k = n->data;
if (0 == match(k->host, host))
return k;
if (0 == match_ips(k->host, host))
return k;
}
return NULL;
}
static void syn_add_kline(const char *setter, const char *user, const char *host, int duration, const char *reason)
{
if (_syn_find_kline(user, host))
{
syn_debug(3, "Duplicate K:line %s@%s", user, host);
return;
}
if (host[0] == '*' && host[1] == '\0')
{
wallops("%s is an idiot. Dropping *@* kline.", setter);
return;
}
kline_t *k = mowgli_heap_alloc(ircd_kline_heap);
k->duration = duration;
k->settime = CURRTIME;
k->expires = CURRTIME + k->duration;
k->user = sstrdup(user);
k->host = sstrdup(host);
k->reason = sstrdup(reason);
char *p;
if (NULL != (p = strchr(k->reason, '|')))
{
*p = '\0';
}
if (strchr(k->host, '*') || strchr(k->host, '?') || (strchr(k->host, '/') && valid_ip_or_mask(k->host)))
{
mowgli_node_t *n = mowgli_node_create();
mowgli_node_add(k, n, &ircd_wildcard_klines);
}
else
{
mowgli_patricia_add(ircd_klines, k->host, k);
}
hook_call_event("syn_kline_added", k);
syn_debug(1, "Added K:line %s@%s (%s)", k->user, k->host, k->reason);
}
static void syn_remove_kline(const char *user, const char *host)
{
mowgli_node_t *n, *tn;
kline_t *k;
kline_t *removed = mowgli_patricia_delete(ircd_klines, host);
if (removed)
{
syn_debug(1, "Removing K:line on %s@%s", removed->user, removed->host);
free(removed->user);
free(removed->host);
free(removed->reason);
mowgli_heap_free(ircd_kline_heap, removed);
return;
}
MOWGLI_LIST_FOREACH_SAFE(n, tn, ircd_wildcard_klines.head)
{
k = (kline_t*) n->data;
if (0 == strcasecmp(k->user, user) &&
0 == strcasecmp(k->host, host))
{
syn_debug(1, "Removing K:line on %s@%s", k->user, k->host);
mowgli_node_delete(n, &ircd_wildcard_klines);
free(k->user);
free(k->host);
free(k->reason);
mowgli_heap_free(ircd_kline_heap, k);
}
}
}
static void expire_klines(void *unused)
{
mowgli_node_t *n, *tn;
kline_t *k;
MOWGLI_LIST_FOREACH_SAFE(n, tn, ircd_wildcard_klines.head)
{
k = (kline_t*) n->data;
if (k->duration == 0)
continue;
if (k->expires <= CURRTIME)
{
syn_debug(1, "Expiring K:line on %s@%s", k->user, k->host);
mowgli_node_delete(n, &ircd_wildcard_klines);
free(k->user);
free(k->host);
free(k->reason);
mowgli_heap_free(ircd_kline_heap, k);
}
}
mowgli_patricia_iteration_state_t state;
MOWGLI_PATRICIA_FOREACH(k, &state, ircd_klines)
{
if (k->duration == 0)
continue;
if (k->expires <= CURRTIME)
{
syn_debug(1, "Expiring K:line on %s@%s", k->user, k->host);
mowgli_patricia_delete(ircd_klines, k->host);
free(k->user);
free(k->host);
free(k->reason);
mowgli_heap_free(ircd_kline_heap, k);
}
}
}
static void _syn_vkline(const char *host, int duration, const char *reason, va_list ap)
{
/* if (_syn_find_kline("*", host))
return;
*/
char buf[BUFSIZE];
vsnprintf(buf, BUFSIZE, reason, ap);
kline_t *k = mowgli_heap_alloc(ircd_kline_heap);
k->duration = duration;
k->settime = CURRTIME;
k->expires = CURRTIME + duration;
k->user = sstrdup("*");
k->host = sstrdup(host);
k->reason = sstrdup(buf);
char *p;
if (NULL != (p = strchr(k->reason, '|')))
{
*p = '\0';
}
if (strchr(k->host, '*') || strchr(k->host, '?') || (strchr(k->host, '/') && valid_ip_or_mask(k->host)))
{
mowgli_node_t *n = mowgli_node_create();
mowgli_node_add(k, n, &ircd_wildcard_klines);
}
else
{
mowgli_patricia_add(ircd_klines, k->host, k);
}
kline_sts("*", "*", k->host, k->duration, k->reason);
hook_call_event("syn_kline_added", k);
syn_debug(1, "Added K:line %s@%s (%s)", k->user, k->host, k->reason);
}
void _syn_kline(const char *host, int duration, const char *reason, ...)
{
va_list ap;
va_start(ap, reason);
_syn_vkline(host, duration, reason, ap);
va_end(ap);
}
static void _syn_vkill(user_t *victim, const char *reason, va_list ap)
{
char buf[BUFSIZE];
vsnprintf(buf, BUFSIZE, reason, ap);
notice(syn->nick, victim->nick, "%s", buf);
kill_user(syn->me, victim, "%s", kline_kill_reason);
}
void _syn_kill(user_t *victim, const char *reason, ...)
{
va_list ap;
va_start(ap, reason);
_syn_vkill(victim, reason, ap);
va_end(ap);
}
static void _syn_vkill2(user_t *victim, const char *killreason, const char *reason, va_list ap)
{
char buf[BUFSIZE];
vsnprintf(buf, BUFSIZE, reason, ap);
notice(syn->nick, victim->nick, "%s", buf);
kill_user(syn->me, victim, "%s", killreason);
}
void _syn_kill2(user_t *victim, const char *killreason, const char *reason, ...)
{
va_list ap;
va_start(ap, reason);
_syn_vkill2(victim, killreason, reason, ap);
va_end(ap);
}
void _syn_kill_or_kline(user_t *victim, int duration, const char *reason, ...)
{
va_list ap;
va_start(ap, reason);
if (!victim->ip || victim->ip[0] == '\0')
{
// No IP means an auth spoofed user, probably a gateway. Kill it instead.
// Don't give away the oper reason, though.
char user_reason[BUFSIZE];
strncpy(user_reason, reason, sizeof(user_reason));
char *pipe;
if (0 != (pipe = strchr(user_reason, '|')))
*pipe = '\0';
_syn_vkill(victim, user_reason, ap);
}
else
{
_syn_vkline(victim->ip, duration, reason, ap);
}
va_end(ap);
}
static void syn_m_kline(sourceinfo_t *si, int parc, char **parv)
{
syn_add_kline(si->su->nick, parv[2], parv[3], atoi(parv[1]), parv[4]);
}
static void syn_m_unkline(sourceinfo_t *si, int parc, char **parv)
{
syn_remove_kline(parv[1], parv[2]);
}
static void syn_m_ban(sourceinfo_t *si, int parc, char **parv)
{
if (parv[0][0] != 'K')
{
// Not a K:line; ignore
return;
}
const char *user = parv[1], *host = parv[2], *setter = parv[6], *reason = parv[7];
int duration = atoi(parv[4]); // creation = atoi(parv[3]), lifetime = atoi(parv[5]);
if (setter[0] == '*' && si->su)
setter = si->su->nick;
if (duration != 0)
{
syn_add_kline(setter, user, host, duration, reason);
}
else
{
syn_remove_kline(user, host);
}
}
DECLARE_MODULE_V1
(
"syn/kline", false, mod_init, mod_deinit,
"$Revision$",
"Stephen Bennett <stephen -at- freenode.net>"
);