Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Feature] Implement more strict configuration tests
  • Loading branch information
vstakhov committed Dec 3, 2018
1 parent 5f7111c commit 2bcf327
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 14 deletions.
68 changes: 68 additions & 0 deletions src/libserver/cfg_rcl.c
Expand Up @@ -381,40 +381,108 @@ rspamd_rcl_symbol_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
nshots = cfg->default_max_shots;

if ((elt = ucl_object_lookup (obj, "one_shot")) != NULL) {
if (ucl_object_type (elt) != UCL_BOOLEAN) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"one_shot attribute is not boolean for symbol: '%s'",
key);

return FALSE;
}
if (ucl_object_toboolean (elt)) {
nshots = 1;
}
}

if ((elt = ucl_object_lookup (obj, "any_shot")) != NULL) {
if (ucl_object_type (elt) != UCL_BOOLEAN) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"any_shot attribute is not boolean for symbol: '%s'",
key);

return FALSE;
}
if (ucl_object_toboolean (elt)) {
nshots = -1;
}
}

if ((elt = ucl_object_lookup (obj, "one_param")) != NULL) {
if (ucl_object_type (elt) != UCL_BOOLEAN) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"one_param attribute is not boolean for symbol: '%s'",
key);

return FALSE;
}

if (ucl_object_toboolean (elt)) {
flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
}
}

if ((elt = ucl_object_lookup (obj, "ignore")) != NULL) {
if (ucl_object_type (elt) != UCL_BOOLEAN) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"ignore attribute is not boolean for symbol: '%s'",
key);

return FALSE;
}

if (ucl_object_toboolean (elt)) {
flags |= RSPAMD_SYMBOL_FLAG_IGNORE;
}
}

if ((elt = ucl_object_lookup (obj, "nshots")) != NULL) {
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"nshots attribute is not numeric for symbol: '%s'",
key);

return FALSE;
}

nshots = ucl_object_toint (elt);
}

elt = ucl_object_lookup_any (obj, "score", "weight", NULL);
if (elt) {
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"score attribute is not numeric for symbol: '%s'",
key);

return FALSE;
}

score = ucl_object_todouble (elt);
}

elt = ucl_object_lookup (obj, "priority");
if (elt) {
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"priority attribute is not numeric for symbol: '%s'",
key);

return FALSE;
}

priority = ucl_object_toint (elt);
}
else {
Expand Down
19 changes: 15 additions & 4 deletions src/libserver/cfg_utils.c
Expand Up @@ -1470,6 +1470,7 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
module_t *mod, **pmod;
guint i = 0;
struct module_ctx *mod_ctx, *cur_ctx;
gboolean ret = TRUE;

/* Init all compiled modules */

Expand Down Expand Up @@ -1504,11 +1505,19 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
mod_ctx->enabled = rspamd_config_is_module_enabled (cfg, mod->name);

if (reconfig) {
(void)mod->module_reconfig_func (cfg);
msg_info_config ("reconfig of %s", mod->name);
if (!mod->module_reconfig_func (cfg)) {
msg_err_config ("reconfig of %s failed!", mod->name);
}
else {
msg_info_config ("reconfig of %s", mod->name);
}

}
else {
(void)mod->module_config_func (cfg);
if (!mod->module_config_func (cfg)) {
msg_info_config ("config of %s failed!", mod->name);
ret = FALSE;
}
}
}

Expand All @@ -1519,7 +1528,9 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
cur = g_list_next (cur);
}

return rspamd_init_lua_filters (cfg, 0);
ret = rspamd_init_lua_filters (cfg, 0) && ret;

return ret;
}

static void
Expand Down
75 changes: 66 additions & 9 deletions src/plugins/regexp.c
Expand Up @@ -279,39 +279,96 @@ regexp_module_config (struct rspamd_config *cfg)
elt = ucl_object_lookup (value, "score");

if (elt) {
score = ucl_object_todouble (elt);
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
msg_err_config (
"score attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
score = ucl_object_todouble (elt);
}
}

elt = ucl_object_lookup (value, "one_shot");

if (elt) {
if (ucl_object_toboolean (elt)) {
nshots = 1;
if (ucl_object_type (elt) != UCL_BOOLEAN) {
msg_err_config (
"one_shot attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
if (ucl_object_toboolean (elt)) {
nshots = 1;
}
}
}

if ((elt = ucl_object_lookup (value, "any_shot")) != NULL) {
if (ucl_object_toboolean (elt)) {
nshots = -1;
if (ucl_object_type (elt) != UCL_BOOLEAN) {
msg_err_config (
"any_shot attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
if (ucl_object_toboolean (elt)) {
nshots = -1;
}
}
}

if ((elt = ucl_object_lookup (value, "nshots")) != NULL) {
nshots = ucl_object_toint (elt);
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
msg_err_config (
"nshots attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
nshots = ucl_object_toint (elt);
}
}

elt = ucl_object_lookup (value, "one_param");

if (elt) {
if (ucl_object_toboolean (elt)) {
flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
if (ucl_object_type (elt) != UCL_BOOLEAN) {
msg_err_config (
"one_param attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
if (ucl_object_toboolean (elt)) {
flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM;
}
}
}

elt = ucl_object_lookup (value, "priority");

if (elt) {
priority = ucl_object_toint (elt);
if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) {
msg_err_config (
"priority attribute is not numeric for symbol: '%s'",
cur_item->symbol);

res = FALSE;
}
else {
priority = ucl_object_toint (elt);
}
}
else {
priority = ucl_object_get_priority (value) + 1;
}

rspamd_config_add_symbol (cfg, cur_item->symbol,
Expand Down
2 changes: 1 addition & 1 deletion src/rspamadm/configtest.c
Expand Up @@ -156,7 +156,7 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
ret = rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
}

if (!rspamd_symcache_validate (rspamd_main->cfg->cache,
if (ret && !rspamd_symcache_validate (rspamd_main->cfg->cache,
rspamd_main->cfg,
FALSE)) {
ret = FALSE;
Expand Down

0 comments on commit 2bcf327

Please sign in to comment.