]> WPIA git - cassiopeia.git/blob - src/main.cpp
add: Plug things together so we can have TBSCertificates from the database
[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 #include "util.h"
12 #include "bios.h"
13 #include "slipBio.h"
14 #include "remoteSigner.h"
15 #include "sslUtil.h"
16
17 #ifdef NO_DAEMON
18 #define DAEMON false
19 #else
20 #define DAEMON true
21 #endif
22
23 std::string keyDir;
24 std::vector<Profile> profiles;
25 std::string sqlHost, sqlUser, sqlPass, sqlDB;
26
27 std::string writeBackFile( uint32_t serial, std::string cert ) {
28     std::string filename = "keys";
29     mkdir( filename.c_str(), 0755 );
30     filename += "/crt";
31     mkdir( filename.c_str(), 0755 );
32     filename += "/" + std::to_string( serial / 1000 );
33     mkdir( filename.c_str(), 0755 );
34     filename += "/" + std::to_string( serial ) + ".crt";
35     writeFile( filename, cert );
36     std::cout << "wrote to " << filename << std::endl;
37     return filename;
38 }
39
40 int parseConfig() {
41     std::ifstream config;
42
43     if( DAEMON ) {
44         config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
45     } else {
46         config.open( "config.txt" );
47     }
48
49     if( !config.is_open() ) {
50         std::cerr << "config missing" << std::endl;
51         return 1;
52     }
53
54     std::string line1;
55
56     while( config >> line1 ) {
57         if( line1[0] == '#' ) {
58             continue;
59         }
60
61         int splitter = line1.find( "=" );
62
63         if( splitter == -1 ) {
64             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
65             continue;
66         }
67
68         std::string key = line1.substr( 0, splitter );
69         std::string value = line1.substr( splitter + 1 );
70
71         if( key == "key.directory" ) {
72             keyDir = value;
73             continue;
74         } else if( key == "sql.host" ) {
75             sqlHost = value;
76         } else if( key == "sql.user" ) {
77             sqlUser = value;
78         } else if( key == "sql.password" ) {
79             sqlPass = value;
80         } else if( key == "sql.database" ) {
81             sqlDB = value;
82         }
83
84         if( key.compare( 0, 8, "profile." ) == 0 ) {
85             int numE = key.find( ".", 9 );
86
87             if( numE == 0 ) {
88                 std::cout << "invalid line: " << line1 << std::endl;
89                 continue;
90             }
91
92             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
93             std::string rest = key.substr( numE + 1 );
94
95             if( i + 1 > profiles.size() ) {
96                 profiles.resize( i + 1 );
97             }
98
99             if( rest == "key" ) {
100                 profiles[i].key = value;
101             } else if( rest == "cert" ) {
102                 profiles[i].cert = value;
103             } else {
104                 std::cout << "invalid line: " << line1 << std::endl;
105                 continue;
106             }
107         }
108     }
109
110     std::cout << profiles.size() << " profiles loaded." << std::endl;
111
112     if( keyDir == "" ) {
113         std::cerr << "Missing config property key.directory" << std::endl;
114         return -1;
115     }
116
117     config.close();
118
119     return 0;
120 }
121
122 int handlermain( int argc, const char* argv[] );
123
124 int main( int argc, const char* argv[] ) {
125     ( void ) argc;
126     ( void ) argv;
127     bool once = false;
128
129     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
130         once = true;
131     }
132
133     if( parseConfig() != 0 ) {
134         return -1;
135     }
136
137     if( argc == 0 ) {
138         return handlermain( argc, argv );
139     }
140
141     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
142     std::shared_ptr<BIO> b = openSerial( "/dev/ttyUSB0" );
143     std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
144     ( ( SlipBIO* )slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
145     std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
146     // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
147
148     while( true ) {
149         std::shared_ptr<Job> job = jp->fetchJob();
150
151         if( !job ) {
152             std::cout << "Nothing to work on" << std::endl;
153             sleep( 5 );
154             continue;
155         }
156
157         if( job->task == "sign" ) {
158             try {
159                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
160
161                 if( !cert ) {
162                     std::cout << "wasn't able to load CSR" << std::endl;
163                     return 2;
164                 }
165
166                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
167                 cert->csr_content = readFile( cert->csr );
168                 std::cout << cert->csr_content << " content " << std::endl;
169
170                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
171                 std::cout << "did it!" << res->certificate << std::endl;
172                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
173                 res->crt_name = fn;
174                 jp->writeBack( job, res );
175                 std::cout << "wrote back" << std::endl;
176             } catch( const char* c ) {
177                 std::cerr << "ERROR: " << c << std::endl;
178                 return 2;
179             } catch( std::string c ) {
180                 std::cerr << "ERROR: " << c << std::endl;
181                 return 2;
182             }
183         } else {
184             std::cout << "Unknown job type" << job->task << std::endl;
185         }
186
187         if( DAEMON && !jp->finishJob( job ) ) {
188             return 1;
189         }
190
191         if( !DAEMON || once ) {
192             return 0;
193         }
194     }
195 }