2 * ircd-ratbox: A slightly useful ircd
3 * reject.c: reject users with prejudice
5 * Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
6 * Copyright (C) 2003-2005 ircd-ratbox development team
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
37 static rb_patricia_tree_t
*reject_tree
;
38 static rb_dlink_list delay_exit
;
39 static rb_dlink_list reject_list
;
40 static rb_dlink_list throttle_list
;
41 static rb_patricia_tree_t
*throttle_tree
;
42 static void throttle_expires(void *unused
);
45 typedef struct _reject_data
53 typedef struct _delay_data
59 typedef struct _throttle
67 delay_exit_length(void)
69 return rb_dlink_list_length(&delay_exit
);
73 reject_exit(void *unused
)
75 rb_dlink_node
*ptr
, *ptr_next
;
77 static const char *errbuf
= "ERROR :Closing Link: (*** Banned (cache))\r\n";
79 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, delay_exit
.head
)
83 rb_write(ddata
->F
, errbuf
, strlen(errbuf
));
88 delay_exit
.head
= delay_exit
.tail
= NULL
;
89 delay_exit
.length
= 0;
93 reject_expires(void *unused
)
95 rb_dlink_node
*ptr
, *next
;
96 rb_patricia_node_t
*pnode
;
99 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
104 if(rdata
->time
+ ConfigFileEntry
.reject_duration
> rb_current_time())
107 rb_dlinkDelete(ptr
, &reject_list
);
109 rb_patricia_remove(reject_tree
, pnode
);
116 reject_tree
= rb_new_patricia(PATRICIA_BITS
);
117 throttle_tree
= rb_new_patricia(PATRICIA_BITS
);
118 rb_event_add("reject_exit", reject_exit
, NULL
, DELAYED_EXIT_TIME
);
119 rb_event_add("reject_expires", reject_expires
, NULL
, 60);
120 rb_event_add("throttle_expires", throttle_expires
, NULL
, 10);
128 rb_patricia_node_t
*pnode
;
132 RB_DLINK_FOREACH(ptr
, throttle_list
.head
)
136 if (t
->count
> ConfigFileEntry
.throttle_count
)
144 add_reject(struct Client
*client_p
, const char *mask1
, const char *mask2
)
146 rb_patricia_node_t
*pnode
;
150 /* Reject is disabled */
151 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
156 hashv
^= fnv_hash_upper((const unsigned char *)mask1
, 32);
158 hashv
^= fnv_hash_upper((const unsigned char *)mask2
, 32);
160 if((pnode
= rb_match_ip(reject_tree
, (struct sockaddr
*)&client_p
->localClient
->ip
)) != NULL
)
163 rdata
->time
= rb_current_time();
170 if(GET_SS_FAMILY(&client_p
->localClient
->ip
) == AF_INET6
)
173 pnode
= make_and_lookup_ip(reject_tree
, (struct sockaddr
*)&client_p
->localClient
->ip
, bitlen
);
174 pnode
->data
= rdata
= rb_malloc(sizeof(reject_t
));
175 rb_dlinkAddTail(pnode
, &rdata
->rnode
, &reject_list
);
176 rdata
->time
= rb_current_time();
179 rdata
->mask_hashv
= hashv
;
183 check_reject(rb_fde_t
*F
, struct sockaddr
*addr
)
185 rb_patricia_node_t
*pnode
;
188 /* Reject is disabled */
189 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
192 pnode
= rb_match_ip(reject_tree
, addr
);
197 rdata
->time
= rb_current_time();
198 if(rdata
->count
> (unsigned long)ConfigFileEntry
.reject_after_count
)
200 ddata
= rb_malloc(sizeof(delay_t
));
201 ServerStats
.is_rej
++;
202 rb_setselect(F
, RB_SELECT_WRITE
| RB_SELECT_READ
, NULL
, NULL
);
204 rb_dlinkAdd(ddata
, &ddata
->node
, &delay_exit
);
208 /* Caller does what it wants */
213 is_reject_ip(struct sockaddr
*addr
)
215 rb_patricia_node_t
*pnode
;
219 /* Reject is disabled */
220 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
223 pnode
= rb_match_ip(reject_tree
, addr
);
228 if(rdata
->count
> (unsigned long)ConfigFileEntry
.reject_after_count
)
230 duration
= rdata
->time
+ ConfigFileEntry
.reject_duration
- rb_current_time();
231 return duration
> 0 ? duration
: 1;
240 rb_dlink_node
*ptr
, *next
;
241 rb_patricia_node_t
*pnode
;
244 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
248 rb_dlinkDelete(ptr
, &reject_list
);
250 rb_patricia_remove(reject_tree
, pnode
);
255 remove_reject_ip(const char *ip
)
257 rb_patricia_node_t
*pnode
;
259 /* Reject is disabled */
260 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
263 if((pnode
= rb_match_string(reject_tree
, ip
)) != NULL
)
265 reject_t
*rdata
= pnode
->data
;
266 rb_dlinkDelete(&rdata
->rnode
, &reject_list
);
268 rb_patricia_remove(reject_tree
, pnode
);
275 remove_reject_mask(const char *mask1
, const char *mask2
)
277 rb_dlink_node
*ptr
, *next
;
278 rb_patricia_node_t
*pnode
;
285 hashv
^= fnv_hash_upper((const unsigned char *)mask1
, 32);
287 hashv
^= fnv_hash_upper((const unsigned char *)mask2
, 32);
288 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
292 if (rdata
->mask_hashv
== hashv
)
294 rb_dlinkDelete(ptr
, &reject_list
);
296 rb_patricia_remove(reject_tree
, pnode
);
304 throttle_add(struct sockaddr
*addr
)
307 rb_patricia_node_t
*pnode
;
309 if((pnode
= rb_match_ip(throttle_tree
, addr
)) != NULL
)
313 if(t
->count
> ConfigFileEntry
.throttle_count
)
315 ServerStats
.is_thr
++;
318 /* Stop penalizing them after they've been throttled */
319 t
->last
= rb_current_time();
325 if(GET_SS_FAMILY(addr
) == AF_INET6
)
328 t
= rb_malloc(sizeof(throttle_t
));
329 t
->last
= rb_current_time();
331 pnode
= make_and_lookup_ip(throttle_tree
, addr
, bitlen
);
333 rb_dlinkAdd(pnode
, &t
->node
, &throttle_list
);
339 is_throttle_ip(struct sockaddr
*addr
)
342 rb_patricia_node_t
*pnode
;
345 if((pnode
= rb_match_ip(throttle_tree
, addr
)) != NULL
)
348 if(t
->count
> ConfigFileEntry
.throttle_count
)
350 duration
= t
->last
+ ConfigFileEntry
.throttle_duration
- rb_current_time();
351 return duration
> 0 ? duration
: 1;
360 rb_dlink_node
*ptr
, *next
;
361 rb_patricia_node_t
*pnode
;
364 RB_DLINK_FOREACH_SAFE(ptr
, next
, throttle_list
.head
)
369 rb_dlinkDelete(ptr
, &throttle_list
);
371 rb_patricia_remove(throttle_tree
, pnode
);
376 throttle_expires(void *unused
)
378 rb_dlink_node
*ptr
, *next
;
379 rb_patricia_node_t
*pnode
;
382 RB_DLINK_FOREACH_SAFE(ptr
, next
, throttle_list
.head
)
387 if(t
->last
+ ConfigFileEntry
.throttle_duration
> rb_current_time())
390 rb_dlinkDelete(ptr
, &throttle_list
);
392 rb_patricia_remove(throttle_tree
, pnode
);