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