2008-04-02 15:37:50 +00:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd
|
|
|
|
* reject.c: reject users with prejudice
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
|
|
|
|
* Copyright (C) 2003-2005 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2008-07-31 23:59:08 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
2008-04-02 15:37:50 +00:00
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "s_conf.h"
|
|
|
|
#include "reject.h"
|
|
|
|
#include "s_stats.h"
|
2008-07-31 23:59:08 +00:00
|
|
|
#include "ircd.h"
|
|
|
|
#include "send.h"
|
|
|
|
#include "numeric.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "hostmask.h"
|
|
|
|
#include "match.h"
|
2008-04-02 15:37:50 +00:00
|
|
|
#include "hash.h"
|
|
|
|
|
2008-04-02 18:01:18 +00:00
|
|
|
static rb_patricia_tree_t *reject_tree;
|
2008-07-31 23:59:08 +00:00
|
|
|
static rb_dlink_list delay_exit;
|
2008-04-02 15:37:50 +00:00
|
|
|
static rb_dlink_list reject_list;
|
2008-07-31 23:59:08 +00:00
|
|
|
static rb_dlink_list throttle_list;
|
|
|
|
static rb_patricia_tree_t *throttle_tree;
|
|
|
|
static void throttle_expires(void *unused);
|
2008-04-02 15:37:50 +00:00
|
|
|
|
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
typedef struct _reject_data
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
|
|
|
rb_dlink_node rnode;
|
2019-12-31 01:11:45 +00:00
|
|
|
struct ConfItem *aconf;
|
|
|
|
const char *reason;
|
2008-04-02 15:37:50 +00:00
|
|
|
time_t time;
|
|
|
|
unsigned int count;
|
|
|
|
uint32_t mask_hashv;
|
2008-07-31 23:59:08 +00:00
|
|
|
} reject_t;
|
|
|
|
|
|
|
|
typedef struct _delay_data
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
rb_fde_t *F;
|
2019-12-31 01:11:45 +00:00
|
|
|
struct ConfItem *aconf;
|
|
|
|
const char *reason;
|
2023-06-15 22:32:12 +00:00
|
|
|
bool ssl;
|
2008-07-31 23:59:08 +00:00
|
|
|
} delay_t;
|
|
|
|
|
|
|
|
typedef struct _throttle
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
time_t last;
|
|
|
|
int count;
|
|
|
|
} throttle_t;
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
delay_exit_length(void)
|
|
|
|
{
|
|
|
|
return rb_dlink_list_length(&delay_exit);
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2019-12-31 01:11:45 +00:00
|
|
|
static void
|
|
|
|
reject_free(reject_t *rdata)
|
|
|
|
{
|
|
|
|
struct ConfItem *aconf = rdata->aconf;
|
|
|
|
|
|
|
|
if (aconf)
|
|
|
|
deref_conf(aconf);
|
|
|
|
|
|
|
|
rb_free(rdata);
|
|
|
|
}
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
static void
|
|
|
|
reject_exit(void *unused)
|
|
|
|
{
|
2019-12-31 01:11:45 +00:00
|
|
|
static char dynamic_reason[BUFSIZE];
|
2008-04-02 15:37:50 +00:00
|
|
|
rb_dlink_node *ptr, *ptr_next;
|
2008-07-31 23:59:08 +00:00
|
|
|
delay_t *ddata;
|
|
|
|
static const char *errbuf = "ERROR :Closing Link: (*** Banned (cache))\r\n";
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2023-06-15 22:32:12 +00:00
|
|
|
static const unsigned char ssldeniederrcode[] = {
|
|
|
|
// SSLv3.0 Fatal Alert: Access Denied
|
|
|
|
0x15, 0x03, 0x00, 0x00, 0x02, 0x02, 0x31
|
|
|
|
};
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
|
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
ddata = ptr->data;
|
|
|
|
|
2023-06-15 22:32:12 +00:00
|
|
|
if (ddata->ssl)
|
2019-12-31 01:11:45 +00:00
|
|
|
{
|
2023-06-15 22:32:12 +00:00
|
|
|
rb_write(ddata->F, ssldeniederrcode, sizeof(ssldeniederrcode));
|
2019-12-31 01:11:45 +00:00
|
|
|
}
|
2023-06-15 22:32:12 +00:00
|
|
|
else
|
2019-12-31 01:11:45 +00:00
|
|
|
{
|
2023-06-15 22:32:12 +00:00
|
|
|
*dynamic_reason = '\0';
|
|
|
|
|
|
|
|
if (ddata->aconf)
|
|
|
|
snprintf(dynamic_reason, sizeof dynamic_reason, form_str(ERR_YOUREBANNEDCREEP) "\r\n",
|
|
|
|
me.name, "*", get_user_ban_reason(ddata->aconf));
|
|
|
|
else if (ddata->reason)
|
|
|
|
snprintf(dynamic_reason, sizeof dynamic_reason, ":%s 465 %s :%s\r\n",
|
|
|
|
me.name, "*", ddata->reason);
|
|
|
|
|
|
|
|
if (*dynamic_reason)
|
|
|
|
rb_write(ddata->F, dynamic_reason, strlen(dynamic_reason));
|
|
|
|
|
|
|
|
rb_write(ddata->F, errbuf, strlen(errbuf));
|
2019-12-31 01:11:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 22:32:12 +00:00
|
|
|
if (ddata->aconf)
|
|
|
|
deref_conf(ddata->aconf);
|
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
rb_close(ddata->F);
|
|
|
|
rb_free(ddata);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
delay_exit.head = delay_exit.tail = NULL;
|
|
|
|
delay_exit.length = 0;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reject_expires(void *unused)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
|
|
|
{
|
|
|
|
pnode = ptr->data;
|
2014-03-03 04:25:47 +00:00
|
|
|
rdata = pnode->data;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
|
|
|
if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rb_dlinkDelete(ptr, &reject_list);
|
2019-12-31 01:11:45 +00:00
|
|
|
reject_free(rdata);
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_remove(reject_tree, pnode);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_reject(void)
|
|
|
|
{
|
2008-04-02 18:01:18 +00:00
|
|
|
reject_tree = rb_new_patricia(PATRICIA_BITS);
|
2008-07-31 23:59:08 +00:00
|
|
|
throttle_tree = rb_new_patricia(PATRICIA_BITS);
|
2008-04-02 15:37:50 +00:00
|
|
|
rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
|
|
|
|
rb_event_add("reject_expires", reject_expires, NULL, 60);
|
2008-07-31 23:59:08 +00:00
|
|
|
rb_event_add("throttle_expires", throttle_expires, NULL, 10);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2008-12-16 20:02:04 +00:00
|
|
|
unsigned long
|
|
|
|
throttle_size(void)
|
|
|
|
{
|
2008-12-16 20:10:09 +00:00
|
|
|
unsigned long count;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
rb_patricia_node_t *pnode;
|
|
|
|
throttle_t *t;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
RB_DLINK_FOREACH(ptr, throttle_list.head)
|
|
|
|
{
|
|
|
|
pnode = ptr->data;
|
|
|
|
t = pnode->data;
|
|
|
|
if (t->count > ConfigFileEntry.throttle_count)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2008-12-16 20:02:04 +00:00
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
|
|
|
void
|
2019-12-31 01:11:45 +00:00
|
|
|
add_reject(struct Client *client_p, const char *mask1, const char *mask2, struct ConfItem *aconf, const char *reason)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata;
|
2008-04-02 15:37:50 +00:00
|
|
|
uint32_t hashv;
|
|
|
|
|
|
|
|
/* Reject is disabled */
|
2008-07-31 23:59:08 +00:00
|
|
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
hashv = 0;
|
|
|
|
if (mask1 != NULL)
|
2008-07-31 23:59:08 +00:00
|
|
|
hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
|
2008-04-02 15:37:50 +00:00
|
|
|
if (mask2 != NULL)
|
2008-07-31 23:59:08 +00:00
|
|
|
hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2008-04-02 18:01:18 +00:00
|
|
|
if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
|
|
|
rdata = pnode->data;
|
|
|
|
rdata->time = rb_current_time();
|
|
|
|
rdata->count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int bitlen = 32;
|
2008-07-31 23:59:08 +00:00
|
|
|
if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
|
2008-04-02 15:37:50 +00:00
|
|
|
bitlen = 128;
|
|
|
|
pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
|
2008-07-31 23:59:08 +00:00
|
|
|
pnode->data = rdata = rb_malloc(sizeof(reject_t));
|
2008-04-02 15:37:50 +00:00
|
|
|
rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
|
|
|
|
rdata->time = rb_current_time();
|
|
|
|
rdata->count = 1;
|
2019-12-31 01:11:45 +00:00
|
|
|
rdata->aconf = NULL;
|
|
|
|
rdata->reason = NULL;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
rdata->mask_hashv = hashv;
|
2019-12-31 01:11:45 +00:00
|
|
|
|
|
|
|
if (aconf != NULL && aconf != rdata->aconf && (aconf->status & CONF_KILL) && aconf->passwd)
|
|
|
|
{
|
|
|
|
if (rdata->aconf != NULL)
|
|
|
|
deref_conf(rdata->aconf);
|
|
|
|
aconf->clients++;
|
|
|
|
rdata->aconf = aconf;
|
|
|
|
}
|
|
|
|
else if (reason != NULL)
|
|
|
|
{
|
|
|
|
if (rdata->aconf != NULL)
|
|
|
|
deref_conf(rdata->aconf);
|
|
|
|
rdata->aconf = NULL;
|
|
|
|
rdata->reason = reason;
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-06-15 22:32:12 +00:00
|
|
|
check_reject(rb_fde_t *F, struct sockaddr *addr, bool ssl)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata;
|
|
|
|
delay_t *ddata;
|
2020-01-03 17:07:25 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
/* Reject is disabled */
|
2020-01-03 17:07:25 +00:00
|
|
|
if (ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return 0;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
pnode = rb_match_ip(reject_tree, addr);
|
2020-01-03 17:07:25 +00:00
|
|
|
if (pnode == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rdata = pnode->data;
|
|
|
|
rdata->time = rb_current_time();
|
|
|
|
|
|
|
|
if (rdata->count <= (unsigned long)ConfigFileEntry.reject_after_count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (rdata->aconf != NULL && rdata->aconf->status & CONF_ILLEGAL)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2020-01-03 17:07:25 +00:00
|
|
|
rb_dlinkDelete(&rdata->rnode, &reject_list);
|
|
|
|
reject_free(rdata);
|
|
|
|
rb_patricia_remove(reject_tree, pnode);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2020-01-03 17:07:25 +00:00
|
|
|
ddata = rb_malloc(sizeof(delay_t));
|
|
|
|
ServerStats.is_rej++;
|
|
|
|
rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL);
|
|
|
|
if (rdata->aconf)
|
|
|
|
{
|
|
|
|
ddata->aconf = rdata->aconf;
|
|
|
|
ddata->aconf->clients++;
|
|
|
|
ddata->reason = NULL;
|
2014-03-03 04:25:47 +00:00
|
|
|
}
|
2020-01-03 17:07:25 +00:00
|
|
|
else if (rdata->reason)
|
|
|
|
{
|
|
|
|
ddata->reason = rdata->reason;
|
|
|
|
ddata->aconf = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ddata->aconf = NULL;
|
|
|
|
ddata->reason = NULL;
|
|
|
|
}
|
|
|
|
ddata->F = F;
|
2023-06-15 22:32:12 +00:00
|
|
|
ddata->ssl = ssl;
|
2020-01-03 17:07:25 +00:00
|
|
|
rb_dlinkAdd(ddata, &ddata->node, &delay_exit);
|
|
|
|
return 1;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2008-12-19 18:24:12 +00:00
|
|
|
int
|
|
|
|
is_reject_ip(struct sockaddr *addr)
|
|
|
|
{
|
|
|
|
rb_patricia_node_t *pnode;
|
|
|
|
reject_t *rdata;
|
|
|
|
int duration;
|
|
|
|
|
|
|
|
/* Reject is disabled */
|
|
|
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
|
|
|
return 0;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-12-19 18:24:12 +00:00
|
|
|
pnode = rb_match_ip(reject_tree, addr);
|
|
|
|
if(pnode != NULL)
|
|
|
|
{
|
|
|
|
rdata = pnode->data;
|
|
|
|
|
|
|
|
if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
|
|
|
|
{
|
|
|
|
duration = rdata->time + ConfigFileEntry.reject_duration - rb_current_time();
|
|
|
|
return duration > 0 ? duration : 1;
|
|
|
|
}
|
2014-03-03 04:25:47 +00:00
|
|
|
}
|
2008-12-19 18:24:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
void
|
2008-04-02 15:37:50 +00:00
|
|
|
flush_reject(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
|
|
|
{
|
|
|
|
pnode = ptr->data;
|
|
|
|
rdata = pnode->data;
|
|
|
|
rb_dlinkDelete(ptr, &reject_list);
|
2019-12-31 01:11:45 +00:00
|
|
|
reject_free(rdata);
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_remove(reject_tree, pnode);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
int
|
2008-04-02 15:37:50 +00:00
|
|
|
remove_reject_ip(const char *ip)
|
|
|
|
{
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
/* Reject is disabled */
|
2008-07-31 23:59:08 +00:00
|
|
|
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-04-02 18:01:18 +00:00
|
|
|
if((pnode = rb_match_string(reject_tree, ip)) != NULL)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata = pnode->data;
|
2008-04-02 15:37:50 +00:00
|
|
|
rb_dlinkDelete(&rdata->rnode, &reject_list);
|
2019-12-31 01:11:45 +00:00
|
|
|
reject_free(rdata);
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_remove(reject_tree, pnode);
|
2008-04-02 15:37:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
remove_reject_mask(const char *mask1, const char *mask2)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
reject_t *rdata;
|
2008-04-02 15:37:50 +00:00
|
|
|
uint32_t hashv;
|
|
|
|
int n = 0;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
hashv = 0;
|
|
|
|
if (mask1 != NULL)
|
2008-07-31 23:59:08 +00:00
|
|
|
hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
|
2008-04-02 15:37:50 +00:00
|
|
|
if (mask2 != NULL)
|
2008-07-31 23:59:08 +00:00
|
|
|
hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
|
2008-04-02 15:37:50 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
|
|
|
|
{
|
|
|
|
pnode = ptr->data;
|
|
|
|
rdata = pnode->data;
|
|
|
|
if (rdata->mask_hashv == hashv)
|
|
|
|
{
|
|
|
|
rb_dlinkDelete(ptr, &reject_list);
|
2019-12-31 01:11:45 +00:00
|
|
|
reject_free(rdata);
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_remove(reject_tree, pnode);
|
2008-04-02 15:37:50 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-07-31 23:59:08 +00:00
|
|
|
throttle_add(struct sockaddr *addr)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
throttle_t *t;
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
t = pnode->data;
|
|
|
|
|
|
|
|
if(t->count > ConfigFileEntry.throttle_count)
|
2008-12-16 20:02:04 +00:00
|
|
|
{
|
|
|
|
ServerStats.is_thr++;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-07-31 23:59:08 +00:00
|
|
|
/* Stop penalizing them after they've been throttled */
|
|
|
|
t->last = rb_current_time();
|
|
|
|
t->count++;
|
|
|
|
|
|
|
|
} else {
|
2008-04-02 15:37:50 +00:00
|
|
|
int bitlen = 32;
|
2008-07-31 23:59:08 +00:00
|
|
|
if(GET_SS_FAMILY(addr) == AF_INET6)
|
2008-04-02 15:37:50 +00:00
|
|
|
bitlen = 128;
|
2014-03-03 04:25:47 +00:00
|
|
|
t = rb_malloc(sizeof(throttle_t));
|
2008-07-31 23:59:08 +00:00
|
|
|
t->last = rb_current_time();
|
|
|
|
t->count = 1;
|
|
|
|
pnode = make_and_lookup_ip(throttle_tree, addr, bitlen);
|
|
|
|
pnode->data = t;
|
2014-03-03 04:25:47 +00:00
|
|
|
rb_dlinkAdd(pnode, &t->node, &throttle_list);
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-19 18:24:12 +00:00
|
|
|
int
|
|
|
|
is_throttle_ip(struct sockaddr *addr)
|
|
|
|
{
|
|
|
|
throttle_t *t;
|
|
|
|
rb_patricia_node_t *pnode;
|
|
|
|
int duration;
|
|
|
|
|
|
|
|
if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
|
|
|
|
{
|
|
|
|
t = pnode->data;
|
|
|
|
if(t->count > ConfigFileEntry.throttle_count)
|
|
|
|
{
|
|
|
|
duration = t->last + ConfigFileEntry.throttle_duration - rb_current_time();
|
|
|
|
return duration > 0 ? duration : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
void
|
2009-03-07 00:49:09 +00:00
|
|
|
flush_throttle(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
rb_patricia_node_t *pnode;
|
|
|
|
throttle_t *t;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2009-03-07 00:49:09 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
|
|
|
|
{
|
|
|
|
pnode = ptr->data;
|
2014-03-03 04:25:47 +00:00
|
|
|
t = pnode->data;
|
2009-03-07 00:49:09 +00:00
|
|
|
|
|
|
|
rb_dlinkDelete(ptr, &throttle_list);
|
|
|
|
rb_free(t);
|
|
|
|
rb_patricia_remove(throttle_tree, pnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
static void
|
|
|
|
throttle_expires(void *unused)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
rb_dlink_node *ptr, *next;
|
2008-04-02 18:01:18 +00:00
|
|
|
rb_patricia_node_t *pnode;
|
2008-07-31 23:59:08 +00:00
|
|
|
throttle_t *t;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-07-31 23:59:08 +00:00
|
|
|
pnode = ptr->data;
|
2014-03-03 04:25:47 +00:00
|
|
|
t = pnode->data;
|
2008-07-31 23:59:08 +00:00
|
|
|
|
|
|
|
if(t->last + ConfigFileEntry.throttle_duration > rb_current_time())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rb_dlinkDelete(ptr, &throttle_list);
|
|
|
|
rb_free(t);
|
|
|
|
rb_patricia_remove(throttle_tree, pnode);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-31 23:59:08 +00:00
|
|
|
|