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;
16 std::string crlPrefix;
17 std::string crtPrefix;
19 std::shared_ptr<std::unordered_map<std::string, std::string>> parseConf( std::string path ) {
20 auto map = std::make_shared<std::unordered_map<std::string, std::string>>();
24 if( !config.is_open() ) {
25 logger::notef( "Where is \"%s\"?", path );
26 throw std::runtime_error("Config missing");
31 while( std::getline( config, line1 ) ) {
32 if( line1[0] == '#' || line1.size() == 0 ) {
36 int splitter = line1.find( "=" );
38 if( splitter == -1 ) {
39 logger::warn( "Ignoring malformed config line: ", line1 );
43 std::string key = line1.substr( 0, splitter );
44 std::string value = line1.substr( splitter + 1 );
45 map->emplace( key, value );
54 CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
58 dp = opendir( "profiles" );
61 logger::error( "Profiles directory not found" );
65 while( ( ep = readdir( dp ) ) ) {
66 if( ep->d_name[0] == '.' ) {
70 std::string profileName( ep->d_name );
72 int splitter = profileName.find( "-" );
74 if( splitter == -1 ) {
75 logger::warn( "Ignoring malformed profile: ", profileName );
79 std::string id = profileName.substr( 0, splitter );
81 if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
82 logger::warn( "Ignoring malformed profile: ", profileName );
86 auto map = parseConf( std::string( "profiles/" ) + profileName );
88 profileName = profileName.substr( 0, profileName.size() - 4 );
91 prof.id = std::stoi( id );
92 prof.eku = map->at( "eku" );
93 prof.ku = map->at( "ku" );
94 prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
96 std::string cas = map->at( "ca" );
101 if( ( dir = opendir( "ca" ) ) != NULL ) {
102 while( ( ent = readdir( dir ) ) != NULL ) {
103 std::string caName = std::string( ent->d_name );
105 if( caName.find( cas ) != 0 ) {
109 if( CAs.find( caName ) == CAs.end() ) {
110 auto ca = std::make_shared<CAConfig>( caName );
111 CAs.emplace( caName, ca );
114 prof.ca.push_back( CAs.at( caName ) );
115 logger::note( "Adding CA: ", caName );
120 throw std::runtime_error("Directory with CAConfigs not found");
123 profiles.emplace( profileName, prof );
124 logger::notef( "Profile: \"%s\" up and running.", profileName );
127 ( void ) closedir( dp );
129 logger::notef( "%s profiles loaded.", profiles.size() );
134 int parseConfig( std::string path ) {
135 auto masterConf = parseConf( path );
137 keyDir = masterConf->at( "key.directory" );
138 sqlHost = masterConf->at( "sql.host" );
139 sqlUser = masterConf->at( "sql.user" );
140 sqlPass = masterConf->at( "sql.password" );
141 sqlDB = masterConf->at( "sql.database" );
142 serialPath = masterConf->at( "serialPath" );
143 crlPrefix = masterConf->at( "crlPrefix" );
144 crtPrefix = masterConf->at( "crtPrefix" );
147 logger::error( "Missing config property key.directory" );
151 if( parseProfiles() != 0 ) {