]> WPIA git - cassiopeia.git/blobdiff - src/db/mysql.cpp
upd: fix problem with tm-initializer
[cassiopeia.git] / src / db / mysql.cpp
index 304d4963dcaed689e54751ca1b6ed756405e6f80..e8f1d4dd44a01cdc33358d53b19d63d6dc93e732 100644 (file)
@@ -5,11 +5,13 @@
 #include <iostream>
 
 #include <mysql/errmsg.h>
+#include <log/logger.hpp>
 
 //This static variable exists to handle initializing and finalizing the MySQL driver library
 std::shared_ptr<int> MySQLJobProvider::lib_ref(
     //Initializer: Store the return code as a pointer to an integer
     new int( mysql_library_init( 0, NULL, NULL ) ),
+
     //Finalizer: Check the pointer and free resources
     []( int* ref ) {
         if( !ref ) {
@@ -30,25 +32,14 @@ std::shared_ptr<int> MySQLJobProvider::lib_ref(
 
 MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
     if( !lib_ref || *lib_ref ) {
-        throw "MySQL library not initialized!";
+        throw std::runtime_error("MySQL library not initialized!");
     }
 
     connect( server, user, password, database );
 }
 
-MySQLJobProvider::~MySQLJobProvider() {
-    disconnect();
-}
-
 bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
-    if( conn ) {
-        if( !disconnect() ) {
-            return false;
-        }
-
-        conn.reset();
-    }
-
+    disconnect();
     conn = _connect( server, user, password, database );
 
     return !!conn;
@@ -58,13 +49,13 @@ std::shared_ptr<MYSQL> MySQLJobProvider::_connect( const std::string& server, co
     MYSQL* tmp( mysql_init( NULL ) );
 
     if( !tmp ) {
-        return std::shared_ptr<MYSQL>();
+        return nullptr;
     }
 
     tmp = mysql_real_connect( tmp, server.c_str(), user.c_str(), password.c_str(), database.c_str(), 3306, NULL, CLIENT_COMPRESS );
 
     if( !tmp ) {
-        return std::shared_ptr<MYSQL>();
+        return nullptr;
     }
 
     auto l = lib_ref;
@@ -95,7 +86,7 @@ std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std:
     int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
 
     if( err ) {
-        throw std::string( "MySQL error: " ) + mysql_error( this->conn.get() );
+        throw std::runtime_error(std::string( "MySQL error: " ) + mysql_error( this->conn.get() ));
     }
 
     auto c = conn;
@@ -121,23 +112,21 @@ std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
     std::tie( err, res ) = query( q );
 
     if( err ) {
-        return std::shared_ptr<Job>();
+        return nullptr;
     }
 
-    unsigned int num = mysql_num_fields( res.get() );
-
     MYSQL_ROW row = mysql_fetch_row( res.get() );
 
     if( !row ) {
-        return std::shared_ptr<Job>();
+        return nullptr;
     }
 
-    std::shared_ptr<Job> job( new Job() );
+    auto job = std::make_shared<Job>();
 
     unsigned long* l = mysql_fetch_lengths( res.get() );
 
     if( !l ) {
-        return std::shared_ptr<Job>();
+        return nullptr;
     }
 
     job->id = std::string( row[0], row[0] + l[0] );
@@ -147,18 +136,14 @@ std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
     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" );
-    }
-
-    printf( "\n" );
+    logger::notef( "Got a job: (id=%s, target=%s, task=%s, from=%s, to=%s, warnings=%s)", job->id, job->target, job->task, job->from, job->to, job->warning );
 
     return job;
 }
 
 std::string MySQLJobProvider::escape_string( const std::string& target ) {
     if( !conn ) {
-        throw "Not connected!";
+        throw std::runtime_error("Not connected!");
     }
 
     std::string result;
@@ -174,31 +159,30 @@ std::string MySQLJobProvider::escape_string( const std::string& target ) {
 
 void MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
     if( !conn ) {
-        throw "Not connected!";
+        throw std::runtime_error("Not connected!");
     }
 
     std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
 
     if( query( q ).first ) {
-        throw "No database entry found.";
+        throw std::runtime_error("No database entry found.");
     }
-
 }
 
 void MySQLJobProvider::failJob( std::shared_ptr<Job> job ) {
     if( !conn ) {
-        throw "Not connected!";
+        throw std::runtime_error("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.";
+        throw std::runtime_error("No database entry found.");
     }
 }
 
 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
-    std::shared_ptr<TBSCertificate> cert = std::shared_ptr<TBSCertificate>( new TBSCertificate() );
+    auto cert = std::make_shared<TBSCertificate>();
     std::string q = "SELECT md, profile, csr_name, csr_type, keyname FROM certs INNER JOIN profiles ON profiles.id = certs.profile WHERE certs.id='" + this->escape_string( job->target ) + "'";
 
     int err = 0;
@@ -208,19 +192,19 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
     std::tie( err, res ) = query( q );
 
     if( err ) {
-        return std::shared_ptr<TBSCertificate>();
+        return nullptr;
     }
 
     MYSQL_ROW row = mysql_fetch_row( res.get() );
 
     if( !row ) {
-        return std::shared_ptr<TBSCertificate>();
+        return nullptr;
     }
 
     unsigned long* l = mysql_fetch_lengths( res.get() );
 
     if( !l ) {
-        return std::shared_ptr<TBSCertificate>();
+        return nullptr;
     }
 
     std::string profileName = std::string( row[4], row[4] + l[4] );
@@ -244,7 +228,7 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
 
     if( err ) {
         std::cout << mysql_error( this->conn.get() );
-        return std::shared_ptr<TBSCertificate>();
+        return nullptr;
     }
 
     std::cout << "Fetching SANs" << std::endl;
@@ -253,10 +237,10 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
         unsigned long* l = mysql_fetch_lengths( res.get() );
 
         if( !l ) {
-            return std::shared_ptr<TBSCertificate>();
+            return nullptr;
         }
 
-        std::shared_ptr<SAN> nSAN = std::shared_ptr<SAN>( new SAN() );
+        auto nSAN = std::make_shared<SAN>();
         nSAN->content = std::string( row[0], row[0] + l[0] );
         nSAN->type = std::string( row[1], row[1] + l[1] );
         cert->SANs.push_back( nSAN );
@@ -267,7 +251,7 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
 
     if( err ) {
         std::cout << mysql_error( this->conn.get() );
-        return std::shared_ptr<TBSCertificate>();
+        return nullptr;
 
     }
 
@@ -275,10 +259,10 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
         unsigned long* l = mysql_fetch_lengths( res.get() );
 
         if( !l ) {
-            return std::shared_ptr<TBSCertificate>();
+            return nullptr;
         }
 
-        std::shared_ptr<AVA> nAVA = std::shared_ptr<AVA>( new AVA() );
+        auto nAVA = std::make_shared<AVA>();
         nAVA->name = std::string( row[0], row[0] + l[0] );
         nAVA->value = std::string( row[1], row[1] + l[1] );
         cert->AVAs.push_back( nAVA );
@@ -289,7 +273,7 @@ std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<
 
 void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
     if( !conn ) {
-        throw "Error while writing back";
+        throw std::runtime_error("Error while writing back");
     }
 
     std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'";
@@ -299,7 +283,7 @@ void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<Sign
     std::tie( err, resu ) = query( id );
 
     if( err ) {
-        throw "Error while looking ca cert id";
+        throw std::runtime_error("Error while looking ca cert id");
     }
 
     MYSQL_ROW row = mysql_fetch_row( resu.get() );
@@ -308,13 +292,7 @@ void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<Sign
     std::string read_id;
 
     if( !row || !l ) {
-        if( query( "INSERT INTO cacerts SET keyname= '" + this->escape_string( res->ca_name ) + "', subroot = 0" ).first ) {
-            throw "Error while inserting new ca cert";
-        }
-
-        my_ulonglong insert_id = mysql_insert_id( conn.get() );
-
-        read_id = std::to_string( insert_id );
+        throw std::runtime_error("Error while inserting new ca cert not found");
     } else {
         read_id = std::string( row[0], row[0] + l[0] );
     }
@@ -323,7 +301,7 @@ void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<Sign
     // TODO write more thingies back
 
     if( query( q ).first ) {
-        throw "Error while writing back";
+        throw std::runtime_error("Error while writing back");
     }
 }
 
@@ -334,14 +312,14 @@ std::pair<std::string, std::string> MySQLJobProvider::getRevocationInfo( std::sh
     std::tie( err, resu ) = query( q );
 
     if( err ) {
-        throw "Error while looking ca cert id";
+        throw std::runtime_error("Error while looking ca cert id");
     }
 
     MYSQL_ROW row = mysql_fetch_row( resu.get() );
     unsigned long* l = mysql_fetch_lengths( resu.get() );
 
     if( !row || !l ) {
-        throw "Error while inserting new ca cert";
+        throw std::runtime_error("Error while inserting new ca cert");
     }
 
     return std::pair<std::string, std::string>( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) );
@@ -349,6 +327,6 @@ std::pair<std::string, std::string> MySQLJobProvider::getRevocationInfo( std::sh
 
 void MySQLJobProvider::writeBackRevocation( std::shared_ptr<Job> job, std::string date ) {
     if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) {
-        throw "Error while writing back revocation";
+        throw std::runtime_error("Error while writing back revocation");
     }
 }