612796a786f1a5cacaa3dfdf38fb5497fadb1020
[strongswan.git] / src / libcharon / daemon.c
1 /*
2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <time.h>
23
24 #include "daemon.h"
25
26 #include <library.h>
27 #include <plugins/plugin_feature.h>
28 #include <config/proposal.h>
29 #include <kernel/kernel_handler.h>
30 #include <processing/jobs/start_action_job.h>
31
32 #ifndef CAP_NET_ADMIN
33 #define CAP_NET_ADMIN 12
34 #endif
35
36 typedef struct private_daemon_t private_daemon_t;
37
38 /**
39 * Private additions to daemon_t, contains threads and internal functions.
40 */
41 struct private_daemon_t {
42 /**
43 * Public members of daemon_t.
44 */
45 daemon_t public;
46
47 /**
48 * Handler for kernel events
49 */
50 kernel_handler_t *kernel_handler;
51 };
52
53 /**
54 * One and only instance of the daemon.
55 */
56 daemon_t *charon;
57
58 /**
59 * hook in library for debugging messages
60 */
61 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
62
63 /**
64 * we store the previous debug function so we can reset it
65 */
66 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
67
68 /**
69 * Logging hook for library logs, spreads debug message over bus
70 */
71 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
72 {
73 va_list args;
74
75 va_start(args, fmt);
76 charon->bus->vlog(charon->bus, group, level, fmt, args);
77 va_end(args);
78 }
79
80 /**
81 * Clean up all daemon resources
82 */
83 static void destroy(private_daemon_t *this)
84 {
85 /* terminate all idle threads */
86 lib->processor->set_threads(lib->processor, 0);
87
88 /* close all IKE_SAs */
89 if (this->public.ike_sa_manager)
90 {
91 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
92 }
93 if (this->public.traps)
94 {
95 this->public.traps->flush(this->public.traps);
96 }
97 if (this->public.sender)
98 {
99 this->public.sender->flush(this->public.sender);
100 }
101
102 /* cancel all threads and wait for their termination */
103 lib->processor->cancel(lib->processor);
104
105 DESTROY_IF(this->public.receiver);
106 #ifdef ME
107 DESTROY_IF(this->public.connect_manager);
108 DESTROY_IF(this->public.mediation_manager);
109 #endif /* ME */
110 /* make sure the cache is clear before unloading plugins */
111 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
112 lib->plugins->unload(lib->plugins);
113 DESTROY_IF(this->kernel_handler);
114 DESTROY_IF(this->public.traps);
115 DESTROY_IF(this->public.shunts);
116 DESTROY_IF(this->public.ike_sa_manager);
117 DESTROY_IF(this->public.controller);
118 DESTROY_IF(this->public.eap);
119 DESTROY_IF(this->public.xauth);
120 DESTROY_IF(this->public.backends);
121 DESTROY_IF(this->public.sender);
122 DESTROY_IF(this->public.socket);
123 DESTROY_IF(this->public.caps);
124
125 /* rehook library logging, shutdown logging */
126 dbg = dbg_old;
127 DESTROY_IF(this->public.bus);
128 this->public.file_loggers->destroy_offset(this->public.file_loggers,
129 offsetof(file_logger_t, destroy));
130 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
131 offsetof(sys_logger_t, destroy));
132 free((void*)this->public.name);
133 free(this);
134 }
135
136 METHOD(daemon_t, start, void,
137 private_daemon_t *this)
138 {
139 /* start the engine, go multithreaded */
140 lib->processor->set_threads(lib->processor,
141 lib->settings->get_int(lib->settings, "%s.threads",
142 DEFAULT_THREADS, charon->name));
143 }
144
145 METHOD(daemon_t, initialize, bool,
146 private_daemon_t *this, char *plugins)
147 {
148 static plugin_feature_t features[] = {
149 PLUGIN_PROVIDE(CUSTOM, "libcharon"),
150 PLUGIN_DEPENDS(HASHER, HASH_SHA1),
151 PLUGIN_DEPENDS(RNG, RNG_STRONG),
152 PLUGIN_DEPENDS(NONCE_GEN),
153 PLUGIN_DEPENDS(CUSTOM, "kernel-ipsec"),
154 PLUGIN_DEPENDS(CUSTOM, "kernel-net"),
155 PLUGIN_DEPENDS(CUSTOM, "socket"),
156 };
157 lib->plugins->add_static_features(lib->plugins, charon->name, features,
158 countof(features), TRUE);
159
160 /* load plugins, further infrastructure may need it */
161 if (!lib->plugins->load(lib->plugins, NULL, plugins))
162 {
163 return FALSE;
164 }
165 DBG1(DBG_DMN, "loaded plugins: %s",
166 lib->plugins->loaded_plugins(lib->plugins));
167
168 this->public.ike_sa_manager = ike_sa_manager_create();
169 if (this->public.ike_sa_manager == NULL)
170 {
171 return FALSE;
172 }
173 this->public.sender = sender_create();
174 this->public.receiver = receiver_create();
175 if (this->public.receiver == NULL)
176 {
177 return FALSE;
178 }
179
180 /* Queue start_action job */
181 lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
182
183 #ifdef ME
184 this->public.connect_manager = connect_manager_create();
185 if (this->public.connect_manager == NULL)
186 {
187 return FALSE;
188 }
189 this->public.mediation_manager = mediation_manager_create();
190 #endif /* ME */
191
192 return TRUE;
193 }
194
195 /**
196 * Create the daemon.
197 */
198 private_daemon_t *daemon_create(const char *name)
199 {
200 private_daemon_t *this;
201
202 INIT(this,
203 .public = {
204 .initialize = _initialize,
205 .start = _start,
206 .bus = bus_create(),
207 .file_loggers = linked_list_create(),
208 .sys_loggers = linked_list_create(),
209 .name = strdup(name ?: "libcharon"),
210 },
211 );
212 charon = &this->public;
213 this->public.caps = capabilities_create();
214 this->public.controller = controller_create();
215 this->public.eap = eap_manager_create();
216 this->public.xauth = xauth_manager_create();
217 this->public.backends = backend_manager_create();
218 this->public.socket = socket_manager_create();
219 this->public.traps = trap_manager_create();
220 this->public.shunts = shunt_manager_create();
221 this->kernel_handler = kernel_handler_create();
222
223 this->public.caps->keep(this->public.caps, CAP_NET_ADMIN);
224
225 return this;
226 }
227
228 /**
229 * Described in header.
230 */
231 void libcharon_deinit()
232 {
233 destroy((private_daemon_t*)charon);
234 charon = NULL;
235 }
236
237 /**
238 * Described in header.
239 */
240 bool libcharon_init(const char *name)
241 {
242 daemon_create(name);
243
244 /* for uncritical pseudo random numbers */
245 srandom(time(NULL) + getpid());
246
247 /* set up hook to log dbg message in library via charons message bus */
248 dbg_old = dbg;
249 dbg = dbg_bus;
250
251 lib->printf_hook->add_handler(lib->printf_hook, 'P',
252 proposal_printf_hook,
253 PRINTF_HOOK_ARGTYPE_POINTER,
254 PRINTF_HOOK_ARGTYPE_END);
255
256 if (lib->integrity &&
257 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
258 {
259 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
260 return FALSE;
261 }
262
263 return TRUE;
264 }