From 2ad0276a3189c61e673ea716dbd461ac6ee59f87 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 27 Mar 2019 12:03:54 +0000 Subject: [PATCH] [Minor] Pass variables from the environment to rspamd_env --- src/libutil/str_util.h | 3 +++ src/libutil/upstream.c | 13 +++++-------- src/lua/lua_common.c | 44 ++++++++++++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 9145b97b4..91efdf187 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -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_ */ diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 3a2b803b4..3e04e68e9 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -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, diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 7f49c090d..1c687fb48 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -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"); } -- 2.39.5