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/07/29 16:48:14 UTC

svn commit: r426812 - in /james/server/trunk/src: java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java test/org/apache/james/smtpserver/SMTPServerTest.java test/org/apache/james/smtpserver/SMTPTestConfiguration.java

Author: norman
Date: Sat Jul 29 07:48:14 2006
New Revision: 426812

URL: http://svn.apache.org/viewvc?rev=426812&view=rev
Log:
Part 2 of refactoring the EHLO/HELO filter command handlers to reject on RCPT.See JAMES-583

Modified:
    james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java?rev=426812&r1=426811&r2=426812&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ReverseEqualsEhloHeloHandler.java Sat Jul 29 07:48:14 2006
@@ -17,8 +17,6 @@
  * under the License.                                           *
  ****************************************************************/
 
-
-
 package org.apache.james.smtpserver.core.filter.fastfail;
 
 import org.apache.avalon.framework.configuration.Configurable;
@@ -32,16 +30,21 @@
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPSession;
 import org.apache.james.util.mail.dsn.DSNStatus;
+import org.apache.mailet.MailAddress;
 
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
 
-public class ReverseEqualsEhloHeloHandler extends AbstractLogEnabled
-        implements CommandHandler, Configurable, Serviceable {
+public class ReverseEqualsEhloHeloHandler extends AbstractLogEnabled implements
+        CommandHandler, Configurable, Serviceable {
+
+    public final static String BAD_EHLO_HELO = "BAD_EHLO_HELO";
 
     private boolean checkAuthNetworks = false;
 
+    private boolean checkAuthUsers = false;
+
     private DNSServer dnsServer = null;
 
     /**
@@ -54,6 +57,12 @@
         if (configRelay != null) {
             setCheckAuthNetworks(configRelay.getValueAsBoolean(false));
         }
+
+        Configuration configAuthUser = handlerConfiguration.getChild(
+                "checkAuthUsers", false);
+        if (configAuthUser != null) {
+            setCheckAuthUsers(configAuthUser.getValueAsBoolean(false));
+        }
     }
 
     /**
@@ -74,6 +83,16 @@
     }
 
     /**
+     * Set to true if Auth users should be included in the EHLO/HELO check
+     * 
+     * @param checkAuthNetworks
+     *            Set to true to enable
+     */
+    public void setCheckAuthUsers(boolean checkAuthUsers) {
+        this.checkAuthUsers = checkAuthUsers;
+    }
+
+    /**
      * Set the DNSServer
      * 
      * @param dnsServer
@@ -88,47 +107,77 @@
      */
     public void onCommand(SMTPSession session) {
         String argument = session.getCommandArgument();
-        String responseString = null;
+        String command = session.getCommandName();
+        if (command.equals("HELO") || command.equals("EHLO")) {
+            checkEhloHelo(session, argument);
+        } else if (command.equals("RCPT")) {
+            reject(session, argument);
+        }
+    }
 
+    /**
+     * Method which get called on HELO/EHLO
+     * 
+     * @param session The SMTPSession
+     * @param argument The argument
+     */
+    private void checkEhloHelo(SMTPSession session, String argument) {
         /**
          * don't check if the ip address is allowed to relay. Only check if it
          * is set in the config. ed.
          */
         if (!session.isRelayingAllowed() || checkAuthNetworks) {
+            boolean badHelo = false;
             try {
                 // get reverse entry
                 String reverse = dnsServer.getByName(
                         session.getRemoteIPAddress()).getHostName();
 
                 if (!argument.equals(reverse)) {
-                    responseString = "501 "
-                            + DSNStatus.getStatus(DSNStatus.PERMANENT,
-                                    DSNStatus.DELIVERY_INVALID_ARG)
-                            + " Provided EHLO " + argument
-                            + " not equal reverse of "
-                            + session.getRemoteIPAddress();
-
-                    session.writeResponse(responseString);
-                    getLogger().info(responseString);
-
-                    // After this filter match we should not call any other handler!
-                    session.setStopHandlerProcessing(true);
+                    badHelo = true;
                 }
             } catch (UnknownHostException e) {
-                responseString = "501 "
-                        + DSNStatus.getStatus(DSNStatus.PERMANENT,
-                                DSNStatus.DELIVERY_INVALID_ARG) + " Ipaddress "
-                        + session.getRemoteIPAddress() + " can not resolved";
+                badHelo = true;
+            }
 
-                session.writeResponse(responseString);
-                getLogger().info(responseString);
+            // bad EHLO/HELO
+            if (badHelo)
+                session.getState().put(BAD_EHLO_HELO, "true");
+        }
+    }
 
-                // After this filter match we should not call any other handler!
-                session.setStopHandlerProcessing(true);
-            }
+    /**
+     * Method which get called on RCPT
+     * 
+     * @param session The SMTPSession
+     * @param argument The argument
+     */
+    private void reject(SMTPSession session, String argument) {
+        MailAddress rcpt = (MailAddress) session.getState().get(
+                SMTPSession.CURRENT_RECIPIENT);
+
+        // not reject it
+        if (session.getState().get(BAD_EHLO_HELO) == null
+                || rcpt.getUser().equalsIgnoreCase("postmaster")
+                || rcpt.getUser().equalsIgnoreCase("abuse"))
+            return;
+
+        // Check if the client was authenticated
+        if (!(session.isAuthRequired() && session.getUser() != null && !checkAuthUsers)) {
+            String responseString = "501 "
+                    + DSNStatus.getStatus(DSNStatus.PERMANENT,
+                            DSNStatus.DELIVERY_INVALID_ARG) + " Provided EHLO "
+                    + argument + " not equal reverse of "
+                    + session.getRemoteIPAddress();
+
+            session.writeResponse(responseString);
+            getLogger().info(responseString);
+
+            // After this filter match we should not call any other handler!
+            session.setStopHandlerProcessing(true);
         }
     }
-    
+
     /**
      * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
      */
@@ -136,7 +185,8 @@
         Collection implCommands = new ArrayList();
         implCommands.add("EHLO");
         implCommands.add("HELO");
-        
+        implCommands.add("RCPT");
+
         return implCommands;
     }
 

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java?rev=426812&r1=426811&r2=426812&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java Sat Jul 29 07:48:14 2006
@@ -419,13 +419,21 @@
     
             String helo1 = "abgsfe3rsf.de";
             String helo2 = "james.apache.org";
+            String mail = "sender";
+            String rcpt = "recipient";
     
             smtpProtocol1.sendCommand("helo", helo1);
+            smtpProtocol1.setSender(mail);
+            smtpProtocol1.addRecipient(rcpt);
+            
             // this should give a 501 code cause the helo not equal reverse of ip
             assertEquals("expected error: helo not equals reverse of ip", 501,
                     smtpProtocol1.getReplyCode());
     
             smtpProtocol1.sendCommand("helo", helo2);
+            smtpProtocol1.setSender(mail);
+            smtpProtocol1.addRecipient(rcpt);
+            
             // helo is resolvable. so this should give a 250 code
             assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());
     
@@ -692,13 +700,21 @@
     
             String ehlo1 = "abgsfe3rsf.de";
             String ehlo2 = "james.apache.org";
-    
+            String mail = "sender";
+            String rcpt = "recipient";
+            
             smtpProtocol1.sendCommand("ehlo", ehlo1);
+            smtpProtocol1.setSender(mail);
+            smtpProtocol1.addRecipient(rcpt);
+            
             // this should give a 501 code cause the ehlo not equals reverse of ip
             assertEquals("expected error: ehlo not equals reverse of ip", 501,
                     smtpProtocol1.getReplyCode());
     
             smtpProtocol1.sendCommand("ehlo", ehlo2);
+            smtpProtocol1.setSender(mail);
+            smtpProtocol1.addRecipient(rcpt);
+            
             // ehlo is resolvable. so this should give a 250 code
             assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
     

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java?rev=426812&r1=426811&r2=426812&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java Sat Jul 29 07:48:14 2006
@@ -185,7 +185,7 @@
         if (m_reverseEqualsHelo || m_reverseEqualsEhlo) {
             DefaultConfiguration d = createHandler(
                     ReverseEqualsEhloHeloHandler.class.getName(), null);
-            d.setAttribute("command", "EHLO,HELO");
+            d.setAttribute("command", "EHLO,HELO,RCPT");
             d.addChild(Util.getValuedConfiguration("checkAuthNetworks",
                     m_checkAuthNetworks + ""));
             config.addChild(d);



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