@Override protected HookResult callHook(RcptHook rawHook, SMTPSession session, String parameters) { MaybeSender sender = (MaybeSender) session.getAttachment(SMTPSession.SENDER, State.Transaction); return rawHook.doRcpt(session, sender, (MailAddress) session.getAttachment(CURRENT_RECIPIENT, State.Transaction)); }
@Override public HookResult doMailParameter(SMTPSession session, String paramName, String paramValue) { MaybeSender tempSender = (MaybeSender) session.getAttachment(SMTPSession.SENDER, State.Transaction); return doMailSize(session, paramValue, tempSender); }
@Override public HookResult onMessage(SMTPSession session, MailEnvelope mail) { Boolean failed = (Boolean) session.getAttachment(MESG_FAILED, State.Transaction); if (failed != null && failed.booleanValue()) { LOGGER.error("Rejected message from {} from {} exceeding system maximum message size of {}", session.getAttachment(SMTPSession.SENDER, State.Transaction), session.getRemoteAddress().getAddress().getHostAddress(), session.getConfiguration().getMaxMessageSize()); return QUOTA_EXCEEDED; } else { return HookResult.DECLINED; } }
@Override protected Response onHeadersLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { if (getLocation() == Location.Prefix && session.getAttachment(headersPrefixAdded, State.Transaction) == null) { session.setAttachment(headersPrefixAdded, Boolean.TRUE, State.Transaction); return addHeaders(session, line, next); } return super.onHeadersLine(session, line, next); }
@Override protected Response onSeparatorLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { if (getLocation() == Location.Suffix && session.getAttachment(headersSuffixAdded, State.Transaction) == null) { session.setAttachment(headersSuffixAdded, Boolean.TRUE, State.Transaction); return addHeaders(session, line, next); } return super.onSeparatorLine(session, line, next); }
@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 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 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 testRejectInvalidHelo() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, INVALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Invalid HELO").isNotNull(); HookReturnCode result = handler.doRcpt(session,null, mailAddress).getResult(); assertThat(HookReturnCode.deny()).describedAs("Reject").isEqualTo(result); }
@Test public void testRejectInvalidHeloAuthUser() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, INVALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Value stored").isNotNull(); HookReturnCode result = handler.doRcpt(session,null, mailAddress).getResult(); assertThat(HookReturnCode.deny()).describedAs("Reject").isEqualTo(result); }
@Test public void testRejectInvalidHelo() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, INVALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Invalid HELO").isNotNull(); HookReturnCode result = handler.doRcpt(session, MaybeSender.nullSender(), mailAddress).getResult(); assertThat(HookReturnCode.deny()).describedAs("Reject").isEqualTo(result); }
@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(); }
@Test public void testRejectInvalidHeloAuthUser() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, INVALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Value stored").isNotNull(); HookReturnCode result = handler.doRcpt(session, MaybeSender.nullSender(), mailAddress).getResult(); assertThat(HookReturnCode.deny()).describedAs("Reject").isEqualTo(result); }
@Test public void testInvalidSenderDomainReject() throws Exception { ValidSenderDomainHandler handler = createHandler(); SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid")); MaybeSender sender = (MaybeSender) session.getAttachment(SMTPSession.SENDER, State.Transaction); HookReturnCode response = handler.doMail(session, sender).getResult(); assertThat(HookReturnCode.deny()).describedAs("Blocked cause we use reject action").isEqualTo(response); } }
@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 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(); }
@Test public void testNotRejectValidHelo() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(VALID_HOST,false,false,null,mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, VALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Valid HELO").isNull(); HookReturnCode result = handler.doRcpt(session,null, mailAddress).getResult(); assertThat(HookReturnCode.declined()).describedAs("Not reject").isEqualTo(result); }
@Test public void testNotRejectValidHelo() throws Exception { MailAddress mailAddress = new MailAddress("test@localhost"); SMTPSession session = setupMockSession(VALID_HOST,false,false,null,mailAddress); ResolvableEhloHeloHandler handler = createHandler(); handler.doHelo(session, VALID_HOST); assertThat(session.getAttachment(BAD_EHLO_HELO, Transaction)).withFailMessage("Valid HELO").isNull(); HookReturnCode result = handler.doRcpt(session, MaybeSender.nullSender(), mailAddress).getResult(); assertThat(HookReturnCode.declined()).describedAs("Not reject").isEqualTo(result); }