10 #include "simpleOpensslSigner.h"
19 std::vector<Profile> profiles;
20 std::string sqlHost, sqlUser, sqlPass, sqlDB;
22 std::string writeBackFile( uint32_t serial, std::string cert ) {
23 std::string filename = "keys";
24 mkdir( filename.c_str(), 0755 );
26 mkdir( filename.c_str(), 0755 );
27 filename += "/" + std::to_string( serial / 1000 );
28 mkdir( filename.c_str(), 0755 );
29 filename += "/" + std::to_string( serial ) + ".crt";
31 file.open( filename.c_str() );
34 std::cout << "wrote to " << filename << std::endl;
38 int main( int argc, const char* argv[] ) {
43 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
48 config.open( "config.txt" );
50 if( !config.is_open() ) {
51 std::cerr << "config missing" << std::endl;
57 while( config >> line1 ) {
58 if( line1[0] == '#' ) {
62 int splitter = line1.find( "=" );
64 if( splitter == -1 ) {
65 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
69 std::string key = line1.substr( 0, splitter );
70 std::string value = line1.substr( splitter + 1 );
72 if( key == "key.directory" ) {
75 } else if( key == "sql.host" ) {
77 } else if( key == "sql.user" ) {
79 } else if( key == "sql.password" ) {
81 } else if( key == "sql.database" ) {
85 if( key.compare( 0, 8, "profile." ) == 0 ) {
86 int numE = key.find( ".", 9 );
89 std::cout << "invalid line: " << line1 << std::endl;
93 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
94 std::string rest = key.substr( numE + 1 );
96 if( i + 1 > profiles.size() ) {
97 profiles.resize( i + 1 );
100 if( rest == "key" ) {
101 profiles[i].key = value;
102 } else if( rest == "cert" ) {
103 profiles[i].cert = value;
105 std::cout << "invalid line: " << line1 << std::endl;
111 std::cout << profiles.size() << " profiles loaded." << std::endl;
114 std::cerr << "Missing config property key.directory" << std::endl;
120 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
121 std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
124 std::shared_ptr<Job> job = jp->fetchJob();
127 std::cout << "Nothing to work on" << std::endl;
132 if( job->task == "sign" ) {
134 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
137 std::cout << "wasn't able to load CSR" << std::endl;
141 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
142 std::ifstream t( cert->csr );
143 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
145 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
146 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
148 jp->writeBack( job, res );
149 } catch( const char* c ) {
150 std::cerr << "ERROR: " << c << std::endl;
152 } catch( std::string c ) {
153 std::cerr << "ERROR: " << c << std::endl;
157 std::cout << "Unknown job type" << job->task << std::endl;
160 if( DAEMON && !jp->finishJob( job ) ) {
164 if( !DAEMON || once ) {