if (ip != NULL) {
rspamd_inet_addr_t *addr = NULL;
- if (!rspamd_parse_inet_address (&addr, ip, strlen (ip))) {
+ if (!rspamd_parse_inet_address (&addr, ip, strlen (ip),
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
/* Try to resolve */
struct addrinfo hints, *res, *cur;
gint r;
comma = hdr->begin;
}
if (rspamd_parse_inet_address (&addr, comma,
- (hdr->begin + hdr->len) - comma)) {
+ (hdr->begin + hdr->len) - comma,
+ RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) {
/* We have addr now, so check if it is still trusted */
if (ctx->secure_map &&
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
hdr = rspamd_http_message_find_header (msg, alt_hdr_name);
if (hdr) {
- if (rspamd_parse_inet_address (&addr, hdr->begin, hdr->len)) {
+ if (rspamd_parse_inet_address (&addr, hdr->begin, hdr->len,
+ RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) {
/* We have addr now, so check if it is still trusted */
if (ctx->secure_map &&
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
if (recv->real_ip) {
if (!rspamd_parse_inet_address (&task->from_addr,
recv->real_ip,
- strlen (recv->real_ip))) {
+ strlen (recv->real_ip),
+ RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) {
msg_warn_task ("cannot get IP from received header: '%s'",
recv->real_ip);
task->from_addr = NULL;
ref_entry_t ref;
};
+#define RSPAMD_INET_ADDRESS_PARSE_RECEIVED \
+ (RSPAMD_INET_ADDRESS_PARSE_REMOTE|RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)
+
static void
rspamd_mime_header_check_special (struct rspamd_task *task,
struct rspamd_mime_header *rh)
if (brace_pos) {
addr = rspamd_parse_inet_address_pool (data + 1,
brace_pos - data - 1,
- task->task_pool);
+ task->task_pool,
+ RSPAMD_INET_ADDRESS_PARSE_RECEIVED);
if (addr) {
rh->addr = addr;
if (g_ascii_isxdigit (data[0])) {
/* Try to parse IP address */
addr = rspamd_parse_inet_address_pool (data,
- len, task->task_pool);
+ len, task->task_pool, RSPAMD_INET_ADDRESS_PARSE_RECEIVED);
if (addr) {
rh->addr = addr;
rh->real_ip = rspamd_mempool_strdup (task->task_pool,
if (ebrace_pos) {
addr = rspamd_parse_inet_address_pool (obrace_pos + 1,
ebrace_pos - obrace_pos - 1,
- task->task_pool);
+ task->task_pool,
+ RSPAMD_INET_ADDRESS_PARSE_RECEIVED);
if (addr) {
rh->addr = addr;
if (brace_pos) {
addr = rspamd_parse_inet_address_pool (rpart->data + 1,
brace_pos - rpart->data - 1,
- task->task_pool);
+ task->task_pool,
+ RSPAMD_INET_ADDRESS_PARSE_RECEIVED);
if (addr) {
seen_ip_in_data = TRUE;
/* Try to parse IP address */
rspamd_inet_addr_t *addr;
addr = rspamd_parse_inet_address_pool (rpart->data,
- rpart->dlen, task->task_pool);
+ rpart->dlen, task->task_pool,
+ RSPAMD_INET_ADDRESS_PARSE_RECEIVED);
if (addr) {
seen_ip_in_data = TRUE;
rh->addr = addr;
struct rspamd_rcl_struct_parser *pd = ud;
rspamd_inet_addr_t **target;
const gchar *val;
+ gsize size;
target = (rspamd_inet_addr_t **)(((gchar *)pd->user_struct) + pd->offset);
if (ucl_object_type (obj) == UCL_STRING) {
- val = ucl_object_tostring (obj);
+ val = ucl_object_tolstring (obj, &size);
- if (!rspamd_parse_inet_address (target, val, strlen (val))) {
+ if (!rspamd_parse_inet_address (target, val, size,
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
msg_info_config ("parsed nameserver %s from resolv.conf", name);
/* Try to open a connection */
- if (!rspamd_parse_inet_address (&addr, name, strlen (name))) {
+ if (!rspamd_parse_inet_address (&addr, name, strlen (name),
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
msg_warn_config ("cannot parse nameserver address %s", name);
return FALSE;
case 'I':
IF_HEADER (IP_ADDR_HEADER) {
if (!rspamd_parse_inet_address (&task->from_addr,
- hv_tok->begin, hv_tok->len)) {
+ hv_tok->begin, hv_tok->len,
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
msg_err_protocol ("bad ip header: '%T'", hv_tok);
}
else {
}
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;
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);
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;
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] == '[') {
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);
}
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)) &&
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;
}
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);
}
}
- 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));
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)
* @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
*/
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
*/
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
/* 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 (
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);
}
cbd->session = session;
}
- if (rspamd_parse_inet_address (&cbd->addr, msg->host->str, msg->host->len)) {
+ if (rspamd_parse_inet_address (&cbd->addr,
+ msg->host->str, msg->host->len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
/* Host is numeric IP, no need to resolve */
gboolean ret;
if (ip_str) {
ip = lua_ip_new (L, NULL);
- if (!rspamd_parse_inet_address (&ip->addr, ip_str, len)) {
+ if (!rspamd_parse_inet_address (&ip->addr,
+ ip_str, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
msg_warn ("cannot parse ip: %*s", (gint) len, ip_str);
ip->addr = NULL;
}
else {
ip = g_malloc0 (sizeof (struct rspamd_lua_ip));
- if (rspamd_parse_inet_address (&ip->addr, ip_str, strlen (ip_str))) {
+ if (rspamd_parse_inet_address (&ip->addr,
+ ip_str, strlen (ip_str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
rspamd_lua_setclass (L, "rspamd{ip}", -1);
else if (lua_type (L, -1) == LUA_TSTRING) {
host = lua_tostring (L, -1);
- if (rspamd_parse_inet_address (&ip, host, strlen (host))) {
+ if (rspamd_parse_inet_address (&ip,
+ host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
addr = g_alloca (sizeof (*addr));
addr->addr = ip;
}
else if (lua_type (L, -1) == LUA_TSTRING) {
host = lua_tostring (L, -1);
- if (rspamd_parse_inet_address (&ip, host, strlen (host))) {
+ if (rspamd_parse_inet_address (&ip,
+ host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
addr = g_alloca (sizeof (*addr));
addr->addr = ip;
else {
if (!rspamd_parse_inet_address (&addr,
ip_str,
- len)) {
+ len,
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
msg_warn_task ("cannot get IP from received header: '%s'",
ip_str);
}
}
}
- if (rspamd_parse_inet_address (&cbd->addr, host, strlen (host))) {
+ if (rspamd_parse_inet_address (&cbd->addr,
+ host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
rspamd_inet_address_set_port (cbd->addr, port);
/* Host is numeric IP, no need to resolve */
lua_tcp_register_watcher (cbd);
}
}
- if (rspamd_parse_inet_address (&cbd->addr, host, strlen (host))) {
+ if (rspamd_parse_inet_address (&cbd->addr,
+ host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
rspamd_inet_address_set_port (cbd->addr, (guint16)port);
/* Host is numeric IP, no need to resolve */
if (!lua_tcp_make_connection (cbd)) {
if (lua_type (L, -1) == LUA_TSTRING) {
host = luaL_checkstring (L, -1);
- if (rspamd_parse_inet_address (&addr, host, strlen (host))) {
+ if (rspamd_parse_inet_address (&addr,
+ host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
if (port != 0) {
rspamd_inet_address_set_port (addr, port);
}
%% write exec;
if (ip_start && ip_end && ip_end > ip_start) {
- return rspamd_parse_inet_address_pool (ip_start, ip_end - ip_start, pool);
+ return rspamd_parse_inet_address_pool (ip_start, ip_end - ip_start, pool,
+ RSPAMD_INET_ADDRESS_PARSE_NO_UNIX|RSPAMD_INET_ADDRESS_PARSE_REMOTE);
}
return NULL;
exit (1);
}
- if (!rspamd_parse_inet_address (&addr, control_path, strlen (control_path))) {
+ if (!rspamd_parse_inet_address (&addr,
+ control_path, strlen (control_path), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
rspamd_fprintf (stderr, "bad control path: %s\n", control_path);
exit (1);
}
if (rspamd_main->cfg->control_socket_path) {
if (!rspamd_parse_inet_address (&control_addr,
rspamd_main->cfg->control_socket_path,
- strlen (rspamd_main->cfg->control_socket_path))) {
+ strlen (rspamd_main->cfg->control_socket_path),
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
msg_err_main ("cannot parse inet address %s",
rspamd_main->cfg->control_socket_path);
}
RSPAMD_UPSTREAM_PARSE_DEFAULT,
NULL));
up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0);
- rspamd_parse_inet_address (&paddr, "127.0.0.2", strlen ("127.0.0.2"));
+ rspamd_parse_inet_address (&paddr, "127.0.0.2", strlen ("127.0.0.2"),
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT);
g_assert (rspamd_upstream_add_addr (up, paddr));
- rspamd_parse_inet_address (&paddr, "::1", strlen ("::1"));
+ rspamd_parse_inet_address (&paddr, "::1", strlen ("::1"),
+ RSPAMD_INET_ADDRESS_PARSE_DEFAULT);
g_assert (rspamd_upstream_add_addr (up, paddr));
/* Rewind to start */
addr = rspamd_upstream_addr_next (up);