X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fdb%2Fmysql.cpp;h=304d4963dcaed689e54751ca1b6ed756405e6f80;hb=db26a9bc01d5ee5f34557fa1ea00feca5be3b79f;hp=973e9d0fb68f06ba13f8d57f29e07485ba632c88;hpb=9e866a1a2facc8cb1565cd660c6b6d482f18ecb1;p=cassiopeia.git diff --git a/src/db/mysql.cpp b/src/db/mysql.cpp index 973e9d0..304d496 100644 --- a/src/db/mysql.cpp +++ b/src/db/mysql.cpp @@ -292,11 +292,63 @@ void MySQLJobProvider::writeBack( std::shared_ptr job, std::shared_ptrescape_string( res->crt_name ) + "', serial='" + this->escape_string( res->serial ) + "', created=NOW() WHERE id='" + this->escape_string( job->target ) + "' LIMIT 1"; + std::string id = "SELECT id FROM cacerts WHERE keyname='" + this->escape_string( res->ca_name ) + "'"; + int err = 0; + std::shared_ptr resu; + std::tie( err, resu ) = query( id ); + + if( err ) { + throw "Error while looking ca cert id"; + } + + MYSQL_ROW row = mysql_fetch_row( resu.get() ); + unsigned long* l = mysql_fetch_lengths( resu.get() ); + + std::string read_id; + + if( !row || !l ) { + if( query( "INSERT INTO cacerts SET keyname= '" + this->escape_string( res->ca_name ) + "', subroot = 0" ).first ) { + throw "Error while inserting new ca cert"; + } + + my_ulonglong insert_id = mysql_insert_id( conn.get() ); + + read_id = std::to_string( insert_id ); + } else { + read_id = std::string( row[0], row[0] + l[0] ); + } + + 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"; // TODO write more thingies back if( query( q ).first ) { throw "Error while writing back"; } } + +std::pair MySQLJobProvider::getRevocationInfo( std::shared_ptr job ) { + 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 ) + "' "; + int err = 0; + std::shared_ptr resu; + std::tie( err, resu ) = query( q ); + + if( err ) { + throw "Error while looking ca cert id"; + } + + MYSQL_ROW row = mysql_fetch_row( resu.get() ); + unsigned long* l = mysql_fetch_lengths( resu.get() ); + + if( !row || !l ) { + throw "Error while inserting new ca cert"; + } + + return std::pair( std::string( row[0], row[0] + l[0] ), std::string( row[1], row[1] + l[1] ) ); +} + +void MySQLJobProvider::writeBackRevocation( std::shared_ptr job, std::string date ) { + if( query( "UPDATE certs SET revoked = '" + this->escape_string( date ) + "' WHERE id = '" + this->escape_string( job->target ) + "'" ).first ) { + throw "Error while writing back revocation"; + } +}