sw-collector: Moved info class to libimcv

This commit is contained in:
Andreas Steffen
2017-08-09 13:28:00 +02:00
parent f237bfcb9e
commit e658fd475a
8 changed files with 72 additions and 77 deletions
-1
View File
@@ -5,7 +5,6 @@ sw_collector_SOURCES = \
sw_collector_db.h sw_collector_db.c \
sw_collector_dpkg.h sw_collector_dpkg.c \
sw_collector_history.h sw_collector_history.c \
sw_collector_info.h sw_collector_info.c \
sw_collector_rest_api.h sw_collector_rest_api.c
sw-collector.o : $(top_builddir)/config.status
+14 -30
View File
@@ -23,7 +23,6 @@
# include <syslog.h>
#endif
#include "sw_collector_info.h"
#include "sw_collector_db.h"
#include "sw_collector_history.h"
#include "sw_collector_rest_api.h"
@@ -33,7 +32,6 @@
#include <utils/debug.h>
#include <utils/lexparser.h>
#include <imv/imv_os_info.h>
#include <swid_gen/swid_gen.h>
/**
@@ -221,26 +219,15 @@ static collector_op_t do_args(int argc, char *argv[], bool *full_tags,
/**
* Extract software events from apt history log files
*/
static int extract_history(sw_collector_info_t *info, sw_collector_db_t *db)
static int extract_history(sw_collector_db_t *db)
{
sw_collector_history_t *history = NULL;
uint32_t epoch, last_eid, eid = 0;
char *history_path, *os, *last_time = NULL, rfc_time[21];
char *history_path, *last_time = NULL, rfc_time[21];
chunk_t *h, history_chunk, line, cmd;
os_type_t os_type;
int status = EXIT_FAILURE;
bool skip = TRUE;
/* check if OS supports apg/dpkg history logs */
info->get_os(info, &os);
os_type = info->get_os_type(info);
if (os_type != OS_TYPE_DEBIAN && os_type != OS_TYPE_UBUNTU)
{
DBG1(DBG_IMC, "%.*s not supported", os);
return EXIT_FAILURE;
}
/* open history file for reading */
history_path = lib->settings->get_str(lib->settings, "%s.history", NULL,
lib->ns);
@@ -259,7 +246,11 @@ static int extract_history(sw_collector_info_t *info, sw_collector_db_t *db)
history_chunk = *h;
/* Instantiate history extractor */
history = sw_collector_history_create(info, db, 1);
history = sw_collector_history_create(db, 1);
if (!history)
{
return EXIT_FAILURE;
}
/* retrieve last event in database */
if (!db->get_last_event(db, &last_eid, &epoch, &last_time) || !last_eid)
@@ -472,8 +463,8 @@ static int unregistered_identifiers(sw_collector_db_t *db,
* Generate ISO 19770-2:2015 SWID tags for [installed|removed|all]
* SW identifiers that are not registered centrally
*/
static int generate_tags(sw_collector_info_t *info, sw_collector_db_t *db,
bool full_tags, sw_collector_db_query_t type)
static int generate_tags(sw_collector_db_t *db, bool full_tags,
sw_collector_db_query_t type)
{
swid_gen_t * swid_gen;
sw_collector_rest_api_t *rest_api;
@@ -547,7 +538,7 @@ end:
/**
* Append missing architecture suffix to package entries in the database
*/
static int migrate(sw_collector_info_t *info, sw_collector_db_t *db)
static int migrate(sw_collector_db_t *db)
{
sw_collector_dpkg_t *dpkg;
@@ -595,10 +586,9 @@ int main(int argc, char *argv[])
{
sw_collector_db_t *db = NULL;
sw_collector_db_query_t query_type;
sw_collector_info_t *info;
collector_op_t op;
bool full_tags;
char *uri, *tag_creator;
char *uri;
int status = EXIT_FAILURE;
op = do_args(argc, argv, &full_tags, &query_type);
@@ -638,15 +628,10 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
/* Attach OS info */
tag_creator = lib->settings->get_str(lib->settings, "%s.tag_creator.regid",
"strongswan.org", lib->ns);
info = sw_collector_info_create(tag_creator);
switch (op)
{
case COLLECTOR_OP_EXTRACT:
status = extract_history(info, db);
status = extract_history(db);
break;
case COLLECTOR_OP_LIST:
status = list_identifiers(db, query_type);
@@ -655,14 +640,13 @@ int main(int argc, char *argv[])
status = unregistered_identifiers(db, query_type);
break;
case COLLECTOR_OP_GENERATE:
status = generate_tags(info, db, full_tags, query_type);
status = generate_tags(db, full_tags, query_type);
break;
case COLLECTOR_OP_MIGRATE:
status = migrate(info, db);
status = migrate(db);
break;
}
db->destroy(db);
info->destroy(info);
exit(status);
}
+22 -6
View File
@@ -20,7 +20,8 @@
#include "sw_collector_history.h"
#include "sw_collector_dpkg.h"
#include "swima/swima_event.h"
#include <swima/swima_event.h>
#include <swid_gen/swid_gen_info.h>
typedef struct private_sw_collector_history_t private_sw_collector_history_t;
@@ -42,7 +43,7 @@ struct private_sw_collector_history_t {
/**
* Reference to OS info object
*/
sw_collector_info_t *info;
swid_gen_info_t *info;
/**
* Reference to collector database
@@ -67,7 +68,7 @@ struct package_t {
/**
* Create package_t list item object
*/
static package_t* create_package(sw_collector_info_t *info, chunk_t package,
static package_t* create_package(swid_gen_info_t *info, chunk_t package,
chunk_t version, chunk_t old_version)
{
package_t *this;
@@ -107,7 +108,7 @@ static void free_package(package_t *this)
/**
* Extract and parse a single package item
*/
static package_t* extract_package(chunk_t item, sw_collector_info_t *info,
static package_t* extract_package(chunk_t item, swid_gen_info_t *info,
sw_collector_history_op_t op)
{
chunk_t package, package_stripped, version, old_version;
@@ -475,17 +476,32 @@ end:
METHOD(sw_collector_history_t, destroy, void,
private_sw_collector_history_t *this)
{
this->info->destroy(this->info);
free(this);
}
/**
* Described in header.
*/
sw_collector_history_t *sw_collector_history_create(sw_collector_info_t *info,
sw_collector_db_t *db,
sw_collector_history_t *sw_collector_history_create(sw_collector_db_t *db,
uint8_t source)
{
private_sw_collector_history_t *this;
swid_gen_info_t *info;
os_type_t os_type;
char *os;
info = swid_gen_info_create();
/* check if OS supports apg/dpkg history logs */
info->get_os(info, &os);
os_type = info->get_os_type(info);
if (os_type != OS_TYPE_DEBIAN && os_type != OS_TYPE_UBUNTU)
{
DBG1(DBG_IMC, "%.*s not supported", os);
info->destroy(info);
return NULL;
}
INIT(this,
.public = {
+1 -5
View File
@@ -21,8 +21,6 @@
#ifndef SW_COLLECTOR_HISTORY_H_
#define SW_COLLECTOR_HISTORY_H_
#include "sw_collector_history.h"
#include "sw_collector_info.h"
#include "sw_collector_db.h"
#include <library.h>
@@ -84,12 +82,10 @@ struct sw_collector_history_t {
/**
* Create an sw_collector_history_t instance
*
* @param info Internal reference to collector info
* @param db Internal reference to collector database
* @param source Software event source number
*/
sw_collector_history_t* sw_collector_history_create(sw_collector_info_t *info,
sw_collector_db_t *db,
sw_collector_history_t* sw_collector_history_create(sw_collector_db_t *db,
uint8_t source);
#endif /** SW_COLLECTOR_HISTORY_H_ @}*/
-170
View File
@@ -1,170 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include "sw_collector_info.h"
#include <library.h>
#include <utils/lexparser.h>
typedef struct private_sw_collector_info_t private_sw_collector_info_t;
/**
* Private data of an sw_collector_info_t object.
*/
struct private_sw_collector_info_t {
/**
* Public members of sw_collector_info_state_t
*/
sw_collector_info_t public;
/**
* tagCreator
*/
char *tag_creator;
/**
* OS string 'Name_Version-Arch'
*/
char *os;
/**
* Product string 'Name Version Arch'
*/
char *product;
/**
* OS info about endpoint
*/
imc_os_info_t *os_info;
};
/**
* Replaces invalid character by a valid one
*/
static void sanitize_uri(char *uri, char a, char b)
{
char *pos = uri;
while (TRUE)
{
pos = strchr(pos, a);
if (!pos)
{
break;
}
*pos = b;
pos++;
}
}
METHOD(sw_collector_info_t, get_os_type, os_type_t,
private_sw_collector_info_t *this)
{
return this->os_info->get_type(this->os_info);
}
METHOD(sw_collector_info_t, get_os, char*,
private_sw_collector_info_t *this, char **product)
{
if (product)
{
*product = this->product;
}
return this->os;
}
METHOD(sw_collector_info_t, create_sw_id, char*,
private_sw_collector_info_t *this, char *package, char *version)
{
char *sw_id;
if (asprintf(&sw_id, "%s__%s-%s%s%s", this->tag_creator, this->os,
package, strlen(version) ? "-" : "", version) == -1)
{
return NULL;
}
sanitize_uri(sw_id, ':', '~');
sanitize_uri(sw_id, '+', '~');
return sw_id;
}
METHOD(sw_collector_info_t, destroy, void,
private_sw_collector_info_t *this)
{
this->os_info->destroy(this->os_info);
free(this->os);
free(this->product);
free(this->tag_creator);
free(this);
}
/**
* Described in header.
*/
sw_collector_info_t *sw_collector_info_create(char *tag_creator)
{
private_sw_collector_info_t *this;
chunk_t os_name, os_version, os_arch;
INIT(this,
.public = {
.get_os_type = _get_os_type,
.get_os = _get_os,
.create_sw_id = _create_sw_id,
.destroy = _destroy,
},
.os_info = imc_os_info_create(),
.tag_creator = strdup(tag_creator),
);
os_name = this->os_info->get_name(this->os_info);
os_arch = this->os_info->get_version(this->os_info);
/* get_version() returns version followed by arch */
if (!extract_token(&os_version, ' ', &os_arch))
{
DBG1(DBG_IMC, "separation of OS version from arch failed");
destroy(this);
return NULL;
}
/* construct OS string */
if (asprintf(&this->os, "%.*s_%.*s-%.*s", (int)os_name.len, os_name.ptr,
(int)os_version.len, os_version.ptr, (int)os_arch.len,
os_arch.ptr) == -1)
{
DBG1(DBG_IMC, "constructon of OS string failed");
destroy(this);
return NULL;
}
/* construct product string */
if (asprintf(&this->product, "%.*s %.*s %.*s", (int)os_name.len,
os_name.ptr, (int)os_version.len, os_version.ptr,
(int)os_arch.len, os_arch.ptr) == -1)
{
DBG1(DBG_IMC, "constructon of product string failed");
destroy(this);
return NULL;
}
return &this->public;
}
-71
View File
@@ -1,71 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
/**
* @defgroup sw_collector sw-collector
*
* @defgroup sw_collector_info_t sw_collector_info
* @{ @ingroup sw_collector
*/
#ifndef SW_COLLECTOR_INFO_H_
#define SW_COLLECTOR_INFO_H_
typedef struct sw_collector_info_t sw_collector_info_t;
#include "imc/imc_os_info.h"
struct sw_collector_info_t {
/**
* Get OS type
*
* @return OS type
*/
os_type_t (*get_os_type)(sw_collector_info_t *this);
/**
* Get OS and product strings
*
* @param product Product string 'Name Version Arch'
* @return OS string 'Name_Version-Arch'
*/
char* (*get_os)(sw_collector_info_t *this, char **product);
/**
* Create software identifier including tagCreator and OS
*
* @param package Package string
* @param version Version string
* @return Software Identifier string
*/
char* (*create_sw_id)(sw_collector_info_t *this, char *package,
char *version);
/**
* Destroy sw_collector_info_t object
*/
void (*destroy)(sw_collector_info_t *this);
};
/**
* Create an sw_collector_info_t instance
*
* @param tag_creator Regid of tagCreator
*/
sw_collector_info_t* sw_collector_info_create(char *tag_creator);
#endif /** SW_COLLECTOR_INFO_H_ @}*/