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