]> jfr.im git - irc/rqf/shadowircd.git/blob - src/reject.c
Merging s_newconf.c from nenolod and me
[irc/rqf/shadowircd.git] / src / reject.c
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 "reject.h"
32 #include "s_stats.h"
33 #include "msg.h"
34 #include "hash.h"
35
36 static patricia_tree_t *reject_tree;
37 rb_dlink_list delay_exit;
38 static rb_dlink_list reject_list;
39
40 static patricia_tree_t *unknown_tree;
41
42 struct reject_data
43 {
44 rb_dlink_node rnode;
45 time_t time;
46 unsigned int count;
47 uint32_t mask_hashv;
48 };
49
50 static patricia_tree_t *unknown_tree;
51
52 static void
53 reject_exit(void *unused)
54 {
55 struct Client *client_p;
56 rb_dlink_node *ptr, *ptr_next;
57
58 RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
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))
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 }
80 close_connection(client_p);
81 SetDead(client_p);
82 rb_dlinkAddAlloc(client_p, &dead_list);
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 {
92 rb_dlink_node *ptr, *next;
93 patricia_node_t *pnode;
94 struct reject_data *rdata;
95
96 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
97 {
98 pnode = ptr->data;
99 rdata = pnode->data;
100
101 if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time())
102 continue;
103
104 rb_dlinkDelete(ptr, &reject_list);
105 rb_free(rdata);
106 patricia_remove(reject_tree, pnode);
107 }
108 }
109
110 void
111 init_reject(void)
112 {
113 reject_tree = New_Patricia(PATRICIA_BITS);
114 unknown_tree = New_Patricia(PATRICIA_BITS);
115 rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
116 rb_event_add("reject_expires", reject_expires, NULL, 60);
117 }
118
119
120 void
121 add_reject(struct Client *client_p, const char *mask1, const char *mask2)
122 {
123 patricia_node_t *pnode;
124 struct reject_data *rdata;
125 uint32_t hashv;
126
127 /* Reject is disabled */
128 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0)
129 return;
130
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
137 if((pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
138 {
139 rdata = pnode->data;
140 rdata->time = rb_current_time();
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);
151 pnode->data = rdata = rb_malloc(sizeof(struct reject_data));
152 rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
153 rdata->time = rb_current_time();
154 rdata->count = 1;
155 }
156 rdata->mask_hashv = hashv;
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
175 rdata->time = rb_current_time();
176 if(rdata->count > ConfigFileEntry.reject_after_count)
177 {
178 ServerStats->is_rej++;
179 SetReject(client_p);
180 rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
181 SetClosing(client_p);
182 rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
183 return 1;
184 }
185 }
186 /* Caller does what it wants */
187 return 0;
188 }
189
190 void
191 flush_reject(void)
192 {
193 rb_dlink_node *ptr, *next;
194 patricia_node_t *pnode;
195 struct reject_data *rdata;
196
197 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
198 {
199 pnode = ptr->data;
200 rdata = pnode->data;
201 rb_dlinkDelete(ptr, &reject_list);
202 rb_free(rdata);
203 patricia_remove(reject_tree, pnode);
204 }
205 }
206
207 int
208 remove_reject_ip(const char *ip)
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;
220 rb_dlinkDelete(&rdata->rnode, &reject_list);
221 rb_free(rdata);
222 patricia_remove(reject_tree, pnode);
223 return 1;
224 }
225 return 0;
226 }
227
228 int
229 remove_reject_mask(const char *mask1, const char *mask2)
230 {
231 rb_dlink_node *ptr, *next;
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);
242 RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
243 {
244 pnode = ptr->data;
245 rdata = pnode->data;
246 if (rdata->mask_hashv == hashv)
247 {
248 rb_dlinkDelete(ptr, &reject_list);
249 rb_free(rdata);
250 patricia_remove(reject_tree, pnode);
251 n++;
252 }
253 }
254 return n;
255 }
256
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);
278 rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
279 SetClosing(client_p);
280 rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
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 }
302 /* this can happen due to m_webirc.c's manipulations, for example */
303 }