]> WPIA git - cassiopeia.git/blob - src/main.cpp
56ff30fd3e8cc8a2670b4832b1297bd141737e8f
[cassiopeia.git] / src / main.cpp
1 /*
2     Cassiopeia - CAcert signing module
3     Copyright (C) 2014  CAcert Inc.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include <iostream>
23 #include <fstream>
24 #include <streambuf>
25
26 #include "database.h"
27 #include "mysql.h"
28 #include "simpleOpensslSigner.h"
29
30 #ifdef NO_DAEMON
31 #define DAEMON false
32 #else
33 #define DAEMON true
34 #endif
35
36 std::string keyDir;
37 std::vector<Profile> profiles;
38 std::string sqlHost, sqlUser, sqlPass, sqlDB;
39
40 std::string writeBackFile( uint32_t serial, std::string cert ) {
41     std::string filename = "keys";
42     mkdir( filename.c_str(), 0755 );
43     filename += "/crt";
44     mkdir( filename.c_str(), 0755 );
45     filename += "/" + std::to_string( serial / 1000 );
46     mkdir( filename.c_str(), 0755 );
47     filename += "/" + std::to_string( serial ) + ".crt";
48     std::ofstream file;
49     file.open( filename.c_str() );
50     file << cert.c_str();
51     file.close();
52     std::cout << "wrote to " << filename << std::endl;
53     return filename;
54 }
55
56 int main( int argc, const char* argv[] ) {
57     ( void ) argc;
58     ( void ) argv;
59     std::ifstream config;
60     config.open( "config.txt" );
61
62     if( !config.is_open() ) {
63         std::cerr << "config missing" << std::endl;
64         return 1;
65     }
66
67     std::string line1;
68
69     while( config >> line1 ) {
70         if( line1[0] == '#' ) {
71             continue;
72         }
73
74         int splitter = line1.find( "=" );
75
76         if( splitter == -1 ) {
77             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
78             continue;
79         }
80
81         std::string key = line1.substr( 0, splitter );
82         std::string value = line1.substr( splitter + 1 );
83
84         if( key == "key.directory" ) {
85             keyDir = value;
86             continue;
87         } else if( key == "sql.host" ) {
88             sqlHost = value;
89         } else if( key == "sql.user" ) {
90             sqlUser = value;
91         } else if( key == "sql.password" ) {
92             sqlPass = value;
93         } else if( key == "sql.database" ) {
94             sqlDB = value;
95         }
96
97         if( key.compare( 0, 8, "profile." ) == 0 ) {
98             int numE = key.find( ".", 9 );
99
100             if( numE == 0 ) {
101                 std::cout << "invalid line: " << line1 << std::endl;
102                 continue;
103             }
104
105             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
106             std::string rest = key.substr( numE + 1 );
107
108             if( i + 1 > profiles.size() ) {
109                 profiles.resize( i + 1 );
110             }
111
112             if( rest == "key" ) {
113                 profiles[i].key = value;
114             } else if( rest == "cert" ) {
115                 profiles[i].cert = value;
116             } else {
117                 std::cout << "invalid line: " << line1 << std::endl;
118                 continue;
119             }
120         }
121     }
122
123     std::cout << profiles.size() << " profiles loaded." << std::endl;
124
125     if( keyDir == "" ) {
126         std::cerr << "Missing config property key.directory" << std::endl;
127         return -1;
128     }
129
130     config.close();
131
132     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
133     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
134
135     while( true ) {
136         std::shared_ptr<Job> job = jp->fetchJob();
137
138         if( !job ) {
139             std::cout << "Nothing to work on" << std::endl;
140             sleep( 5 );
141             continue;
142         }
143
144         if( job->task == "sign" ) {
145             try {
146                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
147
148                 if( !cert ) {
149                     std::cout << "wasn't able to load CSR" << std::endl;
150                     return 2;
151                 }
152
153                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
154                 std::ifstream t( cert->csr );
155                 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
156
157                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
158                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
159                 res->crt_name = fn;
160                 jp->writeBack( job, res );
161             } catch( const char* c ) {
162                 std::cerr << c << std::endl;
163                 return 2;
164             }
165         } else {
166             std::cout << "Unknown job type" << job->task << std::endl;
167         }
168
169         if( DAEMON && !jp->finishJob( job ) ) {
170             return 1;
171         }
172
173         if( !DAEMON ) {
174             return 0;
175         }
176     }
177 }