]> WPIA git - cassiopeia.git/blob - src/main.cpp
2d42e848461e2e34555d3b1f857003ed66cd5047
[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 <iostream>
20 #include <fstream>
21 #include <streambuf>
22
23 #include "database.h"
24 #include "mysql.h"
25 #include "simpleOpensslSigner.h"
26
27 int main( int argc, const char* argv[] ) {
28     if( argc < 2 ) {
29         std::cout << argv[0] << " password" << std::endl;
30         return 1;
31     }
32
33     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( "localhost", "cacert", argv[1], "cacert" ) );
34     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
35     std::shared_ptr<Job> job = jp->fetchJob();
36
37     if( !job ) {
38         std::cout << "Nothing to work on" << std::endl;
39         return 2;
40     }
41
42     if( job->task == "sign" ) {
43         try {
44             std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
45
46             if( !cert ) {
47                 std::cout << "wasn't able to load CSR" << std::endl;
48                 return 2;
49             }
50
51             std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
52             std::ifstream t( cert->csr );
53             cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
54             sign->sign( cert );
55         } catch( const char* c ) {
56             std::cerr << c << std::endl;
57             return 2;
58         }
59     }
60
61     if( !jp->finishJob( job ) ) {
62         return 1;
63     }
64
65     return 0;
66 }