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 main( int argc, const char* argv[] ) {
41 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
48 config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
50 config.open( "config.txt" );
53 if( !config.is_open() ) {
54 std::cerr << "config missing" << std::endl;
60 while( config >> line1 ) {
61 if( line1[0] == '#' ) {
65 int splitter = line1.find( "=" );
67 if( splitter == -1 ) {
68 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
72 std::string key = line1.substr( 0, splitter );
73 std::string value = line1.substr( splitter + 1 );
75 if( key == "key.directory" ) {
78 } else if( key == "sql.host" ) {
80 } else if( key == "sql.user" ) {
82 } else if( key == "sql.password" ) {
84 } else if( key == "sql.database" ) {
88 if( key.compare( 0, 8, "profile." ) == 0 ) {
89 int numE = key.find( ".", 9 );
92 std::cout << "invalid line: " << line1 << std::endl;
96 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
97 std::string rest = key.substr( numE + 1 );
99 if( i + 1 > profiles.size() ) {
100 profiles.resize( i + 1 );
103 if( rest == "key" ) {
104 profiles[i].key = value;
105 } else if( rest == "cert" ) {
106 profiles[i].cert = value;
108 std::cout << "invalid line: " << line1 << std::endl;
114 std::cout << profiles.size() << " profiles loaded." << std::endl;
117 std::cerr << "Missing config property key.directory" << std::endl;
123 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
124 std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
127 std::shared_ptr<Job> job = jp->fetchJob();
130 std::cout << "Nothing to work on" << std::endl;
135 if( job->task == "sign" ) {
137 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
140 std::cout << "wasn't able to load CSR" << std::endl;
144 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
145 cert->csr_content = readFile( cert->csr );
147 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
148 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
150 jp->writeBack( job, res );
151 } catch( const char* c ) {
152 std::cerr << "ERROR: " << c << std::endl;
154 } catch( std::string c ) {
155 std::cerr << "ERROR: " << c << std::endl;
159 std::cout << "Unknown job type" << job->task << std::endl;
162 if( DAEMON && !jp->finishJob( job ) ) {
166 if( !DAEMON || once ) {