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
|
/*
* Copyright (C) 2013-2014 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 "swid_inventory.h"
#include "swid_tag.h"
#include "swid_tag_id.h"
#include <collections/linked_list.h>
#include <bio/bio_writer.h>
#include <utils/debug.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <libgen.h>
#include <errno.h>
typedef struct private_swid_inventory_t private_swid_inventory_t;
/**
* Private data of a swid_inventory_t object.
*
*/
struct private_swid_inventory_t {
/**
* Public swid_inventory_t interface.
*/
swid_inventory_t public;
/**
* Full SWID tags or just SWID tag IDs
*/
bool full_tags;
/**
* List of SWID tags or tag IDs
*/
linked_list_t *list;
};
/**
* Read SWID tags issued by the swid_generator tool
*/
static status_t read_swid_tags(private_swid_inventory_t *this, FILE *file)
{
swid_tag_t *tag;
bio_writer_t *writer;
chunk_t tag_encoding, tag_file_path = chunk_empty;
bool more_tags = TRUE, last_newline;
char line[8192];
size_t len;
while (more_tags)
{
last_newline = TRUE;
writer = bio_writer_create(512);
while (TRUE)
{
if (!fgets(line, sizeof(line), file))
{
more_tags = FALSE;
break;
}
len = strlen(line);
if (last_newline && line[0] == '\n')
{
break;
}
else
{
last_newline = (line[len-1] == '\n');
writer->write_data(writer, chunk_create(line, len));
}
}
tag_encoding = writer->get_buf(writer);
if (tag_encoding.len > 1)
{
/* remove trailing newline if present */
if (tag_encoding.ptr[tag_encoding.len - 1] == '\n')
{
tag_encoding.len--;
}
DBG3(DBG_IMC, " %.*s", tag_encoding.len, tag_encoding.ptr);
tag = swid_tag_create(tag_encoding, tag_file_path);
this->list->insert_last(this->list, tag);
}
writer->destroy(writer);
}
return SUCCESS;
}
/**
* Read SWID tag or software IDs issued by the swid_generator tool
*/
static status_t read_swid_tag_ids(private_swid_inventory_t *this, FILE *file)
{
swid_tag_id_t *tag_id;
chunk_t tag_creator, unique_sw_id, tag_file_path = chunk_empty;
char line[BUF_LEN];
while (TRUE)
{
char *separator;
size_t len;
if (!fgets(line, sizeof(line), file))
{
return SUCCESS;
}
len = strlen(line);
/* remove trailing newline if present */
if (len > 0 && line[len - 1] == '\n')
{
len--;
}
DBG3(DBG_IMC, " %.*s", len, line);
separator = strchr(line, '_');
if (!separator)
{
DBG1(DBG_IMC, "separation of regid from unique software ID failed");
return FAILED;
}
tag_creator = chunk_create(line, separator - line);
separator++;
unique_sw_id = chunk_create(separator, len - (separator - line));
tag_id = swid_tag_id_create(tag_creator, unique_sw_id, tag_file_path);
this->list->insert_last(this->list, tag_id);
}
}
static status_t generate_tags(private_swid_inventory_t *this, char *generator,
swid_inventory_t *targets, bool pretty, bool full)
{
FILE *file;
char command[BUF_LEN];
char doc_separator[] = "'\n\n'";
status_t status = SUCCESS;
if (targets->get_count(targets) == 0)
{
/* Assemble the SWID generator command */
if (this->full_tags)
{
snprintf(command, BUF_LEN, "%s swid --doc-separator %s%s%s",
generator, doc_separator, pretty ? " --pretty" : "",
full ? " --full" : "");
}
else
{
snprintf(command, BUF_LEN, "%s software-id", generator);
}
/* Open a pipe stream for reading the SWID generator output */
file = popen(command, "r");
if (!file)
{
DBG1(DBG_IMC, "failed to run swid_generator command");
return NOT_SUPPORTED;
}
if (this->full_tags)
{
DBG2(DBG_IMC, "SWID tag generation by package manager");
status = read_swid_tags(this, file);
}
else
{
DBG2(DBG_IMC, "SWID tag ID generation by package manager");
status = read_swid_tag_ids(this, file);
}
pclose(file);
}
else if (this->full_tags)
{
swid_tag_id_t *tag_id;
enumerator_t *enumerator;
enumerator = targets->create_enumerator(targets);
while (enumerator->enumerate(enumerator, &tag_id))
{
char software_id[BUF_LEN];
chunk_t tag_creator, unique_sw_id;
tag_creator = tag_id->get_tag_creator(tag_id);
unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
snprintf(software_id, BUF_LEN, "%.*s_%.*s",
tag_creator.len, tag_creator.ptr,
unique_sw_id.len, unique_sw_id.ptr);
/* Assemble the SWID generator command */
snprintf(command, BUF_LEN, "%s swid --software-id %s%s%s",
generator, software_id, pretty ? " --pretty" : "",
full ? " --full" : "");
/* Open a pipe stream for reading the SWID generator output */
file = popen(command, "r");
if (!file)
{
DBG1(DBG_IMC, "failed to run swid_generator command");
return NOT_SUPPORTED;
}
status = read_swid_tags(this, file);
pclose(file);
if (status != SUCCESS)
{
break;
}
}
enumerator->destroy(enumerator);
}
return status;
}
static bool collect_tags(private_swid_inventory_t *this, char *pathname,
swid_inventory_t *targets)
{
char *rel_name, *abs_name;
struct stat st;
bool success = FALSE;
enumerator_t *enumerator;
enumerator = enumerator_create_directory(pathname);
if (!enumerator)
{
DBG1(DBG_IMC, "directory '%s' can not be opened, %s",
pathname, strerror(errno));
return FALSE;
}
DBG2(DBG_IMC, "entering %s", pathname);
while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
{
char * start, *stop;
chunk_t tag_creator;
chunk_t unique_sw_id = chunk_empty, tag_file_path = chunk_empty;
if (!strstr(rel_name, "regid."))
{
continue;
}
if (S_ISDIR(st.st_mode))
{
/* In case of a targeted request */
if (targets->get_count(targets))
{
enumerator_t *target_enumerator;
swid_tag_id_t *tag_id;
bool match = FALSE;
target_enumerator = targets->create_enumerator(targets);
while (target_enumerator->enumerate(target_enumerator, &tag_id))
{
if (chunk_equals(tag_id->get_tag_creator(tag_id),
chunk_from_str(rel_name)))
{
match = TRUE;
break;
}
}
target_enumerator->destroy(target_enumerator);
if (!match)
{
continue;
}
}
if (!collect_tags(this, abs_name, targets))
{
goto end;
}
continue;
}
/* parse the regid filename into its components */
start = rel_name;
stop = strchr(start, '_');
if (!stop)
{
DBG1(DBG_IMC, " %s", rel_name);
DBG1(DBG_IMC, " '_' separator not found");
goto end;
}
tag_creator = chunk_create(start, stop-start);
start = stop + 1;
stop = strstr(start, ".swidtag");
if (!stop)
{
DBG1(DBG_IMC, " %s", rel_name);
DBG1(DBG_IMC, " swidtag postfix not found");
goto end;
}
unique_sw_id = chunk_create(start, stop-start);
tag_file_path = chunk_from_str(abs_name);
/* In case of a targeted request */
if (targets->get_count(targets))
{
chunk_t target_unique_sw_id, target_tag_creator;
enumerator_t *target_enumerator;
swid_tag_id_t *tag_id;
bool match = FALSE;
target_enumerator = targets->create_enumerator(targets);
while (target_enumerator->enumerate(target_enumerator, &tag_id))
{
target_unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
target_tag_creator = tag_id->get_tag_creator(tag_id);
if (chunk_equals(target_unique_sw_id, unique_sw_id) &&
chunk_equals(target_tag_creator, tag_creator))
{
match = TRUE;
break;
}
}
target_enumerator->destroy(target_enumerator);
if (!match)
{
continue;
}
}
DBG2(DBG_IMC, " %s", rel_name);
if (this->full_tags)
{
swid_tag_t *tag;
chunk_t *xml_tag;
xml_tag = chunk_map(abs_name, FALSE);
if (!xml_tag)
{
DBG1(DBG_IMC, " opening '%s' failed: %s", abs_name,
strerror(errno));
goto end;
}
tag = swid_tag_create(*xml_tag, tag_file_path);
this->list->insert_last(this->list, tag);
chunk_unmap(xml_tag);
}
else
{
swid_tag_id_t *tag_id;
tag_id = swid_tag_id_create(tag_creator, unique_sw_id, tag_file_path);
this->list->insert_last(this->list, tag_id);
}
}
success = TRUE;
end:
enumerator->destroy(enumerator);
DBG2(DBG_IMC, "leaving %s", pathname);
return success;
}
METHOD(swid_inventory_t, collect, bool,
private_swid_inventory_t *this, char *directory, char *generator,
swid_inventory_t *targets, bool pretty, bool full)
{
/**
* Tags are generated by a package manager
*/
generate_tags(this, generator, targets, pretty, full);
/**
* Collect swidtag files by iteratively entering all directories in
* the tree under the "directory" path.
*/
return collect_tags(this, directory, targets);
}
METHOD(swid_inventory_t, add, void,
private_swid_inventory_t *this, void *item)
{
this->list->insert_last(this->list, item);
}
METHOD(swid_inventory_t, get_count, int,
private_swid_inventory_t *this)
{
return this->list->get_count(this->list);
}
METHOD(swid_inventory_t, create_enumerator, enumerator_t*,
private_swid_inventory_t *this)
{
return this->list->create_enumerator(this->list);
}
METHOD(swid_inventory_t, destroy, void,
private_swid_inventory_t *this)
{
if (this->full_tags)
{
this->list->destroy_offset(this->list, offsetof(swid_tag_t, destroy));
}
else
{
this->list->destroy_offset(this->list, offsetof(swid_tag_id_t, destroy));
}
free(this);
}
/**
* See header
*/
swid_inventory_t *swid_inventory_create(bool full_tags)
{
private_swid_inventory_t *this;
INIT(this,
.public = {
.collect = _collect,
.add = _add,
.get_count = _get_count,
.create_enumerator = _create_enumerator,
.destroy = _destroy,
},
.full_tags = full_tags,
.list = linked_list_create(),
);
return &this->public;
}
|