2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "ietf/ietf_attr.h"
17 #include "ita/ita_attr.h"
19 #include <utils/debug.h>
20 #include <utils/utils.h>
25 #define IMCV_DEBUG_LEVEL 1
26 #define IMCV_DEFAULT_POLICY_SCRIPT "ipsec _imv_policy"
30 * PA-TNC attribute manager
32 pa_tnc_attr_manager_t
*imcv_pa_tnc_attributes
;
37 imv_database_t
*imcv_db
;
40 * Reference count for libimcv
42 static refcount_t libimcv_ref
= 0;
45 * Reference count for libstrongswan
47 static refcount_t libstrongswan_ref
= 0;
50 * Global configuration of imcv dbg function
52 static int imcv_debug_level
;
53 static bool imcv_stderr_quiet
;
58 static void imcv_dbg(debug_t group
, level_t level
, char *fmt
, ...)
60 int priority
= LOG_INFO
;
62 char *current
= buffer
, *next
;
65 if (level
<= imcv_debug_level
)
67 if (!imcv_stderr_quiet
)
70 fprintf(stderr
, "[HSR] ");
71 vfprintf(stderr
, fmt
, args
);
72 fprintf(stderr
, "\n");
76 /* write in memory buffer first */
78 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
81 /* do a syslog with every line */
84 next
= strchr(current
, '\n');
89 syslog(priority
, "[HSR] %s\n", current
);
96 * Described in header.
98 bool libimcv_init(bool is_imv
)
100 /* initialize libstrongswan library only once */
103 /* did main program initialize libstrongswan? */
104 if (libstrongswan_ref
== 0)
106 ref_get(&libstrongswan_ref
);
111 /* we are the first to initialize libstrongswan */
112 if (!library_init(NULL
, "libimcv"))
117 /* set the debug level and stderr output */
118 imcv_debug_level
= lib
->settings
->get_int(lib
->settings
,
119 "libimcv.debug_level", IMCV_DEBUG_LEVEL
);
120 imcv_stderr_quiet
= lib
->settings
->get_int(lib
->settings
,
121 "libimcv.stderr_quiet", FALSE
);
123 /* activate the imcv debugging hook */
125 openlog("imcv", 0, LOG_DAEMON
);
127 if (!lib
->plugins
->load(lib
->plugins
,
128 lib
->settings
->get_str(lib
->settings
, "libimcv.load",
129 "random nonce gmp pubkey x509")))
135 ref_get(&libstrongswan_ref
);
137 if (libimcv_ref
== 0)
141 /* initialize the PA-TNC attribute manager */
142 imcv_pa_tnc_attributes
= pa_tnc_attr_manager_create();
143 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_IETF
,
144 ietf_attr_create_from_data
, ietf_attr_names
);
145 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_ITA
,
146 ita_attr_create_from_data
, ita_attr_names
);
148 /* attach global IMV database */
151 uri
= lib
->settings
->get_str(lib
->settings
,
152 "libimcv.database", NULL
);
153 script
= lib
->settings
->get_str(lib
->settings
,
154 "libimcv.policy_script", IMCV_DEFAULT_POLICY_SCRIPT
);
157 imcv_db
= imv_database_create(uri
, script
);
160 DBG1(DBG_LIB
, "libimcv initialized");
162 ref_get(&libimcv_ref
);
168 * Described in header.
170 void libimcv_deinit(void)
172 if (ref_put(&libimcv_ref
))
174 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_IETF
);
175 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_ITA
);
176 DESTROY_IF(imcv_pa_tnc_attributes
);
177 imcv_pa_tnc_attributes
= NULL
;
179 DBG1(DBG_LIB
, "libimcv terminated");
181 if (ref_put(&libstrongswan_ref
))