refactored reason string and remediation instructions framework

This commit is contained in:
Andreas Steffen
2012-11-23 12:30:33 +01:00
parent b5d27839ad
commit ee6aeca892
15 changed files with 781 additions and 389 deletions
+3 -2
View File
@@ -642,7 +642,8 @@ METHOD(imv_agent_t, provide_recommendation, TNC_Result,
TNC_IMV_Action_Recommendation rec;
TNC_IMV_Evaluation_Result eval;
TNC_ConnectionID connection_id;
char *reason_string, *reason_lang;
chunk_t reason_string;
char *reason_lang;
enumerator_t *e;
state->get_recommendation(state, &rec, &eval);
@@ -659,7 +660,7 @@ METHOD(imv_agent_t, provide_recommendation, TNC_Result,
{
this->set_attribute(this->id, connection_id,
TNC_ATTRIBUTEID_REASON_STRING,
strlen(reason_string), reason_string);
reason_string.len, reason_string.ptr);
this->set_attribute(this->id, connection_id,
TNC_ATTRIBUTEID_REASON_LANGUAGE,
strlen(reason_lang), reason_lang);
+73
View File
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2012 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_lang_string.h"
#include <utils/debug.h>
/**
* Described in header.
*/
char* imv_lang_string_select_lang(enumerator_t *language_enumerator,
char* languages[], int lang_count)
{
bool match = FALSE;
char *lang;
int i, i_chosen = 0;
while (language_enumerator->enumerate(language_enumerator, &lang))
{
for (i = 0; i < lang_count; i++)
{
if (streq(lang, languages[i]))
{
match = TRUE;
i_chosen = i;
break;
}
}
if (match)
{
break;
}
}
return languages[i_chosen];
}
/**
* Described in header.
*/
char* imv_lang_string_select_string(imv_lang_string_t lang_string[], char *lang)
{
char *string;
int i = 0;
if (!lang_string)
{
return NULL;
}
string = lang_string[0].string;
while (lang_string[i].lang)
{
if (streq(lang, lang_string[i].lang))
{
string = lang_string[i].string;
break;
}
i++;
}
return string;
}
+67
View File
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2012 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_lang_string_t imv_lang_string
* @{ @ingroup imv_lang_string
*/
#ifndef IMV_LANG_STRING_H_
#define IMV_LANG_STRING_H_
#include <library.h>
#include <collections/enumerator.h>
typedef struct imv_lang_string_t imv_lang_string_t;
/**
* Define a language string entry
*/
struct imv_lang_string_t {
/**
* language code
*/
char *lang;
/**
* UTF-8 string in the corresponding language
*/
char *string;
};
/**
* Select the preferred language
*
* @param language_enumerator enumerator over user preferred languages
* @param languages string array of available languages
* @param lang_count number of available languages
* @return selected language as a language code
*/
char* imv_lang_string_select_lang(enumerator_t *language_enumerator,
char* languages[], int lang_count);
/**
* Select the preferred language string
*
* @param lang_string multi-lingual array of strings
* @param lang language code of preferred language
* @return selected string
*/
char* imv_lang_string_select_string(imv_lang_string_t lang_string[], char *lang);
#endif /** IMV_LANG_STRING_H_ @}*/
+4 -4
View File
@@ -191,7 +191,8 @@ METHOD(imv_msg_t, send_assessment, TNC_Result,
TNC_IMV_Action_Recommendation rec;
TNC_IMV_Evaluation_Result eval;
pa_tnc_attr_t *attr;
char *string = NULL, *lang_code = NULL, *uri = NULL;
chunk_t string = chunk_empty;
char *lang_code = NULL, *uri = NULL;
enumerator_t *e;
/* Send an IETF Assessment Result attribute if enabled */
@@ -210,10 +211,9 @@ METHOD(imv_msg_t, send_assessment, TNC_Result,
if (this->state->get_remediation_instructions(this->state,
e, &string, &lang_code, &uri))
{
if (string && lang_code)
if (string.len && lang_code)
{
attr = ietf_attr_remediation_instr_create_from_string(
chunk_create(string, strlen(string)),
attr = ietf_attr_remediation_instr_create_from_string(string,
chunk_create(lang_code, strlen(lang_code)));
add_attribute(this, attr);
}
+95
View File
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2012 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_reason_string.h"
#include <utils/debug.h>
typedef struct private_imv_reason_string_t private_imv_reason_string_t;
/**
* Private data of an imv_reason_string_t object.
*/
struct private_imv_reason_string_t {
/**
* Public members of imv_reason_string_t
*/
imv_reason_string_t public;
/**
* Preferred language
*/
char *lang;
/**
* Contains the concatenated reasons
*/
chunk_t reasons;
};
METHOD(imv_reason_string_t, add_reason, void,
private_imv_reason_string_t *this, imv_lang_string_t reason[])
{
char *s_reason;
s_reason = imv_lang_string_select_string(reason, this->lang);
if (this->reasons.len)
{
/* append any further reasons */
this->reasons = chunk_cat("cm", this->reasons, chunk_from_chars('\n'),
chunk_create(s_reason, strlen(s_reason)));
}
else
{
/* add the first reason */
this->reasons = chunk_clone(chunk_create(s_reason, strlen(s_reason)));
}
}
METHOD(imv_reason_string_t, get_encoding, chunk_t,
private_imv_reason_string_t *this)
{
return this->reasons;
}
METHOD(imv_reason_string_t, destroy, void,
private_imv_reason_string_t *this)
{
free(this->reasons.ptr);
free(this);
}
/**
* Described in header.
*/
imv_reason_string_t *imv_reason_string_create(char *lang)
{
private_imv_reason_string_t *this;
INIT(this,
.public = {
.add_reason = _add_reason,
.get_encoding = _get_encoding,
.destroy = _destroy,
},
.lang = lang,
);
return &this->public;
}
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2012 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_reason_string_t imv_reason_string
* @{ @ingroup imv_reason_string
*/
#ifndef IMV_REASON_STRING_H_
#define IMV_REASON_STRING_H_
#include "imv_lang_string.h"
#include <library.h>
#include <collections/linked_list.h>
typedef struct imv_reason_string_t imv_reason_string_t;
/**
* Defines and builds a TNC Reason String
*/
struct imv_reason_string_t {
/**
* Add an individual remediation instruction to the string
*
* @param reason Multi-lingual reason string
*/
void (*add_reason)(imv_reason_string_t *this, imv_lang_string_t reason[]);
/**
* Gets encoding of the reason string
*
* @return TNC reason string
*/
chunk_t (*get_encoding)(imv_reason_string_t *this);
/**
* Destroys an imv_reason_string_t object
*/
void (*destroy)(imv_reason_string_t *this);
};
/**
* Creates an Reason String object
*
* @param lang Preferred language
*/
imv_reason_string_t* imv_reason_string_create(char *lang);
#endif /** IMV_REASON_STRING_H_ @}*/
+131
View File
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2012 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_remediation_string.h"
#include <utils/debug.h>
typedef struct private_imv_remediation_string_t private_imv_remediation_string_t;
/**
* Private data of an imv_remediation_string_t object.
*/
struct private_imv_remediation_string_t {
/**
* Public members of imv_remediation_string_t
*/
imv_remediation_string_t public;
/**
* XML or plaintext encoding
*/
bool as_xml;
/**
* Preferred language
*/
char *lang;
/**
* Contains the concatenated remediation instructions
*/
chunk_t instructions;
};
METHOD(imv_remediation_string_t, add_instruction, void,
private_imv_remediation_string_t *this, imv_lang_string_t title[],
imv_lang_string_t description[], imv_lang_string_t itemsheader[],
linked_list_t *items)
{
char xml_format[] = " <instruction>\n"
" <title>%s</title>\n"
" <description>%s</description>\n"
"%s%s"
" </instruction>\n";
char *instruction, *format, *s_title, *s_description, *s_itemsheader;
size_t len;
s_title = imv_lang_string_select_string(title, this->lang);
s_description = imv_lang_string_select_string(description, this->lang);
s_itemsheader = imv_lang_string_select_string(itemsheader, this->lang);
len = strlen(s_title) + strlen(s_description);
if (this->as_xml)
{
format = xml_format;
len += strlen(xml_format) - 8;
}
else
{
format = this->instructions.len ? "\n%s\n%s%s%s" : "%s\n%s%s%s";
len += 2;
}
instruction = malloc(len + 1);
sprintf(instruction, format, s_title, s_description, "", "");
this->instructions = chunk_cat("mm", this->instructions,
chunk_create(instruction, strlen(instruction)));
}
METHOD(imv_remediation_string_t, get_encoding, chunk_t,
private_imv_remediation_string_t *this)
{
char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<remediationinstructions>\n";
char xml_trailer[] = "</remediationinstructions>";
if (!this->instructions.len)
{
return chunk_empty;
}
if (this->as_xml)
{
this->instructions = chunk_cat("cmc",
chunk_create(xml_header, strlen(xml_header)),
this->instructions,
chunk_create(xml_trailer, strlen(xml_trailer))
);
}
return this->instructions;
}
METHOD(imv_remediation_string_t, destroy, void,
private_imv_remediation_string_t *this)
{
free(this->instructions.ptr);
free(this);
}
/**
* Described in header.
*/
imv_remediation_string_t *imv_remediation_string_create(bool as_xml, char *lang)
{
private_imv_remediation_string_t *this;
INIT(this,
.public = {
.add_instruction = _add_instruction,
.get_encoding = _get_encoding,
.destroy = _destroy,
},
.as_xml = as_xml,
.lang = lang,
);
return &this->public;
}
+72
View File
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2012 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_remediation_string_t imv_remediation_string
* @{ @ingroup imv_remediation_string
*/
#ifndef IMV_REMEDIATION_STRING_H_
#define IMV_REMEDIATION_STRING_H_
#include "imv_lang_string.h"
#include <library.h>
#include <collections/linked_list.h>
typedef struct imv_remediation_string_t imv_remediation_string_t;
/**
* Defines and builds an IETF Remediation Instructions String
*/
struct imv_remediation_string_t {
/**
* Add an individual remediation instruction to the string
*
* @param title instruction title
* @param description instruction description
* @param itemsheader optional items header or NULL
* @param items optional items list or NULL
*/
void (*add_instruction)(imv_remediation_string_t *this,
imv_lang_string_t title[],
imv_lang_string_t description[],
imv_lang_string_t itemsheader[],
linked_list_t *items);
/**
* Gets the plaintext or XML encoding of the remediation instructions
*
* @return remediation instructions string
*/
chunk_t (*get_encoding)(imv_remediation_string_t *this);
/**
* Destroys an imv_remediation_string_t object
*/
void (*destroy)(imv_remediation_string_t *this);
};
/**
* Creates an IETF Remediation Instructions String object
*
* @param as_xml XML encoding if TRUE, plaintext otherwise
* @param lang Preferred language
*/
imv_remediation_string_t* imv_remediation_string_create(bool as_xml, char *lang);
#endif /** IMV_REMEDIATION_STRING_H_ @}*/
+2 -2
View File
@@ -116,7 +116,7 @@ struct imv_state_t {
*/
bool (*get_reason_string)(imv_state_t *this,
enumerator_t *language_enumerator,
char **reason_string, char **reason_language);
chunk_t *reason_string, char **reason_language);
/**
* Get remediation instructions based on the preferred language
@@ -129,7 +129,7 @@ struct imv_state_t {
*/
bool (*get_remediation_instructions)(imv_state_t *this,
enumerator_t *language_enumerator,
char **string, char **lang_code,
chunk_t *string, char **lang_code,
char **uri);
/**