summaryrefslogtreecommitdiff
path: root/src/libimcv/imv/imv_session.c
blob: 1f0d8cf14af050310a7eab8c7c9b0cc6c6c832c3 (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
/*
 * Copyright (C) 2013 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 "imv_session.h"

#include <utils/debug.h>

typedef struct private_imv_session_t private_imv_session_t;

/**
 * Private data of a imv_session_t object.
 */
struct private_imv_session_t {

	/**
	 * Public imv_session_t interface.
	 */
	imv_session_t public;

	/**
	 * Unique Session ID
	 */
	int session_id;

	/**
	 * Unique Product ID
	 */
	int pid;

	/**
	 * Unique Device ID
	 */
	int did;

	/**
	 * TNCCS connection ID
	 */
	TNC_ConnectionID conn_id;

	/**
	 * Session creation time
	 */
	time_t created;

	/**
	 * Access Requestor ID type
	 */
	uint32_t ar_id_type;

	/**
	 * Access Requestor ID value
	 */
	chunk_t ar_id_value;

	/**
	 * OS information
	 */
	imv_os_info_t *os_info;

	/**
	 * Device ID
	 */
	chunk_t device_id;

	/**
	 * Is Device ID trusted?
	 */
	bool trusted;

	/**
	 * Have the workitems been generated?
	 */
	bool policy_started;

	/**
	 * List of worklist items
	 */
	linked_list_t *workitems;

	/**
	 * Reference count
	 */
	refcount_t ref;

};

METHOD(imv_session_t, set_session_id, void,
	private_imv_session_t *this, int session_id, int pid, int did)
{
	this->session_id = session_id;
	this->pid = pid;
	this->did = did;
}

METHOD(imv_session_t, get_session_id, int,
	private_imv_session_t *this, int *pid, int *did)
{
	if (pid)
	{
		*pid = this->pid;
	}
	if (did)
	{
		*did = this->did;
	}
	return this->session_id;
}

METHOD(imv_session_t, get_connection_id, TNC_ConnectionID,
	private_imv_session_t *this)
{
	return this->conn_id;
}

METHOD(imv_session_t, get_creation_time, time_t,
	private_imv_session_t *this)
{
	return this->created;
}

METHOD(imv_session_t, get_ar_id, chunk_t,
	private_imv_session_t *this, uint32_t *ar_id_type)
{
	if (ar_id_type)
	{
		*ar_id_type = this->ar_id_type;
	}
	return this->ar_id_value;
}

METHOD(imv_session_t, get_os_info, imv_os_info_t*,
	private_imv_session_t *this)
{
	return this->os_info;
}

METHOD(imv_session_t, set_device_id, void,
	private_imv_session_t *this, chunk_t device_id)
{
	if (device_id.len == 0)
	{
		device_id = chunk_from_str("unknown");
	}
	if (this->device_id.len)
	{
		if (chunk_equals(device_id, this->device_id))
		{
			return;
		}
		free(this->device_id.ptr);
	}
	this->device_id = chunk_clone(device_id);
}

METHOD(imv_session_t, get_device_id, bool,
	private_imv_session_t *this, chunk_t *device_id)
{
	if (this->device_id.len == 0)
	{
		return FALSE;
	}
	if (device_id)
	{
		*device_id = this->device_id;
	}
	return TRUE;
}

METHOD(imv_session_t, set_device_trust, void,
	private_imv_session_t *this, bool trusted)
{
	this->trusted = trusted;
}

METHOD(imv_session_t, get_device_trust, bool,
	private_imv_session_t *this)
{
	return this->trusted;
}

METHOD(imv_session_t, set_policy_started, void,
	private_imv_session_t *this, bool start)
{
	this->policy_started = start;
}

METHOD(imv_session_t, get_policy_started, bool,
	private_imv_session_t *this)
{
	return this->policy_started;
}

METHOD(imv_session_t, insert_workitem, void,
	private_imv_session_t *this, imv_workitem_t *workitem)
{
	this->workitems->insert_last(this->workitems, workitem);
}

METHOD(imv_session_t, remove_workitem, void,
	private_imv_session_t *this, enumerator_t *enumerator)
{
	this->workitems->remove_at(this->workitems, enumerator);
}

METHOD(imv_session_t, create_workitem_enumerator, enumerator_t*,
	private_imv_session_t *this)
{
	return this->workitems->create_enumerator(this->workitems);
}

METHOD(imv_session_t, get_workitem_count, int,
	private_imv_session_t *this, TNC_IMVID imv_id)
{
	enumerator_t *enumerator;
	imv_workitem_t *workitem;
	int count = 0;

	enumerator = this->workitems->create_enumerator(this->workitems);
	while (enumerator->enumerate(enumerator, &workitem))
	{
		if (workitem->get_imv_id(workitem) == imv_id)
		{
			count++;
		}
	}
	enumerator->destroy(enumerator);

	return count;
}

METHOD(imv_session_t, get_ref, imv_session_t*,
	private_imv_session_t *this)
{
	ref_get(&this->ref);

	return &this->public;
}

METHOD(imv_session_t, destroy, void,
	private_imv_session_t *this)
{
	if (ref_put(&this->ref))
	{
		this->workitems->destroy_offset(this->workitems,
								 offsetof(imv_workitem_t, destroy));
		this->os_info->destroy(this->os_info);
		free(this->ar_id_value.ptr);
		free(this->device_id.ptr);
		free(this);
	}
}

/**
 * See header
 */
imv_session_t *imv_session_create(TNC_ConnectionID conn_id, time_t created,
								  uint32_t ar_id_type, chunk_t ar_id_value)
{
	private_imv_session_t *this;

	INIT(this,
		.public = {
			.set_session_id = _set_session_id,
			.get_session_id = _get_session_id,
			.get_connection_id = _get_connection_id,
			.get_creation_time = _get_creation_time,
			.get_ar_id = _get_ar_id,
			.get_os_info = _get_os_info,
			.set_device_id = _set_device_id,
			.get_device_id = _get_device_id,
			.set_device_trust = _set_device_trust,
			.get_device_trust = _get_device_trust,
			.set_policy_started = _set_policy_started,
			.get_policy_started = _get_policy_started,
			.insert_workitem = _insert_workitem,
			.remove_workitem = _remove_workitem,
			.create_workitem_enumerator = _create_workitem_enumerator,
			.get_workitem_count = _get_workitem_count,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.conn_id = conn_id,
		.created = created,
		.ar_id_type = ar_id_type,
		.ar_id_value = chunk_clone(ar_id_value),
		.os_info = imv_os_info_create(),
		.workitems = linked_list_create(),
		.ref = 1,
	);

	return &this->public;
}