]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
fix: clean SSL shutdown, reset, allowing deamon operation
[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 = "keys";
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         if( job->task == "sign" ) {
85             try {
86                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
87
88                 if( !cert ) {
89                     std::cout << "wasn't able to load CSR" << std::endl;
90                     return 2;
91                 }
92
93                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
94                 cert->csr_content = readFile( cert->csr );
95                 std::cout << cert->csr_content << " content " << std::endl;
96
97                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
98
99                 if( !res ) {
100                     std::cout << "Error no cert came back." << std::endl;
101                     continue;
102                 }
103
104                 std::cout << "did it!" << res->certificate << std::endl;
105                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
106                 res->crt_name = fn;
107                 jp->writeBack( job, res );
108                 std::cout << "wrote back" << std::endl;
109             } catch( const char* c ) {
110                 std::cerr << "ERROR: " << c << std::endl;
111                 return 2;
112             } catch( std::string c ) {
113                 std::cerr << "ERROR: " << c << std::endl;
114                 return 2;
115             }
116         } else {
117             std::cout << "Unknown job type" << job->task << std::endl;
118         }
119
120         if( DAEMON && !jp->finishJob( job ) ) {
121             return 1;
122         }
123
124         if( !DAEMON || once ) {
125             return 0;
126         }
127     }
128 }