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

Move report format dirs when inheriting user #989

Merged
merged 4 commits into from
Feb 24, 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 @@ -75,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix SecInfo alert filter conditions [#971](https://github.com/greenbone/gvmd/pull/971)
- Accept expanded scheme OIDs in parse_osp_report [#984](https://github.com/greenbone/gvmd/pull/984)
- Fix SCAP update not finishing when CPEs are older [#986](https://github.com/greenbone/gvmd/pull/986)
- Move report format dirs when inheriting user [#989](https://github.com/greenbone/gvmd/pull/989)

### Removed
- Remove support for "All SecInfo": removal of "allinfo" for type in get_info [#790](https://github.com/greenbone/gvmd/pull/790)
Expand Down
69 changes: 67 additions & 2 deletions src/manage_sql_report_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -3915,6 +3915,60 @@ empty_trashcan_report_formats ()
return 0;
}

/**
* @brief Change ownership of report formats, for user deletion.
*
* @param[in] report_format_id UUID of report format.
* @param[in] user Current owner.
* @param[in] inheritor New owner.
*/
static void
inherit_report_format_dir (const gchar *report_format_id, user_t user,
user_t inheritor)
{
gchar *user_id, *inheritor_id, *old_dir, *new_dir;

g_debug ("%s: %s from %llu to %llu", __func__, report_format_id, user,
inheritor);

user_id = user_uuid (user);
if (user_id == NULL)
{
g_warning ("%s: user_id NULL, skipping report format dir", __func__);
return;
}

inheritor_id = user_uuid (inheritor);
if (inheritor_id == NULL)
{
g_warning ("%s: inheritor_id NULL, skipping report format dir", __func__);
return;
}

old_dir = g_build_filename (GVMD_STATE_DIR,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be wrong for old predefined report formats. Their owner is changed in migrate_predefined_report_formats, but their files will still be in the old special dir (and not in the owner's report format dir). I'll fix this in a new PR.

"report_formats",
user_id,
report_format_id,
NULL);

new_dir = g_build_filename (GVMD_STATE_DIR,
"report_formats",
inheritor_id,
report_format_id,
NULL);

g_free (user_id);
g_free (inheritor_id);

if (move_report_format_dir (old_dir, new_dir))
g_warning ("%s: failed to move %s dir, but will try the rest",
report_format_id,
__func__);

g_free (old_dir);
g_free (new_dir);
}

/**
* @brief Change ownership of report formats, for user deletion.
*
Expand All @@ -3924,8 +3978,19 @@ empty_trashcan_report_formats ()
void
inherit_report_formats (user_t user, user_t inheritor)
{
sql ("UPDATE report_formats SET owner = %llu WHERE owner = %llu;",
inheritor, user);
iterator_t rows;

if (user == inheritor)
return;

init_iterator (&rows,
"UPDATE report_formats SET owner = %llu"
" WHERE owner = %llu"
" RETURNING uuid;",
inheritor, user);
while (next (&rows))
inherit_report_format_dir (iterator_string (&rows, 0), user, inheritor);
cleanup_iterator (&rows);

sql ("UPDATE report_formats_trash SET owner = %llu WHERE owner = %llu;",
inheritor, user);
Expand Down