5 #include <unordered_map>
7 #include "crypto/sslUtil.h"
9 #include "log/logger.hpp"
12 std::unordered_map<std::string, Profile> profiles;
13 std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
14 std::string sqlHost, sqlUser, sqlPass, sqlDB;
15 std::string serialPath;
17 std::shared_ptr<std::unordered_map<std::string, std::string>> parseConf( std::string path ) {
18 std::shared_ptr<std::unordered_map<std::string, std::string>> map( new std::unordered_map<std::string, std::string>() );
22 if( !config.is_open() ) {
23 logger::notef( "Where is \"%s\"?", path );
24 throw "Config missing";
29 while( std::getline( config, line1 ) ) {
30 if( line1[0] == '#' || line1.size() == 0 ) {
34 int splitter = line1.find( "=" );
36 if( splitter == -1 ) {
37 logger::warn( "Ignoring malformed config line: ", line1 );
41 std::string key = line1.substr( 0, splitter );
42 std::string value = line1.substr( splitter + 1 );
43 map->emplace( key, value );
52 CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
56 dp = opendir( "profiles" );
59 logger::error( "Profiles directory not found" );
63 while( ( ep = readdir( dp ) ) ) {
64 if( ep->d_name[0] == '.' ) {
68 std::string profileName( ep->d_name );
70 int splitter = profileName.find( "-" );
72 if( splitter == -1 ) {
73 logger::warn( "Ignoring malformed profile: ", profileName );
77 std::string id = profileName.substr( 0, splitter );
79 if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
80 logger::warn( "Ignoring malformed profile: ", profileName );
84 auto map = parseConf( std::string( "profiles/" ) + profileName );
86 profileName = profileName.substr( 0, profileName.size() - 4 );
89 prof.id = std::stoi( id );
90 prof.eku = map->at( "eku" );
91 prof.ku = map->at( "ku" );
92 prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
94 std::string cas = map->at( "ca" );
99 if( ( dir = opendir( "ca" ) ) != NULL ) {
100 while( ( ent = readdir( dir ) ) != NULL ) {
101 std::string caName = std::string( ent->d_name );
103 if( caName.find( cas ) != 0 ) {
107 if( CAs.find( caName ) == CAs.end() ) {
108 std::shared_ptr<CAConfig> ca( new CAConfig( caName ) );
109 CAs.emplace( caName, ca );
112 prof.ca.push_back( CAs.at( caName ) );
113 logger::note( "Adding CA: ", caName );
118 throw "Directory with CAConfigs not found";
121 profiles.emplace( profileName, prof );
122 logger::notef( "Profile: \"%s\" up and running.", profileName );
125 ( void ) closedir( dp );
127 logger::notef( "%s profiles loaded.", profiles.size() );
132 int parseConfig( std::string path ) {
133 auto masterConf = parseConf( path );
135 keyDir = masterConf->at( "key.directory" );
136 sqlHost = masterConf->at( "sql.host" );
137 sqlUser = masterConf->at( "sql.user" );
138 sqlPass = masterConf->at( "sql.password" );
139 sqlDB = masterConf->at( "sql.database" );
140 serialPath = masterConf->at( "serialPath" );
143 logger::error( "Missing config property key.directory" );
147 if( parseProfiles() != 0 ) {