Added member fields for reserved bits and bytes in all payloads
[strongswan.git] / src / libcharon / daemon.c
1 /*
2 * Copyright (C) 2006-2010 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 #ifdef CAPABILITIES
25 #ifdef HAVE_SYS_CAPABILITY_H
26 #include <sys/capability.h>
27 #endif /* HAVE_SYS_CAPABILITY_H */
28 #endif /* CAPABILITIES */
29
30 #include "daemon.h"
31
32 #include <library.h>
33 #include <config/proposal.h>
34 #include <kernel/kernel_handler.h>
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 * capabilities to keep
54 */
55 #ifdef CAPABILITIES_LIBCAP
56 cap_t caps;
57 #endif /* CAPABILITIES_LIBCAP */
58 #ifdef CAPABILITIES_NATIVE
59 struct __user_cap_data_struct caps;
60 #endif /* CAPABILITIES_NATIVE */
61
62 };
63
64 /**
65 * One and only instance of the daemon.
66 */
67 daemon_t *charon;
68
69 /**
70 * hook in library for debugging messages
71 */
72 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
73
74 /**
75 * we store the previous debug function so we can reset it
76 */
77 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
78
79 /**
80 * Logging hook for library logs, spreads debug message over bus
81 */
82 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
83 {
84 va_list args;
85
86 va_start(args, fmt);
87 charon->bus->vlog(charon->bus, group, level, fmt, args);
88 va_end(args);
89 }
90
91 /**
92 * Clean up all daemon resources
93 */
94 static void destroy(private_daemon_t *this)
95 {
96 /* terminate all idle threads */
97 lib->processor->set_threads(lib->processor, 0);
98
99 /* close all IKE_SAs */
100 if (this->public.ike_sa_manager)
101 {
102 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
103 }
104 DESTROY_IF(this->public.receiver);
105 DESTROY_IF(this->public.sender);
106 /* unload plugins to release threads */
107 lib->plugins->unload(lib->plugins);
108 #ifdef CAPABILITIES_LIBCAP
109 cap_free(this->caps);
110 #endif /* CAPABILITIES_LIBCAP */
111 DESTROY_IF(this->kernel_handler);
112 DESTROY_IF(this->public.traps);
113 DESTROY_IF(this->public.ike_sa_manager);
114 DESTROY_IF(this->public.controller);
115 DESTROY_IF(this->public.eap);
116 DESTROY_IF(this->public.sim);
117 DESTROY_IF(this->public.tnccs);
118 #ifdef ME
119 DESTROY_IF(this->public.connect_manager);
120 DESTROY_IF(this->public.mediation_manager);
121 #endif /* ME */
122 DESTROY_IF(this->public.backends);
123 DESTROY_IF(this->public.socket);
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(this);
133 }
134
135 METHOD(daemon_t, keep_cap, void,
136 private_daemon_t *this, u_int cap)
137 {
138 #ifdef CAPABILITIES_LIBCAP
139 cap_set_flag(this->caps, CAP_EFFECTIVE, 1, &cap, CAP_SET);
140 cap_set_flag(this->caps, CAP_INHERITABLE, 1, &cap, CAP_SET);
141 cap_set_flag(this->caps, CAP_PERMITTED, 1, &cap, CAP_SET);
142 #endif /* CAPABILITIES_LIBCAP */
143 #ifdef CAPABILITIES_NATIVE
144 this->caps.effective |= 1 << cap;
145 this->caps.permitted |= 1 << cap;
146 this->caps.inheritable |= 1 << cap;
147 #endif /* CAPABILITIES_NATIVE */
148 }
149
150 METHOD(daemon_t, drop_capabilities, bool,
151 private_daemon_t *this)
152 {
153 #ifdef CAPABILITIES_LIBCAP
154 if (cap_set_proc(this->caps) != 0)
155 {
156 return FALSE;
157 }
158 #endif /* CAPABILITIES_LIBCAP */
159 #ifdef CAPABILITIES_NATIVE
160 struct __user_cap_header_struct header = {
161 .version = _LINUX_CAPABILITY_VERSION,
162 };
163 if (capset(&header, &this->caps) != 0)
164 {
165 return FALSE;
166 }
167 #endif /* CAPABILITIES_NATIVE */
168 return TRUE;
169 }
170
171 METHOD(daemon_t, start, void,
172 private_daemon_t *this)
173 {
174 /* start the engine, go multithreaded */
175 lib->processor->set_threads(lib->processor,
176 lib->settings->get_int(lib->settings, "charon.threads",
177 DEFAULT_THREADS));
178 }
179
180 /**
181 * Log loaded plugins
182 */
183 static void print_plugins()
184 {
185 char buf[512], *plugin;
186 int len = 0;
187 enumerator_t *enumerator;
188
189 buf[0] = '\0';
190 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
191 while (len < sizeof(buf) && enumerator->enumerate(enumerator, &plugin))
192 {
193 len += snprintf(&buf[len], sizeof(buf)-len, "%s ", plugin);
194 }
195 enumerator->destroy(enumerator);
196 DBG1(DBG_DMN, "loaded plugins: %s", buf);
197 }
198
199 METHOD(daemon_t, initialize, bool,
200 private_daemon_t *this)
201 {
202 DBG1(DBG_DMN, "Starting IKEv2 charon daemon (strongSwan "VERSION")");
203
204 if (lib->integrity)
205 {
206 DBG1(DBG_DMN, "integrity tests enabled:");
207 DBG1(DBG_DMN, "lib 'libstrongswan': passed file and segment integrity tests");
208 DBG1(DBG_DMN, "lib 'libhydra': passed file and segment integrity tests");
209 DBG1(DBG_DMN, "lib 'libcharon': passed file and segment integrity tests");
210 DBG1(DBG_DMN, "daemon 'charon': passed file integrity test");
211 }
212
213 /* load plugins, further infrastructure may need it */
214 if (!lib->plugins->load(lib->plugins, NULL,
215 lib->settings->get_str(lib->settings, "charon.load", PLUGINS)))
216 {
217 return FALSE;
218 }
219
220 print_plugins();
221
222 this->public.ike_sa_manager = ike_sa_manager_create();
223 if (this->public.ike_sa_manager == NULL)
224 {
225 return FALSE;
226 }
227 this->public.sender = sender_create();
228 this->public.receiver = receiver_create();
229 if (this->public.receiver == NULL)
230 {
231 return FALSE;
232 }
233
234 #ifdef ME
235 this->public.connect_manager = connect_manager_create();
236 if (this->public.connect_manager == NULL)
237 {
238 return FALSE;
239 }
240 this->public.mediation_manager = mediation_manager_create();
241 #endif /* ME */
242
243 return TRUE;
244 }
245
246 /**
247 * Create the daemon.
248 */
249 private_daemon_t *daemon_create()
250 {
251 private_daemon_t *this;
252
253 INIT(this,
254 .public = {
255 .keep_cap = _keep_cap,
256 .drop_capabilities = _drop_capabilities,
257 .initialize = _initialize,
258 .start = _start,
259 .bus = bus_create(),
260 .file_loggers = linked_list_create(),
261 .sys_loggers = linked_list_create(),
262 },
263 );
264 charon = &this->public;
265 this->public.controller = controller_create();
266 this->public.eap = eap_manager_create();
267 this->public.sim = sim_manager_create();
268 this->public.tnccs = tnccs_manager_create();
269 this->public.backends = backend_manager_create();
270 this->public.socket = socket_manager_create();
271 this->public.traps = trap_manager_create();
272 this->kernel_handler = kernel_handler_create();
273
274 #ifdef CAPABILITIES
275 #ifdef CAPABILITIES_LIBCAP
276 this->caps = cap_init();
277 #endif /* CAPABILITIES_LIBCAP */
278 keep_cap(this, CAP_NET_ADMIN);
279 if (lib->leak_detective)
280 {
281 keep_cap(this, CAP_SYS_NICE);
282 }
283 #endif /* CAPABILITIES */
284
285 return this;
286 }
287
288 /**
289 * Described in header.
290 */
291 void libcharon_deinit()
292 {
293 destroy((private_daemon_t*)charon);
294 charon = NULL;
295 }
296
297 /**
298 * Described in header.
299 */
300 bool libcharon_init()
301 {
302 private_daemon_t *this;
303
304 this = daemon_create();
305
306 /* for uncritical pseudo random numbers */
307 srandom(time(NULL) + getpid());
308
309 /* set up hook to log dbg message in library via charons message bus */
310 dbg_old = dbg;
311 dbg = dbg_bus;
312
313 lib->printf_hook->add_handler(lib->printf_hook, 'P',
314 proposal_printf_hook,
315 PRINTF_HOOK_ARGTYPE_POINTER,
316 PRINTF_HOOK_ARGTYPE_END);
317
318 if (lib->integrity &&
319 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
320 {
321 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
322 return FALSE;
323 }
324
325 return TRUE;
326 }