]> WPIA git - cassiopeia.git/blob - src/main.cpp
1fb8d83ab43a1063dc3dbe60106683ad18af882b
[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     bool once = false;
60
61     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
62         once = true;
63     }
64
65     std::ifstream config;
66     config.open( "config.txt" );
67
68     if( !config.is_open() ) {
69         std::cerr << "config missing" << std::endl;
70         return 1;
71     }
72
73     std::string line1;
74
75     while( config >> line1 ) {
76         if( line1[0] == '#' ) {
77             continue;
78         }
79
80         int splitter = line1.find( "=" );
81
82         if( splitter == -1 ) {
83             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
84             continue;
85         }
86
87         std::string key = line1.substr( 0, splitter );
88         std::string value = line1.substr( splitter + 1 );
89
90         if( key == "key.directory" ) {
91             keyDir = value;
92             continue;
93         } else if( key == "sql.host" ) {
94             sqlHost = value;
95         } else if( key == "sql.user" ) {
96             sqlUser = value;
97         } else if( key == "sql.password" ) {
98             sqlPass = value;
99         } else if( key == "sql.database" ) {
100             sqlDB = value;
101         }
102
103         if( key.compare( 0, 8, "profile." ) == 0 ) {
104             int numE = key.find( ".", 9 );
105
106             if( numE == 0 ) {
107                 std::cout << "invalid line: " << line1 << std::endl;
108                 continue;
109             }
110
111             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
112             std::string rest = key.substr( numE + 1 );
113
114             if( i + 1 > profiles.size() ) {
115                 profiles.resize( i + 1 );
116             }
117
118             if( rest == "key" ) {
119                 profiles[i].key = value;
120             } else if( rest == "cert" ) {
121                 profiles[i].cert = value;
122             } else {
123                 std::cout << "invalid line: " << line1 << std::endl;
124                 continue;
125             }
126         }
127     }
128
129     std::cout << profiles.size() << " profiles loaded." << std::endl;
130
131     if( keyDir == "" ) {
132         std::cerr << "Missing config property key.directory" << std::endl;
133         return -1;
134     }
135
136     config.close();
137
138     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
139     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
140
141     while( true ) {
142         std::shared_ptr<Job> job = jp->fetchJob();
143
144         if( !job ) {
145             std::cout << "Nothing to work on" << std::endl;
146             sleep( 5 );
147             continue;
148         }
149
150         if( job->task == "sign" ) {
151             try {
152                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
153
154                 if( !cert ) {
155                     std::cout << "wasn't able to load CSR" << std::endl;
156                     return 2;
157                 }
158
159                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
160                 std::ifstream t( cert->csr );
161                 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
162
163                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
164                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
165                 res->crt_name = fn;
166                 jp->writeBack( job, res );
167             } catch( const char* c ) {
168                 std::cerr << "ERROR: " << c << std::endl;
169                 return 2;
170             } catch( std::string c ) {
171                 std::cerr << "ERROR: " << c << std::endl;
172                 return 2;
173             }
174         } else {
175             std::cout << "Unknown job type" << job->task << std::endl;
176         }
177
178         if( DAEMON && !jp->finishJob( job ) ) {
179             return 1;
180         }
181
182         if( !DAEMON || once ) {
183             return 0;
184         }
185     }
186 }