f4fffa0fdb139813a41f3e30213ad5d677ab9406
[strongswan.git] / src / libimcv / plugins / imc_os / imc_os.c
1 /*
2 * Copyright (C) 2011-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 "imc_os_state.h"
17
18 #include <imc/imc_agent.h>
19 #include <imc/imc_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_product_info.h>
28 #include <ietf/ietf_attr_string_version.h>
29 #include <os_info/os_info.h>
30
31 #include <tncif_pa_subtypes.h>
32
33 #include <pen/pen.h>
34 #include <debug.h>
35
36 /* IMC definitions */
37
38 static const char imc_name[] = "OS";
39
40 static pen_type_t msg_types[] = {
41 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
42 };
43
44 static imc_agent_t *imc_os;
45 static os_info_t *os;
46
47 /**
48 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
49 */
50 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
51 TNC_Version min_version,
52 TNC_Version max_version,
53 TNC_Version *actual_version)
54 {
55 if (imc_os)
56 {
57 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
58 return TNC_RESULT_ALREADY_INITIALIZED;
59 }
60 imc_os = imc_agent_create(imc_name, msg_types, 1, imc_id, actual_version);
61 if (!imc_os)
62 {
63 return TNC_RESULT_FATAL;
64 }
65
66 os = os_info_create();
67 if (!os)
68 {
69 imc_os->destroy(imc_os);
70 imc_os = NULL;
71
72 return TNC_RESULT_FATAL;
73 }
74
75 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
76 {
77 DBG1(DBG_IMC, "no common IF-IMC version");
78 return TNC_RESULT_NO_COMMON_VERSION;
79 }
80 return TNC_RESULT_SUCCESS;
81 }
82
83 /**
84 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
85 */
86 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
87 TNC_ConnectionID connection_id,
88 TNC_ConnectionState new_state)
89 {
90 imc_state_t *state;
91
92 if (!imc_os)
93 {
94 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
95 return TNC_RESULT_NOT_INITIALIZED;
96 }
97 switch (new_state)
98 {
99 case TNC_CONNECTION_STATE_CREATE:
100 state = imc_os_state_create(connection_id);
101 return imc_os->create_state(imc_os, state);
102 case TNC_CONNECTION_STATE_HANDSHAKE:
103 if (imc_os->change_state(imc_os, connection_id, new_state,
104 &state) != TNC_RESULT_SUCCESS)
105 {
106 return TNC_RESULT_FATAL;
107 }
108 state->set_result(state, imc_id,
109 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
110 return TNC_RESULT_SUCCESS;
111 case TNC_CONNECTION_STATE_DELETE:
112 return imc_os->delete_state(imc_os, connection_id);
113 default:
114 return imc_os->change_state(imc_os, connection_id,
115 new_state, NULL);
116 }
117 }
118
119 /**
120 * Add IETF Product Information attribute to the send queue
121 */
122 static void add_product_info(imc_msg_t *msg)
123 {
124 pa_tnc_attr_t *attr;
125
126 attr = ietf_attr_product_info_create(PEN_IETF, 0, os->get_name(os));
127 msg->add_attribute(msg, attr);
128 }
129
130 /**
131 * Add IETF String Version attribute to the send queue
132 */
133 static void add_string_version(imc_msg_t *msg)
134 {
135 pa_tnc_attr_t *attr;
136
137 attr = ietf_attr_string_version_create(os->get_version(os),
138 chunk_empty, chunk_empty);
139 msg->add_attribute(msg, attr);
140 }
141
142 /**
143 * Add IETF Operational Status attribute to the send queue
144 */
145 static void add_op_status(imc_msg_t *msg)
146 {
147 pa_tnc_attr_t *attr;
148 time_t uptime, last_boot;
149
150 uptime = os->get_uptime(os);
151 last_boot = uptime ? time(NULL) - uptime : UNDEFINED_TIME;
152 if (last_boot != UNDEFINED_TIME)
153 {
154 DBG1(DBG_IMC, "last boot: %T, %u s ago", &last_boot, TRUE, uptime);
155 }
156 attr = ietf_attr_op_status_create(OP_STATUS_OPERATIONAL,
157 OP_RESULT_SUCCESSFUL, last_boot);
158 msg->add_attribute(msg, attr);
159 }
160
161 /**
162 * Add IETF Forwarding Enabled attribute to the send queue
163 */
164 static void add_fwd_enabled(imc_msg_t *msg)
165 {
166 pa_tnc_attr_t *attr;
167 os_fwd_status_t fwd_status;
168
169 fwd_status = os->get_fwd_status(os);
170 DBG1(DBG_IMC, "IPv4 forwarding status: %N",
171 os_fwd_status_names, fwd_status);
172 attr = ietf_attr_fwd_enabled_create(fwd_status);
173 msg->add_attribute(msg, attr);
174 }
175
176 /**
177 * Add IETF Factory Default Password Enabled attribute to the send queue
178 */
179 static void add_default_pwd_enabled(imc_msg_t *msg)
180 {
181 pa_tnc_attr_t *attr;
182
183 DBG1(DBG_IMC, "factory default password: disabled");
184 attr = ietf_attr_default_pwd_enabled_create(FALSE);
185 msg->add_attribute(msg, attr);
186 }
187
188 /**
189 * Add an IETF Installed Packages attribute to the send queue
190 */
191 static void add_installed_packages(imc_msg_t *msg)
192 {
193 pa_tnc_attr_t *attr;
194 ietf_attr_installed_packages_t *attr_cast;
195 chunk_t libc_name = { "libc-bin", 8 };
196 chunk_t libc_version = { "2.15-0ubuntu10.2", 16 };
197 chunk_t selinux_name = { "selinux", 7 };
198 chunk_t selinux_version = { "1:0.11", 6 };
199
200 attr = ietf_attr_installed_packages_create();
201 attr_cast = (ietf_attr_installed_packages_t*)attr;
202 attr_cast->add(attr_cast, libc_name, libc_version);
203 attr_cast->add(attr_cast, selinux_name, selinux_version);
204 msg->add_attribute(msg, attr);
205 }
206
207 /**
208 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
209 */
210 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
211 TNC_ConnectionID connection_id)
212 {
213 imc_state_t *state;
214 imc_msg_t *out_msg;
215 TNC_Result result = TNC_RESULT_SUCCESS;
216
217 if (!imc_os)
218 {
219 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
220 return TNC_RESULT_NOT_INITIALIZED;
221 }
222 if (!imc_os->get_state(imc_os, connection_id, &state))
223 {
224 return TNC_RESULT_FATAL;
225 }
226 if (lib->settings->get_bool(lib->settings,
227 "libimcv.plugins.imc-os.send_info", TRUE))
228 {
229 out_msg = imc_msg_create(imc_os, state, connection_id, imc_id,
230 TNC_IMVID_ANY, msg_types[0]);
231 add_product_info(out_msg);
232 add_string_version(out_msg);
233 add_op_status(out_msg);
234 add_fwd_enabled(out_msg);
235 add_default_pwd_enabled(out_msg);
236
237 /* send PA-TNC message with the excl flag not set */
238 result = out_msg->send(out_msg, FALSE);
239 out_msg->destroy(out_msg);
240 }
241
242 return result;
243 }
244
245 static TNC_Result receive_message(imc_msg_t *in_msg)
246 {
247 imc_msg_t *out_msg;
248 enumerator_t *enumerator;
249 pa_tnc_attr_t *attr;
250 pen_type_t attr_type;
251 TNC_Result result;
252 bool fatal_error = FALSE;
253
254 /* parse received PA-TNC message and handle local and remote errors */
255 result = in_msg->receive(in_msg, &fatal_error);
256 if (result != TNC_RESULT_SUCCESS)
257 {
258 return result;
259 }
260 out_msg = imc_msg_create_as_reply(in_msg);
261
262 /* analyze PA-TNC attributes */
263 enumerator = in_msg->create_attribute_enumerator(in_msg);
264 while (enumerator->enumerate(enumerator, &attr))
265 {
266 attr_type = attr->get_type(attr);
267
268 if (attr_type.vendor_id != PEN_IETF)
269 {
270 continue;
271 }
272 if (attr_type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
273 {
274 ietf_attr_attr_request_t *attr_cast;
275 pen_type_t *entry;
276 enumerator_t *e;
277
278 attr_cast = (ietf_attr_attr_request_t*)attr;
279
280 e = attr_cast->create_enumerator(attr_cast);
281 while (e->enumerate(e, &entry))
282 {
283 if (entry->vendor_id != PEN_IETF)
284 {
285 continue;
286 }
287 switch (entry->type)
288 {
289 case IETF_ATTR_PRODUCT_INFORMATION:
290 add_product_info(out_msg);
291 break;
292 case IETF_ATTR_STRING_VERSION:
293 add_string_version(out_msg);
294 break;
295 case IETF_ATTR_OPERATIONAL_STATUS:
296 add_op_status(out_msg);
297 break;
298 case IETF_ATTR_FORWARDING_ENABLED:
299 add_fwd_enabled(out_msg);
300 break;
301 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
302 add_default_pwd_enabled(out_msg);
303 break;
304 case IETF_ATTR_INSTALLED_PACKAGES:
305 add_installed_packages(out_msg);
306 break;
307 default:
308 break;
309 }
310 }
311 e->destroy(e);
312 }
313 }
314 enumerator->destroy(enumerator);
315
316 if (fatal_error)
317 {
318 result = TNC_RESULT_FATAL;
319 }
320 else
321 {
322 result = out_msg->send(out_msg, TRUE);
323 }
324 out_msg->destroy(out_msg);
325
326 return result;
327 }
328
329 /**
330 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
331
332 */
333 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
334 TNC_ConnectionID connection_id,
335 TNC_BufferReference msg,
336 TNC_UInt32 msg_len,
337 TNC_MessageType msg_type)
338 {
339 imc_state_t *state;
340 imc_msg_t *in_msg;
341 TNC_Result result;
342
343 if (!imc_os)
344 {
345 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
346 return TNC_RESULT_NOT_INITIALIZED;
347 }
348 if (!imc_os->get_state(imc_os, connection_id, &state))
349 {
350 return TNC_RESULT_FATAL;
351 }
352 in_msg = imc_msg_create_from_data(imc_os, state, connection_id, msg_type,
353 chunk_create(msg, msg_len));
354 result = receive_message(in_msg);
355 in_msg->destroy(in_msg);
356
357 return result;
358 }
359
360 /**
361 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
362 */
363 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
364 TNC_ConnectionID connection_id,
365 TNC_UInt32 msg_flags,
366 TNC_BufferReference msg,
367 TNC_UInt32 msg_len,
368 TNC_VendorID msg_vid,
369 TNC_MessageSubtype msg_subtype,
370 TNC_UInt32 src_imv_id,
371 TNC_UInt32 dst_imc_id)
372 {
373 imc_state_t *state;
374 imc_msg_t *in_msg;
375 TNC_Result result;
376
377 if (!imc_os)
378 {
379 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
380 return TNC_RESULT_NOT_INITIALIZED;
381 }
382 if (!imc_os->get_state(imc_os, connection_id, &state))
383 {
384 return TNC_RESULT_FATAL;
385 }
386 in_msg = imc_msg_create_from_long_data(imc_os, state, connection_id,
387 src_imv_id, dst_imc_id,msg_vid, msg_subtype,
388 chunk_create(msg, msg_len));
389 result =receive_message(in_msg);
390 in_msg->destroy(in_msg);
391
392 return result;
393 }
394
395 /**
396 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
397 */
398 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
399 TNC_ConnectionID connection_id)
400 {
401 if (!imc_os)
402 {
403 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
404 return TNC_RESULT_NOT_INITIALIZED;
405 }
406 return TNC_RESULT_SUCCESS;
407 }
408
409 /**
410 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
411 */
412 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
413 {
414 if (!imc_os)
415 {
416 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
417 return TNC_RESULT_NOT_INITIALIZED;
418 }
419 imc_os->destroy(imc_os);
420 imc_os = NULL;
421
422 os->destroy(os);
423 os = NULL;
424
425 return TNC_RESULT_SUCCESS;
426 }
427
428 /**
429 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
430 */
431 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
432 TNC_TNCC_BindFunctionPointer bind_function)
433 {
434 if (!imc_os)
435 {
436 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
437 return TNC_RESULT_NOT_INITIALIZED;
438 }
439 return imc_os->bind_functions(imc_os, bind_function);
440 }