Blame SOURCES/0007-subman-Force-re-subscribe-if-the-admin-already-subsc.patch

6486b0
From 477dc8accccab568002bd19caa3fbf898bc05aad Mon Sep 17 00:00:00 2001
6486b0
From: Ray Strode <rstrode@redhat.com>
6486b0
Date: Tue, 25 Aug 2020 10:34:03 -0400
6486b0
Subject: [PATCH 07/15] subman: Force re-subscribe if the admin already
6486b0
 subscribed
6486b0
6486b0
It's possible for an admin to to half-enroll the system with RHN,
6486b0
using the CLI tools.
6486b0
6486b0
Meaning, it's possible for them to register the system with the
6486b0
service, but not attach to a purchased license for the machine,
6486b0
the, so called, entitlements.
6486b0
6486b0
The subman module always does both halves of the registration process
6486b0
in lock step.  This means, if an admin tries to register using GNOME
6486b0
while in a half-registered state, subman will fail because the first
6486b0
step, the registration step, is already finished.
6486b0
6486b0
This commit addresses that problem by trying to unregister up front
6486b0
before registering.
6486b0
---
6486b0
 plugins/subman/gsd-subman-helper.c | 11 +++++++++--
6486b0
 1 file changed, 9 insertions(+), 2 deletions(-)
6486b0
6486b0
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
6486b0
index f84e91bf..3931ef2e 100644
6486b0
--- a/plugins/subman/gsd-subman-helper.c
6486b0
+++ b/plugins/subman/gsd-subman-helper.c
6486b0
@@ -51,61 +51,60 @@ _helper_convert_error (const gchar *json_txt, GError **error)
6486b0
 	json_root = json_parser_get_root (json_parser);
6486b0
 	json_obj = json_node_get_object (json_root);
6486b0
 	if (!json_object_has_member (json_obj, "message")) {
6486b0
 		g_set_error (error,
6486b0
 			     G_IO_ERROR,
6486b0
 			     G_IO_ERROR_INVALID_DATA,
6486b0
 			     "no message' in %s", json_txt);
6486b0
 		return;
6486b0
 	}
6486b0
 	message = json_object_get_string_member (json_obj, "message");
6486b0
 	if (g_strstr_len (message, -1, "Invalid user credentials") != NULL) {
6486b0
 		g_set_error_literal (error,
6486b0
 				     G_IO_ERROR,
6486b0
 				     G_IO_ERROR_PERMISSION_DENIED,
6486b0
 				     message);
6486b0
 		return;
6486b0
 	}
6486b0
 	g_set_error_literal (error,
6486b0
 			     G_IO_ERROR,
6486b0
 			     G_IO_ERROR_NOT_SUPPORTED,
6486b0
 			     message);
6486b0
 }
6486b0
 
6486b0
 static gboolean
6486b0
 _helper_unregister (GError **error)
6486b0
 {
6486b0
 	g_autoptr(GDBusProxy) proxy = NULL;
6486b0
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
6486b0
 	g_autoptr(GVariant) res = NULL;
6486b0
 
6486b0
-	g_debug ("unregistering");
6486b0
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
6486b0
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
6486b0
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
6486b0
 					       NULL,
6486b0
 					       "com.redhat.RHSM1",
6486b0
 					       "/com/redhat/RHSM1/Unregister",
6486b0
 					       "com.redhat.RHSM1.Unregister",
6486b0
 					       NULL, error);
6486b0
 	if (proxy == NULL) {
6486b0
 		g_prefix_error (error, "Failed to get proxy: ");
6486b0
 		return FALSE;
6486b0
 	}
6486b0
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
6486b0
 	res = g_dbus_proxy_call_sync (proxy,
6486b0
 				      "Unregister",
6486b0
 				      g_variant_new ("(a{sv}s)",
6486b0
 						     proxy_options,
6486b0
 						     locale),
6486b0
 				      G_DBUS_CALL_FLAGS_NONE,
6486b0
 				      DBUS_TIMEOUT,
6486b0
 				      NULL, error);
6486b0
 	return res != NULL;
6486b0
 }
6486b0
 
6486b0
 static gboolean
6486b0
 _helper_auto_attach (GError **error)
6486b0
 {
6486b0
 	const gchar *str = NULL;
6486b0
 	g_autoptr(GDBusProxy) proxy = NULL;
6486b0
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
6486b0
@@ -208,60 +207,61 @@ main (int argc, char *argv[])
6486b0
 		{ "prefix", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
6486b0
 			&prefix, "Registration server prefix", NULL },
6486b0
 		{ "port", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
6486b0
 			&port, "Registration server port", NULL },
6486b0
 		{ "proxy", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
6486b0
 			&proxy_server, "Proxy settings", NULL },
6486b0
 		{ NULL}
6486b0
 	};
6486b0
 
6486b0
 	/* check calling UID */
6486b0
 	if (getuid () != 0 || geteuid () != 0) {
6486b0
 		g_printerr ("This program can only be used by the root user\n");
6486b0
 		return G_IO_ERROR_NOT_SUPPORTED;
6486b0
 	}
6486b0
 
6486b0
 	setlocale (LC_ALL, "");
6486b0
 	locale = setlocale (LC_MESSAGES, NULL);
6486b0
 
6486b0
 	g_option_context_add_main_entries (context, options, NULL);
6486b0
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
6486b0
 		g_printerr ("Failed to parse arguments: %s\n", error->message);
6486b0
 		return G_IO_ERROR_NOT_SUPPORTED;
6486b0
 	}
6486b0
 
6486b0
 	/* uncommon actions */
6486b0
 	if (kind == NULL) {
6486b0
 		g_printerr ("No --kind specified\n");
6486b0
 		return G_IO_ERROR_INVALID_DATA;
6486b0
 	}
6486b0
 	if (g_strcmp0 (kind, "unregister") == 0) {
6486b0
+	        g_debug ("unregistering");
6486b0
 		if (!_helper_unregister (&error)) {
6486b0
 			g_printerr ("Failed to Unregister: %s\n", error->message);
6486b0
 			return G_IO_ERROR_NOT_INITIALIZED;
6486b0
 		}
6486b0
 		return EXIT_SUCCESS;
6486b0
 	}
6486b0
 	if (g_strcmp0 (kind, "auto-attach") == 0) {
6486b0
 		if (!_helper_auto_attach (&error)) {
6486b0
 			g_printerr ("Failed to AutoAttach: %s\n", error->message);
6486b0
 			return G_IO_ERROR_NOT_INITIALIZED;
6486b0
 		}
6486b0
 		return EXIT_SUCCESS;
6486b0
 	}
6486b0
 
6486b0
 	/* connect to abstract socket for reasons */
6486b0
 	if (address == NULL) {
6486b0
 		g_printerr ("No --address specified\n");
6486b0
 		return G_IO_ERROR_INVALID_DATA;
6486b0
 	}
6486b0
 	conn_private = g_dbus_connection_new_for_address_sync (address,
6486b0
 							       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
6486b0
 							       NULL, NULL,
6486b0
 							       &error);
6486b0
 	if (conn_private == NULL) {
6486b0
 		g_printerr ("Invalid --address specified: %s\n", error->message);
6486b0
 		return G_IO_ERROR_INVALID_DATA;
6486b0
 	}
6486b0
 	proxy = g_dbus_proxy_new_sync (conn_private,
6486b0
 				       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
6486b0
 				       NULL, /* GDBusInterfaceInfo */
6486b0
@@ -277,96 +277,103 @@ main (int argc, char *argv[])
6486b0
 	/* no options */
6486b0
 	subman_options = g_variant_builder_new (G_VARIANT_TYPE("a{ss}"));
6486b0
 
6486b0
 	/* set registration server */
6486b0
 	if (hostname == NULL || hostname[0] == '\0')
6486b0
 		hostname = g_strdup ("subscription.rhsm.redhat.com");
6486b0
 	if (prefix == NULL || prefix[0] == '\0')
6486b0
 		prefix = g_strdup ("/subscription");
6486b0
 	if (port == NULL || port[0] == '\0')
6486b0
 		port = g_strdup ("443");
6486b0
 	subman_conopts = g_variant_builder_new (G_VARIANT_TYPE("a{ss}"));
6486b0
 	g_variant_builder_add (subman_conopts, "{ss}", "host", hostname);
6486b0
 	g_variant_builder_add (subman_conopts, "{ss}", "handler", prefix);
6486b0
 	g_variant_builder_add (subman_conopts, "{ss}", "port", port);
6486b0
 
6486b0
 	/* call into RHSM */
6486b0
 	if (g_strcmp0 (kind, "register-with-key") == 0) {
6486b0
 		g_auto(GStrv) activation_keys = NULL;
6486b0
 		g_autoptr(GError) error_local = NULL;
6486b0
 		g_autoptr(GVariant) res = NULL;
6486b0
 
6486b0
 		if (activation_key == NULL) {
6486b0
 			g_printerr ("Required --activation-key\n");
6486b0
 			return G_IO_ERROR_INVALID_DATA;
6486b0
 		}
6486b0
 		if (organisation == NULL) {
6486b0
 			g_printerr ("Required --organisation\n");
6486b0
 			return G_IO_ERROR_INVALID_DATA;
6486b0
 		}
6486b0
 
6486b0
+		g_debug ("trying to unregister in case machine is already registered");
6486b0
+		_helper_unregister (NULL);
6486b0
+
6486b0
 		g_debug ("registering using activation key");
6486b0
 		activation_keys = g_strsplit (activation_key, ",", -1);
6486b0
 		res = g_dbus_proxy_call_sync (proxy,
6486b0
 					      "RegisterWithActivationKeys",
6486b0
 					      g_variant_new ("(s^asa{ss}a{ss}s)",
6486b0
 							     organisation,
6486b0
 							     activation_keys,
6486b0
 							     subman_options,
6486b0
 							     subman_conopts,
6486b0
 							     locale),
6486b0
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
6486b0
 					      DBUS_TIMEOUT,
6486b0
 					      NULL, &error_local);
6486b0
 		if (res == NULL) {
6486b0
 			g_dbus_error_strip_remote_error (error_local);
6486b0
 			_helper_convert_error (error_local->message, &error);
6486b0
 			g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
6486b0
 			return error->code;
6486b0
 		}
6486b0
 	} else if (g_strcmp0 (kind, "register-with-username") == 0) {
6486b0
 		g_autoptr(GError) error_local = NULL;
6486b0
 		g_autoptr(GVariant) res = NULL;
6486b0
 
6486b0
-		g_debug ("registering using username and password");
6486b0
 		if (username == NULL) {
6486b0
 			g_printerr ("Required --username\n");
6486b0
 			return G_IO_ERROR_INVALID_DATA;
6486b0
 		}
6486b0
 		if (password == NULL) {
6486b0
 			g_printerr ("Required --password\n");
6486b0
 			return G_IO_ERROR_INVALID_DATA;
6486b0
 		}
6486b0
 		if (organisation == NULL) {
6486b0
 			g_printerr ("Required --organisation\n");
6486b0
 			return G_IO_ERROR_INVALID_DATA;
6486b0
 		}
6486b0
+
6486b0
+		g_debug ("trying to unregister in case machine is already registered");
6486b0
+		_helper_unregister (NULL);
6486b0
+
6486b0
+		g_debug ("registering using username and password");
6486b0
 		res = g_dbus_proxy_call_sync (proxy,
6486b0
 					      "Register",
6486b0
 					      g_variant_new ("(sssa{ss}a{ss}s)",
6486b0
 							     organisation,
6486b0
 							     username,
6486b0
 							     password,
6486b0
 							     subman_options,
6486b0
 							     subman_conopts,
6486b0
 							     locale),
6486b0
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
6486b0
 					      DBUS_TIMEOUT,
6486b0
 					      NULL, &error_local);
6486b0
 		if (res == NULL) {
6486b0
 			g_dbus_error_strip_remote_error (error_local);
6486b0
 			_helper_convert_error (error_local->message, &error);
6486b0
 			g_printerr ("Failed to Register: %s\n", error->message);
6486b0
 			return error->code;
6486b0
 		}
6486b0
 	} else {
6486b0
 		g_printerr ("Invalid --kind specified: %s\n", kind);
6486b0
 		return G_IO_ERROR_INVALID_DATA;
6486b0
 	}
6486b0
 
6486b0
 	/* set the new hostname */
6486b0
 	if (!_helper_save_config ("server.hostname", hostname, &error)) {
6486b0
 		g_printerr ("Failed to save hostname: %s\n", error->message);
6486b0
 		return G_IO_ERROR_NOT_INITIALIZED;
6486b0
 	}
6486b0
 	if (!_helper_save_config ("server.prefix", prefix, &error)) {
6486b0
 		g_printerr ("Failed to save prefix: %s\n", error->message);
6486b0
-- 
6486b0
2.30.0
6486b0