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