E-MailRelay
gpopserver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 // gpopserver.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gpop.h"
23 #include "gpopserver.h"
24 #include "gsocketprotocol.h"
25 #include "glocal.h"
26 #include "glog.h"
27 #include "gdebug.h"
28 #include "gassert.h"
29 #include <string>
30 
32  const Secrets & secrets , unique_ptr<ServerProtocol::Text> ptext ,
33  ServerProtocol::Config protocol_config ) :
34  GNet::BufferedServerPeer(peer_info,GNet::LineBufferConfig::pop()) ,
35  m_server(server) ,
36  m_ptext(ptext.release()) ,
37  m_protocol(*this,*this,store,secrets,*m_ptext.get(),peer_info.m_address,protocol_config)
38 {
39  G_LOG_S( "GPop::ServerPeer: pop connection from " << peer_info.m_address.displayString() ) ;
40  m_protocol.init() ;
41 }
42 
43 void GPop::ServerPeer::onDelete( const std::string & reason )
44 {
45  G_LOG_S( "GPop::ServerPeer: pop connection closed: " << reason << (reason.empty()?"":": ")
46  << peerAddress().second.displayString() ) ;
47 }
48 
49 bool GPop::ServerPeer::onReceive( const char * line_data , size_t line_size , size_t )
50 {
51  processLine( std::string(line_data,line_size) ) ;
52  return true ;
53 }
54 
55 void GPop::ServerPeer::processLine( const std::string & line )
56 {
57  m_protocol.apply( line ) ;
58 }
59 
60 bool GPop::ServerPeer::protocolSend( const std::string & line , size_t offset )
61 {
62  return send( line , offset ) ; // BufferedServerPeer::send() -- ServerPeer::send() -- see also GNet::Sender
63 }
64 
66 {
67  m_protocol.resume() ; // calls back to protocolSend()
68 }
69 
71 {
73  G_DEBUG( "ServerPeer::securityEnabled: ssl library " << (enabled?"enabled":"disabled") ) ;
74  return enabled ;
75 }
76 
78 {
79  secureAccept() ; // base class
80 }
81 
82 void GPop::ServerPeer::onSecure( const std::string & certificate )
83 {
84  m_protocol.secure() ;
85 }
86 
87 // ===
88 
89 GPop::Server::Server( GNet::ExceptionHandler & eh , Store & store , const Secrets & secrets , Config config ) :
90  GNet::MultiServer(eh,GNet::MultiServer::addressList(config.addresses,config.port)) ,
91  m_allow_remote(config.allow_remote) ,
92  m_store(store) ,
93  m_secrets(secrets)
94 {
95 }
96 
98 {
99  // early cleanup -- not really required
100  serverCleanup() ; // base class
101 }
102 
103 void GPop::Server::report() const
104 {
105  serverReport( "pop" ) ; // base class implementation
106  G_LOG_S( "GPop::Server: pop authentication secrets from \"" << m_secrets.path() << "\"" ) ;
107 }
108 
109 GNet::ServerPeer * GPop::Server::newPeer( GNet::Server::PeerInfo peer_info , GNet::MultiServer::ServerInfo )
110 {
111  try
112  {
113  std::string reason ;
114  if( ! m_allow_remote && ! GNet::Local::isLocal(peer_info.m_address,reason) )
115  {
116  G_WARNING( "GPop::Server: configured to reject non-local pop connection: " << reason ) ;
117  return nullptr ;
118  }
119 
120  return new ServerPeer( peer_info , *this , m_store , m_secrets ,
121  newProtocolText(peer_info.m_address) , ServerProtocol::Config() ) ;
122  }
123  catch( std::exception & e ) // newPeer()
124  {
125  G_WARNING( "GPop::Server: exception from new connection: " << e.what() ) ;
126  return nullptr ;
127  }
128 }
129 
130 unique_ptr<GPop::ServerProtocol::Text> GPop::Server::newProtocolText( GNet::Address peer_address ) const
131 {
132  return unique_ptr<ServerProtocol::Text>( new ServerProtocolText(peer_address) ) ;
133 }
134 
135 // ===
136 
137 GPop::Server::Config::Config() :
138  allow_remote(false) ,
139  port(110)
140 {
141 }
142 
143 GPop::Server::Config::Config( bool allow_remote_ , unsigned int port_ , const G::StringArray & addresses_ ) :
144  allow_remote(allow_remote_) ,
145  port(port_) ,
146  addresses(addresses_)
147 {
148 }
149 
150 /// \file gpopserver.cpp
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:173
A simple interface to a store of secrets as used in authentication.
Definition: gpopsecrets.h:43
Network classes.
Definition: gaddress.h:29
virtual void onSecure(const std::string &) override
Final override from GNet::SocketProtocolSink.
Definition: gpopserver.cpp:82
virtual void onSendComplete() override
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:65
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
void report() const
Generates helpful diagnostics after construction.
ServerPeer(GNet::Server::PeerInfo, Server &, Store &, const Secrets &, unique_ptr< ServerProtocol::Text > ptext, ServerProtocol::Config)
Constructor.
Definition: gpopserver.cpp:31
A structure containing GPop::Server configuration parameters.
Definition: gpopserver.h:98
virtual bool onReceive(const char *, size_t, size_t) override
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:49
void init()
Starts the protocol.
A message store.
Definition: gpopstore.h:45
virtual void onDelete(const std::string &) override
Final override from GNet::ServerPeer.
Definition: gpopserver.cpp:43
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
A POP server class.
Definition: gpopserver.h:94
A structure used in GNet::MultiServer::newPeer().
Definition: gmultiserver.h:100
Server(GNet::ExceptionHandler &, Store &store, const Secrets &, Config)
Constructor. The 'secrets' reference is kept.
Definition: gpopserver.cpp:89
virtual ~Server()
Destructor.
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...
Definition: glocal.cpp:65
virtual bool securityEnabled() const override
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:70
A structure containing configuration parameters for ServerProtocol. NOT USED.
static bool secureAcceptCapable()
Returns true if the implementation supports TLS/SSL and a "server" profile has been configured...
virtual bool protocolSend(const std::string &line, size_t) override
Final override from GPop::ServerProtocol::Sender.
Definition: gpopserver.cpp:60
A structure used in GNet::Server::newPeer().
Definition: gserver.h:85
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
virtual void securityStart() override
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:77