]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Move local IPs check
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 12 Feb 2020 15:02:41 +0000 (15:02 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 12 Feb 2020 15:02:41 +0000 (15:02 +0000)
src/client/rspamc.c
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/libutil/addr.c
src/libutil/addr.h
src/lua/lua_ip.c
src/plugins/dkim_check.c
src/rspamd.h
src/rspamd_proxy.c
src/worker.c

index f759ec6108254aa16ef9ea5bb50141f7b4b6c5b5..6947d5604a8a648693597582e370153e137f8ae5 100644 (file)
@@ -17,6 +17,7 @@
 #include "libutil/util.h"
 #include "libserver/http/http_connection.h"
 #include "libserver/http/http_private.h"
+#include "libserver/cfg_file.h"
 #include "rspamdclient.h"
 #include "utlist.h"
 #include "unix-std.h"
@@ -168,17 +169,6 @@ static GOptionEntry entries[] =
        { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
 };
 
-/* Copy to avoid linking with librspamdserver */
-enum rspamd_action_type {
-       METRIC_ACTION_REJECT = 0,
-       METRIC_ACTION_SOFT_REJECT,
-       METRIC_ACTION_REWRITE_SUBJECT,
-       METRIC_ACTION_ADD_HEADER,
-       METRIC_ACTION_GREYLIST,
-       METRIC_ACTION_NOACTION,
-       METRIC_ACTION_MAX
-};
-
 static void rspamc_symbols_output (FILE *out, ucl_object_t *obj);
 static void rspamc_uptime_output (FILE *out, ucl_object_t *obj);
 static void rspamc_counters_output (FILE *out, ucl_object_t *obj);
index 4a8ab5bfca0f0b36efc05ef8b9f7a0c3efbc5dd7..1d782dd8b7da266d92803f027630992d780e5d13 100644 (file)
@@ -845,6 +845,12 @@ gboolean rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx);
  */
 void rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx);
 
+/**
+ * Returns TRUE if an address belongs to some local address
+ */
+gboolean rspamd_ip_is_local_cfg (struct rspamd_config *cfg,
+               const rspamd_inet_addr_t *addr);
+
 /**
  * Configure libraries
  */
index 204e9bd55d305fde2ed235966038ceb25dc3c578..d8237b88ed33b36b959d7659d3e8fc1a78585f18 100644 (file)
@@ -2998,4 +2998,27 @@ rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx)
 
                g_free (ctx);
        }
-}
\ No newline at end of file
+}
+
+gboolean
+rspamd_ip_is_local_cfg (struct rspamd_config *cfg,
+                                                                const rspamd_inet_addr_t *addr)
+{
+       struct rspamd_radix_map_helper *local_addrs = NULL;
+
+       if (cfg && cfg->libs_ctx) {
+               local_addrs = *(struct rspamd_radix_map_helper**)cfg->libs_ctx->local_addrs;
+       }
+
+       if (rspamd_inet_address_is_local (addr)) {
+               return TRUE;
+       }
+
+       if (local_addrs) {
+               if (rspamd_match_radix_map_addr (local_addrs, addr) != NULL) {
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
index c0cb2d19d3dd6b8e331ae5d9728231626e65207b..4a540fefc5c7c94e232dbd792e6240eb622cec12 100644 (file)
 #include "config.h"
 #include "addr.h"
 #include "util.h"
-/*
- * TODO: fix this cross dependency!
- */
-#include "libserver/maps/map_helpers.h"
 #include "logger.h"
 #include "cryptobox.h"
 #include "unix-std.h"
@@ -32,7 +28,7 @@
 #include <grp.h>
 #endif
 
-static struct rspamd_radix_map_helper *local_addrs;
+static void *local_addrs;
 
 enum {
        RSPAMD_IPV6_UNDEFINED = 0,
@@ -1879,8 +1875,7 @@ rspamd_inet_address_port_equal (gconstpointer a, gconstpointer b)
 #endif
 
 gboolean
-rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr,
-               gboolean check_laddrs)
+rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr)
 {
        if (addr == NULL) {
                return FALSE;
@@ -1904,23 +1899,23 @@ rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr,
                                return TRUE;
                        }
                }
-
-               if (check_laddrs && local_addrs) {
-                       if (rspamd_match_radix_map_addr (local_addrs, addr) != NULL) {
-                               return TRUE;
-                       }
-               }
        }
 
        return FALSE;
 }
 
-struct rspamd_radix_map_helper **
+void **
 rspamd_inet_library_init (void)
 {
        return &local_addrs;
 }
 
+void *
+rspamd_inet_library_get_lib_ctx (void)
+{
+       return local_addrs;
+}
+
 void
 rspamd_inet_library_destroy (void)
 {
index 6a33ad7ec871bae21109064962f35a4a31036001..c0910ad0305228b6a5142e9a515467b4e649204a 100644 (file)
@@ -44,10 +44,19 @@ extern "C" {
  */
 typedef struct rspamd_inet_addr_s rspamd_inet_addr_t;
 
-struct rspamd_radix_map_helper;
-
-struct rspamd_radix_map_helper **rspamd_inet_library_init (void);
-
+/**
+ * Returns pointer storage for global singleton (map for local addresses)
+ * @return
+ */
+void **rspamd_inet_library_init (void);
+/**
+ * Returns local addresses singleton
+ * @return
+ */
+void *rspamd_inet_library_get_lib_ctx (void);
+/**
+ * Cleanup library (currently it does nothing)
+ */
 void rspamd_inet_library_destroy (void);
 
 /**
@@ -323,8 +332,7 @@ gboolean rspamd_inet_address_port_equal (gconstpointer a, gconstpointer b);
 /**
  * Returns TRUE if an address belongs to some local address
  */
-gboolean rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr,
-                                                                          gboolean check_laddrs);
+gboolean rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr);
 
 /**
  * Returns size of storage required to store a complete IP address
index fb6845519ed13eda1bddaa4082eca9f54cdba7d1..2604aa1001fa11f8e6605ee59dbe92610a474f08 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "lua_common.h"
+#include "libserver/maps/map_helpers.h"
 
 /***
  * @module rspamd_ip
@@ -530,8 +531,24 @@ lua_ip_is_local (lua_State *L)
                        check_laddrs = lua_toboolean (L, 2);
                }
 
-               lua_pushboolean (L, rspamd_inet_address_is_local (ip->addr,
-                               check_laddrs));
+               if ( rspamd_inet_address_is_local (ip->addr)) {
+                       lua_pushboolean (L, true);
+
+                       return 1;
+               }
+               else if (check_laddrs) {
+                       struct rspamd_radix_map_helper *local_addrs =
+                                       rspamd_inet_library_get_lib_ctx ();
+                       if (local_addrs) {
+                               if (rspamd_match_radix_map_addr (local_addrs, ip->addr) != NULL) {
+                                       lua_pushboolean (L, true);
+
+                                       return 1;
+                               }
+                       }
+               }
+
+               lua_pushboolean (L, false);
        }
        else {
                lua_pushnil (L);
index 9313b643fae628dfc79deff29efb9c3daca58b6d..da7e092f77474ee9bfdc9a6b5513c9a06dbe1221 100644 (file)
@@ -1144,7 +1144,7 @@ dkim_symbol_callback (struct rspamd_task *task,
        /* First check if plugin should be enabled */
        if ((!dkim_module_ctx->check_authed && task->user != NULL)
                        || (!dkim_module_ctx->check_local &&
-                                       rspamd_inet_address_is_local (task->from_addr, TRUE))) {
+                       rspamd_ip_is_local_cfg (task->cfg, task->from_addr))) {
                msg_info_task ("skip DKIM checks for local networks and authorized users");
                rspamd_symcache_finalize_item (task, item);
 
index be96f07551c0805bec16b7715509d38ab54ad287..8885480c2a7af70aed328bc9a81469f15b5f1f02 100644 (file)
@@ -354,10 +354,8 @@ struct zstd_dictionary {
        guint id;
 };
 
-struct rspamd_radix_map_helper;
-
 struct rspamd_external_libs_ctx {
-       struct rspamd_radix_map_helper **local_addrs;
+       void **local_addrs;
        struct rspamd_cryptobox_library_ctx *crypto_ctx;
        struct ottery_config *ottery_cfg;
        SSL_CTX *ssl_ctx;
index f1a557133595d5f8d82a27fc59273cfb3cf473ca..e9ce9ef5ee24c7f4df33068613eb7ba4bf8e8bbe 100644 (file)
@@ -1415,8 +1415,7 @@ proxy_open_mirror_connections (struct rspamd_proxy_session *session)
                }
 
                if (m->local ||
-                               rspamd_inet_address_is_local (
-                                               rspamd_upstream_addr_cur (bk_conn->up), FALSE)) {
+                               rspamd_inet_address_is_local (rspamd_upstream_addr_cur (bk_conn->up))) {
 
                        if (session->fname) {
                                rspamd_http_message_add_header (msg, "File", session->fname);
@@ -1951,7 +1950,7 @@ retry:
                if (backend->local ||
                                rspamd_inet_address_is_local (
                                                rspamd_upstream_addr_cur (
-                                                               session->master_conn->up), FALSE)) {
+                                                               session->master_conn->up))) {
 
                        if (session->fname) {
                                rspamd_http_message_add_header (msg, "File", session->fname);
index 4cfe27771cef9dd5e087b216f640ce06fe4cf8a8..ad7f12674609f74f01acdeaa07b670ab7acaf9ce 100644 (file)
@@ -371,7 +371,7 @@ accept_socket (EV_P_ ev_io *w, int revents)
        session->ctx = ctx;
        session->worker = worker;
 
-       if (ctx->encrypted_only && !rspamd_inet_address_is_local (addr, FALSE)) {
+       if (ctx->encrypted_only && !rspamd_inet_address_is_local (addr)) {
                http_opts = RSPAMD_HTTP_REQUIRE_ENCRYPTION;
        }