From: Vsevolod Stakhov Date: Mon, 22 Aug 2011 16:31:58 +0000 (+0400) Subject: * Fix DNS PTR resolving X-Git-Tag: 0.4.4~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=623320f7c5e6fd2a6a6502978718350533990dfc;p=rspamd.git * Fix DNS PTR resolving * Add ability to resolve ip in once_received plugin to avoid temporary DNS fails of SMTP resolving --- diff --git a/src/dns.c b/src/dns.c index f328b4e66..c2cdc9169 100644 --- a/src/dns.c +++ b/src/dns.c @@ -422,18 +422,15 @@ format_dns_name (struct rspamd_dns_request *req, const gchar *name, guint namele } static void -make_ptr_req (struct rspamd_dns_request *req, struct in_addr addr) +make_ptr_req (struct rspamd_dns_request *req, struct in_addr *addr) { gchar ipbuf[sizeof("255.255.255.255.in-addr.arpa")]; - guint32 a = ntohl (addr.s_addr), r; - guint16 *p; + guint32 r; + guint16 *p; + guint8 *addr_p = (guint8 *)&addr->s_addr; r = rspamd_snprintf (ipbuf, sizeof(ipbuf), "%d.%d.%d.%d.in-addr.arpa", - (gint)(guint8)((a ) & 0xff), - (gint)(guint8)((a>>8 ) & 0xff), - (gint)(guint8)((a>>16) & 0xff), - (gint)(guint8)((a>>24) & 0xff)); - + addr_p[3], addr_p[2], addr_p[1], addr_p[0]); allocate_packet (req, r); make_dns_header (req); format_dns_name (req, ipbuf, r); @@ -1143,7 +1140,7 @@ make_dns_request (struct rspamd_dns_resolver *resolver, { va_list args; struct rspamd_dns_request *req; - struct in_addr addr; + struct in_addr *addr; const gchar *name, *service, *proto; gint r; struct dns_header *header; @@ -1164,7 +1161,7 @@ make_dns_request (struct rspamd_dns_resolver *resolver, va_start (args, type); switch (type) { case DNS_REQUEST_PTR: - addr = va_arg (args, struct in_addr); + addr = va_arg (args, struct in_addr *); make_ptr_req (req, addr); break; case DNS_REQUEST_MX: diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 92c57a9d6..eb6ba2690 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -668,7 +668,8 @@ lua_task_resolve_dns_ptr (lua_State * L) msg_info ("invalid parameters passed to function"); return 0; } - if (make_dns_request (task->resolver, task->s, task->task_pool, lua_dns_callback, (void *)cd, DNS_REQUEST_PTR, ina)) { + if (make_dns_request (task->resolver, task->s, task->task_pool, + lua_dns_callback, (void *)cd, DNS_REQUEST_PTR, ina)) { task->dns_requests ++; task->save.saved++; } diff --git a/src/plugins/lua/once_received.lua b/src/plugins/lua/once_received.lua index 0de235c01..403646489 100644 --- a/src/plugins/lua/once_received.lua +++ b/src/plugins/lua/once_received.lua @@ -6,6 +6,32 @@ local symbol_strict = nil local bad_hosts = {} local good_hosts = {} +function recv_dns_cb(task, to_resolve, results, err) + if not results then + task:insert_result(symbol_strict, 1) + else + rspamd_logger.info(string.format('SMTP resolver failed to resolve: %s is %s', to_resolve, results[1])) + local i = true + for _,h in ipairs(bad_hosts) do + if string.find(results[1], h) then + -- Check for good hostname + if good_hosts then + for _,gh in ipairs(good_hosts) do + if string.find(results[1], gh) then + i = false + break + end + end + end + if i then + task:insert_result(symbol_strict, 1, h) + return + end + end + end + end +end + function check_quantity_received (task) local recvh = task:get_received_headers() if table.maxn(recvh) <= 1 then @@ -18,7 +44,12 @@ function check_quantity_received (task) end -- Unresolved host if not r['real_hostname'] or string.lower(r['real_hostname']) == 'unknown' or string.match(r['real_hostname'], '^%d+%.%d+%.%d+%.%d+$') then - task:insert_result(symbol_strict, 1) + if r['real_ip'] then + -- Try to resolve it again + task:resolve_dns_ptr(r['real_ip'], 'recv_dns_cb') + else + task:insert_result(symbol_strict, 1) + end return end @@ -69,9 +100,17 @@ if opts then rspamd_config:register_virtual_symbol(symbol_strict, 1.0) end elseif n == 'bad_host' then - bad_hosts = v + if type(v) == 'string' then + bad_hosts[1] = v + else + bad_hosts = v + end elseif n == 'good_host' then - good_hosts = v + if type(v) == 'string' then + good_hosts[1] = v + else + good_hosts = v + end end end diff --git a/src/smtp.c b/src/smtp.c index 0c8b8d5aa..5e25fb568 100644 --- a/src/smtp.c +++ b/src/smtp.c @@ -690,7 +690,8 @@ accept_socket (gint fd, short what, void *arg) /* Set up async session */ session->s = new_async_session (session->pool, free_smtp_session, session); session->state = SMTP_STATE_RESOLVE_REVERSE; - if (! make_dns_request (session->resolver, session->s, session->pool, smtp_dns_cb, session, DNS_REQUEST_PTR, session->client_addr)) { + if (! make_dns_request (session->resolver, session->s, session->pool, + smtp_dns_cb, session, DNS_REQUEST_PTR, &session->client_addr)) { msg_err ("cannot resolve %s", inet_ntoa (session->client_addr)); g_free (session); close (nfd);