Author: norman Date: Fri Jun 30 08:17:57 2006 New Revision: 418309 URL: http://svn.apache.org/viewvc?rev=418309&view=rev Log: Create new package which holds the fastfailfilter handlers Extract the "filtercode" from the CoreHandler and build filterHandlers
Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/MaxRcptHandler.java james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ResolvableEhloHeloHandler.java james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ReverseEqualsEhloHeloHandler.java james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/TarpitHandler.java james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ValidSenderDomainHandler.java Modified: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/SMTPSession.java Modified: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/SMTPSession.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/SMTPSession.java?rev=418309&r1=418308&r2=418309&view=diff ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/SMTPSession.java (original) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/SMTPSession.java Fri Jun 30 08:17:57 2006 @@ -37,6 +37,7 @@ public final static String SENDER = "SENDER_ADDRESS"; // Sender's email address public final static String RCPT_LIST = "RCPT_LIST"; // The message recipients public final static String CURRENT_HELO_MODE = "CURRENT_HELO_MODE"; // HELO or EHLO + public final static String STOP_HANDLER_PROCESSING = "STOP_HANDLER_PROCESSING"; /** * Writes response string to the client Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/MaxRcptHandler.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/MaxRcptHandler.java?rev=418309&view=auto ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/MaxRcptHandler.java (added) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/MaxRcptHandler.java Fri Jun 30 08:17:57 2006 @@ -0,0 +1,95 @@ +/*********************************************************************** + * Copyright (c) 1999-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.fastfailfilter; + +import java.util.Collection; + +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.smtpserver.CommandHandler; +import org.apache.james.smtpserver.SMTPSession; +import org.apache.james.util.mail.dsn.DSNStatus; + +public class MaxRcptHandler extends AbstractLogEnabled implements + CommandHandler, Configurable { + + private int maxRcpt = 0; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) + throws ConfigurationException { + Configuration configuration = handlerConfiguration.getChild("maxRcpt", + false); + if (configuration != null) { + setMaxRcpt(configuration.getValueAsInteger(0)); + } else { + throw new ConfigurationException( + "Please set the maxRcpt configuration value"); + } + } + + /** + * Set the max rcpt for wich should be accepted + * + * @param maxRcpt + * The max rcpt count + */ + public void setMaxRcpt(int maxRcpt) { + this.maxRcpt = maxRcpt; + } + + // TODO: move this to a util class or something simular + private int getRcptCount(SMTPSession session) { + int startCount = 0; + + // check if the key exists + if (session.getState().get(SMTPSession.RCPT_LIST) != null) { + return ((Collection) session.getState().get(SMTPSession.RCPT_LIST)) + .size(); + } else { + return startCount; + } + } + + /** + * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) + */ + public void onCommand(SMTPSession session) { + String responseString = null; + int rcptCount = 0; + + rcptCount = getRcptCount(session) + 1; + + // check if the max recipients has reached + if (rcptCount > maxRcpt) { + responseString = "452 " + + DSNStatus.getStatus(DSNStatus.NETWORK, + DSNStatus.DELIVERY_TOO_MANY_REC) + + " Requested action not taken: max recipients reached"; + session.writeResponse(responseString); + getLogger().error(responseString); + + // After this filter match we should not call any other handler! + session.getState().put(SMTPSession.STOP_HANDLER_PROCESSING, "true"); + } + } +} Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ResolvableEhloHeloHandler.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ResolvableEhloHeloHandler.java?rev=418309&view=auto ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ResolvableEhloHeloHandler.java (added) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ResolvableEhloHeloHandler.java Fri Jun 30 08:17:57 2006 @@ -0,0 +1,111 @@ +/*********************************************************************** + * Copyright (c) 1999-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.fastfailfilter; + +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; +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 java.net.UnknownHostException; + +public class ResolvableEhloHeloHandler extends AbstractLogEnabled + implements CommandHandler, Configurable, Serviceable { + + private boolean checkAuthNetworks = false; + + private DNSServer dnsServer = null; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) + throws ConfigurationException { + Configuration configRelay = handlerConfiguration.getChild( + "checkAuthNetworks", false); + if (configRelay != null) { + setCheckAuthNetworks(configRelay.getValueAsBoolean(false)); + } + } + + /** + * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager) + */ + public void service(ServiceManager serviceMan) throws ServiceException { + setDnsServer((DNSServer) serviceMan.lookup(DNSServer.ROLE)); + } + + /** + * Set to true if AuthNetworks should be included in the EHLO check + * + * @param checkAuthNetworks + * Set to true to enable + */ + public void setCheckAuthNetworks(boolean checkAuthNetworks) { + this.checkAuthNetworks = checkAuthNetworks; + } + + /** + * Set the DNSServer + * + * @param dnsServer + * The DNSServer + */ + public void setDnsServer(DNSServer dnsServer) { + this.dnsServer = dnsServer; + } + + /** + * @see org.apache.james.smtpserver.fastfailfilter.HeloFilterHandler#onEhloCommand(SMTPSession) + */ + public void onCommand(SMTPSession session) { + String argument = session.getCommandArgument(); + String responseString = null; + + /** + * don't check if the ip address is allowed to relay. Only check if it + * is set in the config. + */ + if (!session.isRelayingAllowed() || checkAuthNetworks) { + // try to resolv the provided helo. If it can not resolved do not + // accept it. + try { + dnsServer.getByName(argument); + } catch (UnknownHostException e) { + responseString = "501 " + + DSNStatus.getStatus(DSNStatus.PERMANENT, + DSNStatus.DELIVERY_INVALID_ARG) + + " Provided EHLO/HELO " + argument + + " can not resolved"; + session.writeResponse(responseString); + getLogger().info(responseString); + + // After this filter match we should not call any other handler! + session.getState().put(SMTPSession.STOP_HANDLER_PROCESSING, + "true"); + } + } + } +} Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ReverseEqualsEhloHeloHandler.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ReverseEqualsEhloHeloHandler.java?rev=418309&view=auto ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ReverseEqualsEhloHeloHandler.java (added) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ReverseEqualsEhloHeloHandler.java Fri Jun 30 08:17:57 2006 @@ -0,0 +1,127 @@ +/*********************************************************************** + * Copyright (c) 1999-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.fastfailfilter; + +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; +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 java.net.UnknownHostException; + +public class ReverseEqualsEhloHeloHandler extends AbstractLogEnabled + implements CommandHandler, Configurable, Serviceable { + + private boolean checkAuthNetworks = false; + + private DNSServer dnsServer = null; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) + throws ConfigurationException { + Configuration configRelay = handlerConfiguration.getChild( + "checkAuthNetworks", false); + if (configRelay != null) { + setCheckAuthNetworks(configRelay.getValueAsBoolean(false)); + } + } + + /** + * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager) + */ + public void service(ServiceManager serviceMan) throws ServiceException { + setDnsServer((DNSServer) serviceMan.lookup(DNSServer.ROLE)); + } + + /** + * Set to true if AuthNetworks should be included in the EHLO check + * + * @param checkAuthNetworks + * Set to true to enable + */ + public void setCheckAuthNetworks(boolean checkAuthNetworks) { + this.checkAuthNetworks = checkAuthNetworks; + } + + /** + * Set the DNSServer + * + * @param dnsServer + * The DNSServer + */ + public void setDnsServer(DNSServer dnsServer) { + this.dnsServer = dnsServer; + } + + /** + * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) + */ + public void onCommand(SMTPSession session) { + String argument = session.getCommandArgument(); + String responseString = null; + + /** + * 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) { + 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.getState().put(SMTPSession.STOP_HANDLER_PROCESSING, + "true"); + } + } catch (UnknownHostException e) { + responseString = "501 " + + DSNStatus.getStatus(DSNStatus.PERMANENT, + DSNStatus.DELIVERY_INVALID_ARG) + " Ipaddress " + + session.getRemoteIPAddress() + " can not resolved"; + + session.writeResponse(responseString); + getLogger().info(responseString); + + // After this filter match we should not call any other handler! + session.getState().put(SMTPSession.STOP_HANDLER_PROCESSING, + "true"); + } + } + } +} Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/TarpitHandler.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/TarpitHandler.java?rev=418309&view=auto ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/TarpitHandler.java (added) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/TarpitHandler.java Fri Jun 30 08:17:57 2006 @@ -0,0 +1,123 @@ +/*********************************************************************** + * Copyright (c) 1999-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.fastfailfilter; + +import java.util.Collection; + +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.smtpserver.CommandHandler; +import org.apache.james.smtpserver.SMTPSession; + +public class TarpitHandler extends AbstractLogEnabled implements + CommandHandler, Configurable { + + private int tarpitRcptCount = 0; + + private long tarpitSleepTime = 5000; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) + throws ConfigurationException { + + Configuration configTarpitRcptCount = handlerConfiguration.getChild( + "tarpitRcptCount", false); + if (configTarpitRcptCount != null) { + setTarpitRcptCount(configTarpitRcptCount.getValueAsInteger(0)); + } + + if (tarpitRcptCount == 0) + throw new ConfigurationException( + "Please set the tarpitRcptCount bigger values as 0"); + + Configuration configTarpitSleepTime = handlerConfiguration.getChild( + "tarpitSleepTime", false); + if (configTarpitSleepTime != null) { + setTarpitSleepTime(configTarpitSleepTime.getValueAsLong(5000)); + } + + if (tarpitSleepTime == 0) + throw new ConfigurationException( + "Please set the tarpitSleepTimeto a bigger values as 0"); + + } + + /** + * Set the tarpit count after which the tarpit sleep time will be activated + * + * @param tarpitRcptCount + */ + public void setTarpitRcptCount(int tarpitRcptCount) { + this.tarpitRcptCount = tarpitRcptCount; + } + + /** + * Set the tarpit sleep time + * + * @param tarpitSleepTime + * Time in milliseconds + */ + public void setTarpitSleepTime(long tarpitSleepTime) { + this.tarpitSleepTime = tarpitSleepTime; + } + + //TODO: Move to util class + private int getRcptCount(SMTPSession session) { + int startCount = 0; + + // check if the key exists + if (session.getState().get(SMTPSession.RCPT_LIST) != null) { + return ((Collection) session.getState().get(SMTPSession.RCPT_LIST)) + .size(); + } else { + return startCount; + } + } + + /** + * Add a sleep for the given milliseconds + * + * @param timeInMillis + * Time in ms + * @throws InterruptedException + */ + private void sleep(float timeInMillis) throws InterruptedException { + Thread.sleep((long) timeInMillis); + } + + /** + * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) + */ + public void onCommand(SMTPSession session) { + + int rcptCount = 0; + rcptCount = getRcptCount(session); + rcptCount++; + + if (rcptCount > tarpitRcptCount) { + try { + sleep(tarpitSleepTime); + } catch (InterruptedException e) { + } + } + } +} Added: james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ValidSenderDomainHandler.java URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ValidSenderDomainHandler.java?rev=418309&view=auto ============================================================================== --- james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ValidSenderDomainHandler.java (added) +++ james/server/sandbox/handlerapi/src/java/org/apache/james/smtpserver/fastfailfilter/ValidSenderDomainHandler.java Fri Jun 30 08:17:57 2006 @@ -0,0 +1,109 @@ +/*********************************************************************** + * Copyright (c) 1999-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.fastfailfilter; + +import java.util.Collection; + +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; +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; + +public class ValidSenderDomainHandler + extends AbstractLogEnabled + implements CommandHandler, Configurable, Serviceable { + + private boolean checkAuthClients = false; + + private DNSServer dnsServer = null; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) throws ConfigurationException { + + Configuration configRelay = handlerConfiguration.getChild("checkAuthClients",false); + if(configRelay != null) { + setCheckAuthClients(configRelay.getValueAsBoolean(false)); + } + } + + /** + * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager) + */ + public void service(ServiceManager serviceMan) throws ServiceException { + setDnsServer((DNSServer) serviceMan.lookup(DNSServer.ROLE)); + } + + /** + * Set the DnsServer + * + * @param dnsServer The DnsServer + */ + public void setDnsServer(DNSServer dnsServer) { + this.dnsServer = dnsServer; + } + + /** + * Enable checking of authorized clients + * + * @param checkAuthClients Set to true to enable + */ + public void setCheckAuthClients(boolean checkAuthClients) { + this.checkAuthClients = checkAuthClients; + } + + + /** + * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) + */ + public void onCommand(SMTPSession session) { + + String responseString = null; + MailAddress senderAddress = (MailAddress) session.getState().get(SMTPSession.SENDER); + + /** + * don't check if the ip address is allowed to relay. Only check if it is set in the config. + */ + if (checkAuthClients || !session.isRelayingAllowed()) { + + // Maybe we should build a static method in org.apache.james.dnsserver.DNSServer ? + Collection records; + + + // try to resolv the provided domain in the senderaddress. If it can not resolved do not accept it. + records = dnsServer.findMXRecords(senderAddress.getHost()); + if (records == null || records.size() == 0) { + responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " sender " + senderAddress + " contains a domain with no valid MX records"; + session.writeResponse(responseString); + getLogger().info(responseString); + + // After this filter match we should not call any other handler! + session.getState().put(SMTPSession.STOP_HANDLER_PROCESSING, "true"); + } + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]