Skip to content

Commit

Permalink
Merge branch 'main' into get-nvts-lean
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell authored Oct 9, 2023
2 parents 14ee551 + 3f9922b commit a9649d8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Prerequisites:
* libmicrohttpd >= 0.9.0
* pkg-config
* gcc
* zlib >= 1.2

Prerequisites for building documentation:
* Doxygen
Expand All @@ -72,7 +73,7 @@ Prerequisites for building documentation:
Install prerequisites on Debian GNU/Linux:

```bash
apt-get install libmicrohttpd-dev libxml2-dev
apt-get install libmicrohttpd-dev libxml2-dev zlib1g-dev
```

### Compiling
Expand Down
9 changes: 6 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pkg_check_modules (LIBGVM_BASE REQUIRED libgvm_base>=22.6)
pkg_check_modules (LIBGVM_UTIL REQUIRED libgvm_util>=22.6)
pkg_check_modules (LIBGVM_GMP REQUIRED libgvm_gmp>=22.6)
pkg_check_modules (GNUTLS REQUIRED gnutls>=3.2.15)
pkg_check_modules (ZLIB REQUIRED zlib>=1.2)

message (STATUS "Looking for libgcrypt...")
find_library (LIBGCRYPT gcrypt)
Expand All @@ -48,13 +49,13 @@ endif (NOT LIBGCRYPT)
if (NOT LIBMICROHTTPD_FOUND OR NOT LIBXML_FOUND OR NOT GLIB_FOUND OR
(GTHREAD_REQUIRED AND NOT GTHREAD_FOUND) OR NOT
LIBGVM_GMP_FOUND OR NOT GNUTLS_FOUND OR NOT
LIBGCRYPT)
LIBGCRYPT OR NOT ZLIB_FOUND)
message (FATAL_ERROR "One or more required libraries was not found "
"(see message above), please install the missing "
"libraries and run cmake again.")
endif (NOT LIBMICROHTTPD_FOUND OR NOT LIBXML_FOUND OR NOT GLIB_FOUND OR
(GTHREAD_REQUIRED AND NOT GTHREAD_FOUND) OR NOT
LIBGVM_GMP_FOUND OR NOT GNUTLS_FOUND OR NOT LIBGCRYPT)
LIBGVM_GMP_FOUND OR NOT GNUTLS_FOUND OR NOT LIBGCRYPT OR NOT ZLIB_FOUND)

## Program

Expand All @@ -71,7 +72,8 @@ include_directories (${LIBMICROHTTPD_INCLUDE_DIRS} ${LIBXML_INCLUDE_DIRS}
${LIBGVM_BASE_INCLUDE_DIRS}
${LIBGVM_UTIL_INCLUDE_DIRS}
${LIBGVM_GMP_INCLUDE_DIRS}
${GNUTLS_INCLUDE_DIRS})
${GNUTLS_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS})

find_package (Threads)

Expand Down Expand Up @@ -101,6 +103,7 @@ target_link_libraries (gsad ${LIBMICROHTTPD_LDFLAGS}
${LIBXML_LDFLAGS}
${LIBGCRYPT_LDFLAGS}
${GNUTLS_LDFLAGS}
${ZLIB_LDFLAGS}
${LIBGVM_BASE_LDFLAGS}
${LIBGVM_UTIL_LDFLAGS}
${LIBGVM_GMP_LDFLAGS}
Expand Down
87 changes: 86 additions & 1 deletion src/gsad.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <zlib.h>
/* This must follow the system includes. */
#include "gsad_base.h"
#include "gsad_credentials.h"
Expand Down Expand Up @@ -1152,6 +1153,72 @@ watch_client_connection (void *data)
else if (!strcmp (cmd, G_STRINGIFY (name))) res = \
name##_gmp (&connection, credentials, params, response_data);

/**
* @brief Check whether may compress response.
*
* @param[in] con HTTP Connection
*
* @return 1 if may, else 0.
*/
static int
may_compress_response (http_connection_t *con)
{
const char *ae;
const char *de;

ae = MHD_lookup_connection_value (con, MHD_HEADER_KIND,
MHD_HTTP_HEADER_ACCEPT_ENCODING);
if (ae == NULL)
return 0;
if (strcmp (ae, "*") == 0)
return 1;

de = strstr (ae, "deflate");
if (de == NULL)
return 0;

if (((de == ae) || (de[-1] == ',') || (de[-1] == ' '))
&& ((de[strlen ("deflate")] == '\0') || (de[strlen ("deflate")] == ',')
|| (de[strlen ("deflate")] == ';')))
return 1;

return 0;
}

/**
* @brief Compress response.
*
* @param[in] res_len Response length.
* @param[in] res Response.
* @param[out] comp_len Compressed length.
* @param[out] comp Compressed response.
*
* @return 1 on success, else 0.
*/
static int
compress_response (const size_t res_len, const char *res, size_t *comp_len,
char **comp)
{
Bytef *cbuf;
uLongf cbuf_size;
int ret;

cbuf_size = compressBound (res_len);
cbuf = g_malloc (cbuf_size);

ret = compress (cbuf, &cbuf_size, (const Bytef *) res, res_len);

if ((ret == Z_OK) && (cbuf_size < res_len))
{
*comp = (char *) cbuf;
*comp_len = cbuf_size;
return 1;
}

free (cbuf);
return 0;
}

/**
* @brief Handle a complete GET request.
*
Expand All @@ -1172,7 +1239,7 @@ exec_gmp_get (http_connection_t *con, gsad_connection_info_t *con_info,
const int CMD_MAX_SIZE = 27; /* delete_trash_lsc_credential */
params_t *params = con_info->params;
gvm_connection_t connection;
char *res = NULL;
char *res = NULL, *comp;
gsize res_len = 0;
http_response_t *response;
cmd_response_data_t *response_data;
Expand Down Expand Up @@ -1481,9 +1548,27 @@ exec_gmp_get (http_connection_t *con, gsad_connection_info_t *con_info,
if (res_len == 0)
res_len = strlen (res);

if (may_compress_response (con))
{
gsize comp_len;

if (compress_response (res_len, res, &comp_len, &comp))
{
free (res);
res_len = comp_len;
res = comp;
}
}
else
comp = NULL;

response = MHD_create_response_from_buffer (res_len, (void *) res,
MHD_RESPMEM_MUST_FREE);

if (comp)
MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING,
"deflate");

if (watcher_data)
{
pthread_mutex_lock (&(watcher_data->mutex));
Expand Down
16 changes: 2 additions & 14 deletions src/gsad_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5599,7 +5599,6 @@ char *
restore_gmp (gvm_connection_t *connection, credentials_t *credentials,
params_t *params, cmd_response_data_t *response_data)
{
GString *xml;
gchar *ret;
entity_t entity;
const char *target_id;
Expand All @@ -5608,8 +5607,6 @@ restore_gmp (gvm_connection_t *connection, credentials_t *credentials,

CHECK_VARIABLE_INVALID (target_id, "Restore")

xml = g_string_new ("");

/* Restore the resource. */

if (gvm_connection_sendf (connection,
Expand All @@ -5618,7 +5615,6 @@ restore_gmp (gvm_connection_t *connection, credentials_t *credentials,
target_id)
== -1)
{
g_string_free (xml, TRUE);
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (
Expand All @@ -5629,9 +5625,8 @@ restore_gmp (gvm_connection_t *connection, credentials_t *credentials,
response_data);
}

if (read_entity_and_string_c (connection, &entity, &xml))
if (read_entity_and_string_c (connection, &entity, NULL))
{
g_string_free (xml, TRUE);
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (
Expand All @@ -5647,7 +5642,6 @@ restore_gmp (gvm_connection_t *connection, credentials_t *credentials,
ret = response_from_entity (connection, credentials, params, entity,
"Restore", response_data);
free_entity (entity);
g_string_free (xml, FALSE);
return ret;
}

Expand All @@ -5665,17 +5659,13 @@ char *
empty_trashcan_gmp (gvm_connection_t *connection, credentials_t *credentials,
params_t *params, cmd_response_data_t *response_data)
{
GString *xml;
gchar *ret;
entity_t entity;

xml = g_string_new ("");

/* Empty the trash. */

if (gvm_connection_sendf (connection, "<empty_trashcan/>") == -1)
{
g_string_free (xml, TRUE);
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (
Expand All @@ -5685,9 +5675,8 @@ empty_trashcan_gmp (gvm_connection_t *connection, credentials_t *credentials,
response_data);
}

if (read_entity_and_string_c (connection, &entity, &xml))
if (read_entity_and_string_c (connection, &entity, NULL))
{
g_string_free (xml, TRUE);
cmd_response_data_set_status_code (response_data,
MHD_HTTP_INTERNAL_SERVER_ERROR);
return gsad_message (
Expand All @@ -5702,7 +5691,6 @@ empty_trashcan_gmp (gvm_connection_t *connection, credentials_t *credentials,
ret = response_from_entity (connection, credentials, params, entity,
"Empty Trashcan", response_data);
free_entity (entity);
g_string_free (xml, FALSE);
return ret;
}

Expand Down
6 changes: 1 addition & 5 deletions src/gsad_gmp_arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ gmp_arguments_string (gmp_arguments_t *arguments)
g_free (escaped_value);
}

gchar *retval = argumentslist->str;

g_string_free (argumentslist, FALSE);

return retval;
return g_string_free (argumentslist, FALSE);
}

gboolean
Expand Down

0 comments on commit a9649d8

Please sign in to comment.