]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
upd: Better configuration, respecting profiles
[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
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 #include "config.h"
17
18 #ifdef NO_DAEMON
19 #define DAEMON false
20 #else
21 #define DAEMON true
22 #endif
23
24 extern std::string keyDir;
25 extern std::vector<Profile> profiles;
26 extern std::string sqlHost, sqlUser, sqlPass, sqlDB;
27 extern std::string serialPath;
28
29 int main( int argc, const char* argv[] ) {
30     ( void ) argc;
31     ( void ) argv;
32     bool once = false;
33
34     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
35         once = true;
36     }
37
38     std::string path;
39
40 #ifdef NDEBUG
41     path = "/etc/cacert/cassiopeia/cassiopeia.conf";
42 #else
43     path = "config.txt";
44 #endif
45
46     if( parseConfig( path ) != 0 ) {
47         return -1;
48     }
49
50     if( serialPath == "" ) {
51         std::cout << "Error: no serial device is given" << std::endl;
52         return -1;
53     }
54
55     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
56     std::shared_ptr<BIO> b = openSerial( serialPath );
57     std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
58     ( ( SlipBIO* )slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
59     std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
60     // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
61
62     while( true ) {
63         std::shared_ptr<Job> job = jp->fetchJob();
64
65         if( !job ) {
66             std::cout << "Nothing to work on" << std::endl;
67             sleep( 5 );
68             continue;
69         }
70
71         std::ofstream* logP = new std::ofstream( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
72         std::shared_ptr<std::ofstream> logPtr(
73             logP,
74             []( std::ofstream * ptr ) {
75                 ( *ptr ).close();
76                 delete ptr;
77             } );
78         std::ofstream& log = *logP;
79
80         sign->setLog( logPtr );
81         log << "TASK ID: " << job->id << std::endl;
82         log << "TRY: " << job->warning << std::endl;
83         log << "TARGET: " << job->target << std::endl;
84         log << "TASK: " << job->task << std::endl << std::endl;
85
86         if( job->task == "sign" ) {
87             try {
88                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
89                 log << "INFO: message digest: " << cert->md << std::endl;
90                 log << "INFO: profile id: " << cert->profile << std::endl;
91
92                 for( auto& SAN : cert->SANs ) {
93                     log << "INFO: SAN " << SAN->type << ": " << SAN->content;
94                 }
95
96                 for( auto& AVA : cert->AVAs ) {
97                     log << "INFO: AVA " << AVA->name << ": " << AVA->value;
98                 }
99
100                 if( !cert ) {
101                     std::cout << "wasn't able to load CSR" << std::endl;
102                     jp->failJob( job );
103                     continue;
104                 }
105
106                 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
107                 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
108                 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
109
110                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
111
112                 if( !res ) {
113                     log << "ERROR: The signer failed. There was no certificate." << std::endl;
114                     jp->failJob( job );
115                     continue;
116                 }
117
118                 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
119                 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
120                 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
121                 res->crt_name = fn;
122                 jp->writeBack( job, res );
123                 log << "FINE: signing done." << std::endl;
124
125                 if( DAEMON ) {
126                     jp->finishJob( job );
127                 }
128
129                 continue;
130             } catch( const char* c ) {
131                 log << "ERROR: " << c << std::endl;
132             } catch( std::string c ) {
133                 log << "ERROR: " << c << std::endl;
134             }
135
136             try {
137                 jp->failJob( job );
138             } catch( const char* c ) {
139                 log << "ERROR: " << c << std::endl;
140             } catch( std::string c ) {
141                 log << "ERROR: " << c << std::endl;
142             }
143         } else {
144             log << "Unknown job type" << job->task << std::endl;
145         }
146
147         if( !DAEMON || once ) {
148             return 0;
149         }
150     }
151 }