From: Felix Dörre Date: Wed, 1 Feb 2017 08:35:33 +0000 (+0100) Subject: remove remaining dependencies to mysql X-Git-Url: https://code.wpia.club/?p=cassiopeia.git;a=commitdiff_plain;h=3fd3c6168c5a3dcae2bd4a1dab3f9a56dfcce807 remove remaining dependencies to mysql Change-Id: I0ac6452ca6b1d285a0c35c4a71d7669e414a9ef5 --- diff --git a/Makefile b/Makefile index e3131e3..74caedf 100644 --- a/Makefile +++ b/Makefile @@ -121,7 +121,7 @@ coverage: cassiopeia: bin/cassiopeia bin/cassiopeia-signer bin/cassiopeia: libs ${FS_OBJ} ${OBJ_DIR}/apps/client.lo - ${MKDIR} $(shell dirname $@) && ${LD} ${LDFLAGS} -lmysqlclient -lpqxx -lpq -o $@ ${FS_OBJ} ${OBJ_DIR}/apps/client.lo + ${MKDIR} $(shell dirname $@) && ${LD} ${LDFLAGS} -lpqxx -lpq -o $@ ${FS_OBJ} ${OBJ_DIR}/apps/client.lo bin/cassiopeia-signer: libs ${FS_OBJ} ${OBJ_DIR}/apps/signer.lo ${MKDIR} $(shell dirname $@) && ${LD} ${LDFLAGS} -o $@ $(filter-out ${OBJ_DIR}/db/psql.lo, $(filter-out ${OBJ_DIR}/db/mysql.lo,${FS_OBJ})) ${OBJ_DIR}/apps/signer.lo diff --git a/debian/control b/debian/control index 2ffb80c..f8fb972 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: cacert-cassiopeia Section: utils Priority: extra Maintainer: CAcert Software Team -Build-Depends: debhelper (>= 8.0.0), libtool, libmysqlclient-dev (>= 5.5), libboost-test-dev +Build-Depends: debhelper (>= 8.0.0), libtool, libpqxx-dev, libboost-test-dev Standards-Version: 3.9.4 Homepage: https://cacert.org/ #Vcs-Git: git://git.debian.org/collab-maint/cassiopeia.git diff --git a/src/apps/signer.cpp b/src/apps/signer.cpp index 6b49241..26909bd 100644 --- a/src/apps/signer.cpp +++ b/src/apps/signer.cpp @@ -5,7 +5,6 @@ #include #include "db/database.h" -#include "db/mysql.h" #include "crypto/simpleOpensslSigner.h" #include "crypto/remoteSigner.h" #include "crypto/sslUtil.h" diff --git a/src/db/mysql.cpp b/src/db/mysql.cpp deleted file mode 100644 index e8f1d4d..0000000 --- a/src/db/mysql.cpp +++ /dev/null @@ -1,332 +0,0 @@ -#include "mysql.h" - -#include - -#include - -#include -#include - -//This static variable exists to handle initializing and finalizing the MySQL driver library -std::shared_ptr 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 ) { - //The library is not initialized - return; - } - - if( *ref ) { - //The library did return an error when initializing - delete ref; - return; - } - - delete ref; - - mysql_library_end(); - } ); - -MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) { - if( !lib_ref || *lib_ref ) { - throw std::runtime_error("MySQL library not initialized!"); - } - - connect( server, user, password, database ); -} - -bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) { - disconnect(); - conn = _connect( server, user, password, database ); - - return !!conn; -} - -std::shared_ptr MySQLJobProvider::_connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) { - MYSQL* tmp( mysql_init( NULL ) ); - - if( !tmp ) { - 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 nullptr; - } - - auto l = lib_ref; - return std::shared_ptr( - tmp, - [l]( MYSQL * c ) { - if( c ) { - mysql_close( c ); - } - } ); -} - -bool MySQLJobProvider::disconnect() { - if( !conn ) { - return false; - } - - conn.reset(); - - return true; -} - -std::pair< int, std::shared_ptr > MySQLJobProvider::query( const std::string& query ) { - if( !conn ) { - return std::make_pair( CR_SERVER_LOST, std::shared_ptr() ); - } - - int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() ); - - if( err ) { - throw std::runtime_error(std::string( "MySQL error: " ) + mysql_error( this->conn.get() )); - } - - auto c = conn; - std::shared_ptr res( - mysql_store_result( conn.get() ), - [c]( MYSQL_RES * r ) { - if( !r ) { - return; - } - - mysql_free_result( r ); - } ); - - return std::make_pair( err, res ); -} - -std::shared_ptr MySQLJobProvider::fetchJob() { - std::string q = "SELECT id, targetId, task, executeFrom, executeTo, warning FROM jobs WHERE state='open' AND warning < 3"; - - int err = 0; - std::shared_ptr res; - - std::tie( err, res ) = query( q ); - - if( err ) { - return nullptr; - } - - MYSQL_ROW row = mysql_fetch_row( res.get() ); - - if( !row ) { - return nullptr; - } - - auto job = std::make_shared(); - - unsigned long* l = mysql_fetch_lengths( res.get() ); - - if( !l ) { - return nullptr; - } - - job->id = std::string( row[0], row[0] + l[0] ); - job->target = std::string( row[1], row[1] + l[1] ); - 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] ); - - 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 std::runtime_error("Not connected!"); - } - - std::string result; - - result.resize( target.size() * 2 ); - - long unsigned int len = mysql_real_escape_string( conn.get(), const_cast( result.data() ), target.c_str(), target.size() ); - - result.resize( len ); - - return result; -} - -void MySQLJobProvider::finishJob( std::shared_ptr job ) { - if( !conn ) { - 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 std::runtime_error("No database entry found."); - } -} - -void MySQLJobProvider::failJob( std::shared_ptr job ) { - if( !conn ) { - 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 std::runtime_error("No database entry found."); - } -} - -std::shared_ptr MySQLJobProvider::fetchTBSCert( std::shared_ptr job ) { - auto cert = std::make_shared(); - 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; - - std::shared_ptr res; - - std::tie( err, res ) = query( q ); - - if( err ) { - return nullptr; - } - - MYSQL_ROW row = mysql_fetch_row( res.get() ); - - if( !row ) { - return nullptr; - } - - unsigned long* l = mysql_fetch_lengths( res.get() ); - - if( !l ) { - return nullptr; - } - - std::string profileName = std::string( row[4], row[4] + l[4] ); - - cert->md = std::string( row[0], row[0] + l[0] ); - std::string profileId = std::string( row[1], row[1] + l[1] ); - - while( profileId.size() < 4 ) { - profileId = "0" + profileId; - } - - cert->profile = profileId + "-" + profileName; - - cert->csr = std::string( row[2], row[2] + l[2] ); - cert->csr_type = std::string( row[3], row[3] + l[3] ); - - cert->SANs = std::vector>(); - - q = "SELECT contents, type FROM subjectAlternativeNames WHERE certId='" + this->escape_string( job->target ) + "'"; - std::tie( err, res ) = query( q ); - - if( err ) { - std::cout << mysql_error( this->conn.get() ); - return nullptr; - } - - std::cout << "Fetching SANs" << std::endl; - - while( ( row = mysql_fetch_row( res.get() ) ) ) { - unsigned long* l = mysql_fetch_lengths( res.get() ); - - if( !l ) { - return nullptr; - } - - auto nSAN = std::make_shared(); - 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 ); - } - - q = "SELECT name, value FROM certAvas WHERE certid='" + this->escape_string( job->target ) + "'"; - std::tie( err, res ) = query( q ); - - if( err ) { - std::cout << mysql_error( this->conn.get() ); - return nullptr; - - } - - while( ( row = mysql_fetch_row( res.get() ) ) ) { - unsigned long* l = mysql_fetch_lengths( res.get() ); - - if( !l ) { - return nullptr; - } - - auto nAVA = std::make_shared(); - 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 ); - } - - return cert; -} - -void MySQLJobProvider::writeBack( std::shared_ptr job, std::shared_ptr res ) { - if( !conn ) { - throw std::runtime_error("Error while writing back"); - } - - std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'"; - - int err = 0; - std::shared_ptr resu; - std::tie( err, resu ) = query( id ); - - if( err ) { - 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() ); - - std::string read_id; - - if( !row || !l ) { - throw std::runtime_error("Error while inserting new ca cert not found"); - } else { - read_id = std::string( row[0], row[0] + l[0] ); - } - - std::string q = "UPDATE certs SET crt_name='" + this->escape_string( res->crt_name ) + "', serial='" + this->escape_string( res->serial ) + "', caId = '" + this->escape_string( read_id ) + "', created='" + this->escape_string( res->before ) + "', expire='" + this->escape_string( res->after ) + "' WHERE id='" + this->escape_string( job->target ) + "' LIMIT 1"; - // TODO write more thingies back - - if( query( q ).first ) { - throw std::runtime_error("Error while writing back"); - } -} - -std::pair MySQLJobProvider::getRevocationInfo( std::shared_ptr job ) { - std::string q = "SELECT certs.serial, cacerts.keyname FROM certs INNER JOIN cacerts ON certs.caId = cacerts.id WHERE certs.id = '" + this->escape_string( job->target ) + "' "; - int err = 0; - std::shared_ptr resu; - std::tie( err, resu ) = query( q ); - - if( err ) { - 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 std::runtime_error("Error while inserting new ca cert"); - } - - return std::pair( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) ); -} - -void MySQLJobProvider::writeBackRevocation( std::shared_ptr job, std::string date ) { - if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) { - throw std::runtime_error("Error while writing back revocation"); - } -} diff --git a/src/db/mysql.h b/src/db/mysql.h deleted file mode 100644 index 5559244..0000000 --- a/src/db/mysql.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -#include "database.h" - -class MySQLJobProvider : public JobProvider { -private: - static std::shared_ptr lib_ref; - - std::shared_ptr conn; - -private: - std::shared_ptr _connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ); - -public: - MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ); - -public: - bool connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ); - bool disconnect(); - - std::string escape_string( const std::string& target ); - - std::pair< int, std::shared_ptr > query( const std::string& query ); - -public: - std::shared_ptr fetchJob(); - void finishJob( std::shared_ptr job ); - void failJob( std::shared_ptr job ); - std::shared_ptr fetchTBSCert( std::shared_ptr job ); - void writeBack( std::shared_ptr job, std::shared_ptr res ); - std::pair getRevocationInfo( std::shared_ptr job ); - void writeBackRevocation( std::shared_ptr job, std::string date ); -}; diff --git a/src/db/psql.h b/src/db/psql.h index ccebf87..0f773bb 100644 --- a/src/db/psql.h +++ b/src/db/psql.h @@ -4,8 +4,6 @@ #include #include -#include - #include "database.h" #include diff --git a/test/Makefile b/test/Makefile index c457631..3e55b13 100644 --- a/test/Makefile +++ b/test/Makefile @@ -21,7 +21,7 @@ endif CFLAGS+=${ADDFLAGS} -Wall -Werror -Wextra -pedantic -std=c++11 -I../src -I../lib/openssl/include CXXFLAGS=$(CFLAGS) -LDFLAGS+=${ADDFLAGS} -lmysqlclient -lssl -lcrypto -ldl -lboost_unit_test_framework -L../lib/openssl -L/usr/lib/i386-linux-gnu -lpqxx -lpq +LDFLAGS+=${ADDFLAGS} -lssl -lcrypto -ldl -lboost_unit_test_framework -L../lib/openssl -L/usr/lib/i386-linux-gnu -lpqxx -lpq ifneq (,$(filter coverage,$(DEB_BUILD_OPTIONS))) LDFLAGS += -lgcov diff --git a/test/src/sql.cpp b/test/src/sql.cpp deleted file mode 100644 index cf85629..0000000 --- a/test/src/sql.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -#include -#include - -#include - -extern std::string sqlHost, sqlUser, sqlPass, sqlDB; - -BOOST_AUTO_TEST_SUITE( TestSQL ) - -BOOST_AUTO_TEST_CASE( testSQL ) { - std::ifstream conf( "config.txt" ); - - if( !conf ) { - BOOST_WARN_MESSAGE( 0, "Config file is missing. Exiting." ); - return; - } - - BOOST_REQUIRE( parseConfig( "config.txt" ) == 0 ); - std::shared_ptr jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) ); - BOOST_REQUIRE( jp->query( "TRUNCATE TABLE profiles" ).first == 0 ); - BOOST_REQUIRE( jp->query( "TRUNCATE TABLE certs" ).first == 0 ); - BOOST_REQUIRE( jp->query( "TRUNCATE TABLE certAvas" ).first == 0 ); - BOOST_REQUIRE( jp->query( "TRUNCATE TABLE subjectAlternativeNames" ).first == 0 ); - BOOST_REQUIRE( jp->query( "TRUNCATE TABLE jobs" ).first == 0 ); - BOOST_REQUIRE( jp->query( "INSERT INTO profiles SET id='1', keyname='assured', keyUsage='', extendedKeyUsage='', name='assured'" ).first == 0 ); - BOOST_REQUIRE( jp->query( "INSERT INTO jobs SET task='sign', targetId='1'" ).first == 0 ); - - std::shared_ptr job = jp->fetchJob(); - BOOST_REQUIRE( job ); - jp->failJob( job ); - BOOST_REQUIRE_EQUAL( job->target, "1" ); - BOOST_REQUIRE_EQUAL( job->task, "sign" ); - job = jp->fetchJob(); - BOOST_REQUIRE( job ); - std::shared_ptr cert = jp->fetchTBSCert( job ); - BOOST_REQUIRE( !cert ); - BOOST_REQUIRE( jp->query( "INSERT INTO certs SET csr_type='CSR', id='1', profile='1'" ).first == 0 ); - BOOST_REQUIRE( jp->query( "INSERT INTO subjectAlternativeNames SET certId='1', contents='example.org', type='DNS'" ).first == 0 ); - BOOST_REQUIRE( jp->query( "INSERT INTO certAvas SET certid='1', name='CN', value='example.org'" ).first == 0 ); - cert = jp->fetchTBSCert( job ); - BOOST_REQUIRE( cert ); - - std::shared_ptr fcert( new SignedCertificate() ); - fcert->certificate = "CERT"; - fcert->serial = "1234"; - fcert->crt_name = "crt.name.crt"; - fcert->ca_name = "assured"; - jp->writeBack( job, fcert ); - jp->finishJob( job ); - BOOST_REQUIRE( !jp->fetchJob() ); - BOOST_REQUIRE( jp->query( "INSERT INTO jobs SET task='revoke', targetId='1'" ).first == 0 ); - job = jp->fetchJob(); - BOOST_REQUIRE_EQUAL( job->target, "1" ); - BOOST_REQUIRE_EQUAL( job->task, "revoke" ); - std::pair revocationInfo = jp->getRevocationInfo( job ); - BOOST_REQUIRE_EQUAL( revocationInfo.first, "1234" ); - BOOST_REQUIRE_EQUAL( revocationInfo.second, "assured" ); - jp->writeBackRevocation( job, "2000-01-01 01:01:01" ); - jp->finishJob( job ); -} - -BOOST_AUTO_TEST_CASE( testSQLDisconnected ) { - std::ifstream conf( "config.txt" ); - - if( !conf ) { - BOOST_WARN_MESSAGE( 0, "Config file is missing. Exiting." ); - return; - } - - //if(1) return; - //BOOST_REQUIRE( parseConfig("config.txt") == 0 ); - std::shared_ptr jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) ); - jp->disconnect(); - jp->disconnect(); - BOOST_REQUIRE( jp->query( "SELECT 1" ).first ); - BOOST_REQUIRE_THROW( jp->escape_string( "uia" ), const char* ); - BOOST_REQUIRE_THROW( jp->finishJob( std::shared_ptr() ), const char* ); - BOOST_REQUIRE_THROW( jp->failJob( std::shared_ptr() ), const char* ); -} - -BOOST_AUTO_TEST_SUITE_END()