View Javadoc

1   /************************************************************************
2    * Copyright (c) 1999-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.smtpserver;
19  
20  
21  import java.net.UnknownHostException;
22  import org.apache.avalon.framework.configuration.Configurable;
23  import org.apache.avalon.framework.configuration.Configuration;
24  import org.apache.avalon.framework.configuration.ConfigurationException;
25  import org.apache.avalon.framework.logger.AbstractLogEnabled;
26  
27  
28  /***
29    * Handles HELO command
30    */
31  public class HeloCmdHandler extends AbstractLogEnabled implements CommandHandler,Configurable {
32  
33      /***
34       * The name of the command handled by the command handler
35       */
36      private final static String COMMAND_NAME = "HELO";
37  
38      /***
39       * set checkValidHelo to false as default value
40       */
41      private boolean checkResolvableHelo = false;
42      
43      private boolean checkAuthNetworks = false;
44      
45      /***
46       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
47       */
48      public void configure(Configuration handlerConfiguration) throws ConfigurationException {
49          Configuration configuration = handlerConfiguration.getChild("checkResolvableHelo",false);
50          if(configuration != null) {
51              checkResolvableHelo = configuration.getValueAsBoolean();
52          }
53          
54          Configuration configRelay = handlerConfiguration.getChild("checkAuthNetworks",false);
55          if(configRelay != null) {
56              checkAuthNetworks = configRelay.getValueAsBoolean();
57          }
58          
59      }
60         
61      /*
62       * process HELO command
63       *
64       * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
65      **/
66      public void onCommand(SMTPSession session) {
67          doHELO(session, session.getCommandArgument());
68      }
69  
70      /***
71       * Handler method called upon receipt of a HELO command.
72       * Responds with a greeting and informs the client whether
73       * client authentication is required.
74       *
75       * @param session SMTP session object
76       * @param argument the argument passed in with the command by the SMTP client
77       */
78      private void doHELO(SMTPSession session, String argument) {
79          String responseString = null;
80          boolean badHelo = false;
81                  
82          
83          // check for resolvable helo if its set in config
84          if (checkResolvableHelo) {
85              
86              /***
87               * don't check if the ip address is allowed to relay. Only check if it is set in the config. ed.
88               */
89              if (!session.isRelayingAllowed() || checkAuthNetworks) {
90  
91                  // try to resolv the provided helo. If it can not resolved do not accept it.
92                  try {
93                      org.apache.james.dnsserver.DNSServer.getByName(argument);
94                  } catch (UnknownHostException e) {
95                      badHelo = true;
96                      responseString = "501 Provided HELO " + argument + " can not resolved";
97                      session.writeResponse(responseString);
98                      getLogger().info(responseString);
99                  } 
100 
101             }
102         }
103         
104         if (argument == null) {
105             responseString = "501 Domain address required: " + COMMAND_NAME;
106             session.writeResponse(responseString);
107             getLogger().info(responseString);
108         } else if (!badHelo) {
109             session.resetState();
110             session.getState().put(SMTPSession.CURRENT_HELO_MODE, COMMAND_NAME);
111             session.getResponseBuffer().append("250 ")
112                           .append(session.getConfigurationData().getHelloName())
113                           .append(" Hello ")
114                           .append(argument)
115                           .append(" (")
116                           .append(session.getRemoteHost())
117                           .append(" [")
118                           .append(session.getRemoteIPAddress())
119                           .append("])");
120             responseString = session.clearResponseBuffer();
121             session.writeResponse(responseString);
122         }
123     }
124 }