7 #include <mysql/errmsg.h>
9 //This static variable exists to handle initializing and finalizing the MySQL driver library
10 std::shared_ptr<int> MySQLJobProvider::lib_ref(
11 //Initializer: Store the return code as a pointer to an integer
12 new int( mysql_library_init( 0, NULL, NULL ) ),
13 //Finalizer: Check the pointer and free resources
16 //The library is not initialized
21 //The library did return an error when initializing
31 MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
32 if( !lib_ref || *lib_ref ) {
33 throw "MySQL library not initialized!";
36 connect( server, user, password, database );
39 MySQLJobProvider::~MySQLJobProvider() {
43 bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
52 conn = _connect( server, user, password, database );
57 std::shared_ptr<MYSQL> MySQLJobProvider::_connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
58 MYSQL* tmp( mysql_init( NULL ) );
61 return std::shared_ptr<MYSQL>();
64 tmp = mysql_real_connect( tmp, server.c_str(), user.c_str(), password.c_str(), database.c_str(), 3306, NULL, CLIENT_COMPRESS );
67 return std::shared_ptr<MYSQL>();
71 return std::shared_ptr<MYSQL>(
80 bool MySQLJobProvider::disconnect() {
90 std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std::string& query ) {
92 return std::make_pair( CR_SERVER_LOST, std::shared_ptr<MYSQL_RES>() );
95 int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
98 throw std::string( "MySQL error: " ) + mysql_error( this->conn.get() );
102 std::shared_ptr<MYSQL_RES> res(
103 mysql_store_result( conn.get() ),
104 [c]( MYSQL_RES * r ) {
109 mysql_free_result( r );
112 return std::make_pair( err, res );
115 std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
116 std::string q = "SELECT id, targetId, task, executeFrom, executeTo, warning FROM jobs WHERE state='open' AND warning < 3";
119 std::shared_ptr<MYSQL_RES> res;
121 std::tie( err, res ) = query( q );
124 return std::shared_ptr<Job>();
127 unsigned int num = mysql_num_fields( res.get() );
129 MYSQL_ROW row = mysql_fetch_row( res.get() );
132 return std::shared_ptr<Job>();
135 std::shared_ptr<Job> job( new Job() );
137 unsigned long* l = mysql_fetch_lengths( res.get() );
140 return std::shared_ptr<Job>();
143 job->id = std::string( row[0], row[0] + l[0] );
144 job->target = std::string( row[1], row[1] + l[1] );
145 job->task = std::string( row[2], row[2] + l[2] );
146 job->from = std::string( row[3], row[3] + l[3] );
147 job->to = std::string( row[4], row[4] + l[4] );
148 job->warning = std::string( row[5], row[5] + l[5] );
150 for( unsigned int i = 0; i < num; i++ ) {
151 printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
159 std::string MySQLJobProvider::escape_string( const std::string& target ) {
161 throw "Not connected!";
166 result.resize( target.size() * 2 );
168 long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
170 result.resize( len );
175 void MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
177 throw "Not connected!";
180 std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
182 if( query( q ).first ) {
183 throw "No database entry found.";
188 void MySQLJobProvider::failJob( std::shared_ptr<Job> job ) {
190 throw "Not connected!";
193 std::string q = "UPDATE jobs SET warning = warning + 1 WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
195 if( query( q ).first ) {
196 throw "No database entry found.";
200 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
201 std::shared_ptr<TBSCertificate> cert = std::shared_ptr<TBSCertificate>( new TBSCertificate() );
202 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 ) + "'";
206 std::shared_ptr<MYSQL_RES> res;
208 std::tie( err, res ) = query( q );
211 return std::shared_ptr<TBSCertificate>();
214 MYSQL_ROW row = mysql_fetch_row( res.get() );
217 return std::shared_ptr<TBSCertificate>();
220 unsigned long* l = mysql_fetch_lengths( res.get() );
223 return std::shared_ptr<TBSCertificate>();
226 std::string profileName = std::string( row[4], row[4] + l[4] );
228 cert->md = std::string( row[0], row[0] + l[0] );
229 std::string profileId = std::string( row[1], row[1] + l[1] );
231 while( profileId.size() < 4 ) {
232 profileId = "0" + profileId;
235 cert->profile = profileId + "-" + profileName;
237 cert->csr = std::string( row[2], row[2] + l[2] );
238 cert->csr_type = std::string( row[3], row[3] + l[3] );
240 cert->SANs = std::vector<std::shared_ptr<SAN>>();
242 q = "SELECT contents, type FROM subjectAlternativeNames WHERE certId='" + this->escape_string( job->target ) + "'";
243 std::tie( err, res ) = query( q );
246 std::cout << mysql_error( this->conn.get() );
247 return std::shared_ptr<TBSCertificate>();
250 std::cout << "Fetching SANs" << std::endl;
252 while( ( row = mysql_fetch_row( res.get() ) ) ) {
253 unsigned long* l = mysql_fetch_lengths( res.get() );
256 return std::shared_ptr<TBSCertificate>();
259 std::shared_ptr<SAN> nSAN = std::shared_ptr<SAN>( new SAN() );
260 nSAN->content = std::string( row[0], row[0] + l[0] );
261 nSAN->type = std::string( row[1], row[1] + l[1] );
262 cert->SANs.push_back( nSAN );
265 q = "SELECT name, value FROM certAvas WHERE certid='" + this->escape_string( job->target ) + "'";
266 std::tie( err, res ) = query( q );
269 std::cout << mysql_error( this->conn.get() );
270 return std::shared_ptr<TBSCertificate>();
274 while( ( row = mysql_fetch_row( res.get() ) ) ) {
275 unsigned long* l = mysql_fetch_lengths( res.get() );
278 return std::shared_ptr<TBSCertificate>();
281 std::shared_ptr<AVA> nAVA = std::shared_ptr<AVA>( new AVA() );
282 nAVA->name = std::string( row[0], row[0] + l[0] );
283 nAVA->value = std::string( row[1], row[1] + l[1] );
284 cert->AVAs.push_back( nAVA );
290 void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
292 throw "Error while writing back";
295 std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'";
298 std::shared_ptr<MYSQL_RES> resu;
299 std::tie( err, resu ) = query( id );
302 throw "Error while looking ca cert id";
305 MYSQL_ROW row = mysql_fetch_row( resu.get() );
306 unsigned long* l = mysql_fetch_lengths( resu.get() );
311 if( query( "INSERT INTO cacerts SET keyname= '" + this->escape_string( res->ca_name ) + "', subroot = 0" ).first ) {
312 throw "Error while inserting new ca cert";
315 my_ulonglong insert_id = mysql_insert_id( conn.get() );
317 read_id = std::to_string( insert_id );
319 read_id = std::string( row[0], row[0] + l[0] );
322 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";
323 // TODO write more thingies back
325 if( query( q ).first ) {
326 throw "Error while writing back";
330 std::pair<std::string, std::string> MySQLJobProvider::getRevocationInfo( std::shared_ptr<Job> job ) {
331 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 ) + "' ";
333 std::shared_ptr<MYSQL_RES> resu;
334 std::tie( err, resu ) = query( q );
337 throw "Error while looking ca cert id";
340 MYSQL_ROW row = mysql_fetch_row( resu.get() );
341 unsigned long* l = mysql_fetch_lengths( resu.get() );
344 throw "Error while inserting new ca cert";
347 return std::pair<std::string, std::string>( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) );
350 void MySQLJobProvider::writeBackRevocation( std::shared_ptr<Job> job, std::string date ) {
351 if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) {
352 throw "Error while writing back revocation";