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

Add --rebuild-gvmd-data command line option #1680

Merged
merged 13 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [21.4.4] (unreleased)
### Added
- Add --rebuild-gvmd-data command line option [#1680](https://github.com/greenbone/gvmd/pull/1680)

### Changed
### Deprecated
### Removed
Expand Down
5 changes: 5 additions & 0 deletions doc/gvmd.8
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ Use port number NUMBER.
\fB--port2=\fINUMBER\fB\f1
Use port number NUMBER for address 2.
.TP
\fB--rebuild-gvmd-data=\fITYPES\fB\f1
Reload all gvmd data objects of a given types from feed.

The types must be "all" or a comma-separated of the following: "configs", "port_lists" and "report_formats".
.TP
\fB--rebuild-scap\f1
Rebuild all SCAP data.
.TP
Expand Down
13 changes: 13 additions & 0 deletions doc/gvmd.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<p>Use port number NUMBER for address 2.</p>
</optdesc>
</option>
<option>
<p><opt>--rebuild-gvmd-data=<arg>TYPES</arg></opt></p>
<optdesc>
<p>
Reload all gvmd data objects of a given types from feed.
</p>
<p>
The types must be &quot;all&quot; or a comma-separated of the
following: &quot;configs&quot;, &quot;port_lists&quot; and
&quot;report_formats&quot;.
</p>
</optdesc>
</option>
<option>
<p><opt>--rebuild-scap</opt></p>
<optdesc>
Expand Down
16 changes: 14 additions & 2 deletions doc/gvmd.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,20 @@ <h2>Options</h2>

<p>Use port number NUMBER for address 2.</p>




<p><b>--rebuild-gvmd-data=<em>TYPES</em></b></p>

<p>
Reload all gvmd data objects of a given types from feed.
</p>

<p>
The types must be &quot;all&quot; or a comma-separated of the
following: &quot;configs&quot;, &quot;port_lists&quot; and
&quot;report_formats&quot;.
</p>


<p><b>--rebuild-scap</b></p>

<p>
Expand Down
33 changes: 33 additions & 0 deletions src/gvmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,7 @@ gvmd (int argc, char** argv)
static gchar *rc_name = NULL;
static gchar *relay_mapper = NULL;
static gboolean rebuild = FALSE;
static gchar *rebuild_gvmd_data = NULL;
static gboolean rebuild_scap = FALSE;
static gchar *role = NULL;
static gchar *disable = NULL;
Expand Down Expand Up @@ -2046,6 +2047,12 @@ gvmd (int argc, char** argv)
&rebuild,
"Remove NVT db, and rebuild it from the scanner.",
NULL },
{ "rebuild-gvmd-data", '\0', 0, G_OPTION_ARG_STRING,
&rebuild_gvmd_data,
"Reload all gvmd data objects of a given types from feed."
" The types must be \"all\" or a comma-separated of the following:"
" \"configs\", \"port_lists\" and \"report_formats\"",
"<types>" },
{ "rebuild-scap", '\0', 0, G_OPTION_ARG_NONE,
&rebuild_scap,
"Rebuild all SCAP data.",
Expand Down Expand Up @@ -2565,6 +2572,32 @@ gvmd (int argc, char** argv)
}
return EXIT_SUCCESS;
}

if (rebuild_gvmd_data)
{
int ret;
gchar *error_msg;

error_msg = NULL;

proctitle_set ("gvmd: --rebuild-gvmd-data");

if (option_lock (&lockfile_checking))
return EXIT_FAILURE;

ret = manage_rebuild_gvmd_data_from_feed (rebuild_gvmd_data,
log_config,
&database,
&error_msg);
log_config_free ();
if (ret)
{
printf ("Failed to rebuild gvmd data: %s\n", error_msg);
g_free (error_msg);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

if (rebuild_scap)
{
Expand Down
171 changes: 167 additions & 4 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -5006,14 +5006,177 @@ manage_sync (sigset_t *sigmask_current,
}
}

if (try_gvmd_data_sync)
if (try_gvmd_data_sync
&& (should_sync_configs ()
|| should_sync_port_lists ()
|| should_sync_report_formats ()))
{
manage_sync_configs ();
manage_sync_port_lists ();
manage_sync_report_formats ();
if (feed_lockfile_lock (&lockfile) == 0)
{
manage_sync_configs ();
manage_sync_port_lists ();
manage_sync_report_formats ();

lockfile_unlock (&lockfile);
}
}
}

/**
* @brief Adds a switch statement for handling the return value of a
* gvmd data rebuild.
* @param type The type as a description string, e.g. "port lists"
*/
#define REBUILD_SWITCH(type) \
switch (ret) \
{ \
case 0: \
g_message ("Rebuilt %s from feed.", type); \
break; \
case 1: \
if (error_msg) \
*error_msg = g_strdup_printf ("No %s feed directory.", \
type); \
return -1; \
case 2: \
if (error_msg) \
*error_msg = g_strdup_printf ("Feed owner not set or invalid" \
" while rebuilding %s.", \
type); \
return -1; \
case 3: \
if (error_msg) \
*error_msg = g_strdup_printf ("NVTs must be available" \
" while rebuilding %s.", \
type); \
return -1; \
default: \
if (error_msg) \
*error_msg = g_strdup_printf ("Internal error" \
" while rebuilding %s.", \
type); \
return -1; \
}

/**
* @brief Rebuild configs, port lists and report formats from feed.
*
* @param[in] types Comma-separated lists of types to rebuild or "all".
* @param[in] log_config Logging configuration list.
* @param[in] database Connection info for manage database.
* @param[out] error_msg Error message.
*
* @return 0 success, -1 failed.
*/
int
manage_rebuild_gvmd_data_from_feed (const char *types,
GSList *log_config,
const db_conn_info_t *database,
gchar **error_msg)
{
int ret;
lockfile_t lockfile;
gboolean sync_configs, sync_port_lists, sync_report_formats;

sync_configs = sync_port_lists = sync_report_formats = FALSE;

if (strcasecmp (types, "all") == 0)
{
sync_configs = TRUE;
sync_port_lists = TRUE;
sync_report_formats = TRUE;
}
else
{
gchar **split, **split_iter;
split = g_strsplit (types, ",", -1);

if (*split == NULL)
{
g_free (split);
if (error_msg)
*error_msg = g_strdup ("No types given.");
return -1;
}

split_iter = split;
while (*split_iter)
{
gchar *type = g_strstrip (*split_iter);

if (strcasecmp (type, "configs") == 0)
sync_configs = TRUE;
else if (strcasecmp (type, "port_lists") == 0)
sync_port_lists = TRUE;
else if (strcasecmp (type, "report_formats") == 0)
sync_report_formats = TRUE;
else
{
if (error_msg)
*error_msg = g_strdup_printf ("Invalid type \"%s\""
" (must be \"configs\","
" \"port_lists\","
" \"report_formats\""
" or \"all\")",
type);
g_strfreev (split);
return -1;
}
split_iter ++;
}
g_strfreev (split);
}

ret = feed_lockfile_lock_timeout (&lockfile);
if (ret == 1)
{
if (error_msg)
*error_msg = g_strdup ("Feed locked.");
return -1;
}
else if (ret)
{
if (error_msg)
*error_msg = g_strdup ("Error acquiring feed lock.");
return -1;
}

ret = manage_option_setup (log_config, database);
if (ret)
{
if (error_msg)
*error_msg = g_strdup ("Error setting up log config or"
" database connection.");
return -1;
}

if (sync_configs)
{
g_message ("Rebuilding configs from feed...");
ret = manage_rebuild_configs ();
REBUILD_SWITCH ("configs")
}

if (sync_port_lists)
{
g_message ("Rebuilding port lists from feed...");
ret = manage_rebuild_port_lists ();
REBUILD_SWITCH ("port lists")
}

if (sync_report_formats)
{
g_message ("Rebuilding report formats from feed...");
ret = manage_rebuild_report_formats ();
REBUILD_SWITCH ("report formats")
}

feed_lockfile_unlock (&lockfile);
return 0;
}

#undef REBUILD_SWITCH

/**
* @brief Schedule any actions that are due.
*
Expand Down
6 changes: 6 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,12 @@ set_scheduled_user_uuid (const gchar* uuid);
void
manage_sync (sigset_t *, int (*fork_update_nvt_cache) (), gboolean);

int
manage_rebuild_gvmd_data_from_feed (const char *,
GSList *,
const db_conn_info_t *,
gchar **);

int
manage_schedule (manage_connection_forker_t,
gboolean,
Expand Down
Loading