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
41namespace 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///
57{
58public:
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
74private: // 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
80public:
81 AdminServerPeer( const AdminServerPeer & ) = delete ;
82 AdminServerPeer( AdminServerPeer && ) = delete ;
83 AdminServerPeer & operator=( const AdminServerPeer & ) = delete ;
84 AdminServerPeer & operator=( AdminServerPeer && ) = delete ;
85
86private:
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
104private:
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 ;
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///
122{
123public:
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
156 const GAuth::SaslClientSecrets & client_secrets , const G::StringArray & interfaces ,
157 const Config & config ) ;
158 ///< Constructor.
159
161 ///< Destructor.
162
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
171 ///< Returns a reference to the message store, as
172 ///< passed in to the constructor.
173
175 ///< Returns a reference to the filter factory, as
176 ///< passed in to the constructor.
177
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
194public:
195 AdminServer( const AdminServer & ) = delete ;
196 AdminServer( AdminServer && ) = delete ;
197 AdminServer & operator=( const AdminServer & ) = delete ;
198 AdminServer & operator=( AdminServer && ) = delete ;
199
200private:
201 std::unique_ptr<AdminServerImp> m_imp ;
202} ;
203
204inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_port( unsigned int n ) noexcept { port = n ; return *this ; }
205inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_with_terminate( bool b ) noexcept { with_terminate = b ; return *this ; }
206inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_allow_remote( bool b ) noexcept { allow_remote = b ; return *this ; }
207inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_remote_address( const std::string & s ) { remote_address = s ; return *this ; }
208inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_info_commands( const G::StringMap & m ) { info_commands = m ; return *this ; }
209inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_smtp_client_config( const Client::Config & c ) { smtp_client_config = c ; return *this ; }
210inline GSmtp::AdminServer::Config & GSmtp::AdminServer::Config::set_net_server_config( const GNet::Server::Config & c ) { net_server_config = c ; return *this ; }
211inline 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
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret.
The EventStateUnbound class is used as a device to force factory methods to plumb-in an ExceptionSour...
Definition: geventstate.h:231
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
A move-only structure used in GNet::Server::newPeer() and containing the new socket.
Definition: gserver.h:142
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserverpeer.h:59
A derivation of ServerPeer for the administration interface.
Definition: gadminserver.h:57
bool notifying() const
Returns true if the remote user has asked for notifications.
AdminServerPeer(GNet::EventStateUnbound, GNet::ServerPeerInfo &&, AdminServerImp &, const std::string &remote, const G::StringMap &info_commands, bool with_terminate)
Constructor.
~AdminServerPeer() override
Destructor.
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.
A server class which implements the emailrelay administration interface.
Definition: gadminserver.h:122
static bool enabled()
Returns true if the server is enabled.
~AdminServer()
Destructor.
G::Slot::Signal< Command, unsigned int > & commandSignal()
Returns a reference to a signal that is emit()ted when the remote user makes a request.
void report(const std::string &group={}) const
Generates helpful diagnostics.
AdminServer(GNet::EventState, GStore::MessageStore &store, FilterFactoryBase &, const GAuth::SaslClientSecrets &client_secrets, const G::StringArray &interfaces, const Config &config)
Constructor.
void emitCommand(Command, unsigned int)
Emits an asynchronous event on the commandSignal().
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.
GStore::MessageStore & store()
Returns a reference to the message store, as passed in to the constructor.
FilterFactoryBase & ff()
Returns a reference to the filter factory, as passed in to the constructor.
const GAuth::SaslClientSecrets & clientSecrets() const
Returns a reference to the client secrets object, as passed in to the constructor.
bool notifying() const
Returns true if the remote user has asked for notifications.
A factory interface for making GSmtp::Filter message processors.
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
SMTP classes.
Definition: gadminserver.h:42
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstringmap.h:30
A configuration structure for GNet::ServerPeer.
Definition: gserverpeer.h:64
A configuration structure for GNet::Server.
Definition: gserver.h:56
A configuration structure for GSmtp::AdminServer.
Definition: gadminserver.h:126
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:67
A slot holder, with connect() and emit() methods.
Definition: gslot.h:184