]> WPIA git - cassiopeia.git/blob - src/config.cpp
cln: Move code around, cleanup structure
[cassiopeia.git] / src / config.cpp
1 #include <iostream>
2 #include <vector>
3 #include <fstream>
4 #include <dirent.h>
5 #include <unordered_map>
6
7 #include "crypto/sslUtil.h"
8
9 std::string keyDir;
10 std::unordered_map<std::string, Profile> profiles;
11 std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
12 std::string sqlHost, sqlUser, sqlPass, sqlDB;
13 std::string serialPath;
14
15 std::shared_ptr<std::unordered_map<std::string, std::string>> parseConf( std::string path ) {
16     std::shared_ptr<std::unordered_map<std::string, std::string>> map( new std::unordered_map<std::string, std::string>() );
17     std::ifstream config;
18     config.open( path );
19
20     if( !config.is_open() ) {
21         std::cout << "Where is " << path << "?" << std::endl;
22         throw "Config missing";
23     }
24
25     std::string line1;
26
27     while( std::getline( config, line1 ) ) {
28         if( line1[0] == '#' || line1.size() == 0 ) {
29             continue;
30         }
31
32         int splitter = line1.find( "=" );
33
34         if( splitter == -1 ) {
35             std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
36             continue;
37         }
38
39         std::string key = line1.substr( 0, splitter );
40         std::string value = line1.substr( splitter + 1 );
41         map->emplace( key, value );
42     }
43
44     config.close();
45
46     return map;
47 }
48
49 int parseConfig( std::string path ) {
50
51     auto masterConf = parseConf( path );
52
53     keyDir = masterConf->at( "key.directory" );
54     sqlHost = masterConf->at( "sql.host" );
55     sqlUser = masterConf->at( "sql.user" );
56     sqlPass = masterConf->at( "sql.password" );
57     sqlDB = masterConf->at( "sql.database" );
58     serialPath = masterConf->at( "serialPath" );
59
60     CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
61
62     DIR* dp;
63     struct dirent* ep;
64     dp = opendir( "profiles" );
65
66     if( dp == NULL ) {
67         std::cerr << "Profiles not found " << std::endl;
68         return -1;
69     }
70
71     while( ( ep = readdir( dp ) ) ) {
72         if( ep->d_name[0] == '.' ) {
73             continue;
74         }
75
76         std::string profileName( ep->d_name );
77
78         int splitter = profileName.find( "-" );
79
80         if( splitter == -1 ) {
81             std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
82             continue;
83         }
84
85         std::string id = profileName.substr( 0, splitter );
86
87         if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
88             std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
89             continue;
90         }
91
92         auto map = parseConf( std::string( "profiles/" ) + profileName );
93
94         profileName = profileName.substr( 0, profileName.size() - 4 );
95
96         Profile prof;
97         prof.id = std::stoi( id );
98         prof.eku = map->at( "eku" );
99         prof.ku = map->at( "ku" );
100
101         if( CAs.find( map->at( "ca" ) ) == CAs.end() ) {
102             std::shared_ptr<CAConfig> ca( new CAConfig( map->at( "ca" ) ) );
103             CAs.emplace( map->at( "ca" ), ca );
104         }
105
106         prof.ca = CAs.at( map->at( "ca" ) );
107
108         profiles.emplace( profileName, prof );
109         std::cout << "Profile: " << profileName << " up and running." << std::endl;
110     }
111
112     ( void ) closedir( dp );
113
114
115     std::cout << profiles.size() << " profiles loaded." << std::endl;
116
117     if( keyDir == "" ) {
118         std::cerr << "Missing config property key.directory" << std::endl;
119         return -1;
120     }
121
122     return 0;
123 }