AnsiString 
   Canonicalization::GetDKIMWithoutSignature_(AnsiString value)
   {
      // locate the b= tag
      int pos = value.Find("b=");
      if (pos < 0)
         return "";

      // Locate end of b-tag. We need to use the ;-separator
      // here. The signature may contain spaces and newlines
      // if it's folded (and canon-mode is set to simple).
      int end = value.Find(";", pos);

      int actualEnd = 0;
      if (end > pos)
         actualEnd = end;
      else
         actualEnd = value.GetLength();

      int len = actualEnd - pos;

      AnsiString before = value.Mid(0, pos+2);
      AnsiString after = value.Mid(actualEnd);

      AnsiString result = before + after;

      return result;
   }
   set<boost::shared_ptr<SpamTestResult> >
   SpamTestSpamAssassin::RunTest(boost::shared_ptr<SpamTestData> pTestData)
   {
      set<boost::shared_ptr<SpamTestResult> > setSpamTestResults;
      
      AntiSpamConfiguration& config = Configuration::Instance()->GetAntiSpamConfiguration();

      boost::shared_ptr<Message> pMessage = pTestData->GetMessageData()->GetMessage();
      const String sFilename = PersistentMessage::GetFileName(pMessage);
      
      boost::shared_ptr<SpamAssassinClient> pSAClient = boost::shared_ptr<SpamAssassinClient>(new SpamAssassinClient(sFilename));

      boost::shared_ptr<TCPConnection> pClientConnection = Application::Instance()->GetIOCPServer()->CreateConnection();
      pClientConnection->Start(pSAClient);
      
      String sHost = config.GetSpamAssassinHost();
      int iPort = config.GetSpamAssassinPort();
      // Copy the event so that we know when we've disconnected.
      Event disconnectEvent(pClientConnection->GetConnectionTerminationEvent());

      // Here we handle of the ownership to the TCPIP-connection layer.
      if (pClientConnection->Connect(sHost, iPort, IPAddress()))
      {
         // Make sure we keep no references to the TCP connection so that it
         // can be terminated whenever. We're longer own the connection.
         pClientConnection.reset();

         disconnectEvent.Wait();
      }

      // Copy back the file...
      pSAClient->FinishTesting();
      
       
      // Check if the message is tagged as spam.
      boost::shared_ptr<MessageData> pMessageData = pTestData->GetMessageData();
      pMessageData->RefreshFromMessage();

      bool bIsSpam = false;
      AnsiString sSpamStatus = pMessageData->GetFieldValue("X-Spam-Status");
      if (sSpamStatus.Mid(0, 3).ToUpper() == "YES")
         bIsSpam = true;

      if (bIsSpam)
      {
         int iScore = 0;
         if (config.GetSpamAssassinMergeScore())
            iScore = _ParseSpamAssassinScore(sSpamStatus);
         else
            iScore = config.GetSpamAssassinScore();

         String sMessage = "Tagged as Spam by SpamAssassin";
         boost::shared_ptr<SpamTestResult> pResult = boost::shared_ptr<SpamTestResult>(new SpamTestResult(GetName(), SpamTestResult::Fail, iScore, sMessage));
         setSpamTestResults.insert(pResult);   
      }
      
      return setSpamTestResults;
   }
Example #3
0
   std::vector<AnsiString>
   StringParser::SplitString(const AnsiString &sInput, const AnsiString &sSeperators)
   {
      std::vector<AnsiString> vecResult;
      int iBeginning = 0;
      int iEnd = sInput.Find(sSeperators);

      if (iEnd == -1)
      {
         // The separator was not found in the string. 
         // We should put the entire string in the result.
         if (!sInput.IsEmpty())
            vecResult.push_back(sInput);

      }

      int iSeperatorLen = sSeperators.GetLength();

      while (iEnd >= 0)
      {
         int iSubStrLength = iEnd - iBeginning;

         String sSubString;
         sSubString = sInput.Mid(iBeginning, iSubStrLength);

         vecResult.push_back(sSubString);

         // Skip to the position where the next substring
         // can start
         iBeginning = iEnd + iSeperatorLen;
         iEnd = sInput.Find(sSeperators, iBeginning);   
      }

      if (iBeginning > 0)
      {
         String sSubString = sInput.Mid(iBeginning);
         if (!sSubString.IsEmpty())
            vecResult.push_back(sSubString);
      }

      return vecResult;


   }
 AnsiString 
 SimpleCanonicalization::CanonicalizeBody(AnsiString value)
 {
    // remove all empty lines.
    while (value.EndsWith("\r\n"))
       value = value.Mid(0, value.GetLength()-2);
    
    value += "\r\n";
    
    return value;
 }
Example #5
0
AnsiString HashCreator::GenerateHash(const AnsiString &inputString, const AnsiString &salt)
{
    AnsiString saltString = salt;
    if (saltString.GetLength() == 0 && _hashType == SHA256)
    {
        AnsiString randomString = PasswordGenerator::Generate();
        saltString = _GetHash(randomString, hex);
        saltString = saltString.Mid(0, SALT_LENGTH);
    }

    AnsiString value = saltString + _GetHash(saltString + inputString, hex);
    return value;
}
Example #6
0
   // store the body part to un-encoded string buffer
   void MimeBody::Store(AnsiString &output, bool bIncludeHeader) const
   {
      // store header fields
      int nSize = 0;

      if (bIncludeHeader)
         MimeHeader::Store(output);

      // Copy the data to the output buffer. 
      output.append(m_pbText);

      if (m_listBodies.empty())
         return;

      // store child body parts
      string strBoundary = GetBoundary();
      if (strBoundary.empty())
         return;					// boundary not be set

      int nBoundSize = (int)strBoundary.size() + 6;

// Bill48105 - These iOutputSize are temp fix for [ ambiguous error
	  int iOutputSizeLess2 = output.size() - 2;
	  int iOutputSizeLess1 = output.size() - 1;
      for (BodyList::const_iterator it=m_listBodies.begin(); it!=m_listBodies.end(); it++)
      {
         // If the initial body ends with \r\n, remove them. We add new ones below.
         if (m_listBodies.begin() == it && output.size() >= 2 && 
            output[iOutputSizeLess2] == '\r' && output[iOutputSizeLess1] == '\n')
         {
            output = output.Mid(0, output.GetLength() - 2);
         }

         AnsiString boundaryLine = Formatter::Format(_T("\r\n--{0}\r\n"), String(strBoundary));
         output.append(boundaryLine);

         shared_ptr<MimeBody> pBP = *it;
         ASSERT(pBP != NULL);	

         pBP->Store(output);
      }

      AnsiString endBoundaryLine = Formatter::Format(_T("\r\n--{0}--\r\n"), String(strBoundary));
      output.append(endBoundaryLine);
   }
   AnsiString 
   RelaxedCanonicalization::CanonicalizeHeaderValue(AnsiString value)
   {
      /*
      Unfold all header field continuation lines as described in
      [RFC2822]; in particular, lines with terminators embedded in
      continued header field values (that is, CRLF sequences followed by
      WSP) MUST be interpreted without the CRLF.  Implementations MUST
      NOT remove the CRLF at the end of the header field value.
      */

      value.Replace("\r\n ", " ");
      value.Replace("\r\n\t", " ");

      /*
      Convert all sequences of one or more WSP characters to a single SP
      character.  WSP characters here include those before and after a
      line folding boundary.
      */

      value.Replace("\t ", " ");
      value.Replace(" \t", " ");
      
      while (value.Find("  ") >= 0)
         value.Replace("  ", " ");

      /*
      Delete all WSP characters at the end of each unfolded header field value.
      */

      while (value.EndsWith(" ") || value.EndsWith("\t"))
         value = value.Mid(0, value.GetLength() -1);

      /* Delete any WSP characters remaining before and after the colon
      separating the header field name from the header field value.  The
      colon separator MUST be retained.
      */
      value.Trim();


      return value;
   }
   int 
   SpamTestSpamAssassin::_ParseSpamAssassinScore(const AnsiString &sHeader)
   {
      int iStartPos = sHeader.FindNoCase("score=");
      if (iStartPos < 0)
         return 0;

      iStartPos += 6;

      int iScoreEnd = sHeader.Find(".", iStartPos);
      if (iScoreEnd < 0)
         return 0;

      int iScoreLength = iScoreEnd - iStartPos;

      if (iScoreLength <= 0)
         return 0;

      AnsiString sScore = sHeader.Mid(iStartPos, iScoreLength);

      int iRetVal = atoi(sScore);
      return iRetVal;

   }
   void
   SMTPClientConnection::InternalParseData(const AnsiString  &Request)
   {
      LOG_DEBUG("SMTPClientConnection::_ParseASCII()");

      String sData = "RECEIVED: " + Request;
      LOG_SMTP_CLIENT(GetSessionID(), GetIPAddress().ToString(), sData);

      // Below 3 lines is fix of the problem that occurs when the remote server answers
      // with 2 line in his welcome message.
      String sMinus = "-";
      if ((Request.GetLength() > 3) && (Request.GetAt(3) == sMinus.GetAt(0)))
      {
         LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 1");
         return;
      }

      int lFirstSpace = Request.Find(" ");
   
      AnsiString sFirstWordTemp;
      if (lFirstSpace < 0)
         sFirstWordTemp = Request;
      else
         sFirstWordTemp = Request.Mid(0, lFirstSpace);
      sFirstWordTemp.MakeUpper();
      int iCode = atoi(sFirstWordTemp);

      // We should not update all recipient's if we've just sent a
      // RCPT TO. We should only update the specific one we've just
      // sent to.
      // 
      // Also, we should not update state if we've just sent QUIT to
      // the server. At the time we send QUIT, the message has already
      // been either accepted for delivery and rejected. Any error after
      // this isn't relvat.
      if (m_CurrentState != RCPTTOSENT && m_CurrentState != QUITSENT)
      {
         if (IsPermanentNegative(iCode))
         {
            _UpdateAllRecipientsWithError(iCode, Request, false);
            _SendQUIT();
            return;
         }
         else if (IsTransientNegative(iCode))
         {
            _UpdateAllRecipientsWithError(iCode, Request, false);
            _SendQUIT();
            return;
         }
      }
   
      switch (m_CurrentState)
      {
      case SENDUSERNAME:
         _ProtocolSendUsername();
         break;
      case SENDPASSWORD:
         _ProtocolSendPassword();
         break;
      case PASSWORDCHECK:
         if (!_ProtocolPassswordCheck(iCode, Request))
         {
            // Authentication failed. We have just sent
            // a quit command.
            return;
         }

         break;
      }
      
      if (m_CurrentState == HELO)
      {
         if (iCode == 220)
         {
			   String sComputerName = Utilities::ComputerName(); 
      
            if (m_bUseSMTPAuth)
            {
               _SendData("EHLO " + sComputerName);
               _SetState(EHLOSENT);
            }
            else
            {
               _SendData("HELO " + sComputerName);
               _SetState(HELOSENT);
            }
        
            LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 2");

            return ;
         }
         else
         {
            LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 3");
            _UpdateAllRecipientsWithError(iCode, Request, false);
            _SendQUIT();
            return;
         }
      }

      if (m_CurrentState == EHLOSENT)
      {
         // Ask the server to initiate login process.
         _SendData("AUTH LOGIN");
         _SetState(SENDUSERNAME);
         return ;
      }

      if (m_CurrentState == HELOSENT)
      {
         if (IsPositiveCompletion(iCode))
         {
            // --- Server accepted HELO. Go to HEADER/MAILFROM state.
            _SetState(MAILFROM);
         }
	      else
         {
            _UpdateAllRecipientsWithError(iCode, Request, false);
         }
      }
   

      if (m_CurrentState == MAILFROM)
      {
         String sFrom = m_pDeliveryMessage->GetFromAddress();
         String sData = "MAIL FROM:<" + sFrom + ">";
         _SendData(sData);
         m_CurrentState = MAILFROMSENT;
         LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 4");
         return;
      }

      if (m_CurrentState == MAILFROMSENT)
      {
         if (IsPositiveCompletion(iCode))
         {
            // --- Server accepted mail from. Go to header/rcpt to state.
            m_CurrentState = RCPTTO;
         }
         else
         {
            LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 5");
            _UpdateAllRecipientsWithError(iCode, Request, false);
         }

      }

      if (m_CurrentState == RCPTTO)
      {
         LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 6");

         shared_ptr<MessageRecipient> pRecipient = _GetNextRecipient();
         if (!pRecipient) 
         {
            _SendQUIT();
            return;
         }
         
         String sRecipient = pRecipient->GetAddress();
         String sData = "RCPT TO:<" + sRecipient + ">";
         _SendData(sData);
         m_CurrentState = RCPTTOSENT;

         return;

      }

      if (m_CurrentState == RCPTTOSENT)
      {
         if (m_iCurRecipient < m_vecRecipients.size())
         {
            if (IsPositiveCompletion(iCode))
            {
               _actualRecipients.insert(m_vecRecipients[m_iCurRecipient]);
            }
            else
            {
               _UpdateRecipientWithError(iCode, Request, m_vecRecipients[m_iCurRecipient], false);
            }
         }

         shared_ptr<MessageRecipient> pRecipient = _GetNextRecipient();
         if (pRecipient)
         {
            // Send next recipient.
            _SendData("RCPT TO:<" + pRecipient->GetAddress() + ">");
            m_CurrentState = RCPTTOSENT;
         }
         else
         {
            if (_actualRecipients.size() == 0)
            {
               _SendQUIT();
               return;
            }
            
            m_CurrentState = DATAQUESTION;
         }
      }

      if (m_CurrentState == DATAQUESTION)
      {
         _SendData("DATA");
         m_CurrentState = DATA;
         LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 7");
         return;
      }

      if (m_CurrentState == DATA)
      {
         if (IsPositiveIntermediate(iCode))
         {
            // Send the data!
            const String fileName = PersistentMessage::GetFileName(m_pDeliveryMessage);
            LOG_DEBUG("SMTPClientConnection::~_BEFORE SendFile");
            _StartSendFile(fileName);
            LOG_DEBUG("SMTPClientConnection::~_AFTER SendFile");
            return;
         }
      }

      if (m_CurrentState == DATASENT)
      {
            LOG_DEBUG("SMTPClientConnection::~_BEFORE SendQUIT");
         _SendQUIT();
            LOG_DEBUG("SMTPClientConnection::~_AFTER SendQUIT");

         if (IsPositiveCompletion(iCode))
         {
            _UpdateSuccessfulRecipients();
            LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 9");
            return;
         }
         else
         {
            _UpdateAllRecipientsWithError(iCode, Request, false);
            LOG_DEBUG("SMTPClientConnection::~_ParseASCII() - 10");
         }

         return;
      }

      if (m_CurrentState == QUITSENT)
      {     
         // We just received a reply on our QUIT. Time to disconnect.
         m_bPendingDisconnect = true;
         PostDisconnect();
      }
   }
Example #10
0
   // get the value of a parameter
   bool MimeField::GetParameter(const char* pszAttr, AnsiString& strValue) const
   {
      strValue = "";

      bool encodedParameter = false;

      vector<AnsiString> parameters = StringParser::SplitString(AnsiString(m_strValue), ";");

      for (unsigned int i = 1; i < parameters.size(); i++)
      {
         AnsiString value = parameters[i];
         value.TrimLeft();

         // Locate end of parameter name.
         int nameEndPos = 0;
         for (nameEndPos = 0; nameEndPos < value.GetLength(); nameEndPos++)
         {
            char c = value[nameEndPos];

            if (c == ' ' || c == '*' || c == '=')
               break;
         }

         // If we haven't found any value for this parameter, bail out.
         if (nameEndPos == 0 && strValue.IsEmpty())
            return false;

         AnsiString parameterName = value.Mid(0, nameEndPos);

         if (parameterName.CompareNoCase(pszAttr) != 0)
            continue;

         // Locate start of parameter value.
         int valuePos = 0;
         for (valuePos = nameEndPos; valuePos < value.GetLength(); valuePos++)
         {
            char c = value[valuePos];

            if (c == '=')
               break;
         }

         // We want the char before = NOT char after param name
         // to detect encoding per RFC 2231 4.1
         // http://www.hmailserver.com/forum/viewtopic.php?f=10&t=21417
         char characterBeforeEquals = value[valuePos - 1];

         if (characterBeforeEquals == '*')
            encodedParameter = true;

         // Skip past the equal sign.
         valuePos++;

         // Locate the start of the actual value. May be enclosed with quotes.
         // 
         // For instance, this is perfectly valid 
         // Content-Type: text/plain; charset = "iso-8859-1"
         //
         for (; valuePos < value.GetLength(); valuePos++)
         {
            char c = value[valuePos];

            if (c == ' ' || c == '"')
               continue;
            else
               break;
         }

         // Locate the end of the value. The value may contain
         // pretty much any character, including space.
         int valueEndPos = valuePos;
         for (; valueEndPos < value.GetLength(); valueEndPos++)
         {
            char c = value[valueEndPos];

            if (c == ';' || c == '"')
               break;
            else
               continue;
         }

         int valueLength = valueEndPos - valuePos;

         value = value.Mid(valuePos, valueLength);

         // If the value is
         //    Content-Type: text/plain; charset = "iso-8859-1"  
         // it needs to be trimmed.
         value.TrimRight();

         strValue.append(value);
      }

      if (strValue.IsEmpty())
         return false;

      /*
      (2)   MIME headers, like the RFC 822 headers they often
      appear in, are limited to 7bit US-ASCII, and the
      encoded-word mechanisms of RFC 2047 are not available
      to parameter values.  This makes it impossible to have
      parameter values in character sets other than US-ASCII
      without specifying some sort of private per-parameter
      encoding.

      http://tools.ietf.org/html/draft-freed-pvcsc-03

      Examples:
      Input: ISO-8859-1''%E9%2E%70%6E%67
      Output: =?ISO-8859-1?Q?=E9=2Epng?=
      */

      if (encodedParameter)
      {
         MimeParameterRFC2184Decoder decoder;
         strValue.assign(decoder.Decode(strValue));
      }

      strValue.TrimLeft();

      return true;
   }
   VirusScanningResult
   ClamAVVirusScanner::Scan(const String &hostName, int primaryPort, const String &sFilename)
   {
      LOG_DEBUG("Connecting to ClamAV virus scanner...");

      int streamPort = 0;

      TimeoutCalculator calculator;

      SynchronousConnection commandConnection(calculator.Calculate(IniFileSettings::Instance()->GetClamMinTimeout(), IniFileSettings::Instance()->GetClamMaxTimeout()));
      if (!commandConnection.Connect(hostName, primaryPort))
      {
         return VirusScanningResult(_T("ClamAVVirusScanner::Scan"), 
            Formatter::Format("Unable to connect to ClamAV server at {0}:{1}.", hostName, primaryPort));
      }

      if (!commandConnection.Write("STREAM\r\n"))
         return VirusScanningResult("ClamAVVirusScanner::Scan", "Unable to write STREAM command.");

      AnsiString readData;
      if (!commandConnection.ReadUntil("\n", readData))
         return VirusScanningResult("ClamAVVirusScanner::Scan", "Unable to read STREAM command response.");

      if (!readData.StartsWith("PORT"))
         return VirusScanningResult("ClamAVVirusScanner::Scan", Formatter::Format("Protocol error. Unexpected response: {0}.", readData));
      
      readData.TrimRight("\n");

      // Determine port.
      std::string portString = readData.Mid(5);
      
      if (!StringParser::TryParseInt(portString, streamPort))
         return VirusScanningResult("ClamAVVirusScanner::Scan", Formatter::Format("Protocol error. Unexpected response: {0} (Unable to parse port).", readData));

      LOG_DEBUG("Connecting to ClamAV stream port...");
      SynchronousConnection streamConnection(15);
      if (!streamConnection.Connect(hostName, streamPort))
         return VirusScanningResult("ClamAVVirusScanner::Scan", Formatter::Format("Unable to connect to ClamAV stream port at {0}:{1}.", hostName, streamPort));

      // Send the file on the stream socket.
      File oFile;
      if (!oFile.Open(sFilename, File::OTReadOnly))
      {
         String sErrorMsg = Formatter::Format("Could not send file {0} via socket since it does not exist.", sFilename);
         return VirusScanningResult("ClamAVVirusScanner::Scan", sErrorMsg);
      }

      const int STREAM_BLOCK_SIZE = 4096;
      const int maxIterations = 100000;
      for (int i = 0; i < maxIterations; i++)
      {
         std::shared_ptr<ByteBuffer> pBuf = oFile.ReadChunk(STREAM_BLOCK_SIZE);

         if (!pBuf)
            break;

         // Send the request.
         if (!streamConnection.Write(*pBuf))
            return VirusScanningResult("ClamAVVirusScanner::Scan", "Unable to write data to stream port.");
      }

      streamConnection.Close();

      if (!commandConnection.ReadUntil("\n", readData))
         return VirusScanningResult("ClamAVVirusScanner::Scan", "Unable to read response (after streaming).");

      readData.TrimRight("\n");

      // Parse the response and see if a virus was reported.
      try
      {
         const regex expression("^stream.*: (.*) FOUND$"); 
         cmatch what; 
         if(regex_match(readData.c_str(), what, expression)) 
         {
            LOG_DEBUG("Virus detected: " + what[1]);
            return VirusScanningResult(VirusScanningResult::VirusFound, String(what[1]));
         }
         else
         {
            LOG_DEBUG("No virus detected: " + readData);
            return VirusScanningResult(VirusScanningResult::NoVirusFound, Formatter::Format("Result: {0}", readData));
         }
      }
      catch (std::runtime_error &) // regex_match will throw runtime_error if regexp is too complex.
      {
         return VirusScanningResult("ClamAVVirusScanner::Scan", "Unable to parse regular expression.");
      }

      
   }
Example #12
0
AnsiString HashCreator::_GetSalt(const AnsiString &inputString)
{
    AnsiString result = inputString.Mid(0,SALT_LENGTH);
    return result;
}
Example #13
0
   POP3Connection::ParseResult
   POP3Connection::InternalParseData(const AnsiString &Request)
   //---------------------------------------------------------------------------()
   // DESCRIPTION:
   // Parses a client POP3 command.
   //---------------------------------------------------------------------------()
   {
      _LogClientCommand(Request);

      if (Request.GetLength() > 500)
      {
         // This line is to long... is this an evil user?
         _SendData("-ERR Line to long.");
         return ResultNormalResponse;
      }
      
      String sCommand;
      String sParameter;

      if (Request.Find(" ")>-1)
      {
         sCommand = Request.Left(Request.Find(" "));
         sParameter = Request.Mid(Request.Find(" ") + 1);
      }
      else
         sCommand = Request;

      POP3Command command = GetCommand(m_CurrentState, sCommand);

      switch (command)
      {
         case NOOP:
            _SendData("+OK");
            return ResultNormalResponse;
         case HELP:
            _SendData("+OK Normal POP3 commands allowed");
            return ResultNormalResponse;
         case USER:
            _ProtocolUSER(sParameter);
            return ResultNormalResponse;
         case PASS:
            return _ProtocolPASS(sParameter);
         case STAT:
            _ProtocolSTAT(sParameter);
            return ResultNormalResponse;
         case LIST:
            _ProtocolLIST(sParameter);
            return ResultNormalResponse;
         case RETR:
            return _ProtocolRETR(sParameter);
         case DELE:
            _ProtocolDELE (sParameter);
            return ResultNormalResponse;
         case TOP:
            _ProtocolTOP(sParameter);
            return ResultNormalResponse;
         case RSET:
            _ProtocolRSET();
            return ResultNormalResponse;   
         case UIDL:
            _ProtocolUIDL(sParameter);
            return ResultNormalResponse;   
         case QUIT:
            _ProtocolQUIT();
            return ResultDisconnect;
         case CAPA:
            _SendData("+OK CAPA list follows\r\nUSER\r\nUIDL\r\nTOP\r\n.");
            return ResultNormalResponse;
         case INVALID:
            _SendData("-ERR Invalid command in current state." );
            return ResultNormalResponse;  
         default:
            assert(0); // What command is this?
            _SendData("-ERR Invalid command in current state." );
            return ResultNormalResponse;  
      }
   }
   bool
   SMTPClientConnection::InternalParseData(const AnsiString &Request)
   {
      LogReceivedResponse_(Request);

      int lFirstSpace = Request.Find(" ");
   
      AnsiString sFirstWordTemp;
      if (lFirstSpace < 0)
         sFirstWordTemp = Request;
      else
         sFirstWordTemp = Request.Mid(0, lFirstSpace);

      sFirstWordTemp.MakeUpper();
      int iCode = atoi(sFirstWordTemp);
   
      bool ifFailureFailAllRecipientsAndQuit = 
         current_state_ == HELO ||
         current_state_ == HELOSENT ||
         current_state_ == AUTHLOGINSENT ||
         current_state_ == USERNAMESENT || 
         current_state_ == PASSWORDSENT ||
         current_state_ == MAILFROMSENT ||
         current_state_ == DATACOMMANDSENT ||
         current_state_ == DATASENT ||
         current_state_ == PASSWORDSENT;

      if (ifFailureFailAllRecipientsAndQuit)
      {
         if (!IsPositiveCompletion(iCode))
         {
            UpdateAllRecipientsWithError_(iCode, Request, false);
            SendQUIT_();
            return true;
         }
      }

      switch (current_state_)
      {
      case HELO:
         ProtocolStateHELOEHLO_(Request);  
         return true;
      case HELOSENT:
         ProtocolHELOSent_(Request);
         return true;
      case EHLOSENT:
         ProtocolEHLOSent_(iCode, Request);
         return true;
      case STARTTLSSENT:
         ProtocolSTARTTLSSent_(iCode);
         return false;
      case AUTHLOGINSENT:
         ProtocolSendUsername_();
         return true;
      case USERNAMESENT:
         ProtocolSendPassword_();
         return true;
      case PASSWORDSENT:
         ProtocolSendMailFrom_();
         return true;
      case MAILFROMSENT:
         ProtocolMailFromSent_();
         return true;
      case DATACOMMANDSENT:
         ProtocolData_();
         return false;
      case DATASENT:
         SendQUIT_();
         UpdateSuccessfulRecipients_();
         return true;
      case RCPTTOSENT:
         ProtocolRcptToSent_(iCode, Request);
         return true;
      case QUITSENT:
         // We just received a reply on our QUIT. Time to disconnect.
         EnqueueDisconnect();
         return false;
      }

      return true;
   }
   AnsiString 
   SimpleCanonicalization::CanonicalizeHeader(AnsiString header, const std::pair<AnsiString, AnsiString> &signatureField, const std::vector<AnsiString> &fieldsToInclude, AnsiString &fieldList)
   {
      // first build a formatted list of header lines.
      std::vector<AnsiString> formattedHeaderLines;

      AnsiString result;
      std::vector<AnsiString> headerLines = StringParser::SplitString(header, "\r\n");

      AnsiString foldedLines;
      for (size_t i = headerLines.size(); i > 0; i--)
      {
         AnsiString line = headerLines[i-1];

         if (line.StartsWith(" ") || line.StartsWith("\t"))
         {
            // line is folded. append to next.
            foldedLines = line + "\r\n" + foldedLines;
         }
         else
         {
            // we have a line!
            int colonPos = line.Find(":");
            if (colonPos < 0)
            {
               assert(0); // broken header.
               continue;
            }

            AnsiString entireHeaderField = line + "\r\n" + foldedLines;

            formattedHeaderLines.push_back(entireHeaderField);

            foldedLines = "";
         }
      }

      for(AnsiString fieldToInclude : fieldsToInclude)
      {
         fieldToInclude.Trim();

         // locate the header line.
         auto iter = formattedHeaderLines.begin();
         auto iterEnd = formattedHeaderLines.end();
         
         for (; iter != iterEnd; iter++)
         {
            AnsiString headerLine = (*iter);

            int colonPos = headerLine.Find(":");
            AnsiString headerName = headerLine.Mid(0, colonPos);

            if (headerName.CompareNoCase(fieldToInclude) == 0) 
            {
               result += headerLine;

               if (!fieldList.IsEmpty())
                  fieldList += ":";

               fieldList += headerName;

               formattedHeaderLines.erase(iter);
               break;
            }
         }

      }

      if (!signatureField.first.IsEmpty())
      {
         // Don't pick the value from the actual header, use the header we're verifying instead
         // If there are more than one DKIM-signature fields in the header, this will be important.
         AnsiString headerName = signatureField.first;

         AnsiString headerLine = headerName + ": " + GetDKIMWithoutSignature_(signatureField.second);

         if (headerLine.EndsWith("\r\n"))
            headerLine = headerLine.Mid(0, headerLine.GetLength()-2);

         result += headerLine;
      }


      return result;
   }