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.core.filter.fastfail;
21
22 import org.apache.james.dsn.DSNStatus;
23 import org.apache.james.smtpserver.SMTPSession;
24
25
26 import java.net.UnknownHostException;
27
28
29 public class ReverseEqualsEhloHeloHandler extends ResolvableEhloHeloHandler {
30
31 /**
32 * Method which get called on HELO/EHLO
33 *
34 * @param session The SMTPSession
35 * @param argument The argument
36 */
37 protected void checkEhloHelo(SMTPSession session, String argument) {
38 /**
39 * don't check if the ip address is allowed to relay. Only check if it
40 * is set in the config. ed.
41 */
42 if (!session.isRelayingAllowed() || checkAuthNetworks) {
43 boolean badHelo = false;
44 try {
45 // get reverse entry
46 String reverse = dnsServer.getHostName(dnsServer.getByName(
47 session.getRemoteIPAddress()));
48 if (!argument.equals(reverse)) {
49 badHelo = true;
50 }
51 } catch (UnknownHostException e) {
52 badHelo = true;
53 }
54
55 // bad EHLO/HELO
56 if (badHelo)
57 session.getState().put(BAD_EHLO_HELO, "true");
58 }
59 }
60
61 /**
62 * @see JunkHandlerData#getJunkScoreLogString()
63 */
64 protected String getJunkScoreLogString(SMTPSession session) {
65 return "Provided EHLO/HELO " + session.getState().get(SMTPSession.CURRENT_HELO_NAME) + " not equal reverse of "
66 + session.getRemoteIPAddress() + ". Add junkScore: " + getScore();
67 }
68
69 /**
70 * @see JunkHandlerData#getRejectLogString()
71 */
72 protected String getRejectLogString(SMTPSession session) {
73 return getResponseString(session);
74 }
75
76 /**
77 * @see JunkHandlerData#getRejectResponseString()
78 */
79 protected String getResponseString(SMTPSession session) {
80 String responseString = "501 "
81 + DSNStatus.getStatus(DSNStatus.PERMANENT,
82 DSNStatus.DELIVERY_INVALID_ARG)
83 + " Provided EHLO/HELO " + session.getState().get(SMTPSession.CURRENT_HELO_NAME) + " not equal reverse of "
84 + session.getRemoteIPAddress();
85 return responseString;
86 }
87
88 /**
89 * @see JunkHandlerData#getScoreName()
90 */
91 protected String getScoreName() {
92 return "ReverseEqualsEhloHeloCheck";
93 }
94 }