]>
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 | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 | * USA | |
22 | * | |
23 | * $Id: reject.c 3456 2007-05-18 19:14:18Z jilles $ | |
24 | */ | |
25 | ||
26 | #include "stdinc.h" | |
27 | #include "config.h" | |
54ac8b60 VY |
28 | #include "client.h" |
29 | #include "s_conf.h" | |
30 | #include "reject.h" | |
31 | #include "s_stats.h" | |
32 | #include "msg.h" | |
33 | #include "hash.h" | |
34 | ||
0240b419 | 35 | static rb_patricia_tree_t *reject_tree; |
54ac8b60 VY |
36 | rb_dlink_list delay_exit; |
37 | static rb_dlink_list reject_list; | |
38 | ||
0240b419 | 39 | static rb_patricia_tree_t *unknown_tree; |
54ac8b60 VY |
40 | |
41 | struct reject_data | |
42 | { | |
43 | rb_dlink_node rnode; | |
44 | time_t time; | |
45 | unsigned int count; | |
46 | uint32_t mask_hashv; | |
47 | }; | |
48 | ||
0240b419 | 49 | static rb_patricia_tree_t *unknown_tree; |
54ac8b60 VY |
50 | |
51 | static void | |
52 | reject_exit(void *unused) | |
53 | { | |
54 | struct Client *client_p; | |
55 | rb_dlink_node *ptr, *ptr_next; | |
56 | ||
57 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head) | |
58 | { | |
59 | client_p = ptr->data; | |
60 | if(IsDead(client_p)) | |
61 | continue; | |
62 | ||
63 | /* this MUST be here, to prevent the possibility | |
64 | * sendto_one() generates a write error, and then a client | |
65 | * ends up on the dead_list and the abort_list --fl | |
66 | * | |
67 | * new disconnect notice stolen from ircu --nenolod | |
68 | * no, this only happens when someone's IP has some | |
69 | * ban on it and rejects them rather longer than the | |
70 | * ircu message suggests --jilles | |
71 | */ | |
72 | if(!IsIOError(client_p)) | |
73 | { | |
74 | if(IsExUnknown(client_p)) | |
75 | sendto_one(client_p, "ERROR :Closing Link: %s (*** Too many unknown connections)", client_p->host); | |
76 | else | |
77 | sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host); | |
78 | } | |
79 | close_connection(client_p); | |
80 | SetDead(client_p); | |
81 | rb_dlinkAddAlloc(client_p, &dead_list); | |
82 | } | |
83 | ||
84 | delay_exit.head = delay_exit.tail = NULL; | |
85 | delay_exit.length = 0; | |
86 | } | |
87 | ||
88 | static void | |
89 | reject_expires(void *unused) | |
90 | { | |
91 | rb_dlink_node *ptr, *next; | |
0240b419 | 92 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
93 | struct reject_data *rdata; |
94 | ||
95 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
96 | { | |
97 | pnode = ptr->data; | |
98 | rdata = pnode->data; | |
99 | ||
100 | if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time()) | |
101 | continue; | |
102 | ||
103 | rb_dlinkDelete(ptr, &reject_list); | |
104 | rb_free(rdata); | |
0240b419 | 105 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
106 | } |
107 | } | |
108 | ||
109 | void | |
110 | init_reject(void) | |
111 | { | |
0240b419 AC |
112 | reject_tree = rb_new_patricia(PATRICIA_BITS); |
113 | unknown_tree = rb_new_patricia(PATRICIA_BITS); | |
54ac8b60 VY |
114 | rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME); |
115 | rb_event_add("reject_expires", reject_expires, NULL, 60); | |
116 | } | |
117 | ||
118 | ||
119 | void | |
120 | add_reject(struct Client *client_p, const char *mask1, const char *mask2) | |
121 | { | |
0240b419 | 122 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
123 | struct reject_data *rdata; |
124 | uint32_t hashv; | |
125 | ||
126 | /* Reject is disabled */ | |
127 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0) | |
128 | return; | |
129 | ||
130 | hashv = 0; | |
131 | if (mask1 != NULL) | |
132 | hashv ^= fnv_hash_upper(mask1, 32); | |
133 | if (mask2 != NULL) | |
134 | hashv ^= fnv_hash_upper(mask2, 32); | |
135 | ||
0240b419 | 136 | if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) |
54ac8b60 VY |
137 | { |
138 | rdata = pnode->data; | |
139 | rdata->time = rb_current_time(); | |
140 | rdata->count++; | |
141 | } | |
142 | else | |
143 | { | |
144 | int bitlen = 32; | |
ccda6e3f | 145 | #ifdef RB_IPV6 |
54ac8b60 VY |
146 | if(client_p->localClient->ip.ss_family == AF_INET6) |
147 | bitlen = 128; | |
148 | #endif | |
149 | pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); | |
150 | pnode->data = rdata = rb_malloc(sizeof(struct reject_data)); | |
151 | rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list); | |
152 | rdata->time = rb_current_time(); | |
153 | rdata->count = 1; | |
154 | } | |
155 | rdata->mask_hashv = hashv; | |
156 | } | |
157 | ||
158 | int | |
159 | check_reject(struct Client *client_p) | |
160 | { | |
0240b419 | 161 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
162 | struct reject_data *rdata; |
163 | ||
164 | /* Reject is disabled */ | |
165 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 || | |
166 | ConfigFileEntry.reject_duration == 0) | |
167 | return 0; | |
168 | ||
0240b419 | 169 | pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip); |
54ac8b60 VY |
170 | if(pnode != NULL) |
171 | { | |
172 | rdata = pnode->data; | |
173 | ||
174 | rdata->time = rb_current_time(); | |
175 | if(rdata->count > ConfigFileEntry.reject_after_count) | |
176 | { | |
47adde3d | 177 | ServerStats.is_rej++; |
54ac8b60 | 178 | SetReject(client_p); |
0240b419 | 179 | rb_setselect(client_p->localClient->F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL); |
54ac8b60 VY |
180 | SetClosing(client_p); |
181 | rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); | |
182 | return 1; | |
183 | } | |
184 | } | |
185 | /* Caller does what it wants */ | |
186 | return 0; | |
187 | } | |
188 | ||
189 | void | |
190 | flush_reject(void) | |
191 | { | |
192 | rb_dlink_node *ptr, *next; | |
0240b419 | 193 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
194 | struct reject_data *rdata; |
195 | ||
196 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
197 | { | |
198 | pnode = ptr->data; | |
199 | rdata = pnode->data; | |
200 | rb_dlinkDelete(ptr, &reject_list); | |
201 | rb_free(rdata); | |
0240b419 | 202 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
203 | } |
204 | } | |
205 | ||
206 | int | |
207 | remove_reject_ip(const char *ip) | |
208 | { | |
0240b419 | 209 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
210 | |
211 | /* Reject is disabled */ | |
212 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 || | |
213 | ConfigFileEntry.reject_duration == 0) | |
214 | return -1; | |
215 | ||
0240b419 | 216 | if((pnode = rb_match_string(reject_tree, ip)) != NULL) |
54ac8b60 VY |
217 | { |
218 | struct reject_data *rdata = pnode->data; | |
219 | rb_dlinkDelete(&rdata->rnode, &reject_list); | |
220 | rb_free(rdata); | |
0240b419 | 221 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
222 | return 1; |
223 | } | |
224 | return 0; | |
225 | } | |
226 | ||
227 | int | |
228 | remove_reject_mask(const char *mask1, const char *mask2) | |
229 | { | |
230 | rb_dlink_node *ptr, *next; | |
0240b419 | 231 | rb_patricia_node_t *pnode; |
54ac8b60 VY |
232 | struct reject_data *rdata; |
233 | uint32_t hashv; | |
234 | int n = 0; | |
235 | ||
236 | hashv = 0; | |
237 | if (mask1 != NULL) | |
238 | hashv ^= fnv_hash_upper(mask1, 32); | |
239 | if (mask2 != NULL) | |
240 | hashv ^= fnv_hash_upper(mask2, 32); | |
241 | RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
242 | { | |
243 | pnode = ptr->data; | |
244 | rdata = pnode->data; | |
245 | if (rdata->mask_hashv == hashv) | |
246 | { | |
247 | rb_dlinkDelete(ptr, &reject_list); | |
248 | rb_free(rdata); | |
0240b419 | 249 | rb_patricia_remove(reject_tree, pnode); |
54ac8b60 VY |
250 | n++; |
251 | } | |
252 | } | |
253 | return n; | |
254 | } | |
255 | ||
256 | ||
257 | int | |
258 | add_unknown_ip(struct Client *client_p) | |
259 | { | |
0240b419 | 260 | rb_patricia_node_t *pnode; |
54ac8b60 | 261 | |
0240b419 | 262 | if((pnode = rb_match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) == NULL) |
54ac8b60 VY |
263 | { |
264 | int bitlen = 32; | |
ccda6e3f | 265 | #ifdef RB_IPV6 |
54ac8b60 VY |
266 | if(client_p->localClient->ip.ss_family == AF_INET6) |
267 | bitlen = 128; | |
268 | #endif | |
269 | pnode = make_and_lookup_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); | |
270 | pnode->data = (void *)0; | |
271 | } | |
272 | ||
273 | if((unsigned long)pnode->data >= ConfigFileEntry.max_unknown_ip) | |
274 | { | |
275 | SetExUnknown(client_p); | |
276 | SetReject(client_p); | |
0240b419 | 277 | rb_setselect(client_p->localClient->F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL); |
54ac8b60 VY |
278 | SetClosing(client_p); |
279 | rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); | |
280 | return 1; | |
281 | } | |
282 | ||
283 | pnode->data = (void *)((unsigned long)pnode->data + 1); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | void | |
289 | del_unknown_ip(struct Client *client_p) | |
290 | { | |
0240b419 | 291 | rb_patricia_node_t *pnode; |
54ac8b60 | 292 | |
0240b419 | 293 | if((pnode = rb_match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) |
54ac8b60 VY |
294 | { |
295 | pnode->data = (void *)((unsigned long)pnode->data - 1); | |
296 | if((unsigned long)pnode->data <= 0) | |
297 | { | |
0240b419 | 298 | rb_patricia_remove(unknown_tree, pnode); |
54ac8b60 VY |
299 | } |
300 | } | |
301 | /* this can happen due to m_webirc.c's manipulations, for example */ | |
302 | } |