Load test and suite specific connection configurations
[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()
93 {
94 enumerator_t *enumerator;
95 char *key, *value;
96 certificate_t *cert;
97
98 if (chdir(conftest->suite_dir) != 0)
99 {
100 fprintf(stderr, "opening suite directory '%s' failed",
101 conftest->suite_dir);
102 return FALSE;
103 }
104
105 enumerator = conftest->suite->create_key_value_enumerator(
106 conftest->suite, "certs.trusted");
107 while (enumerator->enumerate(enumerator, &key, &value))
108 {
109 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
110 BUILD_FROM_FILE, value, BUILD_END);
111 if (!cert)
112 {
113 fprintf(stderr, "loading trusted certificate "
114 "'%s' from '%s' failed\n", key, value);
115 enumerator->destroy(enumerator);
116 return FALSE;
117 }
118 conftest->creds->add_cert(conftest->creds, TRUE, cert);
119 }
120 enumerator->destroy(enumerator);
121
122 enumerator = conftest->suite->create_key_value_enumerator(
123 conftest->suite, "certs.untrusted");
124 while (enumerator->enumerate(enumerator, &key, &value))
125 {
126 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
127 BUILD_FROM_FILE, value, BUILD_END);
128 if (!cert)
129 {
130 fprintf(stderr, "loading untrusted certificate "
131 "'%s' from '%s' failed\n", key, value);
132 enumerator->destroy(enumerator);
133 return FALSE;
134 }
135 conftest->creds->add_cert(conftest->creds, FALSE, cert);
136 }
137 enumerator->destroy(enumerator);
138
139 return TRUE;
140 }
141
142 /**
143 * Load configured hooks
144 */
145 static bool load_hooks()
146 {
147 enumerator_t *enumerator;
148 char *name, buf[64];
149 hook_t *(*create)(void);
150 hook_t *hook;
151
152 enumerator = conftest->test->create_section_enumerator(conftest->test,
153 "hooks");
154 while (enumerator->enumerate(enumerator, &name))
155 {
156 snprintf(buf, sizeof(buf), "%s_hook_create", name);
157 create = dlsym(RTLD_DEFAULT, buf);
158 if (create)
159 {
160 hook = create();
161 if (hook)
162 {
163 conftest->hooks->insert_last(conftest->hooks, hook);
164 charon->bus->add_listener(charon->bus, &hook->listener);
165 }
166 }
167 else
168 {
169 fprintf(stderr, "dlsym() for hook '%s' failed: %s\n", name, dlerror());
170 enumerator->destroy(enumerator);
171 return FALSE;
172 }
173 }
174 enumerator->destroy(enumerator);
175 return TRUE;
176 }
177
178 /**
179 * atexit() cleanup handler
180 */
181 static void cleanup()
182 {
183 hook_t *hook;
184
185 DESTROY_IF(conftest->suite);
186 DESTROY_IF(conftest->test);
187 lib->credmgr->remove_set(lib->credmgr, &conftest->creds->set);
188 conftest->creds->destroy(conftest->creds);
189 while (conftest->hooks->remove_last(conftest->hooks,
190 (void**)&hook) == SUCCESS)
191 {
192 charon->bus->remove_listener(charon->bus, &hook->listener);
193 hook->destroy(hook);
194 }
195 conftest->hooks->destroy(conftest->hooks);
196 if (conftest->config)
197 {
198 charon->backends->remove_backend(charon->backends,
199 &conftest->config->backend);
200 conftest->config->destroy(conftest->config);
201 }
202 free(conftest->suite_dir);
203 free(conftest->test_dir);
204 free(conftest);
205 libcharon_deinit();
206 libhydra_deinit();
207 library_deinit();
208 }
209
210 /**
211 * Main function, starts the conftest daemon.
212 */
213 int main(int argc, char *argv[])
214 {
215 struct sigaction action;
216 int status = 0;
217 sigset_t set;
218 int sig;
219 char *suite_file = "suite.conf", *test_file = NULL;
220 file_logger_t *logger;
221
222 if (!library_init(NULL))
223 {
224 library_deinit();
225 return SS_RC_LIBSTRONGSWAN_INTEGRITY;
226 }
227 if (!libhydra_init("conftest"))
228 {
229 libhydra_deinit();
230 library_deinit();
231 return SS_RC_INITIALIZATION_FAILED;
232 }
233 if (!libcharon_init())
234 {
235 libcharon_deinit();
236 libhydra_deinit();
237 library_deinit();
238 return SS_RC_INITIALIZATION_FAILED;
239 }
240
241 INIT(conftest,
242 .creds = mem_cred_create(),
243 );
244 logger = file_logger_create(stdout, NULL, FALSE);
245 logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
246 charon->bus->add_listener(charon->bus, &logger->listener);
247 charon->file_loggers->insert_last(charon->file_loggers, logger);
248
249 lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
250 conftest->hooks = linked_list_create();
251 conftest->config = config_create();
252
253 atexit(cleanup);
254
255 while (TRUE)
256 {
257 struct option long_opts[] = {
258 { "help", no_argument, NULL, 'h' },
259 { "version", no_argument, NULL, 'v' },
260 { "suite", required_argument, NULL, 's' },
261 { "test", required_argument, NULL, 't' },
262 { 0,0,0,0 }
263 };
264 switch (getopt_long(argc, argv, "", long_opts, NULL))
265 {
266 case EOF:
267 break;
268 case 'h':
269 usage(stdout);
270 return 0;
271 case 'v':
272 printf("strongSwan %s conftest\n", VERSION);
273 return 0;
274 case 's':
275 suite_file = optarg;
276 continue;
277 case 't':
278 test_file = optarg;
279 continue;
280 default:
281 usage(stderr);
282 return 1;
283 }
284 break;
285 }
286
287 if (!load_configs(suite_file, test_file))
288 {
289 return 1;
290 }
291 if (!charon->initialize(charon))
292 {
293 return 1;
294 }
295 if (!load_certs(suite_file))
296 {
297 return 1;
298 }
299 if (!load_hooks())
300 {
301 return 1;
302 }
303 charon->backends->add_backend(charon->backends, &conftest->config->backend);
304 conftest->config->load(conftest->config, conftest->suite);
305 conftest->config->load(conftest->config, conftest->test);
306
307 /* set up thread specific handlers */
308 action.sa_handler = segv_handler;
309 action.sa_flags = 0;
310 sigemptyset(&action.sa_mask);
311 sigaddset(&action.sa_mask, SIGINT);
312 sigaddset(&action.sa_mask, SIGTERM);
313 sigaddset(&action.sa_mask, SIGHUP);
314 sigaction(SIGSEGV, &action, NULL);
315 sigaction(SIGILL, &action, NULL);
316 sigaction(SIGBUS, &action, NULL);
317 action.sa_handler = SIG_IGN;
318 sigaction(SIGPIPE, &action, NULL);
319 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
320
321 /* start thread pool */
322 charon->start(charon);
323
324 /* handle SIGINT/SIGTERM in main thread */
325 sigemptyset(&set);
326 sigaddset(&set, SIGINT);
327 sigaddset(&set, SIGHUP);
328 sigaddset(&set, SIGTERM);
329 sigprocmask(SIG_BLOCK, &set, NULL);
330
331 while (sigwait(&set, &sig) == 0)
332 {
333 switch (sig)
334 {
335 case SIGINT:
336 case SIGTERM:
337 fprintf(stderr, "\nshutting down...\n");
338 break;
339 default:
340 continue;
341 }
342 break;
343 }
344 return status;
345 }