Fixing bugs
This commit is contained in:
parent
ea0d463a50
commit
3e32e4d686
|
@ -31,9 +31,6 @@
|
||||||
|
|
||||||
extern rb_dlink_list delay_exit;
|
extern rb_dlink_list delay_exit;
|
||||||
|
|
||||||
struct ConfItem *find_dline(struct sockaddr *addr);
|
|
||||||
struct ConfItem *find_dline_exact(struct sockaddr *addr, unsigned int bitlen);
|
|
||||||
|
|
||||||
void init_reject(void);
|
void init_reject(void);
|
||||||
int check_reject(struct Client *);
|
int check_reject(struct Client *);
|
||||||
void add_reject(struct Client *, const char *mask1, const char *mask2);
|
void add_reject(struct Client *, const char *mask1, const char *mask2);
|
||||||
|
@ -45,4 +42,3 @@ int add_unknown_ip(struct Client *client_p);
|
||||||
void del_unknown_ip(struct Client *client_p);
|
void del_unknown_ip(struct Client *client_p);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
895
src/reject.c
895
src/reject.c
|
@ -1,305 +1,590 @@
|
||||||
/*
|
/*
|
||||||
* ircd-ratbox: A slightly useful ircd
|
* ircd-ratbox: A slightly useful ircd
|
||||||
* reject.c: reject users with prejudice
|
* reject.c: reject users with prejudice
|
||||||
*
|
*
|
||||||
* Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
|
* Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
|
||||||
* Copyright (C) 2003-2005 ircd-ratbox development team
|
* Copyright (C) 2003-2005 ircd-ratbox development team
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
||||||
* USA
|
* USA
|
||||||
*
|
*
|
||||||
* $Id: reject.c 3456 2007-05-18 19:14:18Z jilles $
|
* $Id: reject.c 25119 2008-03-13 16:57:05Z androsyn $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "stdinc.h"
|
#include "stdinc.h"
|
||||||
#include "config.h"
|
#include "client.h"
|
||||||
#include "patricia.h"
|
#include "s_conf.h"
|
||||||
#include "client.h"
|
#include "reject.h"
|
||||||
#include "s_conf.h"
|
#include "s_stats.h"
|
||||||
#include "event.h"
|
#include "ircd.h"
|
||||||
#include "rb_tools.h"
|
#include "send.h"
|
||||||
#include "reject.h"
|
#include "numeric.h"
|
||||||
#include "s_stats.h"
|
#include "parse.h"
|
||||||
#include "msg.h"
|
#include "hostmask.h"
|
||||||
#include "hash.h"
|
|
||||||
|
static rb_patricia_tree_t *global_tree;
|
||||||
static patricia_tree_t *reject_tree;
|
static rb_patricia_tree_t *reject_tree;
|
||||||
rb_dlink_list delay_exit;
|
static rb_patricia_tree_t *dline_tree;
|
||||||
static rb_dlink_list reject_list;
|
static rb_patricia_tree_t *eline_tree;
|
||||||
|
static rb_dlink_list delay_exit;
|
||||||
static patricia_tree_t *unknown_tree;
|
static rb_dlink_list reject_list;
|
||||||
|
static rb_dlink_list throttle_list;
|
||||||
struct reject_data
|
static rb_patricia_tree_t *throttle_tree;
|
||||||
{
|
static void throttle_expires(void *unused);
|
||||||
rb_dlink_node rnode;
|
|
||||||
time_t time;
|
|
||||||
unsigned int count;
|
typedef struct _reject_data
|
||||||
uint32_t mask_hashv;
|
{
|
||||||
};
|
rb_dlink_node rnode;
|
||||||
|
time_t time;
|
||||||
static patricia_tree_t *unknown_tree;
|
unsigned int count;
|
||||||
|
} reject_t;
|
||||||
static void
|
|
||||||
reject_exit(void *unused)
|
typedef struct _delay_data
|
||||||
{
|
{
|
||||||
struct Client *client_p;
|
rb_dlink_node node;
|
||||||
rb_dlink_node *ptr, *ptr_next;
|
rb_fde_t *F;
|
||||||
|
} delay_t;
|
||||||
RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
|
|
||||||
{
|
typedef struct _throttle
|
||||||
client_p = ptr->data;
|
{
|
||||||
if(IsDead(client_p))
|
rb_dlink_node node;
|
||||||
continue;
|
time_t last;
|
||||||
|
int count;
|
||||||
/* this MUST be here, to prevent the possibility
|
} throttle_t;
|
||||||
* sendto_one() generates a write error, and then a client
|
|
||||||
* ends up on the dead_list and the abort_list --fl
|
typedef struct _global_data
|
||||||
*
|
{
|
||||||
* new disconnect notice stolen from ircu --nenolod
|
int count;
|
||||||
* no, this only happens when someone's IP has some
|
} global_t;
|
||||||
* ban on it and rejects them rather longer than the
|
|
||||||
* ircu message suggests --jilles
|
|
||||||
*/
|
static rb_patricia_node_t *
|
||||||
if(!IsIOError(client_p))
|
add_ipline(struct ConfItem *aconf, rb_patricia_tree_t *tree, struct sockaddr *addr, int cidr)
|
||||||
{
|
{
|
||||||
if(IsExUnknown(client_p))
|
rb_patricia_node_t *pnode;
|
||||||
sendto_one(client_p, "ERROR :Closing Link: %s (*** Too many unknown connections)", client_p->host);
|
pnode = make_and_lookup_ip(tree, addr, cidr);
|
||||||
else
|
if(pnode == NULL)
|
||||||
sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host);
|
return NULL;
|
||||||
}
|
aconf->pnode = pnode;
|
||||||
close_connection(client_p);
|
pnode->data = aconf;
|
||||||
SetDead(client_p);
|
return (pnode);
|
||||||
rb_dlinkAddAlloc(client_p, &dead_list);
|
}
|
||||||
}
|
|
||||||
|
int
|
||||||
delay_exit.head = delay_exit.tail = NULL;
|
add_dline(struct ConfItem *aconf)
|
||||||
delay_exit.length = 0;
|
{
|
||||||
}
|
struct rb_sockaddr_storage st;
|
||||||
|
int bitlen;
|
||||||
static void
|
if(parse_netmask(aconf->host, (struct sockaddr *)&st, &bitlen) == HM_HOST)
|
||||||
reject_expires(void *unused)
|
return 0;
|
||||||
{
|
|
||||||
rb_dlink_node *ptr, *next;
|
if(add_ipline(aconf, dline_tree, (struct sockaddr *)&st, bitlen) != NULL)
|
||||||
patricia_node_t *pnode;
|
return 1;
|
||||||
struct reject_data *rdata;
|
return 0;
|
||||||
|
}
|
||||||
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
|
||||||
{
|
int
|
||||||
pnode = ptr->data;
|
add_eline(struct ConfItem *aconf)
|
||||||
rdata = pnode->data;
|
{
|
||||||
|
struct rb_sockaddr_storage st;
|
||||||
if(rdata->time + ConfigFileEntry.reject_duration > CurrentTime)
|
int bitlen;
|
||||||
continue;
|
if(parse_netmask(aconf->host, (struct sockaddr *)&st, &bitlen) == HM_HOST)
|
||||||
|
return 0;
|
||||||
rb_dlinkDelete(ptr, &reject_list);
|
|
||||||
MyFree(rdata);
|
if(add_ipline(aconf, eline_tree, (struct sockaddr *)&st, bitlen) != NULL)
|
||||||
patricia_remove(reject_tree, pnode);
|
return 1;
|
||||||
}
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
unsigned long
|
||||||
init_reject(void)
|
delay_exit_length(void)
|
||||||
{
|
{
|
||||||
reject_tree = New_Patricia(PATRICIA_BITS);
|
return rb_dlink_list_length(&delay_exit);
|
||||||
unknown_tree = New_Patricia(PATRICIA_BITS);
|
}
|
||||||
eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
|
|
||||||
eventAdd("reject_expires", reject_expires, NULL, 60);
|
static void
|
||||||
}
|
reject_exit(void *unused)
|
||||||
|
{
|
||||||
|
rb_dlink_node *ptr, *ptr_next;
|
||||||
void
|
delay_t *ddata;
|
||||||
add_reject(struct Client *client_p, const char *mask1, const char *mask2)
|
static const char *errbuf = "ERROR :Closing Link: (*** Banned (cache))\r\n";
|
||||||
{
|
|
||||||
patricia_node_t *pnode;
|
RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
|
||||||
struct reject_data *rdata;
|
{
|
||||||
uint32_t hashv;
|
ddata = ptr->data;
|
||||||
|
|
||||||
/* Reject is disabled */
|
rb_write(ddata->F, errbuf, strlen(errbuf));
|
||||||
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0)
|
rb_close(ddata->F);
|
||||||
return;
|
rb_free(ddata);
|
||||||
|
}
|
||||||
hashv = 0;
|
|
||||||
if (mask1 != NULL)
|
delay_exit.head = delay_exit.tail = NULL;
|
||||||
hashv ^= fnv_hash_upper(mask1, 32);
|
delay_exit.length = 0;
|
||||||
if (mask2 != NULL)
|
}
|
||||||
hashv ^= fnv_hash_upper(mask2, 32);
|
|
||||||
|
static void
|
||||||
if((pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
|
reject_expires(void *unused)
|
||||||
{
|
{
|
||||||
rdata = pnode->data;
|
rb_dlink_node *ptr, *next;
|
||||||
rdata->time = CurrentTime;
|
rb_patricia_node_t *pnode;
|
||||||
rdata->count++;
|
reject_t *rdata;
|
||||||
}
|
|
||||||
else
|
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
||||||
{
|
{
|
||||||
int bitlen = 32;
|
pnode = ptr->data;
|
||||||
#ifdef IPV6
|
rdata = pnode->data;
|
||||||
if(client_p->localClient->ip.ss_family == AF_INET6)
|
|
||||||
bitlen = 128;
|
if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time())
|
||||||
#endif
|
continue;
|
||||||
pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
|
|
||||||
pnode->data = rdata = MyMalloc(sizeof(struct reject_data));
|
rb_dlinkDelete(ptr, &reject_list);
|
||||||
rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
|
rb_free(rdata);
|
||||||
rdata->time = CurrentTime;
|
rb_patricia_remove(reject_tree, pnode);
|
||||||
rdata->count = 1;
|
}
|
||||||
}
|
}
|
||||||
rdata->mask_hashv = hashv;
|
|
||||||
}
|
void
|
||||||
|
init_reject(void)
|
||||||
int
|
{
|
||||||
check_reject(struct Client *client_p)
|
reject_tree = rb_new_patricia(PATRICIA_BITS);
|
||||||
{
|
dline_tree = rb_new_patricia(PATRICIA_BITS);
|
||||||
patricia_node_t *pnode;
|
eline_tree = rb_new_patricia(PATRICIA_BITS);
|
||||||
struct reject_data *rdata;
|
throttle_tree = rb_new_patricia(PATRICIA_BITS);
|
||||||
|
global_tree = rb_new_patricia(PATRICIA_BITS);
|
||||||
/* Reject is disabled */
|
rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
|
||||||
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 ||
|
rb_event_add("reject_expires", reject_expires, NULL, 60);
|
||||||
ConfigFileEntry.reject_duration == 0)
|
rb_event_add("throttle_expires", throttle_expires, NULL, 10);
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip);
|
|
||||||
if(pnode != NULL)
|
void
|
||||||
{
|
add_reject(struct Client *client_p)
|
||||||
rdata = pnode->data;
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
rdata->time = CurrentTime;
|
reject_t *rdata;
|
||||||
if(rdata->count > ConfigFileEntry.reject_after_count)
|
|
||||||
{
|
/* Reject is disabled */
|
||||||
ServerStats->is_rej++;
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
||||||
SetReject(client_p);
|
return;
|
||||||
rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
|
|
||||||
SetClosing(client_p);
|
if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
|
||||||
rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
|
{
|
||||||
return 1;
|
rdata = pnode->data;
|
||||||
}
|
rdata->time = rb_current_time();
|
||||||
}
|
rdata->count++;
|
||||||
/* Caller does what it wants */
|
}
|
||||||
return 0;
|
else
|
||||||
}
|
{
|
||||||
|
int bitlen = 32;
|
||||||
void
|
#ifdef RB_IPV6
|
||||||
flush_reject(void)
|
if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
|
||||||
{
|
bitlen = 128;
|
||||||
rb_dlink_node *ptr, *next;
|
#endif
|
||||||
patricia_node_t *pnode;
|
pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
|
||||||
struct reject_data *rdata;
|
pnode->data = rdata = rb_malloc(sizeof(reject_t));
|
||||||
|
rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
|
||||||
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
rdata->time = rb_current_time();
|
||||||
{
|
rdata->count = 1;
|
||||||
pnode = ptr->data;
|
}
|
||||||
rdata = pnode->data;
|
}
|
||||||
rb_dlinkDelete(ptr, &reject_list);
|
|
||||||
MyFree(rdata);
|
int
|
||||||
patricia_remove(reject_tree, pnode);
|
check_reject(rb_fde_t *F, struct sockaddr *addr)
|
||||||
}
|
{
|
||||||
}
|
rb_patricia_node_t *pnode;
|
||||||
|
reject_t *rdata;
|
||||||
int
|
delay_t *ddata;
|
||||||
remove_reject_ip(const char *ip)
|
/* Reject is disabled */
|
||||||
{
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
||||||
patricia_node_t *pnode;
|
return 0;
|
||||||
|
|
||||||
/* Reject is disabled */
|
pnode = rb_match_ip(reject_tree, addr);
|
||||||
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 ||
|
if(pnode != NULL)
|
||||||
ConfigFileEntry.reject_duration == 0)
|
{
|
||||||
return -1;
|
rdata = pnode->data;
|
||||||
|
|
||||||
if((pnode = match_string(reject_tree, ip)) != NULL)
|
rdata->time = rb_current_time();
|
||||||
{
|
if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
|
||||||
struct reject_data *rdata = pnode->data;
|
{
|
||||||
rb_dlinkDelete(&rdata->rnode, &reject_list);
|
ddata = rb_malloc(sizeof(delay_t));
|
||||||
MyFree(rdata);
|
ServerStats.is_rej++;
|
||||||
patricia_remove(reject_tree, pnode);
|
rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL);
|
||||||
return 1;
|
ddata->F = F;
|
||||||
}
|
rb_dlinkAdd(ddata, &ddata->node, &delay_exit);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int
|
/* Caller does what it wants */
|
||||||
remove_reject_mask(const char *mask1, const char *mask2)
|
return 0;
|
||||||
{
|
}
|
||||||
rb_dlink_node *ptr, *next;
|
|
||||||
patricia_node_t *pnode;
|
void
|
||||||
struct reject_data *rdata;
|
flush_reject(void)
|
||||||
uint32_t hashv;
|
{
|
||||||
int n = 0;
|
rb_dlink_node *ptr, *next;
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
hashv = 0;
|
reject_t *rdata;
|
||||||
if (mask1 != NULL)
|
|
||||||
hashv ^= fnv_hash_upper(mask1, 32);
|
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
||||||
if (mask2 != NULL)
|
{
|
||||||
hashv ^= fnv_hash_upper(mask2, 32);
|
pnode = ptr->data;
|
||||||
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
rdata = pnode->data;
|
||||||
{
|
rb_dlinkDelete(ptr, &reject_list);
|
||||||
pnode = ptr->data;
|
rb_free(rdata);
|
||||||
rdata = pnode->data;
|
rb_patricia_remove(reject_tree, pnode);
|
||||||
if (rdata->mask_hashv == hashv)
|
}
|
||||||
{
|
}
|
||||||
rb_dlinkDelete(ptr, &reject_list);
|
|
||||||
MyFree(rdata);
|
int
|
||||||
patricia_remove(reject_tree, pnode);
|
remove_reject(const char *ip)
|
||||||
n++;
|
{
|
||||||
}
|
rb_patricia_node_t *pnode;
|
||||||
}
|
|
||||||
return n;
|
/* Reject is disabled */
|
||||||
}
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
int
|
if((pnode = rb_match_string(reject_tree, ip)) != NULL)
|
||||||
add_unknown_ip(struct Client *client_p)
|
{
|
||||||
{
|
reject_t *rdata = pnode->data;
|
||||||
patricia_node_t *pnode;
|
rb_dlinkDelete(&rdata->rnode, &reject_list);
|
||||||
|
rb_free(rdata);
|
||||||
if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) == NULL)
|
rb_patricia_remove(reject_tree, pnode);
|
||||||
{
|
return 1;
|
||||||
int bitlen = 32;
|
}
|
||||||
#ifdef IPV6
|
return 0;
|
||||||
if(client_p->localClient->ip.ss_family == AF_INET6)
|
}
|
||||||
bitlen = 128;
|
|
||||||
#endif
|
static void
|
||||||
pnode = make_and_lookup_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
|
delete_ipline(struct ConfItem *aconf, rb_patricia_tree_t *t)
|
||||||
pnode->data = (void *)0;
|
{
|
||||||
}
|
rb_patricia_remove(t, aconf->pnode);
|
||||||
|
if(!aconf->clients)
|
||||||
if((unsigned long)pnode->data >= ConfigFileEntry.max_unknown_ip)
|
{
|
||||||
{
|
free_conf(aconf);
|
||||||
SetExUnknown(client_p);
|
}
|
||||||
SetReject(client_p);
|
}
|
||||||
rb_setselect(client_p->localClient->F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
|
|
||||||
SetClosing(client_p);
|
static struct ConfItem *
|
||||||
rb_dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
|
find_ipline(rb_patricia_tree_t *t, struct sockaddr *addr)
|
||||||
return 1;
|
{
|
||||||
}
|
rb_patricia_node_t *pnode;
|
||||||
|
pnode = rb_match_ip(t, addr);
|
||||||
pnode->data = (void *)((unsigned long)pnode->data + 1);
|
if(pnode != NULL)
|
||||||
|
return (struct ConfItem *) pnode->data;
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static struct ConfItem *
|
||||||
del_unknown_ip(struct Client *client_p)
|
find_ipline_exact(rb_patricia_tree_t *t, struct sockaddr *addr, unsigned int bitlen)
|
||||||
{
|
{
|
||||||
patricia_node_t *pnode;
|
rb_patricia_node_t *pnode;
|
||||||
|
pnode = rb_match_ip_exact(t, addr, bitlen);
|
||||||
if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
|
if(pnode != NULL)
|
||||||
{
|
return (struct ConfItem *) pnode->data;
|
||||||
pnode->data = (void *)((unsigned long)pnode->data - 1);
|
return NULL;
|
||||||
if((unsigned long)pnode->data <= 0)
|
}
|
||||||
{
|
|
||||||
patricia_remove(unknown_tree, pnode);
|
|
||||||
}
|
struct ConfItem *
|
||||||
}
|
find_dline(struct sockaddr *addr)
|
||||||
/* this can happen due to m_webirc.c's manipulations, for example */
|
{
|
||||||
}
|
struct ConfItem *aconf;
|
||||||
|
aconf = find_ipline(eline_tree, addr);
|
||||||
|
if(aconf != NULL)
|
||||||
|
{
|
||||||
|
return aconf;
|
||||||
|
}
|
||||||
|
return (find_ipline(dline_tree, addr));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ConfItem *
|
||||||
|
find_dline_exact(struct sockaddr *addr, unsigned int bitlen)
|
||||||
|
{
|
||||||
|
return find_ipline_exact(dline_tree, addr, bitlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
remove_dline(struct ConfItem *aconf)
|
||||||
|
{
|
||||||
|
delete_ipline(aconf, dline_tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
report_dlines(struct Client *source_p)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
struct ConfItem *aconf;
|
||||||
|
const char *host, *pass, *user, *oper_reason;
|
||||||
|
RB_PATRICIA_WALK(dline_tree->head, pnode)
|
||||||
|
{
|
||||||
|
aconf = pnode->data;
|
||||||
|
if(aconf->flags & CONF_FLAGS_TEMPORARY)
|
||||||
|
RB_PATRICIA_WALK_BREAK;
|
||||||
|
get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
|
||||||
|
sendto_one_numeric(source_p, RPL_STATSDLINE,
|
||||||
|
form_str (RPL_STATSDLINE),
|
||||||
|
'D', host, pass,
|
||||||
|
oper_reason ? "|" : "",
|
||||||
|
oper_reason ? oper_reason : "");
|
||||||
|
}
|
||||||
|
RB_PATRICIA_WALK_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
report_tdlines(struct Client *source_p)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
struct ConfItem *aconf;
|
||||||
|
const char *host, *pass, *user, *oper_reason;
|
||||||
|
RB_PATRICIA_WALK(dline_tree->head, pnode)
|
||||||
|
{
|
||||||
|
aconf = pnode->data;
|
||||||
|
if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
|
||||||
|
RB_PATRICIA_WALK_BREAK;
|
||||||
|
get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
|
||||||
|
sendto_one_numeric(source_p, RPL_STATSDLINE,
|
||||||
|
form_str (RPL_STATSDLINE),
|
||||||
|
'd', host, pass,
|
||||||
|
oper_reason ? "|" : "",
|
||||||
|
oper_reason ? oper_reason : "");
|
||||||
|
}
|
||||||
|
RB_PATRICIA_WALK_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
report_elines(struct Client *source_p)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
struct ConfItem *aconf;
|
||||||
|
int port;
|
||||||
|
const char *name, *host, *pass, *user, *classname;
|
||||||
|
RB_PATRICIA_WALK(eline_tree->head, pnode)
|
||||||
|
{
|
||||||
|
aconf = pnode->data;
|
||||||
|
get_printable_conf(aconf, &name, &host, &pass, &user, &port, &classname);
|
||||||
|
sendto_one_numeric(source_p, RPL_STATSDLINE,
|
||||||
|
form_str (RPL_STATSDLINE),
|
||||||
|
'e', host, pass,
|
||||||
|
"", "");
|
||||||
|
}
|
||||||
|
RB_PATRICIA_WALK_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
throttle_add(struct sockaddr *addr)
|
||||||
|
{
|
||||||
|
throttle_t *t;
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
|
||||||
|
if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
|
||||||
|
{
|
||||||
|
t = pnode->data;
|
||||||
|
|
||||||
|
if(t->count > ConfigFileEntry.throttle_count)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
/* Stop penalizing them after they've been throttled */
|
||||||
|
t->last = rb_current_time();
|
||||||
|
t->count++;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
int bitlen = 32;
|
||||||
|
#ifdef RB_IPV6
|
||||||
|
if(GET_SS_FAMILY(addr) == AF_INET6)
|
||||||
|
bitlen = 128;
|
||||||
|
#endif
|
||||||
|
t = rb_malloc(sizeof(throttle_t));
|
||||||
|
t->last = rb_current_time();
|
||||||
|
t->count = 1;
|
||||||
|
pnode = make_and_lookup_ip(throttle_tree, addr, bitlen);
|
||||||
|
pnode->data = t;
|
||||||
|
rb_dlinkAdd(pnode, &t->node, &throttle_list);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
throttle_expires(void *unused)
|
||||||
|
{
|
||||||
|
rb_dlink_node *ptr, *next;
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
throttle_t *t;
|
||||||
|
|
||||||
|
RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
|
||||||
|
{
|
||||||
|
pnode = ptr->data;
|
||||||
|
t = pnode->data;
|
||||||
|
|
||||||
|
if(t->last + ConfigFileEntry.throttle_duration > rb_current_time())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
rb_dlinkDelete(ptr, &throttle_list);
|
||||||
|
rb_free(t);
|
||||||
|
rb_patricia_remove(throttle_tree, pnode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_global_count(struct sockaddr *addr)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
global_t *glb;
|
||||||
|
|
||||||
|
if((pnode = rb_match_ip(global_tree, addr)))
|
||||||
|
{
|
||||||
|
glb = pnode->data;
|
||||||
|
return glb->count;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
inc_global_ip(struct sockaddr *addr, int bitlen)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
global_t *glb;
|
||||||
|
|
||||||
|
|
||||||
|
if((pnode = rb_match_ip(global_tree, addr)))
|
||||||
|
{
|
||||||
|
glb = pnode->data;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pnode = make_and_lookup_ip(global_tree, addr, bitlen);
|
||||||
|
glb = rb_malloc(sizeof(global_t));
|
||||||
|
pnode->data = glb;
|
||||||
|
}
|
||||||
|
glb->count++;
|
||||||
|
return glb->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dec_global_ip(struct sockaddr *addr)
|
||||||
|
{
|
||||||
|
rb_patricia_node_t *pnode;
|
||||||
|
global_t *glb;
|
||||||
|
|
||||||
|
if((pnode = rb_match_ip(global_tree, addr)))
|
||||||
|
{
|
||||||
|
glb = pnode->data;
|
||||||
|
glb->count--;
|
||||||
|
if(glb->count == 0)
|
||||||
|
{
|
||||||
|
rb_free(glb);
|
||||||
|
rb_patricia_remove(global_tree, pnode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
inc_global_cidr_count(struct Client *client_p)
|
||||||
|
{
|
||||||
|
struct rb_sockaddr_storage ip;
|
||||||
|
struct sockaddr *addr;
|
||||||
|
int bitlen;
|
||||||
|
|
||||||
|
if(!MyClient(client_p))
|
||||||
|
{
|
||||||
|
if(EmptyString(client_p->sockhost) || !strcmp(client_p->sockhost, "0"))
|
||||||
|
return -1;
|
||||||
|
if(!rb_inet_pton_sock(client_p->sockhost, (struct sockaddr *)&ip))
|
||||||
|
return -1;
|
||||||
|
addr = (struct sockaddr *)&ip;
|
||||||
|
} else
|
||||||
|
addr = (struct sockaddr *)&client_p->localClient->ip;
|
||||||
|
#ifdef RB_IPV6
|
||||||
|
if(GET_SS_FAMILY(addr) == AF_INET6)
|
||||||
|
{
|
||||||
|
bitlen = ConfigFileEntry.global_cidr_ipv6_bitlen;
|
||||||
|
} else
|
||||||
|
#endif
|
||||||
|
bitlen = ConfigFileEntry.global_cidr_ipv4_bitlen;
|
||||||
|
|
||||||
|
return inc_global_ip(addr, bitlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
dec_global_cidr_count(struct Client *client_p)
|
||||||
|
{
|
||||||
|
struct rb_sockaddr_storage ip;
|
||||||
|
struct sockaddr *addr;
|
||||||
|
if(!MyClient(client_p))
|
||||||
|
{
|
||||||
|
if(EmptyString(client_p->sockhost) || !strcmp(client_p->sockhost, "0"))
|
||||||
|
return;
|
||||||
|
if(!rb_inet_pton_sock(client_p->sockhost, (struct sockaddr *)&ip))
|
||||||
|
return;
|
||||||
|
addr = (struct sockaddr *)&ip;
|
||||||
|
} else
|
||||||
|
addr = (struct sockaddr *)&client_p->localClient->ip;
|
||||||
|
|
||||||
|
dec_global_ip(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
check_global_cidr_count(struct Client *client_p)
|
||||||
|
{
|
||||||
|
struct rb_sockaddr_storage ip;
|
||||||
|
struct sockaddr *addr;
|
||||||
|
int count, max;
|
||||||
|
if(!MyClient(client_p))
|
||||||
|
{
|
||||||
|
if(EmptyString(client_p->sockhost) || !strcmp(client_p->sockhost, "0"))
|
||||||
|
return -1;
|
||||||
|
if(!rb_inet_pton_sock(client_p->sockhost, (struct sockaddr *)&ip))
|
||||||
|
return -1;
|
||||||
|
addr = (struct sockaddr *)&ip;
|
||||||
|
} else
|
||||||
|
addr = (struct sockaddr *)&client_p->localClient->ip;
|
||||||
|
count = get_global_count(addr);
|
||||||
|
#ifdef RB_IPV6
|
||||||
|
if(GET_SS_FAMILY(addr) == AF_INET6)
|
||||||
|
max = ConfigFileEntry.global_cidr_ipv6_count;
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
max = ConfigFileEntry.global_cidr_ipv4_count;
|
||||||
|
if(count >= max)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
clear_cidr_tree(void *data)
|
||||||
|
{
|
||||||
|
rb_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rehash_global_cidr_tree(void)
|
||||||
|
{
|
||||||
|
struct Client *client_p;
|
||||||
|
rb_dlink_node *ptr;
|
||||||
|
rb_clear_patricia(global_tree, clear_cidr_tree);
|
||||||
|
RB_DLINK_FOREACH(ptr, global_client_list.head)
|
||||||
|
{
|
||||||
|
client_p = ptr->data;
|
||||||
|
if(IsMe(client_p) && IsServer(client_p))
|
||||||
|
continue;
|
||||||
|
inc_global_cidr_count(client_p);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue