Blame SOURCES/0132-cli-use-the-DBus-methods-for-getting-problem-informa.patch

06486d
From 5560ca0e51919bc5aeccb22584e24219040dc78b Mon Sep 17 00:00:00 2001
06486d
From: Jakub Filak <jfilak@redhat.com>
06486d
Date: Tue, 24 Mar 2015 20:57:34 +0100
06486d
Subject: [PATCH] cli: use the DBus methods for getting problem information
06486d
06486d
The dump directory is no longer accessible by non-root users and we also
06486d
want to get rid of direct access to allow administrators (wheel members)
06486d
see problem data without the need to ChownProblem directory before.
06486d
06486d
Related: #1224984
06486d
06486d
Signed-off-by: Jakub Filak <jfilak@redhat.com>
06486d
---
06486d
 src/cli/abrt-cli-core.c | 74 ++++++++++++++++++++++++-------------------------
06486d
 src/cli/abrt-cli-core.h |  4 ++-
06486d
 src/cli/list.c          | 45 +++++++++++-------------------
06486d
 src/cli/process.c       |  6 +---
06486d
 src/cli/status.c        | 66 +++++++++++++------------------------------
06486d
 5 files changed, 77 insertions(+), 118 deletions(-)
06486d
06486d
diff --git a/src/cli/abrt-cli-core.c b/src/cli/abrt-cli-core.c
06486d
index 23a74a8..77a37f7 100644
06486d
--- a/src/cli/abrt-cli-core.c
06486d
+++ b/src/cli/abrt-cli-core.c
06486d
@@ -39,24 +39,22 @@ vector_of_problem_data_t *new_vector_of_problem_data(void)
06486d
     return g_ptr_array_new_with_free_func((void (*)(void*)) &problem_data_free);
06486d
 }
06486d
 
06486d
-static int
06486d
-append_problem_data(struct dump_dir *dd, void *arg)
06486d
+vector_of_problem_data_t *fetch_crash_infos(void)
06486d
 {
06486d
-    vector_of_problem_data_t *vpd = arg;
06486d
-
06486d
-    problem_data_t *problem_data = create_problem_data_from_dump_dir(dd);
06486d
-    problem_data_add(problem_data, CD_DUMPDIR, dd->dd_dirname,
06486d
-                            CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE + CD_FLAG_LIST);
06486d
-    g_ptr_array_add(vpd, problem_data);
06486d
-    return 0;
06486d
-}
06486d
+    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
06486d
+    if (problems == ERR_PTR)
06486d
+        return NULL;
06486d
 
06486d
-vector_of_problem_data_t *fetch_crash_infos(GList *dir_list)
06486d
-{
06486d
     vector_of_problem_data_t *vpd = new_vector_of_problem_data();
06486d
 
06486d
-    for (GList *li = dir_list; li; li = li->next)
06486d
-        for_each_problem_in_dir(li->data, getuid(), append_problem_data, vpd);
06486d
+    for (GList *iter = problems; iter; iter = g_list_next(iter))
06486d
+    {
06486d
+        problem_data_t *problem_data = get_full_problem_data_over_dbus((const char *)(iter->data));
06486d
+        if (problem_data == ERR_PTR)
06486d
+            continue;
06486d
+
06486d
+        g_ptr_array_add(vpd, problem_data);
06486d
+    }
06486d
 
06486d
     return vpd;
06486d
 }
06486d
@@ -74,36 +72,38 @@ static bool isxdigit_str(const char *str)
06486d
     return true;
06486d
 }
06486d
 
06486d
-struct name_resolution_param {
06486d
-    const char *shortcut;
06486d
-    unsigned strlen_shortcut;
06486d
-    char *found_name;
06486d
-};
06486d
-
06486d
-static int find_dir_by_hash(struct dump_dir *dd, void *arg)
06486d
+char *find_problem_by_hash(const char *hash, GList *problems)
06486d
 {
06486d
-    struct name_resolution_param *param = arg;
06486d
-    char hash_str[SHA1_RESULT_LEN*2 + 1];
06486d
-    str_to_sha1str(hash_str, dd->dd_dirname);
06486d
-    if (strncasecmp(param->shortcut, hash_str, param->strlen_shortcut) == 0)
06486d
+    unsigned hash_len = strlen(hash);
06486d
+    if (!isxdigit_str(hash) || hash_len < 5)
06486d
+        return NULL;
06486d
+
06486d
+    char *found_name = NULL;
06486d
+    for (GList *iter = problems; iter; iter = g_list_next(iter))
06486d
     {
06486d
-        if (param->found_name)
06486d
-            error_msg_and_die(_("'%s' identifies more than one problem directory"), param->shortcut);
06486d
-        param->found_name = xstrdup(dd->dd_dirname);
06486d
+        char hash_str[SHA1_RESULT_LEN*2 + 1];
06486d
+        str_to_sha1str(hash_str, (const char *)(iter->data));
06486d
+        if (strncasecmp(hash, hash_str, hash_len) == 0)
06486d
+        {
06486d
+            if (found_name)
06486d
+                error_msg_and_die(_("'%s' identifies more than one problem directory"), hash);
06486d
+            found_name = xstrdup((const char *)(iter->data));
06486d
+        }
06486d
     }
06486d
-    return 0;
06486d
+
06486d
+    return found_name;
06486d
 }
06486d
 
06486d
 char *hash2dirname(const char *hash)
06486d
 {
06486d
-    unsigned hash_len = strlen(hash);
06486d
-    if (!isxdigit_str(hash) || hash_len < 5)
06486d
+    /* Try loading by dirname hash */
06486d
+    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
06486d
+    if (problems == ERR_PTR)
06486d
         return NULL;
06486d
 
06486d
-    /* Try loading by dirname hash */
06486d
-    struct name_resolution_param param = { hash, hash_len, NULL };
06486d
-    GList *dir_list = get_problem_storages();
06486d
-    for (GList *li = dir_list; li; li = li->next)
06486d
-        for_each_problem_in_dir(li->data, getuid(), find_dir_by_hash, ¶m;;
06486d
-    return param.found_name;
06486d
+    char *found_name = find_problem_by_hash(hash, problems);
06486d
+
06486d
+    g_list_free_full(problems, free);
06486d
+
06486d
+    return found_name;
06486d
 }
06486d
diff --git a/src/cli/abrt-cli-core.h b/src/cli/abrt-cli-core.h
06486d
index 83d0b5d..33b2ea6 100644
06486d
--- a/src/cli/abrt-cli-core.h
06486d
+++ b/src/cli/abrt-cli-core.h
06486d
@@ -28,9 +28,11 @@ problem_data_t *get_problem_data(vector_of_problem_data_t *vector, unsigned i);
06486d
 
06486d
 void free_vector_of_problem_data(vector_of_problem_data_t *vector);
06486d
 vector_of_problem_data_t *new_vector_of_problem_data(void);
06486d
-vector_of_problem_data_t *fetch_crash_infos(GList *dir_list);
06486d
+vector_of_problem_data_t *fetch_crash_infos(void);
06486d
 
06486d
 /* Returns malloced string, or NULL if not found: */
06486d
+char *find_problem_by_hash(const char *hash, GList *problems);
06486d
+/* Returns malloced string, or NULL if not found: */
06486d
 char *hash2dirname(const char *hash);
06486d
 
06486d
 
06486d
diff --git a/src/cli/list.c b/src/cli/list.c
06486d
index ccb5f3b..1594906 100644
06486d
--- a/src/cli/list.c
06486d
+++ b/src/cli/list.c
06486d
@@ -30,33 +30,28 @@
06486d
  *       ~/.abrt/spool and /var/tmp/abrt? needs more _meditation_.
06486d
  */
06486d
 
06486d
-static problem_data_t *load_problem_data(const char *dump_dir_name)
06486d
+static problem_data_t *load_problem_data(const char *problem_id)
06486d
 {
06486d
-    /* First, try loading by dirname */
06486d
-    int sv_logmode = logmode;
06486d
-    logmode = 0; /* suppress EPERM/EACCES errors in opendir */
06486d
-    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ DD_OPEN_READONLY);
06486d
-    logmode = sv_logmode;
06486d
+    char *name2 = NULL;
06486d
+
06486d
+    /* First, check if there is a problem with the passed id */
06486d
+    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
06486d
+    GList *item = g_list_find_custom(problems, problem_id, (GCompareFunc)strcmp);
06486d
 
06486d
     /* (git requires at least 5 char hash prefix, we do the same) */
06486d
-    if (!dd && errno == ENOENT)
06486d
+    if (item == NULL)
06486d
     {
06486d
         /* Try loading by dirname hash */
06486d
-        char *name2 = hash2dirname(dump_dir_name);
06486d
-        if (name2)
06486d
-            dd = dd_opendir(name2, /*flags:*/ DD_OPEN_READONLY);
06486d
-        free(name2);
06486d
-    }
06486d
+        name2 = find_problem_by_hash(problem_id, problems);
06486d
+        if (name2 == NULL)
06486d
+            return NULL;
06486d
 
06486d
-    if (!dd)
06486d
-        return NULL;
06486d
+        problem_id = name2;
06486d
+    }
06486d
 
06486d
-    problem_data_t *problem_data = create_problem_data_from_dump_dir(dd);
06486d
-    problem_data_add(problem_data, CD_DUMPDIR, dd->dd_dirname,
06486d
-                            CD_FLAG_TXT + CD_FLAG_ISNOTEDITABLE + CD_FLAG_LIST);
06486d
-    dd_close(dd);
06486d
+    problem_data_t *problem_data = get_full_problem_data_over_dbus(problem_id);
06486d
 
06486d
-    return problem_data;
06486d
+    return (problem_data == ERR_PTR ? NULL : problem_data);
06486d
 }
06486d
 
06486d
 /** Prints basic information about a crash to stdout. */
06486d
@@ -176,7 +171,7 @@ static bool print_crash_list(vector_of_problem_data_t *crash_list, int detailed,
06486d
 int cmd_list(int argc, const char **argv)
06486d
 {
06486d
     const char *program_usage_string = _(
06486d
-        "& list [options] [DIR]..."
06486d
+        "& list [options]"
06486d
         );
06486d
 
06486d
     int opt_not_reported = 0;
06486d
@@ -194,15 +189,8 @@ int cmd_list(int argc, const char **argv)
06486d
     };
06486d
 
06486d
     parse_opts(argc, (char **)argv, program_options, program_usage_string);
06486d
-    argv += optind;
06486d
-
06486d
-    GList *D_list = NULL;
06486d
-    while (*argv)
06486d
-        D_list = g_list_append(D_list, xstrdup(*argv++));
06486d
-    if (!D_list)
06486d
-        D_list = get_problem_storages();
06486d
 
06486d
-    vector_of_problem_data_t *ci = fetch_crash_infos(D_list);
06486d
+    vector_of_problem_data_t *ci = fetch_crash_infos();
06486d
 
06486d
     g_ptr_array_sort_with_data(ci, &cmp_problem_data, (char *) FILENAME_LAST_OCCURRENCE);
06486d
 
06486d
@@ -212,7 +200,6 @@ int cmd_list(int argc, const char **argv)
06486d
     print_crash_list(ci, opt_detailed, opt_not_reported, opt_since, opt_until, CD_TEXT_ATT_SIZE_BZ);
06486d
 
06486d
     free_vector_of_problem_data(ci);
06486d
-    list_free_with_free(D_list);
06486d
 
06486d
 #if SUGGEST_AUTOREPORTING != 0
06486d
     load_abrt_conf();
06486d
diff --git a/src/cli/process.c b/src/cli/process.c
06486d
index 7f4fff5..39462f9 100644
06486d
--- a/src/cli/process.c
06486d
+++ b/src/cli/process.c
06486d
@@ -152,18 +152,14 @@ int cmd_process(int argc, const char **argv)
06486d
     };
06486d
 
06486d
     parse_opts(argc, (char **)argv, program_options, program_usage_string);
06486d
-    argv += optind;
06486d
 
06486d
-    GList *D_list = get_problem_storages();
06486d
-
06486d
-    vector_of_problem_data_t *ci = fetch_crash_infos(D_list);
06486d
+    vector_of_problem_data_t *ci = fetch_crash_infos();
06486d
 
06486d
     g_ptr_array_sort_with_data(ci, &cmp_problem_data, (char *) FILENAME_LAST_OCCURRENCE);
06486d
 
06486d
     process_crashes(ci, opt_since);
06486d
 
06486d
     free_vector_of_problem_data(ci);
06486d
-    list_free_with_free(D_list);
06486d
 
06486d
     return 0;
06486d
 }
06486d
diff --git a/src/cli/status.c b/src/cli/status.c
06486d
index 1de2d41..68bdd0e 100644
06486d
--- a/src/cli/status.c
06486d
+++ b/src/cli/status.c
06486d
@@ -21,53 +21,36 @@
06486d
 #include <sys/types.h>
06486d
 #include "problem_api.h"
06486d
 
06486d
-struct time_range {
06486d
-    unsigned count;
06486d
-    unsigned long since;
06486d
-};
06486d
-
06486d
-static int count_dir_if_newer_than(struct dump_dir *dd, void *arg)
06486d
-{
06486d
-    struct time_range *me = arg;
06486d
-
06486d
-    if (dd_exist(dd, FILENAME_REPORTED_TO))
06486d
-        return 0;
06486d
-
06486d
-    char *time_str = dd_load_text(dd, FILENAME_LAST_OCCURRENCE);
06486d
-    long val = atol(time_str);
06486d
-    free(time_str);
06486d
-    if (val < me->since)
06486d
-        return 0;
06486d
-
06486d
-    me->count++;
06486d
-    return 0;
06486d
-}
06486d
-
06486d
-static void count_problems_in_dir(gpointer data, gpointer arg)
06486d
+static unsigned int count_problem_dirs(unsigned long since)
06486d
 {
06486d
-    char *path = data;
06486d
-    struct time_range *me = arg;
06486d
+    unsigned count = 0;
06486d
 
06486d
-    log_info("scanning '%s' for problems since %lu", path, me->since);
06486d
+    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
06486d
+    for (GList *iter = problems; iter != NULL; iter = g_list_next(iter))
06486d
+    {
06486d
+        const char *problem_id = (const char *)iter->data;
06486d
+        if (test_exist_over_dbus(problem_id, FILENAME_REPORTED_TO))
06486d
+            continue;
06486d
 
06486d
-    for_each_problem_in_dir(path, getuid(), count_dir_if_newer_than, me);
06486d
-}
06486d
+        char *time_str = load_text_over_dbus(problem_id, FILENAME_LAST_OCCURRENCE);
06486d
+        if (time_str == NULL)
06486d
+            continue;
06486d
 
06486d
-static unsigned int count_problem_dirs(GList *paths, unsigned long since)
06486d
-{
06486d
-    struct time_range me;
06486d
-    me.count = 0;
06486d
-    me.since = since;
06486d
+        long val = atol(time_str);
06486d
+        free(time_str);
06486d
+        if (val < since)
06486d
+            return 0;
06486d
 
06486d
-    g_list_foreach(paths, count_problems_in_dir, &me);
06486d
+        count++;
06486d
+    }
06486d
 
06486d
-    return me.count;
06486d
+    return count;
06486d
 }
06486d
 
06486d
 int cmd_status(int argc, const char **argv)
06486d
 {
06486d
     const char *program_usage_string = _(
06486d
-        "& status [DIR]..."
06486d
+        "& status"
06486d
         );
06486d
 
06486d
     int opt_bare = 0; /* must be _int_, OPT_BOOL expects that! */
06486d
@@ -81,17 +64,8 @@ int cmd_status(int argc, const char **argv)
06486d
     };
06486d
 
06486d
     parse_opts(argc, (char **)argv, program_options, program_usage_string);
06486d
-    argv += optind;
06486d
-
06486d
-    GList *problem_dir_list = NULL;
06486d
-    while (*argv)
06486d
-        problem_dir_list = g_list_append(problem_dir_list, xstrdup(*argv++));
06486d
-    if (!problem_dir_list)
06486d
-        problem_dir_list = get_problem_storages();
06486d
-
06486d
-    unsigned int problem_count = count_problem_dirs(problem_dir_list, opt_since);
06486d
 
06486d
-    list_free_with_free(problem_dir_list);
06486d
+    unsigned int problem_count = count_problem_dirs(opt_since);
06486d
 
06486d
     /* show only if there is at least 1 problem or user set the -v */
06486d
     if (problem_count > 0 || g_verbose > 0)
06486d
-- 
06486d
2.4.3
06486d