KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > inputIPFilters > RBLFilter


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.inputIPFilters;
22
23 import java.net.Inet4Address JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jsmtpd.core.common.PluginInitException;
32 import org.jsmtpd.core.common.PluginStore;
33 import org.jsmtpd.core.common.acl.IACL;
34 import org.jsmtpd.core.common.dnsService.IDNSResolver;
35 import org.jsmtpd.core.common.inputIPFilter.IFilterIP;
36 import org.jsmtpd.tools.cache.ICache;
37 import org.jsmtpd.tools.cache.SimpleCache;
38
39 /**
40  * Real Time Black
41  * @author Jean-Francois POUX
42  */

43 public class RBLFilter implements IFilterIP {
44     /**
45      * RBL servers to check
46      */

47     private List JavaDoc<String JavaDoc> serverList = new ArrayList JavaDoc<String JavaDoc>();
48     /**
49      * daemon's logger
50      */

51     private Log log = LogFactory.getLog(RBLFilter.class);
52     /**
53      * DNS service to resolve against RBL servers
54      */

55     private IDNSResolver resolver = null;
56     /**
57      * ACL instance
58      */

59     private IACL acl = null;
60     /**
61      * ACL's relayed host will bypass this plugin if set to true
62      */

63     private boolean bypassLocal = false;
64     /**
65      * RBL dns queries are slow, cache them.
66      * TODO: Write expiration cache.
67      */

68     private ICache<String JavaDoc,Inet4Address JavaDoc> cache = new SimpleCache<String JavaDoc,Inet4Address JavaDoc> (500);
69     
70     public boolean checkIP(InetAddress JavaDoc input) {
71
72         if (!(input instanceof Inet4Address JavaDoc)) // We only work with ipv4 atm
73
return false;
74
75         String JavaDoc ipString = ((Inet4Address JavaDoc) input).getHostAddress();
76         
77         if (bypassLocal && acl.isValidRelay(ipString))
78             return true;
79
80         if (cache.get(ipString)!=null) // Ip is in cache, check canceled
81
return true;
82         
83         // We reverse the incoming IPv4 addresss to check it against RBL server (that's the way they work)
84
String JavaDoc[] parts = ((Inet4Address JavaDoc) input).getHostAddress().split("\\.");
85         String JavaDoc reverse = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0];
86
87         // For each RBL server, try to resolve. First successfull resolution fails the filter (eg it means that's the ip is blacklisted)
88
for (Iterator JavaDoc iter = serverList.iterator(); iter.hasNext();) {
89             String JavaDoc element = (String JavaDoc) iter.next();
90             String JavaDoc toCheck = reverse + "." + element;
91             log.debug("Client ip: " + ipString + " RBL filtering with " + element);
92             if (resolver.exists(toCheck)) {
93                 log.warn("Client ip: " + ipString + " rejected by RBLFilter plugin on server " + element);
94                 return false;
95             }
96         }
97         cache.cache(ipString,(Inet4Address JavaDoc) input); // put in cache so don't resolve it next time
98
return true; // Not found, go on in the servers
99
}
100
101     public String JavaDoc getPluginName() {
102         return "Real time Blacklist plugin for jsmtpd";
103     }
104
105     public void initPlugin() throws PluginInitException {
106         resolver = PluginStore.getInstance().getResolver();
107         // Should not happen because of init order in Controler. For debugg purpose
108
if (resolver == null)
109             throw new PluginInitException();
110
111         acl = PluginStore.getInstance().getAcl();
112
113         if (acl == null)
114             throw new PluginInitException();
115
116     }
117
118
119     public void shutdownPlugin() {
120         // nothing to do for this plugin
121
}
122
123     // For autoconfig from xml file
124
public void setRBLServer(String JavaDoc serv) {
125         serverList.add(serv);
126     }
127     public void setBypassLocal(boolean b) {
128         bypassLocal = b;
129     }
130 }
Popular Tags