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