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