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

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