refactored PA-TNC message handling by IMVs
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * 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 "imv_os_state.h"
17
18 #include <imv/imv_agent.h>
19 #include <imv/imv_msg.h>
20 #include <pa_tnc/pa_tnc_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_attr_request.h>
23 #include <ietf/ietf_attr_default_pwd_enabled.h>
24 #include <ietf/ietf_attr_fwd_enabled.h>
25 #include <ietf/ietf_attr_installed_packages.h>
26 #include <ietf/ietf_attr_op_status.h>
27 #include <ietf/ietf_attr_pa_tnc_error.h>
28 #include <ietf/ietf_attr_product_info.h>
29 #include <ietf/ietf_attr_string_version.h>
30 #include <os_info/os_info.h>
31
32 #include <tncif_names.h>
33 #include <tncif_pa_subtypes.h>
34
35 #include <pen/pen.h>
36 #include <utils/linked_list.h>
37 #include <debug.h>
38
39 /* IMV definitions */
40
41 static const char imv_name[] = "OS";
42
43 static pen_type_t msg_types[] = {
44 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
45 };
46
47 static imv_agent_t *imv_os;
48
49 /*
50 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
51 */
52 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
53 TNC_Version min_version,
54 TNC_Version max_version,
55 TNC_Version *actual_version)
56 {
57 if (imv_os)
58 {
59 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
60 return TNC_RESULT_ALREADY_INITIALIZED;
61 }
62 imv_os = imv_agent_create(imv_name, msg_types, 1, imv_id, actual_version);
63 if (!imv_os)
64 {
65 return TNC_RESULT_FATAL;
66 }
67 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
68 {
69 DBG1(DBG_IMV, "no common IF-IMV version");
70 return TNC_RESULT_NO_COMMON_VERSION;
71 }
72
73 return TNC_RESULT_SUCCESS;
74 }
75
76 /**
77 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
78 */
79 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
80 TNC_ConnectionID connection_id,
81 TNC_ConnectionState new_state)
82 {
83 imv_state_t *state;
84
85 if (!imv_os)
86 {
87 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
88 return TNC_RESULT_NOT_INITIALIZED;
89 }
90 switch (new_state)
91 {
92 case TNC_CONNECTION_STATE_CREATE:
93 state = imv_os_state_create(connection_id);
94 return imv_os->create_state(imv_os, state);
95 case TNC_CONNECTION_STATE_DELETE:
96 return imv_os->delete_state(imv_os, connection_id);
97 default:
98 return imv_os->change_state(imv_os, connection_id,
99 new_state, NULL);
100 }
101 }
102
103 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
104 {
105 imv_msg_t *out_msg;
106 imv_os_state_t *os_state;
107 enumerator_t *enumerator;
108 pa_tnc_attr_t *attr;
109 pen_type_t type;
110 TNC_Result result;
111 chunk_t os_name = chunk_empty;
112 chunk_t os_version = chunk_empty;
113 bool fatal_error = FALSE, assessment = FALSE;
114
115
116 /* parse received PA-TNC message and handle local and remote errors */
117 result = in_msg->receive(in_msg, &fatal_error);
118 if (result != TNC_RESULT_SUCCESS)
119 {
120 return result;
121 }
122
123 out_msg = imv_msg_create_as_reply(in_msg);
124
125 /* analyze PA-TNC attributes */
126 enumerator = in_msg->create_attribute_enumerator(in_msg);
127 while (enumerator->enumerate(enumerator, &attr))
128 {
129 type = attr->get_type(attr);
130
131 if (type.vendor_id != PEN_IETF)
132 {
133 continue;
134 }
135 switch (type.type)
136 {
137 case IETF_ATTR_PRODUCT_INFORMATION:
138 {
139 ietf_attr_product_info_t *attr_cast;
140
141 attr_cast = (ietf_attr_product_info_t*)attr;
142 os_name = attr_cast->get_info(attr_cast, NULL, NULL);
143 DBG1(DBG_IMV, "operating system name is '%.*s'",
144 os_name.len, os_name.ptr);
145 break;
146 }
147 case IETF_ATTR_STRING_VERSION:
148 {
149 ietf_attr_string_version_t *attr_cast;
150
151 attr_cast = (ietf_attr_string_version_t*)attr;
152 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
153 if (os_version.len)
154 {
155 DBG1(DBG_IMV, "operating system version is '%.*s'",
156 os_version.len, os_version.ptr);
157 }
158 break;
159 }
160 case IETF_ATTR_OPERATIONAL_STATUS:
161 {
162 ietf_attr_op_status_t *attr_cast;
163 op_status_t op_status;
164 op_result_t op_result;
165 time_t last_boot;
166
167 attr_cast = (ietf_attr_op_status_t*)attr;
168 op_status = attr_cast->get_status(attr_cast);
169 op_result = attr_cast->get_result(attr_cast);
170 last_boot = attr_cast->get_last_use(attr_cast);
171 DBG1(DBG_IMV, "operational status: %N, result: %N",
172 op_status_names, op_status, op_result_names, op_result);
173 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
174 break;
175 }
176 case IETF_ATTR_FORWARDING_ENABLED:
177 {
178 ietf_attr_fwd_enabled_t *attr_cast;
179 os_fwd_status_t fwd_status;
180
181 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
182 fwd_status = attr_cast->get_status(attr_cast);
183 DBG1(DBG_IMV, "IPv4 forwarding status: %N",
184 os_fwd_status_names, fwd_status);
185 break;
186 }
187 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
188 {
189 ietf_attr_default_pwd_enabled_t *attr_cast;
190 bool default_pwd_status;
191
192 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
193 default_pwd_status = attr_cast->get_status(attr_cast);
194 DBG1(DBG_IMV, "factory default password: %sabled",
195 default_pwd_status ? "en":"dis");
196 break;
197 }
198 case IETF_ATTR_INSTALLED_PACKAGES:
199 {
200 ietf_attr_installed_packages_t *attr_cast;
201 enumerator_t *e;
202 chunk_t name, version;
203
204 attr_cast = (ietf_attr_installed_packages_t*)attr;
205 e = attr_cast->create_enumerator(attr_cast);
206 while (e->enumerate(e, &name, &version))
207 {
208 DBG1(DBG_IMV, "package '%.*s' %.*s", name.len, name.ptr,
209 version.len, version.ptr);
210 }
211 e->destroy(e);
212
213 state->set_recommendation(state,
214 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
215 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
216 assessment = TRUE;
217 break;
218 }
219 default:
220 break;
221 }
222 }
223 enumerator->destroy(enumerator);
224
225 if (os_name.len && os_version.len)
226 {
227 char *product_info;
228
229 os_state = (imv_os_state_t*)state;
230 os_state->set_info(os_state, os_name, os_version);
231 product_info = os_state->get_info(os_state);
232
233 if (streq(product_info, "Windows 1.2.3"))
234 {
235 DBG1(DBG_IMV, "OS '%s' is not supported", product_info);
236
237 state->set_recommendation(state,
238 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
239 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
240 assessment = TRUE;
241 }
242 else
243 {
244 DBG1(DBG_IMV, "requesting installed packages for '%s'",
245 product_info);
246 attr = ietf_attr_attr_request_create(PEN_IETF,
247 IETF_ATTR_INSTALLED_PACKAGES);
248 out_msg->add_attribute(out_msg, attr);
249 }
250 }
251
252 if (fatal_error)
253 {
254 state->set_recommendation(state,
255 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
256 TNC_IMV_EVALUATION_RESULT_ERROR);
257 assessment = TRUE;
258 }
259
260 if (assessment)
261 {
262 result = out_msg->send_assessment(out_msg);
263 out_msg->destroy(out_msg);
264 if (result != TNC_RESULT_SUCCESS)
265 {
266 return result;
267 }
268 return imv_os->provide_recommendation(imv_os, state);
269 }
270
271 /* send PA-TNC message with excl flag set */
272 result = out_msg->send(out_msg, TRUE);
273 out_msg->destroy(out_msg);
274
275 return result;
276 }
277
278 /**
279 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
280 */
281 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
282 TNC_ConnectionID connection_id,
283 TNC_BufferReference msg,
284 TNC_UInt32 msg_len,
285 TNC_MessageType msg_type)
286 {
287 imv_state_t *state;
288 imv_msg_t *in_msg;
289 TNC_Result result;
290
291 if (!imv_os)
292 {
293 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
294 return TNC_RESULT_NOT_INITIALIZED;
295 }
296 if (!imv_os->get_state(imv_os, connection_id, &state))
297 {
298 return TNC_RESULT_FATAL;
299 }
300 in_msg = imv_msg_create_from_data(imv_os, state, connection_id, msg_type,
301 chunk_create(msg, msg_len));
302 result = receive_message(state, in_msg);
303 in_msg->destroy(in_msg);
304
305 return result;
306 }
307
308 /**
309 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
310 */
311 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
312 TNC_ConnectionID connection_id,
313 TNC_UInt32 msg_flags,
314 TNC_BufferReference msg,
315 TNC_UInt32 msg_len,
316 TNC_VendorID msg_vid,
317 TNC_MessageSubtype msg_subtype,
318 TNC_UInt32 src_imc_id,
319 TNC_UInt32 dst_imv_id)
320 {
321 imv_state_t *state;
322 imv_msg_t *in_msg;
323 TNC_Result result;
324
325 if (!imv_os)
326 {
327 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
328 return TNC_RESULT_NOT_INITIALIZED;
329 }
330 if (!imv_os->get_state(imv_os, connection_id, &state))
331 {
332 return TNC_RESULT_FATAL;
333 }
334 in_msg = imv_msg_create_from_long_data(imv_os, state, connection_id,
335 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
336 chunk_create(msg, msg_len));
337 result =receive_message(state, in_msg);
338 in_msg->destroy(in_msg);
339
340 return result;
341 }
342
343 /**
344 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
345 */
346 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
347 TNC_ConnectionID connection_id)
348 {
349 imv_state_t *state;
350
351 if (!imv_os)
352 {
353 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
354 return TNC_RESULT_NOT_INITIALIZED;
355 }
356 if (!imv_os->get_state(imv_os, connection_id, &state))
357 {
358 return TNC_RESULT_FATAL;
359 }
360 return imv_os->provide_recommendation(imv_os, state);
361 }
362
363 /**
364 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
365 */
366 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
367 TNC_ConnectionID connection_id)
368 {
369 imv_state_t *state;
370 imv_os_state_t *os_state;
371 TNC_Result result = TNC_RESULT_SUCCESS;
372
373 if (!imv_os)
374 {
375 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
376 return TNC_RESULT_NOT_INITIALIZED;
377 }
378 if (!imv_os->get_state(imv_os, connection_id, &state))
379 {
380 return TNC_RESULT_FATAL;
381 }
382 os_state = (imv_os_state_t*)state;
383
384 if (os_state->get_info(os_state) == NULL)
385 {
386 imv_msg_t *out_msg;
387 pa_tnc_attr_t *attr;
388 ietf_attr_attr_request_t *attr_cast;
389
390 out_msg = imv_msg_create(imv_os, state, connection_id, imv_id,
391 TNC_IMCID_ANY, msg_types[0]);
392 attr = ietf_attr_attr_request_create(PEN_IETF,
393 IETF_ATTR_PRODUCT_INFORMATION);
394 attr_cast = (ietf_attr_attr_request_t*)attr;
395 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
396 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
397 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
398 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
399 out_msg->add_attribute(out_msg, attr);
400
401 /* send PA-TNC message with excl flag not set */
402 result = out_msg->send(out_msg, FALSE);
403 out_msg->destroy(out_msg);
404 }
405
406 return result;
407 }
408
409 /**
410 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
411 */
412 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
413 {
414 if (!imv_os)
415 {
416 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
417 return TNC_RESULT_NOT_INITIALIZED;
418 }
419 imv_os->destroy(imv_os);
420 imv_os = NULL;
421
422 return TNC_RESULT_SUCCESS;
423 }
424
425 /**
426 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
427 */
428 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
429 TNC_TNCS_BindFunctionPointer bind_function)
430 {
431 if (!imv_os)
432 {
433 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
434 return TNC_RESULT_NOT_INITIALIZED;
435 }
436 return imv_os->bind_functions(imv_os, bind_function);
437 }