]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Pass variables from the environment to rspamd_env
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 27 Mar 2019 12:03:54 +0000 (12:03 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 27 Mar 2019 12:03:54 +0000 (12:03 +0000)
src/libutil/str_util.h
src/libutil/upstream.c
src/lua/lua_common.c

index 9145b97b465d898415db8baeea02b987da602001..91efdf187ab55982097c76751cfccc2e8d842319 100644 (file)
@@ -502,4 +502,7 @@ const gchar* rspamd_string_len_strip (const gchar *in,
                                                                ((uc) >= 0x205F && (uc) <= 0x206F) || \
                                                                (uc) == 0xFEFF)
 
+#define RSPAMD_LEN_CHECK_STARTS_WITH(s, len, lit) \
+       ((len) >= sizeof(lit) - 1 && g_ascii_strncasecmp ((s), (lit), sizeof(lit) - 1) == 0)
+
 #endif /* SRC_LIBUTIL_STR_UTIL_H_ */
index 3a2b803b4fd212361cf11ffc29a60313ea4498a7..3e04e68e9c0438dcfa686eb36c3db5052ca17308 100644 (file)
@@ -788,8 +788,6 @@ rspamd_upstream_add_addr (struct upstream *up, rspamd_inet_addr_t *addr)
        return TRUE;
 }
 
-#define LEN_CHECK_STARTS_WITH(s, len, lit) \
-       ((len) >= sizeof(lit) - 1 && g_ascii_strncasecmp ((s), (lit), sizeof(lit) - 1) == 0)
 gboolean
 rspamd_upstreams_parse_line_len (struct upstream_list *ups,
                const gchar *str, gsize len, guint16 def_port, void *data)
@@ -800,23 +798,23 @@ rspamd_upstreams_parse_line_len (struct upstream_list *ups,
        guint span_len;
        gboolean ret = FALSE;
 
-       if (LEN_CHECK_STARTS_WITH(p, len, "random:")) {
+       if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "random:")) {
                ups->rot_alg = RSPAMD_UPSTREAM_RANDOM;
                p += sizeof ("random:") - 1;
        }
-       else if (LEN_CHECK_STARTS_WITH(p, len, "master-slave:")) {
+       else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "master-slave:")) {
                ups->rot_alg = RSPAMD_UPSTREAM_MASTER_SLAVE;
                p += sizeof ("master-slave:") - 1;
        }
-       else if (LEN_CHECK_STARTS_WITH(p, len, "round-robin:")) {
+       else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "round-robin:")) {
                ups->rot_alg = RSPAMD_UPSTREAM_ROUND_ROBIN;
                p += sizeof ("round-robin:") - 1;
        }
-       else if (LEN_CHECK_STARTS_WITH(p, len, "hash:")) {
+       else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "hash:")) {
                ups->rot_alg = RSPAMD_UPSTREAM_HASHED;
                p += sizeof ("hash:") - 1;
        }
-       else if (LEN_CHECK_STARTS_WITH(p, len, "sequential:")) {
+       else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "sequential:")) {
                ups->rot_alg = RSPAMD_UPSTREAM_SEQUENTIAL;
                p += sizeof ("sequential:") - 1;
        }
@@ -847,7 +845,6 @@ rspamd_upstreams_parse_line_len (struct upstream_list *ups,
        return ret;
 }
 
-#undef LEN_CHECK_STARTS_WITH
 
 gboolean
 rspamd_upstreams_parse_line (struct upstream_list *ups,
index 7f49c090d445af2aa5585a832d5b7ebeb0e152e0..1c687fb48097923ddd0f2c2e99747ae8c1d0fcaa 100644 (file)
@@ -541,6 +541,7 @@ void
 rspamd_lua_set_env (lua_State *L, GHashTable *vars)
 {
        gint orig_top = lua_gettop (L);
+       gchar **env = g_get_environ ();
 
        /* Set known paths as rspamd_paths global */
        lua_getglobal (L, "rspamd_paths");
@@ -559,52 +560,52 @@ rspamd_lua_set_env (lua_State *L, GHashTable *vars)
                const gchar *t;
 
                /* Try environment */
-               t = getenv ("SHAREDIR");
+               t = g_environ_getenv (env, "SHAREDIR");
                if (t) {
                        sharedir = t;
                }
 
-               t = getenv ("PLUGINSDIR");
+               t = g_environ_getenv (env, "PLUGINSDIR");
                if (t) {
                        pluginsdir = t;
                }
 
-               t = getenv ("RULESDIR");
+               t = g_environ_getenv (env, "RULESDIR");
                if (t) {
                        rulesdir = t;
                }
 
-               t = getenv ("DBDIR");
+               t = g_environ_getenv (env, "DBDIR");
                if (t) {
                        dbdir = t;
                }
 
-               t = getenv ("RUNDIR");
+               t = g_environ_getenv (env, "RUNDIR");
                if (t) {
                        rundir = t;
                }
 
-               t = getenv ("LUALIBDIR");
+               t = g_environ_getenv (env, "LUALIBDIR");
                if (t) {
                        lualibdir = t;
                }
 
-               t = getenv ("LOGDIR");
+               t = g_environ_getenv (env, "LOGDIR");
                if (t) {
                        logdir = t;
                }
 
-               t = getenv ("WWWDIR");
+               t = g_environ_getenv (env, "WWWDIR");
                if (t) {
                        wwwdir = t;
                }
 
-               t = getenv ("CONFDIR");
+               t = g_environ_getenv (env, "CONFDIR");
                if (t) {
                        confdir = t;
                }
 
-               t = getenv ("LOCAL_CONFDIR");
+               t = g_environ_getenv (env, "LOCAL_CONFDIR");
                if (t) {
                        local_confdir = t;
                }
@@ -718,6 +719,29 @@ rspamd_lua_set_env (lua_State *L, GHashTable *vars)
                lua_pushinteger (L, RSPAMD_VERSION_NUM);
                lua_settable (L, -3);
 
+               if (env) {
+                       gint lim = g_strv_length (env);
+
+                       for (gint i = 0; i < lim; i++) {
+                               if (RSPAMD_LEN_CHECK_STARTS_WITH(env[i], strlen (env[i]), "RSPAMD_")) {
+                                       const char *var = env[i] + sizeof ("RSPAMD_") - 1, *value;
+                                       gint varlen;
+
+                                       varlen = strcspn (var, "=");
+                                       value = var + varlen;
+
+                                       if (*value == '=') {
+                                               value ++;
+
+                                               lua_pushlstring (L, var, varlen);
+                                               lua_pushstring (L, value);
+                                               lua_settable (L, -3);
+                                       }
+
+                               }
+                       }
+               }
+
                lua_setglobal (L, "rspamd_env");
        }