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;
20 std::shared_ptr<std::unordered_map<std::string, std::string>> parseConf( std::string path ) {
21 auto map = std::make_shared<std::unordered_map<std::string, std::string>>();
25 if( !config.is_open() ) {
26 logger::notef( "Where is \"%s\"?", path );
27 throw std::runtime_error( "Config missing" );
32 while( std::getline( config, line1 ) ) {
33 if( line1[0] == '#' || line1.size() == 0 ) {
37 int splitter = line1.find( "=" );
39 if( splitter == -1 ) {
40 logger::warn( "Ignoring malformed config line: ", line1 );
44 std::string key = line1.substr( 0, splitter );
45 std::string value = line1.substr( splitter + 1 );
46 map->emplace( key, value );
55 CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
59 dp = opendir( "profiles" );
62 logger::error( "Profiles directory not found" );
66 while( ( ep = readdir( dp ) ) ) {
67 if( ep->d_name[0] == '.' ) {
71 std::string profileName( ep->d_name );
73 int splitter = profileName.find( "-" );
75 if( splitter == -1 ) {
76 logger::warn( "Ignoring malformed profile: ", profileName );
80 std::string id = profileName.substr( 0, splitter );
82 if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
83 logger::warn( "Ignoring malformed profile: ", profileName );
87 auto map = parseConf( std::string( "profiles/" ) + profileName );
89 profileName = profileName.substr( 0, profileName.size() - 4 );
92 prof.id = std::stoi( id );
93 prof.eku = map->at( "eku" );
94 prof.ku = map->at( "ku" );
95 prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
101 if( profileName == "0100-ocsp" ) {
102 //This profile does not have a specific CA. The concrete CA has to be set in each request.
103 } else if( ( dir = opendir( "ca" ) ) != NULL ) {
104 std::string cas = map->at( "ca" );
105 std::string toFind = cas + "_";
107 while( ( ent = readdir( dir ) ) != NULL ) {
108 std::string caName = std::string( ent->d_name );
110 if( caName.find( toFind ) != 0 ) {
114 if( CAs.find( caName ) == CAs.end() ) {
115 auto ca = std::make_shared<CAConfig>( caName );
116 CAs.emplace( caName, ca );
119 prof.ca.push_back( CAs.at( caName ) );
120 logger::note( "Adding CA: ", caName );
125 throw std::runtime_error( "Directory with CAConfigs not found" );
128 profiles.emplace( profileName, prof );
129 logger::notef( "Profile: \"%s\" up and running.", profileName );
132 ( void ) closedir( dp );
134 logger::notef( "%s profiles loaded.", profiles.size() );
139 int parseConfig( std::string path ) {
140 auto masterConf = parseConf( path );
142 keyDir = masterConf->at( "key.directory" );
143 sqlHost = masterConf->at( "sql.host" );
144 sqlUser = masterConf->at( "sql.user" );
145 sqlPass = masterConf->at( "sql.password" );
146 sqlDB = masterConf->at( "sql.database" );
147 serialPath = masterConf->at( "serialPath" );
148 crlPrefix = masterConf->at( "crlPrefix" );
149 crtPrefix = masterConf->at( "crtPrefix" );
151 auto ocspPathEntry = masterConf->find( "ocsp.path" );
153 if( ocspPathEntry == masterConf->end() ) {
156 ocspPath = ocspPathEntry->second;
160 logger::error( "Missing config property key.directory" );
164 if( parseProfiles() != 0 ) {