2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-12-20 14:30:41 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2017-10-29 05:23:19 +00:00
|
|
|
#include "sock_addr.h"
|
2005-12-20 14:30:41 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-12-20 14:30:41 +00:00
|
|
|
/**
|
|
|
|
* mod_evasive
|
|
|
|
*
|
|
|
|
* we indent to implement all features the mod_evasive from apache has
|
|
|
|
*
|
|
|
|
* - limit of connections per IP
|
|
|
|
* - provide a list of block-listed ip/networks (no access)
|
|
|
|
* - provide a white-list of ips/network which is not affected by the limit
|
|
|
|
* (hmm, conditionals might be enough)
|
|
|
|
* - provide a bandwidth limiter per IP
|
|
|
|
*
|
|
|
|
* started by:
|
|
|
|
* - w1zzard@techpowerup.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-27 08:19:52 +00:00
|
|
|
unsigned short max_conns;
|
|
|
|
unsigned short silent;
|
|
|
|
const buffer *location;
|
2005-12-20 14:30:41 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-27 08:19:52 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2005-12-20 14:30:41 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_evasive_init) {
|
2019-10-27 08:19:52 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2005-12-20 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 08:19:52 +00:00
|
|
|
static void mod_evasive_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* evasive.max-conns-per-ip */
|
|
|
|
pconf->max_conns = cpv->v.shrt;
|
|
|
|
break;
|
|
|
|
case 1: /* evasive.silent */
|
|
|
|
pconf->silent = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 2: /* evasive.location */
|
|
|
|
pconf->location = cpv->v.b;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-27 08:19:52 +00:00
|
|
|
static void mod_evasive_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_evasive_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_evasive_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-10-27 08:19:52 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-10-27 08:19:52 +00:00
|
|
|
mod_evasive_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
2005-12-20 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 08:19:52 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_evasive_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("evasive.max-conns-per-ip"),
|
|
|
|
T_CONFIG_SHORT,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("evasive.silent"),
|
|
|
|
T_CONFIG_BOOL,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("evasive.location"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_evasive"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_evasive_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-12-20 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_evasive_uri_handler) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_evasive_patch_config(r, p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/* no limit set, nothing to block */
|
2005-12-20 14:30:41 +00:00
|
|
|
if (p->conf.max_conns == 0) return HANDLER_GO_ON;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
sock_addr * const dst_addr = &r->con->dst_addr;
|
|
|
|
const connections * const conns = &r->con->srv->conns;
|
2019-11-26 07:13:05 +00:00
|
|
|
for (uint32_t i = 0, conns_by_ip = 0; i < conns->used; ++i) {
|
|
|
|
connection *c = conns->ptr[i];
|
2005-12-20 14:30:41 +00:00
|
|
|
|
2006-01-04 23:30:07 +00:00
|
|
|
/* check if other connections are already actively serving data for the same IP
|
|
|
|
* we can only ban connections which are already behind the 'read request' state
|
|
|
|
* */
|
2020-01-10 05:17:12 +00:00
|
|
|
if (c->request.state <= CON_STATE_REQUEST_END) continue;
|
2008-07-28 08:43:19 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!sock_addr_is_addr_eq(&c->dst_addr, dst_addr)) continue;
|
2008-07-28 08:43:19 +00:00
|
|
|
conns_by_ip++;
|
|
|
|
|
|
|
|
if (conns_by_ip > p->conf.max_conns) {
|
2009-04-26 18:29:14 +00:00
|
|
|
if (!p->conf.silent) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"%s turned away. Too many connections.",
|
2020-01-13 02:51:12 +00:00
|
|
|
r->con->dst_addr_buf->ptr);
|
2009-04-26 18:29:14 +00:00
|
|
|
}
|
2008-07-28 08:43:19 +00:00
|
|
|
|
2016-05-12 05:11:48 +00:00
|
|
|
if (!buffer_is_empty(p->conf.location)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->conf.location));
|
|
|
|
r->http_status = 302;
|
|
|
|
r->resp_body_finished = 1;
|
2016-05-12 05:11:48 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->http_status = 403;
|
2016-05-12 05:11:48 +00:00
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
r->handler_module = NULL;
|
2008-07-28 08:43:19 +00:00
|
|
|
return HANDLER_FINISHED;
|
2005-12-20 14:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-12-20 14:30:41 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_evasive_plugin_init(plugin *p);
|
2005-12-20 14:30:41 +00:00
|
|
|
int mod_evasive_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "evasive";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-12-20 14:30:41 +00:00
|
|
|
p->init = mod_evasive_init;
|
|
|
|
p->set_defaults = mod_evasive_set_defaults;
|
|
|
|
p->handle_uri_clean = mod_evasive_uri_handler;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-12-20 14:30:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|