]> WPIA git - cassiopeia.git/blobdiff - src/config.cpp
add: handle the 'noOCSP' feature in the include section
[cassiopeia.git] / src / config.cpp
index a518db568863ba5a5d6f775269bc717e8104a2ff..8b175967de13ad74c672ab90fc0ffcae5b205324 100644 (file)
@@ -6,20 +6,25 @@
 
 #include "crypto/sslUtil.h"
 
+#include "log/logger.hpp"
+
 std::string keyDir;
 std::unordered_map<std::string, Profile> profiles;
 std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
 std::string sqlHost, sqlUser, sqlPass, sqlDB;
 std::string serialPath;
+std::string crlPrefix;
+std::string crtPrefix;
+std::string ocspPath;
 
 std::shared_ptr<std::unordered_map<std::string, std::string>> parseConf( std::string path ) {
-    std::shared_ptr<std::unordered_map<std::string, std::string>> map( new std::unordered_map<std::string, std::string>() );
+    auto map = std::make_shared<std::unordered_map<std::string, std::string>>();
     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 +37,7 @@ std::shared_ptr<std::unordered_map<std::string, std::string>> 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,25 +51,15 @@ std::shared_ptr<std::unordered_map<std::string, std::string>> 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<std::string, std::shared_ptr<CAConfig>>();
 
-    DIRdp;
-    struct direntep;
+    DIR *dp;
+    struct dirent *ep;
     dp = opendir( "profiles" );
 
     if( dp == NULL ) {
-        std::cerr << "Profiles not found " << std::endl;
+        logger::error( "Profiles directory not found" );
         return -1;
     }
 
@@ -78,14 +73,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 +92,90 @@ int parseConfig( std::string path ) {
         prof.id = std::stoi( id );
         prof.eku = map->at( "eku" );
         prof.ku = map->at( "ku" );
-
-        if( CAs.find( map->at( "ca" ) ) == CAs.end() ) {
-            std::shared_ptr<CAConfig> ca( new CAConfig( map->at( "ca" ) ) );
-            CAs.emplace( map->at( "ca" ), ca );
+        {
+            std::string include = map->at( "include" );
+            size_t pos = 0;
+            size_t end = 0;
+            std::unordered_set<std::string> include_set;
+
+            while( ( end = include.find( ",", pos ) ) != std::string::npos ) {
+                include_set.emplace( include.substr( pos, end - pos ) );
+                pos = end + 1;
+            }
+
+            include_set.emplace( include.substr( pos ) );
+            prof.include = include_set;
         }
+        prof.maxValidity = std::stoi( map->at( "days" ) ) * /* DAYS */24 * 60 * 60;
+
+
+        DIR *dir;
+        struct dirent *ent;
 
-        prof.ca = CAs.at( map->at( "ca" ) );
+        if( profileName == "0100-ocsp" ) {
+            //This profile does not have a specific CA. The concrete CA has to be set in each request.
+        } else if( ( dir = opendir( "ca" ) ) != NULL ) {
+            std::string cas = map->at( "ca" );
+            std::string toFind = cas + "_";
+
+            while( ( ent = readdir( dir ) ) != NULL ) {
+                std::string caName = std::string( ent->d_name );
+
+                if( caName.find( toFind ) != 0 ) {
+                    continue;
+                }
+
+                if( CAs.find( caName ) == CAs.end() ) {
+                    auto ca = std::make_shared<CAConfig>( caName );
+                    CAs.emplace( caName, 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" );
+
+    auto ocspPathEntry = masterConf->find( "ocsp.path" );
+
+    if( ocspPathEntry == masterConf->end() ) {
+        ocspPath = "";
+    } else {
+        ocspPath = ocspPathEntry->second;
+    }
 
     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;
     }