Preload plugins configured in tests
[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 trusted/untrusted certificates
91 */
92 static bool load_trusted_cert(settings_t *settings, bool trusted)
93 {
94 enumerator_t *enumerator;
95 char *key, *value;
96
97 enumerator = settings->create_key_value_enumerator(settings,
98 trusted ? "certs.trusted" : "certs.untrusted");
99 while (enumerator->enumerate(enumerator, &key, &value))
100 {
101 certificate_t *cert = NULL;
102
103 if (strcaseeq(key, "x509"))
104 {
105 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
106 CERT_X509, BUILD_FROM_FILE, value, BUILD_END);
107 }
108 else if (strcaseeq(key, "crl"))
109 {
110 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
111 CERT_X509_CRL, BUILD_FROM_FILE, value, BUILD_END);
112 }
113 else
114 {
115 fprintf(stderr, "certificate type '%s' not supported\n", key);
116 enumerator->destroy(enumerator);
117 return FALSE;
118 }
119 if (!cert)
120 {
121 fprintf(stderr, "loading %strusted certificate '%s' from '%s' "
122 "failed\n", trusted ? "" : "un", key, value);
123 enumerator->destroy(enumerator);
124 return FALSE;
125 }
126 conftest->creds->add_cert(conftest->creds, trusted, cert);
127 }
128 enumerator->destroy(enumerator);
129 return TRUE;
130 }
131
132 /**
133 * Load certificates from the confiuguration file
134 */
135 static bool load_certs(settings_t *settings, char *dir)
136 {
137 char wd[PATH_MAX];
138
139 if (getcwd(wd, sizeof(wd)) == NULL)
140 {
141 fprintf(stderr, "getting cwd failed: %s\n", strerror(errno));
142 return FALSE;
143 }
144 if (chdir(dir) != 0)
145 {
146 fprintf(stderr, "opening directory '%s' failed: %s\n",
147 dir, strerror(errno));
148 return FALSE;
149 }
150
151 if (!load_trusted_cert(settings, TRUE) ||
152 !load_trusted_cert(settings, FALSE))
153 {
154 return FALSE;
155 }
156
157 if (chdir(wd) != 0)
158 {
159 fprintf(stderr, "opening directory '%s' failed: %s\n",
160 wd, strerror(errno));
161 return FALSE;
162 }
163 return TRUE;
164 }
165
166 /**
167 * Load private keys from the confiuguration file
168 */
169 static bool load_keys(settings_t *settings, char *dir)
170 {
171 enumerator_t *enumerator;
172 char *type, *value, wd[PATH_MAX];
173 private_key_t *key;
174 key_type_t key_type;
175
176 if (getcwd(wd, sizeof(wd)) == NULL)
177 {
178 fprintf(stderr, "getting cwd failed: %s\n", strerror(errno));
179 return FALSE;
180 }
181 if (chdir(dir) != 0)
182 {
183 fprintf(stderr, "opening directory '%s' failed: %s\n",
184 dir, strerror(errno));
185 return FALSE;
186 }
187
188 enumerator = settings->create_key_value_enumerator(settings, "keys");
189 while (enumerator->enumerate(enumerator, &type, &value))
190 {
191 if (strcaseeq(type, "ecdsa"))
192 {
193 key_type = KEY_ECDSA;
194 }
195 else if (strcaseeq(type, "rsa"))
196 {
197 key_type = KEY_RSA;
198 }
199 else
200 {
201 fprintf(stderr, "unkown key type: '%s'\n", type);
202 enumerator->destroy(enumerator);
203 return FALSE;
204 }
205 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
206 BUILD_FROM_FILE, value, BUILD_END);
207 if (!key)
208 {
209 fprintf(stderr, "loading %s key from '%s' failed\n", type, value);
210 enumerator->destroy(enumerator);
211 return FALSE;
212 }
213 conftest->creds->add_key(conftest->creds, key);
214 }
215 enumerator->destroy(enumerator);
216
217 if (chdir(wd) != 0)
218 {
219 fprintf(stderr, "opening directory '%s' failed: %s\n",
220 wd, strerror(errno));
221 return FALSE;
222 }
223 return TRUE;
224 }
225
226 /**
227 * Load configured hooks
228 */
229 static bool load_hooks()
230 {
231 enumerator_t *enumerator;
232 char *name, *pos, buf[64];
233 hook_t *(*create)(char*);
234 hook_t *hook;
235
236 enumerator = conftest->test->create_section_enumerator(conftest->test,
237 "hooks");
238 while (enumerator->enumerate(enumerator, &name))
239 {
240 pos = strchr(name, '-');
241 if (pos)
242 {
243 snprintf(buf, sizeof(buf), "%.*s_hook_create", pos - name, name);
244 }
245 else
246 {
247 snprintf(buf, sizeof(buf), "%s_hook_create", name);
248 }
249 create = dlsym(RTLD_DEFAULT, buf);
250 if (create)
251 {
252 hook = create(name);
253 if (hook)
254 {
255 conftest->hooks->insert_last(conftest->hooks, hook);
256 charon->bus->add_listener(charon->bus, &hook->listener);
257 }
258 }
259 else
260 {
261 fprintf(stderr, "dlsym() for hook '%s' failed: %s\n", name, dlerror());
262 enumerator->destroy(enumerator);
263 return FALSE;
264 }
265 }
266 enumerator->destroy(enumerator);
267 return TRUE;
268 }
269
270 /**
271 * atexit() cleanup handler
272 */
273 static void cleanup()
274 {
275 hook_t *hook;
276
277 DESTROY_IF(conftest->suite);
278 DESTROY_IF(conftest->test);
279 lib->credmgr->remove_set(lib->credmgr, &conftest->creds->set);
280 conftest->creds->destroy(conftest->creds);
281 DESTROY_IF(conftest->actions);
282 while (conftest->hooks->remove_last(conftest->hooks,
283 (void**)&hook) == SUCCESS)
284 {
285 charon->bus->remove_listener(charon->bus, &hook->listener);
286 hook->destroy(hook);
287 }
288 conftest->hooks->destroy(conftest->hooks);
289 if (conftest->config)
290 {
291 if (charon->backends)
292 {
293 charon->backends->remove_backend(charon->backends,
294 &conftest->config->backend);
295 }
296 conftest->config->destroy(conftest->config);
297 }
298 free(conftest->suite_dir);
299 free(conftest->test_dir);
300 free(conftest);
301 libcharon_deinit();
302 libhydra_deinit();
303 library_deinit();
304 }
305
306 /**
307 * Main function, starts the conftest daemon.
308 */
309 int main(int argc, char *argv[])
310 {
311 struct sigaction action;
312 int status = 0;
313 sigset_t set;
314 int sig;
315 char *suite_file = "suite.conf", *test_file = NULL;
316 file_logger_t *logger;
317
318 if (!library_init(NULL))
319 {
320 library_deinit();
321 return SS_RC_LIBSTRONGSWAN_INTEGRITY;
322 }
323 if (!libhydra_init("conftest"))
324 {
325 libhydra_deinit();
326 library_deinit();
327 return SS_RC_INITIALIZATION_FAILED;
328 }
329 if (!libcharon_init())
330 {
331 libcharon_deinit();
332 libhydra_deinit();
333 library_deinit();
334 return SS_RC_INITIALIZATION_FAILED;
335 }
336
337 INIT(conftest,
338 .creds = mem_cred_create(),
339 );
340 logger = file_logger_create(stdout, NULL, FALSE);
341 logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
342 charon->bus->add_listener(charon->bus, &logger->listener);
343 charon->file_loggers->insert_last(charon->file_loggers, logger);
344
345 lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
346 conftest->hooks = linked_list_create();
347 conftest->config = config_create();
348
349 atexit(cleanup);
350
351 while (TRUE)
352 {
353 struct option long_opts[] = {
354 { "help", no_argument, NULL, 'h' },
355 { "version", no_argument, NULL, 'v' },
356 { "suite", required_argument, NULL, 's' },
357 { "test", required_argument, NULL, 't' },
358 { 0,0,0,0 }
359 };
360 switch (getopt_long(argc, argv, "", long_opts, NULL))
361 {
362 case EOF:
363 break;
364 case 'h':
365 usage(stdout);
366 return 0;
367 case 'v':
368 printf("strongSwan %s conftest\n", VERSION);
369 return 0;
370 case 's':
371 suite_file = optarg;
372 continue;
373 case 't':
374 test_file = optarg;
375 continue;
376 default:
377 usage(stderr);
378 return 1;
379 }
380 break;
381 }
382
383 if (!load_configs(suite_file, test_file))
384 {
385 return 1;
386 }
387 if (!lib->plugins->load(lib->plugins, NULL,
388 conftest->test->get_str(conftest->test, "preload", "")))
389 {
390 return 1;
391 }
392 if (!charon->initialize(charon))
393 {
394 return 1;
395 }
396 if (!load_certs(conftest->suite, conftest->suite_dir) ||
397 !load_certs(conftest->test, conftest->test_dir))
398 {
399 return 1;
400 }
401 if (!load_keys(conftest->suite, conftest->suite_dir) ||
402 !load_keys(conftest->test, conftest->test_dir))
403 {
404 return 1;
405 }
406 if (!load_hooks())
407 {
408 return 1;
409 }
410 charon->backends->add_backend(charon->backends, &conftest->config->backend);
411 conftest->config->load(conftest->config, conftest->test);
412 conftest->config->load(conftest->config, conftest->suite);
413 conftest->actions = actions_create();
414
415 /* set up thread specific handlers */
416 action.sa_handler = segv_handler;
417 action.sa_flags = 0;
418 sigemptyset(&action.sa_mask);
419 sigaddset(&action.sa_mask, SIGINT);
420 sigaddset(&action.sa_mask, SIGTERM);
421 sigaddset(&action.sa_mask, SIGHUP);
422 sigaction(SIGSEGV, &action, NULL);
423 sigaction(SIGILL, &action, NULL);
424 sigaction(SIGBUS, &action, NULL);
425 action.sa_handler = SIG_IGN;
426 sigaction(SIGPIPE, &action, NULL);
427 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
428
429 /* start thread pool */
430 charon->start(charon);
431
432 /* handle SIGINT/SIGTERM in main thread */
433 sigemptyset(&set);
434 sigaddset(&set, SIGINT);
435 sigaddset(&set, SIGHUP);
436 sigaddset(&set, SIGTERM);
437 sigprocmask(SIG_BLOCK, &set, NULL);
438
439 while (sigwait(&set, &sig) == 0)
440 {
441 switch (sig)
442 {
443 case SIGINT:
444 case SIGTERM:
445 fprintf(stderr, "\nshutting down...\n");
446 break;
447 default:
448 continue;
449 }
450 break;
451 }
452 return status;
453 }