10 #include "simpleOpensslSigner.h"
20 std::vector<Profile> profiles;
21 std::string sqlHost, sqlUser, sqlPass, sqlDB;
23 std::string writeBackFile( uint32_t serial, std::string cert ) {
24 std::string filename = "keys";
25 mkdir( filename.c_str(), 0755 );
27 mkdir( filename.c_str(), 0755 );
28 filename += "/" + std::to_string( serial / 1000 );
29 mkdir( filename.c_str(), 0755 );
30 filename += "/" + std::to_string( serial ) + ".crt";
31 writeFile( filename, cert );
32 std::cout << "wrote to " << filename << std::endl;
36 int handlermain( int argc, const char* argv[] );
38 int main( int argc, const char* argv[] ) {
43 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
50 config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
52 config.open( "config.txt" );
55 if( !config.is_open() ) {
56 std::cerr << "config missing" << std::endl;
62 while( config >> line1 ) {
63 if( line1[0] == '#' ) {
67 int splitter = line1.find( "=" );
69 if( splitter == -1 ) {
70 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
74 std::string key = line1.substr( 0, splitter );
75 std::string value = line1.substr( splitter + 1 );
77 if( key == "key.directory" ) {
80 } else if( key == "sql.host" ) {
82 } else if( key == "sql.user" ) {
84 } else if( key == "sql.password" ) {
86 } else if( key == "sql.database" ) {
90 if( key.compare( 0, 8, "profile." ) == 0 ) {
91 int numE = key.find( ".", 9 );
94 std::cout << "invalid line: " << line1 << std::endl;
98 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
99 std::string rest = key.substr( numE + 1 );
101 if( i + 1 > profiles.size() ) {
102 profiles.resize( i + 1 );
105 if( rest == "key" ) {
106 profiles[i].key = value;
107 } else if( rest == "cert" ) {
108 profiles[i].cert = value;
110 std::cout << "invalid line: " << line1 << std::endl;
116 std::cout << profiles.size() << " profiles loaded." << std::endl;
119 std::cerr << "Missing config property key.directory" << std::endl;
124 return handlermain( argc, argv );
126 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
127 std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
130 std::shared_ptr<Job> job = jp->fetchJob();
133 std::cout << "Nothing to work on" << std::endl;
138 if( job->task == "sign" ) {
140 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
143 std::cout << "wasn't able to load CSR" << std::endl;
147 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
148 cert->csr_content = readFile( cert->csr );
150 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
151 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
153 jp->writeBack( job, res );
154 } catch( const char* c ) {
155 std::cerr << "ERROR: " << c << std::endl;
157 } catch( std::string c ) {
158 std::cerr << "ERROR: " << c << std::endl;
162 std::cout << "Unknown job type" << job->task << std::endl;
165 if( DAEMON && !jp->finishJob( job ) ) {
169 if( !DAEMON || once ) {