From 29b95057f598a16fcb462b204413fe84340cbe81 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 16:02:22 +0200 Subject: [PATCH 1/7] Delete report format dirs last when deleting user with inherit --- src/manage_sql.c | 16 +++++++++++++++- src/manage_sql_report_formats.c | 25 ++++++++++++------------- src/manage_sql_report_formats.h | 5 ++++- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 87033c15d..0ccea87ae 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -52312,6 +52312,8 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, { gchar *deleted_user_id, *deleted_user_name; gchar *real_inheritor_id, *real_inheritor_name; + iterator_t rows; + gboolean has_rows; /* Transfer ownership of objects to the inheritor. */ @@ -52385,7 +52387,6 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, inheritor, user); inherit_port_lists (user, inheritor); - inherit_report_formats (user, inheritor); sql ("UPDATE reports SET owner = %llu WHERE owner = %llu;", inheritor, user); @@ -52446,6 +52447,10 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, sql ("UPDATE roles_trash SET owner = %llu WHERE owner = %llu;", inheritor, user); + /* Report Formats. */ + + has_rows = inherit_report_formats (inheritor, user, &rows); + /* Delete user. */ sql ("DELETE FROM group_users WHERE \"user\" = %llu;", user); @@ -52458,6 +52463,15 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, sql ("DELETE FROM settings WHERE owner = %llu;", user); sql ("DELETE FROM users WHERE id = %llu;", user); + /* Very last: report formats dirs. */ + + if (has_rows) + do + { + inherit_report_format_dir (iterator_string (&rows, 0), user, inheritor); + } while (next (&rows)); + cleanup_iterator (&rows); + sql_commit (); return 0; diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index b5897628e..f9b276750 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -3956,7 +3956,7 @@ empty_trashcan_report_formats () * @param[in] user Current owner. * @param[in] inheritor New owner. */ -static void +void inherit_report_format_dir (const gchar *report_format_id, user_t user, user_t inheritor) { @@ -4008,26 +4008,25 @@ inherit_report_format_dir (const gchar *report_format_id, user_t user, * * @param[in] user Current owner. * @param[in] inheritor New owner. + * @param[in] rows Iterator for inherited report formats, with next + * already called. + * + * @return TRUE if there is a row available, else FALSE. */ -void -inherit_report_formats (user_t user, user_t inheritor) +gboolean +inherit_report_formats (user_t user, user_t inheritor, iterator_t *rows) { - iterator_t rows; - - if (user == inheritor) - return; + sql ("UPDATE report_formats_trash SET owner = %llu WHERE owner = %llu;", + inheritor, user); - init_iterator (&rows, + 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); + /* This executes the SQL. */ + return next (rows); } /** diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index 4de735e40..9f120e4a3 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -58,8 +58,11 @@ delete_report_formats_user (user_t); int empty_trashcan_report_formats (); +gboolean +inherit_report_formats (user_t, user_t, iterator_t *); + void -inherit_report_formats (user_t, user_t); +inherit_report_format_dir (const gchar *, user_t, user_t); void update_report_format (report_format_t, const gchar *, const gchar *, From ab29dc3d49ad6130a9a630975266ab2620aaeb2c Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 17:09:45 +0200 Subject: [PATCH 2/7] Delete report format dirs last when deleting user --- src/manage_sql.c | 15 +++++-- src/manage_sql_report_formats.c | 69 +++++++++++++++++++++------------ src/manage_sql_report_formats.h | 5 ++- 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 0ccea87ae..43b91662e 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -52146,6 +52146,8 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, user_t user, inheritor; get_data_t get; char *current_uuid, *feed_owner_id; + gboolean has_rows; + iterator_t rows; assert (user_id_arg || name_arg); @@ -52312,8 +52314,6 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, { gchar *deleted_user_id, *deleted_user_name; gchar *real_inheritor_id, *real_inheritor_name; - iterator_t rows; - gboolean has_rows; /* Transfer ownership of objects to the inheritor. */ @@ -52701,7 +52701,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, return 9; } - /* Report formats (used by alerts). */ + /* Check report formats (used by alerts). */ if (user_resources_in_use (user, "report_formats", report_format_in_use, @@ -52711,7 +52711,6 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, sql_rollback (); return 9; } - delete_report_formats_user (user); /* Delete credentials last because they can be used in various places */ @@ -52775,10 +52774,18 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, sql ("DELETE FROM role_users WHERE \"user\" = %llu;", user); sql ("DELETE FROM role_users_trash WHERE \"user\" = %llu;", user); + /* Delete report formats. */ + + has_rows = delete_report_formats_user (user, &rows); + /* Delete user. */ sql ("DELETE FROM users WHERE id = %llu;", user); + /* Delete report format dirs. */ + + delete_report_format_dirs_user (user, has_rows ? &rows : NULL); + sql_commit (); return 0; } diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index f9b276750..d915f6603 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -4033,32 +4033,13 @@ inherit_report_formats (user_t user, user_t inheritor, iterator_t *rows) * @brief Delete all report formats owned by a user. * * @param[in] user The user. + * @param[in] rows Trash report format ids. + * + * @return TURE if there are rows in rows, else FALSE. */ -void -delete_report_formats_user (user_t user) +gboolean +delete_report_formats_user (user_t user, iterator_t *rows) { - gchar *dir, *user_id; - iterator_t rows; - - /* Remove trash report formats from trash directory. */ - - init_iterator (&rows, - "SELECT id FROM report_formats_trash WHERE owner = %llu;", - user); - while (next (&rows)) - { - gchar *id; - - id = g_strdup_printf ("%llu", iterator_int64 (&rows, 0)); - dir = report_format_trash_dir (id); - g_free (id); - if (gvm_file_remove_recurse (dir)) - g_warning ("%s: failed to remove dir %s, continuing anyway", - __func__, dir); - g_free (dir); - } - cleanup_iterator (&rows); - /* Remove report formats from db. */ sql ("DELETE FROM report_format_param_options" @@ -4085,7 +4066,45 @@ delete_report_formats_user (user_t user) " WHERE owner = %llu);", user); sql ("DELETE FROM report_formats WHERE owner = %llu;", user); - sql ("DELETE FROM report_formats_trash WHERE owner = %llu;", user); + init_iterator (rows, + "DELETE FROM report_formats_trash WHERE owner = %llu" + " RETURNING id;", + user); + + /* This executes the SQL. */ + return next (rows); +} + +/** + * @brief Delete all report formats owned by a user. + * + * @param[in] user The user. + * @param[in] rows Trash report format ids if any, else NULL. Cleaned up + * before returning. + */ +void +delete_report_format_dirs_user (user_t user, iterator_t *rows) +{ + gchar *dir, *user_id; + + /* Remove trash report formats from trash directory. */ + + if (rows) + { + do + { + gchar *id; + + id = g_strdup_printf ("%llu", iterator_int64 (rows, 0)); + dir = report_format_trash_dir (id); + g_free (id); + if (gvm_file_remove_recurse (dir)) + g_warning ("%s: failed to remove dir %s, continuing anyway", + __func__, dir); + g_free (dir); + } while (next (rows)); + cleanup_iterator (rows); + } /* Remove user's regular report formats directory. */ diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index 9f120e4a3..a4edd83e6 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -52,8 +52,11 @@ gchar * apply_report_format (gchar *, gchar *, gchar *, gchar *, GList **); +gboolean +delete_report_formats_user (user_t, iterator_t *); + void -delete_report_formats_user (user_t); +delete_report_format_dirs_user (user_t, iterator_t *); int empty_trashcan_report_formats (); From b2af0410bcbb7472f9f934961ebf0153f09a1b2f Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 17:10:51 +0200 Subject: [PATCH 3/7] Correct args --- src/manage_sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 43b91662e..233abbe60 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -52449,7 +52449,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, /* Report Formats. */ - has_rows = inherit_report_formats (inheritor, user, &rows); + has_rows = inherit_report_formats (user, inheritor, &rows); /* Delete user. */ From 9320b9d98f355ca0581a289f3380dc0a336a3ae4 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 17:15:23 +0200 Subject: [PATCH 4/7] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d4a0681..0cdd6e836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Clean up hosts strings before using them [#1352](https://github.com/greenbone/gvmd/pull/1352) - Improve SCP username and destination path handling [#1350](https://github.com/greenbone/gvmd/pull/1350) - Fix response memory handling in handle_osp_scan [#1364](https://github.com/greenbone/gvmd/pull/1364) - +- Delete report format dirs last when deleting a user [#1368](https://github.com/greenbone/gvmd/pull/1368) ### Removed - Remove DROP from vulns creation [#1281](http://github.com/greenbone/gvmd/pull/1281) From beead8b2e791cb051d4df52d53a898fad7fb79d0 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 19:00:56 +0200 Subject: [PATCH 5/7] Use user UUID from delete_user for inherit_report_format_dir --- src/manage_sql.c | 11 ++++++++--- src/manage_sql_report_formats.c | 14 +++----------- src/manage_sql_report_formats.h | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 233abbe60..95c892aa4 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -52348,7 +52348,6 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, real_inheritor_name, real_inheritor_id, deleted_user_name, deleted_user_id); - g_free (deleted_user_id); g_free (deleted_user_name); g_free (real_inheritor_id); g_free (real_inheritor_name); @@ -52465,11 +52464,17 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, /* Very last: report formats dirs. */ - if (has_rows) + if (deleted_user_id == NULL) + g_warning ("%s: deleted_user_id NULL, skipping dirs", __func__); + else if (has_rows) do { - inherit_report_format_dir (iterator_string (&rows, 0), user, inheritor); + inherit_report_format_dir (iterator_string (&rows, 0), + deleted_user_id, + inheritor); } while (next (&rows)); + + g_free (deleted_user_id); cleanup_iterator (&rows); sql_commit (); diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index d915f6603..8400551ad 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -3957,21 +3957,14 @@ empty_trashcan_report_formats () * @param[in] inheritor New owner. */ void -inherit_report_format_dir (const gchar *report_format_id, user_t user, +inherit_report_format_dir (const gchar *report_format_id, const gchar *user_id, user_t inheritor) { - gchar *user_id, *inheritor_id, *old_dir, *new_dir; + gchar *inheritor_id, *old_dir, *new_dir; - g_debug ("%s: %s from %llu to %llu", __func__, report_format_id, user, + g_debug ("%s: %s from %s to %llu", __func__, report_format_id, user_id, 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) { @@ -3991,7 +3984,6 @@ inherit_report_format_dir (const gchar *report_format_id, user_t user, report_format_id, NULL); - g_free (user_id); g_free (inheritor_id); if (move_report_format_dir (old_dir, new_dir)) diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index a4edd83e6..731170eca 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -65,7 +65,7 @@ gboolean inherit_report_formats (user_t, user_t, iterator_t *); void -inherit_report_format_dir (const gchar *, user_t, user_t); +inherit_report_format_dir (const gchar *, const gchar *, user_t); void update_report_format (report_format_t, const gchar *, const gchar *, From 0cd66c89a53a3d7742310007cf19b2eda8e34334 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 19:58:18 +0200 Subject: [PATCH 6/7] Use user UUID from delete_user for delete_report_format_dirs_user --- src/manage_sql.c | 11 +++++++++-- src/manage_sql_report_formats.c | 10 ++-------- src/manage_sql_report_formats.h | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 95c892aa4..d7c73ceb4 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -52148,6 +52148,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, char *current_uuid, *feed_owner_id; gboolean has_rows; iterator_t rows; + gchar *deleted_user_id; assert (user_id_arg || name_arg); @@ -52312,7 +52313,7 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, if (inheritor) { - gchar *deleted_user_id, *deleted_user_name; + gchar *deleted_user_name; gchar *real_inheritor_id, *real_inheritor_name; /* Transfer ownership of objects to the inheritor. */ @@ -52785,11 +52786,17 @@ delete_user (const char *user_id_arg, const char *name_arg, int ultimate, /* Delete user. */ + deleted_user_id = user_uuid (user); + sql ("DELETE FROM users WHERE id = %llu;", user); /* Delete report format dirs. */ - delete_report_format_dirs_user (user, has_rows ? &rows : NULL); + if (deleted_user_id) + delete_report_format_dirs_user (deleted_user_id, has_rows ? &rows : NULL); + else + g_warning ("%s: deleted_user_id NULL, skipping removal of report formats dir", + __func__); sql_commit (); return 0; diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index 8400551ad..be68f9640 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -4075,9 +4075,9 @@ delete_report_formats_user (user_t user, iterator_t *rows) * before returning. */ void -delete_report_format_dirs_user (user_t user, iterator_t *rows) +delete_report_format_dirs_user (const gchar *user_id, iterator_t *rows) { - gchar *dir, *user_id; + gchar *dir; /* Remove trash report formats from trash directory. */ @@ -4100,16 +4100,10 @@ delete_report_format_dirs_user (user_t user, iterator_t *rows) /* Remove user's regular report formats directory. */ - user_id = user_uuid (user); - if (user_id == NULL) - g_warning ("%s: user_id NULL, skipping removal of report formats dir", - __func__); - dir = g_build_filename (GVMD_STATE_DIR, "report_formats", user_id, NULL); - g_free (user_id); if (g_file_test (dir, G_FILE_TEST_EXISTS) && gvm_file_remove_recurse (dir)) diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index 731170eca..d44d48667 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -56,7 +56,7 @@ gboolean delete_report_formats_user (user_t, iterator_t *); void -delete_report_format_dirs_user (user_t, iterator_t *); +delete_report_format_dirs_user (const gchar *, iterator_t *); int empty_trashcan_report_formats (); From ae6942088381fa48e34b73d9e195f109534584c5 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 30 Nov 2020 20:03:17 +0200 Subject: [PATCH 7/7] Fix param docs --- src/manage_sql_report_formats.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index be68f9640..9f1a95a06 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -3953,7 +3953,7 @@ empty_trashcan_report_formats () * @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] user_id UUID of current owner. * @param[in] inheritor New owner. */ void @@ -4070,9 +4070,9 @@ delete_report_formats_user (user_t user, iterator_t *rows) /** * @brief Delete all report formats owned by a user. * - * @param[in] user The user. - * @param[in] rows Trash report format ids if any, else NULL. Cleaned up - * before returning. + * @param[in] user_id UUID of user. + * @param[in] rows Trash report format ids if any, else NULL. Cleaned up + * before returning. */ void delete_report_format_dirs_user (const gchar *user_id, iterator_t *rows)