View Javadoc

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