String[] rblList = whitelist; for (String rbl : rblList) { if (resolve(reversedOctets + rbl)) { LOGGER.info("Connection from {} whitelisted by {}", ipAddress, rbl); return; String[] rblList = blacklist; for (String rbl : rblList) { if (resolve(reversedOctets + rbl)) { LOGGER.info("Connection from {} restricted by {} to SMTP AUTH/postmaster/abuse.", ipAddress, rbl); Collection<String> txt = resolveTXTRecords(reversedOctets + rbl);
@Resource(name="dnsservice") public void setDNSService(DNSService dns) { super.setDNSService(new SMTPServerDNSServiceAdapter(dns)); } }
@Test public void testBlackListedTextPresent() throws Exception { DNSRBLHandler rbl = createHandler(); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, State.Connection)).describedAs("Details").isEqualTo("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2"); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testWhiteListed() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("127.0.0.2"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setWhitelist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }
@Override public HookResult doRcpt(SMTPSession session, MaybeSender sender, MailAddress rcpt) { checkDNSRBL(session, session.getRemoteAddress().getAddress().getHostAddress()); if (!session.isRelayingAllowed()) { String blocklisted = (String) session.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, State.Connection); if (blocklisted != null) { // was found in the RBL if (blocklistedDetail == null) { return HookResult.builder() .hookReturnCode(HookReturnCode.deny()) .smtpDescription(DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_AUTH) + " Rejected: unauthenticated e-mail from " + session.getRemoteAddress().getAddress() + " is restricted. Contact the postmaster for details.") .build(); } else { return HookResult.builder() .hookReturnCode(HookReturnCode.deny()) .smtpDescription(DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH) + " " + blocklistedDetail) .build(); } } } return HookResult.DECLINED; }
@Test public void testBlackListedTextPresent() throws Exception { DNSRBLHandler rbl = createHandler(); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, State.Connection)).describedAs("Details").isEqualTo("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2"); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testWhiteListed() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("127.0.0.2"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setWhitelist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }
@Test public void testGetNoDetail() throws Exception { DNSRBLHandler rbl = createHandler(); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(false); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testBlackListedNoTxt() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("127.0.0.3"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testGetNoDetail() throws Exception { DNSRBLHandler rbl = createHandler(); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(false); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testBlackListedNoTxt() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("127.0.0.3"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Blocked").isNotNull(); }
@Test public void testRelayAllowed() throws Exception { DNSRBLHandler rbl = createHandler(); setRelayingAllowed(true); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }
@Test public void testRelayAllowed() throws Exception { DNSRBLHandler rbl = createHandler(); setRelayingAllowed(true); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }
@Test public void testNotBlackListed() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("192.168.0.1"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }
@Test public void testNotBlackListed() throws Exception { DNSRBLHandler rbl = createHandler(); setRemoteIp("192.168.0.1"); setupMockedSMTPSession(new MailAddress("any@domain")); rbl.setBlacklist(new String[] { "bl.spamcop.net." }); rbl.setGetDetail(true); rbl.doRcpt(mockedSMTPSession, MaybeSender.nullSender(), new MailAddress("test@localhost")); assertThat(mockedSMTPSession.getAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("No details").isNull(); assertThat(mockedSMTPSession.getAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, Connection)).withFailMessage("Not blocked").isNull(); }