Blame 0139-cli-enable-polkit-authentication-on-command-line.patch

69165ba
From d2dcaeddfe015d3fee3817737e1bae72f1ad3316 Mon Sep 17 00:00:00 2001
69165ba
From: Jakub Filak <jfilak@redhat.com>
69165ba
Date: Wed, 1 Jul 2015 13:38:57 +0200
69165ba
Subject: [PATCH] cli: enable polkit authentication on command line
69165ba
69165ba
This patch will allow users to work with all problems without the need
69165ba
to run abrt-cli under root account.
69165ba
69165ba
The polkit aget will run in a separate thread and will interact with a
69165ba
user via STDOUT and STDIN, so we should not introduce new threads using
69165ba
STDIN or STDOUT and all D-Bus calls should be synchronous.
69165ba
69165ba
http://www.freedesktop.org/software/polkit/docs/latest/ref-authentication-agent-api.html
69165ba
69165ba
Related: #1224984
69165ba
69165ba
Signed-off-by: Jakub Filak <jfilak@redhat.com>
69165ba
69165ba
Conflicts:
69165ba
	src/cli/Makefile.am
69165ba
---
69165ba
 configure.ac            |  1 +
69165ba
 doc/abrt-cli.txt        | 11 +++++++++--
69165ba
 src/cli/Makefile.am     |  2 ++
69165ba
 src/cli/abrt-cli-core.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++--
69165ba
 src/cli/abrt-cli-core.h |  9 ++++++++-
69165ba
 src/cli/abrt-cli.c      | 15 ++++++++++++++-
69165ba
 6 files changed, 82 insertions(+), 6 deletions(-)
69165ba
69165ba
diff --git a/configure.ac b/configure.ac
69165ba
index d65bf54..56b8ad8 100644
69165ba
--- a/configure.ac
69165ba
+++ b/configure.ac
69165ba
@@ -104,6 +104,7 @@ PKG_CHECK_MODULES([NSS], [nss])
69165ba
 PKG_CHECK_MODULES([LIBREPORT], [libreport])
69165ba
 PKG_CHECK_MODULES([LIBREPORT_GTK], [libreport-gtk])
69165ba
 PKG_CHECK_MODULES([POLKIT], [polkit-gobject-1])
69165ba
+PKG_CHECK_MODULES([POLKIT_AGENT], [polkit-agent-1])
69165ba
 PKG_CHECK_MODULES([GIO], [gio-2.0])
69165ba
 PKG_CHECK_MODULES([SATYR], [satyr])
69165ba
 PKG_CHECK_MODULES([LIBSELINUX], [libselinux])
69165ba
diff --git a/doc/abrt-cli.txt b/doc/abrt-cli.txt
69165ba
index 399b5fd..0f18784 100644
69165ba
--- a/doc/abrt-cli.txt
69165ba
+++ b/doc/abrt-cli.txt
69165ba
@@ -7,6 +7,8 @@ abrt-cli - List, remove, print, analyze, report problems
69165ba
 
69165ba
 SYNOPSIS
69165ba
 --------
69165ba
+'abrt-cli' [--authenticate] COMMAND [COMMAND OPTIONS]
69165ba
+
69165ba
 'abrt-cli' list    [-vn] [--detailed] [--since NUM] [--until NUM] [DIR]...
69165ba
 
69165ba
 'abrt-cli' remove  [-v]  DIR...
69165ba
@@ -19,8 +21,13 @@ SYNOPSIS
69165ba
 
69165ba
 'abrt-cli' process [-v]  [--since NUM] DIR...
69165ba
 
69165ba
-OPTIONS
69165ba
--------
69165ba
+GLOBAL OPTIONS
69165ba
+--------------
69165ba
+-a,--authenticate::
69165ba
+   Enable PolicyKit authentication to be able to work with the system problems
69165ba
+
69165ba
+COMMAND OPTIONS
69165ba
+---------------
69165ba
 -v,--verbose::
69165ba
    Be more verbose. Can be given multiple times.
69165ba
 
69165ba
diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am
69165ba
index 9fff5b3..a7c76ef 100644
69165ba
--- a/src/cli/Makefile.am
69165ba
+++ b/src/cli/Makefile.am
69165ba
@@ -17,6 +17,7 @@ abrt_cli_CFLAGS = \
69165ba
 	-I$(srcdir)/../include \
69165ba
 	-I$(srcdir)/../lib \
69165ba
 	$(LIBREPORT_CFLAGS) \
69165ba
+	$(POLKIT_AGENT_CFLAGS) \
69165ba
 	-DWORKFLOWS_DIR=\"${WORKFLOWS_DIR}\"
69165ba
 
69165ba
 if SUGGEST_AUTOREPORTING
69165ba
@@ -24,6 +25,7 @@ abrt_cli_CFLAGS += -DSUGGEST_AUTOREPORTING=1
69165ba
 endif
69165ba
 
69165ba
 abrt_cli_LDADD = \
69165ba
+    $(POLKIT_AGENT_LIBS) \
69165ba
     $(LIBREPORT_LIBS) \
69165ba
     ../lib/libabrt.la
69165ba
 
69165ba
diff --git a/src/cli/abrt-cli-core.c b/src/cli/abrt-cli-core.c
69165ba
index 46acd01..ca49dbd 100644
69165ba
--- a/src/cli/abrt-cli-core.c
69165ba
+++ b/src/cli/abrt-cli-core.c
69165ba
@@ -20,6 +20,17 @@
69165ba
 #include "libabrt.h"
69165ba
 #include "abrt-cli-core.h"
69165ba
 
69165ba
+/* It is not possible to include polkitagent.h without the following define.
69165ba
+ * Check out the included header file.
69165ba
+ */
69165ba
+#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE
69165ba
+#include <polkitagent/polkitagent.h>
69165ba
+
69165ba
+int g_cli_authenticate;
69165ba
+
69165ba
+static PolkitAgentListener *s_local_polkit_agent = NULL;
69165ba
+static gpointer s_local_agent_handle = NULL;
69165ba
+
69165ba
 /* Vector of problems: */
69165ba
 /* problem_data_vector[i] = { "name" = { "content", CD_FLAG_foo_bits } } */
69165ba
 
69165ba
@@ -41,7 +52,7 @@ vector_of_problem_data_t *new_vector_of_problem_data(void)
69165ba
 
69165ba
 vector_of_problem_data_t *fetch_crash_infos(void)
69165ba
 {
69165ba
-    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
69165ba
+    GList *problems = get_problems_over_dbus(g_cli_authenticate);
69165ba
     if (problems == ERR_PTR)
69165ba
         return NULL;
69165ba
 
69165ba
@@ -97,7 +108,7 @@ char *find_problem_by_hash(const char *hash, GList *problems)
69165ba
 char *hash2dirname(const char *hash)
69165ba
 {
69165ba
     /* Try loading by dirname hash */
69165ba
-    GList *problems = get_problems_over_dbus(/*don't authorize*/false);
69165ba
+    GList *problems = get_problems_over_dbus(g_cli_authenticate);
69165ba
     if (problems == ERR_PTR)
69165ba
         return NULL;
69165ba
 
69165ba
@@ -112,3 +123,38 @@ char *hash2dirname_if_necessary(const char *input)
69165ba
 {
69165ba
     return isxdigit_str(input) ? hash2dirname(input) : xstrdup(input);
69165ba
 }
69165ba
+
69165ba
+void initialize_polkit_agent(void)
69165ba
+{
69165ba
+    GError *error = NULL;
69165ba
+    PolkitSubject *subject = polkit_unix_process_new_for_owner(
69165ba
+                                getpid(),
69165ba
+                                /*start time from /proc*/0,
69165ba
+                                getuid());
69165ba
+
69165ba
+    s_local_polkit_agent = polkit_agent_text_listener_new(NULL, &error);
69165ba
+    if (s_local_polkit_agent == NULL)
69165ba
+    {
69165ba
+        error_msg_and_die("polkit_agent_text_listener_new: %s (%s, %d)\n",
69165ba
+                error->message, g_quark_to_string (error->domain), error->code);
69165ba
+    }
69165ba
+
69165ba
+    s_local_agent_handle = polkit_agent_listener_register(s_local_polkit_agent,
69165ba
+            POLKIT_AGENT_REGISTER_FLAGS_RUN_IN_THREAD, subject, NULL, NULL, &error);
69165ba
+    if (s_local_agent_handle == NULL)
69165ba
+    {
69165ba
+        error_msg_and_die("polkit_agent_listener_register: %s (%s, %d)\n",
69165ba
+                error->message, g_quark_to_string (error->domain), error->code);
69165ba
+    }
69165ba
+
69165ba
+    g_object_unref(subject);
69165ba
+}
69165ba
+
69165ba
+void uninitialize_polkit_agent(void)
69165ba
+{
69165ba
+    if (s_local_agent_handle != NULL)
69165ba
+        polkit_agent_listener_unregister(s_local_agent_handle);
69165ba
+
69165ba
+    if (s_local_polkit_agent != NULL)
69165ba
+        g_object_unref(s_local_polkit_agent);
69165ba
+}
69165ba
diff --git a/src/cli/abrt-cli-core.h b/src/cli/abrt-cli-core.h
69165ba
index d69d463..e2456e6 100644
69165ba
--- a/src/cli/abrt-cli-core.h
69165ba
+++ b/src/cli/abrt-cli-core.h
69165ba
@@ -22,6 +22,10 @@
69165ba
 
69165ba
 #include "problem_api.h"
69165ba
 
69165ba
+/* Use authenticate D-Bus methods. The authentication requires a polkit agent
69165ba
+ * to finish an authenticated method successfully. */
69165ba
+extern int g_cli_authenticate;
69165ba
+
69165ba
 typedef GPtrArray vector_of_problem_data_t;
69165ba
 
69165ba
 problem_data_t *get_problem_data(vector_of_problem_data_t *vector, unsigned i);
69165ba
@@ -37,6 +41,9 @@ char *hash2dirname(const char *hash);
69165ba
 /* If input looks like a hash, returns malloced string, or NULL if not found.
69165ba
  * Otherwise returns a copy of the input. */
69165ba
 char *hash2dirname_if_necessary(const char *input);
69165ba
-
69165ba
+/* Initialize a new polkit text agent in a new thread */
69165ba
+void initialize_polkit_agent(void);
69165ba
+/* Uninitialize the polkit text agent */
69165ba
+void uninitialize_polkit_agent(void);
69165ba
 
69165ba
 #endif /* ABRT_CLI_CORE_H_ */
69165ba
diff --git a/src/cli/abrt-cli.c b/src/cli/abrt-cli.c
69165ba
index 8e19081..f45523e 100644
69165ba
--- a/src/cli/abrt-cli.c
69165ba
+++ b/src/cli/abrt-cli.c
69165ba
@@ -19,6 +19,7 @@
69165ba
 
69165ba
 #include "libabrt.h"
69165ba
 #include "builtin-cmd.h"
69165ba
+#include "abrt-cli-core.h"
69165ba
 
69165ba
 #define USAGE_OPTS_WIDTH 16
69165ba
 #define USAGE_GAP         2
69165ba
@@ -75,6 +76,10 @@ static unsigned handle_internal_options(int argc, const char **argv, const char
69165ba
         {
69165ba
             return skip + argc;
69165ba
         }
69165ba
+        else if (strcmp(cmd, "-a") == 0 || strcmp(cmd, "--authenticate") == 0)
69165ba
+        {
69165ba
+            g_cli_authenticate = 1;
69165ba
+        }
69165ba
         else
69165ba
             error_msg_and_die("%s", usage);
69165ba
 
69165ba
@@ -122,7 +127,7 @@ int main(int argc, const char **argv)
69165ba
     argc--;
69165ba
 
69165ba
     const char *abrt_cli_usage_string = _(
69165ba
-        "Usage: abrt-cli [--version] COMMAND [DIR]..."
69165ba
+        "Usage: abrt-cli [--authenticate] [--version] COMMAND [DIR]..."
69165ba
         );
69165ba
 
69165ba
     const struct cmd_struct commands[] = {
69165ba
@@ -141,8 +146,16 @@ int main(int argc, const char **argv)
69165ba
     argc -= skip;
69165ba
     argv += skip;
69165ba
     if (argc > 0)
69165ba
+    {
69165ba
+        if (g_cli_authenticate)
69165ba
+            initialize_polkit_agent();
69165ba
+
69165ba
         handle_internal_command(argc, argv, commands);
69165ba
 
69165ba
+        if (g_cli_authenticate)
69165ba
+            uninitialize_polkit_agent();
69165ba
+    }
69165ba
+
69165ba
     /* user didn't specify command; print out help */
69165ba
     printf("%s\n\n", abrt_cli_usage_string);
69165ba
     list_cmds_help(commands);
69165ba
-- 
69165ba
2.4.3
69165ba