]> WPIA git - cassiopeia.git/blob - src/config.cpp
chg: Build two binaries for the signer
[cassiopeia.git] / src / config.cpp
1 #include <iostream>
2 #include <vector>
3 #include <fstream>
4
5 #include "sslUtil.h"
6
7 std::string keyDir;
8 std::vector<Profile> profiles;
9 std::string sqlHost, sqlUser, sqlPass, sqlDB;
10
11 int parseConfig( std::string path ) {
12     std::ifstream config;
13     config.open( path );
14
15     if( !config.is_open() ) {
16         std::cerr << "config missing" << std::endl;
17         return 1;
18     }
19
20     std::string line1;
21
22     while( std::getline( config, line1 ) ) {
23         if( line1[0] == '#' || line1.size() == 0 ) {
24             continue;
25         }
26
27         int splitter = line1.find( "=" );
28
29         if( splitter == -1 ) {
30             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
31             continue;
32         }
33
34         std::string key = line1.substr( 0, splitter );
35         std::string value = line1.substr( splitter + 1 );
36
37         if( key == "key.directory" ) {
38             keyDir = value;
39             continue;
40         } else if( key == "sql.host" ) {
41             sqlHost = value;
42         } else if( key == "sql.user" ) {
43             sqlUser = value;
44         } else if( key == "sql.password" ) {
45             sqlPass = value;
46         } else if( key == "sql.database" ) {
47             sqlDB = value;
48         }
49
50         if( key.compare( 0, 8, "profile." ) == 0 ) {
51             int numE = key.find( ".", 9 );
52
53             if( numE == 0 ) {
54                 std::cout << "invalid line: " << line1 << std::endl;
55                 continue;
56             }
57
58             unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
59             std::string rest = key.substr( numE + 1 );
60
61             if( i + 1 > profiles.size() ) {
62                 profiles.resize( i + 1 );
63             }
64
65             if( rest == "key" ) {
66                 profiles[i].key = value;
67             } else if( rest == "cert" ) {
68                 profiles[i].cert = value;
69             } else if( rest == "ku" ) {
70                 profiles[i].ku = value;
71             } else if( rest == "eku" ) {
72                 profiles[i].eku = value;
73             } else {
74                 std::cout << "invalid line: " << line1 << std::endl;
75                 continue;
76             }
77         }
78     }
79
80     for( auto& prof : profiles ) {
81         if( prof.cert != "" && prof.key != "" ) {
82             std::cout << "Loading profile... " << std::endl;
83             prof.ca = loadX509FromFile( prof.cert );
84             prof.caKey = loadPkeyFromFile( prof.key );
85         }
86     }
87
88     std::cout << profiles.size() << " profiles loaded." << std::endl;
89
90     if( keyDir == "" ) {
91         std::cerr << "Missing config property key.directory" << std::endl;
92         return -1;
93     }
94
95     config.close();
96     return 0;
97 }