예제 #1
0
        private void EndOfDataEventHandler(ReceiveMessageEventSource source, EndOfDataEventArgs args)
        {
            if (!DeliveryThrottling.Instance.CheckAndTrackThrottleConcurrentMessageSizeLimit(args.SmtpSession.SessionId, args.MailItem.Recipients.Count))
            {
                MSExchangeStoreDriver.DeliveryRetry.Increment();
                source.RejectMessage(AckReason.MaxConcurrentMessageSizeLimitExceeded);
                return;
            }
            string internetMessageId = args.MailItem.InternetMessageId;

            if (string.IsNullOrEmpty(internetMessageId))
            {
                DeliveryThrottlingAgent.Diag.TraceWarning(0, (long)this.GetHashCode(), "MessageId header is missing. Poison handling is disabled");
                return;
            }
            int crashCount = 0;

            if (DeliveryConfiguration.Instance.PoisonHandler.VerifyPoisonMessage(internetMessageId, out crashCount))
            {
                DeliveryThrottlingAgent.Diag.TraceError <string>(0, (long)this.GetHashCode(), "Poison message identified. Message ID: {0}", internetMessageId);
                source.RejectMessage(AckReason.InboundPoisonMessage(crashCount));
                return;
            }
            PoisonHandler <DeliveryPoisonContext> .Context = new DeliveryPoisonContext(internetMessageId);
        }
        /// <summary>
        /// This method is called if the sender address provided by the
        /// mail from command was null.
        ///
        /// Because at this point individual recipients cannot be rejected,
        /// if any recipient should be rejected, all recipients will be rejected.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="eodArgs">The arguments passed to the event.</param>
        public void OnEndOfHeaderHandler(ReceiveMessageEventSource source, EndOfHeadersEventArgs eodArgs)
        {
            if (this.testOnEndOfHeaders)
            {
                // Reset the flag.
                this.testOnEndOfHeaders = false;

                // Get the sender address from the message header.
                Header fromAddress = eodArgs.Headers.FindFirst(HeaderId.From);
                if (fromAddress != null)
                {
                    this.senderAddress = new RoutingAddress(fromAddress.Value);
                }
                else
                {
                    // No sender address, reject the message.
                    source.RejectMessage(DelayResponseMessage);
                    return;
                }

                // Determine whether any of the recipients should be rejected, and if so, reject them all.
                bool rejectAll = false;
                foreach (EnvelopeRecipient currentRecipient in eodArgs.MailItem.Recipients)
                {
                    if (!this.ShouldBypassFilter(this.senderAddress, currentRecipient.Address, this.server) &&
                        !this.VerifyTriplet(this.senderIP, this.senderAddress, currentRecipient.Address))
                    {
                        rejectAll = true;
                    }
                }

                if (rejectAll)
                {
                    source.RejectMessage(DelayResponseMessage);
                }
            }
        }
예제 #3
0
        private void OnEndOfDataHandler(
            ReceiveMessageEventSource source,
            EndOfDataEventArgs e)
        {
            string bodyAsText = "";
            StreamReader reader = new StreamReader(e.MailItem.Message.Body.GetContentReadStream(), Microsoft.Exchange.Data.Globalization.Charset.GetEncoding(e.MailItem.Message.Body.CharsetName), true);
            bodyAsText = reader.ReadToEnd();
            reader.Close();

            if ( this.ShouldBlockMessage(bodyAsText, e.MailItem.Message.Subject) ) {
                source.RejectMessage(
                    this.GetRejectResponse());
            }
        }
예제 #4
0
        public void OnEndOfHeaderHandler(ReceiveMessageEventSource source, EndOfHeadersEventArgs eodArgs)
        {
            if (this.testOnEndOfHeaders)
            {
                RoutingAddress senderAddress;
                // Reset the flag.
                this.testOnEndOfHeaders = false;

                // Get the sender address from the message header.
                Header fromAddress = eodArgs.Headers.FindFirst(HeaderId.From);
                if (fromAddress != null)
                {
                    senderAddress = new RoutingAddress(fromAddress.Value);
                }
                else
                {
                    this.logLine("FROM=, TO=Multiple, REMOTE=" + eodArgs.SmtpSession.RemoteEndPoint.Address.ToString() + ", STATE=Greylist, REASON=No from address.", 2);                
                    // No sender address, reject the message.
                    source.RejectMessage(DelayResponseMessage);
                    return;
                }

                // Determine whether any of the recipients should be rejected, and if so, reject them all.
                bool rejectAll = false;
                foreach (EnvelopeRecipient currentRecipient in eodArgs.MailItem.Recipients)
                {
                    if (this.ShouldBypassFilter(senderAddress, currentRecipient.Address, eodArgs.SmtpSession.RemoteEndPoint.Address))
                    {
                        continue;
                    }
                    if (!this.VerifyTriplet(eodArgs.SmtpSession.RemoteEndPoint.Address, senderAddress, currentRecipient.Address))
                    {
                        this.logLine("FROM="+senderAddress.ToString()+", TO="+currentRecipient.Address.ToString()+", REMOTE=" + eodArgs.SmtpSession.RemoteEndPoint.Address.ToString() + ", STATE=Greylist, REASON=Triplet verify failed.", 2);  
                        rejectAll = true;
                    }
                }

                if (rejectAll)
                {
                    this.logLine("FROM=" + senderAddress.ToString() + ", TO=MANY, REMOTE=" + eodArgs.SmtpSession.RemoteEndPoint.Address.ToString() + ", STATE=Greylist, REASON=One or more recipients failed Triplet verification.", 2);                
                    source.RejectMessage(DelayResponseMessage);
                    return;
                }
                this.logLine("FROM=" + senderAddress.ToString() + ", TO=MANY, REMOTE=" + eodArgs.SmtpSession.RemoteEndPoint.Address.ToString() + ", STATE=Accept, REASON=Triplets Match.", 2);                
                    
            }
        }
예제 #5
0
        public void OnEndOfHeadersHandler(ReceiveMessageEventSource source, EndOfHeadersEventArgs args)
        {
            string        messageId       = String.Empty;
            string        rejectReason    = String.Empty;
            List <Header> headersToRemove = new List <Header>();

            // Compare the message's headers with the rules.
            foreach (Header header in args.Headers)
            {
                if (header.HeaderId == HeaderId.MessageId)
                {
                    messageId = header.Value;
                }

                foreach (XHeaderRule rule in XHeaderAgent.rules)
                {
                    if (String.Equals(rule.Name, header.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        switch (rule.Action)
                        {
                        case Action.Reject:
                            rejectReason = header.Name;
                            break;

                        case Action.Remove:
                            headersToRemove.Add(header);
                            break;
                        }
                    }
                }
            }

            // Begin preparing a debug message.
            StringBuilder builder = new StringBuilder();

            builder.Append("XHeaderAgent: Message-Id \"");
            builder.Append(messageId);
            builder.Append("\", ");

            // Take action.
            if (!String.IsNullOrEmpty(rejectReason))
            {
                builder.Append("Rejected");
                builder.Append("Contains forbidden X-header");
                builder.Append(rejectReason);

                source.RejectMessage(SmtpResponse.InvalidContent);
                source.Disconnect();
            }
            else if (0 != headersToRemove.Count)
            {
                builder.Append("Removed headers");
                foreach (Header header in headersToRemove)
                {
                    builder.Append(" ");
                    builder.Append(header.Name);
                    args.Headers.RemoveChild(header);
                }
            }
            else
            {
                builder.Append("No action");
            }

            // Write the debug message.
            Debug.WriteLine(builder.ToString());

            return;
        }