You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/09/09 16:46:37 UTC
svn commit: r441791 - in /james/server/trunk/src:
java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
java/org/apache/james/util/urirbl/URIScanner.java
test/org/apache/james/smtpserver/URIRBLHandlerTest.java
Author: norman
Date: Sat Sep 9 07:46:36 2006
New Revision: 441791
URL: http://svn.apache.org/viewvc?view=rev&rev=441791
Log:
Fix formating
Fix bug which cause not reject multipart messages
Fix bug which cause only process only the first domain found
Modified:
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
james/server/trunk/src/java/org/apache/james/util/urirbl/URIScanner.java
james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
Modified: james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java?view=diff&rev=441791&r1=441790&r2=441791
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java (original)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java Sat Sep 9 07:46:36 2006
@@ -168,18 +168,23 @@
HashSet domains = scanMailForDomains(message);
Iterator fDomains = domains.iterator();
- Iterator uRbl = uriRbl.iterator();
while (fDomains.hasNext()) {
+ Iterator uRbl = uriRbl.iterator();
String target = fDomains.next().toString();
-
+
while (uRbl.hasNext()) {
try {
String responseString = null;
String detail = null;
String uRblServer = uRbl.next().toString();
-
- dnsServer.getByName(target + "." + uRblServer);
+ String address = target + "." + uRblServer;
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Lookup " + address);
+ }
+
+ dnsServer.getByName(address);
if (getLogger().isInfoEnabled()) {
getLogger().info("Message sended by " + session.getRemoteIPAddress() + " restricted by " + uRblServer + " cause " + target + " is listed");
@@ -187,7 +192,7 @@
// we should try to retrieve details
if (getDetail) {
- Collection txt = dnsServer.findTXTRecords(target + "." + uRblServer);
+ Collection txt = dnsServer.findTXTRecords(address);
// Check if we found a txt record
if (!txt.isEmpty()) {
@@ -200,7 +205,8 @@
if (detail != null) {
responseString = "554 "
- + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_OTHER) + " "
+ + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_OTHER)
+ + " Rejected: message contains domain " + target + " listed by " + uRblServer + " . Details: "
+ detail;
} else {
responseString = "554 "
@@ -211,6 +217,7 @@
session.writeResponse(responseString);
session.setStopHandlerProcessing(true);
session.abortMessage();
+ return;
} catch (UnknownHostException uhe) {
// domain not found. keep processing
@@ -230,6 +237,7 @@
*
* @param domains HashSet for accumulating domain strings
* @param part MimePart to scan
+ * @return domains The HashSet that contains the domains which were extracted
*/
private HashSet scanMailForDomains(MimePart part) throws MessagingException, IOException {
HashSet domains = new HashSet();
@@ -237,7 +245,12 @@
if (part.isMimeType("text/plain") || part.isMimeType("text/html")) {
getLogger().debug("scanning: \"" + part.getContent().toString() + "\"");
- URIScanner.scanContentForDomains(domains, part.getContent().toString());
+ HashSet newDom = URIScanner.scanContentForDomains(domains, part.getContent().toString());
+
+ // Check if new domains are found and add the domains
+ if (newDom != null && newDom.size() > 0) {
+ domains.addAll(newDom);
+ }
} else if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int count = multipart.getCount();
@@ -246,7 +259,12 @@
for (int index = 0; index < count; index++) {
getLogger().debug("recursing index: " + index);
MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(index);
- domains = scanMailForDomains(mimeBodyPart);
+ HashSet newDomains = scanMailForDomains(mimeBodyPart);
+
+ // Check if new domains are found and add the domains
+ if(newDomains != null && newDomains.size() > 0) {
+ domains.addAll(newDomains);
+ }
}
}
return domains;
Modified: james/server/trunk/src/java/org/apache/james/util/urirbl/URIScanner.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/urirbl/URIScanner.java?view=diff&rev=441791&r1=441790&r2=441791
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/urirbl/URIScanner.java (original)
+++ james/server/trunk/src/java/org/apache/james/util/urirbl/URIScanner.java Sat Sep 9 07:46:36 2006
@@ -26,6 +26,7 @@
import java.util.Iterator;
import java.util.regex.*;
import java.net.URI;
+import java.net.URISyntaxException;
public class URIScanner {
@@ -166,7 +167,7 @@
/** Pre-compiled pattern that matches domain string that is possibly
contained in a three-part TLD */
static private final Pattern tld3CapPattern = Pattern.compile(tld3Cap);
-
+
/** controls testing/debug output */
static private boolean testing = false;
@@ -181,18 +182,21 @@
* @param domains a HashSet to be populated with all domain strings found in
* the content
* @param content a character sequence to be scanned for URIs
+ * @return newDomains the domains which were extracted
*/
- static public void scanContentForDomains(HashSet domains,
- CharSequence content) {
- HashSet hosts = scanContentForHosts(content);
- for (Iterator i = hosts.iterator(); i.hasNext();) {
- String domain = domainFromHost((String) i.next());
- if (null != domain) {
- if (false == domains.contains(domain)) {
- domains.add(domain);
- }
+ static public HashSet scanContentForDomains(HashSet domains, CharSequence content) {
+ HashSet newDomains = new HashSet();
+ HashSet hosts = scanContentForHosts(content);
+ for (Iterator i = hosts.iterator(); i.hasNext();) {
+ String domain = domainFromHost((String) i.next());
+
+ if (null != domain) {
+ if (false == domains.contains(domain)) {
+ newDomains.add(domain);
+ }
+ }
}
- }
+ return newDomains;
}
/**
@@ -203,62 +207,64 @@
* @return a HashSet containing host strings
*/
static protected HashSet scanContentForHosts(CharSequence content) {
- HashSet set = new HashSet();
- try {
+ HashSet set = new HashSet();
+
// look for URIs
Matcher mat = uriPattern.matcher(content);
while (mat.find()) {
- String found = mat.group();
- Matcher cleanMat = uriCleanup.matcher(found);
- if (cleanMat.find()) {
- found = cleanMat.group(1);
- }
- cleanMat = uriCleanup2.matcher(found);
- if (cleanMat.find()) {
- found = cleanMat.replaceAll("");
- }
- cleanMat = uriCleanup3.matcher(found);
- if (cleanMat.find()) {
- found = "mailto://" + cleanMat.group(1) + cleanMat.group(2);
- }
- cleanMat = schemePattern.matcher(found);
- if (!cleanMat.find()) {
- if (found.matches("^(?i)www\\d*\\..*")) {
- found = "http://" + found;
- } else if (found.matches("^(?i)ftp\\..*")) {
- found = "ftp://" + found;
+ String found = mat.group();
+ Matcher cleanMat = uriCleanup.matcher(found);
+ if (cleanMat.find()) {
+ found = cleanMat.group(1);
}
- }
- String host = hostFromUriStr(found);
- if (null != host) {
- host = host.toLowerCase();
- if (false == set.contains(host)) {
- set.add(host);
+
+ cleanMat = uriCleanup2.matcher(found);
+ if (cleanMat.find()) {
+ found = cleanMat.replaceAll("");
+ }
+
+ cleanMat = uriCleanup3.matcher(found);
+ if (cleanMat.find()) {
+ found = "mailto://" + cleanMat.group(1) + cleanMat.group(2);
+ }
+
+ cleanMat = schemePattern.matcher(found);
+ if (!cleanMat.find()) {
+ if (found.matches("^(?i)www\\d*\\..*")) {
+ found = "http://" + found;
+ } else if (found.matches("^(?i)ftp\\..*")) {
+ found = "ftp://" + found;
+ }
+ }
+
+ String host = hostFromUriStr(found);
+ if (null != host) {
+ host = host.toLowerCase();
+ if (false == set.contains(host)) {
+ set.add(host);
+ }
}
- }
}
// look for "schemeless" email addresses, too
mat = emailAddrPattern.matcher(content);
while (mat.find()) {
- String found = mat.group();
- debugOut("******** mailfound=\"" + found + "\"");
- found = "mailto://" + found;
- debugOut("*******6 mailfoundfound=\"" + found
- + "\" after cleanup 6");
- String host = hostFromUriStr(found);
- if (null != host) {
- host = host.toLowerCase();
- if (false == set.contains(host)) {
- set.add(host);
+ String found = mat.group();
+ debugOut("******** mailfound=\"" + found + "\"");
+ found = "mailto://" + found;
+ debugOut("*******6 mailfoundfound=\"" + found
+ + "\" after cleanup 6");
+
+ String host = hostFromUriStr(found);
+ if (null != host) {
+
+ host = host.toLowerCase();
+ if (false == set.contains(host)) {
+ set.add(host);
+ }
}
}
- }
- } catch (Exception ex) {
- debugOut(ex.toString());
- ex.printStackTrace();
- }
- return set;
+ return set;
}
/**
@@ -271,14 +277,16 @@
* could be found
*/
static protected String hostFromUriStr(String uriStr) {
- debugOut("hostFromUriStr(\"" + uriStr + "\")");
- String host = null;
- try {
- URI uri = new URI(uriStr);
- host = uri.getHost();
- } catch (Exception ex) {
- }
- return host;
+ debugOut("hostFromUriStr(\"" + uriStr + "\")");
+ String host = null;
+ URI uri;
+ try {
+ uri = new URI(uriStr);
+ host = uri.getHost();
+ } catch (URISyntaxException e) {
+ debugOut(e.getMessage());
+ }
+ return host;
}
/**
@@ -294,65 +302,59 @@
* @return the registrar domain portion of the supplied host string
*/
static protected String domainFromHost(String host) {
- debugOut("domainFromHost(\"" + host + "\")");
- String domain = null;
- Matcher mat;
- try {
-
+ debugOut("domainFromHost(\"" + host + "\")");
+ String domain = null;
+ Matcher mat;
+
// IP addrs
mat = ipCapPattern.matcher(host);
if (mat.find()) {
- // reverse the octets now
- domain = mat.group(5) + "." + mat.group(4) + "." + mat.group(3)
- + "." + mat.group(2);
- debugOut("domain=\"" + domain + "\"");
- return domain;
+ // reverse the octets now
+ domain = mat.group(5) + "." + mat.group(4) + "." + mat.group(3) + "." + mat.group(2);
+ debugOut("domain=\"" + domain + "\"");
+ return domain;
}
// 3-part TLDs
mat = tld3CapPattern.matcher(host);
if (mat.find()) {
- String tld = mat.group(2);
- if (TLDLookup.isThreePartTLD(tld)) {
- domain = mat.group(1);
- debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
- return domain;
- }
+ String tld = mat.group(2);
+ if (TLDLookup.isThreePartTLD(tld)) {
+ domain = mat.group(1);
+ debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
+ return domain;
+ }
}
// 2-part TLDs
mat = tld2CapPattern.matcher(host);
if (mat.find()) {
- String tld = mat.group(2);
- if (TLDLookup.isTwoPartTLD(tld)) {
- domain = mat.group(1);
- debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
- return domain;
- }
+ String tld = mat.group(2);
+ if (TLDLookup.isTwoPartTLD(tld)) {
+ domain = mat.group(1);
+ debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
+ return domain;
+ }
}
// 1-part TLDs
mat = tldCapPattern.matcher(host);
if (mat.find()) {
- String tld = mat.group(2);
- domain = mat.group(1);
- debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
- return domain;
+ String tld = mat.group(2);
+ domain = mat.group(1);
+ debugOut("domain=\"" + domain + ", tld=\"" + tld + "\"");
+ return domain;
}
- } catch (Exception ex) {
- debugOut(ex.toString());
- ex.printStackTrace();
- }
- return domain;
+ return domain;
}
/**
* Debugging output
*/
private static void debugOut(String msg) {
- if (true == testing) {
- System.out.println(msg);
- }
+ if (true == testing) {
+ System.out.println(msg);
+ }
}
/**
@@ -362,17 +364,16 @@
* @return modified "escaped" string
*/
private static String escape(String str) {
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < str.length(); i++) {
- char ch = str.charAt(i);
- if (Character.isDigit(ch) || Character.isUpperCase(ch)
- || Character.isLowerCase(ch) || ch == '_') {
- buffer.append(ch);
- } else {
- buffer.append("\\");
- buffer.append(ch);
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < str.length(); i++) {
+ char ch = str.charAt(i);
+ if (Character.isDigit(ch) || Character.isUpperCase(ch) || Character.isLowerCase(ch) || ch == '_') {
+ buffer.append(ch);
+ } else {
+ buffer.append("\\");
+ buffer.append(ch);
+ }
}
- }
- return buffer.toString();
+ return buffer.toString();
}
}
Modified: james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java?view=diff&rev=441791&r1=441790&r2=441791
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java Sat Sep 9 07:46:36 2006
@@ -30,8 +30,12 @@
import java.util.List;
import java.util.Map;
+import javax.mail.BodyPart;
import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
import junit.framework.TestCase;
@@ -46,7 +50,7 @@
public class URIRBLHandlerTest extends TestCase {
private static final String BAD_DOMAIN1 = "bad.domain.de";
private static final String BAD_DOMAIN2 = "bad2.domain.de";
- private static final String GOOD_DOMAIN = "good.domain2.de";
+ private static final String GOOD_DOMAIN = "good.apache.org";
private static final String URISERVER = "multi.surbl.org.";
private SMTPSession mockedSMTPSession;
@@ -133,6 +137,24 @@
return message;
}
+
+ public MimeMessage setupMockedMimeMessageMP(String text) throws MessagingException {
+ MimeMessage message = new MimeMessage(new MockMimeMessage());
+
+// Create the message part
+ BodyPart messageBodyPart = new MimeBodyPart();
+
+// Fill the message
+ messageBodyPart.setText(text);
+
+ Multipart multipart = new MimeMultipart();
+ multipart.addBodyPart(messageBodyPart);
+ message.setContent(multipart);
+ message.saveChanges();
+
+ return message;
+ }
+
/**
* Setup the mocked dnsserver
@@ -206,6 +228,24 @@
servers.add(URISERVER);
SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("http://" + BAD_DOMAIN1 + "/")));
+
+ URIRBLHandler handler = new URIRBLHandler();
+
+ ContainerUtil.enableLogging(handler, new MockLogger());
+ handler.setDnsServer(setupMockedDnsServer());
+ handler.setUriRblServer(servers);
+ handler.onMessage(session);
+
+ assertTrue("Stop handler processing", session.getStopHandlerProcessing());
+ assertNotNull("Email was rejected", getResponse());
+ }
+
+ public void testBlockedMultiPart() throws IOException, MessagingException {
+
+ ArrayList servers = new ArrayList();
+ servers.add(URISERVER);
+
+ SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessageMP("http://" + BAD_DOMAIN1 + "/" + " " +"http://" + GOOD_DOMAIN + "/")));
URIRBLHandler handler = new URIRBLHandler();
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org