35 AdminServer & server ,
const std::string & remote_address ,
37 bool with_terminate ) :
38 GNet::BufferedServerPeer(peer_info,
GNet::LineBufferConfig::autodetect()) ,
40 m_prompt(
"E-MailRelay> ") ,
42 m_remote_address(remote_address) ,
44 m_info_commands(info_commands) ,
45 m_config_commands(config_commands) ,
46 m_with_terminate(with_terminate)
48 G_LOG_S(
"GSmtp::AdminServerPeer: admin connection from " << peer_info.m_address.
displayString() ) ;
49 m_client.doneSignal().connect(
G::Slot::slot(*
this,&AdminServerPeer::clientDone) ) ;
59 m_server.unregister(
this ) ;
60 m_client.doneSignal().disconnect() ;
63 void GSmtp::AdminServerPeer::clientDone( std::string s )
69 sendLine( std::string(
"error: ") + s ) ;
74 G_LOG_S(
"GSmtp::AdminServerPeer: admin connection closed: " << reason << (reason.empty()?
"":
": ")
75 << peerAddress().second.displayString() ) ;
84 std::string line( line_data , line_size ) ;
85 if( is(line,
"flush") )
89 else if( is(line,
"help") )
93 else if( is(line,
"status") )
97 else if( is(line,
"notify") )
101 else if( is(line,
"list") )
103 sendList( spooled() ) ;
105 else if( is(line,
"failures") )
107 sendList( failures() ) ;
109 else if( is(line,
"unfail-all") )
114 else if( is(line,
"pid") )
116 sendLine( G::Process::Id().str() ) ;
118 else if( is(line,
"quit") )
123 else if( is(line,
"terminate") && m_with_terminate )
128 else if( is(line,
"info") && !m_info_commands.empty() )
130 std::string arg = argument( line ) ;
131 if( arg.empty() || !find(arg,m_info_commands).first )
134 sendLine( find(arg,m_info_commands).second ) ;
136 else if( is(line,
"config") && !m_config_commands.empty() )
138 std::string arg = argument( line ) ;
140 sendLine(
G::Str::join( eol() , m_config_commands ,
"=[" ,
"]" ) ) ;
141 else if( !find(arg,m_config_commands).first )
144 sendLine( find(arg,m_config_commands).second ) ;
146 else if( line.find_first_not_of(
" \r\n\t") != std::string::npos )
148 sendLine(
"error: unrecognised command" ) ;
157 std::string GSmtp::AdminServerPeer::eol()
const
159 return lineBufferEndOfLine().empty() ? std::string(
"\r\n") : lineBufferEndOfLine() ;
162 bool GSmtp::AdminServerPeer::is(
const std::string & line_in ,
const std::string & key )
169 std::string GSmtp::AdminServerPeer::argument(
const std::string & line_in )
173 return parts.size() > 1U ? parts.at(1U) : std::string() ;
176 std::pair<bool,std::string> GSmtp::AdminServerPeer::find(
const std::string & line ,
const G::StringMap & map )
178 for( G::StringMap::const_iterator p = map.begin() ; p != map.end() ; ++p )
180 if( is(line,(*p).first) )
181 return std::make_pair(
true,(*p).second) ;
183 return std::make_pair(
false,std::string()) ;
186 void GSmtp::AdminServerPeer::help()
188 std::set<std::string> commands ;
189 commands.insert(
"flush" ) ;
190 commands.insert(
"help" ) ;
191 commands.insert(
"status" ) ;
192 commands.insert(
"list" ) ;
193 commands.insert(
"failures" ) ;
194 commands.insert(
"unfail-all" ) ;
195 commands.insert(
"notify" ) ;
196 commands.insert(
"pid" ) ;
197 commands.insert(
"quit" ) ;
198 if( !m_info_commands.empty() ) commands.insert(
"info" ) ;
199 if( !m_config_commands.empty() ) commands.insert(
"config" ) ;
200 if( m_with_terminate ) commands.insert(
"terminate" ) ;
201 sendLine( std::string(
"commands: ") +
G::Str::join(
", ",commands) ) ;
204 void GSmtp::AdminServerPeer::flush()
206 G_DEBUG(
"GSmtp::AdminServerPeer: flush: \"" << m_remote_address <<
"\"" ) ;
207 if( m_client.busy() )
209 sendLine(
"error: still working" ) ;
211 else if( m_remote_address.empty() )
213 sendLine(
"error: no remote server configured: use --forward-to" ) ;
215 else if( m_server.store().empty() )
217 sendLine(
"error: no messages to send" ) ;
221 m_client.reset(
new Client(
GNet::Location(m_remote_address),m_server.secrets(),m_server.clientConfig()) ) ;
222 m_client->sendMessagesFrom( m_server.store() ) ;
227 void GSmtp::AdminServerPeer::sendLine( std::string line ,
bool with_prompt )
230 line.append(
"\n" ) ;
233 line.append( m_prompt ) ;
243 void GSmtp::AdminServerPeer::send_(
const std::string & s )
247 G_DEBUG(
"GSmtp::AdminServerPeer::send: flow control asserted: cannot send" ) ;
252 m_blocked = ! send( s ) ;
261 void GSmtp::AdminServerPeer::status()
263 std::ostringstream ss ;
267 std::string report = ss.str() ;
273 sendLine(
"no info" ) ;
279 return m_server.store().iterator(
false) ;
284 return m_server.store().failures() ;
287 void GSmtp::AdminServerPeer::sendList( MessageStore::Iterator iter )
289 std::ostringstream ss ;
290 for(
bool first =
true ;; first = false )
292 unique_ptr<StoredMessage> message( iter.next() ) ;
293 if( message.get() == nullptr )
break ;
294 if( !first ) ss << eol() ;
295 ss << message->name() ;
298 std::string result = ss.str() ;
299 if( result.size() == 0U )
300 sendLine(
"<none>" ) ;
302 sendLine( ss.str() ) ;
305 void GSmtp::AdminServerPeer::unfailAll()
307 return m_server.store().unfailAll() ;
314 const GNet::MultiServer::AddressList & listening_addresses ,
bool allow_remote ,
315 const std::string & remote_address ,
unsigned int connection_timeout ,
317 bool with_terminate ) :
318 GNet::MultiServer(eh,listening_addresses) ,
320 m_client_config(client_config) ,
322 m_allow_remote(allow_remote) ,
323 m_remote_address(remote_address) ,
324 m_connection_timeout(connection_timeout) ,
325 m_info_commands(info_commands) ,
326 m_config_commands(config_commands) ,
327 m_with_terminate(with_terminate)
344 G_WARNING(
"GSmtp::Server: configured to reject non-local admin connection: " << reason ) ;
349 m_info_commands , m_config_commands , m_with_terminate ) ;
350 m_peers.push_back( peer ) ;
353 catch( std::exception & e )
355 G_WARNING(
"GSmtp::AdminServer: exception from new connection: " << e.what() ) ;
362 serverReport(
"admin" ) ;
367 for( PeerList::iterator p = m_peers.begin() ; p != m_peers.end() ; ++p )
369 G_DEBUG(
"GSmtp::AdminServer::notify: " << (*p) <<
": " << s0 <<
": " << s1 ) ;
370 (*p)->notify( s0 , s1 , s2 ) ;
376 G_DEBUG(
"GSmtp::AdminServer::unregister: server=" <<
this <<
": peer=" << peer ) ;
377 PeerList::iterator p = std::find( m_peers.begin() , m_peers.end() , peer ) ;
378 if( p != m_peers.end() )
394 return m_connection_timeout ;
399 return m_client_config ;
An abstract base class for the GNet::Server's connection to a remote client.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
A server class which implements the emailrelay administration interface.
virtual void onSendComplete() override
Override from GNet::BufferedServerPeer.
void notify(const std::string &s0, const std::string &s1, const std::string &s2)
Called when something happens which the admin user might be interested in.
void unregister(AdminServerPeer *)
Called from the AdminServerPeer destructor.
AdminServer(GNet::ExceptionHandler &, MessageStore &store, const GSmtp::Client::Config &client_config, const GAuth::Secrets &client_secrets, const GNet::MultiServer::AddressList &listening_addresses, bool allow_remote, const std::string &remote_address, unsigned int connection_timeout, const G::StringMap &info_commands, const G::StringMap &config_commands, bool with_terminate)
Constructor.
void report(std::ostream &stream, const std::string &line_prefix=std::string(), const std::string &eol=std::string("\n")) const
Reports itself onto a stream.
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring Latin-1 case.
void report() const
Generates helpful diagnostics.
std::vector< std::string > StringArray
A std::vector of std::strings.
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
virtual void onDelete(const std::string &) override
Override from GNet::ServerPeer.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
AdminServerPeer(GNet::Server::PeerInfo, AdminServer &, const std::string &remote, const G::StringMap &info_commands, const G::StringMap &config_commands, bool with_terminate)
Constructor.
A class which allows SMTP messages to be stored and retrieved.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
A simple interface for a store of secrets used in authentication.
static void trimRight(std::string &s, const std::string &ws, size_type limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
const GAuth::Secrets & secrets() const
Returns a reference to the secrets object, as passed in to the constructor.
static Monitor * instance()
Returns the singleton pointer. Returns null if none.
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
A structure used in GNet::MultiServer::newPeer().
MessageStore & store()
Returns a reference to the message store, as passed in to the constructor.
virtual ~AdminServerPeer()
Destructor.
void notify(const std::string &s0, const std::string &s1, const std::string &s2)
Called when something happens.
static bool isLocal(const Address &, std::string &reason)
Returns true if the given address appears to be associated with the local host, or a helpful error me...
A structure containing GSmtp::Client configuration parameters.
A derivation of ServerPeer for the administration interface.
virtual bool onReceive(const char *, size_t, size_t) override
Override from GNet::BufferedServerPeer.
std::map< std::string, std::string > StringMap
A std::map of std::strings.
virtual void onSecure(const std::string &) override
Override from GNet::SocketProtocolSink.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
virtual ~AdminServer()
Destructor.
static bool exists()
Returns true if an instance exists.
virtual GNet::ServerPeer * newPeer(GNet::Server::PeerInfo, GNet::MultiServer::ServerInfo) g__final override
Override from GNet::MultiServer.
GSmtp::Client::Config clientConfig() const
Returns the client configuration.
static std::set< std::string > keySet(const StringMap &string_map)
Extracts the keys from a map of strings.
A structure used in GNet::Server::newPeer().
unsigned int connectionTimeout() const
Returns the connection timeout, as passed in to the constructor.
static std::string ws()
Returns a string of standard whitespace characters.
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
virtual void quit(std::string reason)=0
Causes run() to return (once the call stack has unwound).
An iterator class for GSmtp::MessageStore.