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