james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r381973 - in /james/server/trunk/src: java/org/apache/james/smtpserver/ test/org/apache/james/smtpserver/
Date Wed, 01 Mar 2006 10:16:15 GMT
Author: bago
Date: Wed Mar  1 02:16:12 2006
New Revision: 381973

URL: http://svn.apache.org/viewcvs?rev=381973&view=rev
Log:
Enforcement for HELO/EHLO commands before MAIL (JAMES-445)
Add failed recipient address to log when authorization fails (JAMES-437)
Added a few SMTPServer tests, removed unused HashMap from SMTPSession.

Modified:
    james/server/trunk/src/java/org/apache/james/smtpserver/MailCmdHandler.java
    james/server/trunk/src/java/org/apache/james/smtpserver/RcptCmdHandler.java
    james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java
    james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/MailCmdHandler.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/smtpserver/MailCmdHandler.java?rev=381973&r1=381972&r2=381973&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/MailCmdHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/MailCmdHandler.java Wed Mar  1
02:16:12 2006
@@ -36,6 +36,11 @@
 
     private final static String SENDER = "SENDER_ADDRESS";     // Sender's email address
 
+    /**
+     * The helo mode set in state object
+     */
+    private final static String CURRENT_HELO_MODE = "CURRENT_HELO_MODE"; // HELO or EHLO
+
     /*
      * handles MAIL command
      *
@@ -65,6 +70,9 @@
         }
         if (session.getState().containsKey(SENDER)) {
             responseString = "503 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_OTHER)+"
Sender already specified";
+            session.writeResponse(responseString);
+        } else if (!session.getState().containsKey(CURRENT_HELO_MODE)) {
+            responseString = "503 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_OTHER)+"
Need HELO or EHLO before MAIL";
             session.writeResponse(responseString);
         } else if (argument == null || !argument.toUpperCase(Locale.US).equals("FROM")
                    || sender == null) {

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/RcptCmdHandler.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/smtpserver/RcptCmdHandler.java?rev=381973&r1=381972&r2=381973&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/RcptCmdHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/RcptCmdHandler.java Wed Mar  1
02:16:12 2006
@@ -96,8 +96,8 @@
                     StringBuffer errorBuffer =
                         new StringBuffer(192)
                                 .append("Error parsing recipient address: ")
-                                .append(recipient)
-                                .append(": did not start and end with < >");
+                                .append("Address did not start and end with < >")
+                                .append(getContext(session,null,recipient));
                     getLogger().error(errorBuffer.toString());
                 }
                 return;
@@ -123,8 +123,7 @@
                     StringBuffer errorBuffer =
                         new StringBuffer(192)
                                 .append("Error parsing recipient address: ")
-                                .append(recipient)
-                                .append(": ")
+                                .append(getContext(session,recipientAddress,recipient))
                                 .append(pe.getMessage());
                     getLogger().error(errorBuffer.toString());
                 }
@@ -149,7 +148,10 @@
                     if (!session.getConfigurationData().getMailServer().isLocalServer(toDomain))
{
                         responseString = "530 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH)+"
Authentication Required";
                         session.writeResponse(responseString);
-                        getLogger().error("Rejected message - authentication is required
for mail request");
+                        StringBuffer sb = new StringBuffer(128);
+                        sb.append("Rejected message - authentication is required for mail
request");
+                        sb.append(getContext(session,recipientAddress,recipient));
+                        getLogger().error(sb.toString());
                         return;
                     }
                 } else {
@@ -168,7 +170,8 @@
                                         .append("User ")
                                         .append(authUser)
                                         .append(" authenticated, however tried sending email
as ")
-                                        .append(senderAddress);
+                                        .append(senderAddress)
+                                        .append(getContext(session,recipientAddress,recipient));
                                 getLogger().error(errorBuffer.toString());
                             }
                             return;
@@ -180,7 +183,13 @@
                 if (!session.getConfigurationData().getMailServer().isLocalServer(toDomain))
{
                     responseString = "550 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH)+"
Requested action not taken: relaying denied";
                     session.writeResponse(responseString);
-                    getLogger().error("Rejected message - " + session.getRemoteIPAddress()
+ " not authorized to relay to " + toDomain);
+                    StringBuffer errorBuffer = new StringBuffer(128)
+                        .append("Rejected message - ")
+                        .append(session.getRemoteIPAddress())
+                        .append(" not authorized to relay to ")
+                        .append(toDomain)
+                        .append(getContext(session,recipientAddress,recipient));
+                    getLogger().error(errorBuffer.toString());
                     return;
                 }
             }
@@ -203,7 +212,8 @@
                               .append("RCPT command had unrecognized/unexpected option ")
                               .append(rcptOptionName)
                               .append(" with value ")
-                              .append(rcptOptionValue);
+                              .append(rcptOptionValue)
+                              .append(getContext(session,recipientAddress,recipient));
                       getLogger().debug(debugBuffer.toString());
                   }
               }
@@ -220,4 +230,16 @@
     }
 
 
+    private String getContext(SMTPSession session, MailAddress recipientAddress, String recipient){
+        StringBuffer sb = new StringBuffer(128);
+        if(null!=recipientAddress) {
+            sb.append(" [to:" + (recipientAddress).toInternetAddress().getAddress() + "]");
+        } else if(null!=recipient) {
+            sb.append(" [to:" + recipient + "]");
+        }
+        if (null!=session.getState().get(SENDER)) {
+            sb.append(" [from:" + ((MailAddress)session.getState().get(SENDER)).toInternetAddress().getAddress()
+ "]");
+        }
+        return sb.toString();
+    } 
 }

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java?rev=381973&r1=381972&r2=381973&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java Wed Mar  1 02:16:12
2006
@@ -78,6 +78,7 @@
     private final static String SENDER = "SENDER_ADDRESS";     // Sender's email address
     private final static String MESG_FAILED = "MESG_FAILED";   // Message failed flag
     private final static String RCPT_LIST = "RCPT_LIST";   // The message recipients
+    private final static String CURRENT_HELO_MODE = "CURRENT_HELO_MODE"; // HELO or EHLO
 
     /**
      * Static Random instance used to generate SMTP ids
@@ -212,12 +213,6 @@
 
 
     /**
-     * The per-handler map to store session scope variables
-     * the various handlers use this for storing state information
-     */
-     private HashMap sessionState = new HashMap();
-
-    /**
      * The per-handler map to store message scope variables
      * the various handlers use this for storing state information
      */
@@ -441,11 +436,20 @@
                   if (mail instanceof Disposable) {
                       ((Disposable) mail).dispose();
                   }
+                  
+                  // remember the ehlo mode
+                  Object currentHeloMode = state.get(CURRENT_HELO_MODE);
+                  
                   mail = null;
                   resetState();
 
                   //reset the message scope state
                   messageState.clear();
+                  
+                  // start again with the old helo mode
+                  if (currentHeloMode != null) {
+                      state.put(CURRENT_HELO_MODE,currentHeloMode);
+                  }
               }
 
             }
@@ -492,7 +496,6 @@
             }
         } finally {
             //Clear all the session state variables
-            sessionState.clear();
             resetHandler();
         }
     }
@@ -872,12 +875,5 @@
         return messageState;
     }
 
-
-    /**
-     * @see org.apache.james.smtpserver.SMTPSession#getSessionState()
-     */
-    public HashMap getSessionState() {
-        return sessionState;
-    }
 
 }

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java?rev=381973&r1=381972&r2=381973&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java Wed Mar  1 02:16:12
2006
@@ -216,12 +216,5 @@
     HashMap getMessageState();
 
 
-    /**
-     * Used for storing Session scope variables
-     *
-     * @return map of session scope param-values
-     */
-    HashMap getSessionState();
-
 }
 

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java?rev=381973&r1=381972&r2=381973&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 Wed Mar  1
02:16:12 2006
@@ -157,6 +157,8 @@
         // no message there, yet
         assertNull("no mail received by mail server", m_mailServer.getLastMail());
 
+        smtpProtocol.helo(InetAddress.getLocalHost());
+
         smtpProtocol.mail(new Address("mail@localhost"));
         smtpProtocol.rcpt(new Address("mail@localhost"));
 
@@ -209,6 +211,7 @@
         assertNull("no mail received by mail server", m_mailServer.getLastMail());
 
         smtpProtocol1.helo(InetAddress.getLocalHost());
+        smtpProtocol2.helo(InetAddress.getLocalHost());
 
         String sender1 = "mail_sender1@localhost";
         String recipient1 = "mail_recipient1@localhost";
@@ -228,6 +231,64 @@
 
         smtpProtocol1.quit();
         smtpProtocol2.quit();
+    }
+
+    public void testTwoMailsInSequence() throws Exception, SMTPException {
+        finishSetUp(m_testConfiguration);
+
+        SMTPProtocol smtpProtocol1 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort);
+        smtpProtocol1.openPort();
+
+        assertEquals("first connection taken", 1, smtpProtocol1.getState());
+
+        // no message there, yet
+        assertNull("no mail received by mail server", m_mailServer.getLastMail());
+
+        smtpProtocol1.helo(InetAddress.getLocalHost());
+
+        String sender1 = "mail_sender1@localhost";
+        String recipient1 = "mail_recipient1@localhost";
+        smtpProtocol1.mail(new Address(sender1));
+        smtpProtocol1.rcpt(new Address(recipient1));
+
+        smtpProtocol1.data(MimeTreeRenderer.getInstance().renderMimePart(createMail()));
+        verifyLastMail(sender1, recipient1, null);
+            
+        String sender2 = "mail_sender2@localhost";
+        String recipient2 = "mail_recipient2@localhost";
+        smtpProtocol1.mail(new Address(sender2));
+        smtpProtocol1.rcpt(new Address(recipient2));
+
+        smtpProtocol1.data(MimeTreeRenderer.getInstance().renderMimePart(createMail()));
+        verifyLastMail(sender2, recipient2, null);
+
+        smtpProtocol1.quit();
+    }
+
+    public void testHeloEnforcement() throws Exception, SMTPException {
+        finishSetUp(m_testConfiguration);
+
+        SMTPProtocol smtpProtocol1 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort);
+        smtpProtocol1.openPort();
+
+        assertEquals("first connection taken", 1, smtpProtocol1.getState());
+
+        // no message there, yet
+        assertNull("no mail received by mail server", m_mailServer.getLastMail());
+
+        String sender1 = "mail_sender1@localhost";
+        try {
+            smtpProtocol1.mail(new Address(sender1));
+            fail("helo not enforced");
+        } catch (SMTPException e) {
+            assertEquals("expected 503 error", 503, e.getCode());
+        }
+        
+        smtpProtocol1.helo(InetAddress.getLocalHost());
+        
+        smtpProtocol1.mail(new Address(sender1));
+        
+        smtpProtocol1.quit();
     }
 
     public void testAuth() throws Exception, SMTPException {



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


Mime
View raw message