Author: norman
Date: Fri Nov 17 07:12:23 2006
New Revision: 476171

URL: http://svn.apache.org/viewvc?view=rev&rev=476171
Log:
More refactoring to support JunkScore.See JAMES-614

Modified:
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/SPFHandler.java
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java
    james/server/trunk/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/SPFHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/SPFHandler.java?view=diff&rev=476171&r1=476170&r2=476171
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/SPFHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/SPFHandler.java
 Fri Nov 17 07:12:23 2006
@@ -25,10 +25,8 @@
 import java.util.Collection;
 
 import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.james.jspf.SPF;
 import org.apache.james.jspf.SPF1Utils;
 import org.apache.james.jspf.SPFResult;
@@ -52,8 +50,8 @@
  * <checkAuthNetworks>false&lt/checkAuthNetworks> 
  * </handler>
  */
-public class SPFHandler extends AbstractLogEnabled implements CommandHandler,
-        MessageHandler, Configurable,Initializable {
+public class SPFHandler extends AbstractJunkHandler implements CommandHandler,
+        MessageHandler,Initializable {
 
     public static final String SPF_BLOCKLISTED = "SPF_BLOCKLISTED";
 
@@ -101,6 +99,8 @@
         if (configRelay != null) {
             setCheckAuthNetworks(configRelay.getValueAsBoolean(false));
         }
+        
+        super.configure(handlerConfiguration);
 
     }
     
@@ -164,8 +164,7 @@
         if (session.getCommandName().equals("MAIL")) {
             doSPFCheck(session);
         } else if (session.getCommandName().equals("RCPT")) {
-            rejectSession(session);
-
+            doProcessing(session);
         }
     }
 
@@ -230,18 +229,35 @@
     }
 
     /**
-     * Reject the session with the right responseCode
-     * 
-     * @param session
-     *            The SMTPSession
+     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     */
+    public Collection getImplCommands() {
+        Collection commands = new ArrayList();
+        commands.add("MAIL");
+        commands.add("RCPT");
+
+        return commands;
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.MessageHandler#onMessage(SMTPSession)
      */
-    private void rejectSession(SMTPSession session) {
+    public void onMessage(SMTPSession session) {
+        Mail mail = session.getMail();
+
+        // Store the spf header as attribute for later using
+        mail.setAttribute(SPF_HEADER_MAIL_ATTRIBUTE_NAME, (String) 
session.getState().get(SPF_HEADER));
+    }
+    
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected boolean check(SMTPSession session) {
         MailAddress recipientAddress = (MailAddress) session.getState().get(
                 SMTPSession.CURRENT_RECIPIENT);
         String blocklisted = (String) session.getState().get(SPF_BLOCKLISTED);
-        String blocklistedDetail = (String) session.getState().get(SPF_DETAIL);
-        String tempBlocklisted = (String) session.getState().get(
-                SPF_TEMPBLOCKLISTED);
+        String tempBlocklisted = (String) 
session.getState().get(SPF_TEMPBLOCKLISTED);
 
         // Check if the recipient is postmaster or abuse..
         if (recipientAddress != null
@@ -254,53 +270,72 @@
             session.getState().remove(SPF_DETAIL);
             session.getState().remove(SPF_TEMPBLOCKLISTED);
             session.getState().remove(SPF_HEADER);
+            return false;
         } else {
             // Check if session is blocklisted
-            if (blocklisted != null && blocklisted.equals("true")) {
-
-                String responseString = "530 "
-                        + DSNStatus.getStatus(DSNStatus.PERMANENT,
-                                DSNStatus.SECURITY_AUTH) + " "
-                        + blocklistedDetail;
-                session.writeResponse(responseString);
-
-                session.setStopHandlerProcessing(true);
-
-            } else if (tempBlocklisted != null
-                    && tempBlocklisted.equals("true")) {
-                String responseString = "451 "
-                        + DSNStatus.getStatus(DSNStatus.TRANSIENT,
-                                DSNStatus.NETWORK_DIR_SERVER) + " "
-                        + "Temporarily rejected: Problem on SPF lookup";
-                session.writeResponse(responseString);
-                session.setStopHandlerProcessing(true);
+            if ((blocklisted != null && blocklisted.equals("true")) || 
tempBlocklisted != null) {
+                return true;
             }
         }
-
+        return false;
     }
 
+
     /**
-     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkScoreLogString(org.apache.james.smtpserver.SMTPSession)
      */
-    public Collection getImplCommands() {
-        Collection commands = new ArrayList();
-        commands.add("MAIL");
-        commands.add("RCPT");
+    protected String getJunkScoreLogString(SMTPSession session) {
+        return "Not match SPF-Record. Add junkScore: " + getScore();
+    }
 
-        return commands;
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getRejectLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getRejectLogString(SMTPSession session) {
+        return "Not match SPF-Record. Reject email";
     }
 
     /**
-     * @see org.apache.james.smtpserver.MessageHandler#onMessage(SMTPSession)
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getResponseString(org.apache.james.smtpserver.SMTPSession)
      */
-    public void onMessage(SMTPSession session) {
-        Mail mail = session.getMail();
+    protected String getResponseString(SMTPSession session) {
+        String blocklisted = (String) session.getState().get(SPF_BLOCKLISTED);
+        String blocklistedDetail = (String) session.getState().get(SPF_DETAIL);
+        String tempBlocklisted = (String) session.getState().get(
+                SPF_TEMPBLOCKLISTED);
+        String responseString = null;
+        
+        
+        // Check if session is blocklisted
+        if (blocklisted != null && blocklisted.equals("true")) {
+            responseString = "530 "
+                    + DSNStatus.getStatus(DSNStatus.PERMANENT,
+                            DSNStatus.SECURITY_AUTH) + " "
+                    + blocklistedDetail;
+            session.writeResponse(responseString);
 
-        // Store the spf header as attribute for later using
-        mail.setAttribute(SPF_HEADER_MAIL_ATTRIBUTE_NAME, (String) session
-                .getState().get(SPF_HEADER));
+            session.setStopHandlerProcessing(true);
+
+        } else if (tempBlocklisted != null
+                && tempBlocklisted.equals("true")) {
+            responseString = "451 "
+                    + DSNStatus.getStatus(DSNStatus.TRANSIENT,
+                            DSNStatus.NETWORK_DIR_SERVER) + " "
+                    + "Temporarily rejected: Problem on SPF lookup";
+            session.writeResponse(responseString);
+            session.setStopHandlerProcessing(true);
+        }
+        return responseString;
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getScoreName()
+     */
+    protected String getScoreName() {
+        return "SPFCheck";
     }
     
+    
     /**
      * Inner class to provide a wrapper for loggin to avalon
      */
@@ -427,6 +462,6 @@
         public org.apache.james.jspf.core.Logger getChildLogger(String arg0) {
             return new SPFLogger(logger.getChildLogger(arg0));
         }
-        
+
     }
 }

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java?view=diff&rev=476171&r1=476170&r2=476171
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 Fri Nov 17 07:12:23 2006
@@ -24,10 +24,9 @@
 import java.util.Collection;
 import java.util.Iterator;
 
-import org.apache.avalon.framework.configuration.Configurable;
+
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
@@ -42,8 +41,8 @@
  * This class can be used to reject email with bogus MX which is send from a 
authorized user or an authorized
  * network.
  */
-public class ValidRcptMX extends AbstractLogEnabled implements CommandHandler,
-    Serviceable, Configurable {
+public class ValidRcptMX extends AbstractJunkHandler implements CommandHandler,
+    Serviceable {
 
     private DNSServer dnsServer = null;
 
@@ -51,6 +50,9 @@
 
     private NetMatcher bNetwork = null;
 
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
     public void configure(Configuration arg0) throws ConfigurationException {
 
         Configuration[] badMX = arg0.getChildren("invalidMXNetworks");
@@ -75,6 +77,8 @@
             throw new ConfigurationException(
                 "Please configure at least on invalid MX network");
         }
+        
+        super.configure(arg0);
     }
 
     /**
@@ -112,7 +116,7 @@
      * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
      */
     public void onCommand(SMTPSession session) {
-        doRCPT(session);
+        doProcessing(session);
     }
 
     /**
@@ -125,13 +129,16 @@
         this.dnsServer = dnsServer;
     }
 
-    private void doRCPT(SMTPSession session) {
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected boolean check(SMTPSession session) {
         MailAddress rcpt = (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT);
 
         String domain = rcpt.getHost();
 
         // Email should be deliver local
-        if (domain.equals(LOCALHOST)) return;
+        if (domain.equals(LOCALHOST)) return false;
  
         Iterator mx = dnsServer.findMXRecords(domain).iterator();
 
@@ -144,16 +151,43 @@
 
                     // Check for invalid MX
                     if (bNetwork.matchInetNetwork(ip)) {
-                        String response = "Invalid MX " + ip + " for domain " 
+ rcpt.getHost();
-                        String responseString = "530" + 
DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_AUTH) + " " + 
response;
-                        getLogger().debug(response + ". Reject email");
-                        session.writeResponse(responseString);
-                        return;
+                        return true;
                     }
                 } catch (UnknownHostException e) {
                     // Ignore this
                 }
             }
         }
+        return false;
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkScoreLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getJunkScoreLogString(SMTPSession session) {
+        MailAddress rcpt = (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT);
+        return "Invalid MX " + session.getRemoteIPAddress() + " for domain " + 
rcpt.getHost() + ". Add JunkScore: " + getScore();
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getRejectLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getRejectLogString(SMTPSession session) {
+        MailAddress rcpt = (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT);
+        return "Invalid MX " + session.getRemoteIPAddress() + " for domain " + 
rcpt.getHost() + ". Reject email";   
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getResponseString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getResponseString(SMTPSession session) {
+        return "530" + DSNStatus.getStatus(DSNStatus.PERMANENT, 
DSNStatus.SECURITY_AUTH) + " " + getRejectLogString(session) ;   
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getScoreName()
+     */
+    protected String getScoreName() {
+        return "ValidRcptMXCheck";
     }
 }

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java?view=diff&rev=476171&r1=476170&r2=476171
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 Fri Nov 17 07:12:23 2006
@@ -33,7 +33,6 @@
 import org.apache.james.services.DNSServer;
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPSession;
-import org.apache.james.util.junkscore.JunkScore;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
 

Modified: 
james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java?view=diff&rev=476171&r1=476170&r2=476171
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java 
(original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java 
Fri Nov 17 07:12:23 2006
@@ -30,6 +30,8 @@
 import org.apache.james.smtpserver.core.filter.fastfail.SPFHandler;
 import org.apache.james.test.mock.avalon.MockLogger;
 import org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreImpl;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 
@@ -476,5 +478,34 @@
         assertNotNull("Header should present", mockedSMTPSession.getState()
                 .get(SPFHandler.SPF_HEADER));
         assertFalse(mockedSMTPSession.getStopHandlerProcessing());
+    }
+    
+    public void testSPFfailAddJunkScore() throws Exception {
+        setupMockedSMTPSession("192.168.100.1", "spf2.james.apache.org",
+                new MailAddress("[EMAIL PROTECTED]"), new MailAddress(
+                        "[EMAIL PROTECTED]"));
+        mockedSMTPSession.getState().put(JunkScore.JUNK_SCORE, new 
JunkScoreImpl());
+        
+        SPFHandler spf = new SPFHandler();
+
+        ContainerUtil.enableLogging(spf, new MockLogger());
+        spf.setAction("junkScore");
+        spf.setScore(20);
+        spf.setDNSService(mockedDnsService);     
+        
+        spf.initialize();
+
+        runHandlers(spf, mockedSMTPSession);
+
+        assertNotNull("reject", mockedSMTPSession.getState().get(
+                SPFHandler.SPF_BLOCKLISTED));
+        assertNotNull("blocked", mockedSMTPSession.getState().get(
+                SPFHandler.SPF_DETAIL));
+        assertNull("No tempError", mockedSMTPSession.getState().get(
+                SPFHandler.SPF_TEMPBLOCKLISTED));
+        assertNotNull("Header should present", mockedSMTPSession.getState()
+                .get(SPFHandler.SPF_HEADER));
+        assertFalse("Not stopped", 
mockedSMTPSession.getStopHandlerProcessing());
+        assertEquals("Score added",((JunkScore) 
mockedSMTPSession.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("SPFCheck"),
 20.0, 0d);
     }
 }

Modified: 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java?view=diff&rev=476171&r1=476170&r2=476171
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java 
(original)
+++ 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java 
Fri Nov 17 07:12:23 2006
@@ -33,6 +33,8 @@
 import org.apache.james.services.DNSServer;
 import org.apache.james.smtpserver.core.filter.fastfail.ValidRcptMX;
 import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreImpl;
 import org.apache.mailet.MailAddress;
 
 import junit.framework.TestCase;
@@ -55,6 +57,7 @@
     private SMTPSession setupMockedSMTPSession(final MailAddress rcpt) {
         SMTPSession session = new AbstractSMTPSession() {
             HashMap state = new HashMap();
+            boolean stopProcessing = false;
 
             public Map getState() {
                 state.put(SMTPSession.CURRENT_RECIPIENT, rcpt);
@@ -64,6 +67,14 @@
             public void writeResponse(String resp) {
                 response = resp;
             }
+            
+            public String getRemoteIPAddress() {
+                return "127.0.0.1";
+            }
+            
+            public void setStopHandlerProcessing(boolean stopProcessing) {
+                this.stopProcessing = stopProcessing;
+            }
 
         };
         return session;
@@ -109,5 +120,28 @@
         handler.onCommand(setupMockedSMTPSession(new MailAddress("test@" + 
INVALID_HOST)));
 
         assertNotNull("Reject", response);
+    }
+    
+
+    public void testAddJunkScoreLoopbackMX() throws ParseException {
+        Collection bNetworks = new ArrayList();
+        bNetworks.add("127.0.0.1");
+        
+        SMTPSession session = setupMockedSMTPSession(new MailAddress("test@" + 
INVALID_HOST));
+        session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
+        
+        DNSServer dns = setupMockedDNSServer();
+        ValidRcptMX handler = new ValidRcptMX();
+        handler.setScore(20);
+        handler.setAction("junkScore");
+
+        ContainerUtil.enableLogging(handler, new MockLogger());
+
+        handler.setDNSServer(dns);
+        handler.setBannedNetworks(bNetworks, dns);
+        handler.onCommand(session);
+
+        assertNull("Not Reject", response);
+        assertEquals("JunkScore added",((JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidRcptMXCheck"),20.0,
 0d);
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to