Author: norman
Date: Wed Aug  9 04:36:48 2006
New Revision: 430033

URL: http://svn.apache.org/viewvc?rev=430033&view=rev
Log:
Handler which checks if the recipient domain has not 127.0.0.1 as mx

Added:
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java

Added: 
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?rev=430033&view=auto
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 (added)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
 Wed Aug  9 04:36:48 2006
@@ -0,0 +1,118 @@
+/****************************************************************
+ * 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.smtpserver.core.filter.fastfail;
+
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+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;
+import org.apache.james.services.DNSServer;
+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;
+
+/**
+ * Reject email with an MX which is 127.0.0.1
+ */
+public class ValidRcptMX extends AbstractLogEnabled implements CommandHandler,
+        Serviceable {
+
+    private DNSServer dnsServer = null;
+
+    private static final String LOOPBACK_IP = "127.0.0.1";
+
+    private static final String LOCALHOST = "localhost";
+
+    /**
+     * @see 
org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
+     */
+    public void service(ServiceManager arg0) throws ServiceException {
+        setDNSServer((DNSServer) arg0.lookup(DNSServer.ROLE));
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     */
+    public Collection getImplCommands() {
+        Collection c = new ArrayList();
+        c.add("RCPT");
+        return c;
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
+     */
+    public void onCommand(SMTPSession session) {
+        doRCPT(session);
+    }
+
+    /**
+     * Set the DNSServer
+     * 
+     * @param dnsServer The dnsServer
+     */
+    public void setDNSServer(DNSServer dnsServer) {
+        this.dnsServer = dnsServer;
+    }
+
+    private void doRCPT(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;
+
+        Iterator mx = dnsServer.findMXRecords(domain).iterator();
+
+        if (mx.hasNext()) {
+            while (mx.hasNext()) {
+                String mxRec = mx.next().toString();
+
+                try {
+                    String ip = dnsServer.getByName(mxRec).getHostAddress();
+
+                    // Check for invalid MX
+                    if (ip.equals(LOOPBACK_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;
+                    }
+                } catch (UnknownHostException e) {
+                    // Ignore this
+                }
+            }
+        }
+    }
+}



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

Reply via email to