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

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