2f37e4fdae6b12d2396f64c7f66309b5a84453a8
[strongswan.git] / src / libimcv / ietf / ietf_attr_installed_packages.c
1 /*
2 * Copyright (C) 2012-2014 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 "ietf_attr_installed_packages.h"
17
18 #include <string.h>
19
20 #include <pa_tnc/pa_tnc_msg.h>
21 #include <bio/bio_writer.h>
22 #include <bio/bio_reader.h>
23 #include <collections/linked_list.h>
24 #include <utils/debug.h>
25
26
27 typedef struct private_ietf_attr_installed_packages_t private_ietf_attr_installed_packages_t;
28 typedef struct package_entry_t package_entry_t;
29
30 /**
31 * PA-TNC Installed Packages Type (see section 4.2.7 of RFC 5792)
32 *
33 * 1 2 3
34 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | Reserved | Package Count |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Pkg Name Len | Package Name (Variable Length) |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Version Len | Package Version Number (Variable Length) |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 */
43
44 /**
45 * Private data of an ietf_attr_installed_packages_t object.
46 */
47 struct private_ietf_attr_installed_packages_t {
48
49 /**
50 * Public members of ietf_attr_installed_packages_t
51 */
52 ietf_attr_installed_packages_t public;
53
54 /**
55 * Vendor-specific attribute type
56 */
57 pen_type_t type;
58
59 /**
60 * Length of attribute value
61 */
62 size_t length;
63
64 /**
65 * Attribute value or segment
66 */
67 chunk_t value;
68
69 /**
70 * Noskip flag
71 */
72 bool noskip_flag;
73
74 /**
75 * List of Installed Package entries
76 */
77 linked_list_t *packages;
78
79 /**
80 * Reference count
81 */
82 refcount_t ref;
83 };
84
85 /**
86 * Package entry
87 */
88 struct package_entry_t {
89 chunk_t name;
90 chunk_t version;
91 };
92
93 /**
94 * Free a package entry
95 */
96 static void free_package_entry(package_entry_t *entry)
97 {
98 free(entry->name.ptr);
99 free(entry->version.ptr);
100 free(entry);
101 }
102
103 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
104 private_ietf_attr_installed_packages_t *this)
105 {
106 return this->type;
107 }
108
109 METHOD(pa_tnc_attr_t, get_value, chunk_t,
110 private_ietf_attr_installed_packages_t *this)
111 {
112 return this->value;
113 }
114
115 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
116 private_ietf_attr_installed_packages_t *this)
117 {
118 return this->noskip_flag;
119 }
120
121 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
122 private_ietf_attr_installed_packages_t *this, bool noskip)
123 {
124 this->noskip_flag = noskip;
125 }
126
127 METHOD(pa_tnc_attr_t, build, void,
128 private_ietf_attr_installed_packages_t *this)
129 {
130 bio_writer_t *writer;
131 enumerator_t *enumerator;
132 package_entry_t *entry;
133
134 if (this->value.ptr)
135 {
136 return;
137 }
138 writer = bio_writer_create(IETF_INSTALLED_PACKAGES_MIN_SIZE);
139 writer->write_uint16(writer, 0x0000);
140 writer->write_uint16(writer, this->packages->get_count(this->packages));
141
142 enumerator = this->packages->create_enumerator(this->packages);
143 while (enumerator->enumerate(enumerator, &entry))
144 {
145 writer->write_data8(writer, entry->name);
146 writer->write_data8(writer, entry->version);
147 }
148 enumerator->destroy(enumerator);
149
150 this->value = writer->extract_buf(writer);
151 this->length = this->value.len;
152 writer->destroy(writer);
153 }
154
155 METHOD(pa_tnc_attr_t, process, status_t,
156 private_ietf_attr_installed_packages_t *this, u_int32_t *offset)
157 {
158 bio_reader_t *reader;
159 package_entry_t *entry;
160 status_t status = FAILED;
161 chunk_t name, version;
162 u_int16_t reserved, count;
163 u_char *pos;
164
165 *offset = 0;
166
167 if (this->value.len < this->length)
168 {
169 return NEED_MORE;
170 }
171 if (this->value.len < IETF_INSTALLED_PACKAGES_MIN_SIZE)
172 {
173 DBG1(DBG_TNC, "insufficient data for IETF installed packages");
174 return FAILED;
175 }
176 reader = bio_reader_create(this->value);
177 reader->read_uint16(reader, &reserved);
178 reader->read_uint16(reader, &count);
179 *offset = IETF_INSTALLED_PACKAGES_MIN_SIZE;
180
181 while (reader->remaining(reader))
182 {
183 if (!reader->read_data8(reader, &name))
184 {
185 DBG1(DBG_TNC, "insufficient data for IETF installed package name");
186 goto end;
187 }
188 pos = memchr(name.ptr, '\0', name.len);
189 if (pos)
190 {
191 DBG1(DBG_TNC, "nul termination in IETF installed package name");
192 *offset += 1 + (pos - name.ptr);
193 goto end;
194 }
195 *offset += 1 + name.len;
196
197 if (!reader->read_data8(reader, &version))
198 {
199 DBG1(DBG_TNC, "insufficient data for IETF installed package version");
200 goto end;
201 }
202 pos = memchr(version.ptr, '\0', version.len);
203 if (pos)
204 {
205 DBG1(DBG_TNC, "nul termination in IETF installed package version");
206 *offset += 1 + (pos - version.ptr);
207 goto end;
208 }
209 *offset += 1 + version.len;
210
211 entry = malloc_thing(package_entry_t);
212 entry->name = chunk_clone(name);
213 entry->version = chunk_clone(version);
214 this->packages->insert_last(this->packages, entry);
215 }
216
217 if (count != this->packages->get_count(this->packages))
218 {
219 DBG1(DBG_TNC, "IETF installed package count unequal to "
220 "number of included packages");
221 goto end;
222 }
223 status = SUCCESS;
224
225 end:
226 reader->destroy(reader);
227 return status;
228 }
229
230 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
231 private_ietf_attr_installed_packages_t *this)
232 {
233 ref_get(&this->ref);
234 return &this->public.pa_tnc_attribute;
235 }
236
237 METHOD(pa_tnc_attr_t, destroy, void,
238 private_ietf_attr_installed_packages_t *this)
239 {
240 if (ref_put(&this->ref))
241 {
242 this->packages->destroy_function(this->packages, (void*)free_package_entry);
243 free(this->value.ptr);
244 free(this);
245 }
246 }
247
248 METHOD(ietf_attr_installed_packages_t, add, void,
249 private_ietf_attr_installed_packages_t *this, chunk_t name, chunk_t version)
250 {
251 package_entry_t *entry;
252
253 /* restrict package name and package version number fields to 255 octets */
254 name.len = min(255, name.len);
255 version.len = min(255, version.len);
256
257 entry = malloc_thing(package_entry_t);
258 entry->name = chunk_clone(name);
259 entry->version = chunk_clone(version);
260 this->packages->insert_last(this->packages, entry);
261 }
262
263 /**
264 * Enumerate package filter entries
265 */
266 static bool package_filter(void *null, package_entry_t **entry, chunk_t *name,
267 void *i2, chunk_t *version)
268 {
269 *name = (*entry)->name;
270 *version = (*entry)->version;
271 return TRUE;
272 }
273
274 METHOD(ietf_attr_installed_packages_t, create_enumerator, enumerator_t*,
275 private_ietf_attr_installed_packages_t *this)
276 {
277 return enumerator_create_filter(
278 this->packages->create_enumerator(this->packages),
279 (void*)package_filter, NULL, NULL);
280 }
281
282 /**
283 * Described in header.
284 */
285 pa_tnc_attr_t *ietf_attr_installed_packages_create(void)
286 {
287 private_ietf_attr_installed_packages_t *this;
288
289 INIT(this,
290 .public = {
291 .pa_tnc_attribute = {
292 .get_type = _get_type,
293 .get_value = _get_value,
294 .get_noskip_flag = _get_noskip_flag,
295 .set_noskip_flag = _set_noskip_flag,
296 .build = _build,
297 .process = _process,
298 .get_ref = _get_ref,
299 .destroy = _destroy,
300 },
301 .add = _add,
302 .create_enumerator = _create_enumerator,
303 },
304 .type = { PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
305 .packages = linked_list_create(),
306 .ref = 1,
307 );
308
309 return &this->public.pa_tnc_attribute;
310 }
311
312 /**
313 * Described in header. .length = length,
314
315 */
316 pa_tnc_attr_t *ietf_attr_installed_packages_create_from_data(size_t length,
317 chunk_t data)
318 {
319 private_ietf_attr_installed_packages_t *this;
320
321 INIT(this,
322 .public = {
323 .pa_tnc_attribute = {
324 .get_type = _get_type,
325 .get_value = _get_value,
326 .get_noskip_flag = _get_noskip_flag,
327 .set_noskip_flag = _set_noskip_flag,
328 .build = _build,
329 .process = _process,
330 .get_ref = _get_ref,
331 .destroy = _destroy,
332 },
333 .add = _add,
334 .create_enumerator = _create_enumerator,
335 },
336 .type = {PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
337 .length = length,
338 .value = chunk_clone(data),
339 .packages = linked_list_create(),
340 .ref = 1,
341 );
342
343 return &this->public.pa_tnc_attribute;
344 }
345
346