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