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

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