You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/10/10 15:26:39 UTC

svn commit: r454733 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: SPF.java policies/SPFRetriever.java policies/SPFRetrieverPolicy.java

Author: norman
Date: Tue Oct 10 06:26:36 2006
New Revision: 454733

URL: http://svn.apache.org/viewvc?view=rev&rev=454733
Log:
Add an other SPFRetriever which can be used to strict use RFC. See JSPF-37

Added:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java   (with props)
Modified:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java?view=diff&rev=454733&r1=454732&r2=454733
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java Tue Oct 10 06:26:36 2006
@@ -41,6 +41,7 @@
 import org.apache.james.jspf.policies.Policy;
 import org.apache.james.jspf.policies.ParseRecordPolicy;
 import org.apache.james.jspf.policies.SPFRetriever;
+import org.apache.james.jspf.policies.SPFRetrieverPolicy;
 import org.apache.james.jspf.policies.local.BestGuessPolicy;
 import org.apache.james.jspf.policies.local.DefaultExplanationPolicy;
 import org.apache.james.jspf.policies.local.FallbackPolicy;
@@ -72,7 +73,6 @@
      */
     public static final String TRUSTED_FORWARDER_HOST = "spf.trusted-forwarder.org";
 
-
     public boolean useBestGuess = false;
 
     private FallbackPolicy fallBack;
@@ -80,6 +80,8 @@
     private OverridePolicy override;
     
     private boolean useTrustedForwarder = false;
+    
+    private boolean mustEquals = false;
 
     /**
      * Uses passed logger and passed dnsServicer
@@ -200,7 +202,11 @@
             policies.add(override);
         }
         
-        policies.add(new SPFRetriever(dnsProbe));
+        if (mustEquals) {
+            policies.add(new SPFRetrieverPolicy(dnsProbe));
+        } else {
+            policies.add(new SPFRetriever(dnsProbe));
+        }
         
         if (useBestGuess) {
             policies.add(new BestGuessPolicy());
@@ -297,5 +303,15 @@
             override = new OverridePolicy(this.log, parser);
         }
         return override;
+    }
+    
+    /**
+     * Set to true if a PermError should returned when a domain publish a SPF-Type
+     * and TXT-Type SPF-Record and both are not equals. Defaults false
+     * 
+     * @param mustEquals true or false
+     */
+    public synchronized void setSPFMustEqualsTXT(boolean mustEquals) {
+        this.mustEquals = mustEquals;
     }
 }

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java?view=diff&rev=454733&r1=454732&r2=454733
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java Tue Oct 10 06:26:36 2006
@@ -12,7 +12,7 @@
 import java.util.List;
 
 /**
- * Get the raw dns txt entry which contains a spf entry
+ * Get the raw dns txt or spf entry which contains a spf entry
  */
 public class SPFRetriever extends AbstractNestedPolicy {
     /**
@@ -55,9 +55,7 @@
     }
 
     /**
-     * Get the SPF-Record for a server given it's version
-     * 
-     * TODO: support SPF Records too. This will be done if dnsjava support it!
+     * Get the SPF-Record for a server
      * 
      * @param dns
      *            The dns service to query
@@ -71,12 +69,13 @@
      * @throws TempErrorException
      *             if the lookup result was "TRY_AGAIN"
      */
-    private String retrieveSpfRecord(String hostname)
+    protected String retrieveSpfRecord(String hostname)
             throws PermErrorException, TempErrorException {
 
-        String returnValue = null;
         try {
+            // first check for SPF-Type records
             List spfR = dns.getRecords(hostname, DNSService.SPF);
+            
             if (spfR == null || spfR.isEmpty()) {
                 // do DNS lookup for TXT
                 spfR = dns.getRecords(hostname, DNSService.TXT);
@@ -84,36 +83,62 @@
     
             // process returned records
             if (spfR != null && !spfR.isEmpty()) {
-    
-                Iterator all = spfR.iterator();
-    
-                while (all.hasNext()) {
-                    // DO NOT trim the result!
-                    String compare = all.next().toString();
-    
-                    // TODO is this correct? we remove the first and last char if the
-                    // result has an initial " 
-                    // remove '"'
-                    if (compare.charAt(0)=='"') {
-                        compare = compare.toLowerCase().substring(1,
-                                compare.length() - 1);
-                    }
-    
-                    // We trim the compare value only for the comparison
-                    if (compare.toLowerCase().trim().startsWith(SPF1Constants.SPF_VERSION + " ") || compare.trim().equalsIgnoreCase(SPF1Constants.SPF_VERSION)) {
-                        if (returnValue == null) {
-                            returnValue = compare;
-                        } else {
-                            throw new PermErrorException(
-                                    "More than 1 SPF record found for host: " + hostname);
-                        }
-                    }
-                }
+                return extractSPFRecord(spfR);
+            } else {
+                return null;
             }
-            return returnValue;
         } catch (DNSService.TimeoutException e) {
             throw new TempErrorException("Timeout querying dns");
         }
     }
+    
+    /**
+     * Return the extracted SPF-Record 
+     *  
+     * @param spfR the List which holds TXT/SPF - Records
+     * @return returnValue the extracted SPF-Record
+     * @throws PermErrorException if more then one SPF - Record was found in the 
+     *                            given List.
+     */
+    protected String extractSPFRecord(List spfR) throws PermErrorException {
+       String returnValue = null;
+        Iterator all = spfR.iterator();
+           
+        while (all.hasNext()) {
+            // DO NOT trim the result!
+            String compare = all.next().toString();
+
+            // TODO is this correct? we remove the first and last char if the
+            // result has an initial " 
+            // remove '"'
+            if (compare.charAt(0)=='"') {
+                compare = compare.toLowerCase().substring(1,
+                        compare.length() - 1);
+            }
+
+            // We trim the compare value only for the comparison
+            if (compare.toLowerCase().trim().startsWith(SPF1Constants.SPF_VERSION + " ") || compare.trim().equalsIgnoreCase(SPF1Constants.SPF_VERSION)) {
+                if (returnValue == null) {
+                    returnValue = compare;
+                } else {
+                    throw new PermErrorException(
+                            "More than 1 SPF record found");
+                }
+            }
+        }
+        
+        return returnValue;
+    }
+    
+
+    /**
+     * Return the DNSService
+     * 
+     * @return the dns
+     */
+    protected DNSService getDNSService() {
+        return dns;
+    }
+
 
 }

Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java?view=auto&rev=454733
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java (added)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java Tue Oct 10 06:26:36 2006
@@ -0,0 +1,68 @@
+package org.apache.james.jspf.policies;
+
+import org.apache.james.jspf.core.DNSService;
+import org.apache.james.jspf.exceptions.PermErrorException;
+import org.apache.james.jspf.exceptions.TempErrorException;
+
+import java.util.List;
+
+/**
+ * Get the raw dns txt or spf entry which contains a spf entry. If a domain
+ * publish both, and both are not equals it throws a PermError
+ */
+public class SPFRetrieverPolicy extends SPFRetriever {
+
+    public SPFRetrieverPolicy(DNSService dns) {
+        super(dns);
+    }
+
+    /**
+     * Get the SPF-Record for a server
+     * 
+     * 
+     * @param dns
+     *            The dns service to query
+     * @param hostname
+     *            The hostname for which we want to retrieve the SPF-Record
+     * @param spfVersion
+     *            The SPF-Version which should used.
+     * @return The SPF-Record if one is found.
+     * @throws PermErrorException
+     *             if more then one SPF-Record was found, or one SPF-Type SPF-Record 
+     *             and one TXT-Type SPF-Record was published and these are not equals.
+     * @throws TempErrorException
+     *             if the lookup result was "TRY_AGAIN"
+     */
+    protected String retrieveSpfRecord(String hostname)
+            throws PermErrorException, TempErrorException {
+
+        try {
+            String spfR1 = null;
+            String spfR2 = null;
+            // do DNS lookup for SPF-Type
+            List spfR = getDNSService().getRecords(hostname, DNSService.SPF);
+
+            // do DNS lookup for TXT-Type
+            List spfTxtR = getDNSService().getRecords(hostname, DNSService.TXT);
+            
+            if (spfR != null) spfR1 = extractSPFRecord(spfR);
+            if (spfTxtR != null) spfR2 = extractSPFRecord(spfTxtR);
+            
+            if (spfR1 != null && spfR2 == null) {
+                return spfR1;
+            } else if (spfR1 == null && spfR2 != null) {
+                return spfR2;
+            } else if (spfR1 != null && spfR2 != null) {
+                if (spfR1.toLowerCase().equals(spfR2.toLowerCase()) == false) {
+                    throw new PermErrorException("Published SPF records not equals");
+                } else {
+                    return spfR1;
+                }
+            } else {
+                return null;
+            }
+        } catch (DNSService.TimeoutException e) {
+            throw new TempErrorException("Timeout querying dns");
+        }
+    }
+}
\ No newline at end of file

Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/SPFRetrieverPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org