]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Rework config load routines
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 May 2018 15:58:54 +0000 (16:58 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 May 2018 15:58:54 +0000 (16:58 +0100)
lualib/rspamadm/corpus_test.lua
src/libserver/cfg_rcl.c
src/libserver/cfg_rcl.h
src/lua/lua_util.c
src/rspamadm/configdump.c
src/rspamadm/configtest.c
src/rspamadm/configwizard.c
src/rspamadm/rescore.c
src/rspamd.c

index 59b02f649d7e522ec526474dfc34b7aa1b95a076..60aa2d7a9ceb243e927c8f0aae923bd6176ca757 100644 (file)
@@ -74,16 +74,16 @@ local function encoded_json_to_log(result)
   -- Returns table containing score, action, list of symbols
 
   local filtered_result = {}
-  local parser = ucl.parser()
+  local ucl_parser = ucl.parser()
 
-  local is_good, err = parser:parse_string(result)
+  local is_good, err = ucl_parser:parse_string(result)
 
   if not is_good then
     rspamd_logger.errx("Parser error: %1", err)
     return nil
   end
 
-  result = parser:get_object()
+  result = ucl_parser:get_object()
 
   filtered_result.score = result.score
   if not result.action then
index abb0e9eefaea49edcce62872e983ffa9ea2f1d7f..ad6bbe8a30ad9cf43465416727e66365bd12cf90 100644 (file)
@@ -3229,7 +3229,7 @@ rspamd_rcl_section_free (gpointer p)
  * it is changed, then rcl_obj is imported from lua. Old config is dereferenced.
  * @param cfg
  */
-static void
+void
 rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg)
 {
        lua_State *L = cfg->lua_state;
@@ -3318,37 +3318,56 @@ rspamd_rcl_decrypt_free (unsigned char *data, size_t len, void *user_data)
        g_free (data);
 }
 
+void
+rspamd_config_calculate_cksum (struct rspamd_config *cfg)
+{
+       rspamd_cryptobox_hash_state_t hs;
+       unsigned char cksumbuf[rspamd_cryptobox_HASHBYTES];
+       struct ucl_emitter_functions f;
+
+       /* Calculate checksum */
+       rspamd_cryptobox_hash_init (&hs, NULL, 0);
+       f.ucl_emitter_append_character = rspamd_rcl_emitter_append_c;
+       f.ucl_emitter_append_double = rspamd_rcl_emitter_append_double;
+       f.ucl_emitter_append_int = rspamd_rcl_emitter_append_int;
+       f.ucl_emitter_append_len = rspamd_rcl_emitter_append_len;
+       f.ucl_emitter_free_func = NULL;
+       f.ud = &hs;
+       ucl_object_emit_full (cfg->rcl_obj, UCL_EMIT_MSGPACK,
+                       &f, cfg->config_comments);
+       rspamd_cryptobox_hash_final (&hs, cksumbuf);
+       cfg->checksum = rspamd_encode_base32 (cksumbuf, sizeof (cksumbuf));
+       /* Also change the tag of cfg pool to be equal to the checksum */
+       rspamd_strlcpy (cfg->cfg_pool->tag.uid, cfg->checksum,
+                       MIN (sizeof (cfg->cfg_pool->tag.uid), strlen (cfg->checksum)));
+}
+
 gboolean
-rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
-       const gchar *convert_to, rspamd_rcl_section_fin_t logger_fin,
-       gpointer logger_ud, GHashTable *vars)
+rspamd_config_parse_ucl (struct rspamd_config *cfg, const gchar *filename,
+                                       GHashTable *vars, GError **err)
 {
        struct stat st;
        gint fd;
-       gchar *data;
-       GError *err = NULL;
-       struct rspamd_rcl_section *top, *logger_section;
        struct ucl_parser *parser;
-       const ucl_object_t *logger_obj;
-       rspamd_cryptobox_hash_state_t hs;
-       unsigned char cksumbuf[rspamd_cryptobox_HASHBYTES];
        gchar keypair_path[PATH_MAX];
        struct rspamd_cryptobox_keypair *decrypt_keypair = NULL;
-       struct ucl_emitter_functions f;
+       gchar *data;
 
        if (stat (filename, &st) == -1) {
-               msg_err_config_forced ("cannot stat %s: %s", filename, strerror (errno));
+               g_set_error (err, cfg_rcl_error_quark (), errno,
+                               "cannot stat %s: %s", filename, strerror (errno));
                return FALSE;
        }
        if ((fd = open (filename, O_RDONLY)) == -1) {
-               msg_err_config_forced ("cannot open %s: %s", filename, strerror (errno));
+               g_set_error (err, cfg_rcl_error_quark (), errno,
+                               "cannot open %s: %s", filename, strerror (errno));
                return FALSE;
 
        }
        /* Now mmap this file to simplify reading process */
-       if ((data =
-               mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
-               msg_err_config_forced ("cannot mmap %s: %s", filename, strerror (errno));
+       if ((data = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+               g_set_error (err, cfg_rcl_error_quark (), errno,
+                               "cannot mmap %s: %s", filename, strerror (errno));
                close (fd);
                return FALSE;
        }
@@ -3393,7 +3412,6 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
                ucl_parser_free (kp_parser);
        }
 
-       rspamd_cryptobox_hash_init (&hs, NULL, 0);
        parser = ucl_parser_new (UCL_PARSER_SAVE_COMMENTS);
        rspamd_ucl_add_conf_variables (parser, vars);
        rspamd_ucl_add_conf_macros (parser, cfg);
@@ -3424,6 +3442,25 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
        cfg->config_comments = ucl_object_ref (ucl_parser_get_comments (parser));
        ucl_parser_free (parser);
 
+       return TRUE;
+}
+
+gboolean
+rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
+                                       rspamd_rcl_section_fin_t logger_fin,
+                                       gpointer logger_ud, GHashTable *vars)
+{
+       GError *err = NULL;
+       struct rspamd_rcl_section *top, *logger_section;
+       const ucl_object_t *logger_obj;
+
+       if (!rspamd_config_parse_ucl (cfg, filename, vars, &err)) {
+               msg_err_config_forced ("failed to load config: %e", err);
+               g_error_free (err);
+
+               return FALSE;
+       }
+
        top = rspamd_rcl_config_init (cfg);
        rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, vars);
        rspamd_lua_set_globals (cfg, cfg->lua_state, vars);
@@ -3459,28 +3496,13 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
        /* Transform config if needed */
        rspamd_rcl_maybe_apply_lua_transform (cfg);
 
-       /* Calculate checksum */
-       f.ucl_emitter_append_character = rspamd_rcl_emitter_append_c;
-       f.ucl_emitter_append_double = rspamd_rcl_emitter_append_double;
-       f.ucl_emitter_append_int = rspamd_rcl_emitter_append_int;
-       f.ucl_emitter_append_len = rspamd_rcl_emitter_append_len;
-       f.ucl_emitter_free_func = NULL;
-       f.ud = &hs;
-       ucl_object_emit_full (cfg->rcl_obj, UCL_EMIT_MSGPACK,
-                       &f, cfg->config_comments);
-       rspamd_cryptobox_hash_final (&hs, cksumbuf);
-       cfg->checksum = rspamd_encode_base32 (cksumbuf, sizeof (cksumbuf));
-       /* Also change the tag of cfg pool to be equal to the checksum */
-       rspamd_strlcpy (cfg->cfg_pool->tag.uid, cfg->checksum,
-                       MIN (sizeof (cfg->cfg_pool->tag.uid), strlen (cfg->checksum)));
-
-
        if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) {
                msg_err_config ("rcl parse error: %e", err);
                g_error_free (err);
                return FALSE;
        }
 
+       rspamd_config_calculate_cksum (cfg);
        cfg->lang_det = rspamd_language_detector_init (cfg);
 
        return TRUE;
index c1d958efc94d7bbe830b1b1be55b0cf386207d5e..8661687a349ed528f53d63108fc75174b95a3683 100644 (file)
@@ -446,11 +446,40 @@ gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
                const gchar *path,
                GError **err);
 
+
+/**
+ * Calls for an external lua function to apply potential config transformations
+ * if needed. This function can change the cfg->rcl_obj.
+ *
+ * Example of transformation function:
+ *
+ * function(obj)
+ *   if obj.something == 'foo' then
+ *     obj.something = "bla"
+ *     return true, obj
+ *   end
+ *
+ *   return false, nil
+ * end
+ *
+ * If function returns 'false' then rcl_obj is not touched. Otherwise,
+ * it is changed, then rcl_obj is imported from lua. Old config is dereferenced.
+ * @param cfg
+ */
+void rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg);
+
+void rspamd_config_calculate_cksum (struct rspamd_config *cfg);
+
 /*
  * Read configuration file
  */
+gboolean rspamd_config_parse_ucl (struct rspamd_config *cfg,
+                                                                 const gchar *filename,
+                                                                 GHashTable *vars,
+                                                                 GError **err);
 gboolean rspamd_config_read (struct rspamd_config *cfg,
-               const gchar *filename, const gchar *convert_to,
-               rspamd_rcl_section_fin_t logger_fin, gpointer logger_ud,
-               GHashTable *vars);
+                                                        const gchar *filename,
+                                                        rspamd_rcl_section_fin_t logger_fin,
+                                                        gpointer logger_ud,
+                                                        GHashTable *vars);
 #endif /* CFG_RCL_H_ */
index 47b7106b6d7fdafb491ca1ce5b764e65b8766f97..578b5e3c9e6fbbbad46426957c276e59f3b561aa 100644 (file)
@@ -629,7 +629,7 @@ lua_util_load_rspamd_config (lua_State *L)
                cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
                cfg->lua_state = L;
 
-               if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, NULL)) {
+               if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL)) {
                        msg_err_config ("cannot load config from %s", cfg_name);
                        lua_pushnil (L);
                }
index 486a74cef3ea62899af8f1d3f5181b7e6221e08c..bd776e586f88534ed0453e76603a92499b39c08e 100644 (file)
@@ -283,8 +283,7 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd
        cfg->compiled_workers = workers;
        cfg->cfg_name = config;
 
-       if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
-                       config_logger, rspamd_main, ucl_vars)) {
+       if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, ucl_vars)) {
                ret = FALSE;
        }
        else {
index 94914e817ef430c7ea6575d6849bbcb0fd75fbed..72a8f49453afea20d831e71c16dd629cac406a53 100644 (file)
@@ -141,8 +141,7 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
        cfg->compiled_workers = workers;
        cfg->cfg_name = config;
 
-       if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
-                       config_logger, rspamd_main, ucl_vars)) {
+       if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, ucl_vars)) {
                ret = FALSE;
        }
        else {
index 5aaa94914fb1836ceae9e65642653ff239d46bbc..bfa35bb713b829051a13d640afb786e0c9dbca9c 100644 (file)
@@ -132,8 +132,7 @@ rspamadm_configwizard (gint argc, gchar **argv,
        cfg->compiled_workers = workers;
        cfg->cfg_name = config;
 
-       if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
-                       config_logger, rspamd_main, ucl_vars)) {
+       if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, ucl_vars)) {
                ret = FALSE;
        }
        else {
index e30c6613a2c816f092621cdb71979f0306f0b476..346c49c68bee9c8efb3752913aad25708aba3990 100644 (file)
@@ -150,8 +150,7 @@ rspamadm_rescore (gint argc, gchar **argv, const struct rspamadm_command *cmd)
        cfg->compiled_workers = workers;
        cfg->cfg_name = config;
 
-       if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
-                       config_logger, rspamd_main, ucl_vars)) {
+       if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, ucl_vars)) {
                ret = FALSE;
        }
        else {
index d6490d6aa53aadc668bef473fd68d5c07757ee2c..643d249b184f0cabce02c1e60745fe2993d707d4 100644 (file)
@@ -874,8 +874,7 @@ load_rspamd_config (struct rspamd_main *rspamd_main,
        cfg->compiled_modules = modules;
        cfg->compiled_workers = workers;
 
-       if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
-               config_logger, rspamd_main, ucl_vars)) {
+       if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, ucl_vars)) {
                return FALSE;
        }