Fix segfault if config not found
[strongswan.git] / src / conftest / conftest.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <signal.h>
21 #include <getopt.h>
22 #include <dlfcn.h>
23 #include <libgen.h>
24
25 #include "conftest.h"
26 #include "config.h"
27 #include "hooks/hook.h"
28
29 #include <threading/thread.h>
30
31 /**
32 * Conftest globals struct
33 */
34 conftest_t *conftest;
35
36 /**
37 * Print usage information
38 */
39 static void usage(FILE *out)
40 {
41 fprintf(out, "Usage:\n");
42 fprintf(out, " --help show usage information\n");
43 fprintf(out, " --version show conftest version\n");
44 fprintf(out, " --suite <file> global testsuite configuration "
45 "(default: ./suite.conf)\n");
46 fprintf(out, " --test <file> test specific configuration\n");
47 }
48
49 /**
50 * Handle SIGSEGV/SIGILL signals raised by threads
51 */
52 static void segv_handler(int signal)
53 {
54 fprintf(stderr, "thread %u received %d\n", thread_current_id(), signal);
55 abort();
56 }
57
58 /**
59 * Load suite and test specific configurations
60 */
61 static bool load_configs(char *suite_file, char *test_file)
62 {
63 if (!test_file)
64 {
65 fprintf(stderr, "Missing test configuration file.\n");
66 return FALSE;
67 }
68 if (access(suite_file, R_OK) != 0)
69 {
70 fprintf(stderr, "Reading suite configuration file '%s' failed: %s.\n",
71 suite_file, strerror(errno));
72 return FALSE;
73 }
74 if (access(test_file, R_OK) != 0)
75 {
76 fprintf(stderr, "Reading test configuration file '%s' failed: %s.\n",
77 test_file, strerror(errno));
78 return FALSE;
79 }
80 conftest->suite = settings_create(suite_file);
81 conftest->test = settings_create(test_file);
82 suite_file = dirname(suite_file);
83 test_file = dirname(test_file);
84 conftest->suite_dir = strdup(suite_file);
85 conftest->test_dir = strdup(test_file);
86 return TRUE;
87 }
88
89 /**
90 * Load certificates from the confiuguration file
91 */
92 static bool load_certs(settings_t *settings, char *dir)
93 {
94 enumerator_t *enumerator;
95 char *key, *value, wd[PATH_MAX];
96 certificate_t *cert;
97
98 if (getcwd(wd, sizeof(wd)) == NULL)
99 {
100 fprintf(stderr, "getting cwd failed: %s\n", strerror(errno));
101 return FALSE;
102 }
103 if (chdir(dir) != 0)
104 {
105 fprintf(stderr, "opening directory '%s' failed: %s\n",
106 dir, strerror(errno));
107 return FALSE;
108 }
109
110 enumerator = settings->create_key_value_enumerator(settings, "certs.trusted");
111 while (enumerator->enumerate(enumerator, &key, &value))
112 {
113 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
114 BUILD_FROM_FILE, value, BUILD_END);
115 if (!cert)
116 {
117 fprintf(stderr, "loading trusted certificate "
118 "'%s' from '%s' failed\n", key, value);
119 enumerator->destroy(enumerator);
120 return FALSE;
121 }
122 conftest->creds->add_cert(conftest->creds, TRUE, cert);
123 }
124 enumerator->destroy(enumerator);
125
126 enumerator = settings->create_key_value_enumerator(settings, "certs.untrusted");
127 while (enumerator->enumerate(enumerator, &key, &value))
128 {
129 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
130 BUILD_FROM_FILE, value, BUILD_END);
131 if (!cert)
132 {
133 fprintf(stderr, "loading untrusted certificate "
134 "'%s' from '%s' failed\n", key, value);
135 enumerator->destroy(enumerator);
136 return FALSE;
137 }
138 conftest->creds->add_cert(conftest->creds, FALSE, cert);
139 }
140 enumerator->destroy(enumerator);
141
142 if (chdir(wd) != 0)
143 {
144 fprintf(stderr, "opening directory '%s' failed: %s\n",
145 wd, strerror(errno));
146 return FALSE;
147 }
148 return TRUE;
149 }
150
151 /**
152 * Load private keys from the confiuguration file
153 */
154 static bool load_keys(settings_t *settings, char *dir)
155 {
156 enumerator_t *enumerator;
157 char *type, *value, wd[PATH_MAX];
158 private_key_t *key;
159 key_type_t key_type;
160
161 if (getcwd(wd, sizeof(wd)) == NULL)
162 {
163 fprintf(stderr, "getting cwd failed: %s\n", strerror(errno));
164 return FALSE;
165 }
166 if (chdir(dir) != 0)
167 {
168 fprintf(stderr, "opening directory '%s' failed: %s\n",
169 dir, strerror(errno));
170 return FALSE;
171 }
172
173 enumerator = settings->create_key_value_enumerator(settings, "keys");
174 while (enumerator->enumerate(enumerator, &type, &value))
175 {
176 if (strcaseeq(type, "ecdsa"))
177 {
178 key_type = KEY_ECDSA;
179 }
180 else if (strcaseeq(type, "rsa"))
181 {
182 key_type = KEY_RSA;
183 }
184 else
185 {
186 fprintf(stderr, "unkown key type: '%s'\n", type);
187 enumerator->destroy(enumerator);
188 return FALSE;
189 }
190 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
191 BUILD_FROM_FILE, value, BUILD_END);
192 if (!key)
193 {
194 fprintf(stderr, "loading %s key from '%s' failed\n", type, value);
195 enumerator->destroy(enumerator);
196 return FALSE;
197 }
198 conftest->creds->add_key(conftest->creds, key);
199 }
200 enumerator->destroy(enumerator);
201
202 if (chdir(wd) != 0)
203 {
204 fprintf(stderr, "opening directory '%s' failed: %s\n",
205 wd, strerror(errno));
206 return FALSE;
207 }
208 return TRUE;
209 }
210
211 /**
212 * Load configured hooks
213 */
214 static bool load_hooks()
215 {
216 enumerator_t *enumerator;
217 char *name, buf[64];
218 hook_t *(*create)(void);
219 hook_t *hook;
220
221 enumerator = conftest->test->create_section_enumerator(conftest->test,
222 "hooks");
223 while (enumerator->enumerate(enumerator, &name))
224 {
225 snprintf(buf, sizeof(buf), "%s_hook_create", name);
226 create = dlsym(RTLD_DEFAULT, buf);
227 if (create)
228 {
229 hook = create();
230 if (hook)
231 {
232 conftest->hooks->insert_last(conftest->hooks, hook);
233 charon->bus->add_listener(charon->bus, &hook->listener);
234 }
235 }
236 else
237 {
238 fprintf(stderr, "dlsym() for hook '%s' failed: %s\n", name, dlerror());
239 enumerator->destroy(enumerator);
240 return FALSE;
241 }
242 }
243 enumerator->destroy(enumerator);
244 return TRUE;
245 }
246
247 /**
248 * atexit() cleanup handler
249 */
250 static void cleanup()
251 {
252 hook_t *hook;
253
254 DESTROY_IF(conftest->suite);
255 DESTROY_IF(conftest->test);
256 lib->credmgr->remove_set(lib->credmgr, &conftest->creds->set);
257 conftest->creds->destroy(conftest->creds);
258 DESTROY_IF(conftest->actions);
259 while (conftest->hooks->remove_last(conftest->hooks,
260 (void**)&hook) == SUCCESS)
261 {
262 charon->bus->remove_listener(charon->bus, &hook->listener);
263 hook->destroy(hook);
264 }
265 conftest->hooks->destroy(conftest->hooks);
266 if (conftest->config)
267 {
268 if (charon->backends)
269 {
270 charon->backends->remove_backend(charon->backends,
271 &conftest->config->backend);
272 }
273 conftest->config->destroy(conftest->config);
274 }
275 free(conftest->suite_dir);
276 free(conftest->test_dir);
277 free(conftest);
278 libcharon_deinit();
279 libhydra_deinit();
280 library_deinit();
281 }
282
283 /**
284 * Main function, starts the conftest daemon.
285 */
286 int main(int argc, char *argv[])
287 {
288 struct sigaction action;
289 int status = 0;
290 sigset_t set;
291 int sig;
292 char *suite_file = "suite.conf", *test_file = NULL;
293 file_logger_t *logger;
294
295 if (!library_init(NULL))
296 {
297 library_deinit();
298 return SS_RC_LIBSTRONGSWAN_INTEGRITY;
299 }
300 if (!libhydra_init("conftest"))
301 {
302 libhydra_deinit();
303 library_deinit();
304 return SS_RC_INITIALIZATION_FAILED;
305 }
306 if (!libcharon_init())
307 {
308 libcharon_deinit();
309 libhydra_deinit();
310 library_deinit();
311 return SS_RC_INITIALIZATION_FAILED;
312 }
313
314 INIT(conftest,
315 .creds = mem_cred_create(),
316 );
317 logger = file_logger_create(stdout, NULL, FALSE);
318 logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
319 charon->bus->add_listener(charon->bus, &logger->listener);
320 charon->file_loggers->insert_last(charon->file_loggers, logger);
321
322 lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
323 conftest->hooks = linked_list_create();
324 conftest->config = config_create();
325
326 atexit(cleanup);
327
328 while (TRUE)
329 {
330 struct option long_opts[] = {
331 { "help", no_argument, NULL, 'h' },
332 { "version", no_argument, NULL, 'v' },
333 { "suite", required_argument, NULL, 's' },
334 { "test", required_argument, NULL, 't' },
335 { 0,0,0,0 }
336 };
337 switch (getopt_long(argc, argv, "", long_opts, NULL))
338 {
339 case EOF:
340 break;
341 case 'h':
342 usage(stdout);
343 return 0;
344 case 'v':
345 printf("strongSwan %s conftest\n", VERSION);
346 return 0;
347 case 's':
348 suite_file = optarg;
349 continue;
350 case 't':
351 test_file = optarg;
352 continue;
353 default:
354 usage(stderr);
355 return 1;
356 }
357 break;
358 }
359
360 if (!load_configs(suite_file, test_file))
361 {
362 return 1;
363 }
364 if (!charon->initialize(charon))
365 {
366 return 1;
367 }
368 if (!load_certs(conftest->suite, suite_file) ||
369 !load_certs(conftest->test, test_file))
370 {
371 return 1;
372 }
373 if (!load_keys(conftest->suite, suite_file) ||
374 !load_keys(conftest->test, test_file))
375 {
376 return 1;
377 }
378 if (!load_hooks())
379 {
380 return 1;
381 }
382 charon->backends->add_backend(charon->backends, &conftest->config->backend);
383 conftest->config->load(conftest->config, conftest->suite);
384 conftest->config->load(conftest->config, conftest->test);
385 conftest->actions = actions_create();
386
387 /* set up thread specific handlers */
388 action.sa_handler = segv_handler;
389 action.sa_flags = 0;
390 sigemptyset(&action.sa_mask);
391 sigaddset(&action.sa_mask, SIGINT);
392 sigaddset(&action.sa_mask, SIGTERM);
393 sigaddset(&action.sa_mask, SIGHUP);
394 sigaction(SIGSEGV, &action, NULL);
395 sigaction(SIGILL, &action, NULL);
396 sigaction(SIGBUS, &action, NULL);
397 action.sa_handler = SIG_IGN;
398 sigaction(SIGPIPE, &action, NULL);
399 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
400
401 /* start thread pool */
402 charon->start(charon);
403
404 /* handle SIGINT/SIGTERM in main thread */
405 sigemptyset(&set);
406 sigaddset(&set, SIGINT);
407 sigaddset(&set, SIGHUP);
408 sigaddset(&set, SIGTERM);
409 sigprocmask(SIG_BLOCK, &set, NULL);
410
411 while (sigwait(&set, &sig) == 0)
412 {
413 switch (sig)
414 {
415 case SIGINT:
416 case SIGTERM:
417 fprintf(stderr, "\nshutting down...\n");
418 break;
419 default:
420 continue;
421 }
422 break;
423 }
424 return status;
425 }