]> jfr.im git - solanum.git/blame - ircd/reject.c
extensions/helpops: implement DEHELPER command
[solanum.git] / ircd / reject.c
CommitLineData
54ac8b60
VY
1/*
2 * ircd-ratbox: A slightly useful ircd
3 * reject.c: reject users with prejudice
4 *
5 * Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
6 * Copyright (C) 2003-2005 ircd-ratbox development team
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
43946961 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
54ac8b60
VY
21 * USA
22 *
43946961 23 * $Id: reject.c 25119 2008-03-13 16:57:05Z androsyn $
54ac8b60
VY
24 */
25
26#include "stdinc.h"
54ac8b60
VY
27#include "client.h"
28#include "s_conf.h"
29#include "reject.h"
30#include "s_stats.h"
43946961
JT
31#include "ircd.h"
32#include "send.h"
33#include "numeric.h"
34#include "parse.h"
35#include "hostmask.h"
36#include "match.h"
54ac8b60
VY
37#include "hash.h"
38
0240b419 39static rb_patricia_tree_t *reject_tree;
43946961 40static rb_dlink_list delay_exit;
54ac8b60 41static rb_dlink_list reject_list;
43946961
JT
42static rb_dlink_list throttle_list;
43static rb_patricia_tree_t *throttle_tree;
44static void throttle_expires(void *unused);
54ac8b60 45
54ac8b60 46
43946961 47typedef struct _reject_data
54ac8b60
VY
48{
49 rb_dlink_node rnode;
50 time_t time;
51 unsigned int count;
52 uint32_t mask_hashv;
43946961
JT
53} reject_t;
54
55typedef struct _delay_data
56{
57 rb_dlink_node node;
58 rb_fde_t *F;
59} delay_t;
60
61typedef struct _throttle
62{
63 rb_dlink_node node;
64 time_t last;
65 int count;
66} throttle_t;
67
68unsigned long
69delay_exit_length(void)
70{
71 return rb_dlink_list_length(&delay_exit);
72}
54ac8b60 73
54ac8b60
VY
74static void
75reject_exit(void *unused)
76{
54ac8b60 77 rb_dlink_node *ptr, *ptr_next;
43946961
JT
78 delay_t *ddata;
79 static const char *errbuf = "ERROR :Closing Link: (*** Banned (cache))\r\n";
55abcbb2 80
54ac8b60
VY
81 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
82 {
43946961
JT
83 ddata = ptr->data;
84
55abcbb2 85 rb_write(ddata->F, errbuf, strlen(errbuf));
43946961
JT
86 rb_close(ddata->F);
87 rb_free(ddata);
54ac8b60
VY
88 }
89
43946961
JT
90 delay_exit.head = delay_exit.tail = NULL;
91 delay_exit.length = 0;
54ac8b60
VY
92}
93
94static void
95reject_expires(void *unused)
96{
97 rb_dlink_node *ptr, *next;
0240b419 98 rb_patricia_node_t *pnode;
43946961 99 reject_t *rdata;
55abcbb2 100
54ac8b60
VY
101 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
102 {
103 pnode = ptr->data;
55abcbb2 104 rdata = pnode->data;
54ac8b60
VY
105
106 if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time())
107 continue;
108
109 rb_dlinkDelete(ptr, &reject_list);
110 rb_free(rdata);
0240b419 111 rb_patricia_remove(reject_tree, pnode);
54ac8b60
VY
112 }
113}
114
115void
116init_reject(void)
117{
0240b419 118 reject_tree = rb_new_patricia(PATRICIA_BITS);
43946961 119 throttle_tree = rb_new_patricia(PATRICIA_BITS);
54ac8b60
VY
120 rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
121 rb_event_add("reject_expires", reject_expires, NULL, 60);
43946961 122 rb_event_add("throttle_expires", throttle_expires, NULL, 10);
54ac8b60
VY
123}
124
ae09cb7d
JT
125unsigned long
126throttle_size(void)
127{
16ef24cf
JT
128 unsigned long count;
129 rb_dlink_node *ptr;
130 rb_patricia_node_t *pnode;
131 throttle_t *t;
132
133 count = 0;
134 RB_DLINK_FOREACH(ptr, throttle_list.head)
135 {
136 pnode = ptr->data;
137 t = pnode->data;
138 if (t->count > ConfigFileEntry.throttle_count)
139 count++;
140 }
141
142 return count;
ae09cb7d 143}
54ac8b60
VY
144
145void
146add_reject(struct Client *client_p, const char *mask1, const char *mask2)
147{
0240b419 148 rb_patricia_node_t *pnode;
43946961 149 reject_t *rdata;
54ac8b60
VY
150 uint32_t hashv;
151
152 /* Reject is disabled */
43946961 153 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
54ac8b60
VY
154 return;
155
156 hashv = 0;
157 if (mask1 != NULL)
43946961 158 hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
54ac8b60 159 if (mask2 != NULL)
43946961 160 hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
54ac8b60 161
0240b419 162 if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
54ac8b60
VY
163 {
164 rdata = pnode->data;
165 rdata->time = rb_current_time();
166 rdata->count++;
167 }
168 else
169 {
170 int bitlen = 32;
ccda6e3f 171#ifdef RB_IPV6
43946961 172 if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
54ac8b60
VY
173 bitlen = 128;
174#endif
175 pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
43946961 176 pnode->data = rdata = rb_malloc(sizeof(reject_t));
54ac8b60
VY
177 rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
178 rdata->time = rb_current_time();
179 rdata->count = 1;
180 }
181 rdata->mask_hashv = hashv;
182}
183
184int
43946961 185check_reject(rb_fde_t *F, struct sockaddr *addr)
54ac8b60 186{
0240b419 187 rb_patricia_node_t *pnode;
43946961
JT
188 reject_t *rdata;
189 delay_t *ddata;
54ac8b60 190 /* Reject is disabled */
43946961 191 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
54ac8b60 192 return 0;
55abcbb2 193
43946961 194 pnode = rb_match_ip(reject_tree, addr);
54ac8b60
VY
195 if(pnode != NULL)
196 {
197 rdata = pnode->data;
198
199 rdata->time = rb_current_time();
43946961 200 if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
54ac8b60 201 {
43946961 202 ddata = rb_malloc(sizeof(delay_t));
47adde3d 203 ServerStats.is_rej++;
43946961
JT
204 rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL);
205 ddata->F = F;
206 rb_dlinkAdd(ddata, &ddata->node, &delay_exit);
54ac8b60
VY
207 return 1;
208 }
55abcbb2
KB
209 }
210 /* Caller does what it wants */
54ac8b60
VY
211 return 0;
212}
213
83235e9e
JT
214int
215is_reject_ip(struct sockaddr *addr)
216{
217 rb_patricia_node_t *pnode;
218 reject_t *rdata;
219 int duration;
220
221 /* Reject is disabled */
222 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
223 return 0;
55abcbb2 224
83235e9e
JT
225 pnode = rb_match_ip(reject_tree, addr);
226 if(pnode != NULL)
227 {
228 rdata = pnode->data;
229
230 if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
231 {
232 duration = rdata->time + ConfigFileEntry.reject_duration - rb_current_time();
233 return duration > 0 ? duration : 1;
234 }
55abcbb2 235 }
83235e9e
JT
236 return 0;
237}
238
55abcbb2 239void
54ac8b60
VY
240flush_reject(void)
241{
242 rb_dlink_node *ptr, *next;
0240b419 243 rb_patricia_node_t *pnode;
43946961 244 reject_t *rdata;
55abcbb2 245
54ac8b60
VY
246 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
247 {
248 pnode = ptr->data;
249 rdata = pnode->data;
250 rb_dlinkDelete(ptr, &reject_list);
251 rb_free(rdata);
0240b419 252 rb_patricia_remove(reject_tree, pnode);
54ac8b60
VY
253 }
254}
255
55abcbb2 256int
54ac8b60
VY
257remove_reject_ip(const char *ip)
258{
0240b419 259 rb_patricia_node_t *pnode;
55abcbb2 260
54ac8b60 261 /* Reject is disabled */
43946961 262 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
54ac8b60
VY
263 return -1;
264
0240b419 265 if((pnode = rb_match_string(reject_tree, ip)) != NULL)
54ac8b60 266 {
43946961 267 reject_t *rdata = pnode->data;
54ac8b60
VY
268 rb_dlinkDelete(&rdata->rnode, &reject_list);
269 rb_free(rdata);
0240b419 270 rb_patricia_remove(reject_tree, pnode);
54ac8b60
VY
271 return 1;
272 }
273 return 0;
274}
275
276int
277remove_reject_mask(const char *mask1, const char *mask2)
278{
279 rb_dlink_node *ptr, *next;
0240b419 280 rb_patricia_node_t *pnode;
43946961 281 reject_t *rdata;
54ac8b60
VY
282 uint32_t hashv;
283 int n = 0;
55abcbb2 284
54ac8b60
VY
285 hashv = 0;
286 if (mask1 != NULL)
43946961 287 hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
54ac8b60 288 if (mask2 != NULL)
43946961 289 hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
54ac8b60
VY
290 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
291 {
292 pnode = ptr->data;
293 rdata = pnode->data;
294 if (rdata->mask_hashv == hashv)
295 {
296 rb_dlinkDelete(ptr, &reject_list);
297 rb_free(rdata);
0240b419 298 rb_patricia_remove(reject_tree, pnode);
54ac8b60
VY
299 n++;
300 }
301 }
302 return n;
303}
304
54ac8b60 305int
43946961 306throttle_add(struct sockaddr *addr)
54ac8b60 307{
43946961 308 throttle_t *t;
0240b419 309 rb_patricia_node_t *pnode;
54ac8b60 310
43946961 311 if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
54ac8b60 312 {
43946961
JT
313 t = pnode->data;
314
315 if(t->count > ConfigFileEntry.throttle_count)
ae09cb7d
JT
316 {
317 ServerStats.is_thr++;
318 return 1;
319 }
43946961
JT
320 /* Stop penalizing them after they've been throttled */
321 t->last = rb_current_time();
322 t->count++;
323
324 } else {
54ac8b60 325 int bitlen = 32;
ccda6e3f 326#ifdef RB_IPV6
43946961 327 if(GET_SS_FAMILY(addr) == AF_INET6)
54ac8b60
VY
328 bitlen = 128;
329#endif
55abcbb2 330 t = rb_malloc(sizeof(throttle_t));
43946961
JT
331 t->last = rb_current_time();
332 t->count = 1;
333 pnode = make_and_lookup_ip(throttle_tree, addr, bitlen);
334 pnode->data = t;
55abcbb2
KB
335 rb_dlinkAdd(pnode, &t->node, &throttle_list);
336 }
54ac8b60
VY
337 return 0;
338}
339
83235e9e
JT
340int
341is_throttle_ip(struct sockaddr *addr)
342{
343 throttle_t *t;
344 rb_patricia_node_t *pnode;
345 int duration;
346
347 if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
348 {
349 t = pnode->data;
350 if(t->count > ConfigFileEntry.throttle_count)
351 {
352 duration = t->last + ConfigFileEntry.throttle_duration - rb_current_time();
353 return duration > 0 ? duration : 1;
354 }
355 }
356 return 0;
357}
358
55abcbb2 359void
995f300e
JT
360flush_throttle(void)
361{
362 rb_dlink_node *ptr, *next;
363 rb_patricia_node_t *pnode;
364 throttle_t *t;
55abcbb2 365
995f300e
JT
366 RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
367 {
368 pnode = ptr->data;
55abcbb2 369 t = pnode->data;
995f300e
JT
370
371 rb_dlinkDelete(ptr, &throttle_list);
372 rb_free(t);
373 rb_patricia_remove(throttle_tree, pnode);
374 }
375}
376
43946961
JT
377static void
378throttle_expires(void *unused)
54ac8b60 379{
43946961 380 rb_dlink_node *ptr, *next;
0240b419 381 rb_patricia_node_t *pnode;
43946961 382 throttle_t *t;
55abcbb2 383
43946961 384 RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
54ac8b60 385 {
43946961 386 pnode = ptr->data;
55abcbb2 387 t = pnode->data;
43946961
JT
388
389 if(t->last + ConfigFileEntry.throttle_duration > rb_current_time())
390 continue;
391
392 rb_dlinkDelete(ptr, &throttle_list);
393 rb_free(t);
394 rb_patricia_remove(throttle_tree, pnode);
54ac8b60 395 }
54ac8b60 396}
43946961 397