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

06f198
From 477dc8accccab568002bd19caa3fbf898bc05aad Mon Sep 17 00:00:00 2001
06f198
From: Ray Strode <rstrode@redhat.com>
06f198
Date: Tue, 25 Aug 2020 10:34:03 -0400
06f198
Subject: [PATCH 07/15] subman: Force re-subscribe if the admin already
06f198
 subscribed
06f198
06f198
It's possible for an admin to to half-enroll the system with RHN,
06f198
using the CLI tools.
06f198
06f198
Meaning, it's possible for them to register the system with the
06f198
service, but not attach to a purchased license for the machine,
06f198
the, so called, entitlements.
06f198
06f198
The subman module always does both halves of the registration process
06f198
in lock step.  This means, if an admin tries to register using GNOME
06f198
while in a half-registered state, subman will fail because the first
06f198
step, the registration step, is already finished.
06f198
06f198
This commit addresses that problem by trying to unregister up front
06f198
before registering.
06f198
---
06f198
 plugins/subman/gsd-subman-helper.c | 11 +++++++++--
06f198
 1 file changed, 9 insertions(+), 2 deletions(-)
06f198
06f198
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
06f198
index f84e91bf..3931ef2e 100644
06f198
--- a/plugins/subman/gsd-subman-helper.c
06f198
+++ b/plugins/subman/gsd-subman-helper.c
06f198
@@ -51,61 +51,60 @@ _helper_convert_error (const gchar *json_txt, GError **error)
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
 						     locale),
06f198
 				      G_DBUS_CALL_FLAGS_NONE,
06f198
 				      DBUS_TIMEOUT,
06f198
 				      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
@@ -208,60 +207,61 @@ main (int argc, char *argv[])
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
 
06f198
 	setlocale (LC_ALL, "");
06f198
 	locale = setlocale (LC_MESSAGES, NULL);
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
+	        g_debug ("unregistering");
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
@@ -277,96 +277,103 @@ main (int argc, char *argv[])
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 ("trying to unregister in case machine is already registered");
06f198
+		_helper_unregister (NULL);
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
 							     locale),
06f198
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
 					      DBUS_TIMEOUT,
06f198
 					      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
+
06f198
+		g_debug ("trying to unregister in case machine is already registered");
06f198
+		_helper_unregister (NULL);
06f198
+
06f198
+		g_debug ("registering using username and password");
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
 							     locale),
06f198
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
 					      DBUS_TIMEOUT,
06f198
 					      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
-- 
06f198
2.30.0
06f198