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"
25 #define IMCV_DEBUG_LEVEL 1
28 * PA-TNC attribute manager
30 pa_tnc_attr_manager_t
*imcv_pa_tnc_attributes
;
33 * Reference count for libimcv
35 static refcount_t libimcv_ref
= 0;
38 * Reference count for libstrongswan
40 static refcount_t libstrongswan_ref
= 0;
43 * Global configuration of imcv dbg function
45 static int imcv_debug_level
;
46 static bool imcv_stderr_quiet
;
51 static void imcv_dbg(debug_t group
, level_t level
, char *fmt
, ...)
53 int priority
= LOG_INFO
;
55 char *current
= buffer
, *next
;
58 if (level
<= imcv_debug_level
)
60 if (!imcv_stderr_quiet
)
63 fprintf(stderr
, "[HSR] ");
64 vfprintf(stderr
, fmt
, args
);
65 fprintf(stderr
, "\n");
69 /* write in memory buffer first */
71 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
74 /* do a syslog with every line */
77 next
= strchr(current
, '\n');
82 syslog(priority
, "[HSR] %s\n", current
);
89 * Described in header.
91 bool libimcv_init(void)
93 /* initialize libstrongswan library only once */
96 /* did main program initialize libstrongswan? */
97 if (libstrongswan_ref
== 0)
99 ref_get(&libstrongswan_ref
);
104 /* we are the first to initialize libstrongswan */
105 if (!library_init(NULL
))
110 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
111 "sha1 sha2 random gmp pubkey x509"))
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 ref_get(&libstrongswan_ref
);
129 if (libimcv_ref
== 0)
131 /* initialize the PA-TNC attribute manager */
132 imcv_pa_tnc_attributes
= pa_tnc_attr_manager_create();
133 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_IETF
,
134 ietf_attr_create_from_data
, ietf_attr_names
);
135 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_ITA
,
136 ita_attr_create_from_data
, ita_attr_names
);
137 DBG1(DBG_LIB
, "libimcv initialized");
139 ref_get(&libimcv_ref
);
145 * Described in header.
147 void libimcv_deinit(void)
149 if (ref_put(&libimcv_ref
))
151 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_IETF
);
152 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_ITA
);
153 DESTROY_IF(imcv_pa_tnc_attributes
);
154 DBG1(DBG_LIB
, "libimcv terminated");
156 if (ref_put(&libstrongswan_ref
))