]> WPIA git - cassiopeia.git/blob - src/config.cpp
add: configuration of OCSP path
[cassiopeia.git] / src / config.cpp
1 #include <iostream>
2 #include <vector>
3 #include <fstream>
4 #include <dirent.h>
5 #include <unordered_map>
6
7 #include "crypto/sslUtil.h"
8
9 #include "log/logger.hpp"
10
11 std::string keyDir;
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;
18 std::string ocspPath;
19
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>>();
22     std::ifstream config;
23     config.open( path );
24
25     if( !config.is_open() ) {
26         logger::notef( "Where is \"%s\"?", path );
27         throw std::runtime_error( "Config missing" );
28     }
29
30     std::string line1;
31
32     while( std::getline( config, line1 ) ) {
33         if( line1[0] == '#' || line1.size() == 0 ) {
34             continue;
35         }
36
37         int splitter = line1.find( "=" );
38
39         if( splitter == -1 ) {
40             logger::warn( "Ignoring malformed config line: ", line1 );
41             continue;
42         }
43
44         std::string key = line1.substr( 0, splitter );
45         std::string value = line1.substr( splitter + 1 );
46         map->emplace( key, value );
47     }
48
49     config.close();
50
51     return map;
52 }
53
54 int parseProfiles() {
55     CAs = std::unordered_map<std::string, std::shared_ptr<CAConfig>>();
56
57     DIR *dp;
58     struct dirent *ep;
59     dp = opendir( "profiles" );
60
61     if( dp == NULL ) {
62         logger::error( "Profiles directory not found" );
63         return -1;
64     }
65
66     while( ( ep = readdir( dp ) ) ) {
67         if( ep->d_name[0] == '.' ) {
68             continue;
69         }
70
71         std::string profileName( ep->d_name );
72
73         int splitter = profileName.find( "-" );
74
75         if( splitter == -1 ) {
76             logger::warn( "Ignoring malformed profile: ", profileName );
77             continue;
78         }
79
80         std::string id = profileName.substr( 0, splitter );
81
82         if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) {
83             logger::warn( "Ignoring malformed profile: ", profileName );
84             continue;
85         }
86
87         auto map = parseConf( std::string( "profiles/" ) + profileName );
88
89         profileName = profileName.substr( 0, profileName.size() - 4 );
90
91         Profile prof;
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;
96
97
98         DIR *dir;
99         struct dirent *ent;
100
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 + "_";
106
107             while( ( ent = readdir( dir ) ) != NULL ) {
108                 std::string caName = std::string( ent->d_name );
109
110                 if( caName.find( toFind ) != 0 ) {
111                     continue;
112                 }
113
114                 if( CAs.find( caName ) == CAs.end() ) {
115                     auto ca = std::make_shared<CAConfig>( caName );
116                     CAs.emplace( caName, ca );
117                 }
118
119                 prof.ca.push_back( CAs.at( caName ) );
120                 logger::note( "Adding CA: ", caName );
121             }
122
123             closedir( dir );
124         } else {
125             throw std::runtime_error( "Directory with CAConfigs not found" );
126         }
127
128         profiles.emplace( profileName, prof );
129         logger::notef( "Profile: \"%s\" up and running.", profileName );
130     }
131
132     ( void ) closedir( dp );
133
134     logger::notef( "%s profiles loaded.", profiles.size() );
135
136     return 0;
137 }
138
139 int parseConfig( std::string path ) {
140     auto masterConf = parseConf( path );
141
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" );
150
151     auto ocspPathEntry = masterConf->find( "ocsp.path" );
152
153     if( ocspPathEntry == masterConf->end() ) {
154         ocspPath = "";
155     } else {
156         ocspPath = ocspPathEntry->second;
157     }
158
159     if( keyDir == "" ) {
160         logger::error( "Missing config property key.directory" );
161         return -1;
162     }
163
164     if( parseProfiles() != 0 ) {
165         return -1;
166     }
167
168     return 0;
169 }