]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
chg: Build two binaries for the signer
[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
28 std::string writeBackFile( uint32_t serial, std::string cert ) {
29     std::string filename = "keys";
30     mkdir( filename.c_str(), 0755 );
31     filename += "/crt";
32     mkdir( filename.c_str(), 0755 );
33     filename += "/" + std::to_string( serial / 1000 );
34     mkdir( filename.c_str(), 0755 );
35     filename += "/" + std::to_string( serial ) + ".crt";
36     writeFile( filename, cert );
37     std::cout << "wrote to " << filename << std::endl;
38     return filename;
39 }
40
41 int handlermain( int argc, const char* argv[] );
42
43 int main( int argc, const char* argv[] ) {
44     ( void ) argc;
45     ( void ) argv;
46     bool once = false;
47
48     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
49         once = true;
50     }
51
52     std::string path;
53
54     if( DAEMON ) {
55         path = "/etc/cacert/cassiopeia/cassiopeia.conf";
56     } else {
57         path = "config.txt";
58     }
59
60     if( parseConfig( path ) != 0 ) {
61         return -1;
62     }
63
64     if( argc == 0 ) {
65         return handlermain( argc, argv );
66     }
67
68     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
69     std::shared_ptr<BIO> b = openSerial( "/dev/ttyUSB0" );
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                 std::cout << "did it!" << res->certificate << std::endl;
99                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
100                 res->crt_name = fn;
101                 jp->writeBack( job, res );
102                 std::cout << "wrote back" << std::endl;
103             } catch( const char* c ) {
104                 std::cerr << "ERROR: " << c << std::endl;
105                 return 2;
106             } catch( std::string c ) {
107                 std::cerr << "ERROR: " << c << std::endl;
108                 return 2;
109             }
110         } else {
111             std::cout << "Unknown job type" << job->task << std::endl;
112         }
113
114         if( DAEMON && !jp->finishJob( job ) ) {
115             return 1;
116         }
117
118         if( !DAEMON || once ) {
119             return 0;
120         }
121     }
122 }