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