001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.smtpserver;
019:
020: import java.net.UnknownHostException;
021: import org.apache.avalon.framework.configuration.Configurable;
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.configuration.ConfigurationException;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025:
026: /**
027: * Handles HELO command
028: */
029: public class HeloCmdHandler extends AbstractLogEnabled implements
030: CommandHandler, Configurable {
031:
032: /**
033: * The name of the command handled by the command handler
034: */
035: private final static String COMMAND_NAME = "HELO";
036:
037: /**
038: * set checkValidHelo to false as default value
039: */
040: private boolean checkResolvableHelo = false;
041:
042: private boolean checkAuthNetworks = false;
043:
044: /**
045: * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
046: */
047: public void configure(Configuration handlerConfiguration)
048: throws ConfigurationException {
049: Configuration configuration = handlerConfiguration.getChild(
050: "checkResolvableHelo", false);
051: if (configuration != null) {
052: checkResolvableHelo = configuration.getValueAsBoolean();
053: }
054:
055: Configuration configRelay = handlerConfiguration.getChild(
056: "checkAuthNetworks", false);
057: if (configRelay != null) {
058: checkAuthNetworks = configRelay.getValueAsBoolean();
059: }
060:
061: }
062:
063: /*
064: * process HELO command
065: *
066: * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
067: **/
068: public void onCommand(SMTPSession session) {
069: doHELO(session, session.getCommandArgument());
070: }
071:
072: /**
073: * Handler method called upon receipt of a HELO command.
074: * Responds with a greeting and informs the client whether
075: * client authentication is required.
076: *
077: * @param session SMTP session object
078: * @param argument the argument passed in with the command by the SMTP client
079: */
080: private void doHELO(SMTPSession session, String argument) {
081: String responseString = null;
082: boolean badHelo = false;
083:
084: // check for resolvable helo if its set in config
085: if (checkResolvableHelo) {
086:
087: /**
088: * don't check if the ip address is allowed to relay. Only check if it is set in the config. ed.
089: */
090: if (!session.isRelayingAllowed() || checkAuthNetworks) {
091:
092: // try to resolv the provided helo. If it can not resolved do not accept it.
093: try {
094: org.apache.james.dnsserver.DNSServer
095: .getByName(argument);
096: } catch (UnknownHostException e) {
097: badHelo = true;
098: responseString = "501 Provided HELO " + argument
099: + " can not resolved";
100: session.writeResponse(responseString);
101: getLogger().info(responseString);
102: }
103:
104: }
105: }
106:
107: if (argument == null) {
108: responseString = "501 Domain address required: "
109: + COMMAND_NAME;
110: session.writeResponse(responseString);
111: getLogger().info(responseString);
112: } else if (!badHelo) {
113: session.resetState();
114: session.getState().put(SMTPSession.CURRENT_HELO_MODE,
115: COMMAND_NAME);
116: session.getResponseBuffer().append("250 ").append(
117: session.getConfigurationData().getHelloName())
118: .append(" Hello ").append(argument).append(" (")
119: .append(session.getRemoteHost()).append(" [")
120: .append(session.getRemoteIPAddress()).append("])");
121: responseString = session.clearResponseBuffer();
122: session.writeResponse(responseString);
123: }
124: }
125: }
|