/src/strongswan/src/libimcv/imv/imv_session.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2013-2015 Andreas Steffen |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include "imv_session.h" |
18 | | |
19 | | #include <tncif_identity.h> |
20 | | |
21 | | #include <utils/debug.h> |
22 | | |
23 | | typedef struct private_imv_session_t private_imv_session_t; |
24 | | |
25 | | /** |
26 | | * Private data of a imv_session_t object. |
27 | | */ |
28 | | struct private_imv_session_t { |
29 | | |
30 | | /** |
31 | | * Public imv_session_t interface. |
32 | | */ |
33 | | imv_session_t public; |
34 | | |
35 | | /** |
36 | | * Unique Session ID |
37 | | */ |
38 | | int session_id; |
39 | | |
40 | | /** |
41 | | * Unique Product ID |
42 | | */ |
43 | | int pid; |
44 | | |
45 | | /** |
46 | | * Unique Device ID |
47 | | */ |
48 | | int did; |
49 | | |
50 | | /** |
51 | | * TNCCS connection ID |
52 | | */ |
53 | | TNC_ConnectionID conn_id; |
54 | | |
55 | | /** |
56 | | * Session creation time |
57 | | */ |
58 | | time_t created; |
59 | | |
60 | | /** |
61 | | * List of Access Requestor identities |
62 | | */ |
63 | | linked_list_t *ar_identities; |
64 | | |
65 | | /** |
66 | | * OS information |
67 | | */ |
68 | | imv_os_info_t *os_info; |
69 | | |
70 | | /** |
71 | | * Device ID |
72 | | */ |
73 | | chunk_t device_id; |
74 | | |
75 | | /** |
76 | | * Is Device ID trusted? |
77 | | */ |
78 | | bool trusted; |
79 | | |
80 | | /** |
81 | | * Have the workitems been generated? |
82 | | */ |
83 | | bool policy_started; |
84 | | |
85 | | /** |
86 | | * List of worklist items |
87 | | */ |
88 | | linked_list_t *workitems; |
89 | | |
90 | | /** |
91 | | * Reference count |
92 | | */ |
93 | | refcount_t ref; |
94 | | |
95 | | }; |
96 | | |
97 | | METHOD(imv_session_t, set_session_id, void, |
98 | | private_imv_session_t *this, int session_id, int pid, int did) |
99 | 0 | { |
100 | 0 | this->session_id = session_id; |
101 | 0 | this->pid = pid; |
102 | 0 | this->did = did; |
103 | 0 | } |
104 | | |
105 | | METHOD(imv_session_t, get_session_id, int, |
106 | | private_imv_session_t *this, int *pid, int *did) |
107 | 0 | { |
108 | 0 | if (pid) |
109 | 0 | { |
110 | 0 | *pid = this->pid; |
111 | 0 | } |
112 | 0 | if (did) |
113 | 0 | { |
114 | 0 | *did = this->did; |
115 | 0 | } |
116 | 0 | return this->session_id; |
117 | 0 | } |
118 | | |
119 | | METHOD(imv_session_t, get_connection_id, TNC_ConnectionID, |
120 | | private_imv_session_t *this) |
121 | 0 | { |
122 | 0 | return this->conn_id; |
123 | 0 | } |
124 | | |
125 | | METHOD(imv_session_t, set_creation_time, void, |
126 | | private_imv_session_t *this, time_t created) |
127 | 0 | { |
128 | 0 | this->created = created; |
129 | 0 | } |
130 | | |
131 | | METHOD(imv_session_t, get_creation_time, time_t, |
132 | | private_imv_session_t *this) |
133 | 0 | { |
134 | 0 | return this->created; |
135 | 0 | } |
136 | | |
137 | | METHOD(imv_session_t, create_ar_identities_enumerator, enumerator_t*, |
138 | | private_imv_session_t *this) |
139 | 0 | { |
140 | 0 | return this->ar_identities->create_enumerator(this->ar_identities); |
141 | 0 | } |
142 | | |
143 | | METHOD(imv_session_t, get_os_info, imv_os_info_t*, |
144 | | private_imv_session_t *this) |
145 | 0 | { |
146 | 0 | return this->os_info; |
147 | 0 | } |
148 | | |
149 | | METHOD(imv_session_t, set_device_id, void, |
150 | | private_imv_session_t *this, chunk_t device_id) |
151 | 0 | { |
152 | 0 | if (device_id.len == 0) |
153 | 0 | { |
154 | 0 | device_id = chunk_from_str("unknown"); |
155 | 0 | } |
156 | 0 | if (this->device_id.len) |
157 | 0 | { |
158 | 0 | if (chunk_equals(device_id, this->device_id)) |
159 | 0 | { |
160 | 0 | return; |
161 | 0 | } |
162 | 0 | free(this->device_id.ptr); |
163 | 0 | } |
164 | 0 | this->device_id = chunk_clone(device_id); |
165 | 0 | } |
166 | | |
167 | | METHOD(imv_session_t, get_device_id, bool, |
168 | | private_imv_session_t *this, chunk_t *device_id) |
169 | 0 | { |
170 | 0 | if (this->device_id.len == 0) |
171 | 0 | { |
172 | 0 | return FALSE; |
173 | 0 | } |
174 | 0 | if (device_id) |
175 | 0 | { |
176 | 0 | *device_id = this->device_id; |
177 | 0 | } |
178 | 0 | return TRUE; |
179 | 0 | } |
180 | | |
181 | | METHOD(imv_session_t, set_device_trust, void, |
182 | | private_imv_session_t *this, bool trusted) |
183 | 0 | { |
184 | 0 | this->trusted = trusted; |
185 | 0 | } |
186 | | |
187 | | METHOD(imv_session_t, get_device_trust, bool, |
188 | | private_imv_session_t *this) |
189 | 0 | { |
190 | 0 | return this->trusted; |
191 | 0 | } |
192 | | |
193 | | METHOD(imv_session_t, set_policy_started, void, |
194 | | private_imv_session_t *this, bool start) |
195 | 0 | { |
196 | 0 | this->policy_started = start; |
197 | 0 | } |
198 | | |
199 | | METHOD(imv_session_t, get_policy_started, bool, |
200 | | private_imv_session_t *this) |
201 | 0 | { |
202 | 0 | return this->policy_started; |
203 | 0 | } |
204 | | |
205 | | METHOD(imv_session_t, insert_workitem, void, |
206 | | private_imv_session_t *this, imv_workitem_t *workitem) |
207 | 0 | { |
208 | 0 | this->workitems->insert_last(this->workitems, workitem); |
209 | 0 | } |
210 | | |
211 | | METHOD(imv_session_t, remove_workitem, void, |
212 | | private_imv_session_t *this, enumerator_t *enumerator) |
213 | 0 | { |
214 | 0 | this->workitems->remove_at(this->workitems, enumerator); |
215 | 0 | } |
216 | | |
217 | | METHOD(imv_session_t, create_workitem_enumerator, enumerator_t*, |
218 | | private_imv_session_t *this) |
219 | 0 | { |
220 | 0 | return this->workitems->create_enumerator(this->workitems); |
221 | 0 | } |
222 | | |
223 | | METHOD(imv_session_t, get_workitem_count, int, |
224 | | private_imv_session_t *this, TNC_IMVID imv_id) |
225 | 0 | { |
226 | 0 | enumerator_t *enumerator; |
227 | 0 | imv_workitem_t *workitem; |
228 | 0 | int count = 0; |
229 | |
|
230 | 0 | enumerator = this->workitems->create_enumerator(this->workitems); |
231 | 0 | while (enumerator->enumerate(enumerator, &workitem)) |
232 | 0 | { |
233 | 0 | if (workitem->get_imv_id(workitem) == imv_id) |
234 | 0 | { |
235 | 0 | count++; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | enumerator->destroy(enumerator); |
239 | |
|
240 | 0 | return count; |
241 | 0 | } |
242 | | |
243 | | METHOD(imv_session_t, get_ref, imv_session_t*, |
244 | | private_imv_session_t *this) |
245 | 0 | { |
246 | 0 | ref_get(&this->ref); |
247 | |
|
248 | 0 | return &this->public; |
249 | 0 | } |
250 | | |
251 | | METHOD(imv_session_t, destroy, void, |
252 | | private_imv_session_t *this) |
253 | 0 | { |
254 | 0 | if (ref_put(&this->ref)) |
255 | 0 | { |
256 | 0 | this->workitems->destroy_offset(this->workitems, |
257 | 0 | offsetof(imv_workitem_t, destroy)); |
258 | 0 | this->os_info->destroy(this->os_info); |
259 | 0 | this->ar_identities->destroy_offset(this->ar_identities, |
260 | 0 | offsetof(tncif_identity_t, destroy)); |
261 | 0 | free(this->device_id.ptr); |
262 | 0 | free(this); |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | | /** |
267 | | * See header |
268 | | */ |
269 | | imv_session_t *imv_session_create(TNC_ConnectionID conn_id, |
270 | | linked_list_t *ar_identities) |
271 | 0 | { |
272 | 0 | private_imv_session_t *this; |
273 | |
|
274 | 0 | INIT(this, |
275 | 0 | .public = { |
276 | 0 | .set_session_id = _set_session_id, |
277 | 0 | .get_session_id = _get_session_id, |
278 | 0 | .get_connection_id = _get_connection_id, |
279 | 0 | .set_creation_time = _set_creation_time, |
280 | 0 | .get_creation_time = _get_creation_time, |
281 | 0 | .create_ar_identities_enumerator = _create_ar_identities_enumerator, |
282 | 0 | .get_os_info = _get_os_info, |
283 | 0 | .set_device_id = _set_device_id, |
284 | 0 | .get_device_id = _get_device_id, |
285 | 0 | .set_device_trust = _set_device_trust, |
286 | 0 | .get_device_trust = _get_device_trust, |
287 | 0 | .set_policy_started = _set_policy_started, |
288 | 0 | .get_policy_started = _get_policy_started, |
289 | 0 | .insert_workitem = _insert_workitem, |
290 | 0 | .remove_workitem = _remove_workitem, |
291 | 0 | .create_workitem_enumerator = _create_workitem_enumerator, |
292 | 0 | .get_workitem_count = _get_workitem_count, |
293 | 0 | .get_ref = _get_ref, |
294 | 0 | .destroy = _destroy, |
295 | 0 | }, |
296 | 0 | .conn_id = conn_id, |
297 | 0 | .ar_identities = ar_identities, |
298 | 0 | .os_info = imv_os_info_create(), |
299 | 0 | .workitems = linked_list_create(), |
300 | 0 | .ref = 1, |
301 | 0 | ); |
302 | |
|
303 | 0 | return &this->public; |
304 | 0 | } |