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