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