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] ) ) {
49 config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
51 config.open( "config.txt" );
54 if( !config.is_open() ) {
55 std::cerr << "config missing" << std::endl;
61 while( config >> line1 ) {
62 if( line1[0] == '#' ) {
66 int splitter = line1.find( "=" );
68 if( splitter == -1 ) {
69 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
73 std::string key = line1.substr( 0, splitter );
74 std::string value = line1.substr( splitter + 1 );
76 if( key == "key.directory" ) {
79 } else if( key == "sql.host" ) {
81 } else if( key == "sql.user" ) {
83 } else if( key == "sql.password" ) {
85 } else if( key == "sql.database" ) {
89 if( key.compare( 0, 8, "profile." ) == 0 ) {
90 int numE = key.find( ".", 9 );
93 std::cout << "invalid line: " << line1 << std::endl;
97 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
98 std::string rest = key.substr( numE + 1 );
100 if( i + 1 > profiles.size() ) {
101 profiles.resize( i + 1 );
104 if( rest == "key" ) {
105 profiles[i].key = value;
106 } else if( rest == "cert" ) {
107 profiles[i].cert = value;
109 std::cout << "invalid line: " << line1 << std::endl;
115 std::cout << profiles.size() << " profiles loaded." << std::endl;
118 std::cerr << "Missing config property key.directory" << std::endl;
124 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
125 std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
128 std::shared_ptr<Job> job = jp->fetchJob();
131 std::cout << "Nothing to work on" << std::endl;
136 if( job->task == "sign" ) {
138 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
141 std::cout << "wasn't able to load CSR" << std::endl;
145 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
146 std::ifstream t( cert->csr );
147 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
149 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
150 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
152 jp->writeBack( job, res );
153 } catch( const char* c ) {
154 std::cerr << "ERROR: " << c << std::endl;
156 } catch( std::string c ) {
157 std::cerr << "ERROR: " << c << std::endl;
161 std::cout << "Unknown job type" << job->task << std::endl;
164 if( DAEMON && !jp->finishJob( job ) ) {
168 if( !DAEMON || once ) {