]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | * | |
8017ad2d | 23 | * $Id: reject.c 3456 2007-05-18 19:14:18Z jilles $ |
212380e3 AC |
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" | |
35f6f850 | 36 | #include "hash.h" |
212380e3 AC |
37 | |
38 | static patricia_tree_t *reject_tree; | |
39 | dlink_list delay_exit; | |
40 | static dlink_list reject_list; | |
41 | ||
54015b5f JT |
42 | static patricia_tree_t *unknown_tree; |
43 | ||
212380e3 AC |
44 | struct reject_data |
45 | { | |
46 | dlink_node rnode; | |
47 | time_t time; | |
48 | unsigned int count; | |
35f6f850 | 49 | uint32_t mask_hashv; |
212380e3 AC |
50 | }; |
51 | ||
54015b5f JT |
52 | static patricia_tree_t *unknown_tree; |
53 | ||
212380e3 AC |
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)) | |
54015b5f JT |
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 | } | |
212380e3 AC |
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); | |
54015b5f | 116 | unknown_tree = New_Patricia(PATRICIA_BITS); |
212380e3 AC |
117 | eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME); |
118 | eventAdd("reject_expires", reject_expires, NULL, 60); | |
119 | } | |
120 | ||
121 | ||
122 | void | |
35f6f850 | 123 | add_reject(struct Client *client_p, const char *mask1, const char *mask2) |
212380e3 AC |
124 | { |
125 | patricia_node_t *pnode; | |
126 | struct reject_data *rdata; | |
35f6f850 | 127 | uint32_t hashv; |
212380e3 AC |
128 | |
129 | /* Reject is disabled */ | |
130 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0) | |
131 | return; | |
132 | ||
35f6f850 JT |
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 | ||
212380e3 AC |
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 | } | |
35f6f850 | 158 | rdata->mask_hashv = hashv; |
212380e3 AC |
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); | |
c5c2f506 | 182 | comm_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); |
212380e3 AC |
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 | |
35f6f850 | 210 | remove_reject_ip(const char *ip) |
212380e3 AC |
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 | ||
35f6f850 JT |
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 | ||
54015b5f JT |
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); | |
c5c2f506 | 280 | comm_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); |
54015b5f JT |
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 | } | |
8017ad2d | 304 | /* this can happen due to m_webirc.c's manipulations, for example */ |
54015b5f | 305 | } |