Skip to content

Commit

Permalink
Add: New multi_selection report format param type
Browse files Browse the repository at this point in the history
A new parameter type for report formats and report configs is added that
allows selecting multiple predefined options.

This can later be used to add new customization options to report
formats like a column selection for CSV.
  • Loading branch information
timopollmeier committed Apr 25, 2024
1 parent 169c7eb commit 65cd021
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 31 deletions.
1 change: 1 addition & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ necessary to install dependent development packages.
Prerequisites:
* GCC (Debian package: gcc)
* cmake >= 3.0 (Debian package: cmake)
* cJSON >= 1.7.14 (Debian package: libcjson-dev)
* glib-2.0 >= 2.42 (Debian package: libglib2.0-dev)
* gnutls >= 3.2.15 (Debian package: libgnutls28-dev)
* libgvm_base, libgvm_util, libgvm_osp, libgvm_gmp >= 20.08.0 ([gvm-libs](https://github.com/greenbone/gvm-libs/tree/gvm-libs-20.08) component)
Expand Down
13 changes: 7 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ find_package (Threads)
## list and throw an error, otherwise long install-cmake-install-cmake cycles
## might occur.

pkg_check_modules (CJSON REQUIRED libcjson>=1.7.14)
pkg_check_modules (LIBGVM_BASE REQUIRED libgvm_base>=22.9)
pkg_check_modules (LIBGVM_UTIL REQUIRED libgvm_util>=22.9)
pkg_check_modules (LIBGVM_OSP REQUIRED libgvm_osp>=22.9)
Expand Down Expand Up @@ -290,32 +291,32 @@ add_executable (gvmd

target_link_libraries (gvmd m
${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${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} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${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} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${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} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${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} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${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} ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${PostgreSQL_LIBRARIES} ${LIBBSD_LDFLAGS} ${CJSON_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS}
${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS}
${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${OPT_THEIA_TGT})
target_link_libraries (gvm-pg-server ${LIBBSD_LDFLAGS} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
Expand Down
12 changes: 8 additions & 4 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15201,8 +15201,10 @@ print_report_config_params (gmp_parser_t *gmp_parser, GError **error,
report_config_param_iterator_fallback_value (&params));
}

if (report_config_param_iterator_type (&params)
== REPORT_FORMAT_PARAM_TYPE_SELECTION)
report_format_param_type_t param_type;
param_type = report_config_param_iterator_type (&params);
if (param_type == REPORT_FORMAT_PARAM_TYPE_SELECTION
|| param_type == REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION)
{
SEND_TO_CLIENT_OR_FAIL ("<options>");
init_param_option_iterator
Expand Down Expand Up @@ -15624,8 +15626,10 @@ handle_get_report_formats (gmp_parser_t *gmp_parser, GError **error)
report_format_param_iterator_fallback (&params));
}

if (report_format_param_iterator_type (&params)
== REPORT_FORMAT_PARAM_TYPE_SELECTION)
report_format_param_type_t param_type;
param_type = report_format_param_iterator_type (&params);
if (param_type == REPORT_FORMAT_PARAM_TYPE_SELECTION
|| param_type == REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION)
{
SEND_TO_CLIENT_OR_FAIL ("<options>");
init_param_option_iterator
Expand Down
4 changes: 4 additions & 0 deletions src/manage_report_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ report_format_param_type_name (report_format_param_type_t type)
return "text";
case REPORT_FORMAT_PARAM_TYPE_REPORT_FORMAT_LIST:
return "report_format_list";
case REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION:
return "multi_selection";
default:
assert (0);
case REPORT_FORMAT_PARAM_TYPE_ERROR:
Expand Down Expand Up @@ -143,6 +145,8 @@ report_format_param_type_from_name (const char *name)
return REPORT_FORMAT_PARAM_TYPE_TEXT;
if (strcmp (name, "report_format_list") == 0)
return REPORT_FORMAT_PARAM_TYPE_REPORT_FORMAT_LIST;
if (strcmp (name, "multi_selection") == 0)
return REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION;
return REPORT_FORMAT_PARAM_TYPE_ERROR;
}

Expand Down
1 change: 1 addition & 0 deletions src/manage_report_formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ typedef enum
REPORT_FORMAT_PARAM_TYPE_STRING = 3,
REPORT_FORMAT_PARAM_TYPE_TEXT = 4,
REPORT_FORMAT_PARAM_TYPE_REPORT_FORMAT_LIST = 5,
REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION = 6,
REPORT_FORMAT_PARAM_TYPE_ERROR = 100
} report_format_param_type_t;

Expand Down
134 changes: 113 additions & 21 deletions src/manage_sql_report_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "sql.h"
#include "utils.h"

#include <cjson/cJSON.h>
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
Expand Down Expand Up @@ -2406,6 +2407,28 @@ report_format_param_type_min (report_format_t report_format, const char *name)
return min;
}

static int
report_format_param_value_in_options (report_format_param_t param,
const char *value)
{
iterator_t options;
int found = 0;

init_param_option_iterator (&options, param, 1, NULL);
while (next (&options))
{
if (param_option_iterator_value (&options)
&& (strcmp (param_option_iterator_value (&options), value)
== 0))
{
found = 1;
break;
}
}
cleanup_iterator (&options);
return found;
}

/**
* @brief Validate a value for a report format param.
*
Expand Down Expand Up @@ -2459,29 +2482,18 @@ report_format_validate_param_value (report_format_t report_format,
break;
case REPORT_FORMAT_PARAM_TYPE_SELECTION:
{
iterator_t options;
int found = 0;

init_param_option_iterator (&options, param, 1, NULL);
while (next (&options))
if (param_option_iterator_value (&options)
&& (strcmp (param_option_iterator_value (&options), value)
== 0))
{
found = 1;
break;
}
cleanup_iterator (&options);
if (found)
break;
if (error_message)
if (! report_format_param_value_in_options (param, value))
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" is not a valid selection option",
name);
if (error_message)
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" is not a valid selection option",
name);
}
return 1;
}
return 1;
break;
}
case REPORT_FORMAT_PARAM_TYPE_STRING:
case REPORT_FORMAT_PARAM_TYPE_TEXT:
Expand Down Expand Up @@ -2533,6 +2545,86 @@ report_format_validate_param_value (report_format_t report_format,
return 0;
}
break;
case REPORT_FORMAT_PARAM_TYPE_MULTI_SELECTION:
{
long long int min, max, actual;
min = report_format_param_type_min (report_format, name);
max = report_format_param_type_max (report_format, name);
actual = 0LL;
cJSON *json = cJSON_Parse (value);
cJSON *array_item = NULL;

if (!cJSON_IsArray (json))
{
if (error_message)
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" is not a valid JSON array",
name);
}
cJSON_Delete (json);
return 1;
}

cJSON_ArrayForEach (array_item, json)
{
char *string;
if (!cJSON_IsString (array_item))
{
if (error_message)
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" contains a non-string value",
name);
}
cJSON_Delete (json);
return 1;
}
string = cJSON_GetStringValue (array_item);
if (! report_format_param_value_in_options (param, string))
{
if (error_message)
{
*error_message
= g_strdup_printf ("\"%s\" in value of param \"%s\""
" is not a valid selection option",
string, name);
}
cJSON_Delete (json);
return 1;
}
actual ++;
}

cJSON_Delete (json);
if (actual < min)
{
if (error_message)
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" must contain at least %lld option(s),"
" got %lld",
name, min, actual);
}
return 1;
}
if (actual > max)
{
if (error_message)
{
*error_message
= g_strdup_printf ("value of param \"%s\""
" must contain a maximum of %lld"
" option(s), got %lld",
name, max, actual);
}
return 1;
}
break;
}
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/schema_formats/XML/GMP.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -16016,6 +16016,7 @@ END:VCALENDAR
<alts>
<alt>boolean</alt>
<alt>integer</alt>
<alt>multi_selection</alt>
<alt>selection</alt>
<alt>string</alt>
<alt>text</alt>
Expand Down Expand Up @@ -16678,6 +16679,7 @@ END:VCALENDAR
<alts>
<alt>boolean</alt>
<alt>integer</alt>
<alt>multi_selection</alt>
<alt>selection</alt>
<alt>string</alt>
<alt>text</alt>
Expand Down

0 comments on commit 65cd021

Please sign in to comment.