transmit Product Vendor ID if known
[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 <collections/linked_list.h>
38 #include <utils/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 pen_t vendor_id;
142
143 attr_cast = (ietf_attr_product_info_t*)attr;
144 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
145 if (vendor_id != PEN_IETF)
146 {
147 DBG1(DBG_IMV, "operating system name is '%.*s' "
148 "from vendor %N", os_name.len, os_name.ptr,
149 pen_names, vendor_id);
150 }
151 else
152 {
153 DBG1(DBG_IMV, "operating system name is '%.*s'",
154 os_name.len, os_name.ptr);
155 }
156 break;
157 }
158 case IETF_ATTR_STRING_VERSION:
159 {
160 ietf_attr_string_version_t *attr_cast;
161
162 attr_cast = (ietf_attr_string_version_t*)attr;
163 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
164 if (os_version.len)
165 {
166 DBG1(DBG_IMV, "operating system version is '%.*s'",
167 os_version.len, os_version.ptr);
168 }
169 break;
170 }
171 case IETF_ATTR_NUMERIC_VERSION:
172 {
173 ietf_attr_numeric_version_t *attr_cast;
174 u_int32_t major, minor;
175
176 attr_cast = (ietf_attr_numeric_version_t*)attr;
177 attr_cast->get_version(attr_cast, &major, &minor);
178 DBG1(DBG_IMV, "operating system numeric version is %d.%d",
179 major, minor);
180 break;
181 }
182 case IETF_ATTR_OPERATIONAL_STATUS:
183 {
184 ietf_attr_op_status_t *attr_cast;
185 op_status_t op_status;
186 op_result_t op_result;
187 time_t last_boot;
188
189 attr_cast = (ietf_attr_op_status_t*)attr;
190 op_status = attr_cast->get_status(attr_cast);
191 op_result = attr_cast->get_result(attr_cast);
192 last_boot = attr_cast->get_last_use(attr_cast);
193 DBG1(DBG_IMV, "operational status: %N, result: %N",
194 op_status_names, op_status, op_result_names, op_result);
195 DBG1(DBG_IMV, "last boot: %T", &last_boot, TRUE);
196 break;
197 }
198 case IETF_ATTR_FORWARDING_ENABLED:
199 {
200 ietf_attr_fwd_enabled_t *attr_cast;
201 os_fwd_status_t fwd_status;
202
203 attr_cast = (ietf_attr_fwd_enabled_t*)attr;
204 fwd_status = attr_cast->get_status(attr_cast);
205 DBG1(DBG_IMV, "IPv4 forwarding status: %N",
206 os_fwd_status_names, fwd_status);
207 break;
208 }
209 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
210 {
211 ietf_attr_default_pwd_enabled_t *attr_cast;
212 bool default_pwd_status;
213
214 attr_cast = (ietf_attr_default_pwd_enabled_t*)attr;
215 default_pwd_status = attr_cast->get_status(attr_cast);
216 DBG1(DBG_IMV, "factory default password: %sabled",
217 default_pwd_status ? "en":"dis");
218 break;
219 }
220 case IETF_ATTR_INSTALLED_PACKAGES:
221 {
222 ietf_attr_installed_packages_t *attr_cast;
223 enumerator_t *e;
224 chunk_t name, version;
225
226 attr_cast = (ietf_attr_installed_packages_t*)attr;
227 e = attr_cast->create_enumerator(attr_cast);
228 while (e->enumerate(e, &name, &version))
229 {
230 DBG1(DBG_IMV, "package '%.*s' %.*s", name.len, name.ptr,
231 version.len, version.ptr);
232 }
233 e->destroy(e);
234
235 state->set_recommendation(state,
236 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
237 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
238 assessment = TRUE;
239 break;
240 }
241 default:
242 break;
243 }
244 }
245 enumerator->destroy(enumerator);
246
247 if (os_name.len && os_version.len)
248 {
249 char *product_info;
250 char *uri = "http://remediation.strongswan.org/fix-it/";
251 char *string = "use a Linux operating system instead of Windows 1.2.3";
252 char *lang_code = "en";
253
254 os_state = (imv_os_state_t*)state;
255 os_state->set_info(os_state, os_name, os_version);
256 product_info = os_state->get_info(os_state);
257
258 if (streq(product_info, "Windows 1.2.3"))
259 {
260 DBG1(DBG_IMV, "OS '%s' is not supported", product_info);
261
262 attr = ietf_attr_remediation_instr_create_from_string(
263 chunk_create(string, strlen(string)),
264 chunk_create(lang_code, strlen(lang_code)));
265 out_msg->add_attribute(out_msg, attr);
266 attr = ietf_attr_remediation_instr_create_from_uri(
267 chunk_create(uri, strlen(uri)));
268 out_msg->add_attribute(out_msg, attr);
269
270 state->set_recommendation(state,
271 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
272 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
273 assessment = TRUE;
274 }
275 else
276 {
277 DBG1(DBG_IMV, "requesting installed packages for '%s'",
278 product_info);
279 attr = ietf_attr_attr_request_create(PEN_IETF,
280 IETF_ATTR_INSTALLED_PACKAGES);
281 out_msg->add_attribute(out_msg, attr);
282 }
283 }
284
285 if (fatal_error)
286 {
287 state->set_recommendation(state,
288 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
289 TNC_IMV_EVALUATION_RESULT_ERROR);
290 assessment = TRUE;
291 }
292
293 if (assessment)
294 {
295 result = out_msg->send_assessment(out_msg);
296 out_msg->destroy(out_msg);
297 if (result != TNC_RESULT_SUCCESS)
298 {
299 return result;
300 }
301 return imv_os->provide_recommendation(imv_os, state);
302 }
303
304 /* send PA-TNC message with excl flag set */
305 result = out_msg->send(out_msg, TRUE);
306 out_msg->destroy(out_msg);
307
308 return result;
309 }
310
311 /**
312 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
313 */
314 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
315 TNC_ConnectionID connection_id,
316 TNC_BufferReference msg,
317 TNC_UInt32 msg_len,
318 TNC_MessageType msg_type)
319 {
320 imv_state_t *state;
321 imv_msg_t *in_msg;
322 TNC_Result result;
323
324 if (!imv_os)
325 {
326 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
327 return TNC_RESULT_NOT_INITIALIZED;
328 }
329 if (!imv_os->get_state(imv_os, connection_id, &state))
330 {
331 return TNC_RESULT_FATAL;
332 }
333 in_msg = imv_msg_create_from_data(imv_os, state, connection_id, msg_type,
334 chunk_create(msg, msg_len));
335 result = receive_message(state, in_msg);
336 in_msg->destroy(in_msg);
337
338 return result;
339 }
340
341 /**
342 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
343 */
344 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
345 TNC_ConnectionID connection_id,
346 TNC_UInt32 msg_flags,
347 TNC_BufferReference msg,
348 TNC_UInt32 msg_len,
349 TNC_VendorID msg_vid,
350 TNC_MessageSubtype msg_subtype,
351 TNC_UInt32 src_imc_id,
352 TNC_UInt32 dst_imv_id)
353 {
354 imv_state_t *state;
355 imv_msg_t *in_msg;
356 TNC_Result result;
357
358 if (!imv_os)
359 {
360 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
361 return TNC_RESULT_NOT_INITIALIZED;
362 }
363 if (!imv_os->get_state(imv_os, connection_id, &state))
364 {
365 return TNC_RESULT_FATAL;
366 }
367 in_msg = imv_msg_create_from_long_data(imv_os, state, connection_id,
368 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
369 chunk_create(msg, msg_len));
370 result =receive_message(state, in_msg);
371 in_msg->destroy(in_msg);
372
373 return result;
374 }
375
376 /**
377 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
378 */
379 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
380 TNC_ConnectionID connection_id)
381 {
382 imv_state_t *state;
383
384 if (!imv_os)
385 {
386 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
387 return TNC_RESULT_NOT_INITIALIZED;
388 }
389 if (!imv_os->get_state(imv_os, connection_id, &state))
390 {
391 return TNC_RESULT_FATAL;
392 }
393 return imv_os->provide_recommendation(imv_os, state);
394 }
395
396 /**
397 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
398 */
399 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
400 TNC_ConnectionID connection_id)
401 {
402 imv_state_t *state;
403 imv_os_state_t *os_state;
404 TNC_Result result = TNC_RESULT_SUCCESS;
405
406 if (!imv_os)
407 {
408 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
409 return TNC_RESULT_NOT_INITIALIZED;
410 }
411 if (!imv_os->get_state(imv_os, connection_id, &state))
412 {
413 return TNC_RESULT_FATAL;
414 }
415 os_state = (imv_os_state_t*)state;
416
417 if (os_state->get_info(os_state) == NULL)
418 {
419 imv_msg_t *out_msg;
420 pa_tnc_attr_t *attr;
421 ietf_attr_attr_request_t *attr_cast;
422
423 out_msg = imv_msg_create(imv_os, state, connection_id, imv_id,
424 TNC_IMCID_ANY, msg_types[0]);
425 attr = ietf_attr_attr_request_create(PEN_IETF,
426 IETF_ATTR_PRODUCT_INFORMATION);
427 attr_cast = (ietf_attr_attr_request_t*)attr;
428 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
429 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_NUMERIC_VERSION);
430 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS);
431 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FORWARDING_ENABLED);
432 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED);
433 out_msg->add_attribute(out_msg, attr);
434
435 /* send PA-TNC message with excl flag not set */
436 result = out_msg->send(out_msg, FALSE);
437 out_msg->destroy(out_msg);
438 }
439
440 return result;
441 }
442
443 /**
444 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
445 */
446 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
447 {
448 if (!imv_os)
449 {
450 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
451 return TNC_RESULT_NOT_INITIALIZED;
452 }
453 imv_os->destroy(imv_os);
454 imv_os = NULL;
455
456 return TNC_RESULT_SUCCESS;
457 }
458
459 /**
460 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
461 */
462 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
463 TNC_TNCS_BindFunctionPointer bind_function)
464 {
465 if (!imv_os)
466 {
467 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
468 return TNC_RESULT_NOT_INITIALIZED;
469 }
470 return imv_os->bind_functions(imv_os, bind_function);
471 }