41 m_verifier(verifier) ,
44 m_sasl(
GAuth::SaslServerFactory::newSaslServer(secrets,false)) ,
46 m_fsm(sStart,sEnd,s_Same,s_Any) ,
47 m_with_starttls(false) ,
48 m_peer_address(peer_address) ,
50 m_bad_client_count(0U) ,
51 m_bad_client_limit(8U) ,
52 m_session_authenticated(false)
59 m_fsm.
addTransition( eQuit , s_Any , sEnd , &GSmtp::ServerProtocol::doQuit ) ;
60 m_fsm.
addTransition( eUnknown , sProcessing , s_Same , &GSmtp::ServerProtocol::doIgnore ) ;
61 m_fsm.
addTransition( eUnknown , s_Any , s_Same , &GSmtp::ServerProtocol::doUnknown ) ;
62 m_fsm.
addTransition( eRset , sStart , s_Same , &GSmtp::ServerProtocol::doNoop ) ;
63 m_fsm.
addTransition( eRset , s_Any , sIdle , &GSmtp::ServerProtocol::doRset ) ;
64 m_fsm.
addTransition( eNoop , s_Any , s_Same , &GSmtp::ServerProtocol::doNoop ) ;
65 m_fsm.
addTransition( eHelp , s_Any , s_Same , &GSmtp::ServerProtocol::doHelp ) ;
66 m_fsm.
addTransition( eExpn , s_Any , s_Same , &GSmtp::ServerProtocol::doExpn ) ;
67 m_fsm.
addTransition( eVrfy , sStart , sVrfyStart , &GSmtp::ServerProtocol::doVrfy , s_Same ) ;
68 m_fsm.
addTransition( eVrfyReply , sVrfyStart , sStart , &GSmtp::ServerProtocol::doVrfyReply ) ;
69 m_fsm.
addTransition( eVrfy , sIdle , sVrfyIdle , &GSmtp::ServerProtocol::doVrfy , s_Same ) ;
70 m_fsm.
addTransition( eVrfyReply , sVrfyIdle , sIdle , &GSmtp::ServerProtocol::doVrfyReply ) ;
71 m_fsm.
addTransition( eVrfy , sGotMail , sVrfyGotMail, &GSmtp::ServerProtocol::doVrfy , s_Same ) ;
72 m_fsm.
addTransition( eVrfyReply , sVrfyGotMail, sGotMail , &GSmtp::ServerProtocol::doVrfyReply ) ;
73 m_fsm.
addTransition( eVrfy , sGotRcpt , sVrfyGotRcpt, &GSmtp::ServerProtocol::doVrfy , s_Same ) ;
74 m_fsm.
addTransition( eVrfyReply , sVrfyGotRcpt, sGotRcpt , &GSmtp::ServerProtocol::doVrfyReply ) ;
75 m_fsm.
addTransition( eEhlo , s_Any , sIdle , &GSmtp::ServerProtocol::doEhlo , s_Same ) ;
76 m_fsm.
addTransition( eHelo , s_Any , sIdle , &GSmtp::ServerProtocol::doHelo , s_Same ) ;
77 m_fsm.
addTransition( eMail , sIdle , sGotMail , &GSmtp::ServerProtocol::doMail , sIdle ) ;
78 m_fsm.
addTransition( eRcpt , sGotMail , sVrfyTo1 , &GSmtp::ServerProtocol::doRcpt , s_Same ) ;
79 m_fsm.
addTransition( eVrfyReply , sVrfyTo1 , sGotRcpt , &GSmtp::ServerProtocol::doVrfyToReply , sGotMail ) ;
80 m_fsm.
addTransition( eRcpt , sGotRcpt , sVrfyTo2 , &GSmtp::ServerProtocol::doRcpt , s_Same ) ;
81 m_fsm.
addTransition( eVrfyReply , sVrfyTo2 , sGotRcpt , &GSmtp::ServerProtocol::doVrfyToReply ) ;
82 m_fsm.
addTransition( eData , sGotMail , sIdle , &GSmtp::ServerProtocol::doNoRecipients ) ;
83 m_fsm.
addTransition( eData , sGotRcpt , sData , &GSmtp::ServerProtocol::doData ) ;
84 m_fsm.
addTransition( eContent , sData , sData , &GSmtp::ServerProtocol::doContent , sDiscarding ) ;
85 m_fsm.
addTransition( eEot , sData , sProcessing , &GSmtp::ServerProtocol::doEot ) ;
86 m_fsm.
addTransition( eDone , sProcessing , sIdle , &GSmtp::ServerProtocol::doComplete ) ;
87 m_fsm.
addTransition( eTimeout , sProcessing , sIdle , &GSmtp::ServerProtocol::doComplete ) ;
88 m_fsm.
addTransition( eContent , sDiscarding , sDiscarding , &GSmtp::ServerProtocol::doDiscard ) ;
89 m_fsm.
addTransition( eEot , sDiscarding , sIdle , &GSmtp::ServerProtocol::doDiscarded ) ;
91 if( m_sasl->active() )
93 m_fsm.
addTransition( eAuth , sIdle , sAuth , &GSmtp::ServerProtocol::doAuth , sIdle ) ;
94 m_fsm.
addTransition( eAuthData, sAuth , sAuth , &GSmtp::ServerProtocol::doAuthData , sIdle ) ;
100 m_fsm.
addTransition( eStartTls , sIdle , sStartingTls , &GSmtp::ServerProtocol::doStartTls , sIdle ) ;
101 m_fsm.
addTransition( eSecure , sStartingTls , sIdle , &GSmtp::ServerProtocol::doSecure ) ;
107 sendGreeting( m_text.greeting() ) ;
112 m_message.doneSignal().disconnect() ;
113 m_verifier.doneSignal().disconnect() ;
118 State new_state = m_fsm.apply( *
this , eSecure , std::make_pair(certificate.data(),certificate.size()) ) ;
119 if( new_state == s_Any )
120 throw ProtocolDone(
"protocol error" ) ;
123 void GSmtp::ServerProtocol::doSecure( EventData certificate ,
bool & )
125 G_DEBUG(
"GSmtp::ServerProtocol::doSecure" ) ;
127 m_certificate = std::string(certificate.first,certificate.second) ;
130 void GSmtp::ServerProtocol::doStartTls( EventData ,
bool & ok )
134 send(
"503 command out of sequence" ) ;
139 send(
"220 ready to start tls" ,
true ) ;
143 void GSmtp::ServerProtocol::sendGreeting(
const std::string & text )
145 send( std::string(
"220 ") + text ) ;
150 Event
event = eUnknown ;
151 State state = m_fsm.state() ;
152 EventData event_data( line_data , line_size ) ;
153 if( (state == sData || state == sDiscarding) && isEndOfText(line_data,line_size) )
157 else if( state == sData || state == sDiscarding )
161 else if( state == sAuth )
167 std::string line( line_data , line_size ) ;
169 event = commandEvent( commandWord(line) ) ;
170 m_buffer = commandLine( line ) ;
171 event_data = EventData( m_buffer.data() , m_buffer.size() ) ;
174 State new_state = m_fsm.apply( *
this , event , event_data ) ;
175 if( new_state == s_Any )
176 sendOutOfSequence( std::string(line_data,line_size) ) ;
181 void GSmtp::ServerProtocol::doContent( EventData event_data ,
bool & ok )
183 if( isEscaped(event_data.first,event_data.second) )
184 ok = m_message.addText( event_data.first+1 , event_data.second-1U ) ;
186 ok = m_message.addText( event_data.first , event_data.second ) ;
189 if( !ok && m_config.disconnect_on_max_size )
193 void GSmtp::ServerProtocol::doEot( EventData event_data ,
bool & )
195 std::string line( event_data.first , event_data.second ) ;
196 G_LOG(
"GSmtp::ServerProtocol: rx<<: [message content not logged]" ) ;
198 if( m_config.filter_timeout != 0U )
200 G_DEBUG(
"GSmtp::ServerProtocol: starting filter timer: " << m_config.filter_timeout ) ;
201 startTimer( m_config.filter_timeout ) ;
203 m_message.process( m_sasl->id() , m_peer_address.hostPartString() , m_certificate ) ;
206 void GSmtp::ServerProtocol::processDone(
bool success ,
unsigned long id , std::string response , std::string reason )
208 G_IGNORE_PARAMETER(
unsigned long ,
id ) ;
209 G_IGNORE_PARAMETER( std::string , reason ) ;
210 G_DEBUG(
"GSmtp::ServerProtocol::processDone: " << (success?1:0) <<
" " <<
id <<
" [" << response <<
"] [" << reason <<
"]" ) ;
211 G_ASSERT( success == response.empty() ) ;
213 State new_state = m_fsm.apply( *
this , eDone , std::make_pair(response.data(),response.size()) ) ;
214 if( new_state == s_Any )
215 throw ProtocolDone(
"protocol error" ) ;
220 G_WARNING(
"GSmtp::ServerProtocol::onTimeout: message filter timed out after " << m_config.filter_timeout <<
"s" ) ;
221 std::string reason =
"timed out" ;
222 State new_state = m_fsm.apply( *
this , eTimeout , std::make_pair(reason.data(),reason.size()) ) ;
223 if( new_state == s_Any )
224 throw ProtocolDone(
"protocol error" ) ;
227 void GSmtp::ServerProtocol::doComplete( EventData event_data ,
bool & )
230 const bool empty = event_data.second == 0U ;
231 sendCompletionReply( empty , std::string(event_data.first,event_data.second) ) ;
234 void GSmtp::ServerProtocol::doQuit( EventData ,
bool & )
238 throw ProtocolDone() ;
241 void GSmtp::ServerProtocol::doDiscard( EventData ,
bool & )
243 if( m_config.disconnect_on_max_size )
247 throw ProtocolDone() ;
251 void GSmtp::ServerProtocol::doIgnore( EventData ,
bool & )
255 void GSmtp::ServerProtocol::doNoop( EventData ,
bool & )
260 void GSmtp::ServerProtocol::doNothing( EventData ,
bool & )
264 void GSmtp::ServerProtocol::doDiscarded( EventData ,
bool & )
270 void GSmtp::ServerProtocol::doExpn( EventData ,
bool & )
272 sendNotImplemented() ;
275 void GSmtp::ServerProtocol::doHelp( EventData ,
bool & )
277 sendNotImplemented() ;
280 void GSmtp::ServerProtocol::doVrfy( EventData event_data ,
bool & predicate )
282 std::string line( event_data.first , event_data.second ) ;
283 if( m_config.with_vrfy )
285 std::string to = parseRcptParameter( line ) ;
289 sendNotVerified(
"invalid mailbox" ,
false ) ;
299 sendNotImplemented() ;
303 void GSmtp::ServerProtocol::verify(
const std::string & to ,
const std::string & from )
305 std::string mechanism = m_sasl->active() ? m_sasl->mechanism() : std::string() ;
306 std::string
id = m_sasl->active() ? m_sasl->id() : std::string() ;
307 if( m_sasl->active() && !m_session_authenticated )
309 m_verifier.verify( to , from , m_peer_address , mechanism ,
id ) ;
312 void GSmtp::ServerProtocol::verifyDone( std::string mbox , VerifierStatus status )
315 throw ProtocolDone(
"verifier abort" ) ;
317 std::string status_str = status.str( mbox ) ;
318 State new_state = m_fsm.apply( *
this , eVrfyReply , std::make_pair(status_str.data(),status_str.size()) ) ;
319 if( new_state == s_Any )
320 throw ProtocolDone(
"protocol error" ) ;
323 void GSmtp::ServerProtocol::doVrfyReply( EventData event_data ,
bool & )
325 std::string line( event_data.first , event_data.second ) ;
329 if( status.is_valid && status.is_local )
330 sendVerified( status.full_name ) ;
331 else if( status.is_valid )
332 sendWillAccept( mbox ) ;
334 sendNotVerified( status.response , status.temporary ) ;
337 std::string GSmtp::ServerProtocol::parseRcptParameter(
const std::string & line )
const
340 size_t pos = line.find_first_of(
" \t" ) ;
341 if( pos != std::string::npos )
342 to = line.substr(pos) ;
348 void GSmtp::ServerProtocol::doEhlo( EventData event_data ,
bool & predicate )
350 std::string line( event_data.first , event_data.second ) ;
351 std::string smtp_peer_name = parsePeerName( line ) ;
352 if( smtp_peer_name.empty() )
355 sendMissingParameter() ;
359 m_session_peer_name = smtp_peer_name ;
360 m_session_authenticated = false ;
366 void GSmtp::ServerProtocol::doHelo( EventData event_data ,
bool & predicate )
368 std::string line( event_data.first , event_data.second ) ;
369 std::string smtp_peer_name = parsePeerName( line ) ;
370 if( smtp_peer_name.empty() )
373 sendMissingParameter() ;
377 m_session_peer_name = smtp_peer_name ;
383 bool GSmtp::ServerProtocol::authenticationRequiresEncryption()
const
385 bool encryption_required_by_user = m_config.authentication_requires_encryption ;
386 bool encryption_required_by_sasl = m_sasl->active() && m_sasl->requiresEncryption() ;
387 return encryption_required_by_user || encryption_required_by_sasl ;
390 void GSmtp::ServerProtocol::doAuth( EventData event_data ,
bool & predicate )
395 std::string mechanism = word_array.size() > 1U ? word_array[1U] : std::string() ;
397 std::string initial_response = word_array.size() > 2U ? word_array[2U] : std::string() ;
398 bool got_initial_response = word_array.size() > 2U ;
400 G_DEBUG(
"ServerProtocol::doAuth: [" << mechanism <<
"], [" << initial_response <<
"]" ) ;
402 if( !m_secure && authenticationRequiresEncryption() )
404 G_WARNING(
"GSmtp::ServerProtocol: rejecting authentication attempt without encryption" ) ;
406 send(
"504 Unsupported authentication mechanism" ) ;
408 else if( m_session_authenticated )
410 G_WARNING(
"GSmtp::ServerProtocol: too many AUTH requests" ) ;
412 sendOutOfSequence(std::string(event_data.first,event_data.second)) ;
414 else if( ! m_sasl->init(mechanism) )
416 G_WARNING(
"GSmtp::ServerProtocol: request for unsupported server AUTH mechanism: " << mechanism ) ;
418 send(
"504 Unsupported authentication mechanism" ) ;
422 G_WARNING(
"GSmtp::ServerProtocol: invalid base64 encoding of AUTH parameter" ) ;
424 send(
"501 Invalid argument" ) ;
426 else if( got_initial_response )
428 std::string s = initial_response ==
"=" ? std::string() :
G::Base64::decode(initial_response) ;
430 std::string next_challenge = m_sasl->apply( s , done ) ;
434 m_session_authenticated = m_sasl->authenticated() ;
435 sendAuthDone( m_sasl->authenticated() ) ;
439 sendChallenge( next_challenge ) ;
444 sendChallenge( m_sasl->initialChallenge() ) ;
448 void GSmtp::ServerProtocol::sendAuthDone(
bool ok )
451 send(
"235 Authentication sucessful" ) ;
453 send(
"535 Authentication failed" ) ;
456 void GSmtp::ServerProtocol::doAuthData( EventData event_data ,
bool & predicate )
458 G_LOG(
"GSmtp::ServerProtocol: rx<<: [authentication response not logged]" ) ;
459 std::string line( event_data.first , event_data.second ) ;
463 send(
"501 authentication cancelled" ) ;
467 G_WARNING(
"GSmtp::ServerProtocol: invalid base64 encoding of authentication response" ) ;
469 sendAuthDone(
false ) ;
478 m_session_authenticated = m_sasl->authenticated() ;
479 sendAuthDone( m_sasl->authenticated() ) ;
483 sendChallenge( next_challenge ) ;
488 void GSmtp::ServerProtocol::sendChallenge(
const std::string & challenge )
493 void GSmtp::ServerProtocol::doMail( EventData event_data ,
bool & predicate )
495 std::string line( event_data.first , event_data.second ) ;
496 if( !m_session_authenticated && m_sasl->active() && !m_sasl->trusted(m_peer_address) )
501 else if( !m_secure && m_config.mail_requires_encryption )
504 sendEncryptionRequired() ;
506 else if( m_config.max_size && parseMailSize(line) > m_config.max_size )
514 std::pair<std::string,std::string> from_pair = parseMailFrom( line ) ;
515 bool ok = from_pair.second.empty() && m_message.setFrom( from_pair.first , parseMailAuth(line) ) ;
523 sendBadFrom( from_pair.second ) ;
528 void GSmtp::ServerProtocol::doRcpt( EventData event_data ,
bool & predicate )
530 std::pair<std::string,std::string> to_pair = parseRcptTo( std::string(event_data.first,event_data.second) ) ;
531 std::string reason = to_pair.second ;
532 bool ok = reason.empty() ;
536 verify( to_pair.first , m_message.from() ) ;
541 sendBadTo( reason ,
false ) ;
545 void GSmtp::ServerProtocol::doVrfyToReply( EventData event_data ,
bool & predicate )
548 VerifierStatus status =
VerifierStatus::parse( std::string(event_data.first,event_data.second) , to ) ;
550 bool ok = m_message.addTo( to , status ) ;
562 void GSmtp::ServerProtocol::doUnknown( EventData event_data ,
bool & )
564 sendUnrecognised( std::string(event_data.first,event_data.second) ) ;
567 void GSmtp::ServerProtocol::reset()
572 m_verifier.cancel() ;
575 void GSmtp::ServerProtocol::doRset( EventData ,
bool & )
582 void GSmtp::ServerProtocol::doNoRecipients( EventData ,
bool & )
587 void GSmtp::ServerProtocol::doData( EventData ,
bool & )
589 std::string received_line = m_text.received( m_session_peer_name , m_session_authenticated , m_secure ) ;
590 if( received_line.length() )
591 m_message.addReceived( received_line ) ;
596 void GSmtp::ServerProtocol::sendOutOfSequence(
const std::string & )
598 send(
"503 command out of sequence -- use RSET to resynchronise" ) ;
602 void GSmtp::ServerProtocol::sendMissingParameter()
604 send(
"501 parameter required" ) ;
607 bool GSmtp::ServerProtocol::isEndOfText(
const char * line ,
size_t line_size )
const
609 return line_size == 1U && *line ==
'.' ;
612 bool GSmtp::ServerProtocol::isEscaped(
const char * line ,
size_t line_size )
const
614 return line_size > 1U && *line ==
'.' ;
617 std::string GSmtp::ServerProtocol::commandWord(
const std::string & line_in )
const
619 std::string line( line_in ) ;
622 size_t pos = line.find_first_of(
" \t" ) ;
623 std::string command = line.substr( 0U , pos ) ;
629 std::string GSmtp::ServerProtocol::commandLine(
const std::string & line_in )
const
631 std::string line( line_in ) ;
636 GSmtp::ServerProtocol::Event GSmtp::ServerProtocol::commandEvent(
const std::string & command )
const
638 if( command ==
"QUIT" )
return eQuit ;
639 if( command ==
"HELO" )
return eHelo ;
640 if( command ==
"EHLO" )
return eEhlo ;
641 if( command ==
"RSET" )
return eRset ;
642 if( command ==
"DATA" )
return eData ;
643 if( command ==
"RCPT" )
return eRcpt ;
644 if( command ==
"MAIL" )
return eMail ;
645 if( command ==
"VRFY" )
return eVrfy ;
646 if( command ==
"NOOP" )
return eNoop ;
647 if( command ==
"EXPN" )
return eExpn ;
648 if( command ==
"HELP" )
return eHelp ;
649 if( command ==
"STARTTLS" )
return eStartTls ;
650 if( m_sasl->active() && command ==
"AUTH" )
return eAuth ;
654 void GSmtp::ServerProtocol::sendClosing()
656 send(
"221 closing connection" ) ;
657 m_sender.protocolShutdown() ;
660 void GSmtp::ServerProtocol::sendVerified(
const std::string & user )
662 send(
"250 " + user ) ;
665 void GSmtp::ServerProtocol::sendNotVerified(
const std::string & response ,
bool temporary )
667 send( (temporary?
"450":
"550") + std::string(1U,
' ') + response ) ;
670 void GSmtp::ServerProtocol::sendWillAccept(
const std::string & user )
675 void GSmtp::ServerProtocol::sendUnrecognised(
const std::string & line )
677 send(
"500 command unrecognized: \"" +
G::Str::printable(line) + std::string(1U,
'\"') ) ;
681 void GSmtp::ServerProtocol::sendNotImplemented()
683 send(
"502 command not implemented" ) ;
686 void GSmtp::ServerProtocol::sendAuthRequired()
688 std::string more_help = authenticationRequiresEncryption() && !m_secure ?
": use starttls" :
"" ;
689 send(
"530 authentication required" + more_help ) ;
692 void GSmtp::ServerProtocol::sendEncryptionRequired()
694 send(
"530 encryption required: use starttls" ) ;
697 void GSmtp::ServerProtocol::sendNoRecipients()
699 send(
"554 no valid recipients" ) ;
702 void GSmtp::ServerProtocol::sendTooBig(
bool disconnecting )
704 std::string s =
"552 message exceeds fixed maximum message size" ;
705 if( disconnecting ) s.append(
", disconnecting" ) ;
709 void GSmtp::ServerProtocol::sendDataReply()
711 send(
"354 start mail input -- end with <CRLF>.<CRLF>" ) ;
714 void GSmtp::ServerProtocol::sendRsetReply()
716 send(
"250 state reset" ) ;
719 void GSmtp::ServerProtocol::sendMailReply()
724 void GSmtp::ServerProtocol::sendCompletionReply(
bool ok ,
const std::string & reason )
729 send(
"452 " + reason ) ;
732 void GSmtp::ServerProtocol::sendRcptReply()
737 void GSmtp::ServerProtocol::sendBadFrom( std::string reason )
739 std::string msg =
"553 mailbox name not allowed" ;
740 if( ! reason.empty() )
743 msg.append( reason ) ;
748 void GSmtp::ServerProtocol::sendBadTo(
const std::string & text ,
bool temporary )
750 send( (temporary?
"450":
"550") + std::string(text.empty()?
"":
" ") + text ) ;
753 void GSmtp::ServerProtocol::sendEhloReply()
755 std::ostringstream ss ;
756 ss <<
"250-" << m_text.hello(m_session_peer_name) << crlf() ;
758 if( m_config.max_size != 0U )
759 ss <<
"250-SIZE " << m_config.max_size << crlf() ;
761 if( m_sasl->active() && !( authenticationRequiresEncryption() && !m_secure ) )
762 ss <<
"250-AUTH " << m_sasl->mechanisms() << crlf() ;
764 if( m_with_starttls && !m_secure )
765 ss <<
"250-STARTTLS" << crlf() ;
767 if( m_config.with_vrfy )
768 ss <<
"250-VRFY" << crlf() ;
770 ss <<
"250 8BITMIME" ;
774 void GSmtp::ServerProtocol::sendHeloReply()
779 void GSmtp::ServerProtocol::sendOk()
784 const std::string & GSmtp::ServerProtocol::crlf()
786 static const std::string s(
"\015\012" ) ;
790 void GSmtp::ServerProtocol::send( std::string line ,
bool go_secure )
793 line.append( crlf() ) ;
794 m_sender.protocolSend( line , go_secure ) ;
797 size_t GSmtp::ServerProtocol::parseMailSize(
const std::string & line )
const
799 std::string parameter = parseMailParameter( line ,
"SIZE=" ) ;
806 std::string GSmtp::ServerProtocol::parseMailAuth(
const std::string & line )
const
808 return parseMailParameter( line ,
"AUTH=" ) ;
811 std::string GSmtp::ServerProtocol::parseMailParameter(
const std::string & line ,
const std::string & key )
const
814 size_t end = line.find(
'>' ) ;
815 if( end != std::string::npos )
818 for( G::StringArray::iterator p = parameters.begin() ; p != parameters.end() ; ++p )
821 if( pos == 0U && (*p).length() > key.size() )
831 std::pair<std::string,std::string> GSmtp::ServerProtocol::parseMailFrom(
const std::string & line )
const
834 return parseAddress( line ) ;
837 std::pair<std::string,std::string> GSmtp::ServerProtocol::parseRcptTo(
const std::string & line )
const
841 return parseAddress( line ) ;
844 std::pair<std::string,std::string> GSmtp::ServerProtocol::parseAddress(
const std::string & line )
const
846 size_t start = line.find(
'<' ) ;
847 size_t end = line.find(
'>' ) ;
848 if( start == std::string::npos || end == std::string::npos || end < start )
850 std::string reason(
"missing or invalid angle brackets in mailbox name" ) ;
851 return std::make_pair(std::string(),reason) ;
854 std::string s = line.substr( start + 1U , end - start - 1U ) ;
858 if( s.length() > 0U && s.at(0U) ==
'@' )
860 size_t colon_pos = s.find(
':' ) ;
861 if( colon_pos == std::string::npos )
863 std::string reason(
"invalid mailbox name: no colon after leading at character" ) ;
864 return std::make_pair(std::string(),reason) ;
866 s = s.substr( colon_pos + 1U ) ;
869 return std::make_pair(s,std::string()) ;
872 std::string GSmtp::ServerProtocol::parsePeerName(
const std::string & line )
const
874 size_t pos = line.find_first_of(
" \t" ) ;
875 if( pos == std::string::npos )
876 return std::string() ;
878 std::string smtp_peer_name = line.substr( pos + 1U ) ;
880 return smtp_peer_name ;
883 void GSmtp::ServerProtocol::badClientEvent()
885 m_bad_client_count++ ;
886 if( m_bad_client_limit && m_bad_client_count >= m_bad_client_limit )
888 std::string reason =
"too many protocol errors from the client" ;
889 G_DEBUG(
"GSmtp::ServerProtocol::badClientEvent: " << reason <<
": dropping the connection" ) ;
890 throw ProtocolDone( reason ) ;
898 m_code_ident(code_ident) ,
899 m_thishost(thishost) ,
900 m_peer_address(peer_address)
906 return m_thishost +
" -- " + m_code_ident +
" -- Service ready" ;
911 return m_thishost +
" says hello" ;
915 bool authenticated ,
bool secure )
const
917 return receivedLine( smtp_peer_name , m_peer_address.hostPartString() , m_thishost , authenticated , secure ) ;
921 const std::string & peer_address ,
const std::string & thishost ,
922 bool authenticated ,
bool secure )
929 const std::string esmtp = std::string(
"ESMTP") + (secure?
"S":
"") + (authenticated?
"A":
"") ;
932 std::ostringstream ss ;
934 <<
"Received: from " << peer_name
936 <<
"[" << peer_address <<
"]"
937 <<
") by " << thishost <<
" with " << esmtp <<
" ; "
941 << date.
yyyy() <<
" "
942 << time.
hhmmss(
":") <<
" "
949 GSmtp::ServerProtocol::Text::~Text()
955 GSmtp::ServerProtocol::Sender::~Sender()
961 GSmtp::ServerProtocol::Config::Config(
bool with_vrfy_ ,
unsigned int filter_timeout_ ,
962 size_t max_size_ ,
bool authentication_requires_encryption_ ,
bool mail_requires_encryption_ ,
963 bool advertise_tls_if_possible_ ) :
964 with_vrfy(with_vrfy_) ,
965 filter_timeout(filter_timeout_) ,
966 max_size(max_size_) ,
967 authentication_requires_encryption(authentication_requires_encryption_) ,
968 mail_requires_encryption(mail_requires_encryption_) ,
969 disconnect_on_max_size(false) ,
970 advertise_tls_if_possible(advertise_tls_if_possible_)
void secure(const std::string &certificate)
To be called when the transport protocol goes into secure mode.
std::string hhmmss(const char *sep=nullptr) const
Returns the hhmmss string.
An interface used by ServerProtocol to send protocol replies.
static unsigned long toULong(const std::string &s, Limited)
Converts string 's' to an unsigned long.
A subsecond-resolution timestamp based on a time_t.
static bool valid(const std::string &)
Returns true if the string can be decoded without any strict errors.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
ServerProtocolText(const std::string &code_ident, const std::string &thishost, const GNet::Address &peer_address)
Constructor.
A date (dd/mm/yyyy) class.
std::string monthName(bool brief=false) const
Returns the month as a string (in english).
An interface used by ServerProtocol to provide response text strings.
static Offset offset(EpochTime epoch_time)
Returns the offset between UTC and localtime as at 'epoch_time'.
static std::string decode(const std::string &)
Decodes the given string.
static BrokenDownTime local(EpochTime epoch_time)
Converts from epoch time to local broken-down-time.
virtual G::Slot::Signal2< std::string, VerifierStatus > & doneSignal()=0
Returns a signal that is emit()ed when the verify() request is complete.
The GNet::Address class encapsulates a TCP/UDP transport address.
static std::string encode(const std::string &)
Encodes the given string.
virtual G::Slot::Signal4< bool, unsigned long, std::string, std::string > & doneSignal()=0
Returns a signal which is raised once process() has completed.
int monthday() const
Returns the day of the month.
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 void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
A simple time-of-day (hh/mm/ss) class.
static VerifierStatus parse(const std::string &str, std::string &to_ref)
Parses a str() string into a structure and a recipient 'to' address (by reference).
virtual std::string received(const std::string &smtp_peer_name_from_helo, bool authenticated, bool secure) const override
Override from GSmtp::ServerProtocol::Text.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
An asynchronous interface that verifies recipient 'to' addresses.
static EpochTime now()
Returns the current epoch time.
static void trimLeft(std::string &s, const std::string &ws, size_type limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
void init()
Starts the protocol. Use only once after construction.
static std::string escaped(const std::string &, char c_escape, const std::string &specials_in, const std::string &specials_out)
Returns the escape()d string.
A simple interface for a store of secrets used in authentication.
virtual ~ServerProtocol()
Destructor.
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
std::string yyyy() const
Returns the year as a four-digit decimal string.
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...
virtual std::string hello(const std::string &smtp_peer_name_from_helo) const override
Override from GSmtp::ServerProtocol::Text.
static std::string receivedLine(const std::string &smtp_peer_name_from_helo, const std::string &peer_address, const std::string &thishost, bool authenticated, bool secure)
Returns a standard "Received:" line.
std::string weekdayName(bool brief=false) const
Returns an english string representation of the day of the week.
Overload discrimiator for G::Str::toUWhatever()
static std::string offsetString(Offset offset)
Converts the given utc/localtime offset into a five-character "+/-hhmm" string.
A structure containing configuration parameters for ServerProtocol.
static bool secureAcceptCapable()
Returns true if the implementation supports TLS/SSL and a "server" profile has been configured...
An interface used by the ServerProtocol class to assemble and process an incoming message...
virtual std::string greeting() const override
Override from GSmtp::ServerProtocol::Text.
void addTransition(Event event, State from, State to, Action action)
Adds a transition.
bool apply(const char *line_data, size_t line_size, size_t eolsize)
Called on receipt of a line of text from the remote client Returns true.
virtual void onTimeout() override
Override from GNet::TimerBase.
SASL authentication classes.
static bool isULong(const std::string &s)
Returns true if the string can be converted into an unsigned long without throwing an exception...
static std::string encode(const std::string &s, const std::string &line_break)
Encodes the given string.
ServerProtocol(GNet::ExceptionHandler &, Sender &, Verifier &, ProtocolMessage &, const GAuth::Secrets &secrets, Text &text, GNet::Address peer_address, Config config)
Constructor.
static void toUpper(std::string &s)
Replaces all Latin-1 lowercase characters in string 's' by uppercase characters.