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" );
96 std::string include = map->at( "include" );
99 std::unordered_set<std::string> include_set;
101 while( ( end = include.find( ",", pos ) ) != std::string::npos ) {
102 include_set.emplace( include.substr( pos, end - pos ) );
106 include_set.emplace( include.substr( pos ) );
107 prof.include = include_set;
109 prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
115 if( profileName == "0100-ocsp" ) {
116 //This profile does not have a specific CA. The concrete CA has to be set in each request.
117 } else if( ( dir = opendir( "ca" ) ) != NULL ) {
118 std::string cas = map->at( "ca" );
119 std::string toFind = cas + "_";
121 while( ( ent = readdir( dir ) ) != NULL ) {
122 std::string caName = std::string( ent->d_name );
124 if( caName.find( toFind ) != 0 ) {
128 if( CAs.find( caName ) == CAs.end() ) {
129 auto ca = std::make_shared<CAConfig>( caName );
130 CAs.emplace( caName, ca );
133 prof.ca.push_back( CAs.at( caName ) );
134 logger::note( "Adding CA: ", caName );
139 throw std::runtime_error( "Directory with CAConfigs not found" );
142 profiles.emplace( profileName, prof );
143 logger::notef( "Profile: \"%s\" up and running.", profileName );
146 ( void ) closedir( dp );
148 logger::notef( "%s profiles loaded.", profiles.size() );
153 int parseConfig( std::string path ) {
154 auto masterConf = parseConf( path );
156 keyDir = masterConf->at( "key.directory" );
157 sqlHost = masterConf->at( "sql.host" );
158 sqlUser = masterConf->at( "sql.user" );
159 sqlPass = masterConf->at( "sql.password" );
160 sqlDB = masterConf->at( "sql.database" );
161 serialPath = masterConf->at( "serialPath" );
162 crlPrefix = masterConf->at( "crlPrefix" );
163 crtPrefix = masterConf->at( "crtPrefix" );
165 auto ocspPathEntry = masterConf->find( "ocsp.path" );
167 if( ocspPathEntry == masterConf->end() ) {
170 ocspPath = ocspPathEntry->second;
174 logger::error( "Missing config property key.directory" );
178 if( parseProfiles() != 0 ) {