]> WPIA git - cassiopeia.git/blob - src/mysql.cpp
add: Basic implementation to generate the certificate serial
[cassiopeia.git] / src / mysql.cpp
1 #include "mysql.h"
2
3 #include <stdio.h>
4
5 #include <iostream>
6
7 #include <mysql/errmsg.h>
8
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
14     []( int* ref ) {
15         if( !ref ) {
16             //The library is not initialized
17             return;
18         }
19
20         if( *ref ) {
21             //The library did return an error when initializing
22             delete ref;
23             return;
24         }
25
26         delete ref;
27
28         mysql_library_end();
29     } );
30
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!";
34     }
35
36     connect( server, user, password, database );
37 }
38
39 MySQLJobProvider::~MySQLJobProvider() {
40     disconnect();
41 }
42
43 bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
44     if( conn ) {
45         if( !disconnect() ) {
46             return false;
47         }
48
49         conn.reset();
50     }
51
52     conn = _connect( server, user, password, database );
53
54     return !!conn;
55 }
56
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 ) );
59
60     if( !tmp ) {
61         return std::shared_ptr<MYSQL>();
62     }
63
64     tmp = mysql_real_connect( tmp, server.c_str(), user.c_str(), password.c_str(), database.c_str(), 3306, NULL, CLIENT_COMPRESS );
65
66     if( !tmp ) {
67         return std::shared_ptr<MYSQL>();
68     }
69
70     auto l = lib_ref;
71     return std::shared_ptr<MYSQL>(
72         tmp,
73         [l]( MYSQL * c ) {
74             if( c ) {
75                 mysql_close( c );
76             }
77         } );
78 }
79
80 bool MySQLJobProvider::disconnect() {
81     if( !conn ) {
82         return false;
83     }
84
85     conn.reset();
86
87     return true;
88 }
89
90 std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std::string& query ) {
91     if( !conn ) {
92         return std::make_pair( CR_SERVER_LOST, std::shared_ptr<MYSQL_RES>() );
93     }
94
95     int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
96
97     if( err ) {
98         throw( std::string( "MySQL error: " ) + mysql_error( this->conn.get() ) ).c_str();
99     }
100
101     auto c = conn;
102     std::shared_ptr<MYSQL_RES> res(
103         mysql_store_result( conn.get() ),
104         [c]( MYSQL_RES * r ) {
105             if( !r ) {
106                 return;
107             }
108
109             mysql_free_result( r );
110         } );
111
112     return std::make_pair( err, res );
113 }
114
115 std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
116     std::string q = "SELECT id, targetId, task, executeFrom, executeTo FROM jobs WHERE state='open'";
117
118     int err = 0;
119     std::shared_ptr<MYSQL_RES> res;
120
121     std::tie( err, res ) = query( q );
122
123     if( err ) {
124         return std::shared_ptr<Job>();
125     }
126
127     unsigned int num = mysql_num_fields( res.get() );
128
129     MYSQL_ROW row = mysql_fetch_row( res.get() );
130
131     if( !row ) {
132         return std::shared_ptr<Job>();
133     }
134
135     std::shared_ptr<Job> job( new Job() );
136
137     unsigned long* l = mysql_fetch_lengths( res.get() );
138
139     if( !l ) {
140         return std::shared_ptr<Job>();
141     }
142
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
149     for( unsigned int i = 0; i < num; i++ ) {
150         printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
151     }
152
153     printf( "\n" );
154
155     return job;
156 }
157
158 std::string MySQLJobProvider::escape_string( const std::string& target ) {
159     if( !conn ) {
160         throw "Not connected!";
161     }
162
163     std::string result;
164
165     result.resize( target.size() * 2 );
166
167     long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
168
169     result.resize( len );
170
171     return result;
172 }
173
174 bool MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
175     if( !conn ) {
176         return false;
177     }
178
179     std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
180
181     if( query( q ).first ) {
182         return false;
183     }
184
185     return true;
186 }
187
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 CN, subject, md, profile, csr_name, csr_type FROM certs WHERE id='" + this->escape_string( job->target ) + "'";
191
192     int err = 0;
193
194     std::shared_ptr<MYSQL_RES> res;
195
196     std::tie( err, res ) = query( q );
197
198     if( err ) {
199         return std::shared_ptr<TBSCertificate>();
200     }
201
202     MYSQL_ROW row = mysql_fetch_row( res.get() );
203
204     if( !row ) {
205         return std::shared_ptr<TBSCertificate>();
206     }
207
208     unsigned long* l = mysql_fetch_lengths( res.get() );
209
210     if( !l ) {
211         return std::shared_ptr<TBSCertificate>();
212     }
213
214     cert->CN = std::string( row[0], row[0] + l[0] );
215     cert->subj = std::string( row[1], row[1] + l[1] );
216     cert->md = std::string( row[2], row[2] + l[2] );
217     cert->profile = std::string( row[3], row[3] + l[3] );
218     cert->csr = std::string( row[4], row[4] + l[4] );
219     cert->csr_type = std::string( row[5], row[5] + l[5] );
220
221     cert->SANs = std::vector<std::shared_ptr<SAN>>();
222
223     q = "SELECT contents, type FROM subjectAlternativeNames WHERE certId='" + this->escape_string( job->target ) + "'";
224     std::tie( err, res ) = query( q );
225
226     if( err ) {
227         std::cout << mysql_error( this->conn.get() );
228         return std::shared_ptr<TBSCertificate>();
229     }
230
231     while( ( row = mysql_fetch_row( res.get() ) ) ) {
232         unsigned long* l = mysql_fetch_lengths( res.get() );
233
234         if( !l ) {
235             return std::shared_ptr<TBSCertificate>();
236         }
237
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 );
242     }
243
244     return cert;
245 }
246
247 void MySQLJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
248     if( !conn ) {
249         throw "Error while writing back";
250     }
251
252     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";
253
254     // TODO write more thingies back
255
256     if( query( q ).first ) {
257         throw "Error while writing back";
258     }
259 }