aboutsummaryrefslogtreecommitdiffstats
path: root/src/libimcv/plugins/imc_hcd/imc_hcd.c
blob: b631683cec8ec54f8bbdb99b0ddce441c511a82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
 * Copyright (C) 2015 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "imc_hcd_state.h"

#include <imc/imc_agent.h>
#include <imc/imc_msg.h>
#include <imc/imc_os_info.h>
#include <generic/generic_attr_bool.h>
#include <generic/generic_attr_chunk.h>
#include <generic/generic_attr_string.h>
#include <ietf/ietf_attr.h>
#include <ietf/ietf_attr_attr_request.h>
#include "ietf/ietf_attr_fwd_enabled.h"
#include <pwg/pwg_attr.h>
#include <pwg/pwg_attr_vendor_smi_code.h>

#include <tncif_pa_subtypes.h>

#include <pen/pen.h>
#include <utils/debug.h>

/* IMC definitions */

static const char imc_name[] = "HCD";

static pen_type_t msg_types[] = {
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_SYSTEM },
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_CONSOLE },
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_MARKER },
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_FINISHER },
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_INTERFACE },
	{ PEN_PWG, PA_SUBTYPE_PWG_HCD_SCANNER }
};

static imc_agent_t *imc_hcd;
static imc_os_info_t *os;

typedef struct section_subtype_t section_subtype_t;

struct section_subtype_t {
	char *section;
	pa_subtype_pwg_t subtype;
};

static section_subtype_t section_subtypes[] = {
	{ "system",    PA_SUBTYPE_PWG_HCD_SYSTEM    },
	{ "console",   PA_SUBTYPE_PWG_HCD_CONSOLE   },
	{ "marker",    PA_SUBTYPE_PWG_HCD_MARKER    },
	{ "finisher",  PA_SUBTYPE_PWG_HCD_FINISHER  },
	{ "interface", PA_SUBTYPE_PWG_HCD_INTERFACE },
	{ "scanner"  , PA_SUBTYPE_PWG_HCD_SCANNER   }
};

typedef struct quadruple_t quadruple_t;

struct quadruple_t {
	char *section;
	pwg_attr_t name_attr;
	pwg_attr_t patches_attr;
	pwg_attr_t string_version_attr;
	pwg_attr_t version_attr;
};

static quadruple_t quadruples[] = {
	{ "firmware",
	   PWG_HCD_FIRMWARE_NAME, PWG_HCD_FIRMWARE_PATCHES,
	   PWG_HCD_FIRMWARE_STRING_VERSION, PWG_HCD_FIRMWARE_VERSION },
	{ "resident_application",
	   PWG_HCD_RESIDENT_APP_NAME, PWG_HCD_RESIDENT_APP_PATCHES,
	   PWG_HCD_RESIDENT_APP_STRING_VERSION, PWG_HCD_RESIDENT_APP_VERSION },
	{ "user_application",
	   PWG_HCD_USER_APP_NAME, PWG_HCD_USER_APP_PATCHES,
	   PWG_HCD_USER_APP_STRING_VERSION, PWG_HCD_USER_APP_VERSION }
};

/**
 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_Initialize(TNC_IMCID imc_id,
										  TNC_Version min_version,
										  TNC_Version max_version,
										  TNC_Version *actual_version)
{
	if (imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
		return TNC_RESULT_ALREADY_INITIALIZED;
	}
	imc_hcd = imc_agent_create(imc_name, msg_types, countof(msg_types),
							  imc_id, actual_version);
	if (!imc_hcd)
	{
		return TNC_RESULT_FATAL;
	}

	os = imc_os_info_create();
	if (!os)
	{
		imc_hcd->destroy(imc_hcd);
		imc_hcd = NULL;

		return TNC_RESULT_FATAL;
	}

	if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
	{
		DBG1(DBG_IMC, "no common IF-IMC version");
		return TNC_RESULT_NO_COMMON_VERSION;
	}
	return TNC_RESULT_SUCCESS;
}

/**
 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
				TNC_ConnectionID connection_id, TNC_ConnectionState new_state)
{
	imc_state_t *state;

	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	switch (new_state)
	{
		case TNC_CONNECTION_STATE_CREATE:
			state = imc_hcd_state_create(connection_id);
			return imc_hcd->create_state(imc_hcd, state);
		case TNC_CONNECTION_STATE_HANDSHAKE:
			if (imc_hcd->change_state(imc_hcd, connection_id, new_state,
				&state) != TNC_RESULT_SUCCESS)
			{
				return TNC_RESULT_FATAL;
			}
			state->set_result(state, imc_id,
							  TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
			return TNC_RESULT_SUCCESS;
		case TNC_CONNECTION_STATE_DELETE:
			return imc_hcd->delete_state(imc_hcd, connection_id);
		default:
			return imc_hcd->change_state(imc_hcd, connection_id,
											 new_state, NULL);
	}
}

/**
 * Add AttributesNaturalLanguage attribute to send queue
 */
static void add_attrs_natural_lang(imc_msg_t *msg, char *section)
{
	pa_tnc_attr_t *attr;
	char *string;

	string = lib->settings->get_str(lib->settings,
				"%s.plugins.imc-hcd.subtypes.%s.attributes_natural_language",
				"en", lib->ns, section);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_ATTRS_NATURAL_LANG,
				string);
	attr = generic_attr_string_create(chunk_from_str(string),
				pen_type_create(PEN_PWG, PWG_HCD_ATTRS_NATURAL_LANG));
	msg->add_attribute(msg, attr);
}

/**
 * Add DefaultPasswordEnabled attribute to send queue
 */
static void add_default_pwd_enabled(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	bool status;

	status = os->get_default_pwd_status(os);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_DEFAULT_PWD_ENABLED,
				status ? "yes" : "no");
	attr = generic_attr_bool_create(status,
				pen_type_create(PEN_PWG, PWG_HCD_DEFAULT_PWD_ENABLED));
	msg->add_attribute(msg, attr);
}

/**
 * Add ForwardingEnabled attribute to send queue
 */
static void add_forwarding_enabled(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	os_fwd_status_t fwd_status;

	fwd_status = os->get_fwd_status(os);
	DBG2(DBG_IMC, "  %N: %N", pwg_attr_names, PWG_HCD_FORWARDING_ENABLED,
				os_fwd_status_names, fwd_status);
	attr = ietf_attr_fwd_enabled_create(fwd_status,
				pen_type_create(PEN_PWG, PWG_HCD_FORWARDING_ENABLED));
	msg->add_attribute(msg, attr);
}

/**
 * Add MachineTypeModel attribute to send queue
 */
static void add_machine_type_model(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	char *string;

	string = lib->settings->get_str(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.machine_type_model",
				"", lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_MACHINE_TYPE_MODEL,
				string);
	attr = generic_attr_string_create(chunk_from_str(string),
				pen_type_create(PEN_PWG, PWG_HCD_MACHINE_TYPE_MODEL));
	msg->add_attribute(msg, attr);
}

/**
 * Add PSTNFaxEnabled attribute to send queue
 */
static void add_pstn_fax_enabled(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	bool status;

	status = lib->settings->get_bool(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.pstn_fax_enabled",
				FALSE, lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_PSTN_FAX_ENABLED,
				status ? "yes" : "no");
	attr = generic_attr_bool_create(status,
				pen_type_create(PEN_PWG, PWG_HCD_PSTN_FAX_ENABLED));
	msg->add_attribute(msg, attr);
}

/**
 * Add TimeSource attribute to send queue
 */
static void add_time_source(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	char *string;

	string = lib->settings->get_str(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.time_source",
				"", lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_TIME_SOURCE,
				string);
	attr = generic_attr_string_create(chunk_from_str(string),
				pen_type_create(PEN_PWG, PWG_HCD_TIME_SOURCE));
	msg->add_attribute(msg, attr);
}

/**
 * Add UserApplicationEnabled attribute to send queue
 */
static void add_user_app_enabled(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	bool status;

	status = lib->settings->get_bool(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.user_application_enabled",
				FALSE, lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_USER_APP_ENABLED,
				status ? "yes" : "no");
	attr = generic_attr_bool_create(status,
				pen_type_create(PEN_PWG, PWG_HCD_USER_APP_ENABLED));
	msg->add_attribute(msg, attr);
}

/**
 * Add UserApplicationPersistenceEnabled attribute to send queue
 */
static void add_user_app_persist_enabled(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	bool status;

	status = lib->settings->get_bool(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.user_application_persistence.enabled",
				FALSE, lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_USER_APP_PERSIST_ENABLED,
				status ? "yes" : "no");
	attr = generic_attr_bool_create(status,
				pen_type_create(PEN_PWG, PWG_HCD_USER_APP_PERSIST_ENABLED));
	msg->add_attribute(msg, attr);
}

/**
 * Add VendorName attribute to send queue
 */
static void add_vendor_name(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	char *string;

	string = lib->settings->get_str(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.vendor_name",
				"", lib->ns);
	DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, PWG_HCD_VENDOR_NAME,
				string);
	attr = generic_attr_string_create(chunk_from_str(string),
				pen_type_create(PEN_PWG, PWG_HCD_VENDOR_NAME));
	msg->add_attribute(msg, attr);
}

/**
 * Add VendorSMICode attribute to send queue
 */
static void add_vendor_smi_code(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	int smi_code;

	smi_code = lib->settings->get_int(lib->settings,
				"%s.plugins.imc-hcd.subtypes.system.vendor_smi_code",
				0, lib->ns);
	DBG2(DBG_IMC, "  %N: 0x%06x (%d)", pwg_attr_names, PWG_HCD_VENDOR_SMI_CODE,
				smi_code, smi_code);
	attr = pwg_attr_vendor_smi_code_create(smi_code);
	msg->add_attribute(msg, attr);
}

/**
 * Add CertificationState attribute to send queue
 */
static void add_certification_state(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	char *hex_string;
	chunk_t blob;

	hex_string = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.system.certification_state",
					NULL, lib->ns);
	if (hex_string)
	{
		blob = chunk_from_hex(chunk_from_str(hex_string), NULL);
	
		DBG2(DBG_IMC, "  %N: %B", pwg_attr_names, PWG_HCD_CERTIFICATION_STATE,
					&blob);
		attr = generic_attr_chunk_create(blob,
					pen_type_create(PEN_PWG, PWG_HCD_CERTIFICATION_STATE));
		msg->add_attribute(msg, attr);
		chunk_free(&blob);
	}
}

/**
 * Add CertificationState attribute to send queue
 */
static void add_configuration_state(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	char *hex_string;
	chunk_t blob;

	hex_string = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.system.configuration_state",
					NULL, lib->ns);
	if (hex_string)
	{
		blob = chunk_from_hex(chunk_from_str(hex_string), NULL);
	
		DBG2(DBG_IMC, "  %N: %B", pwg_attr_names, PWG_HCD_CONFIGURATION_STATE,
					&blob);
		attr = generic_attr_chunk_create(blob,
					pen_type_create(PEN_PWG, PWG_HCD_CONFIGURATION_STATE));
		msg->add_attribute(msg, attr);
		chunk_free(&blob);
	}
}

/**
 * Add Correlated Attributes to send queue
 */
static void add_quadruple(imc_msg_t *msg, char *section, quadruple_t *quad)
{
	pa_tnc_attr_t *attr;
	const size_t version_len = 16;
	char version[version_len];
	char hex_version_default[] = "00000000000000000000000000000000";
	char *app, *name, *patches, *string_version, *hex_version;
	size_t len;
	chunk_t num_version;
	enumerator_t *enumerator;

	enumerator = lib->settings->create_section_enumerator(lib->settings,
					"%s.plugins.imc-hcd.subtypes.%s.%s",
					lib->ns, section, quad->section);
	while (enumerator->enumerate(enumerator, &app))
	{
		name = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.%s.%s.%s.name",
					"",	lib->ns, section, quad->section, app);
		patches = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.%s.%s.%s.patches",
					"", lib->ns, section, quad->section, app);
		string_version = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.%s.%s.%s.string_version",
					"",	lib->ns, section, quad->section, app);
		hex_version = lib->settings->get_str(lib->settings,
					"%s.plugins.imc-hcd.subtypes.%s.%s.%s.version", 
					hex_version_default, lib->ns, section, quad->section, app);

		/* convert hex string into binary chunk */
		if (strlen(hex_version) > 2 * version_len)
		{
			hex_version = hex_version_default;
		}
		num_version = chunk_from_hex(chunk_from_str(hex_version), version);

		DBG2(DBG_IMC, "--- %s ---", app);

		DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, quad->name_attr, name);
		attr = generic_attr_string_create(chunk_from_str(name),
						pen_type_create(PEN_PWG, quad->name_attr));
		msg->add_attribute(msg, attr);

		/* remove any trailing LF from patches string for logging */
		len = strlen(patches);
		if (len && (patches[len - 1] == '\n'))
		{
			len--;
		}
		DBG2(DBG_IMC, "  %N:%s%.*s", pwg_attr_names, quad->patches_attr,
						len ? "\n" : " ", len, patches);
		attr = generic_attr_string_create(chunk_from_str(patches),
						pen_type_create(PEN_PWG, quad->patches_attr));
		msg->add_attribute(msg, attr);

		DBG2(DBG_IMC, "  %N: %s", pwg_attr_names, quad->string_version_attr,
						string_version);
		attr = generic_attr_string_create(chunk_from_str(string_version),
						pen_type_create(PEN_PWG, quad->string_version_attr));
		msg->add_attribute(msg, attr);

		DBG2(DBG_IMC, "  %N: %#B", pwg_attr_names, quad->version_attr, &num_version);
		attr = generic_attr_chunk_create(num_version,
						pen_type_create(PEN_PWG, quad->version_attr));
		msg->add_attribute(msg, attr);
	}
	enumerator->destroy(enumerator);
}

/**
 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
											  TNC_ConnectionID connection_id)
{
	imc_state_t *state;
	imc_msg_t *out_msg;
	TNC_Result result = TNC_RESULT_SUCCESS;
	pa_subtype_pwg_t subtype;
	pen_type_t msg_type;
	enumerator_t *enumerator;
	char *section;
	int i;

	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	if (!imc_hcd->get_state(imc_hcd, connection_id, &state))
	{
		return TNC_RESULT_FATAL;
	}

	/* Enumerate over all HCD subtype sections */
	enumerator = lib->settings->create_section_enumerator(lib->settings,
						"%s.plugins.imc-hcd.subtypes", lib->ns);
	while (enumerator->enumerate(enumerator, &section) &&
		   result == TNC_RESULT_SUCCESS)
	{
		subtype = PA_SUBTYPE_PWG_HCD_UNKNOWN;

		for (i = 0; i < countof(section_subtypes); i++)
		{
			if (streq(section, section_subtypes[i].section))
			{
				subtype = section_subtypes[i].subtype;
				break;
			}
		}
		if (subtype == PA_SUBTYPE_PWG_HCD_UNKNOWN)
		{
			DBG1(DBG_IMC, "HCD subtype '%s' not supported", section);
			continue;
		}
		DBG2(DBG_IMC, "retrieving attributes for PA subtype %N/%N",
			 pen_names, PEN_PWG, pa_subtype_pwg_names, subtype);

		msg_type = pen_type_create(PEN_PWG, subtype);
		out_msg = imc_msg_create(imc_hcd, state, connection_id, imc_id,
								 TNC_IMVID_ANY, msg_type);

		/* mandatory attributes that are always sent without request */
		add_attrs_natural_lang(out_msg, section);
		if (subtype == PA_SUBTYPE_PWG_HCD_SYSTEM)
		{
			add_default_pwd_enabled(out_msg);
			add_forwarding_enabled(out_msg);
			add_machine_type_model(out_msg);
			add_pstn_fax_enabled(out_msg);
			add_time_source(out_msg);
			add_vendor_name(out_msg);
			add_vendor_smi_code(out_msg);
			add_user_app_enabled(out_msg);
			add_user_app_persist_enabled(out_msg);
		}
		if (lib->settings->get_bool(lib->settings,
								"%s.plugins.imc-hcd.push_info", FALSE, lib->ns))
		{
			/* correlated attributes */
			for (i = 0; i < countof(quadruples); i++)
			{
				add_quadruple(out_msg, section, &quadruples[i]);
			}
		}

		/* send PA-TNC message with the excl flag not set */
		result = out_msg->send(out_msg, FALSE);
		out_msg->destroy(out_msg);
	}
	enumerator->destroy(enumerator);

	return result;
}

static TNC_Result receive_message(imc_state_t *state, imc_msg_t *in_msg)
{
	imc_msg_t *out_msg;
	enumerator_t *enumerator;
	pa_tnc_attr_t *attr;
	pen_type_t type, msg_type;
	TNC_Result result;
	char *section = NULL;
	int i;
	bool fatal_error = FALSE, pushed_info;

	/* generate an outgoing PA-TNC message - we might need it */
	out_msg = imc_msg_create_as_reply(in_msg);

	/* parse received PA-TNC message and handle local and remote errors */
	result = in_msg->receive(in_msg, out_msg, &fatal_error);
	if (result != TNC_RESULT_SUCCESS)
	{
		out_msg->destroy(out_msg);
		return result;
	}
	msg_type = in_msg->get_msg_type(in_msg);

	for (i = 0; i < countof(section_subtypes); i++)
	{
		if (msg_type.type == section_subtypes[i].subtype)
		{
			section = section_subtypes[i].section;
			break;
		}
	}
	pushed_info = lib->settings->get_bool(lib->settings,
						"%s.plugins.imc-hcd.push_info", FALSE, lib->ns);

	/* analyze PA-TNC attributes */
	enumerator = in_msg->create_attribute_enumerator(in_msg);
	while (enumerator->enumerate(enumerator, &attr))
	{
		type = attr->get_type(attr);

		if (type.vendor_id == PEN_IETF)
		{
			if (type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
			{
				ietf_attr_attr_request_t *attr_cast;
				pen_type_t *entry;
				enumerator_t *e;

				attr_cast = (ietf_attr_attr_request_t*)attr;

				e = attr_cast->create_enumerator(attr_cast);
				while (e->enumerate(e, &entry))
				{
					if (entry->vendor_id == PEN_PWG)
					{
						switch (entry->type)
						{
							case PWG_HCD_ATTRS_NATURAL_LANG:
								add_attrs_natural_lang(out_msg, section);
								break;
							case PWG_HCD_DEFAULT_PWD_ENABLED:
								add_default_pwd_enabled(out_msg);
								break;
							case PWG_HCD_FORWARDING_ENABLED:
								add_forwarding_enabled(out_msg);
								break;
							case PWG_HCD_MACHINE_TYPE_MODEL:
								add_machine_type_model(out_msg);
								break;
							case PWG_HCD_PSTN_FAX_ENABLED:
								add_pstn_fax_enabled(out_msg);
								break;
							case PWG_HCD_TIME_SOURCE:
								add_time_source(out_msg);
								break;
							case PWG_HCD_USER_APP_ENABLED:
								add_user_app_enabled(out_msg);
								break;
							case PWG_HCD_USER_APP_PERSIST_ENABLED:
								add_user_app_persist_enabled(out_msg);
								break;
							case PWG_HCD_VENDOR_NAME:
								add_vendor_name(out_msg);
								break;
							case PWG_HCD_VENDOR_SMI_CODE:
								add_vendor_smi_code(out_msg);
								break;
							case PWG_HCD_CERTIFICATION_STATE:
								add_certification_state(out_msg);
								break;
							case PWG_HCD_CONFIGURATION_STATE:
								add_configuration_state(out_msg);
								break;
							default:
								if (pushed_info)
								{
									continue;
								}
						}

						/* if not pushed, deliver on request */
						switch (entry->type)
						{
							case PWG_HCD_FIRMWARE_NAME:
								add_quadruple(out_msg, section, &quadruples[0]);
								break;
							case PWG_HCD_RESIDENT_APP_NAME:
								add_quadruple(out_msg, section, &quadruples[1]);
								break;
							case PWG_HCD_USER_APP_NAME:
								add_quadruple(out_msg, section, &quadruples[2]);
								break;
							default:
								break;
						}
					}
				}
				e->destroy(e);
			}
		}
	}
	enumerator->destroy(enumerator);

	if (fatal_error)
	{
		result = TNC_RESULT_FATAL;
	}
	else
	{
		/* send PA-TNC message with the EXCL flag set */
		result = out_msg->send(out_msg, TRUE);
	}
	out_msg->destroy(out_msg);

	return result;
}

/**
 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
											  TNC_ConnectionID connection_id,
											  TNC_BufferReference msg,
											  TNC_UInt32 msg_len,
											  TNC_MessageType msg_type)
{
	imc_state_t *state;
	imc_msg_t *in_msg;
	TNC_Result result;

	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	if (!imc_hcd->get_state(imc_hcd, connection_id, &state))
	{
		return TNC_RESULT_FATAL;
	}
	in_msg = imc_msg_create_from_data(imc_hcd, state, connection_id, msg_type,
									  chunk_create(msg, msg_len));
	result = receive_message(state, in_msg);
	in_msg->destroy(in_msg);

	return result;
}

/**
 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
												  TNC_ConnectionID connection_id,
												  TNC_UInt32 msg_flags,
												  TNC_BufferReference msg,
												  TNC_UInt32 msg_len,
												  TNC_VendorID msg_vid,
												  TNC_MessageSubtype msg_subtype,
												  TNC_UInt32 src_imv_id,
												  TNC_UInt32 dst_imc_id)
{
	imc_state_t *state;
	imc_msg_t *in_msg;
	TNC_Result result;

	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	if (!imc_hcd->get_state(imc_hcd, connection_id, &state))
	{
		return TNC_RESULT_FATAL;
	}
	in_msg = imc_msg_create_from_long_data(imc_hcd, state, connection_id,
								src_imv_id, dst_imc_id,msg_vid, msg_subtype,
								chunk_create(msg, msg_len));
	result =receive_message(state, in_msg);
	in_msg->destroy(in_msg);

	return result;
}

/**
 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_BatchEnding(TNC_IMCID imc_id,
										   TNC_ConnectionID connection_id)
{
	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	return TNC_RESULT_SUCCESS;
}

/**
 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_Terminate(TNC_IMCID imc_id)
{
	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	imc_hcd->destroy(imc_hcd);
	imc_hcd = NULL;

	os->destroy(os);
	os = NULL;

	return TNC_RESULT_SUCCESS;
}

/**
 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
 */
TNC_Result TNC_IMC_API TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
									TNC_TNCC_BindFunctionPointer bind_function)
{
	if (!imc_hcd)
	{
		DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
		return TNC_RESULT_NOT_INITIALIZED;
	}
	return imc_hcd->bind_functions(imc_hcd, bind_function);
}