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