Store the name of the binary using libcharon to enable specific settings.
[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 #ifdef CAPABILITIES
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 */
31
32 #include "daemon.h"
33
34 #include <library.h>
35 #include <plugins/plugin.h>
36 #include <config/proposal.h>
37 #include <kernel/kernel_handler.h>
38 #include <processing/jobs/start_action_job.h>
39
40 typedef struct private_daemon_t private_daemon_t;
41
42 /**
43 * Private additions to daemon_t, contains threads and internal functions.
44 */
45 struct private_daemon_t {
46 /**
47 * Public members of daemon_t.
48 */
49 daemon_t public;
50
51 /**
52 * Handler for kernel events
53 */
54 kernel_handler_t *kernel_handler;
55
56 /**
57 * capabilities to keep
58 */
59 #ifdef CAPABILITIES_LIBCAP
60 cap_t caps;
61 #endif /* CAPABILITIES_LIBCAP */
62 #ifdef CAPABILITIES_NATIVE
63 struct __user_cap_data_struct caps[2];
64 #endif /* CAPABILITIES_NATIVE */
65
66 };
67
68 /**
69 * One and only instance of the daemon.
70 */
71 daemon_t *charon;
72
73 /**
74 * hook in library for debugging messages
75 */
76 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
77
78 /**
79 * we store the previous debug function so we can reset it
80 */
81 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
82
83 /**
84 * Logging hook for library logs, spreads debug message over bus
85 */
86 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
87 {
88 va_list args;
89
90 va_start(args, fmt);
91 charon->bus->vlog(charon->bus, group, level, fmt, args);
92 va_end(args);
93 }
94
95 /**
96 * Clean up all daemon resources
97 */
98 static void destroy(private_daemon_t *this)
99 {
100 /* terminate all idle threads */
101 lib->processor->set_threads(lib->processor, 0);
102
103 /* close all IKE_SAs */
104 if (this->public.ike_sa_manager)
105 {
106 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
107 }
108 if (this->public.traps)
109 {
110 this->public.traps->flush(this->public.traps);
111 }
112 if (this->public.sender)
113 {
114 this->public.sender->flush(this->public.sender);
115 }
116 DESTROY_IF(this->public.receiver);
117 #ifdef ME
118 DESTROY_IF(this->public.connect_manager);
119 DESTROY_IF(this->public.mediation_manager);
120 #endif /* ME */
121 /* make sure the cache is clear before unloading plugins */
122 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
123 /* unload plugins to release threads */
124 lib->plugins->unload(lib->plugins);
125 #ifdef CAPABILITIES_LIBCAP
126 cap_free(this->caps);
127 #endif /* CAPABILITIES_LIBCAP */
128 DESTROY_IF(this->kernel_handler);
129 DESTROY_IF(this->public.traps);
130 DESTROY_IF(this->public.shunts);
131 DESTROY_IF(this->public.ike_sa_manager);
132 DESTROY_IF(this->public.controller);
133 DESTROY_IF(this->public.eap);
134 DESTROY_IF(this->public.xauth);
135 DESTROY_IF(this->public.backends);
136 DESTROY_IF(this->public.sender);
137 DESTROY_IF(this->public.socket);
138
139 /* rehook library logging, shutdown logging */
140 dbg = dbg_old;
141 DESTROY_IF(this->public.bus);
142 this->public.file_loggers->destroy_offset(this->public.file_loggers,
143 offsetof(file_logger_t, destroy));
144 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
145 offsetof(sys_logger_t, destroy));
146 free(this);
147 }
148
149 METHOD(daemon_t, keep_cap, void,
150 private_daemon_t *this, u_int cap)
151 {
152 #ifdef CAPABILITIES_LIBCAP
153 cap_set_flag(this->caps, CAP_EFFECTIVE, 1, &cap, CAP_SET);
154 cap_set_flag(this->caps, CAP_INHERITABLE, 1, &cap, CAP_SET);
155 cap_set_flag(this->caps, CAP_PERMITTED, 1, &cap, CAP_SET);
156 #endif /* CAPABILITIES_LIBCAP */
157 #ifdef CAPABILITIES_NATIVE
158 int i = 0;
159
160 if (cap >= 32)
161 {
162 i++;
163 cap -= 32;
164 }
165 this->caps[i].effective |= 1 << cap;
166 this->caps[i].permitted |= 1 << cap;
167 this->caps[i].inheritable |= 1 << cap;
168 #endif /* CAPABILITIES_NATIVE */
169 }
170
171 METHOD(daemon_t, drop_capabilities, bool,
172 private_daemon_t *this)
173 {
174 #ifdef CAPABILITIES_LIBCAP
175 if (cap_set_proc(this->caps) != 0)
176 {
177 return FALSE;
178 }
179 #endif /* CAPABILITIES_LIBCAP */
180 #ifdef CAPABILITIES_NATIVE
181 struct __user_cap_header_struct header = {
182 #if defined(_LINUX_CAPABILITY_VERSION_3)
183 .version = _LINUX_CAPABILITY_VERSION_3,
184 #elif defined(_LINUX_CAPABILITY_VERSION_2)
185 .version = _LINUX_CAPABILITY_VERSION_2,
186 #elif defined(_LINUX_CAPABILITY_VERSION_1)
187 .version = _LINUX_CAPABILITY_VERSION_1,
188 #else
189 .version = _LINUX_CAPABILITY_VERSION,
190 #endif
191 };
192 if (capset(&header, this->caps) != 0)
193 {
194 return FALSE;
195 }
196 #endif /* CAPABILITIES_NATIVE */
197 return TRUE;
198 }
199
200 METHOD(daemon_t, start, void,
201 private_daemon_t *this)
202 {
203 /* start the engine, go multithreaded */
204 lib->processor->set_threads(lib->processor,
205 lib->settings->get_int(lib->settings, "charon.threads",
206 DEFAULT_THREADS));
207 }
208
209 METHOD(daemon_t, initialize, bool,
210 private_daemon_t *this, char *plugins)
211 {
212 /* load plugins, further infrastructure may need it */
213 if (!lib->plugins->load(lib->plugins, NULL, plugins))
214 {
215 return FALSE;
216 }
217 DBG1(DBG_DMN, "loaded plugins: %s",
218 lib->plugins->loaded_plugins(lib->plugins));
219
220 this->public.ike_sa_manager = ike_sa_manager_create();
221 if (this->public.ike_sa_manager == NULL)
222 {
223 return FALSE;
224 }
225 this->public.sender = sender_create();
226 this->public.receiver = receiver_create();
227 if (this->public.receiver == NULL)
228 {
229 return FALSE;
230 }
231
232 /* Queue start_action job */
233 lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
234
235 #ifdef ME
236 this->public.connect_manager = connect_manager_create();
237 if (this->public.connect_manager == NULL)
238 {
239 return FALSE;
240 }
241 this->public.mediation_manager = mediation_manager_create();
242 #endif /* ME */
243
244 return TRUE;
245 }
246
247 /**
248 * Create the daemon.
249 */
250 private_daemon_t *daemon_create(const char *name)
251 {
252 private_daemon_t *this;
253
254 INIT(this,
255 .public = {
256 .keep_cap = _keep_cap,
257 .drop_capabilities = _drop_capabilities,
258 .initialize = _initialize,
259 .start = _start,
260 .bus = bus_create(),
261 .file_loggers = linked_list_create(),
262 .sys_loggers = linked_list_create(),
263 .name = strdup(name ?: "libcharon"),
264 },
265 );
266 charon = &this->public;
267 this->public.controller = controller_create();
268 this->public.eap = eap_manager_create();
269 this->public.xauth = xauth_manager_create();
270 this->public.backends = backend_manager_create();
271 this->public.socket = socket_manager_create();
272 this->public.traps = trap_manager_create();
273 this->public.shunts = shunt_manager_create();
274 this->kernel_handler = kernel_handler_create();
275
276 #ifdef CAPABILITIES
277 #ifdef CAPABILITIES_LIBCAP
278 this->caps = cap_init();
279 #endif /* CAPABILITIES_LIBCAP */
280 keep_cap(this, CAP_NET_ADMIN);
281 if (lib->leak_detective)
282 {
283 keep_cap(this, CAP_SYS_NICE);
284 }
285 #endif /* CAPABILITIES */
286
287 return this;
288 }
289
290 /**
291 * Described in header.
292 */
293 void libcharon_deinit()
294 {
295 destroy((private_daemon_t*)charon);
296 charon = NULL;
297 }
298
299 /**
300 * Described in header.
301 */
302 bool libcharon_init(const char *name)
303 {
304 daemon_create(name);
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 }