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/23 13:10:52 UTC

svn commit: r424709 - in /james/server/trunk/src: conf/ java/org/apache/james/pop3server/ java/org/apache/james/smtpserver/ java/org/apache/james/smtpserver/core/ java/org/apache/james/util/ test/org/apache/james/pop3server/ test/org/apache/james/smtps...

Author: norman
Date: Sun Jul 23 04:10:51 2006
New Revision: 424709

URL: http://svn.apache.org/viewvc?rev=424709&view=rev
Log:
Add support for RoaminUsers (pop before smtp). See JAMES-569

Added:
    james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java
    james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java
Modified:
    james/server/trunk/src/conf/james-config.xml
    james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java
    james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java
    james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java
    james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java
    james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java
    james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java

Modified: james/server/trunk/src/conf/james-config.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/src/conf/james-config.xml?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/conf/james-config.xml (original)
+++ james/server/trunk/src/conf/james-config.xml Sun Jul 23 04:10:51 2006
@@ -922,11 +922,15 @@
             <!-- Load the core filter command handlers-->
             <handler class="org.apache.james.smtpserver.core.filter.CoreFilterCmdHandlerLoader"></handler>        
 
+            <!-- This connect handler can be used to enable RoaminUsers support (pop-before-smtp) -->
+            <!--
+            <handler class="org.apache.james.smtpserver.core.RoaminUsersHandler"></handler>
+            -->
+            
             <!-- This command handler check against RBL-Lists -->
             <!-- If getDetail is set to true it try to retrieve information from TXT Record -->
             <!-- why the ip was blocked. Default to false -->            
             <!--
-            
             <handler class="org.apache.james.smtpserver.core.filter.fastfail.DNSRBLHandler" command="RCPT">
                <getDetail> false </getDetail>
                <rblservers>

Modified: james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java Sun Jul 23 04:10:51 2006
@@ -19,6 +19,7 @@
 
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.james.services.MailRepository;
+import org.apache.james.util.RoaminUsersHelper;
 
 /**
   * Handles PASS command
@@ -51,6 +52,10 @@
                     }
                     session.setUserInbox(inbox);
                     session.stat();
+                    
+                    // Store the ipAddress to use it later for pop before smtp 
+                    RoaminUsersHelper.addIPAddress(session.getRemoteIPAddress());
+                    
                     StringBuffer responseBuffer =
                         new StringBuffer(64)
                                 .append(POP3Handler.OK_RESPONSE)

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java?rev=424709&r1=424708&r2=424709&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 Sun Jul 23 04:10:51 2006
@@ -437,6 +437,13 @@
     public boolean isRelayingAllowed() {
         return relayingAllowed;
     }
+    
+    /**
+     * @see org.apache.james.smtpserver.SMTPSession#setRelayingAllowed(boolean relayingAllowed)
+     */
+    public void setRelayingAllowed(boolean relayingAllowed) {
+        this.relayingAllowed = relayingAllowed;
+    }
 
     /**
      * @see org.apache.james.smtpserver.SMTPSession#isAuthRequired()

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java Sun Jul 23 04:10:51 2006
@@ -17,8 +17,6 @@
 
 package org.apache.james.smtpserver;
 
-import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
-import org.apache.avalon.excalibur.pool.ObjectFactory;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.container.ContainerUtil;

Modified: james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java?rev=424709&r1=424708&r2=424709&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 Sun Jul 23 04:10:51 2006
@@ -165,6 +165,13 @@
      * @return the relaying status
      */
     boolean isRelayingAllowed();
+    
+    /**
+     * Set if reallying is allowed
+     * 
+     * @param relayingAllowed
+     */
+    void setRelayingAllowed(boolean relayingAllowed);
 
     /**
      * Returns whether Authentication is required or not

Added: james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java?rev=424709&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java (added)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java Sun Jul 23 04:10:51 2006
@@ -0,0 +1,72 @@
+/***********************************************************************
+ * Copyright (c) 2000-2006 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.smtpserver.core;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.james.smtpserver.ConnectHandler;
+import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.util.RoaminUsersHelper;
+
+/**
+ * This ConnectHandler can be used to activate pop-before-smtp
+ */
+public class RoaminUsersHandler implements ConnectHandler, Configurable {
+
+    /**
+     * The time after which ipAddresses should be handled as expired
+     */
+    private long expireTime;
+
+    /**
+     * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
+     */
+    public void configure(Configuration arg0) throws ConfigurationException {
+        Configuration config = arg0.getChild("expireTime", false);
+
+        if (config != null) {
+            setExpireTime(config.getValueAsLong(RoaminUsersHelper.EXPIRE_TIME));
+        }
+    }
+
+    /**
+     * Set the time after which an ipAddresses should be handled as expired
+     * 
+     * @param expireTime The time in ms
+     */
+    public void setExpireTime(long expireTime) {
+        this.expireTime = expireTime;
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.ConnectHandler#onConnect(SMTPSession)
+     */
+    public void onConnect(SMTPSession session) {
+
+        // some kind of random cleanup process
+        if (Math.random() > 0.5) {
+            RoaminUsersHelper.removeExpiredIP(expireTime);
+        }
+
+        // Check if the ip is allowed to relay
+        if (RoaminUsersHelper.isAuthorized(session.getRemoteIPAddress())) {
+            session.setRelayingAllowed(true);
+        }
+    }
+
+}

Added: james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java?rev=424709&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java (added)
+++ james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java Sun Jul 23 04:10:51 2006
@@ -0,0 +1,103 @@
+/***********************************************************************
+ * Copyright (c) 2000-2006 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.util;
+
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * Helper class which is used to store ipAddresses and timestamps for pop before
+ * smtp support.
+ */
+public class RoaminUsersHelper {
+
+    private RoaminUsersHelper() {
+    }
+
+    /**
+     * The map in which the ipAddresses and timestamp stored
+     */
+    public static HashMap ipMap = new HashMap();
+
+    /**
+     * Default expire time in ms (1 hour)
+     */
+    public static final long EXPIRE_TIME = 216000000;
+
+    /**
+     * Return true if the ip is authorized to relay
+     * 
+     * @param ipAddress
+     *            The ipAddress
+     * @return true if authorized. Else false
+     */
+    public static synchronized boolean isAuthorized(String ipAddress) {
+
+        if (ipMap.containsKey(ipAddress)) {
+            return true;
+
+        }
+        return false;
+    }
+
+    /**
+     * Add the ipAddress to the authorized ipAddresses
+     * 
+     * @param ipAddress
+     *            The ipAddress
+     */
+    public static synchronized void addIPAddress(String ipAddress) {
+        ipMap.put(ipAddress, Long.toString(System.currentTimeMillis()));
+    }
+
+    /**
+     * @see #removeExpiredIP(String, long)
+     */
+    public static synchronized void removeExpiredIP() {
+        removeExpiredIP(EXPIRE_TIME);
+    }
+
+    /**
+     * Remove all ipAddress from the authorized map which are older then the
+     * given time
+     * 
+     * @param clearTime
+     *            The time in milliseconds after which an ipAddress should be
+     *            handled as expired
+     */
+    public static synchronized void removeExpiredIP(long clearTime) {
+        Iterator storedIP = ipMap.keySet().iterator();
+        long currTime = System.currentTimeMillis();
+
+        while (storedIP.hasNext()) {
+            String key = storedIP.next().toString();
+            long storedTime = Long.parseLong((String) ipMap.get(key));
+
+            // remove the ip from the map when it is expired
+            if ((currTime - clearTime) > storedTime) {
+                ipMap.remove(key);
+            }
+        }
+    }
+
+    /**
+     * Remove all ipAddresses from the authorized map
+     */
+    public static synchronized void clearIP() {
+        ipMap.clear();
+    }
+}

Modified: james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java Sun Jul 23 04:10:51 2006
@@ -35,6 +35,7 @@
 import org.apache.james.test.mock.james.MockMailServer;
 import org.apache.james.test.util.Util;
 import org.apache.james.userrepository.MockUsersRepository;
+import org.apache.james.util.RoaminUsersHelper;
 import org.apache.james.util.connection.SimpleConnectionManager;
 import org.apache.mailet.MailAddress;
 
@@ -288,6 +289,21 @@
                 pop3Protocol2.disconnect();
             }
         }
+    }
+    
+    public void testIpStored() throws Exception {
+        finishSetUp(m_testConfiguration);
+
+        m_pop3Protocol = new POP3Client();
+        m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
+
+        String pass = "password";
+        m_usersRepository.addUser("foo", pass);
+        m_mailServer.setUserInbox("foo", new MockMailRepository());
+
+        m_pop3Protocol.login("foo", pass);
+        assertEquals(1, m_pop3Protocol.getState());
+        assertTrue(RoaminUsersHelper.isAuthorized("127.0.0.1"));
     }
 
 }

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java Sun Jul 23 04:10:51 2006
@@ -246,7 +246,6 @@
             }
 
             public int getRcptCount() {
-                // TODO Auto-generated method stub
                 return 0;
             }
 
@@ -264,6 +263,11 @@
 
             public void resetConnectionState() {
                 connectionState.clear();
+            }
+
+            public void setRelayingAllowed(boolean relayingAllowed) {
+                throw new UnsupportedOperationException(
+                "Unimplemented mock service");
             }
 
         };

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?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java Sun Jul 23 04:10:51 2006
@@ -294,6 +294,11 @@
                 connectionState.clear();
             }
 
+            public void setRelayingAllowed(boolean relayingAllowed) {
+                throw new UnsupportedOperationException(
+                "Unimplemented mock service");
+            }
+
         };
     }
 

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java?rev=424709&r1=424708&r2=424709&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java Sun Jul 23 04:10:51 2006
@@ -151,26 +151,6 @@
                         "Unimplemented mock service");
             }
 
-            public void setBlockListed(boolean blocklisted) {
-                throw new UnsupportedOperationException(
-                        "Unimplemented mock service");
-            }
-
-            public boolean isBlockListed() {
-                throw new UnsupportedOperationException(
-                        "Unimplemented mock service");
-            }
-
-            public void setBlockListedDetail(String detail) {
-                throw new UnsupportedOperationException(
-                        "Unimplemented mock service");
-            }
-
-            public String getBlockListedDetail() {
-                throw new UnsupportedOperationException(
-                        "Unimplemented mock service");
-            }
-
             public boolean isRelayingAllowed() {
                 throw new UnsupportedOperationException(
                         "Unimplemented mock service");
@@ -227,6 +207,11 @@
             }
 
             public void resetConnectionState() {
+                throw new UnsupportedOperationException(
+                "Unimplemented mock service");
+            }
+
+            public void setRelayingAllowed(boolean relayingAllowed) {
                 throw new UnsupportedOperationException(
                 "Unimplemented mock service");
             }



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


Re: POP3 and other handlers ...

Posted by Stefano Bagnara <ap...@bago.org>.
Noel J. Bergman wrote:
>> 3) We start having a little mess with authorized users, relaying 
>> allowed, authorized networks and how handlers manage this. Let's
>> keep in mind this and maybe we'll find a clean solution to this.
> 
> I don't disagree.  What if anything do you have in mind at this time?

Nothing in mind. I just wanted to raise the problem.

I have been too busy with other issues and I really have not yet found 
the time to work on the fastfail/handlerchain stuff.

Stefano


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


POP3 and other handlers ...

Posted by "Noel J. Bergman" <no...@devtech.com>.
> I don't know if "RoamingUsers" is better or worst than "PopBeforeSmtp": 
> what do ther thinks?

There are many ways to handle RoamingUsers.  POPBeforeSMTP is at least descriptive.

> 2) Before 3.0 I would like to find a better solution than hardcoding the 
> check in PassCmdHandler (maybe support the new fastfail in pop3, and add 
> support for filters after command execution for this).

Well, I agree and disagree.  I disagree in that these are not really filters; they are command handlers.  In some cases, the commands filter, but they are structurally indentical.  And I agree that we should rebuild the protocol handlers in the same manner as we rebuilt SMTP (and which IMAP, conceptually, used earlier), using pluggable command handlers.

> 3) We start having a little mess with authorized users, relaying 
> allowed, authorized networks and how handlers manage this. Let's
> keep in mind this and maybe we'll find a clean solution to this.

I don't disagree.  What if anything do you have in mind at this time?

	--- Noel


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


Re: svn commit: r424709 - in /james/server/trunk/src: conf/ java/org/apache/james/pop3server/ java/org/apache/james/smtpserver/ java/org/apache/james/smtpserver/core/ java/org/apache/james/util/ test/org/apache/james/pop3server/ test/org/apache/james/smtps...

Posted by Stefano Bagnara <ap...@bago.org>.
Hi Norman,

1) I think that the correct name is RoamingUsers (note the G after roamin).

I don't know if "RoamingUsers" is better or worst than "PopBeforeSmtp": 
what do ther thinks?

I like RoamingUsers as a name but I don't know what would be the most 
intuitive name to our users.

2) Before 3.0 I would like to find a better solution than hardcoding the 
check in PassCmdHandler (maybe support the new fastfail in pop3, and add 
support for filters after command execution for this). Maybe we should 
also think at the possibility to use a single handler as smtp and pop3 
filter at the same time... This would be a much more complex issue, but 
much better than using a static map.

3) We start having a little mess with authorized users, relaying 
allowed, authorized networks and how handlers manage this. Let's keep in 
mind this and maybe we'll find a clean solution to this.

Stefano

PS: I think this is a good feature to James!

norman@apache.org wrote:
> Author: norman
> Date: Sun Jul 23 04:10:51 2006
> New Revision: 424709
> 
> URL: http://svn.apache.org/viewvc?rev=424709&view=rev
> Log:
> Add support for RoaminUsers (pop before smtp). See JAMES-569
> 
> Added:
>     james/server/trunk/src/java/org/apache/james/smtpserver/core/RoaminUsersHandler.java
>     james/server/trunk/src/java/org/apache/james/util/RoaminUsersHelper.java
> Modified:
>     james/server/trunk/src/conf/james-config.xml
>     james/server/trunk/src/java/org/apache/james/pop3server/PassCmdHandler.java
>     james/server/trunk/src/java/org/apache/james/smtpserver/SMTPHandler.java
>     james/server/trunk/src/java/org/apache/james/smtpserver/SMTPServer.java
>     james/server/trunk/src/java/org/apache/james/smtpserver/SMTPSession.java
>     james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java
>     james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java
>     james/server/trunk/src/test/org/apache/james/smtpserver/SPFHandlerTest.java
>     james/server/trunk/src/test/org/apache/james/smtpserver/SetMimeHeaderHandlerTest.java


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