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