Blame SOURCES/0001-subman-Add-a-new-plugin-to-provide-system-subscripti.patch

06f198
From bee6d42503ec9b56f6e10704db02bcedb6a4fdf1 Mon Sep 17 00:00:00 2001
06f198
From: Richard Hughes <rhughes@redhat.com>
06f198
Date: Thu, 20 Aug 2020 11:16:09 -0400
06f198
Subject: [PATCH 01/15] subman: Add a new plugin to provide system subscription
06f198
 registration
06f198
06f198
---
06f198
 meson.build                                   |   1 +
06f198
 plugins/meson.build                           |   1 +
06f198
 plugins/subman/README.md                      |  56 +
06f198
 plugins/subman/gsd-subman-common.c            |  36 +
06f198
 plugins/subman/gsd-subman-common.h            |  40 +
06f198
 plugins/subman/gsd-subman-helper.c            | 378 +++++++
06f198
 plugins/subman/gsd-subscription-manager.c     | 982 ++++++++++++++++++
06f198
 plugins/subman/gsd-subscription-manager.h     |  63 ++
06f198
 plugins/subman/main.c                         |   8 +
06f198
 plugins/subman/meson.build                    |  56 +
06f198
 ...ome.SettingsDaemon.Subscription.desktop.in |   9 +
06f198
 ...ettings-daemon.plugins.subman.policy.in.in |  27 +
06f198
 ...gnome.settings-daemon.plugins.subman.rules |   7 +
06f198
 13 files changed, 1664 insertions(+)
06f198
 create mode 100644 plugins/subman/README.md
06f198
 create mode 100644 plugins/subman/gsd-subman-common.c
06f198
 create mode 100644 plugins/subman/gsd-subman-common.h
06f198
 create mode 100644 plugins/subman/gsd-subman-helper.c
06f198
 create mode 100644 plugins/subman/gsd-subscription-manager.c
06f198
 create mode 100644 plugins/subman/gsd-subscription-manager.h
06f198
 create mode 100644 plugins/subman/main.c
06f198
 create mode 100644 plugins/subman/meson.build
06f198
 create mode 100644 plugins/subman/org.gnome.SettingsDaemon.Subscription.desktop.in
06f198
 create mode 100644 plugins/subman/org.gnome.settings-daemon.plugins.subman.policy.in.in
06f198
 create mode 100644 plugins/subman/org.gnome.settings-daemon.plugins.subman.rules
06f198
06f198
diff --git a/meson.build b/meson.build
06f198
index 1632ea05..27bf8c4c 100644
06f198
--- a/meson.build
06f198
+++ b/meson.build
06f198
@@ -70,60 +70,61 @@ if gsd_buildtype.contains('debug')
06f198
   ]
06f198
 
06f198
   compiler_flags = cc.get_supported_arguments(test_cflags)
06f198
 elif gsd_buildtype.contains('release')
06f198
   common_flags += ['-DG_DISABLE_CAST_CHECKS']
06f198
 endif
06f198
 
06f198
 # Workaround for meson's bug
06f198
 # https://github.com/mesonbuild/meson/pull/1896
06f198
 if get_option('b_ndebug') == true
06f198
   common_flags += ['-DG_DISABLE_ASSERT']
06f198
 endif
06f198
 
06f198
 add_project_arguments(common_flags + compiler_flags, language: 'c')
06f198
 
06f198
 glib_dep = dependency('glib-2.0', version: '>= 2.56')
06f198
 colord_dep = dependency('colord', version: '>= 1.0.2')
06f198
 geocode_glib_dep = dependency('geocode-glib-1.0', version: '>= 3.10.0')
06f198
 gio_dep = dependency('gio-2.0', version: '>= 2.53.0')
06f198
 gio_unix_dep = dependency('gio-unix-2.0')
06f198
 gnome_desktop_dep = dependency('gnome-desktop-3.0', version: '>= 3.11.1')
06f198
 gsettings_desktop_dep = dependency('gsettings-desktop-schemas', version: '>= 3.27.90')
06f198
 gtk_dep = dependency('gtk+-3.0', version: '>= 3.15.3')
06f198
 gtk_x11_dep = dependency('gtk+-x11-3.0')
06f198
 gweather_dep = dependency('gweather-3.0', version: '>= 3.9.5')
06f198
 lcms_dep = dependency('lcms2', version: '>= 2.2')
06f198
 libcanberra_gtk_dep = dependency('libcanberra-gtk3')
06f198
 libgeoclue_dep = dependency('libgeoclue-2.0', version: '>= 2.3.1')
06f198
 libnotify_dep = dependency('libnotify', version: '>= 0.7.3')
06f198
 libpulse_mainloop_glib_dep = dependency('libpulse-mainloop-glib', version: '>= 2.0')
06f198
+jsonglib_dep = dependency('json-glib-1.0', version: '>= 1.1.1')
06f198
 pango_dep = dependency('pango', version: '>= 1.20.0')
06f198
 polkit_gobject_dep = dependency('polkit-gobject-1', version: '>= 0.103')
06f198
 upower_glib_dep = dependency('upower-glib', version: '>= 0.99.0')
06f198
 x11_dep = dependency('x11')
06f198
 
06f198
 m_dep = cc.find_library('m')
06f198
 dl_dep = cc.find_library('dl')
06f198
 
06f198
 # ALSA integration (default enabled)
06f198
 enable_alsa = get_option('alsa')
06f198
 assert(enable_alsa or not host_is_linux, 'ALSA is not optional on Linux platforms')
06f198
 
06f198
 libgvc = subproject(
06f198
   'gvc',
06f198
   default_options: [
06f198
     'static=true',
06f198
     'alsa=' + enable_alsa.to_string()
06f198
   ]
06f198
 )
06f198
 libgvc_dep = libgvc.get_variable('libgvc_dep')
06f198
 
06f198
 # GUdev integration (default enabled)
06f198
 enable_gudev = get_option('gudev')
06f198
 if enable_gudev
06f198
   gudev_dep = dependency('gudev-1.0')
06f198
 endif
06f198
 config_h.set10('HAVE_GUDEV', enable_gudev)
06f198
 if host_is_linux
06f198
   assert(enable_gudev, 'GUdev is not optional on Linux platforms')
06f198
 endif
06f198
diff --git a/plugins/meson.build b/plugins/meson.build
06f198
index 3c4d42ac..4c9caf47 100644
06f198
--- a/plugins/meson.build
06f198
+++ b/plugins/meson.build
06f198
@@ -1,35 +1,36 @@
06f198
 enabled_plugins = [
06f198
   ['a11y-settings', 'A11ySettings'],
06f198
   ['account', 'Account'],
06f198
   ['clipboard', 'Clipboard'],
06f198
   ['color', 'Color'],
06f198
+  ['subman', 'Subscription'],
06f198
   ['datetime', 'Datetime'],
06f198
   ['dummy', ''],
06f198
   ['power', 'Power'],
06f198
   ['housekeeping', 'Housekeeping'],
06f198
   ['keyboard', 'Keyboard'],
06f198
   ['media-keys', 'MediaKeys'],
06f198
   ['mouse', 'Mouse'],
06f198
   ['screensaver-proxy', 'ScreensaverProxy'],
06f198
   ['sharing', 'Sharing'],
06f198
   ['sound', 'Sound'],
06f198
   ['xsettings', 'XSettings']
06f198
 ]
06f198
 
06f198
 if enable_smartcard
06f198
   enabled_plugins += [['smartcard', 'Smartcard']]
06f198
 endif
06f198
 
06f198
 if enable_wacom
06f198
   enabled_plugins += [['wacom', 'Wacom']]
06f198
 endif
06f198
 
06f198
 if enable_cups
06f198
   enabled_plugins += [['print-notifications', 'PrintNotifications']]
06f198
 endif
06f198
 
06f198
 if enable_rfkill
06f198
   enabled_plugins += [['rfkill', 'Rfkill']]
06f198
 endif
06f198
 
06f198
 plugins_conf = configuration_data()
06f198
diff --git a/plugins/subman/README.md b/plugins/subman/README.md
06f198
new file mode 100644
06f198
index 00000000..3e1cc3cd
06f198
--- /dev/null
06f198
+++ b/plugins/subman/README.md
06f198
@@ -0,0 +1,56 @@
06f198
+GNOME Settings Daemon: Subscription Manager Plugin
06f198
+==================================================
06f198
+
06f198
+Testing:
06f198
+
06f198
+To add a test acccount on subscription.rhsm.stage.redhat.com, use Ethel:
06f198
+http://account-manager-stage.app.eng.rdu2.redhat.com/#view
06f198
+
06f198
+Register with a username and password
06f198
+-------------------------------------
06f198
+
06f198
+    gdbus call \
06f198
+     --session \
06f198
+     --dest org.gnome.SettingsDaemon.Subscription \
06f198
+     --object-path /org/gnome/SettingsDaemon/Subscription \
06f198
+     --method org.gnome.SettingsDaemon.Subscription.Register "{'kind':<'username'>,'hostname':<'subscription.rhsm.stage.redhat.com'>,'username':<'rhughes_test'>,'password':<'barbaz'>}"
06f198
+
06f198
+To register with a certificate
06f198
+------------------------------
06f198
+
06f198
+    gdbus call \
06f198
+     --session \
06f198
+     --dest org.gnome.SettingsDaemon.Subscription \
06f198
+     --object-path /org/gnome/SettingsDaemon/Subscription \
06f198
+     --method org.gnome.SettingsDaemon.Subscription.Register "{'kind':<'key'>,'hostname':<'subscription.rhsm.stage.redhat.com'>,'organisation':<'foo'>,'activation-key':<'barbaz'>}"
06f198
+
06f198
+To unregister
06f198
+-------------
06f198
+
06f198
+    gdbus call \
06f198
+     --session \
06f198
+     --dest org.gnome.SettingsDaemon.Subscription \
06f198
+     --object-path /org/gnome/SettingsDaemon/Subscription \
06f198
+     --method org.gnome.SettingsDaemon.Subscription.Unregister
06f198
+
06f198
+Debugging
06f198
+---------
06f198
+
06f198
+Get the UNIX socket using `Subscription.Register` then call something like:
06f198
+
06f198
+    sudo G_MESSAGES_DEBUG=all ./plugins/subman/gsd-subman-helper \
06f198
+     --address="unix:abstract=/var/run/dbus-ulGB1wfnbn,guid=71e6bf329d861ce366df7a1d5d036a5b" \
06f198
+     --kind="register-with-username" \
06f198
+     --username="rhughes_test" \
06f198
+     --password="barbaz" \
06f198
+     --hostname="subscription.rhsm.stage.redhat.com" \
06f198
+     --organisation=""
06f198
+
06f198
+You can all see some basic debugging running `rhsmd` in the foreground:
06f198
+
06f198
+    sudo /usr/libexec/rhsmd -d -k
06f198
+
06f198
+Known Limitations
06f198
+=================
06f198
+
06f198
+Proxy servers are not supported, nor are custom host ports or prefixes.
06f198
diff --git a/plugins/subman/gsd-subman-common.c b/plugins/subman/gsd-subman-common.c
06f198
new file mode 100644
06f198
index 00000000..e515131e
06f198
--- /dev/null
06f198
+++ b/plugins/subman/gsd-subman-common.c
06f198
@@ -0,0 +1,36 @@
06f198
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
06f198
+ *
06f198
+ * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
06f198
+ *
06f198
+ * This program is free software; you can redistribute it and/or modify
06f198
+ * it under the terms of the GNU General Public License as published by
06f198
+ * the Free Software Foundation; either version 2 of the License, or
06f198
+ * (at your option) any later version.
06f198
+ *
06f198
+ * This program is distributed in the hope that it will be useful,
06f198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
+ * GNU General Public License for more details.
06f198
+ *
06f198
+ * You should have received a copy of the GNU General Public License
06f198
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
06f198
+ *
06f198
+ */
06f198
+
06f198
+#include "config.h"
06f198
+
06f198
+#include "gsd-subman-common.h"
06f198
+
06f198
+const gchar *
06f198
+gsd_subman_subscription_status_to_string (GsdSubmanSubscriptionStatus status)
06f198
+{
06f198
+	if (status == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID)
06f198
+		return "valid";
06f198
+	if (status == GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID)
06f198
+		return "invalid";
06f198
+	if (status == GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED)
06f198
+		return "disabled";
06f198
+	if (status == GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID)
06f198
+		return "partially-valid";
06f198
+	return "unknown";
06f198
+}
06f198
diff --git a/plugins/subman/gsd-subman-common.h b/plugins/subman/gsd-subman-common.h
06f198
new file mode 100644
06f198
index 00000000..fccf9f6a
06f198
--- /dev/null
06f198
+++ b/plugins/subman/gsd-subman-common.h
06f198
@@ -0,0 +1,40 @@
06f198
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
06f198
+ *
06f198
+ * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
06f198
+ *
06f198
+ * This program is free software; you can redistribute it and/or modify
06f198
+ * it under the terms of the GNU General Public License as published by
06f198
+ * the Free Software Foundation; either version 2 of the License, or
06f198
+ * (at your option) any later version.
06f198
+ *
06f198
+ * This program is distributed in the hope that it will be useful,
06f198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
+ * GNU General Public License for more details.
06f198
+ *
06f198
+ * You should have received a copy of the GNU General Public License
06f198
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
06f198
+ *
06f198
+ */
06f198
+
06f198
+#ifndef __GSD_SUBMAN_COMMON_H
06f198
+#define __GSD_SUBMAN_COMMON_H
06f198
+
06f198
+#include <glib-object.h>
06f198
+
06f198
+G_BEGIN_DECLS
06f198
+
06f198
+typedef enum {
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN,
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID,
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID,
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED,
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID,
06f198
+	GSD_SUBMAN_SUBSCRIPTION_STATUS_LAST
06f198
+} GsdSubmanSubscriptionStatus;
06f198
+
06f198
+const gchar	*gsd_subman_subscription_status_to_string	(GsdSubmanSubscriptionStatus	 status);
06f198
+
06f198
+G_END_DECLS
06f198
+
06f198
+#endif /* __GSD_SUBMAN_COMMON_H */
06f198
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
06f198
new file mode 100644
06f198
index 00000000..182f7190
06f198
--- /dev/null
06f198
+++ b/plugins/subman/gsd-subman-helper.c
06f198
@@ -0,0 +1,378 @@
06f198
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
06f198
+ *
06f198
+ * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
06f198
+ *
06f198
+ * Licensed under the GNU General Public License Version 2
06f198
+ *
06f198
+ * This program is free software; you can redistribute it and/or modify
06f198
+ * it under the terms of the GNU General Public License as published by
06f198
+ * the Free Software Foundation; either version 2 of the License, or
06f198
+ * (at your option) any later version.
06f198
+ *
06f198
+ * This program is distributed in the hope that it will be useful,
06f198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
+ * GNU General Public License for more details.
06f198
+ *
06f198
+ * You should have received a copy of the GNU General Public License
06f198
+ * along with this program; if not, write to the Free Software
06f198
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
06f198
+ */
06f198
+
06f198
+#include "config.h"
06f198
+
06f198
+#include <sys/types.h>
06f198
+#include <unistd.h>
06f198
+#include <stdlib.h>
06f198
+
06f198
+#include <gio/gio.h>
06f198
+#include <json-glib/json-glib.h>
06f198
+
06f198
+static void
06f198
+_helper_convert_error (const gchar *json_txt, GError **error)
06f198
+{
06f198
+	JsonNode *json_root;
06f198
+	JsonObject *json_obj;
06f198
+	const gchar *message;
06f198
+	g_autoptr(JsonParser) json_parser = json_parser_new ();
06f198
+
06f198
+	/* this may be plain text or JSON :| */
06f198
+	if (!json_parser_load_from_data (json_parser, json_txt, -1, NULL)) {
06f198
+		g_set_error_literal (error,
06f198
+				     G_IO_ERROR,
06f198
+				     G_IO_ERROR_NOT_SUPPORTED,
06f198
+				     json_txt);
06f198
+		return;
06f198
+	}
06f198
+	json_root = json_parser_get_root (json_parser);
06f198
+	json_obj = json_node_get_object (json_root);
06f198
+	if (!json_object_has_member (json_obj, "message")) {
06f198
+		g_set_error (error,
06f198
+			     G_IO_ERROR,
06f198
+			     G_IO_ERROR_INVALID_DATA,
06f198
+			     "no message' in %s", json_txt);
06f198
+		return;
06f198
+	}
06f198
+	message = json_object_get_string_member (json_obj, "message");
06f198
+	if (g_strstr_len (message, -1, "Invalid user credentials") != NULL) {
06f198
+		g_set_error_literal (error,
06f198
+				     G_IO_ERROR,
06f198
+				     G_IO_ERROR_PERMISSION_DENIED,
06f198
+				     message);
06f198
+		return;
06f198
+	}
06f198
+	g_set_error_literal (error,
06f198
+			     G_IO_ERROR,
06f198
+			     G_IO_ERROR_NOT_SUPPORTED,
06f198
+			     message);
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_helper_unregister (GError **error)
06f198
+{
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
+	g_autoptr(GVariant) res = NULL;
06f198
+
06f198
+	g_debug ("unregistering");
06f198
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
+					       NULL,
06f198
+					       "com.redhat.RHSM1",
06f198
+					       "/com/redhat/RHSM1/Unregister",
06f198
+					       "com.redhat.RHSM1.Unregister",
06f198
+					       NULL, error);
06f198
+	if (proxy == NULL) {
06f198
+		g_prefix_error (error, "Failed to get proxy: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
06f198
+	res = g_dbus_proxy_call_sync (proxy,
06f198
+				      "Unregister",
06f198
+				      g_variant_new ("(a{sv}s)",
06f198
+						     proxy_options,
06f198
+						     ""), /* lang */
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	return res != NULL;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_helper_auto_attach (GError **error)
06f198
+{
06f198
+	const gchar *str = NULL;
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
+	g_autoptr(GVariant) res = NULL;
06f198
+
06f198
+	g_debug ("auto-attaching subscriptions");
06f198
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
+					       NULL,
06f198
+					       "com.redhat.RHSM1",
06f198
+					       "/com/redhat/RHSM1/Attach",
06f198
+					       "com.redhat.RHSM1.Attach",
06f198
+					       NULL, error);
06f198
+	if (proxy == NULL) {
06f198
+		g_prefix_error (error, "Failed to get proxy: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
06f198
+	res = g_dbus_proxy_call_sync (proxy,
06f198
+				      "AutoAttach",
06f198
+				      g_variant_new ("(sa{sv}s)",
06f198
+						     "", /* now? */
06f198
+						     proxy_options,
06f198
+						     ""), /* lang */
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (res == NULL)
06f198
+		return FALSE;
06f198
+	g_variant_get (res, "(&s)", &str);
06f198
+	g_debug ("Attach.AutoAttach: %s", str);
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_helper_save_config (const gchar *key, const gchar *value, GError **error)
06f198
+{
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GVariant) res = NULL;
06f198
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
+					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
+					       NULL,
06f198
+					       "com.redhat.RHSM1",
06f198
+					       "/com/redhat/RHSM1/Config",
06f198
+					       "com.redhat.RHSM1.Config",
06f198
+					       NULL, error);
06f198
+	if (proxy == NULL) {
06f198
+		g_prefix_error (error, "Failed to get proxy: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	res = g_dbus_proxy_call_sync (proxy, "Set",
06f198
+				      g_variant_new ("(svs)",
06f198
+						     key,
06f198
+						     g_variant_new_string (value),
06f198
+						     ""), /* lang */
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	return res != NULL;
06f198
+}
06f198
+
06f198
+int
06f198
+main (int argc, char *argv[])
06f198
+{
06f198
+	const gchar *userlang = ""; /* as root, so no translations */
06f198
+	g_autofree gchar *activation_key = NULL;
06f198
+	g_autofree gchar *address = NULL;
06f198
+	g_autofree gchar *hostname = NULL;
06f198
+	g_autofree gchar *kind = NULL;
06f198
+	g_autofree gchar *organisation = NULL;
06f198
+	g_autofree gchar *password = NULL;
06f198
+	g_autofree gchar *port = NULL;
06f198
+	g_autofree gchar *prefix = NULL;
06f198
+	g_autofree gchar *proxy_server = NULL;
06f198
+	g_autofree gchar *username = NULL;
06f198
+	g_autoptr(GDBusConnection) conn_private = NULL;
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GError) error = NULL;
06f198
+	g_autoptr(GOptionContext) context = g_option_context_new (NULL);
06f198
+	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
+	g_autoptr(GVariantBuilder) subman_conopts = NULL;
06f198
+	g_autoptr(GVariantBuilder) subman_options = NULL;
06f198
+
06f198
+	const GOptionEntry options[] = {
06f198
+		{ "kind", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&kind, "Kind, e.g. 'username' or 'key'", NULL },
06f198
+		{ "address", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&address, "UNIX address", NULL },
06f198
+		{ "username", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&username, "Username", NULL },
06f198
+		{ "password", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&password, "Password", NULL },
06f198
+		{ "organisation", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&organisation, "Organisation", NULL },
06f198
+		{ "activation-key", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
+			&activation_key, "Activation keys", NULL },
06f198
+		{ "hostname", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
06f198
+			&hostname, "Registration server hostname", NULL },
06f198
+		{ "prefix", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
06f198
+			&prefix, "Registration server prefix", NULL },
06f198
+		{ "port", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
06f198
+			&port, "Registration server port", NULL },
06f198
+		{ "proxy", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
06f198
+			&proxy_server, "Proxy settings", NULL },
06f198
+		{ NULL}
06f198
+	};
06f198
+
06f198
+	/* check calling UID */
06f198
+	if (getuid () != 0 || geteuid () != 0) {
06f198
+		g_printerr ("This program can only be used by the root user\n");
06f198
+		return G_IO_ERROR_NOT_SUPPORTED;
06f198
+	}
06f198
+	g_option_context_add_main_entries (context, options, NULL);
06f198
+	if (!g_option_context_parse (context, &argc, &argv, &error)) {
06f198
+		g_printerr ("Failed to parse arguments: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_SUPPORTED;
06f198
+	}
06f198
+
06f198
+	/* uncommon actions */
06f198
+	if (kind == NULL) {
06f198
+		g_printerr ("No --kind specified\n");
06f198
+		return G_IO_ERROR_INVALID_DATA;
06f198
+	}
06f198
+	if (g_strcmp0 (kind, "unregister") == 0) {
06f198
+		if (!_helper_unregister (&error)) {
06f198
+			g_printerr ("Failed to Unregister: %s\n", error->message);
06f198
+			return G_IO_ERROR_NOT_INITIALIZED;
06f198
+		}
06f198
+		return EXIT_SUCCESS;
06f198
+	}
06f198
+	if (g_strcmp0 (kind, "auto-attach") == 0) {
06f198
+		if (!_helper_auto_attach (&error)) {
06f198
+			g_printerr ("Failed to AutoAttach: %s\n", error->message);
06f198
+			return G_IO_ERROR_NOT_INITIALIZED;
06f198
+		}
06f198
+		return EXIT_SUCCESS;
06f198
+	}
06f198
+
06f198
+	/* connect to abstract socket for reasons */
06f198
+	if (address == NULL) {
06f198
+		g_printerr ("No --address specified\n");
06f198
+		return G_IO_ERROR_INVALID_DATA;
06f198
+	}
06f198
+	conn_private = g_dbus_connection_new_for_address_sync (address,
06f198
+							       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
06f198
+							       NULL, NULL,
06f198
+							       &error);
06f198
+	if (conn_private == NULL) {
06f198
+		g_printerr ("Invalid --address specified: %s\n", error->message);
06f198
+		return G_IO_ERROR_INVALID_DATA;
06f198
+	}
06f198
+	proxy = g_dbus_proxy_new_sync (conn_private,
06f198
+				       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
06f198
+				       NULL, /* GDBusInterfaceInfo */
06f198
+				       NULL, /* name */
06f198
+				       "/com/redhat/RHSM1/Register",
06f198
+				       "com.redhat.RHSM1.Register",
06f198
+				       NULL, &error);
06f198
+	if (proxy == NULL) {
06f198
+		g_printerr ("Count not contact RHSM: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_FOUND;
06f198
+	}
06f198
+
06f198
+	/* no options */
06f198
+	subman_options = g_variant_builder_new (G_VARIANT_TYPE("a{ss}"));
06f198
+
06f198
+	/* set registration server */
06f198
+	if (hostname == NULL || hostname[0] == '\0')
06f198
+		hostname = g_strdup ("subscription.rhsm.redhat.com");
06f198
+	if (prefix == NULL || prefix[0] == '\0')
06f198
+		prefix = g_strdup ("/subscription");
06f198
+	if (port == NULL || port[0] == '\0')
06f198
+		port = g_strdup ("443");
06f198
+	subman_conopts = g_variant_builder_new (G_VARIANT_TYPE("a{ss}"));
06f198
+	g_variant_builder_add (subman_conopts, "{ss}", "host", hostname);
06f198
+	g_variant_builder_add (subman_conopts, "{ss}", "handler", prefix);
06f198
+	g_variant_builder_add (subman_conopts, "{ss}", "port", port);
06f198
+
06f198
+	/* call into RHSM */
06f198
+	if (g_strcmp0 (kind, "register-with-key") == 0) {
06f198
+		g_auto(GStrv) activation_keys = NULL;
06f198
+		g_autoptr(GError) error_local = NULL;
06f198
+		g_autoptr(GVariant) res = NULL;
06f198
+
06f198
+		if (activation_key == NULL) {
06f198
+			g_printerr ("Required --activation-key\n");
06f198
+			return G_IO_ERROR_INVALID_DATA;
06f198
+		}
06f198
+		if (organisation == NULL) {
06f198
+			g_printerr ("Required --organisation\n");
06f198
+			return G_IO_ERROR_INVALID_DATA;
06f198
+		}
06f198
+
06f198
+		g_debug ("registering using activation key");
06f198
+		activation_keys = g_strsplit (activation_key, ",", -1);
06f198
+		res = g_dbus_proxy_call_sync (proxy,
06f198
+					      "RegisterWithActivationKeys",
06f198
+					      g_variant_new ("(s^asa{ss}a{ss}s)",
06f198
+							     organisation,
06f198
+							     activation_keys,
06f198
+							     subman_options,
06f198
+							     subman_conopts,
06f198
+							     userlang),
06f198
+					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
+					      -1, NULL, &error_local);
06f198
+		if (res == NULL) {
06f198
+			g_dbus_error_strip_remote_error (error_local);
06f198
+			_helper_convert_error (error_local->message, &error);
06f198
+			g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
06f198
+			return error->code;
06f198
+		}
06f198
+	} else if (g_strcmp0 (kind, "register-with-username") == 0) {
06f198
+		g_autoptr(GError) error_local = NULL;
06f198
+		g_autoptr(GVariant) res = NULL;
06f198
+
06f198
+		g_debug ("registering using username and password");
06f198
+		if (username == NULL) {
06f198
+			g_printerr ("Required --username\n");
06f198
+			return G_IO_ERROR_INVALID_DATA;
06f198
+		}
06f198
+		if (password == NULL) {
06f198
+			g_printerr ("Required --password\n");
06f198
+			return G_IO_ERROR_INVALID_DATA;
06f198
+		}
06f198
+		if (organisation == NULL) {
06f198
+			g_printerr ("Required --organisation\n");
06f198
+			return G_IO_ERROR_INVALID_DATA;
06f198
+		}
06f198
+		res = g_dbus_proxy_call_sync (proxy,
06f198
+					      "Register",
06f198
+					      g_variant_new ("(sssa{ss}a{ss}s)",
06f198
+							     organisation,
06f198
+							     username,
06f198
+							     password,
06f198
+							     subman_options,
06f198
+							     subman_conopts,
06f198
+							     userlang),
06f198
+					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
+					      -1, NULL, &error_local);
06f198
+		if (res == NULL) {
06f198
+			g_dbus_error_strip_remote_error (error_local);
06f198
+			_helper_convert_error (error_local->message, &error);
06f198
+			g_printerr ("Failed to Register: %s\n", error->message);
06f198
+			return error->code;
06f198
+		}
06f198
+	} else {
06f198
+		g_printerr ("Invalid --kind specified: %s\n", kind);
06f198
+		return G_IO_ERROR_INVALID_DATA;
06f198
+	}
06f198
+
06f198
+	/* set the new hostname */
06f198
+	if (!_helper_save_config ("server.hostname", hostname, &error)) {
06f198
+		g_printerr ("Failed to save hostname: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_INITIALIZED;
06f198
+	}
06f198
+	if (!_helper_save_config ("server.prefix", prefix, &error)) {
06f198
+		g_printerr ("Failed to save prefix: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_INITIALIZED;
06f198
+	}
06f198
+	if (!_helper_save_config ("server.port", port, &error)) {
06f198
+		g_printerr ("Failed to save port: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_INITIALIZED;
06f198
+	}
06f198
+
06f198
+	/* wait for rhsmd to notice the new config */
06f198
+	g_usleep (G_USEC_PER_SEC * 5);
06f198
+
06f198
+	/* auto-attach */
06f198
+	if (!_helper_auto_attach (&error)) {
06f198
+		g_printerr ("Failed to AutoAttach: %s\n", error->message);
06f198
+		return G_IO_ERROR_NOT_INITIALIZED;
06f198
+	}
06f198
+
06f198
+	return EXIT_SUCCESS;
06f198
+}
06f198
diff --git a/plugins/subman/gsd-subscription-manager.c b/plugins/subman/gsd-subscription-manager.c
06f198
new file mode 100644
06f198
index 00000000..08b13fa6
06f198
--- /dev/null
06f198
+++ b/plugins/subman/gsd-subscription-manager.c
06f198
@@ -0,0 +1,982 @@
06f198
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
06f198
+ *
06f198
+ * Copyright (C) 2019 Richard Hughes <richard@hughsie.com>
06f198
+ *
06f198
+ * This program is free software; you can redistribute it and/or modify
06f198
+ * it under the terms of the GNU General Public License as published by
06f198
+ * the Free Software Foundation; either version 2 of the License, or
06f198
+ * (at your option) any later version.
06f198
+ *
06f198
+ * This program is distributed in the hope that it will be useful,
06f198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
+ * GNU General Public License for more details.
06f198
+ *
06f198
+ * You should have received a copy of the GNU General Public License
06f198
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
06f198
+ *
06f198
+ */
06f198
+
06f198
+#include "config.h"
06f198
+
06f198
+#include <glib/gi18n.h>
06f198
+#include <gdk/gdk.h>
06f198
+#include <gtk/gtk.h>
06f198
+#include <json-glib/json-glib.h>
06f198
+#include <libnotify/notify.h>
06f198
+
06f198
+#include "gnome-settings-profile.h"
06f198
+#include "gsd-subman-common.h"
06f198
+#include "gsd-subscription-manager.h"
06f198
+
06f198
+#define GSD_DBUS_NAME "org.gnome.SettingsDaemon"
06f198
+#define GSD_DBUS_PATH "/org/gnome/SettingsDaemon"
06f198
+#define GSD_DBUS_BASE_INTERFACE "org.gnome.SettingsDaemon"
06f198
+
06f198
+#define GSD_SUBSCRIPTION_DBUS_NAME		GSD_DBUS_NAME ".Subscription"
06f198
+#define GSD_SUBSCRIPTION_DBUS_PATH		GSD_DBUS_PATH "/Subscription"
06f198
+#define GSD_SUBSCRIPTION_DBUS_INTERFACE		GSD_DBUS_BASE_INTERFACE ".Subscription"
06f198
+
06f198
+static const gchar introspection_xml[] =
06f198
+"<node>"
06f198
+"  <interface name='org.gnome.SettingsDaemon.Subscription'>"
06f198
+"    <method name='Register'>"
06f198
+"      <arg type='a{sv}' name='options' direction='in'/>"
06f198
+"    </method>"
06f198
+"    <method name='Unregister'/>"
06f198
+"    <property name='SubscriptionStatus' type='u' access='read'/>"
06f198
+"  </interface>"
06f198
+"</node>";
06f198
+
06f198
+#define GSD_SUBSCRIPTION_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_SUBSCRIPTION_MANAGER, GsdSubscriptionManagerPrivate))
06f198
+
06f198
+typedef enum {
06f198
+	_RHSM_INTERFACE_CONFIG,
06f198
+	_RHSM_INTERFACE_REGISTER_SERVER,
06f198
+	_RHSM_INTERFACE_ATTACH,
06f198
+	_RHSM_INTERFACE_ENTITLEMENT,
06f198
+	_RHSM_INTERFACE_PRODUCTS,
06f198
+	_RHSM_INTERFACE_CONSUMER,
06f198
+	_RHSM_INTERFACE_SYSPURPOSE,
06f198
+	_RHSM_INTERFACE_LAST
06f198
+} _RhsmInterface;
06f198
+
06f198
+struct GsdSubscriptionManagerPrivate
06f198
+{
06f198
+	/* D-Bus */
06f198
+	guint		 name_id;
06f198
+	GDBusNodeInfo	*introspection_data;
06f198
+	GDBusConnection	*connection;
06f198
+	GCancellable	*bus_cancellable;
06f198
+
06f198
+	GDBusProxy	*proxies[_RHSM_INTERFACE_LAST];
06f198
+	const gchar	*userlang;	/* owned by GLib internally */
06f198
+	GHashTable	*config; 	/* str:str */
06f198
+	gchar		*address;
06f198
+
06f198
+	GTimer		*timer_last_notified;
06f198
+	NotifyNotification	*notification_expired;
06f198
+	NotifyNotification	*notification_registered;
06f198
+	NotifyNotification	*notification_registration_required;
06f198
+	GsdSubmanSubscriptionStatus	 subscription_status;
06f198
+	GsdSubmanSubscriptionStatus	 subscription_status_last;
06f198
+};
06f198
+
06f198
+enum {
06f198
+	PROP_0,
06f198
+};
06f198
+
06f198
+static void     gsd_subscription_manager_class_init  (GsdSubscriptionManagerClass *klass);
06f198
+static void     gsd_subscription_manager_init        (GsdSubscriptionManager      *subscription_manager);
06f198
+static void     gsd_subscription_manager_finalize    (GObject             *object);
06f198
+
06f198
+G_DEFINE_TYPE (GsdSubscriptionManager, gsd_subscription_manager, G_TYPE_OBJECT)
06f198
+
06f198
+static gpointer manager_object = NULL;
06f198
+
06f198
+GQuark
06f198
+gsd_subscription_manager_error_quark (void)
06f198
+{
06f198
+	static GQuark quark = 0;
06f198
+	if (!quark)
06f198
+		quark = g_quark_from_static_string ("gsd_subscription_manager_error");
06f198
+	return quark;
06f198
+}
06f198
+
06f198
+static GsdSubmanSubscriptionStatus
06f198
+_client_subscription_status_from_text (const gchar *status_txt)
06f198
+{
06f198
+	if (g_strcmp0 (status_txt, "Unknown") == 0)
06f198
+		return GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN;
06f198
+	if (g_strcmp0 (status_txt, "Current") == 0)
06f198
+		return GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID;
06f198
+	if (g_strcmp0 (status_txt, "Invalid") == 0)
06f198
+		return GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID;
06f198
+	if (g_strcmp0 (status_txt, "Disabled") == 0)
06f198
+		return GSD_SUBMAN_SUBSCRIPTION_STATUS_DISABLED;
06f198
+	if (g_strcmp0 (status_txt, "Insufficient") == 0)
06f198
+		return GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID;
06f198
+	g_warning ("Unknown subscription status: %s", status_txt); // 'Current'?
06f198
+	return GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN;
06f198
+}
06f198
+
06f198
+static void
06f198
+_emit_property_changed (GsdSubscriptionManager *manager,
06f198
+		        const gchar *property_name,
06f198
+		        GVariant *property_value)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	GVariantBuilder builder;
06f198
+	GVariantBuilder invalidated_builder;
06f198
+
06f198
+	/* not yet connected */
06f198
+	if (priv->connection == NULL)
06f198
+		return;
06f198
+
06f198
+	/* build the dict */
06f198
+	g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
06f198
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
06f198
+	g_variant_builder_add (&builder,
06f198
+			       "{sv}",
06f198
+			       property_name,
06f198
+			       property_value);
06f198
+	g_dbus_connection_emit_signal (priv->connection,
06f198
+				       NULL,
06f198
+				       GSD_SUBSCRIPTION_DBUS_PATH,
06f198
+				       "org.freedesktop.DBus.Properties",
06f198
+				       "PropertiesChanged",
06f198
+				       g_variant_new ("(sa{sv}as)",
06f198
+				       GSD_SUBSCRIPTION_DBUS_INTERFACE,
06f198
+				       &builder,
06f198
+				       &invalidated_builder),
06f198
+				       NULL);
06f198
+	g_variant_builder_clear (&builder);
06f198
+	g_variant_builder_clear (&invalidated_builder);
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_subscription_status_update (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	JsonNode *json_root;
06f198
+	JsonObject *json_obj;
06f198
+	const gchar *json_txt = NULL;
06f198
+	const gchar *status_txt = NULL;
06f198
+	g_autoptr(GVariant) val = NULL;
06f198
+	g_autoptr(JsonParser) json_parser = json_parser_new ();
06f198
+
06f198
+	/* save old value */
06f198
+	priv->subscription_status_last = priv->subscription_status;
06f198
+
06f198
+	val = g_dbus_proxy_call_sync (priv->proxies[_RHSM_INTERFACE_ENTITLEMENT],
06f198
+				      "GetStatus",
06f198
+				      g_variant_new ("(ss)",
06f198
+						     "", /* assumed as 'now' */
06f198
+						     priv->userlang),
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (val == NULL)
06f198
+		return FALSE;
06f198
+	g_variant_get (val, "(&s)", &json_txt);
06f198
+	g_debug ("Entitlement.GetStatus JSON: %s", json_txt);
06f198
+	if (!json_parser_load_from_data (json_parser, json_txt, -1, error))
06f198
+		return FALSE;
06f198
+	json_root = json_parser_get_root (json_parser);
06f198
+	json_obj = json_node_get_object (json_root);
06f198
+	if (!json_object_has_member (json_obj, "status")) {
06f198
+		g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
06f198
+			     "no Entitlement.GetStatus status in %s", json_txt);
06f198
+		return FALSE;
06f198
+	}
06f198
+
06f198
+	status_txt = json_object_get_string_member (json_obj, "status");
06f198
+	g_debug ("Entitlement.GetStatus: %s", status_txt);
06f198
+	priv->subscription_status = _client_subscription_status_from_text (status_txt);
06f198
+
06f198
+	/* emit notification for g-c-c */
06f198
+	if (priv->subscription_status != priv->subscription_status_last) {
06f198
+		_emit_property_changed (manager, "SubscriptionStatus",
06f198
+				       g_variant_new_uint32 (priv->subscription_status));
06f198
+	}
06f198
+
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_syspurpose_update (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	JsonNode *json_root;
06f198
+	JsonObject *json_obj;
06f198
+	const gchar *json_txt = NULL;
06f198
+	g_autoptr(GVariant) val = NULL;
06f198
+	g_autoptr(JsonParser) json_parser = json_parser_new ();
06f198
+
06f198
+	val = g_dbus_proxy_call_sync (priv->proxies[_RHSM_INTERFACE_SYSPURPOSE],
06f198
+				      "GetSyspurpose",
06f198
+				      g_variant_new ("(s)", priv->userlang),
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (val == NULL)
06f198
+		return FALSE;
06f198
+	g_variant_get (val, "(&s)", &json_txt);
06f198
+	g_debug ("Syspurpose.GetSyspurpose JSON: %s", json_txt);
06f198
+	if (!json_parser_load_from_data (json_parser, json_txt, -1, error))
06f198
+		return FALSE;
06f198
+	json_root = json_parser_get_root (json_parser);
06f198
+	json_obj = json_node_get_object (json_root);
06f198
+	if (!json_object_has_member (json_obj, "status")) {
06f198
+		g_debug ("Syspurpose.GetSyspurpose: Unknown");
06f198
+		return TRUE;
06f198
+	}
06f198
+	g_debug ("Syspurpose.GetSyspurpose: '%s", json_object_get_string_member (json_obj, "status"));
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_register_start (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	const gchar *address = NULL;
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GVariant) val = NULL;
06f198
+
06f198
+	/* already started */
06f198
+	if (priv->address != NULL)
06f198
+		return TRUE;
06f198
+
06f198
+	/* apparently: "we can't send registration credentials over the regular
06f198
+	 * system or session bus since those aren't really locked down..." */
06f198
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+					       G_DBUS_PROXY_FLAGS_NONE,
06f198
+					       NULL,
06f198
+					       "com.redhat.RHSM1",
06f198
+					       "/com/redhat/RHSM1/RegisterServer",
06f198
+					       "com.redhat.RHSM1.RegisterServer",
06f198
+					       NULL, error);
06f198
+	if (proxy == NULL)
06f198
+		return FALSE;
06f198
+	val = g_dbus_proxy_call_sync (proxy, "Start",
06f198
+				      g_variant_new ("(s)", priv->userlang),
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (val == NULL)
06f198
+		return FALSE;
06f198
+	g_variant_get (val, "(&s)", &address);
06f198
+	g_debug ("RegisterServer.Start: %s", address);
06f198
+	priv->address = g_strdup (address);
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_register_stop (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	g_autoptr(GDBusProxy) proxy = NULL;
06f198
+	g_autoptr(GVariant) val = NULL;
06f198
+
06f198
+	/* already started */
06f198
+	if (priv->address == NULL)
06f198
+		return TRUE;
06f198
+
06f198
+	/* stop registration server */
06f198
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+					       G_DBUS_PROXY_FLAGS_NONE,
06f198
+					       NULL,
06f198
+					       "com.redhat.RHSM1",
06f198
+					       "/com/redhat/RHSM1/RegisterServer",
06f198
+					       "com.redhat.RHSM1.RegisterServer",
06f198
+					       NULL, error);
06f198
+	if (proxy == NULL)
06f198
+		return FALSE;
06f198
+	val = g_dbus_proxy_call_sync (proxy, "Stop",
06f198
+				      g_variant_new ("(s)", priv->userlang),
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (val == NULL)
06f198
+		return FALSE;
06f198
+	g_clear_pointer (&priv->address, g_free);
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_subprocess_wait_check (GSubprocess *subprocess, GError **error)
06f198
+{
06f198
+	gint rc;
06f198
+	if (!g_subprocess_wait (subprocess, NULL, error)) {
06f198
+		g_prefix_error (error, "failed to run pkexec: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	rc = g_subprocess_get_exit_status (subprocess);
06f198
+	if (rc != 0) {
06f198
+		GInputStream *istream = g_subprocess_get_stderr_pipe (subprocess);
06f198
+		gchar buf[1024] = { 0x0 };
06f198
+		gsize sz = 0;
06f198
+		g_input_stream_read_all (istream, buf, sizeof(buf) - 1, &sz, NULL, NULL);
06f198
+		if (sz == 0) {
06f198
+			g_set_error_literal (error, G_IO_ERROR, rc,
06f198
+					     "Failed to run helper without stderr");
06f198
+			return FALSE;
06f198
+		}
06f198
+		g_set_error_literal (error, G_IO_ERROR, rc, buf);
06f198
+		return FALSE;
06f198
+	}
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+typedef enum {
06f198
+	_NOTIFY_EXPIRED,
06f198
+	_NOTIFY_REGISTRATION_REQUIRED,
06f198
+	_NOTIFY_REGISTERED
06f198
+} _NotifyKind;
06f198
+
06f198
+static void
06f198
+_show_notification (GsdSubscriptionManager *manager, _NotifyKind notify_kind)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	switch (notify_kind) {
06f198
+	case _NOTIFY_EXPIRED:
06f198
+		notify_notification_close (priv->notification_registered, NULL);
06f198
+		notify_notification_close (priv->notification_registration_required, NULL);
06f198
+		notify_notification_show (priv->notification_expired, NULL);
06f198
+		break;
06f198
+	case _NOTIFY_REGISTRATION_REQUIRED:
06f198
+		notify_notification_close (priv->notification_registered, NULL);
06f198
+		notify_notification_close (priv->notification_expired, NULL);
06f198
+		notify_notification_show (priv->notification_registration_required, NULL);
06f198
+		break;
06f198
+	case _NOTIFY_REGISTERED:
06f198
+		notify_notification_close (priv->notification_expired, NULL);
06f198
+		notify_notification_close (priv->notification_registration_required, NULL);
06f198
+		notify_notification_show (priv->notification_registered, NULL);
06f198
+		break;
06f198
+	default:
06f198
+		break;
06f198
+	}
06f198
+	g_timer_reset (priv->timer_last_notified);
06f198
+}
06f198
+
06f198
+static void
06f198
+_client_maybe__show_notification (GsdSubscriptionManager *manager)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+
06f198
+	/* startup */
06f198
+	if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
06f198
+	    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN) {
06f198
+		_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
06f198
+		return;
06f198
+	}
06f198
+
06f198
+	/* something changed */
06f198
+	if (priv->subscription_status_last != priv->subscription_status) {
06f198
+		g_debug ("transisition from subscription status '%s' to '%s'",
06f198
+			 gsd_subman_subscription_status_to_string (priv->subscription_status_last),
06f198
+			 gsd_subman_subscription_status_to_string (priv->subscription_status));
06f198
+
06f198
+		/* needs registration */
06f198
+		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
06f198
+		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID) {
06f198
+			_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
06f198
+			return;
06f198
+		}
06f198
+
06f198
+		/* was unregistered */
06f198
+		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
06f198
+		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN) {
06f198
+			_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
06f198
+			return;
06f198
+		}
06f198
+
06f198
+		/* registered */
06f198
+		if (priv->subscription_status_last == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
06f198
+		    priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_VALID &&
06f198
+		    g_timer_elapsed (priv->timer_last_notified, NULL) > 60) {
06f198
+			_show_notification (manager, _NOTIFY_REGISTERED);
06f198
+			return;
06f198
+		}
06f198
+	}
06f198
+
06f198
+	/* nag again */
06f198
+	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_UNKNOWN &&
06f198
+	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
06f198
+		_show_notification (manager, _NOTIFY_REGISTRATION_REQUIRED);
06f198
+		return;
06f198
+	}
06f198
+	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_INVALID &&
06f198
+	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
06f198
+		_show_notification (manager, _NOTIFY_EXPIRED);
06f198
+		return;
06f198
+	}
06f198
+	if (priv->subscription_status == GSD_SUBMAN_SUBSCRIPTION_STATUS_PARTIALLY_VALID &&
06f198
+	    g_timer_elapsed (priv->timer_last_notified, NULL) > 60 * 60 * 24) {
06f198
+		_show_notification (manager, _NOTIFY_EXPIRED);
06f198
+		return;
06f198
+	}
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_register_with_keys (GsdSubscriptionManager *manager,
06f198
+				  const gchar *hostname,
06f198
+				  const gchar *organisation,
06f198
+				  const gchar *activation_key,
06f198
+				  GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	g_autoptr(GSubprocess) subprocess = NULL;
06f198
+
06f198
+	/* apparently: "we can't send registration credentials over the regular
06f198
+	 * system or session bus since those aren't really locked down..." */
06f198
+	if (!_client_register_start (manager, error))
06f198
+		return FALSE;
06f198
+	g_debug ("spawning %s", LIBEXECDIR "/gsd-subman-helper");
06f198
+	subprocess = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_PIPE, error,
06f198
+				       "pkexec", LIBEXECDIR "/gsd-subman-helper",
06f198
+				       "--kind", "register-with-key",
06f198
+				       "--address", priv->address,
06f198
+				       "--hostname", hostname,
06f198
+				       "--organisation", organisation,
06f198
+				       "--activation-key", activation_key,
06f198
+				       NULL);
06f198
+	if (subprocess == NULL) {
06f198
+		g_prefix_error (error, "failed to find pkexec: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	if (!_client_subprocess_wait_check (subprocess, error))
06f198
+		return FALSE;
06f198
+
06f198
+	/* FIXME: also do on error? */
06f198
+	if (!_client_register_stop (manager, error))
06f198
+		return FALSE;
06f198
+	if (!_client_subscription_status_update (manager, error))
06f198
+		return FALSE;
06f198
+	_client_maybe__show_notification (manager);
06f198
+
06f198
+	/* success */
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_register (GsdSubscriptionManager *manager,
06f198
+			 const gchar *hostname,
06f198
+			 const gchar *organisation,
06f198
+			 const gchar *username,
06f198
+			 const gchar *password,
06f198
+			 GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	g_autoptr(GSubprocess) subprocess = NULL;
06f198
+
06f198
+	/* fallback */
06f198
+	if (organisation == NULL)
06f198
+		organisation = "";
06f198
+
06f198
+	/* apparently: "we can't send registration credentials over the regular
06f198
+	 * system or session bus since those aren't really locked down..." */
06f198
+	if (!_client_register_start (manager, error))
06f198
+		return FALSE;
06f198
+	g_debug ("spawning %s", LIBEXECDIR "/gsd-subman-helper");
06f198
+	subprocess = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_PIPE, error,
06f198
+				       "pkexec", LIBEXECDIR "/gsd-subman-helper",
06f198
+				       "--kind", "register-with-username",
06f198
+				       "--address", priv->address,
06f198
+				       "--hostname", hostname,
06f198
+				       "--organisation", organisation,
06f198
+				       "--username", username,
06f198
+				       "--password", password,
06f198
+				       NULL);
06f198
+	if (subprocess == NULL) {
06f198
+		g_prefix_error (error, "failed to find pkexec: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	if (!_client_subprocess_wait_check (subprocess, error))
06f198
+		return FALSE;
06f198
+
06f198
+	/* FIXME: also do on error? */
06f198
+	if (!_client_register_stop (manager, error))
06f198
+		return FALSE;
06f198
+	if (!_client_subscription_status_update (manager, error))
06f198
+		return FALSE;
06f198
+	_client_maybe__show_notification (manager);
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_unregister (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	g_autoptr(GSubprocess) subprocess = NULL;
06f198
+
06f198
+	/* apparently: "we can't send registration credentials over the regular
06f198
+	 * system or session bus since those aren't really locked down..." */
06f198
+	if (!_client_register_start (manager, error))
06f198
+		return FALSE;
06f198
+	g_debug ("spawning %s", LIBEXECDIR "/gsd-subman-helper");
06f198
+	subprocess = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_PIPE, error,
06f198
+				       "pkexec", LIBEXECDIR "/gsd-subman-helper",
06f198
+				       "--kind", "unregister",
06f198
+				       NULL);
06f198
+	if (subprocess == NULL) {
06f198
+		g_prefix_error (error, "failed to find pkexec: ");
06f198
+		return FALSE;
06f198
+	}
06f198
+	if (!_client_subprocess_wait_check (subprocess, error))
06f198
+		return FALSE;
06f198
+	if (!_client_subscription_status_update (manager, error))
06f198
+		return FALSE;
06f198
+	_client_maybe__show_notification (manager);
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_update_config (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	g_autoptr(GVariant) val = NULL;
06f198
+	g_autoptr(GVariant) val_server = NULL;
06f198
+	g_autoptr(GVariantDict) dict = NULL;
06f198
+	GVariantIter iter;
06f198
+	gchar *key;
06f198
+	gchar *value;
06f198
+
06f198
+	val = g_dbus_proxy_call_sync (priv->proxies[_RHSM_INTERFACE_CONFIG],
06f198
+				      "GetAll",
06f198
+				      g_variant_new ("(s)", priv->userlang),
06f198
+				      G_DBUS_CALL_FLAGS_NONE,
06f198
+				      -1, NULL, error);
06f198
+	if (val == NULL)
06f198
+		return FALSE;
06f198
+	dict = g_variant_dict_new (g_variant_get_child_value (val, 0));
06f198
+	val_server = g_variant_dict_lookup_value (dict, "server", G_VARIANT_TYPE("a{ss}"));
06f198
+	if (val_server != NULL) {
06f198
+		g_variant_iter_init (&iter, val_server);
06f198
+		while (g_variant_iter_next (&iter, "{ss}", &key, &value)) {
06f198
+			g_debug ("%s=%s", key, value);
06f198
+			g_hash_table_insert (priv->config,
06f198
+					     g_steal_pointer (&key),
06f198
+					     g_steal_pointer (&value));
06f198
+		}
06f198
+	}
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+static void
06f198
+_subman_proxy_signal_cb (GDBusProxy *proxy,
06f198
+			 const gchar *sender_name,
06f198
+			 const gchar *signal_name,
06f198
+			 GVariant *parameters,
06f198
+			 GsdSubscriptionManager *manager)
06f198
+{
06f198
+	g_autoptr(GError) error = NULL;
06f198
+	if (!_client_syspurpose_update (manager, &error)) {
06f198
+		g_warning ("failed to update syspurpose: %s", error->message);
06f198
+		g_clear_error (&error);
06f198
+	}
06f198
+	if (!_client_subscription_status_update (manager, &error)) {
06f198
+		g_warning ("failed to update subscription status: %s", error->message);
06f198
+		g_clear_error (&error);
06f198
+	}
06f198
+	_client_maybe__show_notification (manager);
06f198
+}
06f198
+
06f198
+static void
06f198
+_client_unload (GsdSubscriptionManager *manager)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	for (guint i = 0; i < _RHSM_INTERFACE_LAST; i++)
06f198
+		g_clear_object (&priv->proxies[i]);
06f198
+	g_hash_table_unref (priv->config);
06f198
+}
06f198
+
06f198
+static const gchar *
06f198
+_rhsm_interface_to_string (_RhsmInterface kind)
06f198
+{
06f198
+	if (kind == _RHSM_INTERFACE_CONFIG)
06f198
+		return "Config";
06f198
+	if (kind == _RHSM_INTERFACE_REGISTER_SERVER)
06f198
+		return "RegisterServer";
06f198
+	if (kind == _RHSM_INTERFACE_ATTACH)
06f198
+		return "Attach";
06f198
+	if (kind == _RHSM_INTERFACE_ENTITLEMENT)
06f198
+		return "Entitlement";
06f198
+	if (kind == _RHSM_INTERFACE_PRODUCTS)
06f198
+		return "Products";
06f198
+	if (kind == _RHSM_INTERFACE_CONSUMER)
06f198
+		return "Consumer";
06f198
+	if (kind == _RHSM_INTERFACE_SYSPURPOSE)
06f198
+		return "Syspurpose";
06f198
+	return NULL;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+_client_load (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+
06f198
+	priv->config = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
06f198
+
06f198
+	/* connect to all the interfaces on the *different* objects :| */
06f198
+	for (guint i = 0; i < _RHSM_INTERFACE_LAST; i++) {
06f198
+		const gchar *kind = _rhsm_interface_to_string (i);
06f198
+		g_autofree gchar *opath = g_strdup_printf ("/com/redhat/RHSM1/%s", kind);
06f198
+		g_autofree gchar *iface = g_strdup_printf ("com.redhat.RHSM1.%s", kind);
06f198
+		priv->proxies[i] =
06f198
+			g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
+						       G_DBUS_PROXY_FLAGS_NONE,
06f198
+						       NULL,
06f198
+						       "com.redhat.RHSM1",
06f198
+						       opath, iface,
06f198
+						       NULL,
06f198
+						       error);
06f198
+		if (priv->proxies[i] == NULL)
06f198
+			return FALSE;
06f198
+		/* we want to get notified if the status of the system changes */
06f198
+		g_signal_connect (priv->proxies[i], "g-signal",
06f198
+				  G_CALLBACK (_subman_proxy_signal_cb), manager);
06f198
+	}
06f198
+
06f198
+	/* get initial status */
06f198
+	priv->userlang = "";
06f198
+	if (!_client_update_config (manager, error))
06f198
+		return FALSE;
06f198
+	if (!_client_subscription_status_update (manager, error))
06f198
+		return FALSE;
06f198
+	if (!_client_syspurpose_update (manager, error))
06f198
+		return FALSE;
06f198
+
06f198
+	/* success */
06f198
+	return TRUE;
06f198
+}
06f198
+
06f198
+gboolean
06f198
+gsd_subscription_manager_start (GsdSubscriptionManager *manager, GError **error)
06f198
+{
06f198
+	gboolean ret;
06f198
+	g_debug ("Starting subscription manager");
06f198
+	gnome_settings_profile_start (NULL);
06f198
+	ret = _client_load (manager, error);
06f198
+	_client_maybe__show_notification (manager);
06f198
+	gnome_settings_profile_end (NULL);
06f198
+	return ret;
06f198
+}
06f198
+
06f198
+void
06f198
+gsd_subscription_manager_stop (GsdSubscriptionManager *manager)
06f198
+{
06f198
+	g_debug ("Stopping subscription manager");
06f198
+	_client_unload (manager);
06f198
+}
06f198
+
06f198
+static void
06f198
+gsd_subscription_manager_class_init (GsdSubscriptionManagerClass *klass)
06f198
+{
06f198
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
06f198
+	object_class->finalize = gsd_subscription_manager_finalize;
06f198
+        notify_init ("gnome-settings-daemon");
06f198
+	g_type_class_add_private (klass, sizeof (GsdSubscriptionManagerPrivate));
06f198
+}
06f198
+
06f198
+static void
06f198
+_launch_info_overview (void)
06f198
+{
06f198
+	const gchar *argv[] = { "gnome-control-center", "info-overview", NULL };
06f198
+	g_debug ("Running gnome-control-center info-overview");
06f198
+	g_spawn_async (NULL, (gchar **) argv, NULL, G_SPAWN_SEARCH_PATH,
06f198
+		       NULL, NULL, NULL, NULL);
06f198
+}
06f198
+
06f198
+static void
06f198
+_notify_closed_cb (NotifyNotification *notification, gpointer user_data)
06f198
+{
06f198
+	/* FIXME: only launch when clicking on the main body, not the window close */
06f198
+	if (notify_notification_get_closed_reason (notification) == 0x400)
06f198
+		_launch_info_overview ();
06f198
+}
06f198
+
06f198
+static void
06f198
+_notify_clicked_cb (NotifyNotification *notification, char *action, gpointer user_data)
06f198
+{
06f198
+	_launch_info_overview ();
06f198
+}
06f198
+
06f198
+static void
06f198
+gsd_subscription_manager_init (GsdSubscriptionManager *manager)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv = GSD_SUBSCRIPTION_MANAGER_GET_PRIVATE (manager);
06f198
+
06f198
+	priv->timer_last_notified = g_timer_new ();
06f198
+
06f198
+	/* expired */
06f198
+	priv->notification_expired =
06f198
+		notify_notification_new (_("Subscription Has Expired"),
06f198
+					 _("Add or renew a subscription to continue receiving software updates."),
06f198
+					 NULL);
06f198
+	notify_notification_set_app_name (priv->notification_expired, _("Subscription"));
06f198
+	notify_notification_set_hint_string (priv->notification_expired, "desktop-entry", "subman-panel");
06f198
+	notify_notification_set_hint_string (priv->notification_expired, "x-gnome-privacy-scope", "system");
06f198
+	notify_notification_set_urgency (priv->notification_expired, NOTIFY_URGENCY_CRITICAL);
06f198
+	notify_notification_add_action (priv->notification_expired,
06f198
+					"info-overview", _("Subscribe System…"),
06f198
+					_notify_clicked_cb,
06f198
+					manager, NULL);
06f198
+	g_signal_connect (priv->notification_expired, "closed",
06f198
+			  G_CALLBACK (_notify_closed_cb), manager);
06f198
+
06f198
+	/* registered */
06f198
+	priv->notification_registered =
06f198
+		notify_notification_new (_("Registration Successful"),
06f198
+					 _("The system has been registered and software updates have been enabled."),
06f198
+					 NULL);
06f198
+	notify_notification_set_app_name (priv->notification_registered, _("Subscription"));
06f198
+	notify_notification_set_hint_string (priv->notification_registered, "desktop-entry", "subman-panel");
06f198
+	notify_notification_set_hint_string (priv->notification_registered, "x-gnome-privacy-scope", "system");
06f198
+	notify_notification_set_urgency (priv->notification_registered, NOTIFY_URGENCY_CRITICAL);
06f198
+	g_signal_connect (priv->notification_registered, "closed",
06f198
+			  G_CALLBACK (_notify_closed_cb), manager);
06f198
+
06f198
+	/* registration required */
06f198
+	priv->notification_registration_required =
06f198
+		notify_notification_new (_("System Not Registered"),
06f198
+					 _("Please register your system to receive software updates."),
06f198
+					 NULL);
06f198
+	notify_notification_set_app_name (priv->notification_registration_required, _("Subscription"));
06f198
+	notify_notification_set_hint_string (priv->notification_registration_required, "desktop-entry", "subman-panel");
06f198
+	notify_notification_set_hint_string (priv->notification_registration_required, "x-gnome-privacy-scope", "system");
06f198
+	notify_notification_set_urgency (priv->notification_registration_required, NOTIFY_URGENCY_CRITICAL);
06f198
+	notify_notification_add_action (priv->notification_registration_required,
06f198
+					"info-overview", _("Register System…"),
06f198
+					_notify_clicked_cb,
06f198
+					manager, NULL);
06f198
+	g_signal_connect (priv->notification_registration_required, "closed",
06f198
+			  G_CALLBACK (_notify_closed_cb), manager);
06f198
+}
06f198
+
06f198
+static void
06f198
+gsd_subscription_manager_finalize (GObject *object)
06f198
+{
06f198
+	GsdSubscriptionManager *manager;
06f198
+
06f198
+	g_return_if_fail (object != NULL);
06f198
+	g_return_if_fail (GSD_IS_SUBSCRIPTION_MANAGER (object));
06f198
+
06f198
+	manager = GSD_SUBSCRIPTION_MANAGER (object);
06f198
+
06f198
+	gsd_subscription_manager_stop (manager);
06f198
+
06f198
+	if (manager->priv->bus_cancellable != NULL) {
06f198
+		g_cancellable_cancel (manager->priv->bus_cancellable);
06f198
+		g_clear_object (&manager->priv->bus_cancellable);
06f198
+	}
06f198
+
06f198
+	g_clear_pointer (&manager->priv->introspection_data, g_dbus_node_info_unref);
06f198
+	g_clear_object (&manager->priv->connection);
06f198
+	g_clear_object (&manager->priv->notification_expired);
06f198
+	g_clear_object (&manager->priv->notification_registered);
06f198
+	g_timer_destroy (manager->priv->timer_last_notified);
06f198
+
06f198
+	if (manager->priv->name_id != 0) {
06f198
+		g_bus_unown_name (manager->priv->name_id);
06f198
+		manager->priv->name_id = 0;
06f198
+	}
06f198
+
06f198
+	G_OBJECT_CLASS (gsd_subscription_manager_parent_class)->finalize (object);
06f198
+}
06f198
+
06f198
+static void
06f198
+handle_method_call (GDBusConnection       *connection,
06f198
+		    const gchar           *sender,
06f198
+		    const gchar           *object_path,
06f198
+		    const gchar           *interface_name,
06f198
+		    const gchar           *method_name,
06f198
+		    GVariant              *parameters,
06f198
+		    GDBusMethodInvocation *invocation,
06f198
+		    gpointer               user_data)
06f198
+{
06f198
+	GsdSubscriptionManager *manager = GSD_SUBSCRIPTION_MANAGER (user_data);
06f198
+	g_autoptr(GError) error = NULL;
06f198
+
06f198
+	if (g_strcmp0 (method_name, "Register") == 0) {
06f198
+		const gchar *organisation = NULL;
06f198
+		const gchar *hostname = NULL;
06f198
+
06f198
+		if (FALSE) {
06f198
+			g_dbus_method_invocation_return_error_literal (invocation,
06f198
+								       G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
06f198
+								       "Cannot register at this time");
06f198
+
06f198
+			return;
06f198
+		}
06f198
+
06f198
+		g_autoptr(GVariantDict) dict = g_variant_dict_new (g_variant_get_child_value (parameters, 0));
06f198
+
06f198
+		const gchar *kind = NULL;
06f198
+		if (!g_variant_dict_lookup (dict, "kind", "&s", &kind)) {
06f198
+			g_dbus_method_invocation_return_error_literal (invocation,
06f198
+								       G_IO_ERROR, G_IO_ERROR_FAILED,
06f198
+								       "No kind specified");
06f198
+
06f198
+			return;
06f198
+		}
06f198
+		if (g_strcmp0 (kind, "username") == 0) {
06f198
+			const gchar *username = NULL;
06f198
+			const gchar *password = NULL;
06f198
+			g_variant_dict_lookup (dict, "hostname", "&s", &hostname);
06f198
+			g_variant_dict_lookup (dict, "organisation", "&s", &organisation);
06f198
+			g_variant_dict_lookup (dict, "username", "&s", &username);
06f198
+			g_variant_dict_lookup (dict, "password", "&s", &password);
06f198
+			if (!_client_register (manager,
06f198
+						     hostname,
06f198
+						     organisation,
06f198
+						     username,
06f198
+						     password,
06f198
+						     &error)) {
06f198
+				g_dbus_method_invocation_return_gerror (invocation, error);
06f198
+				return;
06f198
+			}
06f198
+		} else if (g_strcmp0 (kind, "key") == 0) {
06f198
+			const gchar *activation_key = NULL;
06f198
+			g_variant_dict_lookup (dict, "hostname", "&s", &hostname);
06f198
+			g_variant_dict_lookup (dict, "organisation", "&s", &organisation);
06f198
+			g_variant_dict_lookup (dict, "activation-key", "&s", &activation_key);
06f198
+			if (!_client_register_with_keys (manager,
06f198
+							       hostname,
06f198
+							       organisation,
06f198
+							       activation_key,
06f198
+							       &error)) {
06f198
+				g_dbus_method_invocation_return_gerror (invocation, error);
06f198
+				return;
06f198
+			}
06f198
+		} else {
06f198
+			g_dbus_method_invocation_return_error_literal (invocation,
06f198
+								       G_IO_ERROR, G_IO_ERROR_FAILED,
06f198
+								       "Invalid kind specified");
06f198
+
06f198
+			return;
06f198
+		}
06f198
+		g_dbus_method_invocation_return_value (invocation, NULL);
06f198
+	} else if (g_strcmp0 (method_name, "Unregister") == 0) {
06f198
+		if (!_client_unregister (manager, &error)) {
06f198
+			g_dbus_method_invocation_return_gerror (invocation, error);
06f198
+			return;
06f198
+		}
06f198
+		g_dbus_method_invocation_return_value (invocation, NULL);
06f198
+	} else {
06f198
+		g_assert_not_reached ();
06f198
+	}
06f198
+}
06f198
+
06f198
+static GVariant *
06f198
+handle_get_property (GDBusConnection *connection,
06f198
+		     const gchar *sender,
06f198
+		     const gchar *object_path,
06f198
+		     const gchar *interface_name,
06f198
+		     const gchar *property_name,
06f198
+		     GError **error, gpointer user_data)
06f198
+{
06f198
+	GsdSubscriptionManager *manager = GSD_SUBSCRIPTION_MANAGER (user_data);
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+
06f198
+	if (g_strcmp0 (interface_name, GSD_SUBSCRIPTION_DBUS_INTERFACE) != 0) {
06f198
+		g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
06f198
+			     "No such interface: %s", interface_name);
06f198
+		return NULL;
06f198
+	}
06f198
+
06f198
+	if (g_strcmp0 (property_name, "SubscriptionStatus") == 0)
06f198
+		return g_variant_new_uint32 (priv->subscription_status);
06f198
+
06f198
+	g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
06f198
+		     "Failed to get property: %s", property_name);
06f198
+	return NULL;
06f198
+}
06f198
+
06f198
+static gboolean
06f198
+handle_set_property (GDBusConnection *connection,
06f198
+		     const gchar *sender,
06f198
+		     const gchar *object_path,
06f198
+		     const gchar *interface_name,
06f198
+		     const gchar *property_name,
06f198
+		     GVariant *value,
06f198
+		     GError **error, gpointer user_data)
06f198
+{
06f198
+	if (g_strcmp0 (interface_name, GSD_SUBSCRIPTION_DBUS_INTERFACE) != 0) {
06f198
+		g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
06f198
+			     "No such interface: %s", interface_name);
06f198
+		return FALSE;
06f198
+	}
06f198
+	g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
06f198
+		     "No such property: %s", property_name);
06f198
+	return FALSE;
06f198
+}
06f198
+
06f198
+static const GDBusInterfaceVTable interface_vtable =
06f198
+{
06f198
+	handle_method_call,
06f198
+	handle_get_property,
06f198
+	handle_set_property
06f198
+};
06f198
+
06f198
+static void
06f198
+name_lost_handler_cb (GDBusConnection *connection, const gchar *name, gpointer user_data)
06f198
+{
06f198
+	g_debug ("lost name, so exiting");
06f198
+	gtk_main_quit ();
06f198
+}
06f198
+
06f198
+static void
06f198
+on_bus_gotten (GObject *source_object, GAsyncResult *res, GsdSubscriptionManager *manager)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+	GDBusConnection *connection;
06f198
+	g_autoptr(GError) error = NULL;
06f198
+
06f198
+	connection = g_bus_get_finish (res, &error);
06f198
+	if (connection == NULL) {
06f198
+		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
06f198
+			g_warning ("Could not get session bus: %s", error->message);
06f198
+		return;
06f198
+	}
06f198
+
06f198
+	priv->connection = connection;
06f198
+	g_dbus_connection_register_object (connection,
06f198
+					   GSD_SUBSCRIPTION_DBUS_PATH,
06f198
+					   priv->introspection_data->interfaces[0],
06f198
+					   &interface_vtable,
06f198
+					   manager,
06f198
+					   NULL,
06f198
+					   NULL);
06f198
+	priv->name_id = g_bus_own_name_on_connection (connection,
06f198
+						      GSD_SUBSCRIPTION_DBUS_NAME,
06f198
+						      G_BUS_NAME_OWNER_FLAGS_NONE,
06f198
+						      NULL,
06f198
+						      name_lost_handler_cb,
06f198
+						      manager,
06f198
+						      NULL);
06f198
+}
06f198
+
06f198
+static void
06f198
+register_manager_dbus (GsdSubscriptionManager *manager)
06f198
+{
06f198
+	GsdSubscriptionManagerPrivate *priv = manager->priv;
06f198
+
06f198
+	priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
06f198
+	g_assert (priv->introspection_data != NULL);
06f198
+	priv->bus_cancellable = g_cancellable_new ();
06f198
+
06f198
+	g_bus_get (G_BUS_TYPE_SESSION, priv->bus_cancellable,
06f198
+		   (GAsyncReadyCallback) on_bus_gotten, manager);
06f198
+}
06f198
+
06f198
+GsdSubscriptionManager *
06f198
+gsd_subscription_manager_new (void)
06f198
+{
06f198
+	if (manager_object != NULL) {
06f198
+		g_object_ref (manager_object);
06f198
+	} else {
06f198
+		manager_object = g_object_new (GSD_TYPE_SUBSCRIPTION_MANAGER, NULL);
06f198
+		g_object_add_weak_pointer (manager_object,
06f198
+					   (gpointer *) &manager_object);
06f198
+		register_manager_dbus (manager_object);
06f198
+	}
06f198
+
06f198
+	return GSD_SUBSCRIPTION_MANAGER (manager_object);
06f198
+}
06f198
diff --git a/plugins/subman/gsd-subscription-manager.h b/plugins/subman/gsd-subscription-manager.h
06f198
new file mode 100644
06f198
index 00000000..6a524b1b
06f198
--- /dev/null
06f198
+++ b/plugins/subman/gsd-subscription-manager.h
06f198
@@ -0,0 +1,63 @@
06f198
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
06f198
+ *
06f198
+ * Copyright (C) 2019 Richard Hughes <richard@hughsie.com>
06f198
+ *
06f198
+ * This program is free software; you can redistribute it and/or modify
06f198
+ * it under the terms of the GNU General Public License as published by
06f198
+ * the Free Software Foundation; either version 2 of the License, or
06f198
+ * (at your option) any later version.
06f198
+ *
06f198
+ * This program is distributed in the hope that it will be useful,
06f198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
+ * GNU General Public License for more details.
06f198
+ *
06f198
+ * You should have received a copy of the GNU General Public License
06f198
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
06f198
+ *
06f198
+ */
06f198
+
06f198
+#ifndef __GSD_SUBSCRIPTION_MANAGER_H
06f198
+#define __GSD_SUBSCRIPTION_MANAGER_H
06f198
+
06f198
+#include <glib-object.h>
06f198
+
06f198
+G_BEGIN_DECLS
06f198
+
06f198
+#define GSD_TYPE_SUBSCRIPTION_MANAGER		(gsd_subscription_manager_get_type ())
06f198
+#define GSD_SUBSCRIPTION_MANAGER(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_SUBSCRIPTION_MANAGER, GsdSubscriptionManager))
06f198
+#define GSD_SUBSCRIPTION_MANAGER_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_SUBSCRIPTION_MANAGER, GsdSubscriptionManagerClass))
06f198
+#define GSD_IS_SUBSCRIPTION_MANAGER(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_SUBSCRIPTION_MANAGER))
06f198
+#define GSD_IS_SUBSCRIPTION_MANAGER_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_SUBSCRIPTION_MANAGER))
06f198
+#define GSD_SUBSCRIPTION_MANAGER_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_SUBSCRIPTION_MANAGER, GsdSubscriptionManagerClass))
06f198
+#define GSD_SUBSCRIPTION_MANAGER_ERROR		(gsd_subscription_manager_error_quark ())
06f198
+
06f198
+typedef struct GsdSubscriptionManagerPrivate GsdSubscriptionManagerPrivate;
06f198
+
06f198
+typedef struct
06f198
+{
06f198
+	GObject				 parent;
06f198
+	GsdSubscriptionManagerPrivate	*priv;
06f198
+} GsdSubscriptionManager;
06f198
+
06f198
+typedef struct
06f198
+{
06f198
+	GObjectClass			parent_class;
06f198
+} GsdSubscriptionManagerClass;
06f198
+
06f198
+enum
06f198
+{
06f198
+	GSD_SUBSCRIPTION_MANAGER_ERROR_FAILED
06f198
+};
06f198
+
06f198
+GType			gsd_subscription_manager_get_type       (void);
06f198
+GQuark			gsd_subscription_manager_error_quark    (void);
06f198
+
06f198
+GsdSubscriptionManager *gsd_subscription_manager_new		(void);
06f198
+gboolean		gsd_subscription_manager_start		(GsdSubscriptionManager *manager,
06f198
+								 GError                **error);
06f198
+void			gsd_subscription_manager_stop		(GsdSubscriptionManager *manager);
06f198
+
06f198
+G_END_DECLS
06f198
+
06f198
+#endif /* __GSD_SUBSCRIPTION_MANAGER_H */
06f198
diff --git a/plugins/subman/main.c b/plugins/subman/main.c
06f198
new file mode 100644
06f198
index 00000000..28ac995b
06f198
--- /dev/null
06f198
+++ b/plugins/subman/main.c
06f198
@@ -0,0 +1,8 @@
06f198
+#define NEW gsd_subscription_manager_new
06f198
+#define START gsd_subscription_manager_start
06f198
+#define STOP gsd_subscription_manager_stop
06f198
+#define MANAGER GsdSubscriptionManager
06f198
+#define GDK_BACKEND "x11"
06f198
+#include "gsd-subscription-manager.h"
06f198
+
06f198
+#include "daemon-skeleton-gtk.h"
06f198
diff --git a/plugins/subman/meson.build b/plugins/subman/meson.build
06f198
new file mode 100644
06f198
index 00000000..bfd073b6
06f198
--- /dev/null
06f198
+++ b/plugins/subman/meson.build
06f198
@@ -0,0 +1,56 @@
06f198
+sources = files(
06f198
+  'gsd-subscription-manager.c',
06f198
+  'gsd-subman-common.c',
06f198
+  'main.c'
06f198
+)
06f198
+
06f198
+deps = plugins_deps + [
06f198
+  libnotify_dep,
06f198
+  gtk_dep,
06f198
+  jsonglib_dep,
06f198
+  m_dep,
06f198
+]
06f198
+
06f198
+cflags += ['-DBINDIR="@0@"'.format(gsd_bindir)]
06f198
+cflags += ['-DLIBEXECDIR="@0@"'.format(gsd_libexecdir)]
06f198
+
06f198
+executable(
06f198
+  'gsd-' + plugin_name,
06f198
+  sources,
06f198
+  include_directories: [top_inc, common_inc],
06f198
+  dependencies: deps,
06f198
+  c_args: cflags,
06f198
+  install: true,
06f198
+  install_rpath: gsd_pkglibdir,
06f198
+  install_dir: gsd_libexecdir
06f198
+)
06f198
+
06f198
+# .Register needs to be called from root as subman can't do PolicyKit...
06f198
+policy = 'org.gnome.settings-daemon.plugins.subman.policy'
06f198
+policy_in = configure_file(
06f198
+  input: policy + '.in.in',
06f198
+  output: policy + '.in',
06f198
+  configuration: plugins_conf
06f198
+)
06f198
+
06f198
+i18n.merge_file(
06f198
+  policy,
06f198
+  input: policy_in,
06f198
+  output: policy,
06f198
+  po_dir: po_dir,
06f198
+  install: true,
06f198
+  install_dir: join_paths(gsd_datadir, 'polkit-1', 'actions')
06f198
+)
06f198
+
06f198
+install_data('org.gnome.settings-daemon.plugins.subman.rules',
06f198
+             install_dir : join_paths(gsd_datadir, 'polkit-1', 'rules.d'))
06f198
+
06f198
+executable(
06f198
+  'gsd-subman-helper',
06f198
+  'gsd-subman-helper.c',
06f198
+  include_directories: top_inc,
06f198
+  dependencies: [gio_dep, jsonglib_dep],
06f198
+  install: true,
06f198
+  install_rpath: gsd_pkglibdir,
06f198
+  install_dir: gsd_libexecdir
06f198
+)
06f198
diff --git a/plugins/subman/org.gnome.SettingsDaemon.Subscription.desktop.in b/plugins/subman/org.gnome.SettingsDaemon.Subscription.desktop.in
06f198
new file mode 100644
06f198
index 00000000..14fe5915
06f198
--- /dev/null
06f198
+++ b/plugins/subman/org.gnome.SettingsDaemon.Subscription.desktop.in
06f198
@@ -0,0 +1,9 @@
06f198
+[Desktop Entry]
06f198
+Type=Application
06f198
+Name=GNOME Settings Daemon's subscription manager plugin
06f198
+Exec=@libexecdir@/gsd-subman
06f198
+OnlyShowIn=GNOME;
06f198
+NoDisplay=true
06f198
+X-GNOME-Autostart-Phase=Initialization
06f198
+X-GNOME-Autostart-Notify=true
06f198
+X-GNOME-AutoRestart=true
06f198
diff --git a/plugins/subman/org.gnome.settings-daemon.plugins.subman.policy.in.in b/plugins/subman/org.gnome.settings-daemon.plugins.subman.policy.in.in
06f198
new file mode 100644
06f198
index 00000000..59e9fdd4
06f198
--- /dev/null
06f198
+++ b/plugins/subman/org.gnome.settings-daemon.plugins.subman.policy.in.in
06f198
@@ -0,0 +1,27 @@
06f198
+
06f198
+
06f198
+ "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
06f198
+ "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
06f198
+<policyconfig>
06f198
+
06f198
+  
06f198
+    Policy definitions for gnome-settings-daemon system-wide actions.
06f198
+    Copyright (c) 2019 Richard Hughes <richard@hughsie.com>
06f198
+  -->
06f198
+
06f198
+  <vendor>GNOME Settings Daemon</vendor>
06f198
+  <vendor_url>http://git.gnome.org/browse/gnome-settings-daemon</vendor_url>
06f198
+  <icon_name>emblem-synchronizing</icon_name>
06f198
+
06f198
+  <action id="org.gnome.settings-daemon.plugins.subman.register">
06f198
+    <description>Register the system</description>
06f198
+    <message>Authentication is required to register the system</message>
06f198
+    <defaults>
06f198
+      <allow_any>no</allow_any>
06f198
+      <allow_inactive>no</allow_inactive>
06f198
+      <allow_active>auth_admin_keep</allow_active>
06f198
+    </defaults>
06f198
+    <annotate key="org.freedesktop.policykit.exec.path">@libexecdir@/gsd-subman-helper</annotate>
06f198
+  </action>
06f198
+
06f198
+</policyconfig>
06f198
diff --git a/plugins/subman/org.gnome.settings-daemon.plugins.subman.rules b/plugins/subman/org.gnome.settings-daemon.plugins.subman.rules
06f198
new file mode 100644
06f198
index 00000000..1ed3a0ea
06f198
--- /dev/null
06f198
+++ b/plugins/subman/org.gnome.settings-daemon.plugins.subman.rules
06f198
@@ -0,0 +1,7 @@
06f198
+polkit.addRule(function(action, subject) {
06f198
+    if (action.id == "org.gnome.settings-daemon.plugins.subman.register" &&
06f198
+        subject.active == true && subject.local == true &&
06f198
+        subject.isInGroup("wheel")) {
06f198
+            return polkit.Result.YES;
06f198
+    }
06f198
+});
06f198
-- 
06f198
2.30.0
06f198