[mod_evasive] use config_plugin_values_init()

This commit is contained in:
Glenn Strauss 2019-10-27 04:19:52 -04:00
parent b07bc5d266
commit ca95cea4b5
1 changed files with 65 additions and 99 deletions

View File

@ -27,130 +27,96 @@
*/
typedef struct {
unsigned short max_conns;
unsigned short silent;
buffer *location;
unsigned short max_conns;
unsigned short silent;
const buffer *location;
} plugin_config;
typedef struct {
PLUGIN_DATA;
plugin_config **config_storage;
plugin_config conf;
PLUGIN_DATA;
plugin_config defaults;
plugin_config conf;
} plugin_data;
INIT_FUNC(mod_evasive_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
return p;
return calloc(1, sizeof(plugin_data));
}
FREE_FUNC(mod_evasive_free) {
plugin_data *p = p_d;
plugin_data *p = p_d;
if (!p) return HANDLER_GO_ON;
UNUSED(srv);
UNUSED(srv);
free(p->cvlist);
free(p);
if (!p) return HANDLER_GO_ON;
return HANDLER_GO_ON;
}
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
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;
}
}
if (NULL == s) continue;
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);
}
buffer_free(s->location);
free(s);
}
free(p->config_storage);
}
free(p);
return HANDLER_GO_ON;
static void mod_evasive_patch_config(connection * const con, plugin_data * const p) {
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
for (int i = 1, used = p->nconfig; i < used; ++i) {
if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id))
mod_evasive_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
}
}
SETDEFAULTS_FUNC(mod_evasive_set_defaults) {
plugin_data *p = p_d;
size_t i = 0;
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 }
};
config_values_t cv[] = {
{ "evasive.max-conns-per-ip", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "evasive.silent", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ "evasive.location", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
{ NULL, NULL, 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;
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
/* 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);
}
for (i = 0; i < srv->config_context->used; i++) {
data_config const* config = (data_config const*)srv->config_context->data[i];
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
s->max_conns = 0;
s->silent = 0;
s->location = buffer_init();
cv[0].destination = &(s->max_conns);
cv[1].destination = &(s->silent);
cv[2].destination = s->location;
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
return HANDLER_ERROR;
}
}
return HANDLER_GO_ON;
return HANDLER_GO_ON;
}
#define PATCH(x) \
p->conf.x = s->x;
static int mod_evasive_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
PATCH(max_conns);
PATCH(silent);
PATCH(location);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
if (!config_check_cond(con, i)) continue; /* condition not matched */
data_config *dc = (data_config *)srv->config_context->data[i];
s = p->config_storage[i];
/* merge config */
for (j = 0; j < dc->value->used; j++) {
data_unset *du = dc->value->data[j];
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("evasive.max-conns-per-ip"))) {
PATCH(max_conns);
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("evasive.silent"))) {
PATCH(silent);
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("evasive.location"))) {
PATCH(location);
}
}
}
return 0;
}
#undef PATCH
URIHANDLER_FUNC(mod_evasive_uri_handler) {
plugin_data *p = p_d;
if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
mod_evasive_patch_connection(srv, con, p);
mod_evasive_patch_config(con, p);
/* no limit set, nothing to block */
if (p->conf.max_conns == 0) return HANDLER_GO_ON;