527879e113c552a2ee1e12aab3c8774b72a947b9
[strongswan.git] / src / libcharon / plugins / tnc_imv / tnc_imv_manager.c
1 /*
2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "tnc_imv_manager.h"
17 #include "tnc_imv_recommendations.h"
18
19 #include <tnc/imv/imv_manager.h>
20 #include <tnc/tncifimv.h>
21 #include <tnc/tncifimv_names.h>
22
23 #include <debug.h>
24 #include <daemon.h>
25 #include <threading/mutex.h>
26
27 typedef struct private_tnc_imv_manager_t private_tnc_imv_manager_t;
28
29
30 /**
31 * Private data of an imv_manager_t object.
32 */
33 struct private_tnc_imv_manager_t {
34
35 /**
36 * Public members of imv_manager_t.
37 */
38 imv_manager_t public;
39
40 /**
41 * Linked list of IMVs
42 */
43 linked_list_t *imvs;
44
45 /**
46 * Next IMV ID to be assigned
47 */
48 TNC_IMVID next_imv_id;
49 };
50
51 METHOD(imv_manager_t, add, bool,
52 private_tnc_imv_manager_t *this, imv_t *imv)
53 {
54 TNC_Version version;
55
56 /* Initialize the IMV module */
57 imv->set_id(imv, this->next_imv_id);
58 if (imv->initialize(imv->get_id(imv), TNC_IFIMV_VERSION_1,
59 TNC_IFIMV_VERSION_1, &version) != TNC_RESULT_SUCCESS)
60 {
61 DBG1(DBG_TNC, "IMV \"%s\" failed to initialize", imv->get_name(imv));
62 return FALSE;
63 }
64 this->imvs->insert_last(this->imvs, imv);
65 this->next_imv_id++;
66
67 if (imv->provide_bind_function(imv->get_id(imv), TNC_TNCS_BindFunction)
68 != TNC_RESULT_SUCCESS)
69 {
70 DBG1(DBG_TNC, "IMV \"%s\" could failed to obtain bind function",
71 imv->get_name(imv));
72 this->imvs->remove_last(this->imvs, (void**)&imv);
73 return FALSE;
74 }
75
76 return TRUE;
77 }
78
79 METHOD(imv_manager_t, remove_, imv_t*,
80 private_tnc_imv_manager_t *this, TNC_IMVID id)
81 {
82 enumerator_t *enumerator;
83 imv_t *imv;
84
85 enumerator = this->imvs->create_enumerator(this->imvs);
86 while (enumerator->enumerate(enumerator, &imv))
87 {
88 if (id == imv->get_id(imv))
89 {
90 this->imvs->remove_at(this->imvs, enumerator);
91 return imv;
92 }
93 }
94 enumerator->destroy(enumerator);
95 return NULL;
96 }
97
98 METHOD(imv_manager_t, create_recommendations, recommendations_t*,
99 private_tnc_imv_manager_t *this)
100 {
101 return tnc_imv_recommendations_create(this->imvs);
102 }
103
104 METHOD(imv_manager_t, enforce_recommendation, bool,
105 private_tnc_imv_manager_t *this, TNC_IMV_Action_Recommendation rec)
106 {
107 char *group;
108 identification_t *id;
109 ike_sa_t *ike_sa;
110 auth_cfg_t *auth;
111
112 switch (rec)
113 {
114 case TNC_IMV_ACTION_RECOMMENDATION_ALLOW:
115 DBG1(DBG_TNC, "TNC recommendation is allow");
116 group = "allow";
117 break;
118 case TNC_IMV_ACTION_RECOMMENDATION_ISOLATE:
119 DBG1(DBG_TNC, "TNC recommendation is isolate");
120 group = "isolate";
121 break;
122 case TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS:
123 case TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION:
124 default:
125 DBG1(DBG_TNC, "TNC recommendation is none");
126 return FALSE;
127 }
128 ike_sa = charon->bus->get_sa(charon->bus);
129 if (ike_sa)
130 {
131 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
132 id = identification_create_from_string(group);
133 auth->add(auth, AUTH_RULE_GROUP, id);
134 DBG1(DBG_TNC, "TNC added group membership '%s'", group);
135 }
136 return TRUE;
137 }
138
139
140 METHOD(imv_manager_t, notify_connection_change, void,
141 private_tnc_imv_manager_t *this, TNC_ConnectionID id,
142 TNC_ConnectionState state)
143 {
144 enumerator_t *enumerator;
145 imv_t *imv;
146
147 enumerator = this->imvs->create_enumerator(this->imvs);
148 while (enumerator->enumerate(enumerator, &imv))
149 {
150 if (imv->notify_connection_change)
151 {
152 imv->notify_connection_change(imv->get_id(imv), id, state);
153 }
154 }
155 enumerator->destroy(enumerator);
156 }
157
158 METHOD(imv_manager_t, set_message_types, TNC_Result,
159 private_tnc_imv_manager_t *this, TNC_IMVID id,
160 TNC_MessageTypeList supported_types,
161 TNC_UInt32 type_count)
162 {
163 enumerator_t *enumerator;
164 imv_t *imv;
165 TNC_Result result = TNC_RESULT_FATAL;
166
167 enumerator = this->imvs->create_enumerator(this->imvs);
168 while (enumerator->enumerate(enumerator, &imv))
169 {
170 if (id == imv->get_id(imv))
171 {
172 imv->set_message_types(imv, supported_types, type_count);
173 result = TNC_RESULT_SUCCESS;
174 break;
175 }
176 }
177 enumerator->destroy(enumerator);
178 return result;
179 }
180
181 METHOD(imv_manager_t, solicit_recommendation, void,
182 private_tnc_imv_manager_t *this, TNC_ConnectionID id)
183 {
184 enumerator_t *enumerator;
185 imv_t *imv;
186
187 enumerator = this->imvs->create_enumerator(this->imvs);
188 while (enumerator->enumerate(enumerator, &imv))
189 {
190 imv->solicit_recommendation(imv->get_id(imv), id);
191 }
192 enumerator->destroy(enumerator);
193 }
194
195 METHOD(imv_manager_t, receive_message, void,
196 private_tnc_imv_manager_t *this, TNC_ConnectionID connection_id,
197 TNC_BufferReference message,
198 TNC_UInt32 message_len,
199 TNC_MessageType message_type)
200 {
201 enumerator_t *enumerator;
202 imv_t *imv;
203
204 enumerator = this->imvs->create_enumerator(this->imvs);
205 while (enumerator->enumerate(enumerator, &imv))
206 {
207 if (imv->receive_message && imv->type_supported(imv, message_type))
208 {
209 imv->receive_message(imv->get_id(imv), connection_id,
210 message, message_len, message_type);
211 }
212 }
213 enumerator->destroy(enumerator);
214 }
215
216 METHOD(imv_manager_t, batch_ending, void,
217 private_tnc_imv_manager_t *this, TNC_ConnectionID id)
218 {
219 enumerator_t *enumerator;
220 imv_t *imv;
221
222 enumerator = this->imvs->create_enumerator(this->imvs);
223 while (enumerator->enumerate(enumerator, &imv))
224 {
225 if (imv->batch_ending)
226 {
227 imv->batch_ending(imv->get_id(imv), id);
228 }
229 }
230 enumerator->destroy(enumerator);
231 }
232
233 METHOD(imv_manager_t, destroy, void,
234 private_tnc_imv_manager_t *this)
235 {
236 imv_t *imv;
237
238 while (this->imvs->remove_last(this->imvs, (void**)&imv) == SUCCESS)
239 {
240 if (imv->terminate &&
241 imv->terminate(imv->get_id(imv)) != TNC_RESULT_SUCCESS)
242 {
243 DBG1(DBG_TNC, "IMV \"%s\" not terminated successfully",
244 imv->get_name(imv));
245 }
246 imv->destroy(imv);
247 }
248 this->imvs->destroy(this->imvs);
249 free(this);
250 }
251
252 /**
253 * Described in header.
254 */
255 imv_manager_t* tnc_imv_manager_create(void)
256 {
257 private_tnc_imv_manager_t *this;
258
259 INIT(this,
260 .public = {
261 .add = _add,
262 .remove = _remove_, /* avoid name conflict with stdio.h */
263 .create_recommendations = _create_recommendations,
264 .enforce_recommendation = _enforce_recommendation,
265 .notify_connection_change = _notify_connection_change,
266 .set_message_types = _set_message_types,
267 .solicit_recommendation = _solicit_recommendation,
268 .receive_message = _receive_message,
269 .batch_ending = _batch_ending,
270 .destroy = _destroy,
271 },
272 .imvs = linked_list_create(),
273 .next_imv_id = 1,
274 );
275
276 return &this->public;
277 }