1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.smtpserver.core.filter.fastfail;
21
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.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.james.api.dnsservice.DNSService;
29 import org.apache.james.dsn.DSNStatus;
30 import org.apache.james.smtpserver.CommandHandler;
31 import org.apache.james.smtpserver.SMTPSession;
32 import org.apache.james.smtpserver.junkscore.JunkScore;
33 import org.apache.mailet.MailAddress;
34
35 import java.net.UnknownHostException;
36 import java.util.ArrayList;
37 import java.util.Collection;
38
39
40
41
42 public class ResolvableEhloHeloHandler extends AbstractJunkHandler implements
43 CommandHandler, Configurable, Serviceable {
44
45 public final static String BAD_EHLO_HELO = "BAD_EHLO_HELO";
46
47 protected boolean checkAuthNetworks = false;
48
49 private boolean checkAuthUsers = false;
50
51 protected DNSService dnsServer = null;
52
53
54
55
56 public void configure(Configuration handlerConfiguration)
57 throws ConfigurationException {
58 Configuration configRelay = handlerConfiguration.getChild(
59 "checkAuthNetworks", false);
60 if (configRelay != null) {
61 setCheckAuthNetworks(configRelay.getValueAsBoolean(false));
62 }
63
64 Configuration configAuthUser = handlerConfiguration.getChild(
65 "checkAuthUsers", false);
66 if (configAuthUser != null) {
67 setCheckAuthUsers(configAuthUser.getValueAsBoolean(false));
68 }
69
70 super.configure(handlerConfiguration);
71 }
72
73
74
75
76 public void service(ServiceManager serviceMan) throws ServiceException {
77 setDnsServer((DNSService) serviceMan.lookup(DNSService.ROLE));
78 }
79
80
81
82
83
84
85
86 public void setCheckAuthNetworks(boolean checkAuthNetworks) {
87 this.checkAuthNetworks = checkAuthNetworks;
88 }
89
90
91
92
93
94
95
96 public void setCheckAuthUsers(boolean checkAuthUsers) {
97 this.checkAuthUsers = checkAuthUsers;
98 }
99
100
101
102
103
104
105
106 public void setDnsServer(DNSService dnsServer) {
107 this.dnsServer = dnsServer;
108 }
109
110
111
112
113 public void onCommand(SMTPSession session) {
114 String argument = session.getCommandArgument();
115 String command = session.getCommandName();
116 if (command.equals("HELO")
117 || command.equals("EHLO")) {
118 checkEhloHelo(session, argument);
119 } else if (command.equals("RCPT")) {
120 doProcessing(session);
121 }
122 }
123
124
125
126
127
128
129
130
131
132 protected void checkEhloHelo(SMTPSession session, String argument) {
133
134
135
136
137 if (!session.isRelayingAllowed() || checkAuthNetworks) {
138
139
140 try {
141 dnsServer.getByName(argument);
142 } catch (UnknownHostException e) {
143 session.getState().put(BAD_EHLO_HELO, "true");
144 }
145 }
146 }
147
148
149
150
151
152 public Collection getImplCommands() {
153 Collection implCommands = new ArrayList();
154 implCommands.add("EHLO");
155 implCommands.add("HELO");
156 implCommands.add("RCPT");
157
158 return implCommands;
159 }
160
161
162
163
164 protected boolean check(SMTPSession session) {
165
166 MailAddress rcpt = (MailAddress) session.getState().get(
167 SMTPSession.CURRENT_RECIPIENT);
168
169
170 if (session.getState().get(BAD_EHLO_HELO) == null
171 || rcpt.getUser().equalsIgnoreCase("postmaster")
172 || rcpt.getUser().equalsIgnoreCase("abuse"))
173 return false;
174
175
176 if (!(session.isAuthRequired() && session.getUser() != null && !checkAuthUsers)) {
177 return true;
178 }
179 return false;
180 }
181
182
183
184
185
186 protected JunkScore getJunkScore(SMTPSession session) {
187 return (JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION);
188 }
189
190
191
192
193 public JunkHandlerData getJunkHandlerData(SMTPSession session) {
194 JunkHandlerData data = new JunkHandlerData();
195
196 data.setJunkScoreLogString("Provided EHLO/HELO " + session.getState().get(SMTPSession.CURRENT_HELO_NAME) + " can not resolved. Add junkScore: " + getScore());
197 data.setRejectLogString("501 " + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_ARG)
198 + " Provided EHLO/HELO " + session.getState().get(SMTPSession.CURRENT_HELO_NAME) + " can not resolved");
199
200
201 data.setRejectResponseString("501 " + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_ARG)
202 + " Provided EHLO/HELO " + session.getState().get(SMTPSession.CURRENT_HELO_NAME) + " can not resolved");
203
204 data.setScoreName("ResolvableEhloHeloCheck");
205 return data;
206 }
207
208 }