]>
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 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 | ||
43946961 | 39 | static rb_patricia_tree_t *global_tree; |
0240b419 | 40 | static rb_patricia_tree_t *reject_tree; |
43946961 | 41 | static rb_dlink_list delay_exit; |
54ac8b60 | 42 | static rb_dlink_list reject_list; |
43946961 JT |
43 | static rb_dlink_list throttle_list; |
44 | static rb_patricia_tree_t *throttle_tree; | |
45 | static void throttle_expires(void *unused); | |
54ac8b60 | 46 | |
54ac8b60 | 47 | |
43946961 | 48 | typedef struct _reject_data |
54ac8b60 VY |
49 | { |
50 | rb_dlink_node rnode; | |
51 | time_t time; | |
52 | unsigned int count; | |
53 | uint32_t mask_hashv; | |
43946961 JT |
54 | } reject_t; |
55 | ||
56 | typedef struct _delay_data | |
57 | { | |
58 | rb_dlink_node node; | |
59 | rb_fde_t *F; | |
60 | } delay_t; | |
61 | ||
62 | typedef struct _throttle | |
63 | { | |
64 | rb_dlink_node node; | |
65 | time_t last; | |
66 | int count; | |
67 | } throttle_t; | |
68 | ||
69 | unsigned long | |
70 | delay_exit_length(void) | |
71 | { | |
72 | return rb_dlink_list_length(&delay_exit); | |
73 | } | |
54ac8b60 | 74 | |
54ac8b60 VY |
75 | static void |
76 | reject_exit(void *unused) | |
77 | { | |
54ac8b60 | 78 | rb_dlink_node *ptr, *ptr_next; |
43946961 JT |
79 | delay_t *ddata; |
80 | static const char *errbuf = "ERROR :Closing Link: (*** Banned (cache))\r\n"; | |
81 | ||
54ac8b60 VY |
82 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head) |
83 | { | |
43946961 JT |
84 | ddata = ptr->data; |
85 | ||
86 | rb_write(ddata->F, errbuf, strlen(errbuf)); | |
87 | rb_close(ddata->F); | |
88 | rb_free(ddata); | |
54ac8b60 VY |
89 | } |
90 | ||
43946961 JT |
91 | delay_exit.head = delay_exit.tail = NULL; |
92 | delay_exit.length = 0; | |
54ac8b60 VY |
93 | } |
94 | ||
95 | static void | |
96 | reject_expires(void *unused) | |
97 | { | |
98 | rb_dlink_node *ptr, *next; | |
0240b419 | 99 | rb_patricia_node_t *pnode; |
43946961 | 100 | reject_t *rdata; |
54ac8b60 VY |
101 | |
102 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
103 | { | |
104 | pnode = ptr->data; | |
105 | rdata = pnode->data; | |
106 | ||
107 | if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time()) | |
108 | continue; | |
109 | ||
110 | rb_dlinkDelete(ptr, &reject_list); | |
111 | rb_free(rdata); | |
0240b419 | 112 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
113 | } |
114 | } | |
115 | ||
116 | void | |
117 | init_reject(void) | |
118 | { | |
0240b419 | 119 | reject_tree = rb_new_patricia(PATRICIA_BITS); |
43946961 | 120 | throttle_tree = rb_new_patricia(PATRICIA_BITS); |
54ac8b60 VY |
121 | rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME); |
122 | rb_event_add("reject_expires", reject_expires, NULL, 60); | |
43946961 | 123 | rb_event_add("throttle_expires", throttle_expires, NULL, 10); |
54ac8b60 VY |
124 | } |
125 | ||
126 | ||
127 | void | |
128 | add_reject(struct Client *client_p, const char *mask1, const char *mask2) | |
129 | { | |
0240b419 | 130 | rb_patricia_node_t *pnode; |
43946961 | 131 | reject_t *rdata; |
54ac8b60 VY |
132 | uint32_t hashv; |
133 | ||
134 | /* Reject is disabled */ | |
43946961 | 135 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0) |
54ac8b60 VY |
136 | return; |
137 | ||
138 | hashv = 0; | |
139 | if (mask1 != NULL) | |
43946961 | 140 | hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32); |
54ac8b60 | 141 | if (mask2 != NULL) |
43946961 | 142 | hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32); |
54ac8b60 | 143 | |
0240b419 | 144 | if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) |
54ac8b60 VY |
145 | { |
146 | rdata = pnode->data; | |
147 | rdata->time = rb_current_time(); | |
148 | rdata->count++; | |
149 | } | |
150 | else | |
151 | { | |
152 | int bitlen = 32; | |
ccda6e3f | 153 | #ifdef RB_IPV6 |
43946961 | 154 | if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6) |
54ac8b60 VY |
155 | bitlen = 128; |
156 | #endif | |
157 | pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); | |
43946961 | 158 | pnode->data = rdata = rb_malloc(sizeof(reject_t)); |
54ac8b60 VY |
159 | rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list); |
160 | rdata->time = rb_current_time(); | |
161 | rdata->count = 1; | |
162 | } | |
163 | rdata->mask_hashv = hashv; | |
164 | } | |
165 | ||
166 | int | |
43946961 | 167 | check_reject(rb_fde_t *F, struct sockaddr *addr) |
54ac8b60 | 168 | { |
0240b419 | 169 | rb_patricia_node_t *pnode; |
43946961 JT |
170 | reject_t *rdata; |
171 | delay_t *ddata; | |
54ac8b60 | 172 | /* Reject is disabled */ |
43946961 | 173 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0) |
54ac8b60 VY |
174 | return 0; |
175 | ||
43946961 | 176 | pnode = rb_match_ip(reject_tree, addr); |
54ac8b60 VY |
177 | if(pnode != NULL) |
178 | { | |
179 | rdata = pnode->data; | |
180 | ||
181 | rdata->time = rb_current_time(); | |
43946961 | 182 | if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count) |
54ac8b60 | 183 | { |
43946961 | 184 | ddata = rb_malloc(sizeof(delay_t)); |
47adde3d | 185 | ServerStats.is_rej++; |
43946961 JT |
186 | rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL); |
187 | ddata->F = F; | |
188 | rb_dlinkAdd(ddata, &ddata->node, &delay_exit); | |
54ac8b60 VY |
189 | return 1; |
190 | } | |
191 | } | |
192 | /* Caller does what it wants */ | |
193 | return 0; | |
194 | } | |
195 | ||
196 | void | |
197 | flush_reject(void) | |
198 | { | |
199 | rb_dlink_node *ptr, *next; | |
0240b419 | 200 | rb_patricia_node_t *pnode; |
43946961 | 201 | reject_t *rdata; |
54ac8b60 VY |
202 | |
203 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
204 | { | |
205 | pnode = ptr->data; | |
206 | rdata = pnode->data; | |
207 | rb_dlinkDelete(ptr, &reject_list); | |
208 | rb_free(rdata); | |
0240b419 | 209 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
210 | } |
211 | } | |
212 | ||
213 | int | |
214 | remove_reject_ip(const char *ip) | |
215 | { | |
0240b419 | 216 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
217 | |
218 | /* Reject is disabled */ | |
43946961 | 219 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0) |
54ac8b60 VY |
220 | return -1; |
221 | ||
0240b419 | 222 | if((pnode = rb_match_string(reject_tree, ip)) != NULL) |
54ac8b60 | 223 | { |
43946961 | 224 | reject_t *rdata = pnode->data; |
54ac8b60 VY |
225 | rb_dlinkDelete(&rdata->rnode, &reject_list); |
226 | rb_free(rdata); | |
0240b419 | 227 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
228 | return 1; |
229 | } | |
230 | return 0; | |
231 | } | |
232 | ||
233 | int | |
234 | remove_reject_mask(const char *mask1, const char *mask2) | |
235 | { | |
236 | rb_dlink_node *ptr, *next; | |
0240b419 | 237 | rb_patricia_node_t *pnode; |
43946961 | 238 | reject_t *rdata; |
54ac8b60 VY |
239 | uint32_t hashv; |
240 | int n = 0; | |
241 | ||
242 | hashv = 0; | |
243 | if (mask1 != NULL) | |
43946961 | 244 | hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32); |
54ac8b60 | 245 | if (mask2 != NULL) |
43946961 | 246 | hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32); |
54ac8b60 VY |
247 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) |
248 | { | |
249 | pnode = ptr->data; | |
250 | rdata = pnode->data; | |
251 | if (rdata->mask_hashv == hashv) | |
252 | { | |
253 | rb_dlinkDelete(ptr, &reject_list); | |
254 | rb_free(rdata); | |
0240b419 | 255 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
256 | n++; |
257 | } | |
258 | } | |
259 | return n; | |
260 | } | |
261 | ||
54ac8b60 | 262 | int |
43946961 | 263 | throttle_add(struct sockaddr *addr) |
54ac8b60 | 264 | { |
43946961 | 265 | throttle_t *t; |
0240b419 | 266 | rb_patricia_node_t *pnode; |
54ac8b60 | 267 | |
43946961 | 268 | if((pnode = rb_match_ip(throttle_tree, addr)) != NULL) |
54ac8b60 | 269 | { |
43946961 JT |
270 | t = pnode->data; |
271 | ||
272 | if(t->count > ConfigFileEntry.throttle_count) | |
273 | return 1; | |
274 | ||
275 | /* Stop penalizing them after they've been throttled */ | |
276 | t->last = rb_current_time(); | |
277 | t->count++; | |
278 | ||
279 | } else { | |
54ac8b60 | 280 | int bitlen = 32; |
ccda6e3f | 281 | #ifdef RB_IPV6 |
43946961 | 282 | if(GET_SS_FAMILY(addr) == AF_INET6) |
54ac8b60 VY |
283 | bitlen = 128; |
284 | #endif | |
43946961 JT |
285 | t = rb_malloc(sizeof(throttle_t)); |
286 | t->last = rb_current_time(); | |
287 | t->count = 1; | |
288 | pnode = make_and_lookup_ip(throttle_tree, addr, bitlen); | |
289 | pnode->data = t; | |
290 | rb_dlinkAdd(pnode, &t->node, &throttle_list); | |
291 | } | |
54ac8b60 VY |
292 | return 0; |
293 | } | |
294 | ||
43946961 JT |
295 | static void |
296 | throttle_expires(void *unused) | |
54ac8b60 | 297 | { |
43946961 | 298 | rb_dlink_node *ptr, *next; |
0240b419 | 299 | rb_patricia_node_t *pnode; |
43946961 JT |
300 | throttle_t *t; |
301 | ||
302 | RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head) | |
54ac8b60 | 303 | { |
43946961 JT |
304 | pnode = ptr->data; |
305 | t = pnode->data; | |
306 | ||
307 | if(t->last + ConfigFileEntry.throttle_duration > rb_current_time()) | |
308 | continue; | |
309 | ||
310 | rb_dlinkDelete(ptr, &throttle_list); | |
311 | rb_free(t); | |
312 | rb_patricia_remove(throttle_tree, pnode); | |
54ac8b60 | 313 | } |
54ac8b60 | 314 | } |
43946961 | 315 |