]> jfr.im git - irc/rqf/shadowircd.git/blame - src/reject.c
dlink -> rb_dlink
[irc/rqf/shadowircd.git] / src / reject.c
CommitLineData
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"
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 37
38static patricia_tree_t *reject_tree;
af81d5a0
WP
39rb_dlink_list delay_exit;
40static rb_dlink_list reject_list;
212380e3 41
54015b5f 42static patricia_tree_t *unknown_tree;
43
212380e3 44struct reject_data
45{
af81d5a0 46 rb_dlink_node rnode;
212380e3 47 time_t time;
48 unsigned int count;
35f6f850 49 uint32_t mask_hashv;
212380e3 50};
51
54015b5f 52static patricia_tree_t *unknown_tree;
53
212380e3 54static void
55reject_exit(void *unused)
56{
57 struct Client *client_p;
af81d5a0 58 rb_dlink_node *ptr, *ptr_next;
212380e3 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 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 82 close_connection(client_p);
83 SetDead(client_p);
af81d5a0 84 rb_dlinkAddAlloc(client_p, &dead_list);
212380e3 85 }
86
87 delay_exit.head = delay_exit.tail = NULL;
88 delay_exit.length = 0;
89}
90
91static void
92reject_expires(void *unused)
93{
af81d5a0 94 rb_dlink_node *ptr, *next;
212380e3 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
af81d5a0 106 rb_dlinkDelete(ptr, &reject_list);
212380e3 107 MyFree(rdata);
108 patricia_remove(reject_tree, pnode);
109 }
110}
111
112void
113init_reject(void)
114{
115 reject_tree = New_Patricia(PATRICIA_BITS);
54015b5f 116 unknown_tree = New_Patricia(PATRICIA_BITS);
212380e3 117 eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
118 eventAdd("reject_expires", reject_expires, NULL, 60);
119}
120
121
122void
35f6f850 123add_reject(struct Client *client_p, const char *mask1, const char *mask2)
212380e3 124{
125 patricia_node_t *pnode;
126 struct reject_data *rdata;
35f6f850 127 uint32_t hashv;
212380e3 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 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));
af81d5a0 154 rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
212380e3 155 rdata->time = CurrentTime;
156 rdata->count = 1;
157 }
35f6f850 158 rdata->mask_hashv = hashv;
212380e3 159}
160
161int
162check_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);
38e6acdd 182 rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
212380e3 183 SetClosing(client_p);
af81d5a0 184 rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
212380e3 185 return 1;
186 }
187 }
188 /* Caller does what it wants */
189 return 0;
190}
191
192void
193flush_reject(void)
194{
af81d5a0 195 rb_dlink_node *ptr, *next;
212380e3 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;
af81d5a0 203 rb_dlinkDelete(ptr, &reject_list);
212380e3 204 MyFree(rdata);
205 patricia_remove(reject_tree, pnode);
206 }
207}
208
209int
35f6f850 210remove_reject_ip(const char *ip)
212380e3 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;
af81d5a0 222 rb_dlinkDelete(&rdata->rnode, &reject_list);
212380e3 223 MyFree(rdata);
224 patricia_remove(reject_tree, pnode);
225 return 1;
226 }
227 return 0;
228}
229
35f6f850
JT
230int
231remove_reject_mask(const char *mask1, const char *mask2)
232{
af81d5a0 233 rb_dlink_node *ptr, *next;
35f6f850
JT
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 {
af81d5a0 250 rb_dlinkDelete(ptr, &reject_list);
35f6f850
JT
251 MyFree(rdata);
252 patricia_remove(reject_tree, pnode);
253 n++;
254 }
255 }
256 return n;
257}
258
54015b5f 259
260int
261add_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);
38e6acdd 280 rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
54015b5f 281 SetClosing(client_p);
af81d5a0 282 rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
54015b5f 283 return 1;
284 }
285
286 pnode->data = (void *)((unsigned long)pnode->data + 1);
287
288 return 0;
289}
290
291void
292del_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}