restrict PA-TNC messages to maximum size
[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 typedef struct private_daemon_t private_daemon_t;
33
34 /**
35 * Private additions to daemon_t, contains threads and internal functions.
36 */
37 struct private_daemon_t {
38 /**
39 * Public members of daemon_t.
40 */
41 daemon_t public;
42
43 /**
44 * Handler for kernel events
45 */
46 kernel_handler_t *kernel_handler;
47 };
48
49 /**
50 * One and only instance of the daemon.
51 */
52 daemon_t *charon;
53
54 /**
55 * hook in library for debugging messages
56 */
57 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
58
59 /**
60 * we store the previous debug function so we can reset it
61 */
62 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
63
64 /**
65 * Logging hook for library logs, spreads debug message over bus
66 */
67 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
68 {
69 va_list args;
70
71 va_start(args, fmt);
72 charon->bus->vlog(charon->bus, group, level, fmt, args);
73 va_end(args);
74 }
75
76 /**
77 * Clean up all daemon resources
78 */
79 static void destroy(private_daemon_t *this)
80 {
81 /* terminate all idle threads */
82 lib->processor->set_threads(lib->processor, 0);
83
84 /* close all IKE_SAs */
85 if (this->public.ike_sa_manager)
86 {
87 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
88 }
89 if (this->public.traps)
90 {
91 this->public.traps->flush(this->public.traps);
92 }
93 if (this->public.sender)
94 {
95 this->public.sender->flush(this->public.sender);
96 }
97
98 /* cancel all threads and wait for their termination */
99 lib->processor->cancel(lib->processor);
100
101 DESTROY_IF(this->public.receiver);
102 #ifdef ME
103 DESTROY_IF(this->public.connect_manager);
104 DESTROY_IF(this->public.mediation_manager);
105 #endif /* ME */
106 /* make sure the cache is clear before unloading plugins */
107 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
108 lib->plugins->unload(lib->plugins);
109 DESTROY_IF(this->kernel_handler);
110 DESTROY_IF(this->public.traps);
111 DESTROY_IF(this->public.shunts);
112 DESTROY_IF(this->public.ike_sa_manager);
113 DESTROY_IF(this->public.controller);
114 DESTROY_IF(this->public.eap);
115 DESTROY_IF(this->public.xauth);
116 DESTROY_IF(this->public.backends);
117 DESTROY_IF(this->public.sender);
118 DESTROY_IF(this->public.socket);
119 DESTROY_IF(this->public.caps);
120
121 /* rehook library logging, shutdown logging */
122 dbg = dbg_old;
123 DESTROY_IF(this->public.bus);
124 this->public.file_loggers->destroy_offset(this->public.file_loggers,
125 offsetof(file_logger_t, destroy));
126 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
127 offsetof(sys_logger_t, destroy));
128 free((void*)this->public.name);
129 free(this);
130 }
131
132 METHOD(daemon_t, start, void,
133 private_daemon_t *this)
134 {
135 /* start the engine, go multithreaded */
136 lib->processor->set_threads(lib->processor,
137 lib->settings->get_int(lib->settings, "%s.threads",
138 DEFAULT_THREADS, charon->name));
139 }
140
141 METHOD(daemon_t, initialize, bool,
142 private_daemon_t *this, char *plugins)
143 {
144 static plugin_feature_t features[] = {
145 PLUGIN_PROVIDE(CUSTOM, "libcharon"),
146 PLUGIN_DEPENDS(HASHER, HASH_SHA1),
147 PLUGIN_DEPENDS(RNG, RNG_STRONG),
148 PLUGIN_DEPENDS(NONCE_GEN),
149 PLUGIN_DEPENDS(CUSTOM, "kernel-ipsec"),
150 PLUGIN_DEPENDS(CUSTOM, "kernel-net"),
151 PLUGIN_DEPENDS(CUSTOM, "socket"),
152 };
153 lib->plugins->add_static_features(lib->plugins, charon->name, features,
154 countof(features), TRUE);
155
156 /* load plugins, further infrastructure may need it */
157 if (!lib->plugins->load(lib->plugins, NULL, plugins))
158 {
159 return FALSE;
160 }
161 DBG1(DBG_DMN, "loaded plugins: %s",
162 lib->plugins->loaded_plugins(lib->plugins));
163
164 this->public.ike_sa_manager = ike_sa_manager_create();
165 if (this->public.ike_sa_manager == NULL)
166 {
167 return FALSE;
168 }
169 this->public.sender = sender_create();
170 this->public.receiver = receiver_create();
171 if (this->public.receiver == NULL)
172 {
173 return FALSE;
174 }
175
176 /* Queue start_action job */
177 lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
178
179 #ifdef ME
180 this->public.connect_manager = connect_manager_create();
181 if (this->public.connect_manager == NULL)
182 {
183 return FALSE;
184 }
185 this->public.mediation_manager = mediation_manager_create();
186 #endif /* ME */
187
188 return TRUE;
189 }
190
191 /**
192 * Create the daemon.
193 */
194 private_daemon_t *daemon_create(const char *name)
195 {
196 private_daemon_t *this;
197
198 INIT(this,
199 .public = {
200 .initialize = _initialize,
201 .start = _start,
202 .bus = bus_create(),
203 .file_loggers = linked_list_create(),
204 .sys_loggers = linked_list_create(),
205 .name = strdup(name ?: "libcharon"),
206 },
207 );
208 charon = &this->public;
209 this->public.caps = capabilities_create();
210 this->public.controller = controller_create();
211 this->public.eap = eap_manager_create();
212 this->public.xauth = xauth_manager_create();
213 this->public.backends = backend_manager_create();
214 this->public.socket = socket_manager_create();
215 this->public.traps = trap_manager_create();
216 this->public.shunts = shunt_manager_create();
217 this->kernel_handler = kernel_handler_create();
218
219 this->public.caps->keep(this->public.caps, CAP_NET_ADMIN);
220
221 return this;
222 }
223
224 /**
225 * Described in header.
226 */
227 void libcharon_deinit()
228 {
229 destroy((private_daemon_t*)charon);
230 charon = NULL;
231 }
232
233 /**
234 * Described in header.
235 */
236 bool libcharon_init(const char *name)
237 {
238 daemon_create(name);
239
240 /* for uncritical pseudo random numbers */
241 srandom(time(NULL) + getpid());
242
243 /* set up hook to log dbg message in library via charons message bus */
244 dbg_old = dbg;
245 dbg = dbg_bus;
246
247 lib->printf_hook->add_handler(lib->printf_hook, 'P',
248 proposal_printf_hook,
249 PRINTF_HOOK_ARGTYPE_POINTER,
250 PRINTF_HOOK_ARGTYPE_END);
251
252 if (lib->integrity &&
253 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
254 {
255 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
256 return FALSE;
257 }
258
259 return TRUE;
260 }