gserverprotocol.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
20 
21 #ifndef G_SMTP_SERVER_PROTOCOL_H
22 #define G_SMTP_SERVER_PROTOCOL_H
23 
24 #include "gdef.h"
25 #include "gsmtp.h"
26 #include "gprotocolmessage.h"
27 #include "gaddress.h"
28 #include "gverifier.h"
29 #include "gverifierstatus.h"
30 #include "gsaslserver.h"
31 #include "gsecrets.h"
32 #include "gstatemachine.h"
33 #include "gtimer.h"
34 #include "gexception.h"
35 #include <map>
36 #include <utility>
37 #include <memory>
38 
40 namespace GSmtp
41 {
42  class ServerProtocol ;
43  class ServerProtocolText ;
44 }
45 
59 {
60 public:
61  G_EXCEPTION( ProtocolDone , "smtp protocol done" ) ;
63  class Sender
64  {
65  public: virtual void protocolSend( const std::string & s , bool go_secure ) = 0 ;
66  public: virtual ~Sender() ;
67  private: void operator=( const Sender & ) ; // not implemented
68  } ;
70  class Text
71  {
72  public: virtual std::string greeting() const = 0 ;
73  public: virtual std::string hello( const std::string & smtp_peer_name ) const = 0 ;
74  public: virtual std::string received( const std::string & smtp_peer_name ) const = 0 ;
75  public: virtual ~Text() ;
76  private: void operator=( const Text & ) ; // not implemented
77  } ;
79  struct Config
80  {
81  bool with_vrfy ;
82  unsigned int preprocessor_timeout ;
84  Config( bool , unsigned int ) ;
85  } ;
86 
87  ServerProtocol( Sender & sender , Verifier & verifier , ProtocolMessage & pmessage ,
88  const GAuth::Secrets & secrets , Text & text , GNet::Address peer_address ,
89  const std::string & peer_socket_name , Config config ) ;
105 
106  void init() ;
108 
109  virtual ~ServerProtocol() ;
111 
112  void apply( const std::string & line ) ;
116 
117  void secure( const std::string & certificate ) ;
120 
121 protected:
122  virtual void onTimeout() ;
124 
125  virtual void onTimeoutException( std::exception & ) ;
127 
128 private:
129  enum Event
130  {
131  eQuit ,
132  eHelo ,
133  eEhlo ,
134  eRset ,
135  eNoop ,
136  eExpn ,
137  eData ,
138  eRcpt ,
139  eMail ,
140  eStartTls ,
141  eSecure ,
142  eVrfy ,
143  eVrfyReply ,
144  eHelp ,
145  eAuth ,
146  eAuthData ,
147  eContent ,
148  eEot ,
149  eDone ,
150  eTimeout ,
151  eUnknown
152  } ;
153  enum State
154  {
155  sStart ,
156  sEnd ,
157  sIdle ,
158  sGotMail ,
159  sGotRcpt ,
160  sVrfyStart ,
161  sVrfyIdle ,
162  sVrfyGotMail ,
163  sVrfyGotRcpt ,
164  sVrfyTo1 ,
165  sVrfyTo2 ,
166  sData ,
167  sProcessing ,
168  sAuth ,
169  sStartingTls ,
170  sDiscarding ,
171  s_Any ,
172  s_Same
173  } ;
175 
176 private:
177  ServerProtocol( const ServerProtocol & ) ; // not implemented
178  void operator=( const ServerProtocol & ) ; // not implemented
179  void send( std::string , bool = false ) ;
180  Event commandEvent( const std::string & ) const ;
181  std::string commandWord( const std::string & line ) const ;
182  std::string commandLine( const std::string & line ) const ;
183  static const std::string & crlf() ;
184  bool sensitive() const ;
185  void reset() ;
186  void badClientEvent() ;
187  void processDone( bool , unsigned long , std::string ) ; // ProtocolMessage::doneSignal()
188  void prepareDone( bool , bool , std::string ) ;
189  bool isEndOfText( const std::string & ) const ;
190  bool isEscaped( const std::string & ) const ;
191  void doNoop( const std::string & , bool & ) ;
192  void doNothing( const std::string & , bool & ) ;
193  void doDiscarded( const std::string & , bool & ) ;
194  void doDiscard( const std::string & , bool & ) ;
195  void doHelp( const std::string & line , bool & ) ;
196  void doExpn( const std::string & line , bool & ) ;
197  void doQuit( const std::string & , bool & ) ;
198  void doEhlo( const std::string & , bool & ) ;
199  void doHelo( const std::string & , bool & ) ;
200  void doAuth( const std::string & , bool & ) ;
201  void doAuthData( const std::string & , bool & ) ;
202  void doMail( const std::string & , bool & ) ;
203  void doRcpt( const std::string & , bool & ) ;
204  void doUnknown( const std::string & , bool & ) ;
205  void doRset( const std::string & , bool & ) ;
206  void doData( const std::string & , bool & ) ;
207  void doContent( const std::string & , bool & ) ;
208  void doComplete( const std::string & , bool & ) ;
209  void doEot( const std::string & , bool & ) ;
210  void doVrfy( const std::string & , bool & ) ;
211  void doVrfyReply( const std::string & line , bool & ) ;
212  void doVrfyToReply( const std::string & line , bool & ) ;
213  void doNoRecipients( const std::string & , bool & ) ;
214  void doStartTls( const std::string & , bool & ) ;
215  void doSecure( const std::string & , bool & ) ;
216  void verifyDone( std::string , VerifierStatus status ) ;
217  void sendBadFrom( std::string ) ;
218  void sendTooBig( bool disconnecting = false ) ;
219  void sendChallenge( const std::string & ) ;
220  void sendBadTo( const std::string & , bool ) ;
221  void sendOutOfSequence( const std::string & ) ;
222  void sendGreeting( const std::string & ) ;
223  void sendClosing() ;
224  void sendUnrecognised( const std::string & ) ;
225  void sendNotImplemented() ;
226  void sendHeloReply() ;
227  void sendEhloReply() ;
228  void sendRsetReply() ;
229  void sendMailReply() ;
230  void sendRcptReply() ;
231  void sendDataReply() ;
232  void sendCompletionReply( bool ok , const std::string & ) ;
233  void sendAuthRequired() ;
234  void sendNoRecipients() ;
235  void sendMissingParameter() ;
236  void sendVerified( const std::string & ) ;
237  void sendNotVerified( const std::string & , bool ) ;
238  void sendWillAccept( const std::string & ) ;
239  void sendAuthDone( bool ok ) ;
240  void sendOk() ;
241  std::pair<std::string,std::string> parse( const std::string & ) const ;
242  std::pair<std::string,std::string> parseFrom( const std::string & ) const ;
243  std::pair<std::string,std::string> parseTo( const std::string & ) const ;
244  std::string parseToParameter( const std::string & ) const ;
245  std::string parsePeerName( const std::string & ) const ;
246  void verify( const std::string & , const std::string & ) ;
247 
248 private:
249  Sender & m_sender ;
250  Verifier & m_verifier ;
251  ProtocolMessage & m_pmessage ;
252  Text & m_text ;
253  GNet::Address m_peer_address ;
254  std::string m_peer_socket_name ;
255  Fsm m_fsm ;
256  std::string m_smtp_peer_name ;
257  bool m_authenticated ;
258  bool m_secure ;
259  std::string m_certificate ;
260  std::auto_ptr<GAuth::SaslServer> m_sasl ;
261  bool m_with_vrfy ;
262  bool m_with_ssl ;
263  std::string m_buffer ;
264  unsigned int m_preprocessor_timeout ;
265  unsigned int m_bad_client_count ;
266  unsigned int m_bad_client_limit ;
267  bool m_disconnect_on_overflow ;
268 } ;
269 
275 {
276 public:
277  ServerProtocolText( const std::string & ident , const std::string & thishost ,
278  const GNet::Address & peer_address , const std::string & peer_socket_name ) ;
280 
281  virtual std::string greeting() const ;
283 
284  virtual std::string hello( const std::string & smtp_peer_name_from_helo ) const ;
286 
287  virtual std::string received( const std::string & smtp_peer_name_from_helo ) const ;
289 
290  static std::string receivedLine( const std::string & smtp_peer_name_from_helo ,
291  const std::string & peer_address , const std::string & peer_socket_name ,
292  const std::string & thishost ) ;
294 
295 private:
296  static std::string shortened( std::string , std::string ) ;
297 
298 private:
299  std::string m_ident ;
300  std::string m_thishost ;
301  GNet::Address m_peer_address ;
302  std::string m_peer_socket_name ;
303 } ;
304 
305 #endif
void secure(const std::string &certificate)
To be called when the transport protocol goes into secure mode.
ServerProtocolText(const std::string &ident, const std::string &thishost, const GNet::Address &peer_address, const std::string &peer_socket_name)
Constructor.
An interface used by ServerProtocol to send protocol replies.
virtual void onTimeout()
Final override from GNet::AbstractTimer.
SMTP and message-store classes.
virtual void onTimeoutException(std::exception &)
Final override from GNet::AbstractTimer.
An interface used by ServerProtocol to provide response text strings.
virtual std::string received(const std::string &smtp_peer_name_from_helo) const
Final override from GSmtp::ServerProtocol::Text.
ServerProtocol(Sender &sender, Verifier &verifier, ProtocolMessage &pmessage, const GAuth::Secrets &secrets, Text &text, GNet::Address peer_address, const std::string &peer_socket_name, Config config)
Constructor.
virtual std::string greeting() const =0
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
void apply(const std::string &line)
Called on receipt of a string from the client.
virtual void protocolSend(const std::string &s, bool go_secure)=0
Implements the SMTP server-side protocol.
virtual std::string received(const std::string &smtp_peer_name) const =0
An asynchronous interface that verifies recipient 'to' addresses.
Definition: gverifier.h:44
void init()
Starts the protocol. Use only once after construction.
A simple interface to a store of secrets as used in authentication.
Definition: gsecrets.h:44
virtual ~ServerProtocol()
Destructor.
A default implementation for the ServerProtocol::Text interface.
A structure containing configuration parameters for ServerProtocol.
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
An interface used by the ServerProtocol class to assemble and process an incoming message...
virtual std::string hello(const std::string &smtp_peer_name) const =0
virtual std::string hello(const std::string &smtp_peer_name_from_helo) const
Final override from GSmtp::ServerProtocol::Text.
static std::string receivedLine(const std::string &smtp_peer_name_from_helo, const std::string &peer_address, const std::string &peer_socket_name, const std::string &thishost)
Returns a standard "Received:" line.
virtual std::string greeting() const
Final override from GSmtp::ServerProtocol::Text.
A timer base class that calls a pure virtual method on expiry.
Definition: gtimer.h:41