]> WPIA git - cassiopeia.git/blob - src/main.cpp
add: Initial configuration file implementation
[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
39 std::string writeBackFile( uint32_t serial, std::string cert ) {
40     std::string filename = "keys";
41     mkdir( filename.c_str(), 0755 );
42     filename += "/crt";
43     mkdir( filename.c_str(), 0755 );
44     filename += "/" + std::to_string( serial / 1000 );
45     mkdir( filename.c_str(), 0755 );
46     filename += "/" + std::to_string( serial ) + ".crt";
47     std::ofstream file;
48     file.open( filename.c_str() );
49     file << cert.c_str();
50     file.close();
51     return filename;
52 }
53
54 int main( int argc, const char* argv[] ) {
55     if( argc < 2 ) {
56         std::cout << argv[0] << " password" << std::endl;
57         return 1;
58     }
59
60     std::ifstream config;
61     config.open( "config.txt" );
62
63     if( !config.is_open() ) {
64         std::cerr << "config missing" << std::endl;
65         return 1;
66     }
67
68     std::string line1;
69
70     while( config >> line1 ) {
71         if( line1[0] == '#' ) {
72             continue;
73         }
74
75         int splitter = line1.find( "=" );
76
77         if( splitter == -1 ) {
78             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
79             continue;
80         }
81
82         std::string key = line1.substr( 0, splitter );
83         std::string value = line1.substr( splitter + 1 );
84
85         if( key == "key.directory" ) {
86             keyDir = value;
87             continue;
88         }
89
90         if( key.compare( 0, 8, "profile." ) == 0 ) {
91             int numE = key.find( ".", 9 );
92
93             if( numE == 0 ) {
94                 std::cout << "invalid line: " << line1 << std::endl;
95                 continue;
96             }
97
98             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
99             std::string rest = key.substr( numE + 1 );
100
101             if( i + 1 > profiles.size() ) {
102                 profiles.resize( i + 1 );
103             }
104
105             if( rest == "key" ) {
106                 profiles[i].cert = value;
107             } else if( rest == "cert" ) {
108                 profiles[i].key = value;
109             } else {
110                 std::cout << "invalid line: " << line1 << std::endl;
111                 continue;
112             }
113         }
114     }
115
116     std::cout << profiles.size() << " profiles loaded." << std::endl;
117
118     if( keyDir == "" ) {
119         std::cerr << "Missing config property key.directory" << std::endl;
120         return -1;
121     }
122
123     config.close();
124
125     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( "localhost", "cacert", argv[1], "cacert" ) );
126     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
127
128     while( true ) {
129         std::shared_ptr<Job> job = jp->fetchJob();
130
131         if( !job ) {
132             std::cout << "Nothing to work on" << std::endl;
133             sleep( 5 );
134             continue;
135         }
136
137         if( job->task == "sign" ) {
138             try {
139                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
140
141                 if( !cert ) {
142                     std::cout << "wasn't able to load CSR" << std::endl;
143                     return 2;
144                 }
145
146                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
147                 std::ifstream t( cert->csr );
148                 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
149
150                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
151                 std::string fn = writeBackFile( res->serial, res->certificate );
152                 res->crt_name = fn;
153                 jp->writeBack( job, res );
154             } catch( const char* c ) {
155                 std::cerr << c << std::endl;
156                 return 2;
157             }
158         } else {
159             std::cout << "Unknown job type" << job->task << std::endl;
160         }
161
162         if( DAEMON && !jp->finishJob( job ) ) {
163             return 1;
164         }
165
166         if( !DAEMON ) {
167             return 0;
168         }
169     }
170 }