read PDP server name from strongswan.conf
[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.h>
20 #include <debug.h>
21 #include <pen/pen.h>
22
23 #include <syslog.h>
24
25 #define IMCV_DEBUG_LEVEL 1
26
27 /**
28 * PA-TNC attribute manager
29 */
30 pa_tnc_attr_manager_t *imcv_pa_tnc_attributes;
31
32 /**
33 * Reference count for libimcv
34 */
35 static refcount_t libimcv_ref = 0;
36
37 /**
38 * Reference count for libstrongswan
39 */
40 static refcount_t libstrongswan_ref = 0;
41
42 /**
43 * Global configuration of imcv dbg function
44 */
45 static int imcv_debug_level;
46 static bool imcv_stderr_quiet;
47
48 /**
49 * imvc dbg function
50 */
51 static void imcv_dbg(debug_t group, level_t level, char *fmt, ...)
52 {
53 int priority = LOG_INFO;
54 char buffer[8192];
55 char *current = buffer, *next;
56 va_list args;
57
58 if (level <= imcv_debug_level)
59 {
60 if (!imcv_stderr_quiet)
61 {
62 va_start(args, fmt);
63 fprintf(stderr, "[HSR] ");
64 vfprintf(stderr, fmt, args);
65 fprintf(stderr, "\n");
66 va_end(args);
67 }
68
69 /* write in memory buffer first */
70 va_start(args, fmt);
71 vsnprintf(buffer, sizeof(buffer), fmt, args);
72 va_end(args);
73
74 /* do a syslog with every line */
75 while (current)
76 {
77 next = strchr(current, '\n');
78 if (next)
79 {
80 *(next++) = '\0';
81 }
82 syslog(priority, "[HSR] %s\n", current);
83 current = next;
84 }
85 }
86 }
87
88 /**
89 * Described in header.
90 */
91 bool libimcv_init(void)
92 {
93 /* initialize libstrongswan library only once */
94 if (lib)
95 {
96 /* did main program initialize libstrongswan? */
97 if (libstrongswan_ref == 0)
98 {
99 ref_get(&libstrongswan_ref);
100 }
101 }
102 else
103 {
104 /* we are the first to initialize libstrongswan */
105 if (!library_init(NULL))
106 {
107 return FALSE;
108 }
109
110 if (!lib->plugins->load(lib->plugins, NULL,
111 "sha1 sha2 random gmp pubkey x509"))
112 {
113 library_deinit();
114 return FALSE;
115 }
116
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);
122
123 /* activate the imcv debugging hook */
124 dbg = imcv_dbg;
125 openlog("imcv", 0, LOG_DAEMON);
126 }
127 ref_get(&libstrongswan_ref);
128
129 if (libimcv_ref == 0)
130 {
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");
138 }
139 ref_get(&libimcv_ref);
140
141 return TRUE;
142 }
143
144 /**
145 * Described in header.
146 */
147 void libimcv_deinit(void)
148 {
149 if (ref_put(&libimcv_ref))
150 {
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");
155 }
156 if (ref_put(&libstrongswan_ref))
157 {
158 library_deinit();
159 }
160 }
161