Advertisement
Guest User

SMTPConnection.h

a guest
Dec 26th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. // Copyright (c) 2005 Martin Knafve / hMailServer.com.  
  2. // http://www.hmailserver.com
  3.  
  4. #pragma once
  5.  
  6.  
  7. #include "../Common/TCPIP/ProtocolParser.h"
  8. #include "RecipientParser.h"
  9. #include "../Common/BO/Collection.h"
  10. #include "../common/persistence/PersistentDomain.h"
  11.  
  12. namespace HM
  13. {
  14.  
  15.    class SMTPConfiguration;
  16.    class Message;
  17.    class Messages;
  18.    class TransparentTransmissionBuffer;
  19.    class MessageData;
  20.    class Domain;
  21.    class SpamTestResult;
  22.  
  23.    enum eSMTPCommandTypes
  24.    {
  25.       SMTP_COMMAND_UNKNOWN = 0,
  26.       SMTP_COMMAND_HELO = 1001,  
  27.       SMTP_COMMAND_HELP = 1002,  
  28.       SMTP_COMMAND_QUIT = 1003,  
  29.       SMTP_COMMAND_EHLO = 1004,  
  30.       SMTP_COMMAND_AUTH = 1005,  
  31.       SMTP_COMMAND_MAIL = 1006,  
  32.       SMTP_COMMAND_RCPT = 1007,  
  33.       SMTP_COMMAND_TURN = 1008,  
  34.       SMTP_COMMAND_VRFY = 1009,
  35.       SMTP_COMMAND_DATA = 1010,
  36.       SMTP_COMMAND_RSET = 1011,
  37.       SMTP_COMMAND_NOOP = 1012,
  38.       SMTP_COMMAND_ETRN = 1013,
  39.       SMTP_COMMAND_STARTTLS = 1014
  40.    };
  41.  
  42.    class SMTPConnection : public ProtocolParser,
  43.                           public boost::enable_shared_from_this<SMTPConnection>
  44.    {
  45.    public:
  46.        SMTPConnection();
  47.        virtual ~SMTPConnection();
  48.      
  49.    protected:
  50.  
  51.       virtual void OnConnected();
  52.       virtual AnsiString GetCommandSeparator() const;
  53.  
  54.       virtual void ParseData(const AnsiString &sRequest);
  55.       virtual void ParseData(shared_ptr<ByteBuffer> pBuf);
  56.  
  57.       virtual void _SendData(const String &sData);
  58.  
  59.       virtual void OnConnectionTimeout();
  60.       virtual void OnExcessiveDataReceived();
  61.  
  62.    private:
  63.  
  64.       bool _TryExtractAddress(const String &mailFromParameter, String& address);
  65.       void _HandleSMTPFinalizationTaskCompleted();
  66.  
  67.       virtual void InternalParseData(const AnsiString &sRequest);
  68.  
  69.       enum SpamProtectionType
  70.       {
  71.          SPNone = 0,
  72.          SPPreTransmission = 1,
  73.          SPPostTransmission = 2
  74.       };
  75.          
  76.       enum Constants
  77.       {
  78.          MaxNumberOfRecipients = 50000
  79.       };
  80.  
  81.       void _InitializeSpamProtectionType(const String &sFromAddress);
  82.  
  83.       bool _CheckLineEndings() const;
  84.  
  85.       void _LogClientCommand(const String &sClientData) const;
  86.  
  87.       void _LogAwstatsMessageRejected();
  88.      
  89.       bool _DoSpamProtection(SpamProtectionType spType, const String &sFromAddress, const String &hostName, const IPAddress &lIPAddress);
  90.       // Does IP based spam protection. Returns true if we should
  91.       // continue delivery, false otherwise.
  92.  
  93.       void _ResetCurrentMessage();
  94.       bool CheckIfValidSenderAddress(const String &sFromAddress);
  95.      
  96.       bool ReAuthenticateUser();
  97.  
  98.       void _AppendMessageHeaders();
  99.  
  100.       eSMTPCommandTypes _GetCommandType(const String &sType);
  101.  
  102.       void _DoPreAcceptMessageModifications();
  103.       // Make changes to the message before it's accepted for delivery. This is
  104.       // for example where message signature and spam-headers are added.
  105.  
  106.       void _SetMessageSignature(shared_ptr<MessageData> &pMessageData);
  107.       // Sets the signature of the message, based on the signature in the account
  108.       // settings and domain settings.
  109.  
  110.       bool _OnPreAcceptTransfer();
  111.       bool _DoPreAcceptSpamProtection();
  112.  
  113.       void _ProtocolEHLO(const String &sRequest);
  114.       void _ProtocolHELO(const String &sRequest);
  115.       void _ProtocolAUTH(const String &sRequest);
  116.       void _ProtocolNOOP();
  117.       void _ProtocolRSET();
  118.  
  119.       bool _LookupRoute(const String &sToAddress, bool &bDomainIsRemote);
  120.       bool _ProtocolMAIL(const String &Request);
  121.       void _ProtocolQUIT();
  122.       void _ProtocolHELP();
  123.       void _ProtocolRCPT(const String &Request);
  124.       void _ProtocolETRN(const String &sRequest);
  125.  
  126.       void _TarpitCheckDelay();
  127.  
  128.       void _AuthenticateUsingPLAIN(const String &sLine);
  129.       // Authenticates using a PLAIN line.
  130.  
  131.       void _Authenticate();
  132.       // validates the username and password.
  133.  
  134.       void _RestartAuthentication();
  135.       // restarts the authentication process.
  136.      
  137.       void _ResetLoginCredentials();
  138.       // restarts the authentication process.
  139.  
  140.       bool _SendEHLOKeywords();
  141.  
  142.       int _GetMaxMessageSize(shared_ptr<const Domain> pDomain);
  143.  
  144.       bool _ReadDomainAddressFromHelo(const String &sRequest);
  145.  
  146.       void _SendErrorResponse(int iErrorCode, const String &sResponse);
  147.  
  148.       bool _GetDoSpamProtection() const;
  149.  
  150.       bool _GetIsLocalSender();
  151.  
  152.       String _GetSpamTestResultMessage(set<shared_ptr<SpamTestResult> > testResult) const;
  153.  
  154.       enum ConnectionState
  155.       {
  156.          AUTHENTICATION = 1,
  157.          SMTPSELECTAUTH = 2,
  158.          SMTPUSERNAME = 3,
  159.          SMTPUPASSWORD = 4,
  160.          HEADER = 5,
  161.          DATA = 6
  162.       };
  163.  
  164.       enum AuthenticationType
  165.       {
  166.          AUTH_NONE = 0,
  167.          AUTH_PLAIN = 2,
  168.          AUTH_LOGIN = 3,
  169.       };
  170.  
  171.      
  172.  
  173.       ConnectionState m_CurrentState;
  174.  
  175.       shared_ptr<Message> m_pCurrentMessage;
  176.  
  177.       bool m_bTraceHeadersWritten;
  178.  
  179.       String m_sUsername;
  180.       String m_sPassword;
  181.  
  182.       shared_ptr<SMTPConfiguration> m_SMTPConf;
  183.    
  184.       AuthenticationType _requestedAuthenticationType;
  185.      
  186.       DWORD m_lMessageStartTC;
  187.  
  188.       int m_iMaxMessageSizeKB;
  189.       // Maximum message size in KB.
  190.  
  191.       String m_sHeloHost;
  192.  
  193.       shared_ptr<TransparentTransmissionBuffer> m_pTransmissionBuffer;
  194.  
  195.       // Spam detection
  196.       bool m_bRejectedByDelayedGreyListing;
  197.       int m_iCurNoOfRCPTTO;
  198.       int m_iCurNoOfInvalidCommands;
  199.      
  200.       shared_ptr<const Domain> m_pSenderDomain;
  201.       shared_ptr<const Account> m_pSenderAccount;
  202.  
  203.       set<shared_ptr<SpamTestResult> > m_setSpamTestResults;
  204.  
  205.       bool m_bReAuthenticateUser;
  206.       bool m_bPendingDisconnect;
  207.       bool _isAuthenticated;
  208.       SpamProtectionType m_spType;
  209.  
  210.       RecipientParser _recipientParser;
  211.       bool usingStartTLS;
  212.    };
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement