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 FROM jobs WHERE state='open'";
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] );
149 for( unsigned int i = 0; i < num; i++ ) {
150 printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
158 std::string MySQLJobProvider::escape_string( const std::string& target ) {
160 throw "Not connected!";
165 result.resize( target.size() * 2 );
167 long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
169 result.resize( len );
174 bool MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
179 std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
181 if( query( q ).first ) {
188 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
189 std::shared_ptr<TBSCertificate> cert = std::shared_ptr<TBSCertificate>( new TBSCertificate() );
190 std::string q = "SELECT md, profile, csr_name, csr_type FROM certs WHERE id='" + this->escape_string( job->target ) + "'";
194 std::shared_ptr<MYSQL_RES> res;
196 std::tie( err, res ) = query( q );
199 return std::shared_ptr<TBSCertificate>();
202 MYSQL_ROW row = mysql_fetch_row( res.get() );
205 return std::shared_ptr<TBSCertificate>();
208 unsigned long* l = mysql_fetch_lengths( res.get() );
211 return std::shared_ptr<TBSCertificate>();
214 cert->md = std::string( row[0], row[0] + l[0] );
215 cert->profile = std::string( row[1], row[1] + l[1] );
216 cert->csr = std::string( row[2], row[2] + l[2] );
217 cert->csr_type = std::string( row[3], row[3] + l[3] );
219 cert->SANs = std::vector<std::shared_ptr<SAN>>();
221 q = "SELECT contents, type FROM subjectAlternativeNames WHERE certId='" + this->escape_string( job->target ) + "'";
222 std::tie( err, res ) = query( q );
225 std::cout << mysql_error( this->conn.get() );
226 return std::shared_ptr<TBSCertificate>();
229 std::cout << "Fetching SANs" << std::endl;
231 while( ( row = mysql_fetch_row( res.get() ) ) ) {
232 unsigned long* l = mysql_fetch_lengths( res.get() );
235 return std::shared_ptr<TBSCertificate>();
238 std::shared_ptr<SAN> nSAN = std::shared_ptr<SAN>( new SAN() );
239 nSAN->content = std::string( row[0], row[0] + l[0] );
240 nSAN->type = std::string( row[1], row[1] + l[1] );
241 cert->SANs.push_back( nSAN );
244 q = "SELECT name, value FROM certAvas WHERE certid='" + this->escape_string( job->target ) + "'";
245 std::tie( err, res ) = query( q );
248 std::cout << mysql_error( this->conn.get() );
249 return std::shared_ptr<TBSCertificate>();
253 while( ( row = mysql_fetch_row( res.get() ) ) ) {
254 unsigned long* l = mysql_fetch_lengths( res.get() );
257 return std::shared_ptr<TBSCertificate>();
260 std::shared_ptr<AVA> nAVA = std::shared_ptr<AVA>( new AVA() );
261 nAVA->name = std::string( row[0], row[0] + l[0] );
262 nAVA->value = std::string( row[1], row[1] + l[1] );
263 cert->AVAs.push_back( nAVA );
269 void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
271 throw "Error while writing back";
274 std::string q = "UPDATE certs SET crt_name='" + this->escape_string( res->crt_name ) + "', serial='" + this->escape_string( res->serial ) + "', created=NOW() WHERE id='" + this->escape_string( job->target ) + "' LIMIT 1";
276 // TODO write more thingies back
278 if( query( q ).first ) {
279 throw "Error while writing back";