/// <summary> /// Creates a SendID object and fills it with values from the datarecord. /// </summary> /// <param name="record">Record to get data from.</param> /// <returns>SendID filled with data.</returns> private static Sends.Send CreateAndFillSendFromRecord(IDataRecord record) { Sends.Send sendID = new Sends.Send(); sendID.ID = record.GetString("mta_send_id"); sendID.InternalID = record.GetInt32("mta_send_internalId"); sendID.SendStatus = (SendStatus)record.GetInt32("mta_sendStatus_id"); sendID.LastAccessedTimestamp = DateTime.UtcNow; sendID.CreatedTimestamp = record.GetDateTime("mta_send_createdTimestamp"); return(sendID); }
/// <summary> /// Queues the email for relaying. /// </summary> private async Task <SmtpServerTransactionAsyncResult> QueueForRelayingAsync() { // The email is for relaying. Guid messageID = Guid.NewGuid(); // Look for any MTA control headers. MessageHeaderCollection headers = MessageManager.GetMessageHeaders(Data); // Will not be null if the SendGroupID header was present. MessageHeader ipGroupHeader = headers.SingleOrDefault(m => m.Name.Equals(MessageHeaderNames.SendGroupID, StringComparison.OrdinalIgnoreCase)); // Parameter will hold the MtaIPGroup that will be used to relay this message. VirtualMta.VirtualMtaGroup mtaGroup = null; int ipGroupID = 0; if (ipGroupHeader != null) { if (int.TryParse(ipGroupHeader.Value, out ipGroupID)) { mtaGroup = VirtualMta.VirtualMtaManager.GetVirtualMtaGroup(ipGroupID); } } #region Look for a send id, if one doesn't exist create it. MessageHeader sendIdHeader = headers.SingleOrDefault(h => h.Name.Equals(MessageHeaderNames.SendID, StringComparison.OrdinalIgnoreCase)); int internalSendId = -1; if (sendIdHeader != null) { Sends.Send sndID = await Sends.SendManager.Instance.GetSendAsync(sendIdHeader.Value); if (sndID.SendStatus == SendStatus.Discard) { return(SmtpServerTransactionAsyncResult.FailedSendDiscarding); } internalSendId = sndID.InternalID; } else { Sends.Send sndID = await Sends.SendManager.Instance.GetDefaultInternalSendIdAsync(); if (sndID.SendStatus == SendStatus.Discard) { return(SmtpServerTransactionAsyncResult.FailedSendDiscarding); } internalSendId = sndID.InternalID; } #endregion #region Generate Return Path string returnPath = string.Empty; // Can only return path to messages with one rcpt to if (RcptTo.Count == 1) { // Need to check to see if the message contains a return path overide domain. MessageHeader returnPathDomainOverrideHeader = headers.SingleOrDefault(h => h.Name.Equals(MessageHeaderNames.ReturnPathDomain, StringComparison.OrdinalIgnoreCase)); if (returnPathDomainOverrideHeader != null && MtaParameters.LocalDomains.Count(d => d.Hostname.Equals(returnPathDomainOverrideHeader.Value, StringComparison.OrdinalIgnoreCase)) > 0) { // The message contained a local domain in the returnpathdomain // header so use it instead of the default. returnPath = ReturnPathManager.GenerateReturnPath(RcptTo[0], internalSendId, returnPathDomainOverrideHeader.Value); } else { // The message didn't specify a return path overide or it didn't // contain a localdomain so use the default. returnPath = ReturnPathManager.GenerateReturnPath(RcptTo[0], internalSendId); } // Insert the return path header. Data = MessageManager.AddHeader(Data, new MessageHeader("Return-Path", "<" + returnPath + ">")); } else { // multiple rcpt's so can't have unique return paths, use generic mail from. returnPath = MailFrom; } #endregion #region Generate a message ID header string msgIDHeaderVal = "<" + messageID.ToString("N") + MailFrom.Substring(MailFrom.LastIndexOf("@")) + ">"; // If there is already a message header, remove it and add our own. required for feedback loop processing. if (headers.Count(h => h.Name.Equals("Message-ID", StringComparison.OrdinalIgnoreCase)) > 0) { Data = MessageManager.RemoveHeader(Data, "Message-ID"); } // Add the new message-id header. Data = MessageManager.AddHeader(Data, new MessageHeader("Message-ID", msgIDHeaderVal)); #endregion // Remove any control headers. headers = new MessageHeaderCollection(headers.Where(h => h.Name.StartsWith(MessageHeaderNames.HeaderNamePrefix, StringComparison.OrdinalIgnoreCase))); foreach (MessageHeader header in headers) { Data = MessageManager.RemoveHeader(Data, header.Name); } // If the MTA group doesn't exist or it's not got any IPs, use the default. if (mtaGroup == null || mtaGroup.VirtualMtaCollection.Count == 0) { ipGroupID = VirtualMta.VirtualMtaManager.GetDefaultVirtualMtaGroup().ID; } // Attempt to Enqueue the Email for Relaying. if (!QueueManager.Instance.Enqueue(messageID, ipGroupID, internalSendId, returnPath, RcptTo.ToArray(), Data)) { return(SmtpServerTransactionAsyncResult.FailedToEnqueue); } return(SmtpServerTransactionAsyncResult.SuccessMessageQueued); }
/// <summary> /// Looks through a feedback loop email looking for something to identify it as an abuse report and who it relates to. /// If found, logs the event. /// /// How to get the info depending on the ESP (and this is likely to be the best order to check for each too): /// Abuse Report Original-Mail-From. [Yahoo] /// Message-ID from body part child with content-type of message/rfc822. [AOL] /// Return-Path in main message headers. [Hotmail] /// </summary> /// <param name="message">The feedback look email.</param> public EmailProcessingDetails ProcessFeedbackLoop(string content) { EmailProcessingDetails processingDetails = new EmailProcessingDetails(); MimeMessage message = MimeMessage.Parse(content); if (message == null) { processingDetails.ProcessingResult = EmailProcessingResult.ErrorContent; return(processingDetails); } try { // Step 1: Yahoo! provide useable Abuse Reports (AOL's are all redacted). //Look for abuse report BodyPart abuseBodyPart = null; if (this.FindFirstBodyPartByMediaType(message.BodyParts, "message/feedback-report", out abuseBodyPart)) { // Found an abuse report body part to examine. // Abuse report content may have long lines whitespace folded. string abuseReportBody = MimeMessage.UnfoldHeaders(abuseBodyPart.GetDecodedBody()); using (StringReader reader = new StringReader(abuseReportBody)) { while (reader.Peek() > -1) { string line = reader.ReadToCrLf(); // The original mail from value will be the return-path we'd set so we should be able to get all the values we need from that. if (line.StartsWith("Original-Mail-From:", StringComparison.OrdinalIgnoreCase)) { string tmp = line.Substring("Original-Mail-From: ".Length - 1); try { int internalSendID = -1; string rcptTo = string.Empty; if (ReturnPathManager.TryDecode(tmp, out rcptTo, out internalSendID)) { // NEED TO LOG TO DB HERE!!!!! Sends.Send snd = MantaMTA.Core.DAL.SendDB.GetSend(internalSendID); Save(new MantaAbuseEvent { EmailAddress = rcptTo, EventTime = DateTime.UtcNow, EventType = MantaEventType.Abuse, SendID = (snd == null ? string.Empty : snd.ID) }); processingDetails.ProcessingResult = EmailProcessingResult.SuccessAbuse; return(processingDetails); } } catch (Exception) { // Must be redacted break; } } } } } // Function to use against BodyParts to find a return-path header. Func <MessageHeaderCollection, bool> checkForReturnPathHeaders = delegate(MessageHeaderCollection headers) { MessageHeader returnPathHeader = headers.GetFirstOrDefault("Return-Path"); if (returnPathHeader != null && !string.IsNullOrWhiteSpace(returnPathHeader.Value)) { int internalSendID = -1; string rcptTo = string.Empty; if (ReturnPathManager.TryDecode(returnPathHeader.Value, out rcptTo, out internalSendID)) { if (!rcptTo.StartsWith("redacted@", StringComparison.OrdinalIgnoreCase)) { // NEED TO LOG TO DB HERE!!!!! Sends.Send snd = MantaMTA.Core.DAL.SendDB.GetSend(internalSendID); Save(new MantaAbuseEvent { EmailAddress = rcptTo, EventTime = DateTime.UtcNow, EventType = MantaEventType.Abuse, SendID = (snd == null ? string.Empty : snd.ID) }); return(true); } } } MessageHeader messageIdHeader = headers.GetFirstOrDefault("Message-ID"); if (messageIdHeader != null && messageIdHeader.Value.Length > 33) { string tmp = messageIdHeader.Value.Substring(1, 32); Guid messageID; if (Guid.TryParse(tmp, out messageID)) { int internalSendID = -1; string rcptTo = string.Empty; tmp = ReturnPathManager.GetReturnPathFromMessageID(messageID); if (ReturnPathManager.TryDecode(tmp, out rcptTo, out internalSendID)) { // NEED TO LOG TO DB HERE!!!!! Sends.Send snd = MantaMTA.Core.DAL.SendDB.GetSend(internalSendID); Save(new MantaAbuseEvent { EmailAddress = rcptTo, EventTime = DateTime.UtcNow, EventType = MantaEventType.Abuse, SendID = (snd == null ? string.Empty : snd.ID) }); return(true); } } } return(false); } ; // Step 2: AOL give redacted Abuse Reports but include the original email as a bodypart; find that. BodyPart childMessageBodyPart; if (FindFirstBodyPartByMediaType(message.BodyParts, "message/rfc822", out childMessageBodyPart)) { if (childMessageBodyPart.ChildMimeMessage != null) { if (checkForReturnPathHeaders(childMessageBodyPart.ChildMimeMessage.Headers)) { processingDetails.ProcessingResult = EmailProcessingResult.SuccessAbuse; return(processingDetails); } } } // Step 3: Hotmail don't do Abuse Reports, they just return our email to us exactly as we sent it. if (checkForReturnPathHeaders(message.Headers)) { processingDetails.ProcessingResult = EmailProcessingResult.SuccessAbuse; return(processingDetails); } } catch (Exception) { } Logging.Debug("Failed to find return path!"); processingDetails.ProcessingResult = EmailProcessingResult.ErrorNoReturnPath; return(processingDetails); }