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