X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/blobdiff_plain/212380e3f42f585dc1ea927402252eb943f91f7b..58067bff67870a2d8a27c3e0222727e0c734c749:/src/reject.c diff --git a/src/reject.c b/src/reject.c index 13d1d31..f763c41 100644 --- a/src/reject.c +++ b/src/reject.c @@ -20,7 +20,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * - * $Id: reject.c 849 2006-02-15 01:33:43Z jilles $ + * $Id: reject.c 3456 2007-05-18 19:14:18Z jilles $ */ #include "stdinc.h" @@ -33,18 +33,24 @@ #include "reject.h" #include "s_stats.h" #include "msg.h" +#include "hash.h" static patricia_tree_t *reject_tree; dlink_list delay_exit; static dlink_list reject_list; +static patricia_tree_t *unknown_tree; + struct reject_data { dlink_node rnode; time_t time; unsigned int count; + uint32_t mask_hashv; }; +static patricia_tree_t *unknown_tree; + static void reject_exit(void *unused) { @@ -67,8 +73,12 @@ reject_exit(void *unused) * ircu message suggests --jilles */ if(!IsIOError(client_p)) - sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host); - + { + if(IsExUnknown(client_p)) + sendto_one(client_p, "ERROR :Closing Link: %s (*** Too many unknown connections)", client_p->host); + else + sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host); + } close_connection(client_p); SetDead(client_p); dlinkAddAlloc(client_p, &dead_list); @@ -103,21 +113,29 @@ void init_reject(void) { reject_tree = New_Patricia(PATRICIA_BITS); + unknown_tree = New_Patricia(PATRICIA_BITS); eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME); eventAdd("reject_expires", reject_expires, NULL, 60); } void -add_reject(struct Client *client_p) +add_reject(struct Client *client_p, const char *mask1, const char *mask2) { patricia_node_t *pnode; struct reject_data *rdata; + uint32_t hashv; /* Reject is disabled */ if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0) return; + hashv = 0; + if (mask1 != NULL) + hashv ^= fnv_hash_upper(mask1, 32); + if (mask2 != NULL) + hashv ^= fnv_hash_upper(mask2, 32); + if((pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) { rdata = pnode->data; @@ -137,6 +155,7 @@ add_reject(struct Client *client_p) rdata->time = CurrentTime; rdata->count = 1; } + rdata->mask_hashv = hashv; } int @@ -160,7 +179,7 @@ check_reject(struct Client *client_p) { ServerStats->is_rej++; SetReject(client_p); - comm_setselect(client_p->localClient->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); + comm_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); SetClosing(client_p); dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); return 1; @@ -188,7 +207,7 @@ flush_reject(void) } int -remove_reject(const char *ip) +remove_reject_ip(const char *ip) { patricia_node_t *pnode; @@ -208,3 +227,79 @@ remove_reject(const char *ip) return 0; } +int +remove_reject_mask(const char *mask1, const char *mask2) +{ + dlink_node *ptr, *next; + patricia_node_t *pnode; + struct reject_data *rdata; + uint32_t hashv; + int n = 0; + + hashv = 0; + if (mask1 != NULL) + hashv ^= fnv_hash_upper(mask1, 32); + if (mask2 != NULL) + hashv ^= fnv_hash_upper(mask2, 32); + DLINK_FOREACH_SAFE(ptr, next, reject_list.head) + { + pnode = ptr->data; + rdata = pnode->data; + if (rdata->mask_hashv == hashv) + { + dlinkDelete(ptr, &reject_list); + MyFree(rdata); + patricia_remove(reject_tree, pnode); + n++; + } + } + return n; +} + + +int +add_unknown_ip(struct Client *client_p) +{ + patricia_node_t *pnode; + + if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) == NULL) + { + int bitlen = 32; +#ifdef IPV6 + if(client_p->localClient->ip.ss_family == AF_INET6) + bitlen = 128; +#endif + pnode = make_and_lookup_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); + pnode->data = (void *)0; + } + + if((unsigned long)pnode->data >= ConfigFileEntry.max_unknown_ip) + { + SetExUnknown(client_p); + SetReject(client_p); + comm_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); + SetClosing(client_p); + dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); + return 1; + } + + pnode->data = (void *)((unsigned long)pnode->data + 1); + + return 0; +} + +void +del_unknown_ip(struct Client *client_p) +{ + patricia_node_t *pnode; + + if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) + { + pnode->data = (void *)((unsigned long)pnode->data - 1); + if((unsigned long)pnode->data <= 0) + { + patricia_remove(unknown_tree, pnode); + } + } + /* this can happen due to m_webirc.c's manipulations, for example */ +}