#include <stdio.h>
+#include <iostream>
+
#include <mysql/errmsg.h>
//This static variable exists to handle initializing and finalizing the MySQL driver library
int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
if( err ) {
- return std::make_pair( err, std::shared_ptr<MYSQL_RES>() );
+ throw( std::string( "MySQL error: " ) + mysql_error( this->conn.get() ) ).c_str();
}
auto c = conn;
}
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] );
for( unsigned int i = 0; i < num; i++ ) {
printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
return true;
}
+
+std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
+ std::shared_ptr<TBSCertificate> cert = std::shared_ptr<TBSCertificate>( new TBSCertificate() );
+ 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<MYSQL_RES> res;
+
+ std::tie( err, res ) = query( q );
+
+ if( err ) {
+ return std::shared_ptr<TBSCertificate>();
+ }
+
+ MYSQL_ROW row = mysql_fetch_row( res.get() );
+
+ if( !row ) {
+ return std::shared_ptr<TBSCertificate>();
+ }
+
+ unsigned long* l = mysql_fetch_lengths( res.get() );
+
+ if( !l ) {
+ return std::shared_ptr<TBSCertificate>();
+ }
+
+ cert->CN = std::string( row[0], row[0] + l[0] );
+ cert->subj = std::string( row[1], row[1] + l[1] );
+ cert->md = std::string( row[2], row[2] + l[2] );
+ cert->profile = std::string( row[3], row[3] + l[3] );
+ 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<std::shared_ptr<SAN>>();
+
+ 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<TBSCertificate>();
+ }
+
+ while( ( row = mysql_fetch_row( res.get() ) ) ) {
+ unsigned long* l = mysql_fetch_lengths( res.get() );
+
+ if( !l ) {
+ return std::shared_ptr<TBSCertificate>();
+ }
+
+ std::shared_ptr<SAN> nSAN = std::shared_ptr<SAN>( 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> job, std::shared_ptr<SignedCertificate> 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";
+ }
+}