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