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
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>.
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
20 #include <sys/types.h>
25 # ifdef HAVE_SYS_CAPABILITY_H
26 # include <sys/capability.h>
27 # elif defined(CAPABILITIES_NATIVE)
28 # include <linux/capability.h>
29 # endif /* CAPABILITIES_NATIVE */
30 #endif /* CAPABILITIES */
35 #include <plugins/plugin.h>
36 #include <config/proposal.h>
37 #include <kernel/kernel_handler.h>
38 #include <processing/jobs/start_action_job.h>
40 typedef struct private_daemon_t private_daemon_t
;
43 * Private additions to daemon_t, contains threads and internal functions.
45 struct private_daemon_t
{
47 * Public members of daemon_t.
52 * Handler for kernel events
54 kernel_handler_t
*kernel_handler
;
57 * capabilities to keep
59 #ifdef CAPABILITIES_LIBCAP
61 #endif /* CAPABILITIES_LIBCAP */
62 #ifdef CAPABILITIES_NATIVE
63 struct __user_cap_data_struct caps
[2];
64 #endif /* CAPABILITIES_NATIVE */
69 * One and only instance of the daemon.
74 * hook in library for debugging messages
76 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
79 * we store the previous debug function so we can reset it
81 static void (*dbg_old
) (debug_t group
, level_t level
, char *fmt
, ...);
84 * Logging hook for library logs, spreads debug message over bus
86 static void dbg_bus(debug_t group
, level_t level
, char *fmt
, ...)
91 charon
->bus
->vlog(charon
->bus
, group
, level
, fmt
, args
);
96 * Clean up all daemon resources
98 static void destroy(private_daemon_t
*this)
100 /* terminate all idle threads */
101 lib
->processor
->set_threads(lib
->processor
, 0);
103 /* close all IKE_SAs */
104 if (this->public.ike_sa_manager
)
106 this->public.ike_sa_manager
->flush(this->public.ike_sa_manager
);
108 if (this->public.traps
)
110 this->public.traps
->flush(this->public.traps
);
112 DESTROY_IF(this->public.receiver
);
113 DESTROY_IF(this->public.sender
);
114 /* unload plugins to release threads */
115 lib
->plugins
->unload(lib
->plugins
);
116 #ifdef CAPABILITIES_LIBCAP
117 cap_free(this->caps
);
118 #endif /* CAPABILITIES_LIBCAP */
119 DESTROY_IF(this->kernel_handler
);
120 DESTROY_IF(this->public.traps
);
121 DESTROY_IF(this->public.shunts
);
122 DESTROY_IF(this->public.ike_sa_manager
);
123 DESTROY_IF(this->public.controller
);
124 DESTROY_IF(this->public.eap
);
125 DESTROY_IF(this->public.xauth
);
127 DESTROY_IF(this->public.connect_manager
);
128 DESTROY_IF(this->public.mediation_manager
);
130 DESTROY_IF(this->public.backends
);
131 DESTROY_IF(this->public.socket
);
133 /* rehook library logging, shutdown logging */
135 DESTROY_IF(this->public.bus
);
136 this->public.file_loggers
->destroy_offset(this->public.file_loggers
,
137 offsetof(file_logger_t
, destroy
));
138 this->public.sys_loggers
->destroy_offset(this->public.sys_loggers
,
139 offsetof(sys_logger_t
, destroy
));
143 METHOD(daemon_t
, keep_cap
, void,
144 private_daemon_t
*this, u_int cap
)
146 #ifdef CAPABILITIES_LIBCAP
147 cap_set_flag(this->caps
, CAP_EFFECTIVE
, 1, &cap
, CAP_SET
);
148 cap_set_flag(this->caps
, CAP_INHERITABLE
, 1, &cap
, CAP_SET
);
149 cap_set_flag(this->caps
, CAP_PERMITTED
, 1, &cap
, CAP_SET
);
150 #endif /* CAPABILITIES_LIBCAP */
151 #ifdef CAPABILITIES_NATIVE
159 this->caps
[i
].effective
|= 1 << cap
;
160 this->caps
[i
].permitted
|= 1 << cap
;
161 this->caps
[i
].inheritable
|= 1 << cap
;
162 #endif /* CAPABILITIES_NATIVE */
165 METHOD(daemon_t
, drop_capabilities
, bool,
166 private_daemon_t
*this)
168 #ifdef CAPABILITIES_LIBCAP
169 if (cap_set_proc(this->caps
) != 0)
173 #endif /* CAPABILITIES_LIBCAP */
174 #ifdef CAPABILITIES_NATIVE
175 struct __user_cap_header_struct header
= {
176 #if defined(_LINUX_CAPABILITY_VERSION_3)
177 .version
= _LINUX_CAPABILITY_VERSION_3
,
178 #elif defined(_LINUX_CAPABILITY_VERSION_2)
179 .version
= _LINUX_CAPABILITY_VERSION_2
,
180 #elif defined(_LINUX_CAPABILITY_VERSION_1)
181 .version
= _LINUX_CAPABILITY_VERSION_1
,
183 .version
= _LINUX_CAPABILITY_VERSION
,
186 if (capset(&header
, this->caps
) != 0)
190 #endif /* CAPABILITIES_NATIVE */
194 METHOD(daemon_t
, start
, void,
195 private_daemon_t
*this)
197 /* start the engine, go multithreaded */
198 lib
->processor
->set_threads(lib
->processor
,
199 lib
->settings
->get_int(lib
->settings
, "charon.threads",
206 static void print_plugins()
210 enumerator_t
*enumerator
;
214 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
215 while (len
< sizeof(buf
) && enumerator
->enumerate(enumerator
, &plugin
, NULL
))
217 len
+= snprintf(&buf
[len
], sizeof(buf
)-len
, "%s ",
218 plugin
->get_name(plugin
));
220 enumerator
->destroy(enumerator
);
221 DBG1(DBG_DMN
, "loaded plugins: %s", buf
);
224 METHOD(daemon_t
, initialize
, bool,
225 private_daemon_t
*this)
227 DBG1(DBG_DMN
, "Starting IKEv2 charon daemon (strongSwan "VERSION
")");
231 DBG1(DBG_DMN
, "integrity tests enabled:");
232 DBG1(DBG_DMN
, "lib 'libstrongswan': passed file and segment integrity tests");
233 DBG1(DBG_DMN
, "lib 'libhydra': passed file and segment integrity tests");
234 DBG1(DBG_DMN
, "lib 'libcharon': passed file and segment integrity tests");
235 DBG1(DBG_DMN
, "daemon 'charon': passed file integrity test");
238 /* load plugins, further infrastructure may need it */
239 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
240 lib
->settings
->get_str(lib
->settings
, "charon.load", PLUGINS
)))
247 this->public.ike_sa_manager
= ike_sa_manager_create();
248 if (this->public.ike_sa_manager
== NULL
)
252 this->public.sender
= sender_create();
253 this->public.receiver
= receiver_create();
254 if (this->public.receiver
== NULL
)
259 /* Queue start_action job */
260 lib
->processor
->queue_job(lib
->processor
, (job_t
*)start_action_job_create());
263 this->public.connect_manager
= connect_manager_create();
264 if (this->public.connect_manager
== NULL
)
268 this->public.mediation_manager
= mediation_manager_create();
277 private_daemon_t
*daemon_create()
279 private_daemon_t
*this;
283 .keep_cap
= _keep_cap
,
284 .drop_capabilities
= _drop_capabilities
,
285 .initialize
= _initialize
,
288 .file_loggers
= linked_list_create(),
289 .sys_loggers
= linked_list_create(),
292 charon
= &this->public;
293 this->public.controller
= controller_create();
294 this->public.eap
= eap_manager_create();
295 this->public.xauth
= xauth_manager_create();
296 this->public.backends
= backend_manager_create();
297 this->public.socket
= socket_manager_create();
298 this->public.traps
= trap_manager_create();
299 this->public.shunts
= shunt_manager_create();
300 this->kernel_handler
= kernel_handler_create();
303 #ifdef CAPABILITIES_LIBCAP
304 this->caps
= cap_init();
305 #endif /* CAPABILITIES_LIBCAP */
306 keep_cap(this, CAP_NET_ADMIN
);
307 if (lib
->leak_detective
)
309 keep_cap(this, CAP_SYS_NICE
);
311 #endif /* CAPABILITIES */
317 * Described in header.
319 void libcharon_deinit()
321 destroy((private_daemon_t
*)charon
);
326 * Described in header.
328 bool libcharon_init()
332 /* for uncritical pseudo random numbers */
333 srandom(time(NULL
) + getpid());
335 /* set up hook to log dbg message in library via charons message bus */
339 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'P',
340 proposal_printf_hook
,
341 PRINTF_HOOK_ARGTYPE_POINTER
,
342 PRINTF_HOOK_ARGTYPE_END
);
344 if (lib
->integrity
&&
345 !lib
->integrity
->check(lib
->integrity
, "libcharon", libcharon_init
))
347 dbg(DBG_DMN
, 1, "integrity check of libcharon failed");