]> WPIA git - cassiopeia.git/blob - src/main.cpp
upd: Prevent unconfigured startup by checking service defaults
[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     if(DAEMON){
49       config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
50     }else{
51       config.open( "config.txt" );
52     }
53
54     if( !config.is_open() ) {
55         std::cerr << "config missing" << std::endl;
56         return 1;
57     }
58
59     std::string line1;
60
61     while( config >> line1 ) {
62         if( line1[0] == '#' ) {
63             continue;
64         }
65
66         int splitter = line1.find( "=" );
67
68         if( splitter == -1 ) {
69             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
70             continue;
71         }
72
73         std::string key = line1.substr( 0, splitter );
74         std::string value = line1.substr( splitter + 1 );
75
76         if( key == "key.directory" ) {
77             keyDir = value;
78             continue;
79         } else if( key == "sql.host" ) {
80             sqlHost = value;
81         } else if( key == "sql.user" ) {
82             sqlUser = value;
83         } else if( key == "sql.password" ) {
84             sqlPass = value;
85         } else if( key == "sql.database" ) {
86             sqlDB = value;
87         }
88
89         if( key.compare( 0, 8, "profile." ) == 0 ) {
90             int numE = key.find( ".", 9 );
91
92             if( numE == 0 ) {
93                 std::cout << "invalid line: " << line1 << std::endl;
94                 continue;
95             }
96
97             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
98             std::string rest = key.substr( numE + 1 );
99
100             if( i + 1 > profiles.size() ) {
101                 profiles.resize( i + 1 );
102             }
103
104             if( rest == "key" ) {
105                 profiles[i].key = value;
106             } else if( rest == "cert" ) {
107                 profiles[i].cert = value;
108             } else {
109                 std::cout << "invalid line: " << line1 << std::endl;
110                 continue;
111             }
112         }
113     }
114
115     std::cout << profiles.size() << " profiles loaded." << std::endl;
116
117     if( keyDir == "" ) {
118         std::cerr << "Missing config property key.directory" << std::endl;
119         return -1;
120     }
121
122     config.close();
123
124     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
125     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
126
127     while( true ) {
128         std::shared_ptr<Job> job = jp->fetchJob();
129
130         if( !job ) {
131             std::cout << "Nothing to work on" << std::endl;
132             sleep( 5 );
133             continue;
134         }
135
136         if( job->task == "sign" ) {
137             try {
138                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
139
140                 if( !cert ) {
141                     std::cout << "wasn't able to load CSR" << std::endl;
142                     return 2;
143                 }
144
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>() );
148
149                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
150                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
151                 res->crt_name = fn;
152                 jp->writeBack( job, res );
153             } catch( const char* c ) {
154                 std::cerr << "ERROR: " << c << std::endl;
155                 return 2;
156             } catch( std::string c ) {
157                 std::cerr << "ERROR: " << c << std::endl;
158                 return 2;
159             }
160         } else {
161             std::cout << "Unknown job type" << job->task << std::endl;
162         }
163
164         if( DAEMON && !jp->finishJob( job ) ) {
165             return 1;
166         }
167
168         if( !DAEMON || once ) {
169             return 0;
170         }
171     }
172 }