]> WPIA git - cassiopeia.git/blob - src/main.cpp
aed34a2d7cec5537ba95fae4ba8236a1a0561304
[cassiopeia.git] / src / main.cpp
1 #include <sys/stat.h>
2 #include <unistd.h>
3
4 #include <iostream>
5 #include <fstream>
6 #include <streambuf>
7
8 #include "database.h"
9 #include "mysql.h"
10 #include "simpleOpensslSigner.h"
11
12 #ifdef NO_DAEMON
13 #define DAEMON false
14 #else
15 #define DAEMON true
16 #endif
17
18 std::string keyDir;
19 std::vector<Profile> profiles;
20 std::string sqlHost, sqlUser, sqlPass, sqlDB;
21
22 std::string writeBackFile( uint32_t serial, std::string cert ) {
23     std::string filename = "keys";
24     mkdir( filename.c_str(), 0755 );
25     filename += "/crt";
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";
30     std::ofstream file;
31     file.open( filename.c_str() );
32     file << cert.c_str();
33     file.close();
34     std::cout << "wrote to " << filename << std::endl;
35     return filename;
36 }
37
38 int main( int argc, const char* argv[] ) {
39     ( void ) argc;
40     ( void ) argv;
41     bool once = false;
42
43     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
44         once = true;
45     }
46
47     std::ifstream config;
48     config.open( "config.txt" );
49
50     if( !config.is_open() ) {
51         std::cerr << "config missing" << std::endl;
52         return 1;
53     }
54
55     std::string line1;
56
57     while( config >> line1 ) {
58         if( line1[0] == '#' ) {
59             continue;
60         }
61
62         int splitter = line1.find( "=" );
63
64         if( splitter == -1 ) {
65             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
66             continue;
67         }
68
69         std::string key = line1.substr( 0, splitter );
70         std::string value = line1.substr( splitter + 1 );
71
72         if( key == "key.directory" ) {
73             keyDir = value;
74             continue;
75         } else if( key == "sql.host" ) {
76             sqlHost = value;
77         } else if( key == "sql.user" ) {
78             sqlUser = value;
79         } else if( key == "sql.password" ) {
80             sqlPass = value;
81         } else if( key == "sql.database" ) {
82             sqlDB = value;
83         }
84
85         if( key.compare( 0, 8, "profile." ) == 0 ) {
86             int numE = key.find( ".", 9 );
87
88             if( numE == 0 ) {
89                 std::cout << "invalid line: " << line1 << std::endl;
90                 continue;
91             }
92
93             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
94             std::string rest = key.substr( numE + 1 );
95
96             if( i + 1 > profiles.size() ) {
97                 profiles.resize( i + 1 );
98             }
99
100             if( rest == "key" ) {
101                 profiles[i].key = value;
102             } else if( rest == "cert" ) {
103                 profiles[i].cert = value;
104             } else {
105                 std::cout << "invalid line: " << line1 << std::endl;
106                 continue;
107             }
108         }
109     }
110
111     std::cout << profiles.size() << " profiles loaded." << std::endl;
112
113     if( keyDir == "" ) {
114         std::cerr << "Missing config property key.directory" << std::endl;
115         return -1;
116     }
117
118     config.close();
119
120     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
121     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
122
123     while( true ) {
124         std::shared_ptr<Job> job = jp->fetchJob();
125
126         if( !job ) {
127             std::cout << "Nothing to work on" << std::endl;
128             sleep( 5 );
129             continue;
130         }
131
132         if( job->task == "sign" ) {
133             try {
134                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
135
136                 if( !cert ) {
137                     std::cout << "wasn't able to load CSR" << std::endl;
138                     return 2;
139                 }
140
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>() );
144
145                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
146                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
147                 res->crt_name = fn;
148                 jp->writeBack( job, res );
149             } catch( const char* c ) {
150                 std::cerr << "ERROR: " << c << std::endl;
151                 return 2;
152             } catch( std::string c ) {
153                 std::cerr << "ERROR: " << c << std::endl;
154                 return 2;
155             }
156         } else {
157             std::cout << "Unknown job type" << job->task << std::endl;
158         }
159
160         if( DAEMON && !jp->finishJob( job ) ) {
161             return 1;
162         }
163
164         if( !DAEMON || once ) {
165             return 0;
166         }
167     }
168 }