2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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>.
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
27 #include "hooks/hook.h"
29 #include <threading/thread.h>
32 * Conftest globals struct
37 * Print usage information
39 static void usage(FILE *out
)
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");
50 * Handle SIGSEGV/SIGILL signals raised by threads
52 static void segv_handler(int signal
)
54 fprintf(stderr
, "thread %u received %d\n", thread_current_id(), signal
);
59 * Load suite and test specific configurations
61 static bool load_configs(char *suite_file
, char *test_file
)
65 fprintf(stderr
, "Missing test configuration file.\n");
68 if (access(suite_file
, R_OK
) != 0)
70 fprintf(stderr
, "Reading suite configuration file '%s' failed: %s.\n",
71 suite_file
, strerror(errno
));
74 if (access(test_file
, R_OK
) != 0)
76 fprintf(stderr
, "Reading test configuration file '%s' failed: %s.\n",
77 test_file
, strerror(errno
));
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
);
90 * Load certificates from the confiuguration file
92 static bool load_certs()
94 enumerator_t
*enumerator
;
98 if (chdir(conftest
->suite_dir
) != 0)
100 fprintf(stderr
, "opening suite directory '%s' failed",
101 conftest
->suite_dir
);
105 enumerator
= conftest
->suite
->create_key_value_enumerator(
106 conftest
->suite
, "certs.trusted");
107 while (enumerator
->enumerate(enumerator
, &key
, &value
))
109 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
110 BUILD_FROM_FILE
, value
, BUILD_END
);
113 fprintf(stderr
, "loading trusted certificate "
114 "'%s' from '%s' failed\n", key
, value
);
115 enumerator
->destroy(enumerator
);
118 conftest
->creds
->add_cert(conftest
->creds
, TRUE
, cert
);
120 enumerator
->destroy(enumerator
);
122 enumerator
= conftest
->suite
->create_key_value_enumerator(
123 conftest
->suite
, "certs.untrusted");
124 while (enumerator
->enumerate(enumerator
, &key
, &value
))
126 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
127 BUILD_FROM_FILE
, value
, BUILD_END
);
130 fprintf(stderr
, "loading untrusted certificate "
131 "'%s' from '%s' failed\n", key
, value
);
132 enumerator
->destroy(enumerator
);
135 conftest
->creds
->add_cert(conftest
->creds
, FALSE
, cert
);
137 enumerator
->destroy(enumerator
);
143 * Load configured hooks
145 static bool load_hooks()
147 enumerator_t
*enumerator
;
149 hook_t
*(*create
)(void);
152 enumerator
= conftest
->test
->create_section_enumerator(conftest
->test
,
154 while (enumerator
->enumerate(enumerator
, &name
))
156 snprintf(buf
, sizeof(buf
), "%s_hook_create", name
);
157 create
= dlsym(RTLD_DEFAULT
, buf
);
163 conftest
->hooks
->insert_last(conftest
->hooks
, hook
);
164 charon
->bus
->add_listener(charon
->bus
, &hook
->listener
);
169 fprintf(stderr
, "dlsym() for hook '%s' failed: %s\n", name
, dlerror());
170 enumerator
->destroy(enumerator
);
174 enumerator
->destroy(enumerator
);
179 * atexit() cleanup handler
181 static void cleanup()
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
)
192 charon
->bus
->remove_listener(charon
->bus
, &hook
->listener
);
195 conftest
->hooks
->destroy(conftest
->hooks
);
196 if (conftest
->config
)
198 charon
->backends
->remove_backend(charon
->backends
,
199 &conftest
->config
->backend
);
200 conftest
->config
->destroy(conftest
->config
);
202 free(conftest
->suite_dir
);
203 free(conftest
->test_dir
);
211 * Main function, starts the conftest daemon.
213 int main(int argc
, char *argv
[])
215 struct sigaction action
;
219 char *suite_file
= "suite.conf", *test_file
= NULL
;
220 file_logger_t
*logger
;
222 if (!library_init(NULL
))
225 return SS_RC_LIBSTRONGSWAN_INTEGRITY
;
227 if (!libhydra_init("conftest"))
231 return SS_RC_INITIALIZATION_FAILED
;
233 if (!libcharon_init())
238 return SS_RC_INITIALIZATION_FAILED
;
242 .creds
= mem_cred_create(),
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
);
249 lib
->credmgr
->add_set(lib
->credmgr
, &conftest
->creds
->set
);
250 conftest
->hooks
= linked_list_create();
251 conftest
->config
= config_create();
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' },
264 switch (getopt_long(argc
, argv
, "", long_opts
, NULL
))
272 printf("strongSwan %s conftest\n", VERSION
);
287 if (!load_configs(suite_file
, test_file
))
291 if (!charon
->initialize(charon
))
295 if (!load_certs(suite_file
))
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
);
307 /* set up thread specific handlers */
308 action
.sa_handler
= segv_handler
;
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
);
321 /* start thread pool */
322 charon
->start(charon
);
324 /* handle SIGINT/SIGTERM in main thread */
326 sigaddset(&set
, SIGINT
);
327 sigaddset(&set
, SIGHUP
);
328 sigaddset(&set
, SIGTERM
);
329 sigprocmask(SIG_BLOCK
, &set
, NULL
);
331 while (sigwait(&set
, &sig
) == 0)
337 fprintf(stderr
, "\nshutting down...\n");