@Override public HookResult onMessage(SMTPSession session, Mail mail) { if (mail instanceof MailImpl) { final MailImpl mailImpl = (MailImpl) mail; mailImpl.setRemoteHost(session.getRemoteAddress().getHostName()); mailImpl.setRemoteAddr(session.getRemoteAddress().getAddress().getHostAddress()); if (session.getUser() != null) { mail.setAttribute(Mail.SMTP_AUTH_USER_ATTRIBUTE_NAME, session.getUser()); } if (session.isRelayingAllowed()) { mail.setAttribute(SMTP_AUTH_NETWORK_NAME, "true"); } } return HookResult.DECLINED; }
@Override public Response onLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { MailEnvelopeImpl env = (MailEnvelopeImpl) session.getAttachment(DataCmdHandler.MAILENV, ProtocolSession.State.Transaction); OutputStream out = env.getMessageOutputStream(); try { session.popLineHandler(); session.resetState(); return response; LOGGER.error("Unknown error occurred while processing DATA.", e); session.resetState(); return ERROR_PROCESSING_MESSAGE;
/** * Handler method called upon receipt of a DATA command. * Reads in message data, creates header, and delivers to * mail server service for delivery. * * @param session SMTP session object * @param argument the argument passed in with the command by the SMTP client */ protected SMTPResponse doDATA(SMTPSession session, String argument) { try { MimeMessageInputStreamSource mmiss = new MimeMessageInputStreamSource(mailServer.getId()); session.getState().put(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE, mmiss); } catch (MessagingException e) { session.getLogger().warn("Error creating mimemessagesource for incoming data",e); return new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unexpected error preparing to receive DATA."); } // out = new PipedOutputStream(messageIn); session.pushLineHandler(getLineHandler()); return new SMTPResponse(SMTPRetCode.DATA_READY, "Ok Send data ending with <CRLF>.<CRLF>"); }
@Override protected Response doFilterChecks(SMTPSession session, String command, String parameters) { session.resetState(); if (parameters == null) { return DOMAIN_ADDRESS_REQUIRED; } else { // store provided name session.setAttachment(SMTPSession.CURRENT_HELO_NAME, parameters, State.Connection); return null; } }
/** * Handler method called upon receipt of a DATA command. * Reads in message data, creates header, and delivers to * mail server service for delivery. * * @param session SMTP session object * @param argument the argument passed in with the command by the SMTP client */ @SuppressWarnings("unchecked") protected Response doDATA(SMTPSession session, String argument) { MaybeSender sender = (MaybeSender) session.getAttachment(SMTPSession.SENDER, ProtocolSession.State.Transaction); MailEnvelope env = createEnvelope(session, sender, new ArrayList<>((Collection<MailAddress>) session.getAttachment(SMTPSession.RCPT_LIST, ProtocolSession.State.Transaction))); session.setAttachment(MAILENV, env,ProtocolSession.State.Transaction); session.pushLineHandler(lineHandler); return DATA_READY; }
@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; } }
@SuppressWarnings("unchecked") public void onLine(SMTPSession session, byte[] line, LineHandler<SMTPSession> next) { MimeMessageInputStreamSource mmiss = (MimeMessageInputStreamSource) session.getState().get(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE); out.close(); List recipientCollection = (List) session.getState().get(SMTPSession.RCPT_LIST); MailImpl mail = new MailImpl(mailServer.getId(), (MailAddress) session.getState().get(SMTPSession.SENDER), recipientCollection); session.getState().put(SMTPConstants.MAIL, mail); session.popLineHandler(); session.resetState(); session.getLogger().info("Unexpected error handling DATA stream",e); session.writeResponse(new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unexpected error handling DATA stream.")); } finally { LifecycleUtil.dispose(mimeMessageCopyOnWriteProxy); DSNStatus.UNDEFINED_STATUS) + " Error processing message: " + e.getMessage()); session.getLogger().error( "Unknown error occurred while processing DATA.", e); session.writeResponse(response); return;
protected boolean check(SMTPSession session,MailAddress rcpt) { // not reject it if (session.getAttachment(BAD_EHLO_HELO, State.Transaction) == null) { return false; } return true; }
public HookResult onMessage(SMTPSession session, Mail mail) { if (mail instanceof MailImpl) { final MailImpl mailImpl = (MailImpl) mail; mailImpl.setRemoteHost(session.getRemoteHost()); mailImpl.setRemoteAddr(session.getRemoteIPAddress()); if (session.getUser() != null) { mail.setAttribute(SMTP_AUTH_USER_ATTRIBUTE_NAME, session.getUser()); } if (session.isRelayingAllowed()) { mail.setAttribute(SMTP_AUTH_NETWORK_NAME,"true"); } } return new HookResult(HookReturnCode.DECLINED); }
@Override public Response onConnect(SMTPSession session) { // some kind of random cleanup process if (Math.random() > 0.99) { POP3BeforeSMTPHelper.removeExpiredIP(expireTime); } // Check if the ip is allowed to relay if (!session.isRelayingAllowed() && POP3BeforeSMTPHelper.isAuthorized(session.getRemoteAddress().getAddress().getHostAddress())) { session.setRelayingAllowed(true); } return null; }
for (int i = 0; i < mHandlers.size(); i++) { MessageHook rawHandler = mHandlers.get(i); session.getLogger().debug( "executing james message handler " + rawHandler); HookResult hRes = rawHandler.onMessage(session, for (int i2 = 0; i2 < rHooks.size(); i2++) { Object rHook = rHooks.get(i2); session.getLogger() .debug("executing hook " + rHook); hRes = ((HookResultHook) rHook).onHookResult( session.writeResponse(response); return; for (int i = 0; i < count; i++) { Object rawHandler = messageHandlers.get(i); session.getLogger().debug( "executing james message handler " + rawHandler); HookResult hRes = ((JamesMessageHook) rawHandler) for (int i2 = 0; i2 < rHooks.size(); i2++) { Object rHook = rHooks.get(i2); session.getLogger() .debug("executing hook " + rHook); hRes = ((HookResultHook) rHook).onHookResult( session.writeResponse(response); break;
@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; }
lineByteBuffer.get(line, 0, line.length); MimeMessageInputStreamSource mmiss = (MimeMessageInputStreamSource) session.getAttachment(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE, State.Transaction); List<MailAddress> recipientCollection = (List<MailAddress>) session.getAttachment(SMTPSession.RCPT_LIST, State.Transaction); MailAddress mailAddress = (MailAddress) session.getAttachment(SMTPSession.SENDER, State.Transaction); session.setAttachment(SMTPConstants.MAIL, mail, State.Transaction); session.popLineHandler(); return response;
@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); }
String heloMode = (String) session.getAttachment(SMTPSession.CURRENT_HELO_MODE, State.Connection); String heloName = (String) session.getAttachment(SMTPSession.CURRENT_HELO_NAME, State.Connection); headerLineBuffer.append("from ").append(session.getRemoteAddress().getHostName()); headerLineBuffer.append(" ([").append(session.getRemoteAddress().getAddress().getHostAddress()).append("])"); Header header = new Header("Received", headerLineBuffer.toString()); headerLineBuffer.append("by ").append(session.getConfiguration().getHelloName()).append(" (").append(session.getConfiguration().getSoftwareName()).append(") with ").append(getServiceType(session, heloMode)); headerLineBuffer.append(" ID ").append(session.getSessionID()); if (((Collection<?>) session.getAttachment(SMTPSession.RCPT_LIST, State.Transaction)).size() == 1) { headerLineBuffer.append("for <").append(((List<MailAddress>) session.getAttachment(SMTPSession.RCPT_LIST, State.Transaction)).get(0).toString()).append(">;"); } else {
if (session.isRelayingAllowed()) { LOGGER.info("Ipaddress {} is allowed to relay. Don't check it", session.getRemoteAddress().getAddress()); return; return; } else { LOGGER.debug("IpAddress {} not listed on {}", session.getRemoteAddress().getAddress(), rbl); session.setAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, blocklistedDetail, State.Connection); session.setAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "true", State.Connection); return; } else {
/** * @see org.apache.james.smtpserver.protocol.ConnectHandler#onConnect(SMTPSession) */ public void onConnect(SMTPSession session) { // some kind of random cleanup process if (Math.random() > 0.99) { POP3BeforeSMTPHelper.removeExpiredIP(expireTime); } // Check if the ip is allowed to relay if (!session.isRelayingAllowed() && POP3BeforeSMTPHelper.isAuthorized(session.getRemoteIPAddress())) { session.setRelayingAllowed(true); } }
String heloEhlo = (String) session.getAttachment(SMTPSession.CURRENT_HELO_NAME, State.Transaction); } else { String ip = session.getRemoteAddress().getAddress().getHostAddress(); session.setAttachment(SPF_HEADER, result.getHeaderText(), State.Transaction); explanation = "Block caused by an invalid SPF record"; session.setAttachment(SPF_DETAIL, explanation, State.Transaction); session.setAttachment(SPF_BLOCKLISTED, "true", State.Transaction); session.setAttachment(SPF_TEMPBLOCKLISTED, "true", State.Transaction);
@Override protected Response doCoreCmd(SMTPSession session, String command, String parameters) { session.setAttachment(SMTPSession.CURRENT_HELO_MODE, COMMAND_NAME, ProtocolSession.State.Connection); StringBuilder response = new StringBuilder(); response.append(session.getConfiguration().getHelloName()).append( " Hello ").append(parameters).append(" [").append( session.getRemoteAddress().getAddress().getHostAddress()).append("])"); return new SMTPResponse(SMTPRetCode.MAIL_OK, response); }
session.popLineHandler(); session.pushLineHandler(new AbstractSMTPLineHandler() {