E-MailRelay

gpopserverprotocol.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 gpopserverprotocol.h
19///
20
21#ifndef G_POP_SERVER_PROTOCOL_H
22#define G_POP_SERVER_PROTOCOL_H
23
24#include "gdef.h"
25#include "gaddress.h"
26#include "gstatemachine.h"
27#include "gsaslserversecrets.h"
28#include "gpopstore.h"
29#include "gsaslserver.h"
30#include "gtimer.h"
31#include "gexception.h"
32
33 namespace GPop
34{
35 class ServerProtocol ;
36 class ServerProtocolText ;
37}
38
39//| \class GPop::ServerProtocol
40/// Implements the POP server-side protocol.
41///
42/// Uses the ServerProtocol::Sender as its "sideways"
43/// interface to talk back to the client.
44///
45/// \see RFC-1939
46///
47 class GPop::ServerProtocol
48{
49 public:
50 G_EXCEPTION( ProtocolDone , "pop protocol done" ) ;
51
52 class Sender /// An interface used by ServerProtocol to send protocol replies.
53 {
54 public:
55 virtual bool protocolSend( const std::string & s , std::size_t offset ) = 0 ;
56 virtual ~Sender() = default ;
57 } ;
58
59 class Text /// An interface used by ServerProtocol to provide response text strings.
60 {
61 public:
62 virtual std::string greeting() const = 0 ;
63 virtual std::string quit() const = 0 ;
64 virtual std::string capa() const = 0 ;
65 virtual std::string user( const std::string & id ) const = 0 ;
66 virtual ~Text() = default ;
67 } ;
68
69 struct Config /// A structure containing configuration parameters for ServerProtocol, currently empty.
70 {
71 Config() = default ;
72 } ;
73
74 class Security /// An interface used by ServerProtocol to enable TLS.
75 {
76 public:
77 virtual bool securityEnabled() const = 0 ;
78 virtual void securityStart() = 0 ;
79 virtual ~Security() = default ;
80 } ;
81
82 ServerProtocol( Sender & sender , Security & security , Store & store ,
83 const GAuth::SaslServerSecrets & server_secrets , const std::string & sasl_server_config ,
84 const Text & text , const GNet::Address & peer_address , const Config & config ) ;
85 ///< Constructor.
86 ///<
87 ///< The Sender interface is used to send protocol
88 ///< replies back to the client.
89 ///<
90 ///< The Text interface is used to get informational text
91 ///< for returning to the client.
92 ///<
93 ///< All references are kept.
94
95 void init() ;
96 ///< Starts the protocol.
97
98 void apply( const std::string & line ) ;
99 ///< Called on receipt of a string from the client.
100 ///< The string is expected to be CR-LF terminated.
101 ///< Throws ProtocolDone if done.
102
103 void resume() ;
104 ///< Called when the Sender can send again. The Sender returns
105 ///< false from protocolSend() when blocked, and calls
106 ///< resume() when unblocked.
107
108 void secure() ;
109 ///< Called when the server connection becomes secure.
110
111 private:
112 enum class Event
113 {
114 eApop ,
115 eAuth ,
116 eAuthData ,
117 eAuthComplete ,
118 eCapa ,
119 eDele ,
120 eList ,
121 eNoop ,
122 ePass ,
123 eQuit ,
124 eRetr ,
125 eRset ,
126 eSent ,
127 eStat ,
128 eTop ,
129 eUidl ,
130 eUser ,
131 eStls ,
132 eSecure ,
133 eUnknown
134 } ;
135 enum class State
136 {
137 sStart ,
138 sEnd ,
139 sActive ,
140 sData ,
141 sAuth ,
142 s_Any ,
143 s_Same
144 } ;
145 using EventData = const std::string & ;
146 using Fsm = G::StateMachine<ServerProtocol,State,Event,EventData> ;
147
148 public:
149 ~ServerProtocol() = default ;
150 ServerProtocol( const ServerProtocol & ) = delete ;
151 ServerProtocol( ServerProtocol && ) = delete ;
152 void operator=( const ServerProtocol & ) = delete ;
153 void operator=( ServerProtocol && ) = delete ;
154
155 private:
156 void doQuit( const std::string & line , bool & ) ;
157 void doQuitEarly( const std::string & line , bool & ) ;
158 void doStat( const std::string & line , bool & ) ;
159 void doList( const std::string & line , bool & ) ;
160 void doRetr( const std::string & line , bool & ) ;
161 void doDele( const std::string & line , bool & ) ;
162 void doRset( const std::string & line , bool & ) ;
163 void doUser( const std::string & line , bool & ) ;
164 void doPass( const std::string & line , bool & ) ;
165 void doNoop( const std::string & line , bool & ) ;
166 void doNothing( const std::string & line , bool & ) ;
167 void doApop( const std::string & line , bool & ) ;
168 void doTop( const std::string & line , bool & ) ;
169 void doCapa( const std::string & line , bool & ) ;
170 void doStls( const std::string & line , bool & ) ;
171 void doAuth( const std::string & line , bool & ) ;
172 void doAuthData( const std::string & line , bool & ) ;
173 void doAuthComplete( const std::string & line , bool & ) ;
174 void doUidl( const std::string & line , bool & ) ;
175 void sendInit() ;
176 void sendError() ;
177 void sendError( const std::string & ) ;
178 void sendOk() ;
179 static const std::string & crlf() ;
180 Event commandEvent( const std::string & ) const ;
181 int commandNumber( const std::string & , int , std::size_t index = 1U ) const ;
182 void sendList( const std::string & , bool ) ;
183 std::string commandWord( const std::string & ) const ;
184 std::string commandParameter( const std::string & , std::size_t index = 1U ) const ;
185 std::string commandPart( const std::string & , std::size_t index ) const ;
186 void sendContent() ;
187 bool sendContentLine( std::string & , bool & ) ;
188 void sendLine( std::string ) ;
189 void sendLines( std::ostringstream & ) ;
190 void lockStore() ;
191 std::string mechanisms() const ;
192 bool mechanismsIncludePlain() const ;
193
194 private:
195 const Text & m_text ;
196 Sender & m_sender ;
197 Security & m_security ;
198 Store & m_store ;
199 StoreLock m_store_lock ;
200 std::unique_ptr<GAuth::SaslServer> m_sasl_server ;
201 GNet::Address m_peer_address ;
202 Fsm m_fsm ;
203 std::string m_user ;
204 std::unique_ptr<std::istream> m_content ;
205 long m_body_limit ;
206 bool m_in_body ;
207 bool m_secure ;
208 bool m_sasl_server_init_apop ;
209} ;
210
211//| \class GPop::ServerProtocolText
212/// A default implementation for the ServerProtocol::Text interface.
213///
214 class GPop::ServerProtocolText : public ServerProtocol::Text
215{
216 public:
217 explicit ServerProtocolText( const GNet::Address & peer ) ;
218 ///< Constructor.
219
220 public:
221 ~ServerProtocolText() override = default ;
222 ServerProtocolText( const ServerProtocolText & ) = delete ;
223 ServerProtocolText( ServerProtocolText && ) = delete ;
224 void operator=( const ServerProtocolText & ) = delete ;
225 void operator=( ServerProtocolText && ) = delete ;
226
227 private: // overrides
228 std::string greeting() const override ; // Override from GPop::ServerProtocol::Text.
229 std::string quit() const override ; // Override from GPop::ServerProtocol::Text.
230 std::string capa() const override ; // Override from GPop::ServerProtocol::Text.
231 std::string user( const std::string & id ) const override ; // Override from GPop::ServerProtocol::Text.
232} ;
233
234#endif
GAuth::SaslServerSecrets
An interface used by GAuth::SaslServer to obtain authentication secrets.
Definition: gsaslserversecrets.h:40
GNet::Address
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:53
GPop::ServerProtocolText
A default implementation for the ServerProtocol::Text interface.
Definition: gpopserverprotocol.h:215
GPop::ServerProtocolText::ServerProtocolText
ServerProtocolText(const GNet::Address &peer)
Constructor.
Definition: gpopserverprotocol.cpp:654
GPop::ServerProtocol::Security
An interface used by ServerProtocol to enable TLS.
Definition: gpopserverprotocol.h:75
GPop::ServerProtocol::Sender
An interface used by ServerProtocol to send protocol replies.
Definition: gpopserverprotocol.h:53
GPop::ServerProtocol::Text
An interface used by ServerProtocol to provide response text strings.
Definition: gpopserverprotocol.h:60
GPop::ServerProtocol
Implements the POP server-side protocol.
Definition: gpopserverprotocol.h:48
GPop::ServerProtocol::resume
void resume()
Called when the Sender can send again.
Definition: gpopserverprotocol.cpp:161
GPop::ServerProtocol::secure
void secure()
Called when the server connection becomes secure.
Definition: gpopserverprotocol.cpp:502
GPop::ServerProtocol::init
void init()
Starts the protocol.
Definition: gpopserverprotocol.cpp:74
GPop::ServerProtocol::apply
void apply(const std::string &line)
Called on receipt of a string from the client.
Definition: gpopserverprotocol.cpp:113
GPop::ServerProtocol::ServerProtocol
ServerProtocol(Sender &sender, Security &security, Store &store, const GAuth::SaslServerSecrets &server_secrets, const std::string &sasl_server_config, const Text &text, const GNet::Address &peer_address, const Config &config)
Constructor.
Definition: gpopserverprotocol.cpp:32
GPop::Store
A message store.
Definition: gpopstore.h:45
G::StateMachine< ServerProtocol, State, Event, EventData >
gaddress.h
gexception.h
gpopstore.h
gsaslserver.h
gsaslserversecrets.h
gstatemachine.h
gtimer.h
GPop
POP3 classes.
Definition: gpopserver.h:37
GPop::ServerProtocol::Config
A structure containing configuration parameters for ServerProtocol, currently empty.
Definition: gpopserverprotocol.h:70