]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Normalize quoted addresses
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 25 Apr 2016 08:51:00 +0000 (09:51 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 25 Apr 2016 08:51:00 +0000 (09:51 +0100)
src/libmime/email_addr.c
src/libmime/email_addr.h
test/lua/unit/smtp_addr.lua

index afb92b183a9e1a85d37e00e30494b43f4b916dcb..6dfc6f0c627d18cc05ffb1016bab41c1cc4bef03 100644 (file)
 #include "config.h"
 #include "email_addr.h"
 #include "message.h"
+#include "printf.h"
 
 #include "./parsers/smtp_addr_parser.c"
 
 static void
 rspamd_email_addr_dtor (struct rspamd_email_address *addr)
 {
+       if (addr->flags & RSPAMD_EMAIL_ADDR_ALLOCATED) {
+               g_free ((void *)addr->addr);
+       }
        g_slice_free1 (sizeof (*addr), addr);
 }
 
@@ -30,6 +34,7 @@ struct rspamd_email_address *
 rspamd_email_address_from_smtp (const gchar *str, guint len)
 {
        struct rspamd_email_address addr, *ret;
+       gsize nlen;
 
        if (str == NULL || len == 0) {
                return NULL;
@@ -41,6 +46,16 @@ rspamd_email_address_from_smtp (const gchar *str, guint len)
                ret = g_slice_alloc (sizeof (*ret));
                memcpy (ret, &addr, sizeof (addr));
 
+               if ((ret->flags & RSPAMD_EMAIL_ADDR_QUOTED) && ret->addr[0] == '"') {
+                       /* We need to unquote addr */
+                       nlen = ret->domain_len + ret->user_len + 2;
+                       ret->addr = g_malloc (nlen + 1);
+                       ret->addr_len = rspamd_snprintf ((char *)ret->addr, nlen, "%*s@%*s",
+                                       (gint)ret->user_len, ret->user,
+                                       (gint)ret->domain_len, ret->domain);
+                       ret->flags |= RSPAMD_EMAIL_ADDR_ALLOCATED;
+               }
+
                REF_INIT_RETAIN (ret, rspamd_email_addr_dtor);
 
                return ret;
index 1397d84ac7cd7f269129f3f656cc890fa5b9e206..7009c472dd4e72143a7b7ff45b6e2ca97fcd2fc6 100644 (file)
@@ -27,7 +27,8 @@ enum rspamd_email_address_flags {
        RSPAMD_EMAIL_ADDR_BRACED = (1 << 2),
        RSPAMD_EMAIL_ADDR_QUOTED = (1 << 3),
        RSPAMD_EMAIL_ADDR_EMPTY = (1 << 4),
-       RSPAMD_EMAIL_ADDR_SMTP = (1 << 5)
+       RSPAMD_EMAIL_ADDR_SMTP = (1 << 5),
+       RSPAMD_EMAIL_ADDR_ALLOCATED = (1 << 6),
 };
 
 /*
index 4dd6fef87c9289c56029b8c9c39c41af5adea07e..c221950c53e6a4e370f37405bf2cea0075e04215 100644 (file)
@@ -29,8 +29,8 @@ context("SMTP address check functions", function()
       {'<a@example.com>', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
       {'a@example.com', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
       {'a+b@example.com', {user = 'a+b', domain = 'example.com', addr = 'a+b@example.com'}},
-      {'"a"@example.com', {user = 'a', domain = 'example.com', addr = '"a"@example.com'}},
-      {'"a+b"@example.com', {user = 'a+b', domain = 'example.com', addr = '"a+b"@example.com'}},
+      {'"a"@example.com', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
+      {'"a+b"@example.com', {user = 'a+b', domain = 'example.com', addr = 'a+b@example.com'}},
     }
 
     each(function(case)