Repository: modoboa/modoboa

Issue:
SPF parser failure
Parser can fail because of extra space characters.
```python
 File "/srv/modoboa/instance/manage.py", line 25, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/subcommand/base.py", line 53, in run_from_argv
    return super(SubcommandCommand, self).run_from_argv(argv)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/subcommand/base.py", line 86, in handle
    return command.run_from_argv(argv)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/management/commands/subcommands/_mx.py", line 275, in handle
    self.check_domain(domain, **options)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/management/commands/subcommands/_mx.py", line 210, in check_domain
    domain, "spf", ttl)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/dnstools/models.py", line 37, in get_or_create_for_domain
    record.check_syntax(ttl)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/dnstools/models.py", line 74, in check_syntax
    result = func(self.value)
  File "/srv/modoboa/env/local/lib/python2.7/site-packages/modoboa/dnstools/lib.py", line 173, in check_spf_syntax
    if part[0] in ["+", "-", "~", "?"]:
IndexError: string index out of range
```


Discussion:

Resolving patch:
diff --git a/modoboa/dnstools/lib.py b/modoboa/dnstools/lib.py
--- a/modoboa/dnstools/lib.py
+++ b/modoboa/dnstools/lib.py
@@ -165,6 +165,8 @@ def check_spf_syntax(record):
     modifiers = []
     mechanisms = []
     for part in parts:
+        if part == "":
+            continue
         qualifier = None
         if part[0] in ["+", "-", "~", "?"]:
             qualifier = part[0]
@@ -200,6 +202,8 @@ def check_dkim_syntax(record):
     key = None
     for tag in record.split(";")[1:]:
         tag = tag.strip(" ")
+        if tag == "":
+            continue
         parts = tag.split("=", 1)
         if len(parts) != 2:
             raise DNSSyntaxError(_("Invalid tag {}").format(tag))
@@ -250,6 +254,8 @@ def check_dmarc_syntax(record):
         raise DNSSyntaxError(_("Not a valid DMARC record"))
     tags = {}
     for tag in record.split(";")[1:]:
+        if tag == "":
+            continue
         tag = tag.strip(" ")
         parts = tag.split("=")
         if len(parts) != 2:


Test patch:
diff --git a/modoboa/dnstools/tests.py b/modoboa/dnstools/tests.py
--- a/modoboa/dnstools/tests.py
+++ b/modoboa/dnstools/tests.py
@@ -44,7 +44,7 @@
     "v=spf1 ptr -all",
     "v=spf1 ptr:otherdomain.com -all",
     "v=spf1 exists:example.com -all",
-    "v=spf1 include:example.com -all",
+    "v=spf1 include:example.com  -all",
     "v=spf1 ?include:example.com -all",
     "v=spf1 redirect=example.com",
     "v=spf1 exp=example.com",
@@ -65,7 +65,7 @@
 ]
 
 GOOD_DMARC_RECORDS = [
-    "v=DMARC1; p=reject; pct=100",
+    "v=DMARC1; p=reject;; pct=100",
     "v=DMARC1; p=quarantine; sp=none; adkim=s; aspf=s; "
     "rua=mailto:dmarc-aggrep@ngyn.org; ruf=mailto:toto@test.com!24m; "
     "rf=afrf; pct=100; ri=86400"
@@ -74,7 +74,7 @@
 BAD_DKIM_RECORDS = [
     ("", "Not a valid DKIM record"),
     ("v=DKIM1; toto;p=XXX", "Invalid tag toto"),
-    ("v=DKIM1;k=rsa", "No key found in record"),
+    ("v=DKIM1;;k=rsa", "No key found in record"),
 ]