]> WPIA git - cassiopeia.git/blobdiff - src/apps/client.cpp
chg: Modify the code to use the new logger
[cassiopeia.git] / src / apps / client.cpp
index 5258b9abad9b887e48b966cf1a46e35c3b2a895e..d7cda514b5bcaa8830c75156bf284b77089f96d0 100644 (file)
@@ -11,6 +11,7 @@
 #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"
@@ -27,12 +28,33 @@ extern std::string sqlHost, sqlUser, sqlPass, sqlDB;
 extern std::string serialPath;
 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
 
+void checkCRLs( std::shared_ptr<Signer> 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<std::string> serials;
+            std::pair<std::shared_ptr<CRL>, 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;
 
-    if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
+    if( argc == 2 && std::string( "--once" ) == argv[1] ) {
         once = true;
     }
 
@@ -45,83 +67,104 @@ 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<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
     std::shared_ptr<BIO> b = openSerial( serialPath );
     std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
-    ( ( SlipBIO* )slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
+    static_cast<SlipBIO*>( slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
     std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
     // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
 
+    time_t lastCRLCheck = 0;
+
     while( true ) {
+        time_t current;
+        time( &current );
+
+        if( lastCRLCheck + 30 * 60 < current ) {
+            // todo set good log TODO FIXME
+            sign->setLog( std::shared_ptr<std::ostream>(
+                &std::cout,
+                []( std::ostream * o ) {
+                    ( void ) o;
+                } ) );
+            checkCRLs( sign );
+            lastCRLCheck = current;
+        }
+
         std::shared_ptr<Job> job = jp->fetchJob();
 
         if( !job ) {
-            std::cout << "Nothing to work on" << std::endl;
+            logger::debug( "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<std::ofstream> logPtr(
-            logP,
-            []( std::ofstream * ptr ) {
-                ( *ptr ).close();
-                delete ptr;
-            } );
-        std::ofstream& log = *logP;
+        std::shared_ptr<std::ofstream> logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
+
+        std::ofstream& log = *( logPtr.get() );
 
         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<TBSCertificate> 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<SignedCertificate> 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,17 +172,17 @@ 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" ) {
             try {
@@ -154,10 +197,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 ) {
+                logger::error( "Exception: ", c );
             }
         } else {
-            log << "Unknown job type" << job->task << std::endl;
+            logger::errorf( "Unknown job type (\"%s\")", job->task );
             jp->failJob( job );
         }