Skip to content

Commit

Permalink
Use libtheia for get_license GMP command
Browse files Browse the repository at this point in the history
This replaces the dummy license with actual data fetched using the
optional licensing client library libtheia.
If the library is not available, license commands will always return
"Service not available" responses.
  • Loading branch information
timopollmeier committed Sep 24, 2021
1 parent 4ef6a94 commit 343866c
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 248 deletions.
22 changes: 16 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ else (NOT GPGME)
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif (NOT GPGME)

find_package(Theia 1.0.0 QUIET)
if (Theia_DIR)
message (STATUS "Found libtheia ${Theia_VERSION} in ${Theia_DIR}")
add_definitions (-DHAS_LIBTHEIA="1")
set (OPT_THEIA_TGT Theia::Theia)
else (Theia_DIR)
message (STATUS "No compatible libtheia version found")
set (OPT_THEIA_TGT "")
endif (Theia_DIR)

include_directories (${LIBGVM_GMP_INCLUDE_DIRS}
${LIBGVM_BASE_INCLUDE_DIRS} ${LIBGVM_UTIL_INCLUDE_DIRS}
${LIBGVM_OSP_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS})
Expand Down Expand Up @@ -260,32 +270,32 @@ target_link_libraries (gvmd m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (manage-test cgreen m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (manage-sql-test cgreen m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (manage-utils-test cgreen m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (gmp-tickets-test cgreen m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (utils-test cgreen m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (gvm-pg-server ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})

set_target_properties (gvmd PROPERTIES LINKER_LANGUAGE C)
Expand Down
99 changes: 38 additions & 61 deletions src/gmp_license.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,56 +102,22 @@ get_license_element_start (gmp_parser_t *gmp_parser,
attribute_names, attribute_values);
}

/**
* @brief Writes XML for a license access key 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 access key name.
* @param[in] value The value from the tree, i.e. the key content.
* @param[in] buffer The GString to buffer the XML element.
*
* @return Always FALSE to continue traversing the GTree.
*/
static gboolean
buffer_license_key_xml (gchar *key, gchar *value, GString *buffer)
{
xml_string_append (buffer,
"<key name=\"%s\">%s</key>",
key, value);
return FALSE;
}

/**
* @brief Writes XML for a license signature info item 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 info item name.
* @param[in] value The value from the tree, i.e. the info item content.
* @param[in] buffer The GString to buffer the XML element.
*
* @return Always FALSE to continue traversing the GTree.
*/
static gboolean
buffer_license_signature_xml (gchar *key, gchar *value, GString *buffer)
{
xml_string_append (buffer,
"<signature name=\"%s\">%s</signature>",
key, value);
return FALSE;
}


/**
* @brief Writes license data to a GString as XML
*
* @param[in] response The GString buffer to write the license content to.
* @param[in] license_data The license data struct to get the data from.
*/
static void
buffer_license_content_xml (GString *response, license_data_t *license_data)
buffer_license_content_xml (GString *response, theia_license_t *license_data)
{
#ifdef HAS_LIBTHEIA
if (license_data == NULL)
{
xml_string_append (response, "<content></content>");
return;
}

xml_string_append (response,
"<content>"
"<meta>"
Expand Down Expand Up @@ -183,26 +149,22 @@ buffer_license_content_xml (GString *response, license_data_t *license_data)
"<model_type>%s</model_type>"
"<sensor>%d</sensor>"
"</appliance>"
"<keys>",
"<keys>"
"<key name=\"feed\">%s</key>"
"</keys>"
"<signatures>"
"<signature name=\"license\">%s</signature>"
"</signatures>"
"</content>",
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>"
"<signatures>");

g_tree_foreach (license_data->signatures,
(GTraverseFunc) buffer_license_signature_xml,
response);
license_data->appliance->sensor,
license_data->keys->feed,
license_data->signatures->license);

xml_string_append (response,
"</signatures>"
"</content>");
#else // HAS_LIBTHEIA
xml_string_append (response, "<content></content>");
#endif // HAS_LIBTHEIA
}


Expand All @@ -219,7 +181,7 @@ get_license_run (gmp_parser_t *gmp_parser,
int ret;

gchar *license_status;
license_data_t *license_data;
theia_license_t *license_data;

license_status = NULL;
license_data = NULL;
Expand Down Expand Up @@ -262,6 +224,18 @@ get_license_run (gmp_parser_t *gmp_parser,
" status_text=\"Licensing service unavailable.\"/>",
STATUS_SERVICE_DOWN);
break;
case 2:
SENDF_TO_CLIENT_OR_FAIL
("<get_license_response status=\"%s\""
" status_text=\"Could not send get.license command.\"/>",
STATUS_SERVICE_DOWN);
break;
case 3:
SENDF_TO_CLIENT_OR_FAIL
("<get_license_response status=\"%s\""
" status_text=\"Could not retrieve got.license response.\"/>",
STATUS_SERVICE_DOWN);
break;
case 99:
SEND_TO_CLIENT_OR_FAIL (XML_ERROR_ACCESS ("get_license"));
break;
Expand All @@ -271,7 +245,10 @@ get_license_run (gmp_parser_t *gmp_parser,
}

g_free (license_status);
license_data_free (license_data);

#ifdef HAS_LIBTHEIA
theia_license_free (license_data);
#endif

get_license_reset ();
}
Expand Down
Loading

0 comments on commit 343866c

Please sign in to comment.