Debian Codegraph @

src/lib/common/SimpleConfigLoader.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE, The Internet Infrastructure Foundation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 SimpleConfigLoader.cpp
29
30 Loads the configuration from the configuration file.
31 *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <algorithm>
37 #include <limits.h>
38 #ifdef _WIN32
39 # include <io.h>
40 #else
41 # include <unistd.h>
42 #endif
43 #include "config.h"
44 #if defined(HAVE_GETPWUID_R)
45 # include <sys/types.h>
46 # include <pwd.h>
47 #endif
48 #include "SimpleConfigLoader.h"
49 #include "log.h"
50 #include "Configuration.h"
51
52 // Initialise the one-and-only instance
53 #ifdef HAVE_CXX11
54 std::unique_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(nullptr);
55 #else
56 std::auto_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(NULL);
57 #endif
58
59 // Return the one-and-only instance
60 SimpleConfigLoader* SimpleConfigLoader::i()
61 {
62 if (instance.get() == NULL)
63 {
64 instance.reset(new SimpleConfigLoader());
65 }
66
67 return instance.get();
68 }
69
70 // Constructor
71 SimpleConfigLoader::SimpleConfigLoader()
72 {
73 }
74
75 // Load the configuration
76 bool SimpleConfigLoader::loadConfiguration()
77 {
78 char* configPath = getConfigPath();
79
80 FILE* fp = fopen(configPath,"r");
81
82 if (fp == NULL)
83 {
84 ERROR_MSG("Could not open the config file: %s", configPath);
85 free(configPath);
86 return false;
87 }
88 free(configPath);
89
90 char fileBuf[1024];
91
92 // Format in config file
93 //
94 // <name> = <value>
95 // # Line is ignored
96
97 size_t line = 0;
98 while (fgets(fileBuf, sizeof(fileBuf), fp) != NULL)
99 {
100 line++;
101
102 // End the string at the first comment or newline
103 fileBuf[strcspn(fileBuf, "#\n\r")] = '\0';
104
105 // Skip empty lines
106 if (fileBuf[0] == '\0')
107 {
108 continue;
109 }
110
111 // Get the first part of the line
112 char* name = strtok(fileBuf, "=");
113 if (name == NULL)
114 {
115 WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line);
116 continue;
117 }
118
119 // Trim the name
120 char* trimmedName = trimString(name);
121 if (trimmedName == NULL)
122 {
123 WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line);
124 continue;
125 }
126
127 // Get the second part of the line
128 char* value = strtok(NULL, "=");
129 if(value == NULL) {
130 WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line);
131 free(trimmedName);
132 continue;
133 }
134
135 // Trim the value
136 char* trimmedValue = trimString(value);
137 if (trimmedValue == NULL)
138 {
139 WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line);
140 free(trimmedName);
141 continue;
142 }
143
144 // Save name,value
145 std::string stringName(trimmedName);
146 std::string stringValue(trimmedValue);
147 free(trimmedName);
148 free(trimmedValue);
149
150 switch (Configuration::i()->getType(stringName))
151 {
152 case CONFIG_TYPE_STRING:
153 Configuration::i()->setString(stringName, stringValue);
154 break;
155 case CONFIG_TYPE_INT:
156 Configuration::i()->setInt(stringName, atoi(stringValue.c_str()));
157 break;
158 case CONFIG_TYPE_BOOL:
159 bool boolValue;
160 if (string2bool(stringValue, &boolValue))
161 {
162 Configuration::i()->setBool(stringName, boolValue);
163 }
164 else
165 {
166 WARNING_MSG("The value %s is not a boolean", stringValue.c_str());
167 }
168 break;
169 case CONFIG_TYPE_UNSUPPORTED:
170 default:
171 WARNING_MSG("The following configuration is not supported: %s = %s",
172 stringName.c_str(), stringValue.c_str());
173 break;
174 }
175 }
176
177 fclose(fp);
178
179 return true;
180 }
181
182 // Get the boolean value from a string
183 bool SimpleConfigLoader::string2bool(std::string stringValue, bool* boolValue)
184 {
185 // Convert to lowercase
186 std::transform(stringValue.begin(), stringValue.end(), stringValue.begin(), tolower);
187
188 if (stringValue.compare("true") == 0)
189 {
190 *boolValue = true;
191 return true;
192 }
193
194 if (stringValue.compare("false") == 0)
195 {
196 *boolValue = false;
197 return true;
198 }
199
200 return false;
201 }
202
203 #define CONFIG_FILE ".config/softhsm2/softhsm2.conf"
204
205 /* Returns a user-specific path for configuration.
206 */
207 static char *get_user_path(void)
208 {
209 #ifdef _WIN32
210 char path[512];
211 const char *home_drive = getenv("HOMEDRIVE");
212 const char *home_path = getenv("HOMEPATH");
213
214 if (home_drive && home_path) {
215 snprintf(path, sizeof(path), "%s%s\\softhsm2.conf", home_drive, home_path);
216
217 if (_access(path, 0) == 0)
218 return strdup(path);
219 }
220 goto fail;
221 #else
222 char path[_POSIX_PATH_MAX];
223 const char *home_dir = getenv("HOME");
224
225 if (home_dir != NULL && home_dir[0] != 0) {
226 snprintf(path, sizeof(path), "%s/" CONFIG_FILE, home_dir);
227 if (access(path, R_OK) == 0)
228 return strdup(path);
229 else
230 goto fail;
231 }
232
233 # if defined(HAVE_GETPWUID_R)
234 if (home_dir == NULL || home_dir[0] == '\0') {
235 struct passwd *pwd;
236 struct passwd _pwd;
237 int ret;
238 char tmp[512];
239
240 ret = getpwuid_r(getuid(), &_pwd, tmp, sizeof(tmp), &pwd);
241 if (ret == 0 && pwd != NULL) {
242 snprintf(path, sizeof(path), "%s/" CONFIG_FILE, pwd->pw_dir);
243 if (access(path, R_OK) == 0)
244 return strdup(path);
245 else
246 goto fail;
247 }
248 }
249 # endif
250 #endif
251
252 fail:
253 return NULL;
254 }
255
256 static char *get_env_var_path(void)
257 {
258 #ifdef _WIN32
259
260 LPSTR value = NULL;
261 DWORD size = 0;
262
263 size = GetEnvironmentVariableA("SOFTHSM2_CONF", value, size);
264 if (size == 0) {
265 return NULL;
266 }
267
268 value = (LPSTR) malloc(size);
269 if (NULL == value) {
270 return NULL;
271 }
272
273 if (GetEnvironmentVariableA("SOFTHSM2_CONF", value, size) != (size - 1)) {
274 free(value);
275 return NULL;
276 }
277
278 return value;
279
280 #else
281
282 char *value = getenv("SOFTHSM2_CONF");
283
284 if (value == NULL) {
285 return value;
286 } else {
287 return strdup(value);
288 }
289
290 #endif
291 }
292
293 char* SimpleConfigLoader::getConfigPath()
294 {
295 char* configPath = get_env_var_path();
296 char* tpath;
297
298 if (configPath != NULL)
299 {
300 return configPath;
301 }
302 else
303 {
304 tpath = get_user_path();
305 if (tpath != NULL)
306 {
307 return tpath;
308 }
309 }
310
311 return strdup(DEFAULT_SOFTHSM2_CONF);
312 }
313
314 char* SimpleConfigLoader::trimString(char* text)
315 {
316 if (text == NULL)
317 {
318 return NULL;
319 }
320
321 int startPos = 0;
322 int endPos = strlen(text) - 1;
323
324 // Find the first position without a space
325 while (startPos <= endPos && isspace((int)*(text + startPos)))
326 {
327 startPos++;
328 }
329 // Find the last position without a space
330 while (startPos <= endPos && isspace((int)*(text + endPos)))
331 {
332 endPos--;
333 }
334
335 // We must have a valid string
336 int length = endPos - startPos + 1;
337 if (length <= 0)
338 {
339 return NULL;
340 }
341
342 // Create the trimmed text
343 char* trimmedText = (char*)malloc(length + 1);
344 if (trimmedText == NULL)
345 {
346 return NULL;
347 }
348 trimmedText[length] = '\0';
349 memcpy(trimmedText, text + startPos, length);
350
351 return trimmedText;
352 }