diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-28 13:37:22 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-28 13:37:22 +0100 |
commit | 04127efebacaaa088668198cdbe52f933367b731 (patch) | |
tree | 5e19bd17ac25d675666f53b19f746681a57e89da /src/libutil | |
parent | 8e5517a62b981fedc9e42d1323b066fadf086f1a (diff) | |
download | rspamd-04127efebacaaa088668198cdbe52f933367b731.tar.gz rspamd-04127efebacaaa088668198cdbe52f933367b731.zip |
[Fix] Distinguish remote and local addrs parsing
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/addr.c | 61 | ||||
-rw-r--r-- | src/libutil/addr.h | 16 | ||||
-rw-r--r-- | src/libutil/map.c | 3 | ||||
-rw-r--r-- | src/libutil/upstream.c | 3 |
4 files changed, 57 insertions, 26 deletions
diff --git a/src/libutil/addr.c b/src/libutil/addr.c index 7ab5e11ee..f6aed8bf4 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -315,8 +315,10 @@ out: } static gboolean -rspamd_parse_unix_path (rspamd_inet_addr_t **target, const char *src, - rspamd_mempool_t *pool) +rspamd_parse_unix_path (rspamd_inet_addr_t **target, + const char *src, gsize len, + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { gchar **tokens, **cur_tok, *p, *pwbuf; glong pwlen; @@ -325,19 +327,29 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, const char *src, rspamd_inet_addr_t *addr; bool has_group = false; - tokens = g_strsplit_set (src, " ,", -1); addr = rspamd_inet_addr_create (AF_UNIX, pool); - rspamd_strlcpy (addr->u.un->addr.sun_path, tokens[0], - sizeof (addr->u.un->addr.sun_path)); - #if defined(FREEBSD) || defined(__APPLE__) - addr->u.un->addr.sun_len = SUN_LEN (&addr->u.un->addr); - #endif - addr->u.un->mode = 00644; addr->u.un->owner = (uid_t)-1; addr->u.un->group = (gid_t)-1; + if (!(how & RSPAMD_INET_ADDRESS_PARSE_REMOTE)) { + tokens = rspamd_string_len_split (src, len, " ,", -1, pool); + + if (tokens[0] == NULL) { + return FALSE; + } + } + else { + rspamd_strlcpy (addr->u.un->addr.sun_path, src, + MIN (len + 1, sizeof (addr->u.un->addr.sun_path))); +#if defined(FREEBSD) || defined(__APPLE__) + addr->u.un->addr.sun_len = SUN_LEN (&addr->u.un->addr); +#endif + return TRUE; + } + + /* Skip for remote */ cur_tok = &tokens[1]; #ifdef _SC_GETPW_R_SIZE_MAX pwlen = sysconf (_SC_GETPW_R_SIZE_MAX); @@ -679,12 +691,13 @@ static gboolean rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, const char *src, gsize srclen, - rspamd_mempool_t *pool) + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { gboolean ret = FALSE; rspamd_inet_addr_t *addr = NULL; union sa_inet su; - const char *end; + const char *end = NULL; char ipbuf[INET6_ADDRSTRLEN + 1]; guint iplen; gulong portnum; @@ -698,8 +711,9 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, rspamd_ip_check_ipv6 (); - if (src[0] == '/' || src[0] == '.') { - return rspamd_parse_unix_path (target, src, pool); + if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_UNIX) && + (src[0] == '/' || src[0] == '.')) { + return rspamd_parse_unix_path (target, src, srclen, pool, how); } if (src[0] == '[') { @@ -726,7 +740,7 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, ret = TRUE; } - if (ret && end[1] == ':') { + if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_PORT) && ret && end[1] == ':') { /* Port part */ rspamd_strtoul (end + 1, srclen - iplen - 3, &portnum); rspamd_inet_address_set_port (addr, portnum); @@ -734,7 +748,8 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, } else { - if ((end = memchr (src, ':', srclen)) != NULL) { + if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_PORT) && + (end = memchr (src, ':', srclen)) != NULL) { /* This is either port number and ipv4 addr or ipv6 addr */ /* Search for another semicolon */ if (memchr (end + 1, ':', srclen - (end - src + 1)) && @@ -789,19 +804,21 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, const char *src, - gsize srclen) + gsize srclen, + enum rspamd_inet_address_parse_flags how) { - return rspamd_parse_inet_address_common (target, src, srclen, NULL); + return rspamd_parse_inet_address_common (target, src, srclen, NULL, how); } rspamd_inet_addr_t * rspamd_parse_inet_address_pool (const char *src, gsize srclen, - rspamd_mempool_t *pool) + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { rspamd_inet_addr_t *ret = NULL; - if (!rspamd_parse_inet_address_common (&ret, src, srclen, pool)) { + if (!rspamd_parse_inet_address_common (&ret, src, srclen, pool, how)) { return NULL; } @@ -1224,7 +1241,8 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs, rspamd_ip_check_ipv6 (); - if (rspamd_parse_inet_address (&cur_addr, begin, len)) { + if (rspamd_parse_inet_address (&cur_addr, + begin, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { if (*addrs == NULL) { *addrs = g_ptr_array_new_full (1, (GDestroyNotify) rspamd_inet_address_free); @@ -1387,7 +1405,8 @@ rspamd_parse_host_port_priority (const gchar *str, } } - if (!rspamd_parse_inet_address (&cur_addr, str, strlen (str))) { + if (!rspamd_parse_inet_address (&cur_addr, + str, strlen (str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { msg_err_pool_check ("cannot parse unix socket definition %s: %s", str, strerror (errno)); diff --git a/src/libutil/addr.h b/src/libutil/addr.h index 06413a51a..6a33ad7ec 100644 --- a/src/libutil/addr.h +++ b/src/libutil/addr.h @@ -86,6 +86,13 @@ rspamd_inet_addr_t *rspamd_inet_address_from_rnds ( gboolean rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target); +enum rspamd_inet_address_parse_flags { + RSPAMD_INET_ADDRESS_PARSE_DEFAULT = 0, + RSPAMD_INET_ADDRESS_PARSE_REMOTE = 1u << 0u, + RSPAMD_INET_ADDRESS_PARSE_NO_UNIX = 1u << 1u, + RSPAMD_INET_ADDRESS_PARSE_NO_PORT = 1u << 2u, +}; + /** * Parse string with ipv4 address of length `len` to `target` which should be * at least sizeof (in4_addr_t) @@ -105,7 +112,8 @@ gboolean rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, * @return */ gboolean rspamd_parse_inet_address_ip (const char *src, - gsize srclen, rspamd_inet_addr_t *target); + gsize srclen, + rspamd_inet_addr_t *target); /** * Try to parse address from string @@ -115,7 +123,8 @@ gboolean rspamd_parse_inet_address_ip (const char *src, */ gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, const char *src, - gsize srclen); + gsize srclen, + enum rspamd_inet_address_parse_flags how); /** * Use memory pool allocated inet address @@ -126,7 +135,8 @@ gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, */ rspamd_inet_addr_t *rspamd_parse_inet_address_pool (const char *src, gsize srclen, - rspamd_mempool_t *pool); + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how); /** * Returns string representation of inet address diff --git a/src/libutil/map.c b/src/libutil/map.c index 19612f8ff..4f0e2354c 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -1469,7 +1469,8 @@ check: /* Try address */ rspamd_inet_addr_t *addr = NULL; - if (rspamd_parse_inet_address (&addr, data->host, strlen (data->host))) { + if (rspamd_parse_inet_address (&addr, data->host, + strlen (data->host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { rspamd_inet_address_set_port (addr, cbd->data->port); g_ptr_array_add (cbd->addrs, (void *)addr); cbd->conn = rspamd_http_connection_new_client ( diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 0637a0ac1..e2bfd94d9 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -788,7 +788,8 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, break; case RSPAMD_UPSTREAM_PARSE_NAMESERVER: addrs = g_ptr_array_sized_new (1); - if (rspamd_parse_inet_address (&addr, str, strlen (str))) { + if (rspamd_parse_inet_address (&addr, str, strlen (str), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { if (ups->ctx) { upstream->name = rspamd_mempool_strdup (ups->ctx->pool, str); } |