Support targeted retrieval of SWID tags

This commit is contained in:
Andreas Steffen
2014-05-31 20:37:56 +02:00
parent e14507cb71
commit b7679e90e3
+111 -40
View File
@@ -52,39 +52,17 @@ struct private_swid_inventory_t {
linked_list_t *list; linked_list_t *list;
}; };
static status_t generate_tags(private_swid_inventory_t *this, char *generator, /**
swid_inventory_t *targets, bool pretty, bool full) * Read SWID tags issued by the swid_generator tool
*/
static status_t read_swid_tags(private_swid_inventory_t *this, FILE *file)
{ {
FILE *file;
char command[512], line[2048];
char entity_name = "strongSwan Project";
chunk_t tag_creator, unique_sw_id, tag_file_path = chunk_empty;
swid_tag_id_t *tag_id;
swid_tag_t *tag; swid_tag_t *tag;
status_t status = SUCCESS;
/* Assemble the SWID generator command */
snprintf(command, sizeof(command), "%s %s %s%s%s%s\n", generator,
(this->full_tags) ? "swid" : "software-id",
(this->full_tags && entity_name) ? entity_name : "",
(this->full_tags && pretty) ? " --pretty" : "",
(this->full_tags && !pretty) ? " --doc-separator $'\n\n'" : "",
(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; bio_writer_t *writer;
chunk_t tag_encoding; chunk_t tag_encoding, tag_file_path = chunk_empty;
bool more_tags = TRUE, end_of_tag; bool more_tags = TRUE, end_of_tag;
char line[131072];
DBG2(DBG_IMC, "SWID tags generated by package manager:");
while (more_tags) while (more_tags)
{ {
end_of_tag = FALSE; end_of_tag = FALSE;
@@ -125,10 +103,19 @@ static status_t generate_tags(private_swid_inventory_t *this, char *generator,
} }
writer->destroy(writer); writer->destroy(writer);
} }
return SUCCESS;
} }
else
/**
* 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)
{ {
DBG2(DBG_IMC, "SWID tag IDs generated by package manager:"); swid_tag_id_t *tag_id;
chunk_t tag_creator, unique_sw_id, tag_file_path = chunk_empty;
char line[BUF_LEN];
while (TRUE) while (TRUE)
{ {
char *separator; char *separator;
@@ -136,7 +123,7 @@ static status_t generate_tags(private_swid_inventory_t *this, char *generator,
if (fgets(line, sizeof(line), file) <= 0) if (fgets(line, sizeof(line), file) <= 0)
{ {
goto end; return SUCCESS;
} }
len = strlen(line); len = strlen(line);
@@ -150,10 +137,8 @@ static status_t generate_tags(private_swid_inventory_t *this, char *generator,
separator = strchr(line, '_'); separator = strchr(line, '_');
if (!separator) if (!separator)
{ {
DBG1(DBG_IMC, "separation of regid from unique software ID " DBG1(DBG_IMC, "separation of regid from unique software ID failed");
"failed"); return FAILED;
status = FAILED;
goto end;
} }
tag_creator = chunk_create(line, separator - line); tag_creator = chunk_create(line, separator - line);
separator++; separator++;
@@ -164,8 +149,91 @@ static status_t generate_tags(private_swid_inventory_t *this, char *generator,
} }
} }
end: 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 entity_name[] = "strongSwan Project";
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 --entity-name \"%s\" %s%s",
generator, entity_name,
pretty ? "--pretty" : "--doc-separator $'\n\n'",
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 tags generated by package manager:");
status = read_swid_tags(this, file);
}
else
{
DBG2(DBG_IMC, "SWID tag IDs generated by package manager:");
status = read_swid_tag_ids(this, file);
}
pclose(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 "
"--entity-name \"%s\"%s%s",
generator, software_id, entity_name,
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; return status;
} }
@@ -191,6 +259,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
char * start, *stop; char * start, *stop;
chunk_t tag_creator; chunk_t tag_creator;
chunk_t unique_sw_id = chunk_empty, tag_file_path = chunk_empty; chunk_t unique_sw_id = chunk_empty, tag_file_path = chunk_empty;
if (!strstr(rel_name, "regid.")) if (!strstr(rel_name, "regid."))
{ {
continue; continue;
@@ -254,6 +323,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
/* In case of a targeted request */ /* In case of a targeted request */
if (targets->get_count(targets)) if (targets->get_count(targets))
{ {
chunk_t target_unique_sw_id, target_tag_creator;
enumerator_t *target_enumerator; enumerator_t *target_enumerator;
swid_tag_id_t *tag_id; swid_tag_id_t *tag_id;
bool match = FALSE; bool match = FALSE;
@@ -261,10 +331,11 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname,
target_enumerator = targets->create_enumerator(targets); target_enumerator = targets->create_enumerator(targets);
while (target_enumerator->enumerate(target_enumerator, &tag_id)) while (target_enumerator->enumerate(target_enumerator, &tag_id))
{ {
if (chunk_equals(tag_id->get_unique_sw_id(tag_id, NULL), target_unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
unique_sw_id) && target_tag_creator = tag_id->get_tag_creator(tag_id);
chunk_equals(tag_id->get_tag_creator(tag_id),
tag_creator)) if (chunk_equals(target_unique_sw_id, unique_sw_id) &&
chunk_equals(target_tag_creator, tag_creator))
{ {
match = TRUE; match = TRUE;
break; break;