]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix parsing IPv6 nameservers in resolv.conf
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 14 Jun 2017 08:30:08 +0000 (09:30 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 14 Jun 2017 08:30:08 +0000 (09:30 +0100)
contrib/librdns/util.c
src/libserver/dns.c
src/libutil/upstream.c
src/libutil/upstream.h
src/plugins/surbl.c
test/rspamd_upstream_test.c

index 9ea8f5c4326fac1d58448248e3760bb51de23eb7..aa31c96b738a07df7a4a419bb82513d96508b450 100644 (file)
@@ -556,6 +556,7 @@ rdns_resolver_parse_resolv_conf_cb (struct rdns_resolver *resolver,
 {
        FILE *in;
        char buf[BUFSIZ];
+       char *p;
 
        in = fopen (path, "r");
 
@@ -568,6 +569,13 @@ rdns_resolver_parse_resolv_conf_cb (struct rdns_resolver *resolver,
                        break;
                }
 
+               /* Strip trailing spaces */
+               p = buf + strlen (buf) - 1;
+               while (p > buf &&
+                               (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) {
+                       *p-- = '\0';
+               }
+
                if (!rdns_resolver_conf_process_line (resolver, buf, cb, ud)) {
                        rdns_warn ("rdns_resolver_parse_resolv_conf: cannot parse line: %s", buf);
                        fclose (in);
index 21a15297cad75320aeb552cfe9d0dcfe73a4ed81..d2629fd21bca16e4e445c368e1676ef143ad7183 100644 (file)
@@ -240,8 +240,9 @@ rspamd_dns_resolv_conf_on_server (struct rdns_resolver *resolver,
 {
        struct rspamd_dns_resolver *dns_resolver = ud;
 
-       return rspamd_upstreams_add_upstream (dns_resolver->ups,
-               name, port, NULL);
+       return rspamd_upstreams_add_upstream (dns_resolver->ups, name, port,
+                       RSPAMD_UPSTREAM_PARSE_NAMESERVER,
+                       NULL);
 }
 
 struct rspamd_dns_resolver *
index 9070245795ecd10da26636552e4479673b0ecda8..ac15780adb9412d2e5e29a9377679703a2e98997 100644 (file)
@@ -563,20 +563,50 @@ rspamd_upstream_name (struct upstream *up)
 }
 
 gboolean
-rspamd_upstreams_add_upstream (struct upstream_list *ups,
-               const gchar *str, guint16 def_port, void *data)
+rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str,
+               guint16 def_port, enum rspamd_upstream_parse_type parse_type,
+               void *data)
 {
        struct upstream *up;
        GPtrArray *addrs = NULL;
        guint i;
        rspamd_inet_addr_t *addr;
+       gboolean ret = FALSE;
 
        up = g_slice_alloc0 (sizeof (*up));
 
-       if (!rspamd_parse_host_port_priority (str, &addrs,
-                       &up->weight,
-                       &up->name, def_port, ups->ctx->pool)) {
+       switch (parse_type) {
+       case RSPAMD_UPSTREAM_PARSE_DEFAULT:
+               ret = rspamd_parse_host_port_priority (str, &addrs,
+                               &up->weight,
+                               &up->name, def_port, ups->ctx->pool);
+               break;
+       case RSPAMD_UPSTREAM_PARSE_NAMESERVER:
+               addrs = g_ptr_array_sized_new (1);
+
+               ret = rspamd_parse_inet_address (&addr, str, strlen (str));
+
+               if (ret) {
+                       if (rspamd_inet_address_get_port (addr) == 0) {
+                               rspamd_inet_address_set_port (addr, def_port);
+                       }
+
+                       g_ptr_array_add (addrs, addr);
+                       rspamd_mempool_add_destructor (ups->ctx->pool,
+                                       (rspamd_mempool_destruct_t)rspamd_inet_address_free,
+                                       addr);
+                       rspamd_mempool_add_destructor (ups->ctx->pool,
+                                       (rspamd_mempool_destruct_t)rspamd_ptr_array_free_hard,
+                                       addrs);
+               }
+               else {
+                       g_ptr_array_free (addrs, TRUE);
+               }
+
+               break;
+       }
 
+       if (!ret) {
                g_slice_free1 (sizeof (*up), up);
                return FALSE;
        }
@@ -691,7 +721,9 @@ rspamd_upstreams_parse_line (struct upstream_list *ups,
                        tmp = g_malloc (len + 1);
                        rspamd_strlcpy (tmp, p, len + 1);
 
-                       if (rspamd_upstreams_add_upstream (ups, tmp, def_port, data)) {
+                       if (rspamd_upstreams_add_upstream (ups, tmp, def_port,
+                                       RSPAMD_UPSTREAM_PARSE_DEFAULT,
+                                       data)) {
                                ret = TRUE;
                        }
 
index ddc480afea512518e120369bbab6872b08a523b0..84e75288df476f4c971207c8bc9e34a09dba0684 100644 (file)
@@ -110,6 +110,11 @@ gsize rspamd_upstreams_count (struct upstream_list *ups);
  */
 gsize rspamd_upstreams_alive (struct upstream_list *ups);
 
+enum rspamd_upstream_parse_type {
+       RSPAMD_UPSTREAM_PARSE_DEFAULT = 0,
+       RSPAMD_UPSTREAM_PARSE_NAMESERVER,
+};
+
 /**
  * Add upstream from the string
  * @param ups upstream list
@@ -118,8 +123,9 @@ gsize rspamd_upstreams_alive (struct upstream_list *ups);
  * @param data optional userdata
  * @return TRUE if upstream has been added
  */
-gboolean rspamd_upstreams_add_upstream (struct upstream_list *ups,
-               const gchar *str, guint16 def_port, void *data);
+gboolean rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str,
+               guint16 def_port, enum rspamd_upstream_parse_type parse_type,
+               void *data);
 
 /**
  * Add multiple upstreams from comma, semicolon or space separated line
index 78422461a69fdaf1a84bb0d10977fc95b54ebf14..486b483884e43ba4fd31f7d41c97b424ec994b30 100644 (file)
@@ -768,7 +768,8 @@ surbl_module_config (struct rspamd_config *cfg)
                {
                        redir_val = ucl_obj_tostring (cur);
                        if (rspamd_upstreams_add_upstream (surbl_module_ctx->redirectors,
-                                       redir_val, 80, NULL)) {
+                                       redir_val, 80, RSPAMD_UPSTREAM_PARSE_DEFAULT,
+                                       NULL)) {
                                surbl_module_ctx->use_redirector = TRUE;
                        }
                }
index 072a2a4734d363daf0d14f3ce42b131c8b6ddff2..47094398f71c19e162d9b2f26f57bd228dcce225 100644 (file)
@@ -78,7 +78,9 @@ rspamd_upstream_test_func (void)
         * Test v4/v6 priorities
         */
        nls = rspamd_upstreams_create (cfg->ups_ctx);
-       g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0, NULL));
+       g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0,
+                       RSPAMD_UPSTREAM_PARSE_DEFAULT,
+                       NULL));
        up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0);
        rspamd_parse_inet_address (&paddr, "127.0.0.2", 0);
        g_assert (rspamd_upstream_add_addr (up, paddr));