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
38 static rb_patricia_tree_t
*reject_tree
;
39 static rb_dlink_list delay_exit
;
40 static rb_dlink_list reject_list
;
41 static rb_dlink_list throttle_list
;
42 static rb_patricia_tree_t
*throttle_tree
;
43 static void throttle_expires(void *unused
);
46 typedef struct _reject_data
54 typedef struct _delay_data
60 typedef struct _throttle
68 delay_exit_length(void)
70 return rb_dlink_list_length(&delay_exit
);
74 reject_exit(void *unused
)
76 rb_dlink_node
*ptr
, *ptr_next
;
78 static const char *errbuf
= "ERROR :Closing Link: (*** Banned (cache))\r\n";
80 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, delay_exit
.head
)
84 rb_write(ddata
->F
, errbuf
, strlen(errbuf
));
89 delay_exit
.head
= delay_exit
.tail
= NULL
;
90 delay_exit
.length
= 0;
94 reject_expires(void *unused
)
96 rb_dlink_node
*ptr
, *next
;
97 rb_patricia_node_t
*pnode
;
100 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
105 if(rdata
->time
+ ConfigFileEntry
.reject_duration
> rb_current_time())
108 rb_dlinkDelete(ptr
, &reject_list
);
110 rb_patricia_remove(reject_tree
, pnode
);
117 reject_tree
= rb_new_patricia(PATRICIA_BITS
);
118 throttle_tree
= rb_new_patricia(PATRICIA_BITS
);
119 rb_event_add("reject_exit", reject_exit
, NULL
, DELAYED_EXIT_TIME
);
120 rb_event_add("reject_expires", reject_expires
, NULL
, 60);
121 rb_event_add("throttle_expires", throttle_expires
, NULL
, 10);
129 rb_patricia_node_t
*pnode
;
133 RB_DLINK_FOREACH(ptr
, throttle_list
.head
)
137 if (t
->count
> ConfigFileEntry
.throttle_count
)
145 add_reject(struct Client
*client_p
, const char *mask1
, const char *mask2
)
147 rb_patricia_node_t
*pnode
;
151 /* Reject is disabled */
152 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
157 hashv
^= fnv_hash_upper((const unsigned char *)mask1
, 32);
159 hashv
^= fnv_hash_upper((const unsigned char *)mask2
, 32);
161 if((pnode
= rb_match_ip(reject_tree
, (struct sockaddr
*)&client_p
->localClient
->ip
)) != NULL
)
164 rdata
->time
= rb_current_time();
171 if(GET_SS_FAMILY(&client_p
->localClient
->ip
) == AF_INET6
)
174 pnode
= make_and_lookup_ip(reject_tree
, (struct sockaddr
*)&client_p
->localClient
->ip
, bitlen
);
175 pnode
->data
= rdata
= rb_malloc(sizeof(reject_t
));
176 rb_dlinkAddTail(pnode
, &rdata
->rnode
, &reject_list
);
177 rdata
->time
= rb_current_time();
180 rdata
->mask_hashv
= hashv
;
184 check_reject(rb_fde_t
*F
, struct sockaddr
*addr
)
186 rb_patricia_node_t
*pnode
;
189 /* Reject is disabled */
190 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
193 pnode
= rb_match_ip(reject_tree
, addr
);
198 rdata
->time
= rb_current_time();
199 if(rdata
->count
> (unsigned long)ConfigFileEntry
.reject_after_count
)
201 ddata
= rb_malloc(sizeof(delay_t
));
202 ServerStats
.is_rej
++;
203 rb_setselect(F
, RB_SELECT_WRITE
| RB_SELECT_READ
, NULL
, NULL
);
205 rb_dlinkAdd(ddata
, &ddata
->node
, &delay_exit
);
209 /* Caller does what it wants */
214 is_reject_ip(struct sockaddr
*addr
)
216 rb_patricia_node_t
*pnode
;
220 /* Reject is disabled */
221 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
224 pnode
= rb_match_ip(reject_tree
, addr
);
229 if(rdata
->count
> (unsigned long)ConfigFileEntry
.reject_after_count
)
231 duration
= rdata
->time
+ ConfigFileEntry
.reject_duration
- rb_current_time();
232 return duration
> 0 ? duration
: 1;
241 rb_dlink_node
*ptr
, *next
;
242 rb_patricia_node_t
*pnode
;
245 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
249 rb_dlinkDelete(ptr
, &reject_list
);
251 rb_patricia_remove(reject_tree
, pnode
);
256 remove_reject_ip(const char *ip
)
258 rb_patricia_node_t
*pnode
;
260 /* Reject is disabled */
261 if(ConfigFileEntry
.reject_after_count
== 0 || ConfigFileEntry
.reject_duration
== 0)
264 if((pnode
= rb_match_string(reject_tree
, ip
)) != NULL
)
266 reject_t
*rdata
= pnode
->data
;
267 rb_dlinkDelete(&rdata
->rnode
, &reject_list
);
269 rb_patricia_remove(reject_tree
, pnode
);
276 remove_reject_mask(const char *mask1
, const char *mask2
)
278 rb_dlink_node
*ptr
, *next
;
279 rb_patricia_node_t
*pnode
;
286 hashv
^= fnv_hash_upper((const unsigned char *)mask1
, 32);
288 hashv
^= fnv_hash_upper((const unsigned char *)mask2
, 32);
289 RB_DLINK_FOREACH_SAFE(ptr
, next
, reject_list
.head
)
293 if (rdata
->mask_hashv
== hashv
)
295 rb_dlinkDelete(ptr
, &reject_list
);
297 rb_patricia_remove(reject_tree
, pnode
);
305 throttle_add(struct sockaddr
*addr
)
308 rb_patricia_node_t
*pnode
;
310 if((pnode
= rb_match_ip(throttle_tree
, addr
)) != NULL
)
314 if(t
->count
> ConfigFileEntry
.throttle_count
)
316 ServerStats
.is_thr
++;
319 /* Stop penalizing them after they've been throttled */
320 t
->last
= rb_current_time();
326 if(GET_SS_FAMILY(addr
) == AF_INET6
)
329 t
= rb_malloc(sizeof(throttle_t
));
330 t
->last
= rb_current_time();
332 pnode
= make_and_lookup_ip(throttle_tree
, addr
, bitlen
);
334 rb_dlinkAdd(pnode
, &t
->node
, &throttle_list
);
340 is_throttle_ip(struct sockaddr
*addr
)
343 rb_patricia_node_t
*pnode
;
346 if((pnode
= rb_match_ip(throttle_tree
, addr
)) != NULL
)
349 if(t
->count
> ConfigFileEntry
.throttle_count
)
351 duration
= t
->last
+ ConfigFileEntry
.throttle_duration
- rb_current_time();
352 return duration
> 0 ? duration
: 1;
361 rb_dlink_node
*ptr
, *next
;
362 rb_patricia_node_t
*pnode
;
365 RB_DLINK_FOREACH_SAFE(ptr
, next
, throttle_list
.head
)
370 rb_dlinkDelete(ptr
, &throttle_list
);
372 rb_patricia_remove(throttle_tree
, pnode
);
377 throttle_expires(void *unused
)
379 rb_dlink_node
*ptr
, *next
;
380 rb_patricia_node_t
*pnode
;
383 RB_DLINK_FOREACH_SAFE(ptr
, next
, throttle_list
.head
)
388 if(t
->last
+ ConfigFileEntry
.throttle_duration
> rb_current_time())
391 rb_dlinkDelete(ptr
, &throttle_list
);
393 rb_patricia_remove(throttle_tree
, pnode
);