X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fmysql.cpp;h=67fc49ecc65bae1122a196242c41a2380e194ac8;hb=aef2ba57f652658f3bebfa24e706c0083a56e6bf;hp=ab37553ce1899f1fd00c776264398b24209559c8;hpb=e0d596ece5c45eba590040a822f4108657f217f0;p=cassiopeia.git diff --git a/src/mysql.cpp b/src/mysql.cpp index ab37553..67fc49e 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -2,6 +2,8 @@ #include +#include + #include //This static variable exists to handle initializing and finalizing the MySQL driver library @@ -93,7 +95,7 @@ std::pair< int, std::shared_ptr > MySQLJobProvider::query( const std: int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() ); if( err ) { - return std::make_pair( err, std::shared_ptr() ); + throw( std::string( "MySQL error: " ) + mysql_error( this->conn.get() ) ).c_str(); } auto c = conn; @@ -185,9 +187,10 @@ bool MySQLJobProvider::finishJob( std::shared_ptr job ) { std::shared_ptr MySQLJobProvider::fetchTBSCert( std::shared_ptr job ) { std::shared_ptr cert = std::shared_ptr( new TBSCertificate() ); - std::string q = "SELECT CN, subject, md, profile, csr_name, csr_type FROM certs WHERE id='" + this->escape_string( job->id ) + "'"; + std::string q = "SELECT CN, subject, md, profile, csr_name, csr_type FROM certs WHERE id='" + this->escape_string( job->target ) + "'"; int err = 0; + std::shared_ptr res; std::tie( err, res ) = query( q ); @@ -215,5 +218,42 @@ std::shared_ptr MySQLJobProvider::fetchTBSCert( std::shared_ptr< cert->csr = std::string( row[4], row[4] + l[4] ); cert->csr_type = std::string( row[5], row[5] + l[5] ); + 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 std::shared_ptr(); + } + + while( ( row = mysql_fetch_row( res.get() ) ) ) { + unsigned long* l = mysql_fetch_lengths( res.get() ); + + if( !l ) { + return std::shared_ptr(); + } + + std::shared_ptr nSAN = std::shared_ptr( new 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 ); + } + return cert; } + +void MySQLJobProvider::writeBack( std::shared_ptr job, std::shared_ptr res ) { + if( !conn ) { + throw "Error while writing back"; + } + + std::string q = "UPDATE certs SET crt_name='" + this->escape_string( res->crt_name ) + "', serial='" + this->escape_string( std::to_string( res->serial ) ) + "' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1"; + + // TODO write more thingies back + + if( query( q ).first ) { + throw "Error while writing back"; + } +}