Author: norman Date: Mon Jul 17 09:05:46 2006 New Revision: 422752 URL: http://svn.apache.org/viewvc?rev=422752&view=rev Log: Fix the DNSRBLHandler which not worked anymore after my commit
Modified: james/server/trunk/src/java/org/apache/james/smtpserver/core/DNSRBLHandler.java james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java Modified: james/server/trunk/src/java/org/apache/james/smtpserver/core/DNSRBLHandler.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/DNSRBLHandler.java?rev=422752&r1=422751&r2=422752&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/smtpserver/core/DNSRBLHandler.java (original) +++ james/server/trunk/src/java/org/apache/james/smtpserver/core/DNSRBLHandler.java Mon Jul 17 09:05:46 2006 @@ -51,9 +51,11 @@ private boolean getDetail = false; - private boolean blocklisted = false; - private String blocklistedDetail = null; + + public static final String RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.blocklisted"; + + public static final String RBL_DETAIL_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.detail"; /** * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) @@ -107,13 +109,13 @@ setDNSServer((DNSServer) serviceMan.lookup(DNSServer.ROLE)); } - /* + /** * check if the remote Ip address is block listed * * @see org.apache.james.smtpserver.ConnectHandler#onConnect(SMTPSession) **/ public void onConnect(SMTPSession session) { - blocklisted = checkDNSRBL(session, session.getRemoteIPAddress()); + checkDNSRBL(session, session.getRemoteIPAddress()); } /** @@ -151,36 +153,18 @@ public void setGetDetail(boolean getDetail) { this.getDetail = getDetail; } - - /** - * Return true if the ip is blocklisted - * - * @return true uf blocklisted - */ - public boolean isBlocklisted() { - return blocklisted; - } - - /** - * Return the details - * - * @return blocklistDetail - */ - public String getBlocklistedDetail() { - return blocklistedDetail; - } /** - * @see org.apache.james.smtpserver.SMTPHandlerConfigurationData#checkDNSRBL(Socket) - */ - /* + * * This checks DNSRBL whitelists and blacklists. If the remote IP is whitelisted * it will be permitted to send e-mail, otherwise if the remote IP is blacklisted, * the sender will only be permitted to send e-mail to postmaster (RFC 2821) or * abuse (RFC 2142), unless authenticated. + * + * @see org.apache.james.smtpserver.SMTPHandlerConfigurationData#checkDNSRBL(Socket) */ - public boolean checkDNSRBL(SMTPSession session, String ipAddress) { + public void checkDNSRBL(SMTPSession session, String ipAddress) { /* * don't check against rbllists if the client is allowed to relay.. @@ -188,7 +172,8 @@ */ if (session.isRelayingAllowed()) { getLogger().info("Ipaddress " + session.getRemoteIPAddress() + " is allowed to relay. Don't check it"); - return false; + session.getState().put(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "false"); + return; } if (whitelist != null || blacklist != null) { @@ -206,7 +191,9 @@ if (getLogger().isInfoEnabled()) { getLogger().info("Connection from " + ipAddress + " whitelisted by " + rblList[i]); } - return false; + + session.getState().put(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "false"); + return; } catch (java.net.UnknownHostException uhe) { if (getLogger().isInfoEnabled()) { getLogger().info("IpAddress " + session.getRemoteIPAddress() + " not listed on " + rblList[i]); @@ -229,10 +216,14 @@ // Check if we found a txt record if (!txt.isEmpty()) { // Set the detail - blocklistedDetail = txt.iterator().next().toString(); + String blocklistedDetail = txt.iterator().next().toString(); + + session.getState().put(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, blocklistedDetail); } } - return true; + + session.getState().put(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "true"); + return; } catch (java.net.UnknownHostException uhe) { // if it is unknown, it isn't blocked if (getLogger().isInfoEnabled()) { @@ -241,7 +232,8 @@ } } } - return false; + // default state + session.getState().put(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "false"); } /** @@ -258,10 +250,11 @@ */ public void onCommand(SMTPSession session) { String responseString = null; + String blocklisted = (String) session.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME); MailAddress recipientAddress = (MailAddress) session.getState().get( SMTPSession.CURRENT_RECIPIENT); - if (blocklisted && // was found in the RBL + if (blocklisted.equals("true") && // was found in the RBL !(session.isRelayingAllowed() || (session.isAuthRequired() && session .getUser() != null)) && // Not (either an authorized IP // or (SMTP AUTH is enabled and Modified: james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java?rev=422752&r1=422751&r2=422752&view=diff ============================================================================== --- james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java (original) +++ james/server/trunk/src/test/org/apache/james/smtpserver/DNSRBLHandlerTest.java Mon Jul 17 09:05:46 2006 @@ -44,7 +44,11 @@ private String remoteIp = "127.0.0.2"; - private boolean relaying = false; + private boolean relaying = false; + + public static final String RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.blocklisted"; + + public static final String RBL_DETAIL_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.detail"; protected void setUp() throws Exception { super.setUp(); @@ -121,11 +125,8 @@ */ private void setupMockedSMTPSession() { mockedSMTPSession = new SMTPSession() { - - private String blockListedDetail = null; - - private boolean blocklisted = false; - + HashMap state = new HashMap(); + public void writeResponse(String respString) { throw new UnsupportedOperationException( "Unimplemented mock service"); @@ -196,8 +197,7 @@ } public HashMap getState() { - throw new UnsupportedOperationException( - "Unimplemented mock service"); + return state; } public void resetState() { @@ -210,22 +210,6 @@ "Unimplemented mock service"); } - public void setBlockListed(boolean blocklisted) { - this.blocklisted = blocklisted; - } - - public boolean isBlockListed() { - return blocklisted; - } - - public void setBlockListedDetail(String detail) { - this.blockListedDetail = detail; - } - - public String getBlockListedDetail() { - return blockListedDetail; - } - public boolean isRelayingAllowed() { return relaying; } @@ -290,8 +274,8 @@ rbl.setGetDetail(true); rbl.onConnect(mockedSMTPSession); assertEquals("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2", - rbl.getBlocklistedDetail()); - assertTrue(rbl.isBlocklisted()); + mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("true",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } // ip is blacklisted and has txt details but we don'T want to retrieve the txt record @@ -306,8 +290,8 @@ rbl.setBlacklist(new String[] { "bl.spamcop.net" }); rbl.setGetDetail(false); rbl.onConnect(mockedSMTPSession); - assertNull(rbl.getBlocklistedDetail()); - assertTrue(rbl.isBlocklisted()); + assertNull(mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("true",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } // ip is allowed to relay @@ -324,8 +308,8 @@ rbl.setBlacklist(new String[] { "bl.spamcop.net" }); rbl.setGetDetail(true); rbl.onConnect(mockedSMTPSession); - assertNull(rbl.getBlocklistedDetail()); - assertFalse(rbl.isBlocklisted()); + assertNull(mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("false",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } // ip not on blacklist @@ -341,8 +325,8 @@ rbl.setBlacklist(new String[] { "bl.spamcop.net" }); rbl.setGetDetail(true); rbl.onConnect(mockedSMTPSession); - assertNull(rbl.getBlocklistedDetail()); - assertFalse(rbl.isBlocklisted()); + assertNull(mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("false",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } // ip on blacklist without txt details @@ -358,8 +342,8 @@ rbl.setBlacklist(new String[] { "bl.spamcop.net" }); rbl.setGetDetail(true); rbl.onConnect(mockedSMTPSession); - assertNull(rbl.getBlocklistedDetail()); - assertTrue(rbl.isBlocklisted()); + assertNull(mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("true",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } // ip on whitelist @@ -375,8 +359,8 @@ rbl.setWhitelist(new String[] { "bl.spamcop.net" }); rbl.setGetDetail(true); rbl.onConnect(mockedSMTPSession); - assertNull(rbl.getBlocklistedDetail()); - assertFalse(rbl.isBlocklisted()); + assertNull(mockedSMTPSession.getState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME)); + assertEquals("false",mockedSMTPSession.getState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]