7 #include <mysql/errmsg.h>
8 #include <log/logger.hpp>
10 //This static variable exists to handle initializing and finalizing the MySQL driver library
11 std::shared_ptr<int> MySQLJobProvider::lib_ref(
12 //Initializer: Store the return code as a pointer to an integer
13 new int( mysql_library_init( 0, NULL, NULL ) ),
15 //Finalizer: Check the pointer and free resources
18 //The library is not initialized
23 //The library did return an error when initializing
33 MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
34 if( !lib_ref || *lib_ref ) {
35 throw std::runtime_error("MySQL library not initialized!");
38 connect( server, user, password, database );
41 bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
43 conn = _connect( server, user, password, database );
48 std::shared_ptr<MYSQL> MySQLJobProvider::_connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
49 MYSQL* tmp( mysql_init( NULL ) );
55 tmp = mysql_real_connect( tmp, server.c_str(), user.c_str(), password.c_str(), database.c_str(), 3306, NULL, CLIENT_COMPRESS );
62 return std::shared_ptr<MYSQL>(
71 bool MySQLJobProvider::disconnect() {
81 std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std::string& query ) {
83 return std::make_pair( CR_SERVER_LOST, std::shared_ptr<MYSQL_RES>() );
86 int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
89 throw std::runtime_error(std::string( "MySQL error: " ) + mysql_error( this->conn.get() ));
93 std::shared_ptr<MYSQL_RES> res(
94 mysql_store_result( conn.get() ),
95 [c]( MYSQL_RES * r ) {
100 mysql_free_result( r );
103 return std::make_pair( err, res );
106 std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
107 std::string q = "SELECT id, targetId, task, executeFrom, executeTo, warning FROM jobs WHERE state='open' AND warning < 3";
110 std::shared_ptr<MYSQL_RES> res;
112 std::tie( err, res ) = query( q );
118 MYSQL_ROW row = mysql_fetch_row( res.get() );
124 auto job = std::make_shared<Job>();
126 unsigned long* l = mysql_fetch_lengths( res.get() );
132 job->id = std::string( row[0], row[0] + l[0] );
133 job->target = std::string( row[1], row[1] + l[1] );
134 job->task = std::string( row[2], row[2] + l[2] );
135 job->from = std::string( row[3], row[3] + l[3] );
136 job->to = std::string( row[4], row[4] + l[4] );
137 job->warning = std::string( row[5], row[5] + l[5] );
139 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 );
144 std::string MySQLJobProvider::escape_string( const std::string& target ) {
146 throw std::runtime_error("Not connected!");
151 result.resize( target.size() * 2 );
153 long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
155 result.resize( len );
160 void MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
162 throw std::runtime_error("Not connected!");
165 std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
167 if( query( q ).first ) {
168 throw std::runtime_error("No database entry found.");
172 void MySQLJobProvider::failJob( std::shared_ptr<Job> job ) {
174 throw std::runtime_error("Not connected!");
177 std::string q = "UPDATE jobs SET warning = warning + 1 WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
179 if( query( q ).first ) {
180 throw std::runtime_error("No database entry found.");
184 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
185 auto cert = std::make_shared<TBSCertificate>();
186 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 ) + "'";
190 std::shared_ptr<MYSQL_RES> res;
192 std::tie( err, res ) = query( q );
198 MYSQL_ROW row = mysql_fetch_row( res.get() );
204 unsigned long* l = mysql_fetch_lengths( res.get() );
210 std::string profileName = std::string( row[4], row[4] + l[4] );
212 cert->md = std::string( row[0], row[0] + l[0] );
213 std::string profileId = std::string( row[1], row[1] + l[1] );
215 while( profileId.size() < 4 ) {
216 profileId = "0" + profileId;
219 cert->profile = profileId + "-" + profileName;
221 cert->csr = std::string( row[2], row[2] + l[2] );
222 cert->csr_type = std::string( row[3], row[3] + l[3] );
224 cert->SANs = std::vector<std::shared_ptr<SAN>>();
226 q = "SELECT contents, type FROM subjectAlternativeNames WHERE certId='" + this->escape_string( job->target ) + "'";
227 std::tie( err, res ) = query( q );
230 std::cout << mysql_error( this->conn.get() );
234 std::cout << "Fetching SANs" << std::endl;
236 while( ( row = mysql_fetch_row( res.get() ) ) ) {
237 unsigned long* l = mysql_fetch_lengths( res.get() );
243 auto nSAN = std::make_shared<SAN>();
244 nSAN->content = std::string( row[0], row[0] + l[0] );
245 nSAN->type = std::string( row[1], row[1] + l[1] );
246 cert->SANs.push_back( nSAN );
249 q = "SELECT name, value FROM certAvas WHERE certid='" + this->escape_string( job->target ) + "'";
250 std::tie( err, res ) = query( q );
253 std::cout << mysql_error( this->conn.get() );
258 while( ( row = mysql_fetch_row( res.get() ) ) ) {
259 unsigned long* l = mysql_fetch_lengths( res.get() );
265 auto nAVA = std::make_shared<AVA>();
266 nAVA->name = std::string( row[0], row[0] + l[0] );
267 nAVA->value = std::string( row[1], row[1] + l[1] );
268 cert->AVAs.push_back( nAVA );
274 void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
276 throw std::runtime_error("Error while writing back");
279 std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'";
282 std::shared_ptr<MYSQL_RES> resu;
283 std::tie( err, resu ) = query( id );
286 throw std::runtime_error("Error while looking ca cert id");
289 MYSQL_ROW row = mysql_fetch_row( resu.get() );
290 unsigned long* l = mysql_fetch_lengths( resu.get() );
295 throw std::runtime_error("Error while inserting new ca cert not found");
297 read_id = std::string( row[0], row[0] + l[0] );
300 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";
301 // TODO write more thingies back
303 if( query( q ).first ) {
304 throw std::runtime_error("Error while writing back");
308 std::pair<std::string, std::string> MySQLJobProvider::getRevocationInfo( std::shared_ptr<Job> job ) {
309 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 ) + "' ";
311 std::shared_ptr<MYSQL_RES> resu;
312 std::tie( err, resu ) = query( q );
315 throw std::runtime_error("Error while looking ca cert id");
318 MYSQL_ROW row = mysql_fetch_row( resu.get() );
319 unsigned long* l = mysql_fetch_lengths( resu.get() );
322 throw std::runtime_error("Error while inserting new ca cert");
325 return std::pair<std::string, std::string>( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) );
328 void MySQLJobProvider::writeBackRevocation( std::shared_ptr<Job> job, std::string date ) {
329 if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) {
330 throw std::runtime_error("Error while writing back revocation");