]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
193626fe906edaa26607fdf260d374061e7b6721
[cassiopeia.git] / src / apps / client.cpp
1 #include <sys/stat.h>
2 #include <unistd.h>
3
4 #include <iostream>
5 #include <fstream>
6 #include <streambuf>
7 #include <unordered_map>
8
9 #include "db/database.h"
10 #include "db/mysql.h"
11 #include "crypto/simpleOpensslSigner.h"
12 #include "crypto/remoteSigner.h"
13 #include "crypto/sslUtil.h"
14 #include "util.h"
15 #include "io/bios.h"
16 #include "io/slipBio.h"
17 #include "config.h"
18
19 #ifdef NO_DAEMON
20 #define DAEMON false
21 #else
22 #define DAEMON true
23 #endif
24
25 extern std::string keyDir;
26 extern std::string sqlHost, sqlUser, sqlPass, sqlDB;
27 extern std::string serialPath;
28 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
29
30 void checkCRLs( std::shared_ptr<Signer> sign ) {
31     std::cout << "Signing CRLs" << std::endl;
32
33     for( auto& x : CAs ) {
34         std::cout << "Checking: " << x.first << std::endl;
35
36         if( !x.second->crlNeedsResign() ) {
37             std::cout << "Skipping Resigning CRL: " + x.second->name << std::endl;
38             continue;
39         }
40
41         std::cout << "Resigning CRL: " + x.second->name << std::endl;
42
43         try {
44             std::vector<std::string> serials;
45             std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( x.second, serials );
46         } catch( const char* c ) {
47             std::cout << "Exception: " << c << std::endl;
48         }
49     }
50 }
51
52 int main( int argc, const char* argv[] ) {
53     ( void ) argc;
54     ( void ) argv;
55     bool once = false;
56
57     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
58         once = true;
59     }
60
61     std::string path;
62
63 #ifdef NDEBUG
64     path = "/etc/cacert/cassiopeia/cassiopeia.conf";
65 #else
66     path = "config.txt";
67 #endif
68
69     if( parseConfig( path ) != 0 ) {
70         return -1;
71     }
72
73     if( serialPath == "" ) {
74         std::cout << "Error: no serial device is given" << std::endl;
75         return -1;
76     }
77
78     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
79     std::shared_ptr<BIO> b = openSerial( serialPath );
80     std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
81     static_cast<SlipBIO*>( slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
82     std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
83     // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
84
85     time_t lastCRLCheck = 0;
86
87     while( true ) {
88         time_t current;
89         time( &current );
90
91         if( lastCRLCheck + 30 * 60 < current ) {
92             // todo set good log TODO FIXME
93             sign->setLog( std::shared_ptr<std::ostream>(
94                 &std::cout,
95                 []( std::ostream * o ) {
96                     ( void ) o;
97                 } ) );
98             checkCRLs( sign );
99             lastCRLCheck = current;
100         }
101
102         std::shared_ptr<Job> job = jp->fetchJob();
103
104         if( !job ) {
105             std::cout << "Nothing to work on" << std::endl;
106             sleep( 5 );
107             continue;
108         }
109
110         std::shared_ptr<std::ofstream> logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
111
112         std::ofstream& log = *( logPtr.get() );
113
114         sign->setLog( logPtr );
115         log << "TASK ID: " << job->id << std::endl;
116         log << "TRY: " << job->warning << std::endl;
117         log << "TARGET: " << job->target << std::endl;
118         log << "TASK: " << job->task << std::endl << std::endl;
119
120         if( job->task == "sign" ) {
121             try {
122                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
123                 cert->wishFrom = job->from;
124                 cert->wishTo = job->to;
125                 log << "INFO: message digest: " << cert->md << std::endl;
126                 log << "INFO: profile id: " << cert->profile << std::endl;
127
128                 for( auto& SAN : cert->SANs ) {
129                     log << "INFO: SAN " << SAN->type << ": " << SAN->content;
130                 }
131
132                 for( auto& AVA : cert->AVAs ) {
133                     log << "INFO: AVA " << AVA->name << ": " << AVA->value;
134                 }
135
136                 if( !cert ) {
137                     std::cout << "wasn't able to load CSR" << std::endl;
138                     jp->failJob( job );
139                     continue;
140                 }
141
142                 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
143                 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
144                 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
145
146                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
147
148                 if( !res ) {
149                     log << "ERROR: The signer failed. There was no certificate." << std::endl;
150                     jp->failJob( job );
151                     continue;
152                 }
153
154                 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
155                 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
156                 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
157
158                 if( fn.empty() ) {
159                     log << "ERROR: Writeback of the certificate failed." << std::endl;
160                     jp->failJob( job );
161                     continue;
162                 }
163
164                 res->crt_name = fn;
165                 jp->writeBack( job, res ); //! \FIXME: Check return value
166                 log << "FINE: signing done." << std::endl;
167
168                 if( DAEMON ) {
169                     jp->finishJob( job );
170                 }
171
172                 continue;
173             } catch( const char* c ) {
174                 log << "ERROR: " << c << std::endl;
175             } catch( std::string& c ) {
176                 log << "ERROR: " << c << std::endl;
177             }
178
179             try {
180                 jp->failJob( job );
181             } catch( const char* c ) {
182                 log << "ERROR: " << c << std::endl;
183             } catch( std::string& c ) {
184                 log << "ERROR: " << c << std::endl;
185             }
186         } else if( job->task == "revoke" ) {
187             try {
188                 auto data = jp->getRevocationInfo( job );
189                 std::vector<std::string> serials;
190                 serials.push_back( data.first );
191                 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
192                 std::string date = rev.second;
193                 const unsigned char* pos = ( const unsigned char* ) date.data();
194                 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
195
196                 jp->writeBackRevocation( job, timeToString( time ) );
197                 jp->finishJob( job );
198             } catch( const char* c ) {
199                 std::cout << "Exception: " << c << std::endl;
200             } catch( const std::string& c ) {
201                 std::cout << "Exception: " << c << std::endl;
202             }
203         } else {
204             log << "Unknown job type" << job->task << std::endl;
205             jp->failJob( job );
206         }
207
208         if( !DAEMON || once ) {
209             return 0;
210         }
211     }
212 }