X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fapps%2Fclient.cpp;h=96e5b67442bf94046cb01dba31e3a9c25f0fc56b;hb=284fe577c65bbdda151d80e2261617a4c7119ed5;hp=9e85c95e598a4e7c7bb7037898440a2cc29b1848;hpb=890efd9eb1d32033fe3afd088838bde707f3a2bb;p=cassiopeia.git diff --git a/src/apps/client.cpp b/src/apps/client.cpp index 9e85c95..96e5b67 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -6,14 +6,15 @@ #include #include -#include "database.h" -#include "mysql.h" -#include "simpleOpensslSigner.h" +#include "db/database.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 "bios.h" -#include "slipBio.h" -#include "remoteSigner.h" -#include "sslUtil.h" +#include "io/bios.h" +#include "io/slipBio.h" #include "config.h" #ifdef NO_DAEMON @@ -27,14 +28,39 @@ extern std::string sqlHost, sqlUser, sqlPass, sqlDB; extern std::string serialPath; extern std::unordered_map> CAs; +void checkCRLs( std::shared_ptr sign ) { + + logger::note( "Signing CRLs" ); + + for( auto& x : CAs ) { + logger::notef( "Checking: %s ...", x.first ); + + if( !x.second->crlNeedsResign() ) { + logger::warnf( "Skipping Resigning CRL: %s ...", x.second->name ); + continue; + } + + 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 ) { + 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; @@ -45,83 +71,113 @@ 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 ); - ( ( SlipBIO* )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 ) { - std::shared_ptr job = jp->fetchJob(); + time_t current; + time( ¤t ); + + if( lastCRLCheck + 30 * 60 < current ) { + // todo set good log TODO FIXME + sign->setLog( std::shared_ptr( + &std::cout, + []( std::ostream* o ) { + ( void ) o; + } ) ); + checkCRLs( sign ); + lastCRLCheck = current; + } + 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::ofstream* logP = new std::ofstream( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) ); - std::shared_ptr logPtr( - logP, - []( std::ofstream * ptr ) { - ( *ptr ).close(); - delete ptr; - } ); - std::ofstream& log = *logP; - - 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; + 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::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 ); - log << "INFO: message digest: " << cert->md << std::endl; - log << "INFO: profile id: " << cert->profile << std::endl; + cert->wishFrom = job->from; + cert->wishTo = job->to; + 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() ) { + logger::error( "ERROR: Writeback of the certificate failed." ); + jp->failJob( job ); + continue; + } + res->crt_name = fn; - jp->writeBack( job, res ); - log << "FINE: signing done." << std::endl; + jp->writeBack( job, res ); //! \FIXME: Check return value + logger::note( "FINE: signing done." ); if( DAEMON ) { jp->finishJob( job ); @@ -129,29 +185,37 @@ int main( int argc, const char* argv[] ) { continue; } catch( const char* c ) { - log << "ERROR: " << c << std::endl; - } catch( std::string c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); + } catch( std::string& c ) { + logger::error( "ERROR: ", c ); } try { jp->failJob( job ); } catch( const char* c ) { - log << "ERROR: " << c << std::endl; - } catch( std::string c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); + } catch( std::string& c ) { + logger::error( "ERROR: ", c ); } } else if( job->task == "revoke" ) { - std::cout << "Revoking!" << std::endl; - - for( auto& x : CAs ) { - std::cout << " [" << x.first << ']' << std::endl; + try { + auto data = jp->getRevocationInfo( job ); + std::vector serials; + serials.push_back( data.first ); + 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(); + 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 ); } - - sign->revoke( CAs.at( "unassured" ), "12345" ); - jp->finishJob( job ); } else { - log << "Unknown job type" << job->task << std::endl; + logger::errorf( "Unknown job type (\"%s\")", job->task ); jp->failJob( job ); }