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