Skip to content

Commit

Permalink
Clean up get_license response, update GMP doc
Browse files Browse the repository at this point in the history
The GMP command is changed to fit the current specification, which
includes the following changes:
- meta fields schema_version and license_type are renamed
 to version and type
- the hardware element is renamed to appliance and the fields
 cpu_cores and memory are removed, while the sensor field is added
- the sections features and limits are removed
- the signature section is renamed to signatures and its subelements
 to signature
- the file element of the response is removed
  • Loading branch information
timopollmeier committed Aug 27, 2021
1 parent 4081b7b commit c87517e
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 314 deletions.
102 changes: 18 additions & 84 deletions src/gmp_license.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,46 +102,6 @@ get_license_element_start (gmp_parser_t *gmp_parser,
attribute_names, attribute_values);
}

/**
* @brief Writes XML for a license feature to a GString buffer.
*
* This is meant to be used to traverse a GTree with g_tree_foreach.
*
* @param[in] key The key from the tree, i.e. the feature name.
* @param[in] value The value from the tree, i.e. whether it is enabled.
* @param[in] buffer The GString to buffer the XML element.
*
* @return Always FALSE to continue traversing the GTree.
*/
static gboolean
buffer_license_feature_xml (gchar *key, int value, GString *buffer)
{
xml_string_append (buffer,
"<feature name=\"%s\" enabled=\"%d\"/>",
key, value);
return FALSE;
}

/**
* @brief Writes XML for a license limit to a GString buffer.
*
* This is meant to be used to traverse a GTree with g_tree_foreach.
*
* @param[in] key The key from the tree, i.e. the limit name.
* @param[in] value The value from the tree, i.e. the value of the limit.
* @param[in] buffer The GString to buffer the XML element.
*
* @return Always FALSE to continue traversing the GTree.
*/
static gboolean
buffer_license_limit_xml (gchar *key, int value, GString *buffer)
{
xml_string_append (buffer,
"<limit name=\"%s\" value=\"%d\"/>",
key, value);
return FALSE;
}

/**
* @brief Writes XML for a license access key to a GString buffer.
*
Expand Down Expand Up @@ -177,7 +137,7 @@ static gboolean
buffer_license_signature_xml (gchar *key, gchar *value, GString *buffer)
{
xml_string_append (buffer,
"<info_item name=\"%s\">%s</info_item>",
"<signature name=\"%s\">%s</signature>",
key, value);
return FALSE;
}
Expand All @@ -196,15 +156,15 @@ buffer_license_content_xml (GString *response, license_data_t *license_data)
"<content>"
"<meta>"
"<id>%s</id>"
"<schema_version>%d</schema_version>"
"<version>%s</version>"
"<title>%s</title>"
"<license_type>%s</license_type>"
"<type>%s</type>"
"<customer>%s</customer>",
license_data->meta->id,
license_data->meta->schema_version,
license_data->meta->version,
license_data->meta->title,
license_data->meta->license_type,
license_data->meta->customer_name);
license_data->meta->type,
license_data->meta->customer);

xml_string_append (response,
"<created>%s</created>",
Expand All @@ -218,48 +178,30 @@ buffer_license_content_xml (GString *response, license_data_t *license_data)

xml_string_append (response,
"</meta>"
"<hardware>"
"<appliance>"
"<model>%s</model>"
"<model_type>%s</model_type>"
"<memory>%d</memory>"
"<cpu_cores>%d</cpu_cores>"
"</hardware>"
"<features>",
license_data->hardware->model,
license_data->hardware->model_type,
license_data->hardware->memory,
license_data->hardware->cpu_cores);

g_tree_foreach (license_data->features,
(GTraverseFunc) buffer_license_feature_xml,
response);

xml_string_append (response,
"</features>"
"<limits>");

g_tree_foreach (license_data->limits,
(GTraverseFunc) buffer_license_limit_xml,
response);

xml_string_append (response,
"</limits>"
"<keys>");
"<sensor>%d</sensor>"
"</appliance>"
"<keys>",
license_data->appliance->model,
license_data->appliance->model_type,
license_data->appliance->sensor);

g_tree_foreach (license_data->keys,
(GTraverseFunc) buffer_license_key_xml,
response);

xml_string_append (response,
"</keys>"
"<signature>");
"<signatures>");

g_tree_foreach (license_data->signature,
g_tree_foreach (license_data->signatures,
(GTraverseFunc) buffer_license_signature_xml,
response);

xml_string_append (response,
"</signature>"
"</signatures>"
"</content>");
}

Expand All @@ -276,16 +218,14 @@ get_license_run (gmp_parser_t *gmp_parser,
{
int ret;

gchar *license_status, *file_content;
gchar *license_status;
license_data_t *license_data;

license_status = NULL;
license_data = NULL;
file_content = NULL;

ret = manage_get_license (&license_status,
&license_data,
&file_content);
&license_data);

switch (ret)
{
Expand All @@ -308,11 +248,6 @@ get_license_run (gmp_parser_t *gmp_parser,
buffer_license_content_xml (response, license_data);
}

if (file_content)
xml_string_append (response,
"<file>%s</file>",
file_content);

xml_string_append (response,
"</license>"
"</get_license_response>");
Expand All @@ -337,7 +272,6 @@ get_license_run (gmp_parser_t *gmp_parser,

g_free (license_status);
license_data_free (license_data);
g_free (file_content);

get_license_reset ();
}
Expand Down
75 changes: 26 additions & 49 deletions src/manage_license.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,32 @@ license_meta_free (license_meta_t *data)
return;

free (data->id);
free (data->version);
free (data->title);
free (data->license_type);
free (data->customer_name);
free (data->type);
free (data->customer);

g_free (data);
}

/**
* @brief Allocates a new license hardware data struct
* @brief Allocates a new license appliance data struct
*
* @return Newly allocated license hardware data. Free with license_meta_free.
* @return Newly allocated license appliance data. Free with license_meta_free.
*/
license_hardware_t *
license_hardware_new ()
license_appliance_t *
license_appliance_new ()
{
return g_malloc0 (sizeof (license_hardware_t));
return g_malloc0 (sizeof (license_appliance_t));
}

/**
* @brief Frees a license hardware data struct and its fields.
* @brief Frees a license appliance data struct and its fields.
*
* @param[in] data The data struct to free.
*/
void
license_hardware_free (license_hardware_t *data)
license_appliance_free (license_appliance_t *data)
{
if (data == NULL)
return;
Expand All @@ -98,19 +99,13 @@ license_data_new ()
license_data_t *data = g_malloc0 (sizeof (license_data_t));

data->meta = license_meta_new ();
data->hardware = license_hardware_new ();

data->features = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
NULL, g_free, NULL);

data->limits = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
NULL, g_free, NULL);
data->appliance = license_appliance_new ();

data->keys = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
NULL, g_free, g_free);

data->signature = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
NULL, g_free, g_free);
data->signatures = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
NULL, g_free, g_free);

return data;
}
Expand All @@ -127,11 +122,9 @@ license_data_free (license_data_t *data)
return;

license_meta_free (data->meta);
license_hardware_free (data->hardware);
g_tree_destroy (data->features);
g_tree_destroy (data->limits);
license_appliance_free (data->appliance);
g_tree_destroy (data->keys);
g_tree_destroy (data->signature);
g_tree_destroy (data->signatures);

g_free (data);
}
Expand Down Expand Up @@ -168,58 +161,42 @@ manage_update_license_file (const char *new_license)
*/
int
manage_get_license (gchar **status,
license_data_t **license_data,
gchar **file_content)
license_data_t **license_data)
{
if (! acl_user_may ("get_license"))
return 99;

if (status)
*status = g_strdup ("valid");
*status = g_strdup ("active");

if (license_data)
{
*license_data = license_data_new ();
license_meta_t *license_meta = (*license_data)->meta;
license_hardware_t *license_hardware = (*license_data)->hardware;
license_appliance_t *license_appliance = (*license_data)->appliance;

// TODO : replace dummy data with data from license service
license_meta->id = g_strdup ("4711");
license_meta->schema_version = 1;
license_meta->version = g_strdup_printf("1.0.0");
license_meta->title = g_strdup ("Test License");
license_meta->license_type = g_strdup ("Trial");
license_meta->customer_name = g_strdup ("Jane Doe");
license_meta->type = g_strdup ("trial");
license_meta->customer = g_strdup ("Jane Doe");
license_meta->created = time (NULL) - 3600;
license_meta->begins = time (NULL);
license_meta->expires = time (NULL) + 3600 * 24 * 8;

license_hardware->model = g_strdup ("GSM XYZ");
license_hardware->model_type = g_strdup ("Virtual Appliance");
license_hardware->memory = 2048;
license_hardware->cpu_cores = 2;

g_tree_replace ((*license_data)->features,
g_strdup ("GMP_get_reports"),
GINT_TO_POINTER (1));
g_tree_replace ((*license_data)->features,
g_strdup ("GMP_get_tasks"),
GINT_TO_POINTER (1));

g_tree_replace ((*license_data)->limits,
g_strdup ("target_max_hosts"),
GINT_TO_POINTER (4096));
license_appliance->model = g_strdup ("trial");
license_appliance->model_type = g_strdup ("virtual");
license_appliance->sensor = FALSE;

g_tree_replace ((*license_data)->keys,
g_strdup ("GSF"),
g_strdup ("feed"),
g_strdup ("*base64 GSF key*"));

g_tree_replace ((*license_data)->signature,
g_tree_replace ((*license_data)->signatures,
g_strdup ("license"),
g_strdup ("*base64 signature*"));
}

if (file_content)
*file_content = g_strdup ("dummy license file");

return 0;
}
29 changes: 13 additions & 16 deletions src/manage_license.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
*/
typedef struct {
char *id; ///< Unique Identifier of the license
int schema_version; ///< Version of the license file schema
char *version; ///< Version of the license file schema
char *title; ///< Short title summarizing the license
char *license_type; ///< Type of license, e.g. Trial or Full license
char *customer_name; ///< Name of the customer
char *type; ///< Type of license, e.g. Trial or Full license
char *customer; ///< Name of the customer
time_t created; ///< Time the license was created
time_t begins; ///< Time after which the license becomes valid
time_t expires; ///< Time the license expires
Expand All @@ -53,26 +53,23 @@ license_meta_free (license_meta_t *);
typedef struct {
char *model; ///< Appliance model, e.g. "GSM ONE"
char *model_type; ///< Appliance model type, e.g. "Sensor"
int cpu_cores; ///< Number of CPU cores
int memory; ///< Amount of RAM in MiB
} license_hardware_t;
gboolean sensor; ///< Whether the license is applied to a sensor or not
} license_appliance_t;

license_hardware_t *
license_hardware_new ();
license_appliance_t *
license_appliance_new ();

void
license_hardware_free (license_hardware_t *);
license_appliance_free (license_appliance_t *);

/**
* @brief Defines the information contained in a license
*/
typedef struct {
license_meta_t *meta; ///< License metadata
license_hardware_t *hardware; ///< Hardware and appliance information
GTree *features; ///< Map of enabled or disabled features
GTree *limits; ///< Numeric limits, e.g. max. hosts per target
GTree *keys; ///< Base64 encoded access keys, e.g. feed key
GTree *signature; ///< Signature info of the license
license_meta_t *meta; ///< License metadata
license_appliance_t *appliance; ///< Hardware and appliance information
GTree *keys; ///< Base64 encoded access keys, e.g. feed key
GTree *signatures; ///< Signature info of the license
} license_data_t;

license_data_t *
Expand All @@ -88,4 +85,4 @@ int
manage_update_license_file (const char *);

int
manage_get_license (char **, license_data_t **, char **);
manage_get_license (char **, license_data_t **);
Loading

0 comments on commit c87517e

Please sign in to comment.