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/09/27 19:54:39 UTC

svn commit: r450524 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: SPF.java core/FallBack.java

Author: norman
Date: Wed Sep 27 10:54:39 2006
New Revision: 450524

URL: http://svn.apache.org/viewvc?view=rev&rev=450524
Log:
Add support for fallback (not sure i like the classname yet). See JSPF-31

Added:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/core/FallBack.java
Modified:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.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=450524&r1=450523&r2=450524
==============================================================================
--- 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 Wed Sep 27 10:54:39 2006
@@ -22,6 +22,7 @@
 
 import org.apache.james.jspf.core.DNSService;
 import org.apache.james.jspf.core.Directive;
+import org.apache.james.jspf.core.FallBack;
 import org.apache.james.jspf.core.Logger;
 import org.apache.james.jspf.core.Modifier;
 import org.apache.james.jspf.core.SPF1Constants;
@@ -53,6 +54,8 @@
     private String defaultExplanation = null;
 
     private boolean useBestGuess = false;
+
+    private FallBack fallBack;
     
     /**
      * Uses default Log4JLogger and DNSJava based dns resolver
@@ -81,6 +84,7 @@
         this.dnsProbe = dnsProbe;
         this.parser = new SPF1Parser(logger);
         this.log = logger;
+        this.fallBack =  new FallBack(logger);
     }
 
     /**
@@ -152,6 +156,7 @@
      */
     public SPFInternalResult checkSPF(SPF1Data spfData) throws PermErrorException,
             NoneException, TempErrorException, NeutralException {
+        SPF1Record spfRecord = null;
         String result = SPF1Constants.NEUTRAL;
         String explanation = null;
         
@@ -171,18 +176,26 @@
 
         // No SPF-Record found
         if (spfDnsEntry == null) {
-            // We should use bestguess
             if (useBestGuess == true) {
+                // We should use bestguess
                 spfDnsEntry = SPF1Utils.BEST_GUESS_RECORD;
+                
+            } else if (fallBack.getFallBackEntry(spfData.getCurrentDomain()) != null){
+                // We should use fallback
+                spfRecord = fallBack.getFallBackEntry(spfData.getCurrentDomain());
+                log.debug("Set FallBack SPF-Record:" +spfRecord.toString());
             } else {
                 throw new NoneException("No SPF record found for host: " + spfData.getCurrentDomain());
             }
         }
 
-        // logging
-        log.debug("Start parsing SPF-Record:" + spfDnsEntry);
+        // check if the spfRecord was set before
+        if (spfRecord == null) {
+            // logging
+            log.debug("Start parsing SPF-Record:" + spfDnsEntry);
 
-        SPF1Record spfRecord = parser.parse(spfDnsEntry);
+            spfRecord = parser.parse(spfDnsEntry);
+        }
 
         String qualifier = null;
         boolean hasCommand = false;
@@ -383,5 +396,14 @@
             throw new TempErrorException("Timeout querying dns");
         }
     }
-
+    
+    /**
+     * Return the FallBack object which can be used to 
+     * provide default spfRecords for hosts which have no records
+     * 
+     * @return the FallBack object
+     */
+    public FallBack getFallBack() {
+        return fallBack;
+    }
 }

Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/FallBack.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/core/FallBack.java?view=auto&rev=450524
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/core/FallBack.java (added)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/core/FallBack.java Wed Sep 27 10:54:39 2006
@@ -0,0 +1,152 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jspf.core;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.james.jspf.exceptions.NeutralException;
+import org.apache.james.jspf.exceptions.NoneException;
+import org.apache.james.jspf.exceptions.PermErrorException;
+import org.apache.james.jspf.parser.SPF1Parser;
+
+/**
+ * Class to support Fallback feature
+ */
+public class FallBack {
+
+    private Map fallBackMap;
+
+    private SPF1Parser parser;
+
+    private Logger log;
+
+    public FallBack(Logger log) {
+        this.log = log;
+        fallBackMap = Collections.synchronizedMap(new HashMap());
+        parser = new SPF1Parser(log);
+    }
+
+    /**
+     * Add a fallback entry.
+     * 
+     * @param rawHost the host or ipaddress for which the entry should be added. 
+     * @param rawSpfRecord the spfRecord to add
+     * @throws IllegalArgumentException get thrown on invalid spfRecord
+     */
+    public void addFallBackEntry(String rawHost, String rawSpfRecord)
+            throws IllegalArgumentException {
+        String host;
+        try {
+            log.debug("Start parsing SPF-Record: " + rawSpfRecord);
+            SPF1Record spfRecord = parser.parse(rawSpfRecord);
+            if (rawHost.startsWith("*")) {
+                host = rawHost.substring(1);
+                log.debug("Convert host " + rawHost + " to " + host);
+            } else if (rawHost.endsWith("*")) {
+                int length = rawHost.length();
+                host = rawHost.substring(length - 1, length);
+                log.debug("Convert host " + rawHost + " to " + host);
+            } else {
+                host = rawHost;
+            }
+
+            synchronized (fallBackMap) {
+                fallBackMap.put(host, spfRecord);
+            }
+        } catch (PermErrorException e) {
+            throw new IllegalArgumentException("Invalid SPF-Record: "
+                    + rawSpfRecord);
+        } catch (NoneException e) {
+            throw new IllegalArgumentException("Invalid SPF-Record: "
+                    + rawSpfRecord);
+        } catch (NeutralException e) {
+            throw new IllegalArgumentException("Invalid SPF-Record: "
+                    + rawSpfRecord);
+        }
+
+    }
+
+    /**
+     * Clear all fallBack entries
+     *
+     */
+    public void clearFallBackEntrys() {
+        log.debug("Clear all fallback entries");
+        synchronized (fallBackMap) {
+            fallBackMap.clear();
+        }
+    }
+
+    /**
+     * Remove fallBack entry
+     * 
+     * @param host The host
+     */
+    public void removeFallBackEntrys(String host) {
+        log.debug("Remove fallback entry for host: " + host);
+        synchronized (fallBackMap) {
+            fallBackMap.remove(getRawFallBackEntry(host));
+        }
+    }
+
+    /**
+     * Return the SPF1Record for the given host
+     * 
+     * @param host the hostname or ipaddress
+     * @return the SPF1Record of null if no SPF1Record was found in fallback for the given host
+     */
+    public SPF1Record getFallBackEntry(String host) {
+        Object fallBack = null;
+
+        synchronized (fallBackMap) {
+            fallBack = getRawFallBackEntry(host);
+        }
+
+        if (fallBack != null) {
+            return (SPF1Record) fallBack;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Return the Object stored in the map which match the given host.
+     * Keep in mind that this method should only called in a synchronized method or block
+     * 
+     * @param host the host
+     * @return the stored object for the given host or null
+     */
+    private Object getRawFallBackEntry(String host) {
+        Iterator fallBackIt = fallBackMap.keySet().iterator();
+
+        while (fallBackIt.hasNext()) {
+            String rawHost = fallBackIt.next().toString();
+
+            if ((rawHost.startsWith(".") && host.startsWith(rawHost))
+                    || rawHost.endsWith(".") && host.endsWith(rawHost)) {
+                return fallBackMap.get(rawHost);
+            }
+        }
+        return null;
+    }
+}



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


Re: svn commit: r450524 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: SPF.java core/FallBack.java

Posted by Stefano Bagnara <ap...@bago.org>.
Bernd Fondermann wrote:
> Hi Norman,
> 
>> Add support for fallback (not sure i like the classname yet). See JSPF-31
> 
> just a guess concerning the classname: what kind of fallback is it?
> maybe that should go in the classname...
> 
>  Bernd

The fallback is for the spf record lookup.

The fallback optional (non-standard) behaviour is described here:
http://search.cpan.org/dist/Mail-SPF-Query/lib/Mail/SPF/Query.pm#NON-STANDARD_FEATURES

In my todo list I have to look at the fallback code and other spf code 
to introduce a pattern so that all this optional supports (default 
explanation, fallback, override, best guess) will be much more modular: 
as we did in past with jSPF I think the best way is to implement them 
first, and to search for the pattern to refactor them later.

Stefano


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


Re: svn commit: r450524 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: SPF.java core/FallBack.java

Posted by Bernd Fondermann <be...@googlemail.com>.
Hi Norman,

> Add support for fallback (not sure i like the classname yet). See JSPF-31

just a guess concerning the classname: what kind of fallback is it?
maybe that should go in the classname...

  Bernd

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


Re: svn commit: r450524 - in /james/jspf/trunk/src/main/java/org/apache/james/jspf: SPF.java core/FallBack.java

Posted by Norman Maurer <nm...@byteaction.de>.
Bernd Fondermann schrieb:
> Hi Norman,
>
>> Add support for fallback (not sure i like the classname yet). See 
>> JSPF-31
>
> just a guess concerning the classname: what kind of fallback is it?
> maybe that should go in the classname...
>
>  Bernd
>
Well i think i will name it FallbackPolicy  ;-)

bye
Norman


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