commit 3d5281e: [Minor] Settings: Add preliminary policies support

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jun 24 11:35:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-06-24 12:27:58 +0100
URL: https://github.com/rspamd/rspamd/commit/3d5281ec7b5f97763ec92555492a322127dc4bb4

[Minor] Settings: Add preliminary policies support

---
 src/libserver/cfg_file.h  | 10 +++++++++-
 src/libserver/cfg_utils.c | 11 ++++++++---
 src/lua/lua_config.c      | 35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 07e66b826..f6b1adad6 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -308,8 +308,15 @@ struct rspamd_config_post_init_script {
 
 struct rspamd_lang_detector;
 
+enum rspamd_config_settings_policy {
+	RSPAMD_SETTINGS_POLICY_DEFAULT = 0,
+	RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW = 1,
+	RSPAMD_SETTINGS_POLICY_IMPLICIT_DENY = 2,
+};
+
 struct rspamd_config_settings_elt {
 	guint32 id;
+	enum rspamd_config_settings_policy policy;
 	const gchar *name;
 	ucl_object_t *symbols_enabled;
 	ucl_object_t *symbols_disabled;
@@ -744,7 +751,8 @@ gboolean rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
 void rspamd_config_register_settings_id (struct rspamd_config *cfg,
 										 const gchar *name,
 										 ucl_object_t *symbols_enabled,
-										 ucl_object_t *symbols_disabled);
+										 ucl_object_t *symbols_disabled,
+										 enum rspamd_config_settings_policy policy);
 
 /**
  * Convert settings name to settings id
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index ad61f5777..6e244902a 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -2404,9 +2404,10 @@ struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref (
 
 void
 rspamd_config_register_settings_id (struct rspamd_config *cfg,
-										 const gchar *name,
-										 ucl_object_t *symbols_enabled,
-										 ucl_object_t *symbols_disabled)
+									const gchar *name,
+									ucl_object_t *symbols_enabled,
+									ucl_object_t *symbols_disabled,
+									enum rspamd_config_settings_policy policy)
 {
 	struct rspamd_config_settings_elt *elt;
 	guint32 id;
@@ -2433,6 +2434,8 @@ rspamd_config_register_settings_id (struct rspamd_config *cfg,
 			nelt->symbols_disabled = ucl_object_ref (symbols_disabled);
 		}
 
+		nelt->policy = policy;
+
 		REF_INIT_RETAIN (nelt, rspamd_config_settings_elt_dtor);
 		msg_warn_config ("replace settings id %ud (%s)", id, name);
 		rspamd_symcache_process_settings_elt (cfg->cache, elt);
@@ -2460,6 +2463,8 @@ rspamd_config_register_settings_id (struct rspamd_config *cfg,
 			elt->symbols_disabled = ucl_object_ref (symbols_disabled);
 		}
 
+		elt->policy = policy;
+
 		msg_info_config ("register new settings id %ud (%s)", id, name);
 		REF_INIT_RETAIN (elt, rspamd_config_settings_elt_dtor);
 		rspamd_symcache_process_settings_elt (cfg->cache, elt);
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 9f7952cc3..8948dd3ae 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -3475,6 +3475,7 @@ lua_config_register_settings_id (lua_State *L)
 
 	if (cfg != NULL && settings_name) {
 		ucl_object_t *sym_enabled, *sym_disabled;
+		enum rspamd_config_settings_policy policy = RSPAMD_SETTINGS_POLICY_DEFAULT;
 
 		sym_enabled = ucl_object_lua_import (L, 3);
 
@@ -3493,8 +3494,40 @@ lua_config_register_settings_id (lua_State *L)
 			return luaL_error (L, "invalid symbols enabled");
 		}
 
+		/* Check policy */
+		if (lua_isstring (L, 5)) {
+			const gchar *policy_str = lua_tostring (L, 5);
+
+			if (strcmp (policy_str, "default") == 0) {
+				policy = RSPAMD_SETTINGS_POLICY_DEFAULT;
+			}
+			else if (strcmp (policy_str, "implicit_allow") == 0) {
+				policy = RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW;
+			}
+			else if (strcmp (policy_str, "implicit_deny") == 0) {
+				policy = RSPAMD_SETTINGS_POLICY_IMPLICIT_DENY;
+			}
+			else {
+				return luaL_error (L, "invalid settings policy: %s", policy_str);
+			}
+		}
+		else {
+			/* Apply heuristic */
+			if (!sym_enabled) {
+				policy = RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW;
+			}
+		}
+
 		rspamd_config_register_settings_id (cfg, settings_name, sym_enabled,
-				sym_disabled);
+				sym_disabled, policy);
+
+		if (sym_enabled) {
+			ucl_object_unref (sym_enabled);
+		}
+
+		if (sym_disabled) {
+			ucl_object_unref (sym_disabled);
+		}
 	}
 	else {
 		return luaL_error (L, "invalid arguments");


More information about the Commits mailing list