E-MailRelay
gsmtpserver.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gsmtpserver.h
19 ///
20 
21 #ifndef G_SMTP_SERVER_H
22 #define G_SMTP_SERVER_H
23 
24 #include "gdef.h"
25 #include "gsmtp.h"
26 #include "gnoncopyable.h"
27 #include "gmultiserver.h"
28 #include "gsmtpclient.h"
29 #include "glinebuffer.h"
30 #include "gverifier.h"
31 #include "gmessagestore.h"
32 #include "gserverprotocol.h"
33 #include "gprotocolmessage.h"
34 #include "gexception.h"
35 #include <string>
36 #include <sstream>
37 #include <memory>
38 #include <list>
39 
40 namespace GSmtp
41 {
42  class Server ;
43  class ServerPeer ;
44 }
45 
46 /// \class GSmtp::Server
47 /// An SMTP server class.
48 ///
50 {
51 public:
52  typedef std::vector<GNet::Address> AddressList ;
53  G_EXCEPTION( Overflow , "too many interface addresses" ) ;
54 
55  struct Config /// A structure containing GSmtp::Server configuration parameters.
56  {
57  bool allow_remote ;
58  unsigned int port ;
59  AddressList interfaces ;
60  std::string ident ;
61  bool anonymous ;
62  std::string filter_address ;
63  unsigned int filter_timeout ;
64  std::string verifier_address ;
65  unsigned int verifier_timeout ;
66  bool verifier_compatibility ;
67  ServerProtocol::Config protocol_config ;
68 
69  Config( bool allow_remote , unsigned int port , const AddressList & , const std::string & ident ,
70  bool anonymous , const std::string & filter_address , unsigned int filter_timeout ,
71  const std::string & verifier_adress , unsigned int verifier_timeout , bool verifier_compatibility ,
72  ServerProtocol::Config protocol_config ) ;
73  } ;
74 
75  Server( GNet::ExceptionHandler & eh , MessageStore & store , const GAuth::Secrets & client_secrets ,
76  const GAuth::Secrets & server_secrets , Config server_config , std::string forward_to ,
77  GSmtp::Client::Config client_config ) ;
78  ///< Constructor. Listens on the given port number using
79  ///< INET_ANY if 'interfaces' is empty, or on specific
80  ///< interfaces otherwise.
81  ///<
82  ///< If the forward-to address is given then all messages are
83  ///< forwarded immediately, using the given client configuration.
84  ///<
85  ///< The references are kept.
86 
87  virtual ~Server() ;
88  ///< Destructor.
89 
90  void report() const ;
91  ///< Generates helpful diagnostics after construction.
92 
94  ///< Returns a signal that indicates that something has happened.
95 
96  unique_ptr<ProtocolMessage> newProtocolMessage( ServerPeer & ) ;
97  ///< Called by GSmtp::ServerPeer to construct a ProtocolMessage.
98 
99 private:
100  unique_ptr<Filter> newFilter( ServerPeer & ) ;
101  unique_ptr<ProtocolMessage> newProtocolMessageStore( unique_ptr<Filter> ) ;
102  unique_ptr<ProtocolMessage> newProtocolMessageScanner( unique_ptr<ProtocolMessage> ) ;
103  unique_ptr<ProtocolMessage> newProtocolMessageForward( unique_ptr<ProtocolMessage> ) ;
104  unique_ptr<ServerProtocol::Text> newProtocolText( bool , const GNet::Address & ) const ;
105  virtual GNet::ServerPeer * newPeer( GNet::Server::PeerInfo , GNet::MultiServer::ServerInfo ) override ; // GNet::MultiServer
106 
107 private:
108  MessageStore & m_store ;
109  Config m_server_config ;
110  Client::Config m_client_config ;
111  const GAuth::Secrets & m_server_secrets ;
112  std::string m_forward_to ;
113  const GAuth::Secrets & m_client_secrets ;
115 } ;
116 
117 /// \class GSmtp::ServerPeer
118 /// Handles a connection from a remote SMTP client.
119 /// \see GSmtp::Server
120 ///
122 {
123 public:
124  G_EXCEPTION( SendError , "failed to send smtp response" ) ;
125 
127  const GAuth::Secrets & server_secrets , const Server::Config & server_config ,
128  unique_ptr<ServerProtocol::Text> ptext ) ;
129  ///< Constructor.
130 
131 protected:
132  virtual void onSendComplete() override ;
133  ///< Override from GNet::ServerPeer.
134 
135  virtual void onDelete( const std::string & reason ) override ;
136  ///< Override from GNet::ServerPeer.
137 
138  virtual void onData( const char * , size_t ) override ;
139  ///< Override from GNet::SocketProtocolSink.
140 
141  virtual void onSecure( const std::string & ) override ;
142  ///< Override from GNet::SocketProtocolSink.
143 
144 private:
145  ServerPeer( const ServerPeer & ) ;
146  void operator=( const ServerPeer & ) ;
147  virtual void protocolSend( const std::string & line , bool ) override ; // override from private base class
148  virtual void protocolShutdown() override ; // override from private base class
149 
150 private:
151  Server & m_server ;
152  unique_ptr<Verifier> m_verifier ;
153  unique_ptr<ProtocolMessage> m_pmessage ;
154  unique_ptr<ServerProtocol::Text> m_ptext ;
155  GNet::LineBuffer m_line_buffer ;
156  ServerProtocol m_protocol ; // order dependency -- last
157 } ;
158 
159 #endif
virtual ~Server()
Destructor.
An interface used by ServerProtocol to send protocol replies.
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:173
ServerPeer(GNet::Server::PeerInfo peer_info, Server &server, const GAuth::Secrets &server_secrets, const Server::Config &server_config, unique_ptr< ServerProtocol::Text > ptext)
Constructor.
Definition: gsmtpserver.cpp:73
unique_ptr< ProtocolMessage > newProtocolMessage(ServerPeer &)
Called by GSmtp::ServerPeer to construct a ProtocolMessage.
Handles a connection from a remote SMTP client.
Definition: gsmtpserver.h:121
SMTP and message-store classes.
Definition: gadminserver.h:39
virtual void onSecure(const std::string &) override
Override from GNet::SocketProtocolSink.
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
virtual void onDelete(const std::string &reason) override
Override from GNet::ServerPeer.
Definition: gsmtpserver.cpp:89
Implements the SMTP server-side protocol.
Server(GNet::ExceptionHandler &eh, MessageStore &store, const GAuth::Secrets &client_secrets, const GAuth::Secrets &server_secrets, Config server_config, std::string forward_to, GSmtp::Client::Config client_config)
Constructor.
void report() const
Generates helpful diagnostics after construction.
Server * server()
Returns a pointer to the associated server object.
virtual void onData(const char *, size_t) override
Override from GNet::SocketProtocolSink.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
A structure containing GSmtp::Server configuration parameters.
Definition: gsmtpserver.h:55
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:43
A simple interface for a store of secrets used in authentication.
Definition: gsecrets.h:40
An SMTP server class.
Definition: gsmtpserver.h:49
A structure used in GNet::MultiServer::newPeer().
Definition: gmultiserver.h:100
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:55
A structure containing configuration parameters for ServerProtocol.
A class that does line buffering, supporting auto-detection of line endings and fixed-size extraction...
Definition: glinebuffer.h:53
G::Slot::Signal2< std::string, std::string > & eventSignal()
Returns a signal that indicates that something has happened.
A server that listens on more than one interface using a facade pattern to multiple GNet::Server inst...
Definition: gmultiserver.h:94
virtual void onSendComplete() override
Override from GNet::ServerPeer.
Definition: gsmtpserver.cpp:97
A structure used in GNet::Server::newPeer().
Definition: gserver.h:85