Author: bago Date: Thu Mar 30 05:30:32 2006 New Revision: 390122 URL: http://svn.apache.org/viewcvs?rev=390122&view=rev Log: Check for valid domain in EHLO - patch by Norman Maurer (JAMES-451)
Modified: james/server/trunk/src/java/org/apache/james/smtpserver/EhloCmdHandler.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/EhloCmdHandler.java URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/smtpserver/EhloCmdHandler.java?rev=390122&r1=390121&r2=390122&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/smtpserver/EhloCmdHandler.java (original) +++ james/server/trunk/src/java/org/apache/james/smtpserver/EhloCmdHandler.java Thu Mar 30 05:30:32 2006 @@ -1,109 +1,144 @@ -/*********************************************************************** - * 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; - -import org.apache.james.util.mail.dsn.DSNStatus; -import java.util.ArrayList; - -/** - * Handles EHLO command - */ -public class EhloCmdHandler implements CommandHandler { - - /** - * The name of the command handled by the command handler - */ - private final static String COMMAND_NAME = "EHLO"; - - /** - * The helo mode set in state object - */ - private final static String CURRENT_HELO_MODE = "CURRENT_HELO_MODE"; // HELO or EHLO - - - /* - * processes EHLO command - * - * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) - **/ - public void onCommand(SMTPSession session) { - doEHLO(session, session.getCommandArgument()); - } - - /** - * Handler method called upon receipt of a EHLO command. - * Responds with a greeting and informs the client whether - * client authentication is required. - * - * @param session SMTP session object - * @param argument the argument passed in with the command by the SMTP client - */ - private void doEHLO(SMTPSession session, String argument) { - String responseString = null; - StringBuffer responseBuffer = session.getResponseBuffer(); - - if (argument == null) { - responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_INVALID_ARG)+" Domain address required: " + COMMAND_NAME; - session.writeResponse(responseString); - } else { - session.resetState(); - session.getState().put(CURRENT_HELO_MODE, COMMAND_NAME); - - ArrayList esmtpextensions = new ArrayList(); - - esmtpextensions.add(new StringBuffer(session.getConfigurationData().getHelloName()) - .append(" Hello ") - .append(argument) - .append(" (") - .append(session.getRemoteHost()) - .append(" [") - .append(session.getRemoteIPAddress()) - .append("])").toString()); - - // Extension defined in RFC 1870 - long maxMessageSize = session.getConfigurationData().getMaxMessageSize(); - if (maxMessageSize > 0) { - esmtpextensions.add("SIZE " + maxMessageSize); - } - - if (session.isAuthRequired()) { - esmtpextensions.add("AUTH LOGIN PLAIN"); - esmtpextensions.add("AUTH=LOGIN PLAIN"); - } - - esmtpextensions.add("PIPELINING"); - esmtpextensions.add("ENHANCEDSTATUSCODES"); - esmtpextensions.add("8BITMIME"); - - - // Iterator i = esmtpextensions.iterator(); - for (int i = 0; i < esmtpextensions.size(); i++) { - if (i == esmtpextensions.size() - 1) { - responseBuffer.append("250 "); - responseBuffer.append((String) esmtpextensions.get(i)); - session.writeResponse(session.clearResponseBuffer()); - } else { - responseBuffer.append("250-"); - responseBuffer.append((String) esmtpextensions.get(i)); - session.writeResponse(session.clearResponseBuffer()); - } - } - } - } - -} +/*********************************************************************** + * 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; + +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.util.mail.dsn.DSNStatus; + +import java.net.UnknownHostException; +import java.util.ArrayList; + +/** + * Handles EHLO command + */ +public class EhloCmdHandler extends AbstractLogEnabled implements CommandHandler,Configurable { + + /** + * The name of the command handled by the command handler + */ + private final static String COMMAND_NAME = "EHLO"; + + /** + * The helo mode set in state object + */ + private final static String CURRENT_HELO_MODE = "CURRENT_HELO_MODE"; // HELO or EHLO + + /** + * set checkValidHelo to false as default value + */ + private boolean checkValidEhlo = false; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration handlerConfiguration) throws ConfigurationException { + Configuration configuration = handlerConfiguration.getChild("checkValidEhlo",false); + if(configuration != null) { + checkValidEhlo = configuration.getValueAsBoolean(); + } + } + + /* + * processes EHLO command + * + * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession) + **/ + public void onCommand(SMTPSession session) { + doEHLO(session, session.getCommandArgument()); + } + + /** + * Handler method called upon receipt of a EHLO command. + * Responds with a greeting and informs the client whether + * client authentication is required. + * + * @param session SMTP session object + * @param argument the argument passed in with the command by the SMTP client + */ + private void doEHLO(SMTPSession session, String argument) { + String responseString = null; + StringBuffer responseBuffer = session.getResponseBuffer(); + boolean badEhlo = false; + + // check for helo if its set in config + if (checkValidEhlo == true) { + + // try to resolv the provided helo. If it can not resolved do not accept it. + try { + org.apache.james.dnsserver.DNSServer.getByName(argument); + } catch (UnknownHostException e) { + badEhlo = true; + responseString = "501 Ehlo can not resolved"; + session.writeResponse(responseString); + getLogger().info(responseString); + } + } + + if (argument == null) { + responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_INVALID_ARG)+" Domain address required: " + COMMAND_NAME; + session.writeResponse(responseString); + } else if (badEhlo == false){ + session.resetState(); + session.getState().put(CURRENT_HELO_MODE, COMMAND_NAME); + + ArrayList esmtpextensions = new ArrayList(); + + esmtpextensions.add(new StringBuffer(session.getConfigurationData().getHelloName()) + .append(" Hello ") + .append(argument) + .append(" (") + .append(session.getRemoteHost()) + .append(" [") + .append(session.getRemoteIPAddress()) + .append("])").toString()); + + // Extension defined in RFC 1870 + long maxMessageSize = session.getConfigurationData().getMaxMessageSize(); + if (maxMessageSize > 0) { + esmtpextensions.add("SIZE " + maxMessageSize); + } + + if (session.isAuthRequired()) { + esmtpextensions.add("AUTH LOGIN PLAIN"); + esmtpextensions.add("AUTH=LOGIN PLAIN"); + } + + esmtpextensions.add("PIPELINING"); + esmtpextensions.add("ENHANCEDSTATUSCODES"); + esmtpextensions.add("8BITMIME"); + + + // Iterator i = esmtpextensions.iterator(); + for (int i = 0; i < esmtpextensions.size(); i++) { + if (i == esmtpextensions.size() - 1) { + responseBuffer.append("250 "); + responseBuffer.append((String) esmtpextensions.get(i)); + session.writeResponse(session.clearResponseBuffer()); + } else { + responseBuffer.append("250-"); + responseBuffer.append((String) esmtpextensions.get(i)); + session.writeResponse(session.clearResponseBuffer()); + } + } + } + } + +} 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=390122&r1=390121&r2=390122&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 Thu Mar 30 05:30:32 2006 @@ -266,6 +266,7 @@ smtpProtocol1.quit(); } + public void testHeloResolv() throws Exception, SMTPException { m_testConfiguration.setHeloResolv(); finishSetUp(m_testConfiguration); @@ -305,6 +306,49 @@ SMTPResponse response = smtpProtocol1.getResponse(); // helo should not be checked. so this should give a 250 code assertEquals("Helo accepted", 250, response.getCode()); + + smtpProtocol1.quit(); + } + + public void testEhloResolv() throws Exception, SMTPException { + m_testConfiguration.setEhloResolv(); + finishSetUp(m_testConfiguration); + + + MySMTPProtocol smtpProtocol1 = new MySMTPProtocol("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[] ehlo1 = new String[] { "abgsfe3rsf.de"}; + String[] ehlo2 = new String[] { "james.apache.org" }; + + smtpProtocol1.sendCommand("ehlo",ehlo1); + SMTPResponse response = smtpProtocol1.getResponse(); + // this should give a 501 code cause the ehlo could not resolved + assertEquals("expected error: ehlo could not resolved", 501, response.getCode()); + + smtpProtocol1.sendCommand("ehlo", ehlo2); + SMTPResponse response2 = smtpProtocol1.getResponse(); + // ehlo is resolvable. so this should give a 250 code + assertEquals("ehlo accepted", 250, response2.getCode()); + + smtpProtocol1.quit(); + } + + public void testEhloResolvDefault() throws Exception, SMTPException { + finishSetUp(m_testConfiguration); + + MySMTPProtocol smtpProtocol1 = new MySMTPProtocol("127.0.0.1", m_smtpListenerPort); + smtpProtocol1.openPort(); + + smtpProtocol1.sendCommand("ehlo",new String[]{"abgsfe3rsf.de"}); + SMTPResponse response = smtpProtocol1.getResponse(); + // ehlo should not be checked. so this should give a 250 code + assertEquals("ehlo accepted", 250, response.getCode()); smtpProtocol1.quit(); } Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java?rev=390122&r1=390121&r2=390122&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 Thu Mar 30 05:30:32 2006 @@ -30,7 +30,8 @@ private boolean m_verifyIdentity = false; private Integer m_connectionLimit = null; private boolean m_heloResolv = false; - + private boolean m_ehloResolv = false; + public SMTPTestConfiguration(int smtpListenerPort) { super("smptserver"); @@ -76,6 +77,10 @@ public void setHeloResolv() { m_heloResolv = true; } + + public void setEhloResolv() { + m_ehloResolv = true; + } public void init() { @@ -94,10 +99,10 @@ handlerConfig.addChild(Util.createRemoteManagerHandlerChainConfiguration()); - // Add Configuration for Helo checks - + // Add Configuration for Helo checks and Ehlo checks DefaultConfiguration heloConfig = (DefaultConfiguration) handlerConfig.getChild("handlerchain").getChild("handler"); heloConfig.addChild(Util.getValuedConfiguration("checkValidHelo",m_heloResolv+"")); + heloConfig.addChild(Util.getValuedConfiguration("checkValidEhlo",m_ehloResolv+"")); addChild(handlerConfig); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]