implemented IMV session control
This commit is contained in:
@@ -65,8 +65,6 @@ TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
|
||||
TNC_Version max_version,
|
||||
TNC_Version *actual_version)
|
||||
{
|
||||
char *uri;
|
||||
|
||||
if (imv_os)
|
||||
{
|
||||
DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
|
||||
@@ -84,13 +82,8 @@ TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
|
||||
return TNC_RESULT_NO_COMMON_VERSION;
|
||||
}
|
||||
|
||||
/* attach OS database */
|
||||
uri = lib->settings->get_str(lib->settings,
|
||||
"libimcv.plugins.imv-os.database", NULL);
|
||||
if (uri)
|
||||
{
|
||||
os_db = imv_os_database_create(uri);
|
||||
}
|
||||
/* attach OS database co-located with IMV database */
|
||||
os_db = imv_os_database_create(imv_os->get_database(imv_os));
|
||||
|
||||
return TNC_RESULT_SUCCESS;
|
||||
}
|
||||
@@ -289,7 +282,9 @@ static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
|
||||
case ITA_ATTR_SETTINGS:
|
||||
{
|
||||
ita_attr_settings_t *attr_cast;
|
||||
imv_database_t *imv_db;
|
||||
enumerator_t *e;
|
||||
int did;
|
||||
char *name;
|
||||
chunk_t value;
|
||||
|
||||
@@ -306,8 +301,13 @@ static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
|
||||
else if ((streq(name, android_id_str) ||
|
||||
streq(name, machine_id_str)) && os_db)
|
||||
{
|
||||
os_state->set_device_id(os_state,
|
||||
os_db->get_device_id(os_db, value));
|
||||
imv_db = imv_os->get_database(imv_os);
|
||||
if (imv_db)
|
||||
{
|
||||
did = imv_db->add_device(imv_db,
|
||||
state->get_session_id(state), value);
|
||||
os_state->set_device_id(os_state, did);
|
||||
}
|
||||
}
|
||||
DBG1(DBG_IMV, "setting '%s'\n %.*s",
|
||||
name, value.len, value.ptr);
|
||||
@@ -332,11 +332,19 @@ static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
|
||||
{
|
||||
os_type_t os_type;
|
||||
ita_attr_get_settings_t *attr_cast;
|
||||
imv_database_t *imv_db;
|
||||
|
||||
/* set the OS type, name and version */
|
||||
os_type = os_type_from_name(os_name);
|
||||
os_state->set_info(os_state,os_type, os_name, os_version);
|
||||
|
||||
imv_db = imv_os->get_database(imv_os);
|
||||
if (imv_db)
|
||||
{
|
||||
imv_db->add_product(imv_db, state->get_session_id(state),
|
||||
os_state->get_info(os_state, NULL, NULL, NULL));
|
||||
}
|
||||
|
||||
/* requesting installed packages */
|
||||
os_state->set_package_request(os_state, TRUE);
|
||||
attr = ietf_attr_attr_request_create(PEN_IETF,
|
||||
@@ -376,10 +384,8 @@ static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
|
||||
!os_state->get_angel_count(os_state) &&
|
||||
os_state->get_info(os_state, NULL, NULL, NULL))
|
||||
{
|
||||
int device_id, count, count_update, count_blacklist, count_ok;
|
||||
int count, count_update, count_blacklist, count_ok;
|
||||
u_int os_settings;
|
||||
u_int32_t id_type;
|
||||
chunk_t id_value;
|
||||
|
||||
os_settings = os_state->get_os_settings(os_state);
|
||||
os_state->get_count(os_state, &count, &count_update, &count_blacklist,
|
||||
@@ -389,13 +395,10 @@ static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
|
||||
count_ok, count - count_update - count_blacklist - count_ok);
|
||||
|
||||
/* Store device information in database */
|
||||
device_id = os_state->get_device_id(os_state);
|
||||
id_value = state->get_ar_id(state, &id_type);
|
||||
if (os_db && device_id)
|
||||
if (os_db)
|
||||
{
|
||||
os_db->set_device_info(os_db, device_id, id_type, id_value,
|
||||
os_state->get_info(os_state, NULL, NULL, NULL),
|
||||
count, count_update, count_blacklist, os_settings);
|
||||
os_db->set_device_info(os_db, state->get_session_id(state),
|
||||
count, count_update, count_blacklist, os_settings);
|
||||
}
|
||||
|
||||
if (count_update || count_blacklist || os_settings)
|
||||
@@ -589,6 +592,7 @@ TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
|
||||
return TNC_RESULT_NOT_INITIALIZED;
|
||||
}
|
||||
DESTROY_IF(os_db);
|
||||
os_db = NULL;
|
||||
|
||||
imv_os->destroy(imv_os);
|
||||
imv_os = NULL;
|
||||
|
||||
@@ -187,148 +187,46 @@ METHOD(imv_os_database_t, check_packages, status_t,
|
||||
return status;
|
||||
}
|
||||
|
||||
METHOD(imv_os_database_t, get_device_id, int,
|
||||
private_imv_os_database_t *this, chunk_t value)
|
||||
{
|
||||
enumerator_t *e;
|
||||
int id;
|
||||
|
||||
/* get primary key of device ID */
|
||||
e = this->db->query(this->db, "SELECT id FROM devices WHERE value = ?",
|
||||
DB_BLOB, value, DB_INT);
|
||||
if (!e)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (e->enumerate(e, &id))
|
||||
{
|
||||
/* device ID already exists in database - return primary key */
|
||||
e->destroy(e);
|
||||
return id;
|
||||
}
|
||||
e->destroy(e);
|
||||
|
||||
/* register new device ID in database and return primary key */
|
||||
return (this->db->execute(this->db, &id,
|
||||
"INSERT INTO devices (value) VALUES (?)", DB_BLOB, value) == 1) ?
|
||||
id : 0;
|
||||
}
|
||||
|
||||
METHOD(imv_os_database_t, set_device_info, void,
|
||||
private_imv_os_database_t *this, int device_id, u_int32_t ar_id_type,
|
||||
chunk_t ar_id_value, char *os_info, int count, int count_update,
|
||||
int count_blacklist, u_int flags)
|
||||
private_imv_os_database_t *this, int session_id, int count,
|
||||
int count_update, int count_blacklist, u_int flags)
|
||||
{
|
||||
enumerator_t *e;
|
||||
time_t last_time;
|
||||
int pid = 0, last_pid = 0, iid = 0, last_iid;
|
||||
int last_count_update = 0, last_count_blacklist = 0;
|
||||
u_int last_flags;
|
||||
bool found = FALSE;
|
||||
|
||||
/* get primary key of OS info string if it exists */
|
||||
e = this->db->query(this->db,
|
||||
"SELECT id FROM products WHERE name = ?", DB_TEXT, os_info,
|
||||
DB_INT);
|
||||
if (e)
|
||||
{
|
||||
e->enumerate(e, &pid);
|
||||
e->destroy(e);
|
||||
}
|
||||
|
||||
/* if OS info string has not been found - register it */
|
||||
if (!pid)
|
||||
{
|
||||
this->db->execute(this->db, &pid,
|
||||
"INSERT INTO products (name) VALUES (?)", DB_TEXT, os_info);
|
||||
}
|
||||
|
||||
/* get primary key of AR identity if it exists */
|
||||
e = this->db->query(this->db,
|
||||
"SELECT id FROM identities WHERE type = ? AND data = ?",
|
||||
DB_INT, ar_id_type, DB_BLOB, ar_id_value, DB_INT);
|
||||
if (e)
|
||||
{
|
||||
e->enumerate(e, &iid);
|
||||
e->destroy(e);
|
||||
}
|
||||
|
||||
/* if AR identity has not been found - register it */
|
||||
if (!iid)
|
||||
{
|
||||
this->db->execute(this->db, &iid,
|
||||
"INSERT INTO identities (type, data) VALUES (?, ?)",
|
||||
DB_INT, ar_id_type, DB_BLOB, ar_id_value);
|
||||
}
|
||||
|
||||
/* get latest device info record if it exists */
|
||||
e = this->db->query(this->db,
|
||||
"SELECT time, ar_id, product, count_update, count_blacklist, flags "
|
||||
"FROM device_infos WHERE device = ? ORDER BY time DESC",
|
||||
DB_INT, device_id, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT, DB_UINT);
|
||||
if (e)
|
||||
{
|
||||
found = e->enumerate(e, &last_time, &last_iid, &last_pid,
|
||||
&last_count_update, &last_count_blacklist,
|
||||
&last_flags);
|
||||
e->destroy(e);
|
||||
}
|
||||
if (found && !last_count_update && !last_count_blacklist && !last_flags &&
|
||||
iid == last_iid && pid == last_pid)
|
||||
{
|
||||
/* update device info */
|
||||
this->db->execute(this->db, NULL,
|
||||
"UPDATE device_infos SET time = ?, count = ?, count_update = ?, "
|
||||
"count_blacklist = ?, flags = ? WHERE device = ? AND time = ?",
|
||||
DB_UINT, time(NULL), DB_INT, count, DB_INT, count_update,
|
||||
DB_INT, count_blacklist, DB_UINT, flags,
|
||||
DB_INT, device_id, DB_UINT, last_time);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* insert device info */
|
||||
this->db->execute(this->db, NULL,
|
||||
"INSERT INTO device_infos (device, time, ar_id, product, count, "
|
||||
"count_update, count_blacklist, flags) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
DB_INT, device_id, DB_UINT, time(NULL), DB_INT, iid, DB_INT, pid,
|
||||
DB_INT, count, DB_INT, count_update, DB_INT, count_blacklist,
|
||||
DB_UINT, flags);
|
||||
}
|
||||
this->db->execute(this->db, NULL,
|
||||
"INSERT INTO device_infos (session, count, count_update, "
|
||||
"count_blacklist, flags) VALUES (?, ?, ?, ?, ?)",
|
||||
DB_INT, session_id, DB_INT, count, DB_INT, count_update,
|
||||
DB_INT, count_blacklist, DB_UINT, flags);
|
||||
}
|
||||
|
||||
METHOD(imv_os_database_t, destroy, void,
|
||||
private_imv_os_database_t *this)
|
||||
{
|
||||
this->db->destroy(this->db);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* See header
|
||||
*/
|
||||
imv_os_database_t *imv_os_database_create(char *uri)
|
||||
imv_os_database_t *imv_os_database_create(imv_database_t *imv_db)
|
||||
{
|
||||
private_imv_os_database_t *this;
|
||||
|
||||
if (!imv_db)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
INIT(this,
|
||||
.public = {
|
||||
.check_packages = _check_packages,
|
||||
.get_device_id = _get_device_id,
|
||||
.set_device_info = _set_device_info,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.db = lib->db->create(lib->db, uri),
|
||||
.db = imv_db->get_database(imv_db),
|
||||
);
|
||||
|
||||
if (!this->db)
|
||||
{
|
||||
DBG1(DBG_IMV,
|
||||
"failed to connect to OS database '%s'", uri);
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#define IMV_OS_DATABASE_H_
|
||||
|
||||
#include "imv_os_state.h"
|
||||
#include "imv/imv_database.h"
|
||||
|
||||
#include <library.h>
|
||||
|
||||
@@ -42,32 +43,20 @@ struct imv_os_database_t {
|
||||
enumerator_t *package_enumerator);
|
||||
|
||||
/**
|
||||
* Get the primary database key of the device ID
|
||||
*
|
||||
* @param value Device ID value
|
||||
*/
|
||||
int (*get_device_id)(imv_os_database_t *this, chunk_t value);
|
||||
* Set health infos for a given device
|
||||
*
|
||||
* @param sesson_id Session ID
|
||||
* @param count Number of installed packages
|
||||
* @param count_update Number of packages to be updated
|
||||
* @param count_blacklist Number of blacklisted packages
|
||||
* @param flags Various flags, e.g. illegal OS settings
|
||||
*/
|
||||
void (*set_device_info)(imv_os_database_t *this, int session_id, int count,
|
||||
int count_update, int count_blacklist, u_int flags);
|
||||
|
||||
/**
|
||||
* Set health infos for a given device
|
||||
*
|
||||
* @param device_id Device ID primary key
|
||||
* @param ar_id_type Access Requestor ID Type
|
||||
* @param ar_id_value Access Requestor ID Value
|
||||
* @param os_info OS info string
|
||||
* @param count Number of installed packages
|
||||
* @param count_update Number of packages to be updated
|
||||
* @param count_blacklist Number of blacklisted packages
|
||||
* @param flags Various flags, e.g. illegal OS settings
|
||||
*/
|
||||
void (*set_device_info)(imv_os_database_t *this, int device_id,
|
||||
u_int32_t ar_id_type, chunk_t ar_id_value,
|
||||
char *os_info, int count, int count_update,
|
||||
int count_blacklist, u_int flags);
|
||||
|
||||
/**
|
||||
* Destroys an imv_os_database_t object.
|
||||
*/
|
||||
* Destroys an imv_os_database_t object.
|
||||
*/
|
||||
void (*destroy)(imv_os_database_t *this);
|
||||
|
||||
};
|
||||
@@ -75,8 +64,8 @@ struct imv_os_database_t {
|
||||
/**
|
||||
* Create an imv_os_database_t instance
|
||||
*
|
||||
* @param uri database uri
|
||||
* @param imv_db Already attached IMV database
|
||||
*/
|
||||
imv_os_database_t* imv_os_database_create(char *uri);
|
||||
imv_os_database_t* imv_os_database_create(imv_database_t *imv_db);
|
||||
|
||||
#endif /** IMV_OS_DATABASE_H_ @}*/
|
||||
|
||||
@@ -71,6 +71,11 @@ struct private_imv_os_state_t {
|
||||
*/
|
||||
chunk_t ar_id_value;
|
||||
|
||||
/**
|
||||
* Unique session ID
|
||||
*/
|
||||
int session_id;
|
||||
|
||||
/**
|
||||
* IMV action recommendation
|
||||
*/
|
||||
@@ -346,6 +351,18 @@ METHOD(imv_state_t, get_ar_id, chunk_t,
|
||||
return this->ar_id_value;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, set_session_id, void,
|
||||
private_imv_os_state_t *this, int session_id)
|
||||
{
|
||||
this->session_id = session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, get_session_id, int,
|
||||
private_imv_os_state_t *this)
|
||||
{
|
||||
return this->session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, change_state, void,
|
||||
private_imv_os_state_t *this, TNC_ConnectionState new_state)
|
||||
{
|
||||
@@ -633,6 +650,8 @@ imv_state_t *imv_os_state_create(TNC_ConnectionID connection_id)
|
||||
.get_max_msg_len = _get_max_msg_len,
|
||||
.set_ar_id = _set_ar_id,
|
||||
.get_ar_id = _get_ar_id,
|
||||
.set_session_id = _set_session_id,
|
||||
.get_session_id = _get_session_id,
|
||||
.change_state = _change_state,
|
||||
.get_recommendation = _get_recommendation,
|
||||
.set_recommendation = _set_recommendation,
|
||||
|
||||
@@ -68,6 +68,11 @@ struct private_imv_scanner_state_t {
|
||||
*/
|
||||
chunk_t ar_id_value;
|
||||
|
||||
/**
|
||||
* Unique session ID
|
||||
*/
|
||||
int session_id;
|
||||
|
||||
/**
|
||||
* IMV action recommendation
|
||||
*/
|
||||
@@ -182,6 +187,18 @@ METHOD(imv_state_t, set_ar_id, void,
|
||||
this->ar_id_value = chunk_clone(id_value);
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, set_session_id, void,
|
||||
private_imv_scanner_state_t *this, int session_id)
|
||||
{
|
||||
this->session_id = session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, get_session_id, int,
|
||||
private_imv_scanner_state_t *this)
|
||||
{
|
||||
return this->session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, get_ar_id, chunk_t,
|
||||
private_imv_scanner_state_t *this, u_int32_t *id_type)
|
||||
{
|
||||
@@ -296,6 +313,8 @@ imv_state_t *imv_scanner_state_create(TNC_ConnectionID connection_id)
|
||||
.get_max_msg_len = _get_max_msg_len,
|
||||
.set_ar_id = _set_ar_id,
|
||||
.get_ar_id = _get_ar_id,
|
||||
.set_session_id = _set_session_id,
|
||||
.get_session_id = _get_session_id,
|
||||
.change_state = _change_state,
|
||||
.get_recommendation = _get_recommendation,
|
||||
.set_recommendation = _set_recommendation,
|
||||
|
||||
@@ -68,6 +68,11 @@ struct private_imv_test_state_t {
|
||||
*/
|
||||
chunk_t ar_id_value;
|
||||
|
||||
/**
|
||||
* Unique session ID
|
||||
*/
|
||||
int session_id;
|
||||
|
||||
/**
|
||||
* IMV action recommendation
|
||||
*/
|
||||
@@ -170,6 +175,18 @@ METHOD(imv_state_t, get_ar_id, chunk_t,
|
||||
return this->ar_id_value;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, set_session_id, void,
|
||||
private_imv_test_state_t *this, int session_id)
|
||||
{
|
||||
this->session_id = session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, get_session_id, int,
|
||||
private_imv_test_state_t *this)
|
||||
{
|
||||
return this->session_id;
|
||||
}
|
||||
|
||||
METHOD(imv_state_t, change_state, void,
|
||||
private_imv_test_state_t *this, TNC_ConnectionState new_state)
|
||||
{
|
||||
@@ -307,6 +324,8 @@ imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
|
||||
.get_max_msg_len = _get_max_msg_len,
|
||||
.set_ar_id = _set_ar_id,
|
||||
.get_ar_id = _get_ar_id,
|
||||
.set_session_id = _set_session_id,
|
||||
.get_session_id = _get_session_id,
|
||||
.change_state = _change_state,
|
||||
.get_recommendation = _get_recommendation,
|
||||
.set_recommendation = _set_recommendation,
|
||||
|
||||
Reference in New Issue
Block a user