2c61d7254abb756941d604034d60744753b26a4f
[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, add_segment, void,
231 private_ietf_attr_installed_packages_t *this, chunk_t segment)
232 {
233 this->value = chunk_cat("mc", this->value, segment);
234 }
235
236 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
237 private_ietf_attr_installed_packages_t *this)
238 {
239 ref_get(&this->ref);
240 return &this->public.pa_tnc_attribute;
241 }
242
243 METHOD(pa_tnc_attr_t, destroy, void,
244 private_ietf_attr_installed_packages_t *this)
245 {
246 if (ref_put(&this->ref))
247 {
248 this->packages->destroy_function(this->packages, (void*)free_package_entry);
249 free(this->value.ptr);
250 free(this);
251 }
252 }
253
254 METHOD(ietf_attr_installed_packages_t, add, void,
255 private_ietf_attr_installed_packages_t *this, chunk_t name, chunk_t version)
256 {
257 package_entry_t *entry;
258
259 /* restrict package name and package version number fields to 255 octets */
260 name.len = min(255, name.len);
261 version.len = min(255, version.len);
262
263 entry = malloc_thing(package_entry_t);
264 entry->name = chunk_clone(name);
265 entry->version = chunk_clone(version);
266 this->packages->insert_last(this->packages, entry);
267 }
268
269 /**
270 * Enumerate package filter entries
271 */
272 static bool package_filter(void *null, package_entry_t **entry, chunk_t *name,
273 void *i2, chunk_t *version)
274 {
275 *name = (*entry)->name;
276 *version = (*entry)->version;
277 return TRUE;
278 }
279
280 METHOD(ietf_attr_installed_packages_t, create_enumerator, enumerator_t*,
281 private_ietf_attr_installed_packages_t *this)
282 {
283 return enumerator_create_filter(
284 this->packages->create_enumerator(this->packages),
285 (void*)package_filter, NULL, NULL);
286 }
287
288 /**
289 * Described in header.
290 */
291 pa_tnc_attr_t *ietf_attr_installed_packages_create(void)
292 {
293 private_ietf_attr_installed_packages_t *this;
294
295 INIT(this,
296 .public = {
297 .pa_tnc_attribute = {
298 .get_type = _get_type,
299 .get_value = _get_value,
300 .get_noskip_flag = _get_noskip_flag,
301 .set_noskip_flag = _set_noskip_flag,
302 .build = _build,
303 .process = _process,
304 .add_segment = _add_segment,
305 .get_ref = _get_ref,
306 .destroy = _destroy,
307 },
308 .add = _add,
309 .create_enumerator = _create_enumerator,
310 },
311 .type = { PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
312 .packages = linked_list_create(),
313 .ref = 1,
314 );
315
316 return &this->public.pa_tnc_attribute;
317 }
318
319 /**
320 * Described in header. .length = length,
321
322 */
323 pa_tnc_attr_t *ietf_attr_installed_packages_create_from_data(size_t length,
324 chunk_t data)
325 {
326 private_ietf_attr_installed_packages_t *this;
327
328 INIT(this,
329 .public = {
330 .pa_tnc_attribute = {
331 .get_type = _get_type,
332 .get_value = _get_value,
333 .get_noskip_flag = _get_noskip_flag,
334 .set_noskip_flag = _set_noskip_flag,
335 .build = _build,
336 .process = _process,
337 .add_segment = _add_segment,
338 .get_ref = _get_ref,
339 .destroy = _destroy,
340 },
341 .add = _add,
342 .create_enumerator = _create_enumerator,
343 },
344 .type = {PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
345 .length = length,
346 .value = chunk_clone(data),
347 .packages = linked_list_create(),
348 .ref = 1,
349 );
350
351 return &this->public.pa_tnc_attribute;
352 }
353
354