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

Copy instead of moving when migrating predefined report formats #1286

Merged
merged 4 commits into from
Sep 2, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Ignore min_qod when getting single results by UUID [#1276](http://github.com/greenbone/gvmd/pull/1276)
- Fix alternative options for radio type preferences when exporting a scan_config [#1278](http://github.com/greenbone/gvmd/pull/1278)
- Replace deprecated sys_siglist with strsignal [#1280](https://github.com/greenbone/gvmd/pull/1280)
- Copy instead of moving when migrating predefined report formats [#1286](https://github.com/greenbone/gvmd/pull/1286)

### Removed
- Remove DROP from vulns creation [#1281](http://github.com/greenbone/gvmd/pull/1281)
Expand Down
215 changes: 105 additions & 110 deletions src/manage_sql_report_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,160 +1274,72 @@ create_report_format_no_acl (const char *uuid, const char *name,
}

/**
* @brief Create Report Format from an existing Report Format.
* @brief Create a report format dir.
*
* @param[in] name Name of new Report Format. NULL to copy
* from existing.
* @param[in] source_uuid UUID of existing Report Format.
* @param[out] new_report_format New Report Format.
* @param[in] source_dir Full path of source directory, including UUID.
* @param[in] copy_parent Path of destination directory, excluding UUID.
* @param[in] copy_uuid UUID (dirname) of destination directory.
*
* @return 0 success, 1 Report Format exists already, 2 failed to find existing
* Report Format, 99 permission denied, -1 error.
* @return 0 success, -1 error.
*/
int
copy_report_format (const char* name, const char* source_uuid,
report_format_t* new_report_format)
static int
copy_report_format_dir (const gchar *source_dir, const gchar *copy_parent,
const gchar *copy_uuid)
{
report_format_t new, old;
gchar *copy_uuid, *source_dir, *copy_dir, *owner_uuid;
gchar *tmp_dir;
int ret;

assert (current_credentials.uuid);

sql_begin_immediate ();

ret = copy_resource_lock ("report_format", name, NULL, source_uuid,
"extension, content_type, summary, description,"
" signature, trust, trust_time, flags",
1, &new, &old);
if (ret)
{
sql_rollback ();
return ret;
}

sql ("UPDATE report_formats SET predefined = 0 WHERE id = %llu;", new);

if (report_format_predefined (old))
sql ("UPDATE report_formats SET trust = %i, trust_time = %i"
" WHERE id = %llu;",
TRUST_YES,
time (NULL),
new);

/* Copy report format parameters. */

sql ("INSERT INTO report_format_params "
" (report_format, name, type, value, type_min, type_max,"
" type_regex, fallback)"
" SELECT %llu, name, type, value, type_min, type_max,"
" type_regex, fallback"
" FROM report_format_params WHERE report_format = %llu;",
new,
old);
gchar *copy_dir;

/* Copy files on disk. */

owner_uuid = report_format_owner_uuid (old);
assert (owner_uuid);
source_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
owner_uuid,
source_uuid,
NULL);
g_free (owner_uuid);
g_debug ("%s: copy %s to %s/%s", __func__, source_dir, copy_parent,
copy_uuid);

/* Check that the source directory exists. */

if (!g_file_test (source_dir, G_FILE_TEST_EXISTS))
{
g_warning ("%s: report format directory %s not found",
__func__, source_dir);
g_free (source_dir);
sql_rollback ();
return -1;
}

copy_uuid = report_format_uuid (new);
if (copy_uuid == NULL)
{
sql_rollback ();
return -1;
}

/* Prepare directory to copy into. */

copy_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
current_credentials.uuid,
copy_uuid,
NULL);
copy_dir = g_build_filename (copy_parent, copy_uuid, NULL);

if (g_file_test (copy_dir, G_FILE_TEST_EXISTS)
&& gvm_file_remove_recurse (copy_dir))
{
g_warning ("%s: failed to remove dir %s", __func__, copy_dir);
g_free (source_dir);
g_free (copy_dir);
g_free (copy_uuid);
sql_rollback ();
return -1;
}

if (g_mkdir_with_parents (copy_dir, 0755 /* "rwxr-xr-x" */))
{
g_warning ("%s: failed to create dir %s", __func__, copy_dir);
g_free (source_dir);
g_free (copy_dir);
g_free (copy_uuid);
sql_rollback ();
return -1;
}

/* Correct permissions as glib doesn't seem to do so. */

tmp_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
current_credentials.uuid,
NULL);

if (chmod (tmp_dir, 0755 /* rwxr-xr-x */))
if (chmod (copy_parent, 0755 /* rwxr-xr-x */))
{
g_warning ("%s: chmod %s failed: %s",
__func__,
tmp_dir,
copy_parent,
strerror (errno));
g_free (source_dir);
g_free (copy_dir);
g_free (copy_uuid);
g_free (tmp_dir);
sql_rollback ();
return -1;
}
g_free (tmp_dir);

tmp_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
current_credentials.uuid,
copy_uuid,
NULL);

if (chmod (tmp_dir, 0755 /* rwxr-xr-x */))
if (chmod (copy_dir, 0755 /* rwxr-xr-x */))
{
g_warning ("%s: chmod %s failed: %s",
__func__,
tmp_dir,
copy_dir,
strerror (errno));
g_free (source_dir);
g_free (copy_dir);
g_free (copy_uuid);
g_free (tmp_dir);
sql_rollback ();
return -1;
}
g_free (tmp_dir);
g_free (copy_uuid);

/* Copy files into new directory. */
{
Expand All @@ -1444,9 +1356,7 @@ copy_report_format (const char* name, const char* source_uuid,
source_dir, error->message);
g_error_free (error);
}
g_free (source_dir);
g_free (copy_dir);
sql_rollback ();
return -1;
}
else
Expand All @@ -1466,9 +1376,7 @@ copy_report_format (const char* name, const char* source_uuid,
__func__, source_file, copy_file);
g_free (source_file);
g_free (copy_file);
g_free (source_dir);
g_free (copy_dir);
sql_rollback ();
return -1;
}
g_free (source_file);
Expand All @@ -1478,6 +1386,94 @@ copy_report_format (const char* name, const char* source_uuid,
}
}

g_free (copy_dir);
return 0;
}

/**
* @brief Create Report Format from an existing Report Format.
*
* @param[in] name Name of new Report Format. NULL to copy
* from existing.
* @param[in] source_uuid UUID of existing Report Format.
* @param[out] new_report_format New Report Format.
*
* @return 0 success, 1 Report Format exists already, 2 failed to find existing
* Report Format, 99 permission denied, -1 error.
*/
int
copy_report_format (const char* name, const char* source_uuid,
report_format_t* new_report_format)
{
report_format_t new, old;
gchar *copy_uuid, *source_dir, *copy_dir, *owner_uuid;
int ret;

assert (current_credentials.uuid);

sql_begin_immediate ();

ret = copy_resource_lock ("report_format", name, NULL, source_uuid,
"extension, content_type, summary, description,"
" signature, trust, trust_time, flags",
1, &new, &old);
if (ret)
{
sql_rollback ();
return ret;
}

sql ("UPDATE report_formats SET predefined = 0 WHERE id = %llu;", new);

if (report_format_predefined (old))
sql ("UPDATE report_formats SET trust = %i, trust_time = %i"
" WHERE id = %llu;",
TRUST_YES,
time (NULL),
new);

/* Copy report format parameters. */

sql ("INSERT INTO report_format_params "
" (report_format, name, type, value, type_min, type_max,"
" type_regex, fallback)"
" SELECT %llu, name, type, value, type_min, type_max,"
" type_regex, fallback"
" FROM report_format_params WHERE report_format = %llu;",
new,
old);

/* Copy files on disk. */

owner_uuid = report_format_owner_uuid (old);
assert (owner_uuid);
source_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
owner_uuid,
source_uuid,
NULL);
g_free (owner_uuid);

copy_uuid = report_format_uuid (new);
if (copy_uuid == NULL)
{
sql_rollback ();
return -1;
}

copy_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
current_credentials.uuid,
NULL);

if (copy_report_format_dir (source_dir, copy_dir, copy_uuid))
{
sql_rollback ();
g_free (source_dir);
g_free (copy_dir);
return -1;
}

sql_commit ();
g_free (source_dir);
g_free (copy_dir);
Expand Down Expand Up @@ -4289,10 +4285,9 @@ migrate_predefined_report_formats ()
new = g_build_filename (GVMD_STATE_DIR,
"report_formats",
owner_uuid,
iterator_string (&rows, 0),
NULL);

if (move_report_format_dir (old, new))
if (copy_report_format_dir (old, new, iterator_string (&rows, 0)))
{
g_warning ("%s: failed at report format %s", __func__,
iterator_string (&rows, 0));
Expand Down