]> source.dussan.org Git - rspamd.git/commitdiff
* Fix DNS PTR resolving
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 22 Aug 2011 16:31:58 +0000 (20:31 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 22 Aug 2011 16:31:58 +0000 (20:31 +0400)
* Add ability to resolve ip in once_received plugin to avoid temporary DNS fails of SMTP resolving

src/dns.c
src/lua/lua_task.c
src/plugins/lua/once_received.lua
src/smtp.c

index f328b4e66d3cb95dc5c1f4cd527f5f9122feeba6..c2cdc91696ac10c1903c063f56ab6d9bc80aead1 100644 (file)
--- 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:
index 92c57a9d65bc56b4678121bbdb4e69448ff2c821..eb6ba26908370b88a49f29c85f997b832149e9db 100644 (file)
@@ -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++;
                }
index 0de235c01c51a247fe6b7a3f72a0dae4e9882a54..403646489120590fd73478692e5578ee3c9a8cb2 100644 (file)
@@ -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
 
index 0c8b8d5aafdbc73d7053728acb1507aa718a29a6..5e25fb5683cbbc1c2b5635d2de1e91872195020a 100644 (file)
@@ -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);