Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to get NVT preferences by id in create_config (9.0) #821

Merged
merged 10 commits into from
Oct 24, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed
- Consider results_trash when deleting users [#799](https://github.com/greenbone/gvmd/pull/799)
- Try to get NVT preferences by id in create_config [#821](https://github.com/greenbone/gvmd/pull/821)

### Removed

Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ add_executable (manage-utils-test
manage_ranges_all_tcp_nmap_5_51_top_1000.c
manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c
manage_ranges_nmap_5_51_top_2000_top_100.c
manage_acl.c manage_config_discovery.c
manage_acl.c manage_configs.c manage_config_discovery.c
manage_config_host_discovery.c manage_config_system_discovery.c
manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c
manage_sql_configs.c
Expand All @@ -106,7 +106,7 @@ add_executable (manage-test
manage_ranges_all_tcp_nmap_5_51_top_1000.c
manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c
manage_ranges_nmap_5_51_top_2000_top_100.c
manage_acl.c manage_config_discovery.c
manage_acl.c manage_configs.c manage_config_discovery.c
manage_config_host_discovery.c manage_config_system_discovery.c
manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c
manage_sql_configs.c
Expand All @@ -130,7 +130,7 @@ add_executable (gmp-tickets-test
manage_ranges_all_tcp_nmap_5_51_top_1000.c
manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c
manage_ranges_nmap_5_51_top_2000_top_100.c
manage_acl.c manage_config_discovery.c
manage_acl.c manage_configs.c manage_config_discovery.c
manage_config_host_discovery.c manage_config_system_discovery.c
manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c
manage_sql_configs.c
Expand All @@ -154,7 +154,7 @@ add_executable (gvmd
manage_ranges_all_tcp_nmap_5_51_top_1000.c
manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c
manage_ranges_nmap_5_51_top_2000_top_100.c
manage_acl.c manage_config_discovery.c
manage_acl.c manage_configs.c manage_config_discovery.c
manage_config_host_discovery.c manage_config_system_discovery.c
manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c
manage_sql_configs.c
Expand Down
105 changes: 62 additions & 43 deletions src/gmp_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,42 +75,6 @@ nvt_selector_new (char *name, char *type, int include, char *family_or_nvt)
return selector;
}

/**
* @brief Create a new preference.
*
* @param[in] id ID of preference.
* @param[in] name Name of preference.
* @param[in] type Type of preference.
* @param[in] value Value of preference.
* @param[in] nvt_name Name of NVT of preference.
* @param[in] nvt_oid OID of NVT of preference.
* @param[in] alts Array of gchar's. Alternative values for type radio.
* @param[in] default_value Default value of preference.
* @param[in] hr_name Extended, more human-readable name of the preference.
*
* @return Newly allocated preference.
*/
static gpointer
preference_new (char *id, char *name, char *type, char *value, char *nvt_name,
char *nvt_oid, array_t *alts, char* default_value,
char *hr_name)
{
preference_t *preference;

preference = (preference_t*) g_malloc0 (sizeof (preference_t));
preference->id = id;
preference->name = name;
preference->type = type;
preference->value = value;
preference->nvt_name = nvt_name;
preference->nvt_oid = nvt_oid;
preference->alts = alts;
preference->default_value = default_value;
preference->hr_name = hr_name;

return preference;
}


/* CREATE_CONFIG. */

Expand Down Expand Up @@ -309,9 +273,10 @@ create_config_run (gmp_parser_t *gmp_parser, GError **error)
while ((preference = first_entity (children)))
{
entity_t pref_name, pref_nvt_name, hr_name, nvt, alt;
char *preference_hr_name;
char *preference_hr_name, *preference_nvt_oid;
array_t *import_alts;
entities_t alts;
preference_t *new_preference;

pref_name = entity_child (preference, "name");

Expand Down Expand Up @@ -341,17 +306,70 @@ create_config_run (gmp_parser_t *gmp_parser, GError **error)
}
array_terminate (import_alts);

array_add (import_preferences,
preference_new
preference_nvt_oid = attr_or_null (nvt, "oid");

if ((type == NULL || strcmp (entity_text (type), "0") == 0)
&& preference_nvt_oid
&& strcmp (preference_nvt_oid, ""))
{
/* Preference in an OpenVAS config:
* Get the preference from nvt_preferences */
char *preference_id, *preference_name, *preference_type;
char *preference_value;

preference_id
= text_or_null (entity_child (preference, "id"));
preference_name
= text_or_null (entity_child (preference, "name"));
preference_type
= text_or_null (entity_child (preference, "type"));
preference_value
= text_or_null (entity_child (preference, "value"));

if (preference_id && strcmp (preference_id, ""))
{
new_preference
= get_nvt_preference_by_id (preference_nvt_oid,
preference_id,
preference_name,
preference_type,
preference_value ?: "");
if (new_preference == NULL)
g_warning ("%s: Preference %s:%s not found",
__FUNCTION__,
preference_nvt_oid,
preference_id);
}
else
{
g_warning ("%s: Config contains a preference for NVT %s"
" without a preference id: %s",
__FUNCTION__,
preference_nvt_oid,
preference_name);
new_preference = NULL;
}
}
else
{
/* Scanner preference (for OpenVAS or OSP configs):
* Use directly from imported config.
*/
new_preference
= preference_new
(text_or_null (entity_child (preference, "id")),
text_or_null (pref_name),
text_or_null (entity_child (preference, "type")),
text_or_null (entity_child (preference, "value")),
text_or_null (pref_nvt_name),
attr_or_null (nvt, "oid"),
preference_nvt_oid,
import_alts,
text_or_null (entity_child (preference, "default")),
preference_hr_name));
preference_hr_name,
0 /* do not free strings */);
}

array_add (import_preferences, new_preference);

children = next_entities (children);
}
Expand Down Expand Up @@ -440,10 +458,11 @@ create_config_run (gmp_parser_t *gmp_parser, GError **error)
pref = (preference_t*) g_ptr_array_index (import_preferences,
index);
if (pref)
g_ptr_array_free (pref->alts, TRUE);
preference_free (pref);
}
g_ptr_array_free (import_preferences, TRUE);
}
array_free (import_preferences);

array_free (import_nvt_selectors);

create_config_reset ();
Expand Down
1 change: 1 addition & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,7 @@ typedef struct
array_t *alts; ///< Array of gchar's. Alternate values for radio type.
char *default_value; ///< Default value of preference.
char *hr_name; ///< Extended, more human-readable name used by OSP.
int free_strings;///< Whether string fields are freed by preference_free.
} preference_t;

/**
Expand Down
96 changes: 96 additions & 0 deletions src/manage_configs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright (C) 2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* @file
* @brief GVM manage layer: Configs.
*
* General functions for managing scan configs.
*/

#include "manage_configs.h"

#include <glib.h>
#include <stdlib.h>

/**
* @brief Create a new preference.
*
* @param[in] id ID of preference.
* @param[in] name Name of preference.
* @param[in] type Type of preference.
* @param[in] value Value of preference.
* @param[in] nvt_name Name of NVT of preference.
* @param[in] nvt_oid OID of NVT of preference.
* @param[in] alts Array of gchar's. Alternative values for type radio.
* @param[in] default_value Default value of preference.
* @param[in] hr_name Extended, more human-readable name of the preference.
* @param[in] free_strings Whether string fields are freed by preference_free.
*
* @return Newly allocated preference.
*/
gpointer
preference_new (char *id, char *name, char *type, char *value, char *nvt_name,
char *nvt_oid, array_t *alts, char* default_value,
char *hr_name, int free_strings)
{
preference_t *preference;

preference = (preference_t*) g_malloc0 (sizeof (preference_t));
preference->id = id;
preference->name = name;
preference->type = type;
preference->value = value;
preference->nvt_name = nvt_name;
preference->nvt_oid = nvt_oid;
preference->alts = alts;
preference->default_value = default_value;
preference->hr_name = hr_name;
preference->free_strings = free_strings;

return preference;
}

/**
* @brief Frees a preference including its assigned values.
*
* @param[in] preference The preference to free.
*/
void
preference_free (preference_t *preference)
{
if (preference == NULL)
return;

if (preference->alts)
g_ptr_array_free (preference->alts, TRUE);
if (preference->free_strings)
{
free (preference->id);
free (preference->name);
free (preference->type);
free (preference->value);
free (preference->nvt_name);
free (preference->nvt_oid);
free (preference->default_value);
free (preference->hr_name);
}

g_free (preference);
}
14 changes: 14 additions & 0 deletions src/manage_configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@

#include "manage.h"

gpointer
preference_new (char *, char *, char *, char *, char *,
mattmundell marked this conversation as resolved.
Show resolved Hide resolved
char *, array_t *, char*,
char *, int);

void
preference_free (preference_t *);

preference_t *
get_nvt_preference_by_id (const char *,
const char *,
const char *,
const char *,
const char *);

#endif /* not _GVMD_MANAGE_CONFIGS_H */
Loading