implemented IMV session control

This commit is contained in:
Andreas Steffen
2013-06-21 23:25:21 +02:00
parent 1f179c63b3
commit b8db66de15
21 changed files with 978 additions and 454 deletions
+47 -2
View File
@@ -62,6 +62,11 @@ struct private_imv_agent_t {
*/
linked_list_t *additional_ids;
/**
* IMV database
*/
imv_database_t *db;
/**
* list of TNCS connection entries
*/
@@ -402,11 +407,14 @@ METHOD(imv_agent_t, create_state, TNC_Result,
{
TNC_ConnectionID conn_id;
char *tnccs_p = NULL, *tnccs_v = NULL, *t_p = NULL, *t_v = NULL;
bool has_long = FALSE, has_excl = FALSE, has_soh = FALSE;
bool has_long = FALSE, has_excl = FALSE, has_soh = FALSE, first = TRUE;
linked_list_t *ar_identities;
enumerator_t *enumerator;
tncif_identity_t *tnc_id;
int session_id;
u_int32_t max_msg_len;
u_int32_t ar_id_type = TNC_ID_UNKNOWN;
chunk_t ar_id_value = chunk_empty;
conn_id = state->get_connection_id(state);
if (find_connection(this, conn_id))
@@ -462,10 +470,31 @@ METHOD(imv_agent_t, create_state, TNC_Result,
TNC_Subject_names, tcg_subject_type,
id_value.len, id_value.ptr,
TNC_Authentication_names, tcg_auth_type);
state->set_ar_id(state, tcg_id_type, id_value);
if (first)
{
ar_id_type = tcg_id_type;
ar_id_value = id_value;
state->set_ar_id(state, ar_id_type, ar_id_value);
first = FALSE;
}
}
enumerator->destroy(enumerator);
if (this->db)
{
session_id = this->db->get_session_id(this->db, conn_id,
ar_id_type, ar_id_value);
if (session_id)
{
DBG2(DBG_IMV, " assigned session ID %d", session_id);
state->set_session_id(state, session_id);
}
else
{
DBG1(DBG_IMV, " no session ID assigned");
}
}
ar_identities->destroy_offset(ar_identities,
offsetof(tncif_identity_t, destroy));
free(tnccs_p);
@@ -553,6 +582,12 @@ METHOD(imv_agent_t, get_state, bool,
return TRUE;
}
METHOD(imv_agent_t, get_database, imv_database_t*,
private_imv_agent_t *this)
{
return this->db;
}
METHOD(imv_agent_t, get_name, const char*,
private_imv_agent_t *this)
{
@@ -754,6 +789,7 @@ METHOD(imv_agent_t, destroy, void,
private_imv_agent_t *this)
{
DBG1(DBG_IMV, "IMV %u \"%s\" terminated", this->id, this->name);
DESTROY_IF(this->db);
this->additional_ids->destroy(this->additional_ids);
this->connections->destroy_offset(this->connections,
offsetof(imv_state_t, destroy));
@@ -772,6 +808,7 @@ imv_agent_t *imv_agent_create(const char *name,
TNC_IMVID id, TNC_Version *actual_version)
{
private_imv_agent_t *this;
char *uri;
/* initialize or increase the reference count */
if (!libimcv_init())
@@ -786,6 +823,7 @@ imv_agent_t *imv_agent_create(const char *name,
.delete_state = _delete_state,
.change_state = _change_state,
.get_state = _get_state,
.get_database = _get_database,
.get_name = _get_name,
.get_id = _get_id,
.reserve_additional_ids = _reserve_additional_ids,
@@ -804,6 +842,13 @@ imv_agent_t *imv_agent_create(const char *name,
.connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
/* attach IMV database */
uri = lib->settings->get_str(lib->settings, "libimcv.database", NULL);
if (uri)
{
this->db = imv_database_create(uri);
}
*actual_version = TNC_IFIMV_VERSION_1;
DBG1(DBG_IMV, "IMV %u \"%s\" initialized", this->id, this->name);
+8
View File
@@ -23,6 +23,7 @@
#define IMV_AGENT_H_
#include "imv_state.h"
#include "imv_database.h"
#include "pa_tnc/pa_tnc_msg.h"
#include <tncifimv.h>
@@ -137,6 +138,13 @@ struct imv_agent_t {
bool (*get_state)(imv_agent_t *this,
TNC_ConnectionID connection_id, imv_state_t **state);
/**
* Get IMV database
*
* @return IMV database if it exists, NULL otherwise
*/
imv_database_t* (*get_database)(imv_agent_t *this);
/**
* Get IMV name
*
+209
View File
@@ -0,0 +1,209 @@
/*
* Copyright (C) 2013 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 "imv_database.h"
#include <utils/debug.h>
#include <string.h>
#include <time.h>
typedef struct private_imv_database_t private_imv_database_t;
#define SESSION_TIME_DELTA_MAX 2 /* seconds */
/**
* Private data of a imv_database_t object.
*
*/
struct private_imv_database_t {
/**
* Public imv_database_t interface.
*/
imv_database_t public;
/**
* database instance
*/
database_t *db;
};
METHOD(imv_database_t, get_session_id, int,
private_imv_database_t *this, TNC_ConnectionID id, u_int32_t ar_id_type,
chunk_t ar_id_value)
{
enumerator_t *e;
int ar_id = 0, session_id = 0;
u_int created;
time_t now;
/* get most recent session for a given connection ID if available */
e = this->db->query(this->db,
"SELECT id, time FROM sessions WHERE connection = ? "
"ORDER BY time DESC", DB_INT, id, DB_INT, DB_UINT);
if (e)
{
e->enumerate(e, &session_id, &created);
e->destroy(e);
}
/* get current time */
now = time(NULL);
/* check if a new session has already been created by another IMV */
if (session_id && (now - created) <= SESSION_TIME_DELTA_MAX)
{
return session_id;
}
if (ar_id_value.len)
{
/* get primary key of AR identity if it exists */
e = this->db->query(this->db,
"SELECT id FROM identities WHERE type = ? AND value = ?",
DB_INT, ar_id_type, DB_BLOB, ar_id_value, DB_INT);
if (e)
{
e->enumerate(e, &ar_id);
e->destroy(e);
}
/* if AR identity has not been found - register it */
if (!ar_id)
{
this->db->execute(this->db, &ar_id,
"INSERT INTO identities (type, value) VALUES (?, ?)",
DB_INT, ar_id_type, DB_BLOB, ar_id_value);
}
}
/* create a new session ID */
this->db->execute(this->db, &session_id,
"INSERT INTO sessions (time, connection, identity) "
"VALUES (?, ?, ?)", DB_UINT, now, DB_INT, id, DB_INT, ar_id);
return session_id;
}
METHOD(imv_database_t, add_product, int,
private_imv_database_t *this, int session_id, char *product)
{
enumerator_t *e;
int pid = 0;
/* get primary key of product info string if it exists */
e = this->db->query(this->db,
"SELECT id FROM products WHERE name = ?", DB_TEXT, product, DB_INT);
if (e)
{
e->enumerate(e, &pid);
e->destroy(e);
}
/* if product info string has not been found - register it */
if (!pid)
{
this->db->execute(this->db, &pid,
"INSERT INTO products (name) VALUES (?)", DB_TEXT, product);
}
/* add product reference to session */
if (pid)
{
this->db->execute(this->db, NULL,
"UPDATE sessions SET product = ? WHERE id = ?",
DB_INT, pid, DB_INT, session_id);
}
return pid;
}
METHOD(imv_database_t, add_device, int,
private_imv_database_t *this, int session_id, chunk_t device)
{
enumerator_t *e;
int did = 0;
/* get primary key of device identification if it exists */
e = this->db->query(this->db,
"SELECT id FROM devices WHERE value = ?", DB_BLOB, device, DB_INT);
if (e)
{
e->enumerate(e, &did);
e->destroy(e);
}
/* if device identification has not been found - register it */
if (!did)
{
this->db->execute(this->db, &did,
"INSERT INTO devices (value) VALUES (?)", DB_BLOB, device);
}
/* add device reference to session */
if (did)
{
this->db->execute(this->db, NULL,
"UPDATE sessions SET device = ? WHERE id = ?",
DB_INT, did, DB_INT, session_id);
}
return did;
}
METHOD(imv_database_t, get_database, database_t*,
private_imv_database_t *this)
{
return this->db;
}
METHOD(imv_database_t, destroy, void,
private_imv_database_t *this)
{
this->db->destroy(this->db);
free(this);
}
/**
* See header
*/
imv_database_t *imv_database_create(char *uri)
{
private_imv_database_t *this;
INIT(this,
.public = {
.get_session_id = _get_session_id,
.add_product = _add_product,
.add_device = _add_device,
.get_database = _get_database,
.destroy = _destroy,
},
.db = lib->db->create(lib->db, uri),
);
if (!this->db)
{
DBG1(DBG_IMV,
"failed to connect to IMV database '%s'", uri);
free(this);
return NULL;
}
return &this->public;
}
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2013 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 imv_database_t imv_database
* @{ @ingroup libimcv_imv
*/
#ifndef IMV_DATABASE_H_
#define IMV_DATABASE_H_
#include <tncif.h>
#include <library.h>
typedef struct imv_database_t imv_database_t;
/**
* IMV database interface
*/
struct imv_database_t {
/**
* Register or get a unique session ID using the TNCCS connection ID
*
* @param id TNCCS Connection ID
* @param ar_id_type Access Requestor identity type
* @param ar_id_value Access Requestor identity value
* @return Session ID or 0 if not available
*/
int (*get_session_id)(imv_database_t *this, TNC_ConnectionID id,
u_int32_t ar_id_type, chunk_t ar_id_value);
/**
* Add product information string to a session
*
* @param session_id Session ID
* @param product Product information string
* @return Product ID or 0 if not available
*/
int (*add_product)(imv_database_t *this, int session_id, char *product);
/**
* Add device identification to a session
*
* @param session_id Sessiion ID
* @param device Device identification
* @return Device ID or 0 if not available
*/
int (*add_device)(imv_database_t *this, int session_id, chunk_t device);
/**
* Get database handle
*
* @return Database handle
*/
database_t* (*get_database)(imv_database_t *this);
/**
* Destroys an imv_database_t object
*/
void (*destroy)(imv_database_t *this);
};
/**
* Create an imv_database_t instance
*
* @param uri database uri
*/
imv_database_t* imv_database_create(char *uri);
#endif /** IMV_DATABASE_H_ @}*/
+14
View File
@@ -94,6 +94,20 @@ struct imv_state_t {
*/
chunk_t (*get_ar_id)(imv_state_t *this, u_int32_t *id_type);
/**
* Set unique session ID
*
* @param session_id Unique session ID
*/
void (*set_session_id)(imv_state_t *this, int session_id);
/**
* Get unique session_id
*
* @return Unique session ID
*/
int (*get_session_id)(imv_state_t *this);
/**
* Change the connection state
*