X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fconfig.cpp;h=ce78f417582a8a8049389f18435279cde60b7fa8;hb=c51272489a64903f976c6d502fd79925cb537d9b;hp=a9407f8c163d1689431e09fb19aea7750dca14f8;hpb=c3f5775ce88f4df732e5e803dab70ce395c5f504;p=cassiopeia.git diff --git a/src/config.cpp b/src/config.cpp index a9407f8..ce78f41 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,20 +1,25 @@ #include #include #include +#include +#include -#include "sslUtil.h" +#include "crypto/sslUtil.h" std::string keyDir; -std::vector profiles; +std::unordered_map profiles; +std::unordered_map> CAs; std::string sqlHost, sqlUser, sqlPass, sqlDB; +std::string serialPath; -int parseConfig( std::string path ) { +std::shared_ptr> parseConf( std::string path ) { + std::shared_ptr> map( new std::unordered_map() ); std::ifstream config; config.open( path ); if( !config.is_open() ) { - std::cerr << "config missing" << std::endl; - return 1; + std::cout << "Where is " << path << "?" << std::endl; + throw "Config missing"; } std::string line1; @@ -33,65 +38,112 @@ int parseConfig( std::string path ) { std::string key = line1.substr( 0, splitter ); std::string value = line1.substr( splitter + 1 ); + map->emplace( key, value ); + } + + config.close(); + + return map; +} + +int parseProfiles() { + CAs = std::unordered_map>(); + + DIR* dp; + struct dirent* ep; + dp = opendir( "profiles" ); + + if( dp == NULL ) { + std::cerr << "Profiles not found " << std::endl; + return -1; + } - if( key == "key.directory" ) { - keyDir = value; + while( ( ep = readdir( dp ) ) ) { + if( ep->d_name[0] == '.' ) { continue; - } else if( key == "sql.host" ) { - sqlHost = value; - } else if( key == "sql.user" ) { - sqlUser = value; - } else if( key == "sql.password" ) { - sqlPass = value; - } else if( key == "sql.database" ) { - sqlDB = value; } - if( key.compare( 0, 8, "profile." ) == 0 ) { - int numE = key.find( ".", 9 ); + std::string profileName( ep->d_name ); - if( numE == 0 ) { - std::cout << "invalid line: " << line1 << std::endl; - continue; - } + int splitter = profileName.find( "-" ); + + if( splitter == -1 ) { + std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + continue; + } - unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() ); - std::string rest = key.substr( numE + 1 ); + std::string id = profileName.substr( 0, splitter ); - if( i + 1 > profiles.size() ) { - profiles.resize( i + 1 ); - } + if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) { + std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + continue; + } + + auto map = parseConf( std::string( "profiles/" ) + profileName ); + + profileName = profileName.substr( 0, profileName.size() - 4 ); + + Profile prof; + 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( rest == "key" ) { - profiles[i].key = value; - } else if( rest == "cert" ) { - profiles[i].cert = value; - } else if( rest == "ku" ) { - profiles[i].ku = value; - } else if( rest == "eku" ) { - profiles[i].eku = value; + std::string cas = map->at( "ca" ); + + for( size_t pos = 0; pos != std::string::npos; ) { + size_t end = cas.find( ",", pos ); + std::string sub; + + if( end == std::string::npos ) { + sub = cas.substr( pos ); } else { - std::cout << "invalid line: " << line1 << std::endl; - continue; + sub = cas.substr( pos, end - pos ); + end++; } - } - } - for( auto& prof : profiles ) { - if( prof.cert != "" && prof.key != "" ) { - std::cout << "Loading profile... " << std::endl; - prof.ca = loadX509FromFile( prof.cert ); - prof.caKey = loadPkeyFromFile( prof.key ); + pos = end; + + if( CAs.find( sub ) == CAs.end() ) { + std::shared_ptr ca( new CAConfig( sub ) ); + CAs.emplace( sub, ca ); + } + + prof.ca.push_back( CAs.at( sub ) ); + } + + profiles.emplace( profileName, prof ); + std::cout << "Profile: " << profileName << " up and running." << std::endl; } + ( void ) closedir( dp ); + + 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" ); + if( keyDir == "" ) { std::cerr << "Missing config property key.directory" << std::endl; return -1; } - config.close(); + if( parseProfiles() != 0 ) { + return -1; + } + return 0; }