]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix listen socket parsing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Feb 2020 19:40:16 +0000 (19:40 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Feb 2020 19:40:16 +0000 (19:40 +0000)
Issue: #3254

src/libserver/cfg_utils.c
src/libutil/addr.c
src/libutil/addr.h
src/libutil/upstream.c
src/rspamadm/lua_repl.c

index 6a076e673a1ddf89b6986fba1863566cead0e14b..9a308a8a4bfaf9306213080692d1a42ccd9832f6 100644 (file)
@@ -129,7 +129,7 @@ rspamd_parse_bind_line (struct rspamd_config *cfg,
        }
        else {
                if (rspamd_parse_host_port_priority (str, &cnf->addrs,
-                               NULL, &cnf->name, DEFAULT_BIND_PORT, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
+                               NULL, &cnf->name, DEFAULT_BIND_PORT, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
                        msg_err_config ("cannot parse bind line: %s", str);
                        ret = FALSE;
                }
index 1beeb421ea3bb1e7d35bdd59db68c0a2ecd353b5..52c0dbf9777a689be04e87163bad9dd162cdaab5 100644 (file)
@@ -1345,6 +1345,7 @@ rspamd_parse_host_port_priority (const gchar *str,
                                                                 guint *priority,
                                                                 gchar **name_ptr,
                                                                 guint default_port,
+                                                                gboolean allow_listen,
                                                                 rspamd_mempool_t *pool)
 {
        gchar portbuf[8];
@@ -1352,6 +1353,7 @@ rspamd_parse_host_port_priority (const gchar *str,
        gsize namelen;
        rspamd_inet_addr_t *cur_addr = NULL;
        enum rspamd_parse_host_port_result ret = RSPAMD_PARSE_ADDR_FAIL;
+       union sa_union su;
 
        /*
         * In this function, we can have several possibilities:
@@ -1361,19 +1363,62 @@ rspamd_parse_host_port_priority (const gchar *str,
         * 4) ip|host[:port[:priority]]
         */
 
-       if (str[0] == '*') {
-               if (!rspamd_check_port_priority (str + 1, default_port, priority,
+       if (allow_listen && str[0] == '*') {
+               bool v4_any = true, v6_any = true;
+
+               p = &str[1];
+
+               if (g_ascii_strncasecmp (p, "v4", 2) == 0) {
+                       p += 2;
+                       name = "*v4";
+                       v6_any = false;
+               }
+               else if (g_ascii_strncasecmp (p, "v6", 2) == 0) {
+                       p += 2;
+                       name = "*v6";
+                       v4_any = false;
+               }
+               else {
+                       name = "*";
+               }
+
+               if (!rspamd_check_port_priority (p, default_port, priority,
                                portbuf, sizeof (portbuf), pool)) {
                        return ret;
                }
 
-               if (rspamd_resolve_addrs (str, 0, addrs, portbuf, AI_PASSIVE, pool)
-                               == RSPAMD_PARSE_ADDR_FAIL) {
-                       return ret;
+               if (*addrs == NULL) {
+                       *addrs = g_ptr_array_new_full (1,
+                                       (GDestroyNotify) rspamd_inet_address_free);
+
+                       if (pool != NULL) {
+                               rspamd_mempool_add_destructor (pool,
+                                               rspamd_ptr_array_free_hard, *addrs);
+                       }
+               }
+
+               if (v4_any) {
+                       cur_addr = rspamd_inet_addr_create (AF_INET, pool);
+                       rspamd_parse_inet_address_ip4 ("0.0.0.0",
+                                       sizeof ("0.0.0.0") - 1, &su.s4.sin_addr);
+                       memcpy (&cur_addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr,
+                                       sizeof (struct in_addr));
+                       rspamd_inet_address_set_port (cur_addr,
+                                       strtoul (portbuf, NULL, 10));
+                       g_ptr_array_add (*addrs, cur_addr);
+               }
+               if (v6_any) {
+                       cur_addr = rspamd_inet_addr_create (AF_INET6, pool);
+                       rspamd_parse_inet_address_ip6 ("::",
+                                       sizeof ("::") - 1, &su.s6.sin6_addr);
+                       memcpy (&cur_addr->u.in.addr.s6.sin6_addr, &su.s6.sin6_addr,
+                                       sizeof (struct in6_addr));
+                       rspamd_inet_address_set_port (cur_addr,
+                                       strtoul (portbuf, NULL, 10));
+                       g_ptr_array_add (*addrs, cur_addr);
                }
 
-               name = "*";
-               namelen = 1;
+               namelen = strlen (name);
                ret = RSPAMD_PARSE_ADDR_NUMERIC; /* No resolution here */
        }
        else if (str[0] == '[') {
index c0910ad0305228b6a5142e9a515467b4e649204a..e17a4031c837554efdd193861b14da565faebbdb 100644 (file)
@@ -274,7 +274,9 @@ enum rspamd_parse_host_port_result {
 enum rspamd_parse_host_port_result
 rspamd_parse_host_port_priority (const gchar *str,
                                                                 GPtrArray **addrs,
-                                                                guint *priority, gchar **name, guint default_port,
+                                                                guint *priority, gchar **name,
+                                                                guint default_port,
+                                                                gboolean allow_listen,
                                                                 rspamd_mempool_t *pool);
 
 /**
index 196d9cde865a91ded01b42e5a6d4d0f7c28ab801..65cbca10594eaf7d5760406b884b0961cb6a975e 100644 (file)
@@ -1049,6 +1049,7 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str,
                        ret = rspamd_parse_host_port_priority (str, &addrs,
                                        &upstream->weight,
                                        &upstream->name, def_port,
+                                       FALSE,
                                        ups->ctx ? ups->ctx->pool : NULL);
                }
                break;
index 31e06626e1d690c8c3396e0d7dc1ba02ceb8f52d..df970fd017d268f1e3c0c74a2a38e321aeb8d981 100644 (file)
@@ -925,7 +925,7 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd)
                struct rspamadm_lua_repl_context *ctx;
 
                if (rspamd_parse_host_port_priority (serve, &addrs, NULL, &name,
-                               10000, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
+                               10000, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
                        fprintf (stderr, "cannot listen on %s", serve);
                        exit (EXIT_FAILURE);
                }