KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > smtpExtension > ConnectBackCheck


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.smtpExtension;
22
23 import java.io.IOException JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.InetSocketAddress JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.SocketAddress JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.jsmtpd.core.common.PluginInitException;
33 import org.jsmtpd.core.common.PluginStore;
34 import org.jsmtpd.core.common.acl.IACL;
35 import org.jsmtpd.core.common.dnsService.IDNSResolver;
36 import org.jsmtpd.core.common.io.BareLFException;
37 import org.jsmtpd.core.common.io.InputSizeToBig;
38 import org.jsmtpd.core.common.smtpExtension.IProtocolHandler;
39 import org.jsmtpd.core.common.smtpExtension.ISmtpExtension;
40 import org.jsmtpd.core.common.smtpExtension.SmtpExtensionException;
41
42 /**
43  * Highly experimental, blah blah ...
44  *
45  * On data command, this plugin is pre-triggered (ie invocation before normal data behavior.
46  * Examines the sender, resolve to MX, then try to connect back.
47  *
48  * Some spammers do not use valid email adresse as from. this plugin should drop theses mails.
49  *
50  * TODO: verify on smtp server if given @ exists.
51  */

52 public class ConnectBackCheck implements ISmtpExtension {
53     private IACL acl;
54     private Log log = LogFactory.getLog(ConnectBackCheck.class);
55     private IDNSResolver resolver;
56     public boolean smtpTrigger(String JavaDoc command, IProtocolHandler protocol) throws SmtpExtensionException, IOException JavaDoc, InputSizeToBig, IOException JavaDoc, BareLFException {
57         return true;
58     }
59
60     public boolean smtpPreTrigger(String JavaDoc command, IProtocolHandler protocol) throws SmtpExtensionException, IOException JavaDoc, InputSizeToBig, IOException JavaDoc, BareLFException {
61
62         if (command==null) {
63             log.error("Null command, skipping...");
64             return false;
65         }
66         
67         if (command.toLowerCase().equals("data")) {
68             log.debug("Data command issued, checking...");
69             if (protocol.isRelayed()) {
70                 log.info("Protocol is marked relayed.");
71                 return true;
72             }
73             
74             String JavaDoc remoteHost= ((InetSocketAddress JavaDoc) protocol.getSock().getRemoteSocketAddress()).getAddress().getHostAddress();
75             if (acl.isValidRelay(remoteHost)) {
76                 log.info("ACL said remote is to be relayed.");
77                 return true;
78             }
79             
80             if (!performSmtpCheck(protocol)) {
81                 log.error("I could not contact any smtp servers for domain "+protocol.getMail().getFrom().getHost());
82                 // add code to notify of error. in sock
83
return true;
84             }
85             
86         }
87         
88         return false;
89     }
90     public String JavaDoc getWelcome() {
91         return null;
92     }
93
94     /*
95      * Basic approch would be to verify if connected ip (should be a smtp server as others tests have failed)
96      * is realy a smtp service. This does not work as some mail services (like gmail by example) use smtp proxy
97      * for sending outgoing mail.
98      */

99     private boolean performSmtpCheck (IProtocolHandler protocol) throws IOException JavaDoc {
100         String JavaDoc domain = protocol.getMail().getFrom().getHost();
101         List JavaDoc<InetAddress JavaDoc> mxs= resolver.doMXLookup(domain);
102         if ((mxs==null) || (mxs.size()==0)) {
103             log.error("There are no MX configured for the domain "+domain+", rejecting DATA command");
104             return false;
105         }
106         
107         for (InetAddress JavaDoc address : mxs) {
108             log.debug("trying mx "+address);
109             if (checkSmtpServer(address,protocol.getMail().getFrom().toString())) {
110                 log.debug("success with mx "+address);
111                 return true;
112             }
113         }
114         log.error("Smtp check(s) failed, data not allowed.");
115        return false;
116     }
117     
118     private boolean checkSmtpServer (InetAddress JavaDoc smtpAdress, String JavaDoc targetRcpt) {
119         try {
120             Socket JavaDoc client = new Socket JavaDoc();
121             client.setSoTimeout(30*1000);
122             SocketAddress JavaDoc sockaddr = new InetSocketAddress JavaDoc(smtpAdress.getHostAddress(), 25);
123             client.connect(sockaddr);
124             client.close();
125             return true;
126         } catch (Exception JavaDoc e) {
127             log.error("Error connecting to "+smtpAdress.getHostAddress(),e);
128         }
129
130         return false;
131     }
132     
133     public String JavaDoc getPluginName() {
134         return "Callback on DATA verifier";
135     }
136
137     public void initPlugin() throws PluginInitException {
138         acl = PluginStore.getInstance().getAcl();
139         if (acl==null) {
140             log.error("Could not get ACL plugin !");
141             throw new PluginInitException("Could not get ACL plugin");
142         }
143         resolver = PluginStore.getInstance().getResolver();
144         if (resolver==null) {
145             log.error("Could not get Resolver plugin !");
146             throw new PluginInitException("Could not get Resolver plugin");
147         }
148     }
149
150     public void shutdownPlugin() {
151     }
152
153 }
154
Popular Tags