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