5 #include <unordered_map>
7 #include "crypto/sslUtil.h"
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;
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>() );
20 if( !config.is_open() ) {
21 std::cout << "Where is " << path << "?" << std::endl;
22 throw "Config missing";
27 while( std::getline( config, line1 ) ) {
28 if( line1[0] == '#' || line1.size() == 0 ) {
32 int splitter = line1.find( "=" );
34 if( splitter == -1 ) {
35 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
39 std::string key = line1.substr( 0, splitter );
40 std::string value = line1.substr( splitter + 1 );
41 map->emplace( key, value );
50 CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
54 dp = opendir( "profiles" );
57 std::cerr << "Profiles not found " << std::endl;
61 while( ( ep = readdir( dp ) ) ) {
62 if( ep->d_name[0] == '.' ) {
66 std::string profileName( ep->d_name );
68 int splitter = profileName.find( "-" );
70 if( splitter == -1 ) {
71 std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
75 std::string id = profileName.substr( 0, splitter );
77 if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
78 std::cerr << "Ignoring malformed profile: " << profileName << std::endl;
82 auto map = parseConf( std::string( "profiles/" ) + profileName );
84 profileName = profileName.substr( 0, profileName.size() - 4 );
87 prof.id = std::stoi( id );
88 prof.eku = map->at( "eku" );
89 prof.ku = map->at( "ku" );
90 prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
92 std::string cas = map->at( "ca" );
94 for( size_t pos = 0; pos != std::string::npos; ) {
95 size_t end = cas.find( ",", pos );
98 if( end == std::string::npos ) {
99 sub = cas.substr( pos );
101 sub = cas.substr( pos, end - pos );
107 if( CAs.find( sub ) == CAs.end() ) {
108 std::shared_ptr<CAConfig> ca( new CAConfig( sub ) );
109 CAs.emplace( sub, ca );
112 prof.ca.push_back( CAs.at( sub ) );
116 profiles.emplace( profileName, prof );
117 std::cout << "Profile: " << profileName << " up and running." << std::endl;
120 ( void ) closedir( dp );
123 std::cout << profiles.size() << " profiles loaded." << std::endl;
128 int parseConfig( std::string path ) {
130 auto masterConf = parseConf( path );
132 keyDir = masterConf->at( "key.directory" );
133 sqlHost = masterConf->at( "sql.host" );
134 sqlUser = masterConf->at( "sql.user" );
135 sqlPass = masterConf->at( "sql.password" );
136 sqlDB = masterConf->at( "sql.database" );
137 serialPath = masterConf->at( "serialPath" );
140 std::cerr << "Missing config property key.directory" << std::endl;
144 if( parseProfiles() != 0 ) {