X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fapps%2Fclient.cpp;h=96e5b67442bf94046cb01dba31e3a9c25f0fc56b;hb=284fe577c65bbdda151d80e2261617a4c7119ed5;hp=5cbacdc264481a1e13515825eae6bc88fe801042;hpb=bfd47498091c1062c5d2c07884003c2525b60ca1;p=cassiopeia.git diff --git a/src/apps/client.cpp b/src/apps/client.cpp index 5cbacdc..96e5b67 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -7,10 +7,11 @@ #include #include "db/database.h" -#include "db/mysql.h" +#include "db/psql.h" #include "crypto/simpleOpensslSigner.h" #include "crypto/remoteSigner.h" #include "crypto/sslUtil.h" +#include "log/logger.hpp" #include "util.h" #include "io/bios.h" #include "io/slipBio.h" @@ -28,35 +29,38 @@ extern std::string serialPath; extern std::unordered_map> CAs; void checkCRLs( std::shared_ptr sign ) { - std::cout << "Signing CRLs" << std::endl; - for( auto x : CAs ) { - std::cout << "Checking: " << x.first << std::endl; + logger::note( "Signing CRLs" ); + + for( auto& x : CAs ) { + logger::notef( "Checking: %s ...", x.first ); if( !x.second->crlNeedsResign() ) { - std::cout << "Skipping Resigning CRL: " + x.second->name << std::endl; + logger::warnf( "Skipping Resigning CRL: %s ...", x.second->name ); continue; } - std::cout << "Resigning CRL: " + x.second->name << std::endl; + logger::notef( "Resigning CRL: %s ...", x.second->name ); try { std::vector serials; std::pair, std::string> rev = sign->revoke( x.second, serials ); } catch( const char* c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } } } int main( int argc, const char* argv[] ) { - ( void ) argc; - ( void ) argv; bool once = false; + bool resetOnly = false; - if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) { + if( argc == 2 && std::string( "--once" ) == argv[1] ) { once = true; } + if( argc == 2 && std::string( "--reset" ) == argv[1] ) { + resetOnly = true; + } std::string path; @@ -67,21 +71,28 @@ int main( int argc, const char* argv[] ) { #endif if( parseConfig( path ) != 0 ) { + logger::fatal( "Error: Could not parse the configuration file." ); return -1; } if( serialPath == "" ) { - std::cout << "Error: no serial device is given" << std::endl; + logger::fatal( "Error: no serial device is given!" ); return -1; } - - std::shared_ptr jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) ); + 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::shared_ptr( new OpensslBIOWrapper( b ) ) ); - std::shared_ptr sign( new RemoteSigner( slip1, generateSSLContext( false ) ) ); + static_cast( slip1->ptr )->setTarget( std::make_shared( b ), false ); + auto sign = std::make_shared( slip1, generateSSLContext( false ) ); // std::shared_ptr sign( new SimpleOpensslSigner() ); + if( resetOnly ) { + std::cout << "Doing BIO reset" << std::endl; + int result = BIO_reset( slip1.get() ); + std::cout << "Did BIO reset, result " << result << ", exiting." << std::endl; + return result; + } + time_t lastCRLCheck = 0; while( true ) { @@ -92,78 +103,81 @@ int main( int argc, const char* argv[] ) { // todo set good log TODO FIXME sign->setLog( std::shared_ptr( &std::cout, - []( std::ostream * o ) { + []( std::ostream* o ) { ( void ) o; } ) ); checkCRLs( sign ); lastCRLCheck = current; } - std::shared_ptr job = jp->fetchJob(); - + std::shared_ptr job; + try { + job = jp->fetchJob(); + } catch ( std::exception &e ){ + logger::errorf ( "Exception while fetchJob: %s", e.what() ); + } if( !job ) { - std::cout << "Nothing to work on" << std::endl; + 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" ) ); - std::ofstream& log = *( logPtr.get() ); + logger::logger_set log_set({logger::log_target(*logPtr, logger::level::debug)}, logger::auto_register::on); - sign->setLog( logPtr ); - log << "TASK ID: " << job->id << std::endl; - log << "TRY: " << job->warning << std::endl; - log << "TARGET: " << job->target << std::endl; - log << "TASK: " << job->task << std::endl << std::endl; + logger::note( "TASK ID: ", job->id ); + logger::note( "TRY: ", job->warning ); + logger::note( "TARGET: ", job->target ); + logger::note( "TASK: ", job->task ); if( job->task == "sign" ) { try { std::shared_ptr cert = jp->fetchTBSCert( job ); cert->wishFrom = job->from; cert->wishTo = job->to; - log << "INFO: message digest: " << cert->md << std::endl; - log << "INFO: profile id: " << cert->profile << std::endl; + logger::note( "INFO: Message Digest: ", cert->md ); + logger::note( "INFO: Profile ID: ", cert->profile ); for( auto& SAN : cert->SANs ) { - log << "INFO: SAN " << SAN->type << ": " << SAN->content; + logger::notef( "INFO: SAN %s: %s", SAN->type, SAN->content ); } for( auto& AVA : cert->AVAs ) { - log << "INFO: AVA " << AVA->name << ": " << AVA->value; + logger::notef( "INFO: AVA %s: %s", AVA->name, AVA->value ); } if( !cert ) { - std::cout << "wasn't able to load CSR" << std::endl; + logger::error( "Unable to load CSR" ); jp->failJob( job ); continue; } - log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl; + logger::notef( "FINE: Found the CSR at '%s'", cert->csr ); cert->csr_content = readFile( keyDir + "/../" + cert->csr ); - log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl; + logger::note( "FINE: CSR content:\n", cert->csr_content ); std::shared_ptr res = sign->sign( cert ); if( !res ) { - log << "ERROR: The signer failed. There was no certificate." << std::endl; + logger::error( "ERROR: The signer failed. No certificate was returned." ); jp->failJob( job ); continue; } - log << "FINE: CERTIFICATE LOG: " << res->log << std::endl; - log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl; + logger::note( "FINE: CERTIFICATE LOG:\n", res->log ); + logger::note( "FINE: CERTIFICATE:\n", res->certificate ); std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir ); if( fn.empty() ) { - log << "ERROR: Writeback of the certificate failed." << std::endl; + logger::error( "ERROR: Writeback of the certificate failed." ); jp->failJob( job ); continue; } res->crt_name = fn; jp->writeBack( job, res ); //! \FIXME: Check return value - log << "FINE: signing done." << std::endl; + logger::note( "FINE: signing done." ); if( DAEMON ) { jp->finishJob( job ); @@ -171,17 +185,17 @@ int main( int argc, const char* argv[] ) { continue; } catch( const char* c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } catch( std::string& c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } try { jp->failJob( job ); } catch( const char* c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } catch( std::string& c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } } else if( job->task == "revoke" ) { try { @@ -196,12 +210,12 @@ int main( int argc, const char* argv[] ) { jp->writeBackRevocation( job, timeToString( time ) ); jp->finishJob( job ); } catch( const char* c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } catch( const std::string& c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } } else { - log << "Unknown job type" << job->task << std::endl; + logger::errorf( "Unknown job type (\"%s\")", job->task ); jp->failJob( job ); }