]> WPIA git - cassiopeia.git/commitdiff
add: Add more verbose logging
authorFelix Dörre <felix@dogcraft.de>
Tue, 30 Dec 2014 20:52:20 +0000 (21:52 +0100)
committerBenny Baumann <BenBE@geshi.org>
Sat, 24 Jan 2015 17:31:30 +0000 (18:31 +0100)
src/apps/client.cpp
src/apps/signer.cpp
src/database.h
src/mysql.cpp
src/mysql.h
src/record.cpp
src/record.h
src/recordHandler.cpp
src/remoteSigner.cpp
src/remoteSigner.h
src/simpleOpensslSigner.cpp

index 8ed2ca45eacbdd8fe9f1cd4a90f4d7d2c1047894..574160631ac2fdede9b336d03a4d3fc4aa0935eb 100644 (file)
@@ -81,44 +81,80 @@ int main( int argc, const char* argv[] ) {
             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;
+
+        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;
+
         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;
+
+                for( auto& SAN : cert->SANs ) {
+                    log << "INFO: SAN " << SAN->type << ": " << SAN->content;
+                }
+
+                for( auto& AVA : cert->AVAs ) {
+                    log << "INFO: AVA " << AVA->name << ": " << AVA->value;
+                }
 
                 if( !cert ) {
                     std::cout << "wasn't able to load CSR" << std::endl;
-                    return 2;
+                    jp->failJob( job );
+                    continue;
                 }
 
-                std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
+                log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
                 cert->csr_content = readFile( cert->csr );
-                std::cout << cert->csr_content << " content " << std::endl;
+                log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
 
                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
 
                 if( !res ) {
-                    std::cout << "Error no cert came back." << std::endl;
+                    log << "ERROR: The signer failed. There was no certificate." << std::endl;
+                    jp->failJob( job );
                     continue;
                 }
 
-                std::cout << "did it!" << res->certificate << std::endl;
+                log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
+                log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
                 res->crt_name = fn;
                 jp->writeBack( job, res );
-                std::cout << "wrote back" << std::endl;
+                log << "FINE: signing done." << std::endl;
+
+                if( DAEMON ) {
+                    jp->finishJob( job );
+                }
+
+                continue;
             } catch( const char* c ) {
-                std::cerr << "ERROR: " << c << std::endl;
-                return 2;
+                log << "ERROR: " << c << std::endl;
             } catch( std::string c ) {
-                std::cerr << "ERROR: " << c << std::endl;
-                return 2;
+                log << "ERROR: " << c << std::endl;
             }
-        } else {
-            std::cout << "Unknown job type" << job->task << std::endl;
-        }
 
-        if( DAEMON && !jp->finishJob( job ) ) {
-            return 1;
+            try {
+                jp->failJob( job );
+            } catch( const char* c ) {
+                log << "ERROR: " << c << std::endl;
+            } catch( std::string c ) {
+                log << "ERROR: " << c << std::endl;
+            }
+        } else {
+            log << "Unknown job type" << job->task << std::endl;
         }
 
         if( !DAEMON || once ) {
index ec85009c90a1f3ab5f3b67b7724cb46656283ad9..44f05d686f9862069aaf40cedcc13e91f4c8ccc5 100644 (file)
@@ -19,8 +19,6 @@
 #define DAEMON true
 #endif
 
-int handlermain( int argc, const char* argv[] );
-
 extern std::string serialPath;
 extern std::vector<Profile> profiles;
 
index ca90bd447e591a01cd5272ab4b676abf66ccdc70..f23f61012588a297be5d2d577adc36d3716c0bf3 100644 (file)
@@ -6,6 +6,7 @@
 
 struct Job {
     std::string id;
+    std::string warning;
     std::string target;
     std::string task;
     std::string from;
@@ -50,7 +51,8 @@ struct SignedCertificate {
 class JobProvider {
 public:
     virtual std::shared_ptr<Job> fetchJob() = 0;
-    virtual bool finishJob( std::shared_ptr<Job> job ) = 0;
+    virtual void finishJob( std::shared_ptr<Job> job ) = 0;
+    virtual void failJob( std::shared_ptr<Job> job ) = 0;
     virtual std::shared_ptr<TBSCertificate> fetchTBSCert( std::shared_ptr<Job> job ) = 0;
     virtual void writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) = 0;
 };
index bd6929e96f829f4b5eff9a106ea572a94e7e93fe..15478acb553743c315197a95110acd52fe405dd3 100644 (file)
@@ -113,7 +113,7 @@ std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std:
 }
 
 std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
-    std::string q = "SELECT id, targetId, task, executeFrom, executeTo FROM jobs WHERE state='open'";
+    std::string q = "SELECT id, targetId, task, executeFrom, executeTo, warning FROM jobs WHERE state='open' AND warning < 3";
 
     int err = 0;
     std::shared_ptr<MYSQL_RES> res;
@@ -145,6 +145,7 @@ std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
     job->task = std::string( row[2], row[2] + l[2] );
     job->from = std::string( row[3], row[3] + l[3] );
     job->to = std::string( row[4], row[4] + l[4] );
+    job->warning = std::string( row[5], row[5] + l[5] );
 
     for( unsigned int i = 0; i < num; i++ ) {
         printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
@@ -171,18 +172,29 @@ std::string MySQLJobProvider::escape_string( const std::string& target ) {
     return result;
 }
 
-bool MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
+void MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
     if( !conn ) {
-        return false;
+        throw "Not connected!";
     }
 
     std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
 
     if( query( q ).first ) {
-        return false;
+        throw "No database entry found.";
     }
 
-    return true;
+}
+
+void MySQLJobProvider::failJob( std::shared_ptr<Job> job ) {
+    if( !conn ) {
+        throw "Not connected!";
+    }
+
+    std::string q = "UPDATE jobs SET warning = warning + 1 WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
+
+    if( query( q ).first ) {
+        throw "No database entry found.";
+    }
 }
 
 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
index da13e586be830de0225bfc38d2f3e37815a7685e..b1f78cef90bb8c051737a050ba5ee854fe27011b 100644 (file)
@@ -31,7 +31,8 @@ public:
 
 public:
     std::shared_ptr<Job> fetchJob();
-    bool finishJob( std::shared_ptr<Job> job );
+    void finishJob( std::shared_ptr<Job> job );
+    void failJob( std::shared_ptr<Job> job );
     std::shared_ptr<TBSCertificate> fetchTBSCert( std::shared_ptr<Job> job );
     void writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res );
 };
index 5993b6065eadec6409c85552239660919ce0bb4e..8f81d02e683679fb764db59d16527e023d38f565 100644 (file)
@@ -23,13 +23,18 @@ std::string toHexAndChecksum( const std::string& src ) {
     return ss.str();
 }
 
-void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio ) {
+void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio, std::shared_ptr<std::ostream> log ) {
     head.payloadLength = data.size();
     std::string s;
     s += head.packToString();
     s += data;
 
     std::string res = toHexAndChecksum( s );
+
+    if( log ) {
+        ( *log.get() ) << "FINE: RECORD output: " << res << std::endl;
+    }
+
     bio->write( res.data(), res.size() );
 }
 
@@ -47,7 +52,11 @@ int32_t fromHexDigit( char c ) {
     return res;
 }
 
-std::string parseCommand( RecordHeader& head, const std::string& input ) {
+std::string parseCommand( RecordHeader& head, const std::string input, std::shared_ptr<std::ostream> log ) {
+    if( log ) {
+        ( *log.get() ) << "FINE: RECORD input: " << input << std::endl;
+    }
+
     int32_t dlen = ( input.size() - 2 ) / 2;
     char checksum = 0;
     bool error = false;
index 03746eda5704d92a65af0ecccfbaeedf50bf8055..d3173b83220422627f6fca36e93fb0c442d14ebe 100644 (file)
@@ -88,6 +88,6 @@ public:
 
 };
 
-std::string parseCommand( RecordHeader& head, const std::string& input );
+std::string parseCommand( RecordHeader& head, const std::string input, std::shared_ptr<std::ostream> log );
 
-void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio );
+void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio, std::shared_ptr<std::ostream> log );
index 7412b0c844d66230b81acdfb16a446ed4cfb4dbf..dd5201beaebc26e605033a12a9beed7657976363 100644 (file)
@@ -8,6 +8,8 @@
 #include <unistd.h>
 
 #include <iostream>
+#include <fstream>
+#include <ctime>
 
 #include <openssl/ssl.h>
 
@@ -35,10 +37,24 @@ public:
     DefaultRecordHandler* parent;
     std::shared_ptr<Signer> signer;
 
+    std::shared_ptr<std::ofstream> log;
+
     RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
         tbs( new TBSCertificate() ) {
         this->parent = parent;
         this->signer = signer;
+        time_t c_time;
+
+        if( time( &c_time ) == -1 ) {
+            throw "Error while fetching time?";
+        }
+
+        log = std::shared_ptr<std::ofstream>(
+            new std::ofstream( std::string( "logs/log_" ) + std::to_string( c_time ) ),
+            []( std::ofstream * ptr ) {
+                ptr->close();
+                delete ptr;
+            } );
 
         ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
         std::shared_ptr<BIO> bio(
@@ -58,7 +74,7 @@ public:
         rh.flags = 0;
         rh.command_count = 0; // TODO i++
         rh.totalLength = payload.size();
-        sendCommand( rh, payload, io );
+        sendCommand( rh, payload, io, log );
     }
 
     void work() {
@@ -74,10 +90,13 @@ public:
 
         try {
             RecordHeader head;
-            std::string payload = parseCommand( head, content );
+            std::string payload = parseCommand( head, content, log );
             execute( head, payload );
         } catch( const char* msg ) {
-            std::cout << msg << std::endl;
+            if( log ) {
+                ( *log ) << "ERROR: " << msg << std::endl;
+            }
+
             parent->reset();
             return;
         }
@@ -92,7 +111,7 @@ public:
         case RecordHeader::SignerCommand::SET_CSR: // setCSR
             tbs->csr_content = data;
             tbs->csr_type = "CSR";
-            std::cout << "CSR read" << std::endl;
+            ( *log ) << "INFO: CSR read:" << std::endl << tbs->csr_content;
             break;
 
         case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
@@ -136,8 +155,8 @@ public:
 
         case RecordHeader::SignerCommand::SIGN:
             result = signer->sign( tbs );
-            std::cout << "res: " << result->certificate << std::endl;
-            result->log = "I am a dummy log.\nI signed that thing ;-) \n";
+            ( *log ) << "INFO: signlog: " << result->log << std::endl;
+            ( *log ) << "INFO: res: " << result->certificate << std::endl;
             respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
             break;
 
@@ -147,7 +166,7 @@ public:
             }
 
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
-                std::cout << "SSL close failed" << std::endl;
+                ( *log ) << "ERROR: SSL close failed" << std::endl;
             }
 
             break;
index a17f515c566078f1be6b0425fc66f0eece7a57f6..71714003cdabd8812e41d57779834d9f2fba6587 100644 (file)
@@ -13,7 +13,7 @@ void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& h
     head.command = ( uint16_t ) cmd;
     head.command_count++;
     head.totalLength = data.size();
-    sendCommand( head, data, bio );
+    sendCommand( head, data, bio, log );
 
 }
 
@@ -72,7 +72,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
             }
 
             RecordHeader head;
-            std::string payload = parseCommand( head, std::string( buffer.data(), length ) );
+            std::string payload = parseCommand( head, std::string( buffer.data(), length ), log );
 
             switch( ( RecordHeader::SignerResult ) head.command ) {
             case RecordHeader::SignerResult::CERTIFICATE:
@@ -96,3 +96,6 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     return result;
 }
 
+void RemoteSigner::setLog( std::shared_ptr<std::ostream> target ) {
+    this->log = target;
+}
index 36c9339989376875b8c1541a251c518dbc3c42c2..8cc3c72f29631f54732080f98ff3bf04b6d8abfe 100644 (file)
@@ -13,10 +13,12 @@ class RemoteSigner : public Signer {
 private:
     std::shared_ptr<BIO> target;
     std::shared_ptr<SSL_CTX> ctx;
+    std::shared_ptr<std::ostream> log;
     int count = 0;
     void send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& head, RecordHeader::SignerCommand cmd, std::string data );
 public:
     RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx );
     ~RemoteSigner();
     std::shared_ptr<SignedCertificate> sign( std::shared_ptr<TBSCertificate> cert );
+    void setLog( std::shared_ptr<std::ostream> target );
 };
index 749131ba37e03a0f0a8ac134b484129afd7b88cf..83c560974f1cb3b6b13ec77cb6878790ef815766 100644 (file)
@@ -1,6 +1,7 @@
 #include "simpleOpensslSigner.h"
 
 #include <iostream>
+#include <sstream>
 
 #include <openssl/ssl.h>
 #include <openssl/err.h>
@@ -66,10 +67,14 @@ std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
 }
 
 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
+    std::stringstream signlog;
+
     if( !prof.ca ) {
         throw "CA-key not found";
     }
 
+    signlog << "FINE: CA-key is correctly loaded." << std::endl;
+
     std::shared_ptr<X509Req> req;
 
     if( cert->csr_type == "SPKAC" ) {
@@ -87,7 +92,7 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     } else if( i == 0 ) {
         throw "Signature did not match";
     } else {
-        std::cerr << "Signature ok" << std::endl;
+        signlog << "FINE: Signature ok" << std::endl;
     }
 
     // Construct the Certificate
@@ -106,6 +111,8 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     }
 
     for( std::shared_ptr<AVA> a : cert->AVAs ) {
+        signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
+
         if( a->name == "CN" ) {
             c.addRDN( NID_commonName, a->value );
         } else if( a->name == "EMAIL" ) {
@@ -136,9 +143,11 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     std::shared_ptr<BIGNUM> ser = nextSerial( profile );
     c.setSerialNumber( ser.get() );
     c.setTimes( 0, 60 * 60 * 24 * 10 );
+    signlog << "FINE: Setting extensions." << std::endl;
     c.setExtensions( prof.ca, cert->SANs );
-
+    signlog << "FINE: Signed" << std::endl;
     std::shared_ptr<SignedCertificate> output = c.sign( prof.caKey, cert->md );
-
+    signlog << "FINE: all went well" << std::endl;
+    output->log = signlog.str();
     return output;
 }