Skip to content

Commit

Permalink
Merge pull request #1681 from jhelmold/backport_1680
Browse files Browse the repository at this point in the history
 Add --rebuild-gvmd-data command line option (Backport #1680)
  • Loading branch information
jhelmold authored Sep 7, 2021
2 parents 1073e42 + af5292e commit c77c1a9
Show file tree
Hide file tree
Showing 16 changed files with 759 additions and 153 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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
7 changes: 6 additions & 1 deletion doc/gvmd.8
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Disable task scheduling.
Sets the path to the feed lock file.
.TP
\fB--feed-lock-timeout=\fITIMEOUT\fB\f1
Sets the number of seconds to retry for if the feed is locked in contexts (like migration or rebuilds) that do not retry on their own (like automatic syncs). Defaults to 0 (no retry).
Sets the number of seconds to retry for if the feed is locked in contexts (like migration or rebuilds) that do not retry on their own (like automatic syncs). Defaults to 0 (no retry).
.TP
\fB-f, --foreground\f1
Run in foreground.
Expand Down 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=\fITYPE\fB\f1
Rebuild SCAP data of type \fITYPE\f1 (currently only supports 'ovaldefs').
.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=<arg>TYPE</arg></opt></p>
<optdesc>
Expand Down
14 changes: 14 additions & 0 deletions doc/gvmd.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ <h2>Options</h2>




<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=<em>TYPE</em></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 @@ -1797,6 +1797,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 @@ -2006,6 +2007,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 @@ -2498,6 +2505,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 @@ -5017,14 +5017,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

0 comments on commit c77c1a9

Please sign in to comment.