Use python-based swidGenerator to generated SWID tags
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Andreas Steffen
|
||||
* Copyright (C) 2013-2014 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "swid_tag_id.h"
|
||||
|
||||
#include <collections/linked_list.h>
|
||||
#include <bio/bio_writer.h>
|
||||
#include <utils/debug.h>
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -51,6 +52,123 @@ struct private_swid_inventory_t {
|
||||
linked_list_t *list;
|
||||
};
|
||||
|
||||
static status_t generate_tags(private_swid_inventory_t *this, char *generator,
|
||||
swid_inventory_t *targets, bool pretty, bool full)
|
||||
{
|
||||
FILE *file;
|
||||
char command[512], line[2048];
|
||||
chunk_t tag_creator, unique_sw_id, tag_file_path = chunk_empty;
|
||||
swid_tag_id_t *tag_id;
|
||||
swid_tag_t *tag;
|
||||
status_t status = SUCCESS;
|
||||
|
||||
/* Assemble the SWID generator command */
|
||||
snprintf(command, sizeof(command), "%s %s%s%s\n", generator,
|
||||
(this->full_tags) ? "swid" : "software-id",
|
||||
(this->full_tags && pretty) ? " --pretty" : "",
|
||||
(this->full_tags && full) ? " --full" : "");
|
||||
|
||||
/* Open a pipe stream for reading the output of the dpkg-query commmand */
|
||||
file = popen(command, "r");
|
||||
if (!file)
|
||||
{
|
||||
DBG1(DBG_IMC, "failed to run swid_generator command");
|
||||
return NOT_SUPPORTED;
|
||||
}
|
||||
if (this->full_tags)
|
||||
{
|
||||
bio_writer_t *writer;
|
||||
chunk_t tag_encoding;
|
||||
bool more_tags = TRUE, end_of_tag;
|
||||
|
||||
DBG2(DBG_IMC, "SWID tags generated by package manager:");
|
||||
while (more_tags)
|
||||
{
|
||||
end_of_tag = FALSE;
|
||||
writer = bio_writer_create(512);
|
||||
do
|
||||
{
|
||||
if (fgets(line, sizeof(line), file) <= 0)
|
||||
{
|
||||
more_tags = FALSE;
|
||||
end_of_tag = TRUE;
|
||||
break;
|
||||
}
|
||||
if (line[0] == '\n')
|
||||
{
|
||||
end_of_tag = TRUE;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
writer->write_data(writer, chunk_from_str(line));
|
||||
}
|
||||
}
|
||||
while (!end_of_tag);
|
||||
|
||||
tag_encoding = writer->get_buf(writer);
|
||||
|
||||
/* remove trailing newline if present */
|
||||
if (tag_encoding.len > 0 &&
|
||||
tag_encoding.ptr[tag_encoding.len - 1] == '\n')
|
||||
{
|
||||
tag_encoding.len--;
|
||||
}
|
||||
DBG2(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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG2(DBG_IMC, "SWID tag IDs generated by package manager:");
|
||||
while (TRUE)
|
||||
{
|
||||
char *separator;
|
||||
size_t len;
|
||||
|
||||
if (fgets(line, sizeof(line), file) <= 0)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
len = strlen(line);
|
||||
|
||||
/* remove trailing newline if present */
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
{
|
||||
len--;
|
||||
}
|
||||
DBG2(DBG_IMC, " %.*s", len, line);
|
||||
|
||||
separator = strchr(line, '_');
|
||||
if (!separator)
|
||||
{
|
||||
DBG1(DBG_IMC, "separatation of regid from unique software ID "
|
||||
"failed");
|
||||
status = FAILED;
|
||||
goto end;
|
||||
}
|
||||
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);
|
||||
|
||||
if (fgets(line, sizeof(line), file) <= 0)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
pclose(file);
|
||||
return status;
|
||||
}
|
||||
|
||||
static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
swid_inventory_t *targets)
|
||||
{
|
||||
@@ -72,7 +190,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
{
|
||||
char * start, *stop;
|
||||
chunk_t tag_creator;
|
||||
chunk_t unique_sw_id = chunk_empty, unique_seq_id = chunk_empty;
|
||||
chunk_t unique_sw_id = chunk_empty, tag_file_path = chunk_empty;
|
||||
if (!strstr(rel_name, "regid."))
|
||||
{
|
||||
continue;
|
||||
@@ -121,14 +239,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
goto end;
|
||||
}
|
||||
tag_creator = chunk_create(start, stop-start);
|
||||
|
||||
start = stop + 1;
|
||||
stop = strchr(start, '_');
|
||||
if (stop)
|
||||
{
|
||||
unique_sw_id = chunk_create(start, stop-start);
|
||||
start = stop + 1;
|
||||
}
|
||||
|
||||
stop = strstr(start, ".swidtag");
|
||||
if (!stop)
|
||||
@@ -137,14 +248,8 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
DBG1(DBG_IMC, " swidtag postfix not found");
|
||||
goto end;
|
||||
}
|
||||
if (unique_sw_id.ptr)
|
||||
{
|
||||
unique_seq_id = chunk_create(start, stop-start);
|
||||
}
|
||||
else
|
||||
{
|
||||
unique_sw_id = chunk_create(start, stop-start);
|
||||
}
|
||||
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))
|
||||
@@ -187,7 +292,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
goto end;
|
||||
}
|
||||
|
||||
tag = swid_tag_create(*xml_tag, unique_seq_id);
|
||||
tag = swid_tag_create(*xml_tag, tag_file_path);
|
||||
this->list->insert_last(this->list, tag);
|
||||
chunk_unmap(xml_tag);
|
||||
}
|
||||
@@ -195,10 +300,9 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
|
||||
{
|
||||
swid_tag_id_t *tag_id;
|
||||
|
||||
tag_id = swid_tag_id_create(tag_creator, unique_sw_id, unique_seq_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;
|
||||
|
||||
@@ -210,8 +314,18 @@ end:
|
||||
}
|
||||
|
||||
METHOD(swid_inventory_t, collect, bool,
|
||||
private_swid_inventory_t *this, char *directory, swid_inventory_t *targets)
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user