E-MailRelay

gadminserver.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 gadminserver.h
19///
20
21#ifndef G_SMTP_ADMIN_H
22#define G_SMTP_ADMIN_H
23
24#include "gdef.h"
25#include "gmultiserver.h"
26#include "gtimer.h"
27#include "gstr.h"
28#include "glinebuffer.h"
29#include "gsmtpserverprotocol.h"
30#include "gclientptr.h"
31#include "gsmtpforward.h"
32#include "gstringview.h"
33#include "gstringarray.h"
34#include "gstringmap.h"
35#include <string>
36#include <list>
37#include <sstream>
38#include <utility>
39#include <memory>
40
41 namespace GSmtp
42{
43 class AdminServer ;
44 class AdminServerImp ;
45 class AdminServerPeer ;
46}
47
48//| \class GSmtp::AdminServerPeer
49/// A derivation of ServerPeer for the administration interface.
50///
51/// The AdminServerPeer instantiates its own Smtp::Client in order
52/// to implement the "flush" command.
53///
54/// \see GSmtp::AdminServer
55///
56 class GSmtp::AdminServerPeer : public GNet::ServerPeer
57{
58 public:
59 AdminServerPeer( GNet::EventStateUnbound , GNet::ServerPeerInfo && , AdminServerImp & ,
60 const std::string & remote , const G::StringMap & info_commands ,
61 bool with_terminate ) ;
62 ///< Constructor.
63
64 ~AdminServerPeer() override ;
65 ///< Destructor.
66
67 bool notifying() const ;
68 ///< Returns true if the remote user has asked for notifications.
69
70 void notify( const std::string & s0 , const std::string & s1 , const std::string & s2 , const std::string & s3 ) ;
71 ///< Called when something happens which the remote admin
72 ///< user might be interested in.
73
74 private: // overrides
75 void onSendComplete() override ; // GNet::BufferedServerPeer
76 bool onReceive( const char * , std::size_t , std::size_t , std::size_t , char ) override ; // GNet::BufferedServerPeer
77 void onDelete( const std::string & ) override ; // GNet::ServerPeer
78 void onSecure( const std::string & , const std::string & , const std::string & ) override ; // GNet::SocketProtocolSink
79
80 public:
81 AdminServerPeer( const AdminServerPeer & ) = delete ;
82 AdminServerPeer( AdminServerPeer && ) = delete ;
83 AdminServerPeer & operator=( const AdminServerPeer & ) = delete ;
84 AdminServerPeer & operator=( AdminServerPeer && ) = delete ;
85
86 private:
87 void clientDone( const std::string & ) ;
88 static bool is( std::string_view , std::string_view ) ;
89 static std::pair<bool,std::string> find( std::string_view , const G::StringMap & map ) ;
90 void flush() ;
91 void forward() ;
92 void help() ;
93 void status() ;
94 void sendMessageIds( const std::vector<GStore::MessageId> & ) ;
95 void sendLine( std::string && ) ;
96 void sendLineCopy( std::string ) ;
97 void warranty() ;
98 void version() ;
99 void copyright() ;
100 std::string eol() const ;
101 void unfailAll() ;
102 void sendImp( const std::string & ) ;
103
104 private:
105 GNet::EventState m_es ;
106 AdminServerImp & m_server_imp ;
107 std::string m_prompt ;
108 bool m_blocked {false} ;
109 std::string m_remote_address ;
110 GNet::ClientPtr<GSmtp::Forward> m_client_ptr ;
111 bool m_notifying {false} ;
112 G::StringMap m_info_commands ;
113 bool m_with_terminate ;
114 unsigned int m_error_limit {30U} ;
115 unsigned int m_error_count {0U} ;
116} ;
117
118//| \class GSmtp::AdminServer
119/// A server class which implements the emailrelay administration interface.
120///
121 class GSmtp::AdminServer
122{
123 public:
124 G_EXCEPTION( NotImplemented , tx("admin server not implemented") )
125 struct Config /// A configuration structure for GSmtp::AdminServer.
126 {
127 unsigned int port {10026U} ;
128 bool with_terminate {false} ;
129 bool allow_remote {false} ;
130 std::string remote_address ;
131 G::StringMap info_commands ;
132 Client::Config smtp_client_config ;
133 GNet::Server::Config net_server_config ;
134 GNet::ServerPeer::Config net_server_peer_config ;
135
136 Config & set_port( unsigned int ) noexcept ;
137 Config & set_with_terminate( bool = true ) noexcept ;
138 Config & set_allow_remote( bool = true ) noexcept ;
139 Config & set_remote_address( const std::string & ) ;
140 Config & set_info_commands( const G::StringMap & ) ;
141 Config & set_smtp_client_config( const Client::Config & ) ;
142 Config & set_net_server_config( const GNet::Server::Config & ) ;
143 Config & set_net_server_peer_config( const GNet::ServerPeer::Config & ) ;
144 } ;
145 enum class Command
146 {
147 forward ,
148 dnsbl ,
149 smtp_enable
150 } ;
151
152 static bool enabled() ;
153 ///< Returns true if the server is enabled.
154
155 AdminServer( GNet::EventState , GStore::MessageStore & store , FilterFactoryBase & ,
156 const GAuth::SaslClientSecrets & client_secrets , const G::StringArray & interfaces ,
157 const Config & config ) ;
158 ///< Constructor.
159
160 ~AdminServer() ;
161 ///< Destructor.
162
163 G::Slot::Signal<Command,unsigned int> & commandSignal() ;
164 ///< Returns a reference to a signal that is emit()ted when the
165 ///< remote user makes a request.
166
167 void report( const std::string & group = {} ) const ;
168 ///< Generates helpful diagnostics.
169
170 GStore::MessageStore & store() ;
171 ///< Returns a reference to the message store, as
172 ///< passed in to the constructor.
173
174 FilterFactoryBase & ff() ;
175 ///< Returns a reference to the filter factory, as
176 ///< passed in to the constructor.
177
178 const GAuth::SaslClientSecrets & clientSecrets() const ;
179 ///< Returns a reference to the client secrets object, as passed
180 ///< in to the constructor. This is a client-side secrets file,
181 ///< used to authenticate ourselves with a remote server.
182
183 void emitCommand( Command , unsigned int ) ;
184 ///< Emits an asynchronous event on the commandSignal().
185 ///< Used by AdminServerPeer.
186
187 bool notifying() const ;
188 ///< Returns true if the remote user has asked for notifications.
189
190 void notify( const std::string & s0 , const std::string & s1 , const std::string & s2 , const std::string & s3 ) ;
191 ///< Called when something happens which the admin
192 ///< users might be interested in.
193
194 public:
195 AdminServer( const AdminServer & ) = delete ;
196 AdminServer( AdminServer && ) = delete ;
197 AdminServer & operator=( const AdminServer & ) = delete ;
198 AdminServer & operator=( AdminServer && ) = delete ;
199
200 private:
201 std::unique_ptr<AdminServerImp> m_imp ;
202} ;
203
204 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_port( unsigned int n ) noexcept { port = n ; return *this ; }
205 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_with_terminate( bool b ) noexcept { with_terminate = b ; return *this ; }
206 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_allow_remote( bool b ) noexcept { allow_remote = b ; return *this ; }
207 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_remote_address( const std::string & s ) { remote_address = s ; return *this ; }
208 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_info_commands( const G::StringMap & m ) { info_commands = m ; return *this ; }
209 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_smtp_client_config( const Client::Config & c ) { smtp_client_config = c ; return *this ; }
210 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_net_server_config( const GNet::Server::Config & c ) { net_server_config = c ; return *this ; }
211 inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_net_server_peer_config( const GNet::ServerPeer::Config & c ) { net_server_peer_config = c ; return *this ; }
212
213#endif
GAuth::SaslClientSecrets
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret.
Definition: gsaslclientsecrets.h:39
GNet::ClientPtr< GSmtp::Forward >
GNet::EventStateUnbound
The EventStateUnbound class is used as a device to force factory methods to plumb-in an ExceptionSour...
Definition: geventstate.h:231
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::ServerPeerInfo
A move-only structure used in GNet::Server::newPeer() and containing the new socket.
Definition: gserver.h:142
GNet::ServerPeer
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserverpeer.h:59
GSmtp::AdminServerPeer
A derivation of ServerPeer for the administration interface.
Definition: gadminserver.h:57
GSmtp::AdminServerPeer::notifying
bool notifying() const
Returns true if the remote user has asked for notifications.
Definition: gadminserver_enabled.cpp:382
GSmtp::AdminServerPeer::AdminServerPeer
AdminServerPeer(GNet::EventStateUnbound, GNet::ServerPeerInfo &&, AdminServerImp &, const std::string &remote, const G::StringMap &info_commands, bool with_terminate)
Constructor.
Definition: gadminserver_enabled.cpp:81
GSmtp::AdminServerPeer::~AdminServerPeer
~AdminServerPeer() override
Destructor.
Definition: gadminserver_enabled.cpp:98
GSmtp::AdminServerPeer::notify
void notify(const std::string &s0, const std::string &s1, const std::string &s2, const std::string &s3)
Called when something happens which the remote admin user might be interested in.
Definition: gadminserver_enabled.cpp:318
GSmtp::AdminServer
A server class which implements the emailrelay administration interface.
Definition: gadminserver.h:122
GSmtp::AdminServer::enabled
static bool enabled()
Returns true if the server is enabled.
Definition: gadminserver_disabled.cpp:28
GSmtp::AdminServer::~AdminServer
~AdminServer()
Destructor.
GSmtp::AdminServer::commandSignal
G::Slot::Signal< Command, unsigned int > & commandSignal()
Returns a reference to a signal that is emit()ted when the remote user makes a request.
Definition: gadminserver_disabled.cpp:46
GSmtp::AdminServer::report
void report(const std::string &group={}) const
Generates helpful diagnostics.
Definition: gadminserver_disabled.cpp:51
GSmtp::AdminServer::AdminServer
AdminServer(GNet::EventState, GStore::MessageStore &store, FilterFactoryBase &, const GAuth::SaslClientSecrets &client_secrets, const G::StringArray &interfaces, const Config &config)
Constructor.
Definition: gadminserver_disabled.cpp:33
GSmtp::AdminServer::emitCommand
void emitCommand(Command, unsigned int)
Emits an asynchronous event on the commandSignal().
Definition: gadminserver_disabled.cpp:42
GSmtp::AdminServer::notify
void notify(const std::string &s0, const std::string &s1, const std::string &s2, const std::string &s3)
Called when something happens which the admin users might be interested in.
Definition: gadminserver_disabled.cpp:55
GSmtp::AdminServer::store
GStore::MessageStore & store()
Returns a reference to the message store, as passed in to the constructor.
Definition: gadminserver_disabled.cpp:59
GSmtp::AdminServer::ff
FilterFactoryBase & ff()
Returns a reference to the filter factory, as passed in to the constructor.
Definition: gadminserver_disabled.cpp:64
GSmtp::AdminServer::clientSecrets
const GAuth::SaslClientSecrets & clientSecrets() const
Returns a reference to the client secrets object, as passed in to the constructor.
Definition: gadminserver_disabled.cpp:69
GSmtp::AdminServer::notifying
bool notifying() const
Returns true if the remote user has asked for notifications.
Definition: gadminserver_disabled.cpp:74
GSmtp::FilterFactoryBase
A factory interface for making GSmtp::Filter message processors.
Definition: gfilterfactorybase.h:43
GStore::MessageStore
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
gclientptr.h
glinebuffer.h
gmultiserver.h
gsmtpforward.h
gsmtpserverprotocol.h
gstr.h
gstringarray.h
gstringmap.h
gstringview.h
gtimer.h
GSmtp
SMTP classes.
Definition: gadminserver.h:42
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
G::StringMap
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstringmap.h:30
GNet::ServerPeer::Config
A configuration structure for GNet::ServerPeer.
Definition: gserverpeer.h:64
GNet::Server::Config
A configuration structure for GNet::Server.
Definition: gserver.h:56
GSmtp::AdminServer::Config
A configuration structure for GSmtp::AdminServer.
Definition: gadminserver.h:126
GSmtp::Client::Config
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:67
G::Slot::Signal
A slot holder, with connect() and emit() methods.
Definition: gslot.h:184