X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fapps%2Fclient.cpp;h=f044c2b84a43d42394ddf457f4b33aad3f06726d;hb=da9f337a893bd317460118f89efa83a3427f797f;hp=ca9432e453fa8e4449f400f08d46c971b6ad8f52;hpb=ecdc4c456ebcc3e0871b765d1f4d15e73520d2a3;p=cassiopeia.git diff --git a/src/apps/client.cpp b/src/apps/client.cpp index ca9432e..f044c2b 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -16,6 +16,7 @@ #include "io/bios.h" #include "io/slipBio.h" #include "config.h" +#include #ifdef NO_DAEMON #define DAEMON false @@ -45,19 +46,20 @@ void checkCRLs( std::shared_ptr sign ) { try { std::vector serials; std::pair, std::string> rev = sign->revoke( x.second, serials ); - } catch( const char* c ) { - logger::error( "Exception: ", c ); + } catch( const std::exception& e ) { + logger::error( "Exception: ", e.what() ); } } } -int main( int argc, const char* argv[] ) { +int main( int argc, const char *argv[] ) { bool once = false; bool resetOnly = false; if( argc == 2 && std::string( "--once" ) == argv[1] ) { once = true; } + if( argc == 2 && std::string( "--reset" ) == argv[1] ) { resetOnly = true; } @@ -65,7 +67,7 @@ int main( int argc, const char* argv[] ) { std::string path; #ifdef NDEBUG - path = "/etc/cacert/cassiopeia/cassiopeia.conf"; + path = "/etc/wpia/cassiopeia/cassiopeia.conf"; #else path = "config.txt"; #endif @@ -79,10 +81,12 @@ int main( int argc, const char* argv[] ) { logger::fatal( "Error: no serial device is given!" ); return -1; } + std::shared_ptr jp = std::make_shared( sqlHost, sqlUser, sqlPass, sqlDB ); std::shared_ptr b = openSerial( serialPath ); - std::shared_ptr slip1( BIO_new( toBio() ), BIO_free ); - static_cast( slip1->ptr )->setTarget( std::make_shared( b ), false ); + std::shared_ptr m( toBio(), BIO_meth_free ); + std::shared_ptr slip1( BIO_new( m.get() ), BIO_free ); + static_cast( slip1->ptr )->setTarget( std::make_shared( b ), false ); auto sign = std::make_shared( slip1, generateSSLContext( false ) ); // std::shared_ptr sign( new SimpleOpensslSigner() ); @@ -96,7 +100,7 @@ int main( int argc, const char* argv[] ) { time_t lastCRLCheck = 0; while( true ) { - try { + try { time_t current; time( ¤t ); @@ -112,20 +116,21 @@ int main( int argc, const char* argv[] ) { } std::shared_ptr job; + try { job = jp->fetchJob(); - } catch ( std::exception &e ){ - logger::errorf ( "Exception while fetchJob: %s", e.what() ); - } + } catch( std::exception& e ) { + logger::errorf( "Exception while fetchJob: %s", e.what() ); + } + if( !job ) { - logger::note( "Nothing to work on." ); sleep( 5 ); continue; } std::shared_ptr logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) ); - logger::logger_set log_set({logger::log_target(*logPtr, logger::level::debug)}, logger::auto_register::on); + logger::logger_set log_set( {logger::log_target( *logPtr, logger::level::debug )}, logger::auto_register::on ); logger::note( "TASK ID: ", job->id ); logger::note( "TRY: ", job->warning ); @@ -135,6 +140,13 @@ int main( int argc, const char* argv[] ) { if( job->task == "sign" ) { try { std::shared_ptr cert = jp->fetchTBSCert( job ); + + if( !cert ) { + logger::error( "Unable to load CSR" ); + jp->failJob( job ); + continue; + } + cert->wishFrom = job->from; cert->wishTo = job->to; logger::note( "INFO: Message Digest: ", cert->md ); @@ -148,12 +160,6 @@ int main( int argc, const char* argv[] ) { logger::notef( "INFO: AVA %s: %s", AVA->name, AVA->value ); } - if( !cert ) { - logger::error( "Unable to load CSR" ); - jp->failJob( job ); - continue; - } - logger::notef( "FINE: Found the CSR at '%s'", cert->csr ); cert->csr_content = readFile( keyDir + "/../" + cert->csr ); logger::note( "FINE: CSR content:\n", cert->csr_content ); @@ -166,8 +172,9 @@ int main( int argc, const char* argv[] ) { continue; } - logger::note( "FINE: CERTIFICATE LOG:\n", res->log ); - logger::note( "FINE: CERTIFICATE:\n", res->certificate ); + logger::note( "FINE: CERTIFICATE LOG:\n", res->log, + "FINE: CERTIFICATE:\n", res->certificate ); + std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir ); if( fn.empty() ) { @@ -185,35 +192,31 @@ int main( int argc, const char* argv[] ) { } continue; - } catch( const char* c ) { - logger::error( "ERROR: ", c ); - } catch( std::string& c ) { - logger::error( "ERROR: ", c ); + } catch( std::exception& c ) { + logger::error( "ERROR: ", c.what() ); } try { jp->failJob( job ); - } catch( const char* c ) { - logger::error( "ERROR: ", c ); - } catch( std::string& c ) { - logger::error( "ERROR: ", c ); + } catch( std::exception& c ) { + logger::error( "ERROR: ", c.what() ); } } else if( job->task == "revoke" ) { try { + logger::note( "revoking" ); auto data = jp->getRevocationInfo( job ); std::vector serials; serials.push_back( data.first ); + logger::note( "revoking" ); std::pair, std::string> rev = sign->revoke( CAs.at( data.second ), serials ); std::string date = rev.second; - const unsigned char* pos = ( const unsigned char* ) date.data(); + const unsigned char *pos = ( const unsigned char * ) date.data(); std::shared_ptr time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free ); jp->writeBackRevocation( job, timeToString( time ) ); jp->finishJob( job ); - } catch( const char* c ) { - logger::error( "Exception: ", c ); - } catch( const std::string& c ) { - logger::error( "Exception: ", c ); + } catch( const std::exception& c ) { + logger::error( "Exception: ", c.what() ); } } else { logger::errorf( "Unknown job type (\"%s\")", job->task ); @@ -223,12 +226,8 @@ int main( int argc, const char* argv[] ) { if( !DAEMON || once ) { return 0; } - } catch( const char* c ) { - logger::error( "Exception: ", c ); - } catch( const std::string& c ) { - logger::error( "Exception: ", c ); - } catch ( std::exception &e ){ - logger::errorf ( "Exception while fetchJob: %s", e.what() ); + } catch( std::exception& e ) { + logger::errorf( "std::exception in mainloop: %s", e.what() ); } }