]> WPIA git - cassiopeia.git/blob - src/config.cpp
add: Implement signing based on requested "wish time"
[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 parseProfiles() {
50     CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
51
52     DIR* dp;
53     struct dirent* ep;
54     dp = opendir( "profiles" );
55
56     if( dp == NULL ) {
57         std::cerr << "Profiles not found " << std::endl;
58         return -1;
59     }
60
61     while( ( ep = readdir( dp ) ) ) {
62         if( ep->d_name[0] == '.' ) {
63             continue;
64         }
65
66         std::string profileName( ep->d_name );
67
68         int splitter = profileName.find( "-" );
69
70         if( splitter == -1 ) {
71             std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
72             continue;
73         }
74
75         std::string id = profileName.substr( 0, splitter );
76
77         if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
78             std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
79             continue;
80         }
81
82         auto map = parseConf( std::string( "profiles/" ) + profileName );
83
84         profileName = profileName.substr( 0, profileName.size() - 4 );
85
86         Profile prof;
87         prof.id = std::stoi( id );
88         prof.eku = map->at( "eku" );
89         prof.ku = map->at( "ku" );
90
91         std::string cas = map->at( "ca" );
92
93         for( size_t pos = 0; pos != std::string::npos; ) {
94             size_t end = cas.find( ",", pos );
95             std::string sub;
96
97             if( end == std::string::npos ) {
98                 sub = cas.substr( pos );
99             } else {
100                 sub = cas.substr( pos, end - pos );
101                 end++;
102             }
103
104             pos = end;
105
106             if( CAs.find( sub ) == CAs.end() ) {
107                 std::shared_ptr<CAConfig> ca( new CAConfig( sub ) );
108                 CAs.emplace( sub, ca );
109             }
110
111             prof.ca.push_back( CAs.at( sub ) );
112
113         }
114
115         profiles.emplace( profileName, prof );
116         std::cout << "Profile: " << profileName << " up and running." << std::endl;
117     }
118
119     ( void ) closedir( dp );
120
121
122     std::cout << profiles.size() << " profiles loaded." << std::endl;
123
124     return 0;
125 }
126
127 int parseConfig( std::string path ) {
128
129     auto masterConf = parseConf( path );
130
131     keyDir = masterConf->at( "key.directory" );
132     sqlHost = masterConf->at( "sql.host" );
133     sqlUser = masterConf->at( "sql.user" );
134     sqlPass = masterConf->at( "sql.password" );
135     sqlDB = masterConf->at( "sql.database" );
136     serialPath = masterConf->at( "serialPath" );
137
138     if( keyDir == "" ) {
139         std::cerr << "Missing config property key.directory" << std::endl;
140         return -1;
141     }
142
143     if( parseProfiles() != 0 ) {
144         return -1;
145     }
146
147     return 0;
148 }