Merge branch 'win'
[strongswan.git] / src / libimcv / imcv.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imcv.h"
16 #include "ietf/ietf_attr.h"
17 #include "ita/ita_attr.h"
18
19 #include <utils/debug.h>
20 #include <utils/utils.h>
21 #include <pen/pen.h>
22
23 #ifdef HAVE_SYSLOG
24 #include <syslog.h>
25 #endif
26
27 #define IMCV_DEBUG_LEVEL 1
28 #define IMCV_DEFAULT_POLICY_SCRIPT "ipsec _imv_policy"
29
30
31 /**
32 * PA-TNC attribute manager
33 */
34 pa_tnc_attr_manager_t *imcv_pa_tnc_attributes;
35
36 /**
37 * Global list of IMV sessions
38 */
39 imv_session_manager_t *imcv_sessions;
40
41 /**
42 * Global IMV database
43 */
44 imv_database_t *imcv_db;
45
46 /**
47 * Reference count for libimcv
48 */
49 static refcount_t libimcv_ref = 0;
50
51 /**
52 * Reference count for libstrongswan
53 */
54 static refcount_t libstrongswan_ref = 0;
55
56 /**
57 * Global configuration of imcv dbg function
58 */
59 static int imcv_debug_level;
60 static bool imcv_stderr_quiet;
61
62 /**
63 * imvc dbg function
64 */
65 static void imcv_dbg(debug_t group, level_t level, char *fmt, ...)
66 {
67 va_list args;
68
69 if (level <= imcv_debug_level)
70 {
71 if (!imcv_stderr_quiet)
72 {
73 va_start(args, fmt);
74 fprintf(stderr, "[HSR] ");
75 vfprintf(stderr, fmt, args);
76 fprintf(stderr, "\n");
77 va_end(args);
78 }
79
80 #ifdef HAVE_SYSLOG
81 {
82 int priority = LOG_INFO;
83 char buffer[8192];
84 char *current = buffer, *next;
85
86 /* write in memory buffer first */
87 va_start(args, fmt);
88 vsnprintf(buffer, sizeof(buffer), fmt, args);
89 va_end(args);
90
91 /* do a syslog with every line */
92 while (current)
93 {
94 next = strchr(current, '\n');
95 if (next)
96 {
97 *(next++) = '\0';
98 }
99 syslog(priority, "[HSR] %s\n", current);
100 current = next;
101 }
102 }
103 #endif /* HAVE_SYSLOG */
104 }
105 }
106
107 /**
108 * Described in header.
109 */
110 bool libimcv_init(bool is_imv)
111 {
112 /* initialize libstrongswan library only once */
113 if (lib)
114 {
115 /* did main program initialize libstrongswan? */
116 if (libstrongswan_ref == 0)
117 {
118 ref_get(&libstrongswan_ref);
119 }
120 }
121 else
122 {
123 /* we are the first to initialize libstrongswan */
124 if (!library_init(NULL, "libimcv"))
125 {
126 return FALSE;
127 }
128
129 /* set the debug level and stderr output */
130 imcv_debug_level = lib->settings->get_int(lib->settings,
131 "libimcv.debug_level", IMCV_DEBUG_LEVEL);
132 imcv_stderr_quiet = lib->settings->get_int(lib->settings,
133 "libimcv.stderr_quiet", FALSE);
134
135 /* activate the imcv debugging hook */
136 dbg = imcv_dbg;
137 #ifdef HAVE_SYSLOG
138 openlog("imcv", 0, LOG_DAEMON);
139 #endif
140
141 if (!lib->plugins->load(lib->plugins,
142 lib->settings->get_str(lib->settings, "libimcv.load",
143 "random nonce gmp pubkey x509")))
144 {
145 library_deinit();
146 return FALSE;
147 }
148 }
149 ref_get(&libstrongswan_ref);
150
151 lib->settings->add_fallback(lib->settings, "%s.imcv", "libimcv", lib->ns);
152 lib->settings->add_fallback(lib->settings, "%s.plugins", "libimcv.plugins",
153 lib->ns);
154
155 if (libimcv_ref == 0)
156 {
157 char *uri, *script;
158
159 /* initialize the PA-TNC attribute manager */
160 imcv_pa_tnc_attributes = pa_tnc_attr_manager_create();
161 imcv_pa_tnc_attributes->add_vendor(imcv_pa_tnc_attributes, PEN_IETF,
162 ietf_attr_create_from_data, ietf_attr_names);
163 imcv_pa_tnc_attributes->add_vendor(imcv_pa_tnc_attributes, PEN_ITA,
164 ita_attr_create_from_data, ita_attr_names);
165
166 if (is_imv)
167 {
168 /* instantiate global IMV session manager */
169 imcv_sessions = imv_session_manager_create();
170
171 /* instantiate and attach global IMV database if URI is valid */
172 uri = lib->settings->get_str(lib->settings,
173 "%s.imcv.database", NULL, lib->ns);
174 script = lib->settings->get_str(lib->settings,
175 "%s.imcv.policy_script", IMCV_DEFAULT_POLICY_SCRIPT,
176 lib->ns);
177 if (uri)
178 {
179 imcv_db = imv_database_create(uri, script);
180 }
181 }
182 DBG1(DBG_LIB, "libimcv initialized");
183 }
184 ref_get(&libimcv_ref);
185
186 return TRUE;
187 }
188
189 /**
190 * Described in header.
191 */
192 void libimcv_deinit(void)
193 {
194 if (ref_put(&libimcv_ref))
195 {
196 imcv_pa_tnc_attributes->remove_vendor(imcv_pa_tnc_attributes, PEN_IETF);
197 imcv_pa_tnc_attributes->remove_vendor(imcv_pa_tnc_attributes, PEN_ITA);
198 DESTROY_IF(imcv_pa_tnc_attributes);
199 imcv_pa_tnc_attributes = NULL;
200 DESTROY_IF(imcv_db);
201 DESTROY_IF(imcv_sessions);
202 DBG1(DBG_LIB, "libimcv terminated");
203 }
204 if (ref_put(&libstrongswan_ref))
205 {
206 library_deinit();
207 }
208 }