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 21 22 package org.apache.james.transport.matchers; 23 24 import java.net.UnknownHostException; 25 import java.util.Collection; 26 import java.util.StringTokenizer; 27 28 import javax.mail.MessagingException; 29 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.james.Constants; 33 import org.apache.james.api.dnsservice.DNSService; 34 import org.apache.mailet.base.GenericMatcher; 35 import org.apache.mailet.Mail; 36 37 /** 38 * Checks the network IP address of the sending server against a 39 * blacklist of spammers. There are 3 lists that support this... 40 * <ul> 41 * <li><b>blackholes.mail-abuse.org</b>: Rejected - see http://www.mail-abuse.org/rbl/ 42 * <li><b>dialups.mail-abuse.org</b>: Dialup - see http://www.mail-abuse.org/dul/ 43 * <li><b>relays.mail-abuse.org</b>: Open spam relay - see http://www.mail-abuse.org/rss/ 44 * </ul> 45 * 46 * Example: 47 * <mailet match="InSpammerBlacklist=blackholes.mail-abuse.org." class="ToProcessor"> 48 * <processor>spam</processor> 49 * </mailet> 50 * 51 */ 52 public class InSpammerBlacklist extends GenericMatcher { 53 private String network = null; 54 55 private DNSService dnsServer; 56 57 public void init() throws MessagingException { 58 network = getCondition(); 59 60 // check if the needed condition was given 61 if (network == null) throw new MessagingException("Please configure a blacklist"); 62 63 ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 64 65 try { 66 // Instantiate DNSService 67 dnsServer = (DNSService) compMgr.lookup(DNSService.ROLE); 68 } catch (ServiceException cnfe) { 69 log("Failed to retrieve DNSService" + cnfe.getMessage()); 70 } catch (Exception e) { 71 log("Failed to retrieve DNSService:" + e.getMessage()); 72 } 73 74 } 75 76 public Collection match(Mail mail) { 77 String host = mail.getRemoteAddr(); 78 try { 79 //Have to reverse the octets first 80 StringBuffer sb = new StringBuffer(); 81 StringTokenizer st = new StringTokenizer(host, " .", false); 82 83 while (st.hasMoreTokens()) { 84 sb.insert(0, st.nextToken() + "."); 85 } 86 87 //Add the network prefix for this blacklist 88 sb.append(network); 89 90 //Try to look it up 91 dnsServer.getByName(sb.toString()); 92 93 //If we got here, that's bad... it means the host 94 // was found in the blacklist 95 return mail.getRecipients(); 96 } catch (UnknownHostException uhe) { 97 //This is good... it's not on the list 98 return null; 99 } 100 } 101 }