X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fconfig.cpp;h=a4d32848ecb7de4e3e4609adc5a83e997d927d47;hb=23987db96db7962a6ee58d1aeda2bd87780ca579;hp=2eb1f491c3bc42ca41e35eefbb183c42e644409d;hpb=890efd9eb1d32033fe3afd088838bde707f3a2bb;p=cassiopeia.git diff --git a/src/config.cpp b/src/config.cpp index 2eb1f49..a4d3284 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -4,22 +4,26 @@ #include #include -#include "sslUtil.h" +#include "crypto/sslUtil.h" + +#include "log/logger.hpp" std::string keyDir; std::unordered_map profiles; std::unordered_map> CAs; std::string sqlHost, sqlUser, sqlPass, sqlDB; std::string serialPath; +std::string crlPrefix; +std::string crtPrefix; std::shared_ptr> parseConf( std::string path ) { - std::shared_ptr> map( new std::unordered_map() ); + auto map = std::make_shared>(); std::ifstream config; config.open( path ); if( !config.is_open() ) { - std::cout << "Where is " << path << "?" << std::endl; - throw "Config missing"; + logger::notef( "Where is \"%s\"?", path ); + throw std::runtime_error( "Config missing" ); } std::string line1; @@ -32,7 +36,7 @@ std::shared_ptr> parseConf( std::st int splitter = line1.find( "=" ); if( splitter == -1 ) { - std::cerr << "Ignoring malformed config line: " << line1 << std::endl; + logger::warn( "Ignoring malformed config line: ", line1 ); continue; } @@ -46,17 +50,7 @@ std::shared_ptr> parseConf( std::st return map; } -int parseConfig( std::string path ) { - - auto masterConf = parseConf( path ); - - keyDir = masterConf->at( "key.directory" ); - sqlHost = masterConf->at( "sql.host" ); - sqlUser = masterConf->at( "sql.user" ); - sqlPass = masterConf->at( "sql.password" ); - sqlDB = masterConf->at( "sql.database" ); - serialPath = masterConf->at( "serialPath" ); - +int parseProfiles() { CAs = std::unordered_map>(); DIR* dp; @@ -64,7 +58,7 @@ int parseConfig( std::string path ) { dp = opendir( "profiles" ); if( dp == NULL ) { - std::cerr << "Profiles not found " << std::endl; + logger::error( "Profiles directory not found" ); return -1; } @@ -78,14 +72,14 @@ int parseConfig( std::string path ) { int splitter = profileName.find( "-" ); if( splitter == -1 ) { - std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + logger::warn( "Ignoring malformed profile: ", profileName ); continue; } std::string id = profileName.substr( 0, splitter ); if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) { - std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + logger::warn( "Ignoring malformed profile: ", profileName ); continue; } @@ -97,25 +91,64 @@ int parseConfig( std::string path ) { prof.id = std::stoi( id ); prof.eku = map->at( "eku" ); prof.ku = map->at( "ku" ); + prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60; - if( CAs.find( map->at( "ca" ) ) == CAs.end() ) { - std::shared_ptr ca( new CAConfig( map->at( "ca" ) ) ); - CAs.emplace( map->at( "ca" ), ca ); - } + std::string cas = map->at( "ca" ); + + DIR* dir; + struct dirent* ent; + + if( ( dir = opendir( "ca" ) ) != NULL ) { + while( ( ent = readdir( dir ) ) != NULL ) { + std::string caName = std::string( ent->d_name ); + + if( caName.find( cas ) != 0 ) { + continue; + } + + if( CAs.find( caName ) == CAs.end() ) { + auto ca = std::make_shared( caName ); + CAs.emplace( caName, ca ); + } - prof.ca = CAs.at( map->at( "ca" ) ); + prof.ca.push_back( CAs.at( caName ) ); + logger::note( "Adding CA: ", caName ); + } + + closedir( dir ); + } else { + throw std::runtime_error( "Directory with CAConfigs not found" ); + } profiles.emplace( profileName, prof ); - std::cout << "Profile: " << profileName << " up and running." << std::endl; + logger::notef( "Profile: \"%s\" up and running.", profileName ); } ( void ) closedir( dp ); + logger::notef( "%s profiles loaded.", profiles.size() ); - std::cout << profiles.size() << " profiles loaded." << std::endl; + return 0; +} + +int parseConfig( std::string path ) { + auto masterConf = parseConf( path ); + + keyDir = masterConf->at( "key.directory" ); + sqlHost = masterConf->at( "sql.host" ); + sqlUser = masterConf->at( "sql.user" ); + sqlPass = masterConf->at( "sql.password" ); + sqlDB = masterConf->at( "sql.database" ); + serialPath = masterConf->at( "serialPath" ); + crlPrefix = masterConf->at( "crlPrefix" ); + crtPrefix = masterConf->at( "crtPrefix" ); if( keyDir == "" ) { - std::cerr << "Missing config property key.directory" << std::endl; + logger::error( "Missing config property key.directory" ); + return -1; + } + + if( parseProfiles() != 0 ) { return -1; }