34 m_security(security) ,
36 m_store_lock(m_store) ,
39 m_peer_address(peer_address) ,
40 m_fsm(sStart,sEnd,s_Same,s_Any) ,
47 m_fsm.
addTransition( eStat , sActive , sActive , &GPop::ServerProtocol::doStat ) ;
48 m_fsm.
addTransition( eList , sActive , sActive , &GPop::ServerProtocol::doList ) ;
49 m_fsm.
addTransition( eRetr , sActive , sData , &GPop::ServerProtocol::doRetr , sActive ) ;
50 m_fsm.
addTransition( eTop , sActive , sData , &GPop::ServerProtocol::doTop , sActive ) ;
51 m_fsm.
addTransition( eDele , sActive , sActive , &GPop::ServerProtocol::doDele ) ;
52 m_fsm.
addTransition( eNoop , sActive , sActive , &GPop::ServerProtocol::doNoop ) ;
53 m_fsm.
addTransition( eRset , sActive , sActive , &GPop::ServerProtocol::doRset ) ;
54 m_fsm.
addTransition( eUidl , sActive , sActive , &GPop::ServerProtocol::doUidl ) ;
55 m_fsm.
addTransition( eSent , sData , sActive , &GPop::ServerProtocol::doNothing ) ;
56 m_fsm.
addTransition( eUser , sStart , sStart , &GPop::ServerProtocol::doUser ) ;
57 m_fsm.
addTransition( ePass , sStart , sActive , &GPop::ServerProtocol::doPass , sStart ) ;
58 m_fsm.
addTransition( eApop , sStart , sActive , &GPop::ServerProtocol::doApop , sStart ) ;
59 m_fsm.
addTransition( eQuit , sStart , sEnd , &GPop::ServerProtocol::doQuitEarly ) ;
60 m_fsm.
addTransition( eCapa , sStart , sStart , &GPop::ServerProtocol::doCapa ) ;
61 m_fsm.
addTransition( eCapa , sActive , sActive , &GPop::ServerProtocol::doCapa ) ;
62 if( m_security.securityEnabled() )
63 m_fsm.
addTransition( eStls , sStart , sStart , &GPop::ServerProtocol::doStls , sStart ) ;
64 m_fsm.
addTransition( eAuth , sStart , sAuth , &GPop::ServerProtocol::doAuth , sStart ) ;
65 m_fsm.
addTransition( eAuthData , sAuth , sActive , &GPop::ServerProtocol::doAuthData , sStart ) ;
66 m_fsm.
addTransition( eCapa , sActive , sActive , &GPop::ServerProtocol::doCapa ) ;
67 m_fsm.
addTransition( eQuit , sActive , sEnd , &GPop::ServerProtocol::doQuit ) ;
79 void GPop::ServerProtocol::sendInit()
81 std::string greeting = std::string() +
"+OK " + m_text.greeting() ;
82 std::string apop_challenge = m_auth.challenge() ;
83 if( ! apop_challenge.empty() )
85 greeting.append(
" " ) ;
86 greeting.append( apop_challenge ) ;
91 void GPop::ServerProtocol::sendOk()
96 void GPop::ServerProtocol::sendError(
const std::string & more )
101 send( std::string() +
"-ERR " + more ) ;
104 void GPop::ServerProtocol::sendError()
112 Event
event = m_fsm.state() == sAuth ? eAuthData : commandEvent(commandWord(line)) ;
117 log_text = (commandPart(line,0U)+
" [password not logged]") ;
118 if( event == eAuthData )
119 log_text = (
"[password not logged]") ;
120 if( event == eAuth && !commandPart(line,1U).empty() )
121 log_text = commandPart(line,0U) +
" " + commandPart(line,1U) ;
122 G_LOG(
"GPop::ServerProtocol: rx<<: \"" << log_text <<
"\"" ) ;
125 State new_state = m_fsm.apply( *
this , event , line ) ;
126 const bool protocol_error = new_state == s_Any ;
131 if( new_state == sData )
135 void GPop::ServerProtocol::sendContent()
138 std::string line( 200 ,
'.' ) ;
140 bool end_of_content = false ;
141 while( sendContentLine(line,end_of_content) )
144 G_LOG(
"GPop::ServerProtocol: tx>>: [" << n <<
" line(s) of content]" ) ;
148 G_LOG(
"GPop::ServerProtocol: tx>>: ." ) ;
150 m_fsm.apply( *
this , eSent ,
"" ) ;
156 G_DEBUG(
"GPop::ServerProtocol::resume: flow control released" ) ;
157 if( m_fsm.state() == sData )
161 bool GPop::ServerProtocol::sendContentLine( std::string & line ,
bool & stop )
163 G_ASSERT( m_content.get() != nullptr ) ;
166 bool limited = m_in_body && m_body_limit == 0L ;
167 if( m_body_limit > 0L && m_in_body )
175 bool eof = m_content->fail() || m_content->bad() ;
180 line.append( crlf() ) ;
184 line.append( crlf() ) ;
185 offset = line.at(1U) ==
'.' ? 0U : 1U ;
189 if( !m_in_body && line.length() == (offset+2U) )
193 bool line_fully_sent = m_sender.protocolSend( line , offset ) ;
196 stop = ( limited || eof ) && line_fully_sent ;
197 const bool pause = limited || eof || ! line_fully_sent ;
201 int GPop::ServerProtocol::commandNumber(
const std::string & line ,
int default_ ,
size_t index )
const
203 int number = default_ ;
208 catch( G::Str::Overflow & )
211 catch( G::Str::InvalidFormat & )
217 std::string GPop::ServerProtocol::commandWord(
const std::string & line )
const
222 std::string GPop::ServerProtocol::commandPart(
const std::string & line ,
size_t index )
const
226 return index >= part.size() ? std::string() : part.at(index) ;
229 std::string GPop::ServerProtocol::commandParameter(
const std::string & line_in ,
size_t index )
const
231 return commandPart( line_in , index ) ;
234 GPop::ServerProtocol::Event GPop::ServerProtocol::commandEvent(
const std::string & command )
const
236 if( command ==
"QUIT" )
return eQuit ;
237 if( command ==
"STAT" )
return eStat ;
238 if( command ==
"LIST" )
return eList ;
239 if( command ==
"RETR" )
return eRetr ;
240 if( command ==
"DELE" )
return eDele ;
241 if( command ==
"NOOP" )
return eNoop ;
242 if( command ==
"RSET" )
return eRset ;
244 if( command ==
"TOP" )
return eTop ;
245 if( command ==
"UIDL" )
return eUidl ;
246 if( command ==
"USER" )
return eUser ;
247 if( command ==
"PASS" )
return ePass ;
248 if( command ==
"APOP" )
return eApop ;
249 if( command ==
"AUTH" )
return eAuth ;
250 if( command ==
"CAPA" )
return eCapa ;
251 if( command ==
"STLS" )
return eStls ;
256 void GPop::ServerProtocol::doQuitEarly(
const std::string & ,
bool & )
258 send( std::string() +
"+OK " + m_text.quit() ) ;
259 throw ProtocolDone() ;
262 void GPop::ServerProtocol::doQuit(
const std::string & ,
bool & )
264 m_store_lock.commit() ;
265 send( std::string() +
"+OK " + m_text.quit() ) ;
266 throw ProtocolDone() ;
269 void GPop::ServerProtocol::doStat(
const std::string & ,
bool & )
271 std::ostringstream ss ;
272 ss <<
"+OK " << m_store_lock.messageCount() <<
" " << m_store_lock.totalByteCount() ;
276 void GPop::ServerProtocol::doUidl(
const std::string & line ,
bool & )
278 sendList( line ,
true ) ;
281 void GPop::ServerProtocol::doList(
const std::string & line ,
bool & )
283 sendList( line ,
false ) ;
286 void GPop::ServerProtocol::sendList(
const std::string & line ,
bool uidl )
288 std::string id_string = commandParameter( line ) ;
292 if( ! id_string.empty() )
294 id = commandNumber( line , -1 ) ;
295 if( !m_store_lock.valid(
id) )
297 sendError(
"invalid id" ) ;
303 bool multi_line =
id == -1 ;
304 GPop::StoreLock::List list = m_store_lock.list(
id ) ;
305 std::ostringstream ss ;
307 if( multi_line ) ss << list.size() <<
" message(s)" << crlf() ;
308 for( GPop::StoreLock::List::iterator p = list.begin() ; p != list.end() ; ++p )
310 ss << (*p).id <<
" " ;
311 if( uidl ) ss << (*p).uidl ;
312 if( !uidl ) ss << (*p).size ;
313 if( multi_line ) ss << crlf() ;
315 if( multi_line ) ss <<
"." ;
319 void GPop::ServerProtocol::doRetr(
const std::string & line ,
bool & more )
321 int id = commandNumber( line , -1 ) ;
322 if(
id == -1 || ! m_store_lock.valid(
id) )
329 m_content.reset( m_store_lock.get(
id).release() ) ;
332 std::ostringstream ss ;
333 ss <<
"+OK " << m_store_lock.byteCount(
id) <<
" octets" ;
338 void GPop::ServerProtocol::doTop(
const std::string & line ,
bool & more )
340 int id = commandNumber( line , -1 , 1U ) ;
341 int n = commandNumber( line , -1 , 2U ) ;
342 G_DEBUG(
"ServerProtocol::doTop: " <<
id <<
", " << n ) ;
343 if(
id == -1 || ! m_store_lock.valid(
id) || n < 0 )
350 m_content.reset( m_store_lock.get(
id).release() ) ;
357 void GPop::ServerProtocol::doDele(
const std::string & line ,
bool & )
359 int id = commandNumber( line , -1 ) ;
360 if(
id == -1 || ! m_store_lock.valid(
id) )
366 m_store_lock.remove(
id ) ;
371 void GPop::ServerProtocol::doRset(
const std::string & ,
bool & )
373 m_store_lock.rollback() ;
377 void GPop::ServerProtocol::doNoop(
const std::string & ,
bool & )
382 void GPop::ServerProtocol::doNothing(
const std::string & ,
bool & )
386 void GPop::ServerProtocol::doAuth(
const std::string & line ,
bool & ok )
388 std::string mechanism =
G::Str::upper( commandParameter(line) ) ;
390 if( mechanism.empty() )
394 std::string list = m_auth.mechanisms() ;
400 else if( m_auth.sensitive() && ! m_secure )
405 sendError(
"must use STLS before authentication" ) ;
409 std::string initial_response = commandParameter(line,2) ;
410 if( initial_response ==
"=" )
411 initial_response = std::string() ;
416 bool supported = mechanism !=
"LOGIN" && m_auth.init( mechanism ) ;
420 sendError(
"invalid mechanism" ) ;
422 else if( m_auth.mustChallenge() && !initial_response.empty() )
425 sendError(
"invalid initial response" ) ;
427 else if( !initial_response.empty() )
429 m_fsm.apply( *
this , eAuthData , initial_response ) ;
433 std::string initial_challenge = m_auth.challenge() ;
439 void GPop::ServerProtocol::doAuthData(
const std::string & line ,
bool & ok )
446 m_user = m_auth.id() ;
455 void GPop::ServerProtocol::lockStore()
457 m_store_lock.lock( m_user ) ;
458 G_LOG_S(
"GPop::ServerProtocol: pop authentication of " << m_user
459 <<
" connected from " << m_peer_address.displayString() ) ;
462 void GPop::ServerProtocol::doStls(
const std::string & ,
bool & )
464 G_ASSERT( m_security.securityEnabled() ) ;
466 m_security.securityStart() ;
475 bool GPop::ServerProtocol::mechanismsIncludePlain()
const
477 return m_auth.valid() && m_auth.mechanisms().find(
"PLAIN") != std::string::npos ;
480 std::string GPop::ServerProtocol::mechanismsWithoutLogin()
const
485 result = m_auth.mechanisms() ;
492 void GPop::ServerProtocol::doCapa(
const std::string & ,
bool & )
494 send( std::string() +
"+OK " + m_text.capa() ) ;
498 if( mechanismsIncludePlain() )
505 if( m_security.securityEnabled() )
511 std::string mechanisms = std::string(1U,
' ') + mechanismsWithoutLogin() ;
512 if( mechanisms.length() > 1U )
513 send( std::string() +
"SASL" + mechanisms ) ;
518 void GPop::ServerProtocol::doUser(
const std::string & line ,
bool & )
520 if( mechanismsIncludePlain() )
522 m_user = commandParameter(line) ;
523 send( std::string() +
"+OK " + m_text.user(commandParameter(line)) ) ;
527 sendError(
"no SASL PLAIN mechanism to do USER/PASS authentication" ) ;
531 void GPop::ServerProtocol::doPass(
const std::string & line ,
bool & ok )
534 std::string rsp = m_user + std::string(1U,
'\0') + m_user + std::string(1U,
'\0') + commandParameter(line) ;
535 ok = !m_user.empty() && m_auth.valid() && m_auth.init(
"PLAIN") &&
536 m_auth.authenticated( rsp , std::string() ) ;
548 void GPop::ServerProtocol::doApop(
const std::string & line ,
bool & ok )
550 m_user = commandParameter(line,1) ;
551 std::string rsp = m_user +
" " + commandParameter(line,2) ;
552 ok = m_auth.valid() && m_auth.init(
"APOP") && m_auth.authenticated(rsp,std::string()) ;
560 m_user = std::string() ;
565 void GPop::ServerProtocol::send( std::string line )
568 line.append( crlf() ) ;
569 m_sender.protocolSend( line , 0U ) ;
572 const std::string & GPop::ServerProtocol::crlf()
574 static const std::string s(
"\015\012" ) ;
586 return "POP3 server ready" ;
591 return "signing off" ;
596 return "capability list follows" ;
601 return std::string() +
"user: " + id ;
606 GPop::ServerProtocol::Text::~Text()
612 GPop::ServerProtocol::Sender::~Sender()
618 GPop::ServerProtocol::Config::Config()
624 GPop::ServerProtocol::Security::~Security()
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
A simple interface to a store of secrets as used in authentication.
static int toInt(const std::string &s)
Converts string 's' to an int.
virtual std::string greeting() const override
Final override from GPop::ServerProtocol::Text.
The GNet::Address class encapsulates a TCP/UDP transport address.
std::vector< std::string > StringArray
A std::vector of std::strings.
static std::string decode(const std::string &, bool strict=false)
Decodes the given string.
static bool replace(std::string &s, const std::string &from, const std::string &to, size_type *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
virtual std::string user(const std::string &id) const override
Final override from GPop::ServerProtocol::Text.
void init()
Starts the protocol.
virtual ~ServerProtocol()
Destructor.
An interface used by ServerProtocol to send protocol replies.
An interface used by ServerProtocol to enable TLS.
ServerProtocolText(GNet::Address peer)
Constructor.
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'...
static std::string unique(std::string s, char c= ' ', char r= ' ')
Returns a string with repeated 'c' characters replaced by one 'r' character.
An interface used by ServerProtocol to provide response text strings.
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lowercase characters have been replaced by uppercase chara...
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
virtual std::string quit() const override
Final override from GPop::ServerProtocol::Text.
ServerProtocol(Sender &sender, Security &security, Store &store, const Secrets &secrets, const Text &text, GNet::Address peer_address, Config config)
Constructor.
A structure containing configuration parameters for ServerProtocol. NOT USED.
virtual std::string capa() const override
Final override from GPop::ServerProtocol::Text.
void addTransition(Event event, State from, State to, Action action)
Adds a transition.
void secure()
Called when the server connection becomes secure.
void apply(const std::string &line)
Called on receipt of a string from the client.
static std::string encode(const std::string &s, const std::string &line_break)
Encodes the given string.
static std::string ws()
Returns a string of standard whitespace characters.
void resume()
Called when the Sender can send again.