]> source.dussan.org Git - rspamd.git/commitdiff
Add universal routine to check if a modules is enabled.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 6 Aug 2015 13:57:00 +0000 (14:57 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 6 Aug 2015 13:57:00 +0000 (14:57 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c

index 94db0de1b57292fd8653f24230c27dde310e7950..37d65bd74ec33b06eab2e3aeccb7bf975a93483c 100644 (file)
@@ -78,6 +78,8 @@ struct rspamd_symbols_group {
        gchar *name;
        struct rspamd_symbol_def *symbols;
        gdouble max_score;
+       gboolean disabled;
+       gboolean one_shot;
 };
 
 /**
@@ -263,6 +265,7 @@ struct rspamd_config {
        guint32 min_word_len;                                                   /**< minimum length of the word to be considered                */
 
        GList *classify_headers;                                                /**< list of headers using for statistics                               */
+       module_t *compiled_modules;                                             /**< list of compiled C modules                                                 */
 };
 
 
@@ -422,6 +425,22 @@ gboolean rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
                const gchar *symbol, gdouble score, const gchar *description,
                const gchar *group, gboolean one_shot, gboolean rewrite_existing);
 
+/**
+ * Checks if a specified C or lua module is enabled or disabled in the config.
+ * The logic of check is the following:
+ *
+ * - For C modules, we check `filters` line and enable module only if it is found there
+ * - For LUA modules we check the corresponding configuration section:
+ *   - if section exists, then we check `enabled` key and check its value
+ *   - if section is absent, we consider module as disabled
+ * - For both C and LUA modules we check if the group with the module name is disabled in the default metric
+ * @param cfg config file
+ * @param module_name module name
+ * @return TRUE if a module is enabled
+ */
+gboolean rspamd_config_is_module_enabled (struct rspamd_config *cfg,
+               const gchar *module_name);
+
 #endif /* ifdef CFG_FILE_H */
 /*
  * vi:ts=4
index a14a4314686410042da73ed20e40d771d603c4aa..46dd1b60fc844c70cf2d7242cbf620b356a0f3a6 100644 (file)
@@ -250,9 +250,10 @@ rspamd_rcl_options_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
  */
 static gboolean
 rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric,
-       const ucl_object_t *obj, const gchar *group, gboolean is_legacy, GError **err)
+       const ucl_object_t *obj, struct rspamd_symbols_group *gr,
+       gboolean is_legacy, GError **err)
 {
-       const gchar *description = NULL, *sym_name;
+       const gchar *description = NULL, *sym_name, *group;
        gdouble symbol_score;
        const ucl_object_t *val;
        gboolean one_shot = FALSE;
@@ -280,9 +281,13 @@ rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric,
                sym_name = ucl_object_key (obj);
        }
 
-       if (group == NULL) {
+       if (gr == NULL) {
                group = "ungrouped";
        }
+       else {
+               group = gr->name;
+               one_shot = gr->one_shot;
+       }
 
        if (ucl_object_todouble_safe (obj, &symbol_score)) {
                description = NULL;
@@ -336,7 +341,7 @@ rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric,
 static gboolean
 rspamd_rcl_symbols_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                struct rspamd_config *cfg, struct metric *metric,
-               const gchar *group, gboolean new, GError **err)
+               struct rspamd_symbols_group *gr, gboolean new, GError **err)
 {
        const ucl_object_t *val, *cur;
        ucl_object_iter_t it = NULL;
@@ -350,7 +355,7 @@ rspamd_rcl_symbols_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                }
                it = NULL;
                while ((cur = ucl_iterate_object (val, &it, true)) != NULL) {
-                       if (!rspamd_rcl_insert_symbol (cfg, metric, cur, group, FALSE, err)) {
+                       if (!rspamd_rcl_insert_symbol (cfg, metric, cur, gr, FALSE, err)) {
                                return FALSE;
                        }
                }
@@ -369,7 +374,7 @@ rspamd_rcl_symbols_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 
                        it = NULL;
                        while ((cur = ucl_iterate_object (val, &it, false)) != NULL) {
-                               if (!rspamd_rcl_insert_symbol (cfg, metric, cur, group, TRUE, err)) {
+                               if (!rspamd_rcl_insert_symbol (cfg, metric, cur, gr, TRUE, err)) {
                                        return FALSE;
                                }
                        }
@@ -504,23 +509,41 @@ rspamd_rcl_metric_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                                elt = ucl_object_find_key (cur, "name");
 
                                if (elt) {
-                                       if (!rspamd_rcl_symbols_handler (pool, cur, cfg, metric,
-                                                       ucl_object_tostring (elt),
-                                                       !have_symbols, err)) {
-                                               return FALSE;
-                                       }
-
-                                       have_symbols = TRUE;
                                        gr = g_hash_table_lookup (cfg->symbols_groups,
                                                        ucl_object_tostring (elt));
+                                       if (gr == NULL) {
+                                               gr =
+                                                               rspamd_mempool_alloc0 (cfg->cfg_pool,
+                                                                               sizeof (struct rspamd_symbols_group));
+                                               gr->name = rspamd_mempool_strdup (cfg->cfg_pool,
+                                                               ucl_object_tostring (elt));
+                                               g_hash_table_insert (cfg->symbols_groups, gr->name, gr);
+                                       }
 
-                                       if (gr != NULL) {
-                                               elt = ucl_object_find_key (cur, "max_score");
+                                       elt = ucl_object_find_key (cur, "max_score");
 
-                                               if (elt) {
-                                                       gr->max_score = ucl_object_todouble (elt);
-                                               }
+                                       if (elt) {
+                                               gr->max_score = ucl_object_todouble (elt);
                                        }
+
+                                       elt = ucl_object_find_key (cur, "disabled");
+
+                                       if (elt) {
+                                               gr->disabled = ucl_object_toboolean (elt);
+                                       }
+
+                                       elt = ucl_object_find_key (cur, "one_shot");
+
+                                       if (elt) {
+                                               gr->one_shot = ucl_object_toboolean (elt);
+                                       }
+
+                                       if (!rspamd_rcl_symbols_handler (pool, cur, cfg, metric,
+                                                       gr, !have_symbols, err)) {
+                                               return FALSE;
+                                       }
+
+                                       have_symbols = TRUE;
                                }
                        }
                }
index 94fe962bc545ee37e8f89daac2a16ee53b493817..c877be37d3a675427d1826f14b85510c21172bfd 100644 (file)
@@ -873,7 +873,7 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
 
        /* Init all compiled modules */
        if (!reconfig) {
-               for (pmod = modules; *pmod != NULL; pmod ++) {
+               for (pmod = cfg->compiled_modules; *pmod != NULL; pmod ++) {
                        mod = *pmod;
                        mod_ctx = g_slice_alloc0 (sizeof (struct module_ctx));
 
@@ -1017,3 +1017,81 @@ rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
 
        return TRUE;
 }
+
+gboolean
+rspamd_config_is_module_enabled (struct rspamd_config *cfg,
+               const gchar *module_name)
+{
+       gboolean is_c = FALSE;
+       struct metric *metric;
+       const ucl_object_t *conf, *enabled;
+       GList *cur;
+       struct rspamd_symbols_group *gr;
+
+       metric = cfg->default_metric;
+
+       if (g_hash_table_lookup (cfg->c_modules, module_name)) {
+               is_c = TRUE;
+       }
+
+       if (is_c) {
+               gboolean found = FALSE;
+
+               cur = g_list_first (cfg->filters);
+
+               while (cur) {
+                       if (strcmp (cur->data, module_name) == 0) {
+                               found = TRUE;
+                               break;
+                       }
+
+                       cur = g_list_next (cur);
+               }
+
+               if (!found) {
+                       msg_info ("internal module %s is disable in `filters` line",
+                                       module_name);
+
+                       return FALSE;
+               }
+       }
+
+       conf = ucl_object_find_key (cfg->rcl_obj, module_name);
+
+       if (conf == NULL) {
+               if (is_c) {
+                       msg_info ("internal module %s is enabled but has not configured",
+                                       module_name);
+               }
+               else {
+                       msg_info ("lua module %s is disabled as it has not configured",
+                                                               module_name);
+                       return FALSE;
+               }
+       }
+       else {
+               enabled = ucl_object_find_key (conf, "enabled");
+
+               if (enabled && ucl_object_type (enabled) == UCL_BOOLEAN) {
+                       if (!ucl_object_toboolean (enabled)) {
+                               msg_info ("%s module %s is disabled in the configuration",
+                                               is_c ? "internal" : "lua", module_name);
+                               return FALSE;
+                       }
+               }
+       }
+
+       /* Now we check symbols group */
+       gr = g_hash_table_lookup (cfg->symbols_groups, module_name);
+
+       if (gr) {
+               if (gr->disabled) {
+                       msg_info ("%s module %s is disabled in the configuration as "
+                                       "its group is disabled",
+                                       is_c ? "internal" : "lua", module_name);
+                       return FALSE;
+               }
+       }
+
+       return TRUE;
+}