]> WPIA git - cassiopeia.git/blobdiff - src/io/recordHandler.cpp
chg: Modify the code to use the new logger
[cassiopeia.git] / src / io / recordHandler.cpp
index 67214e327496d904e083591a5a29cf662a007446..0e63805e98f8e5e57eb2cfd06913421395ec147b 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <openssl/ssl.h>
 
+#include "util.h"
 #include "io/record.h"
 #include "io/opensslBIO.h"
 #include "io/slipBio.h"
 #include "db/database.h"
 #include "crypto/remoteSigner.h"
 #include "crypto/sslUtil.h"
-
 #include "crypto/simpleOpensslSigner.h"
 
+#include "log/logger.hpp"
+
 extern std::vector<Profile> profiles;
 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
 
@@ -40,7 +42,7 @@ public:
     RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
         sessid( 0 ),
         lastCommandCount( 0 ),
-        tbs( new TBSCertificate() ){
+        tbs( new TBSCertificate() ) {
         this->parent = parent;
         this->signer = signer;
         time_t c_time;
@@ -49,12 +51,7 @@ public:
             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;
-            } );
+        log = openLogfile( std::string( "logs/log_" ) + std::to_string( c_time ) );
 
         ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
         std::shared_ptr<BIO> bio(
@@ -82,7 +79,7 @@ public:
         int res = io->read( buffer.data(), buffer.capacity() );
 
         if( res <= 0 ) {
-            ( *log ) << "Stream error, resetting SSL" << std::endl;
+            logger::error( "Stream error, resetting SSL" );
             parent->reset();
             return;
         }
@@ -95,7 +92,7 @@ public:
             execute( head, payload );
         } catch( const char* msg ) {
             if( log ) {
-                ( *log ) << "ERROR: " << msg << std::endl;
+                logger::error( "ERROR: ", msg );
             }
 
             parent->reset();
@@ -112,13 +109,13 @@ public:
         case RecordHeader::SignerCommand::SET_CSR:
             tbs->csr_content = data;
             tbs->csr_type = "CSR";
-            ( *log ) << "INFO: CSR read:" << std::endl << tbs->csr_content;
+            logger::note( "INFO: CSR read:\n", tbs->csr_content );
             break;
 
         case RecordHeader::SignerCommand::SET_SPKAC:
             tbs->csr_content = data;
             tbs->csr_type = "SPKAC";
-            ( *log ) << "INFO: SPKAC read:" << std::endl << tbs->csr_content;
+            logger::note( "INFO: SPKAC read:\n", tbs->csr_content );
             break;
 
         case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
@@ -171,8 +168,8 @@ public:
 
         case RecordHeader::SignerCommand::SIGN:
             result = signer->sign( tbs );
-            ( *log ) << "INFO: signlog: " << result->log << std::endl;
-            ( *log ) << "INFO: res: " << result->certificate << std::endl;
+            logger::note( "INFO: signlog:\n", result->log );
+            logger::note( "INFO: res:\n", result->certificate );
             respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
             break;
 
@@ -183,9 +180,11 @@ public:
             }
 
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
-                ( *log ) << "ERROR: SSL close failed" << std::endl;
+                logger::warn( "ERROR: SSL shutdown failed." );
             }
 
+            parent->reset(); // Connection ended
+
             break;
 
         case RecordHeader::SignerCommand::ADD_SERIAL:
@@ -195,7 +194,7 @@ public:
         case RecordHeader::SignerCommand::REVOKE: {
             std::string ca = data;
             auto reqCA = CAs.at( ca );
-            ( *log ) << "CA found" << std::endl;
+            logger::note( "CA found" );
             std::shared_ptr<CRL> crl;
             std::string date;
             std::tie<std::shared_ptr<CRL>, std::string>( crl, date ) = signer->revoke( reqCA, serials );
@@ -211,9 +210,10 @@ public:
             respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
 
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
-                ( *log ) << "ERROR: SSL close failed" << std::endl;
+                logger::error( "ERROR: SSL shutdown failed." );
             }
 
+            parent->reset(); // Connection ended
             break;
         }
 
@@ -233,7 +233,7 @@ void DefaultRecordHandler::reset() {
 
 void DefaultRecordHandler::handle() {
     if( !currentSession ) {
-        std::cout << "session allocated" << std::endl;
+        logger::note( "New session allocated." );
         currentSession = std::shared_ptr<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
     }