Blame SOURCES/0005-subman-Use-user-locale-for-registration-subscription.patch

e296c6
From db73e2211ecc746434d78d23d801c92581fa8824 Mon Sep 17 00:00:00 2001
e296c6
From: Ray Strode <rstrode@redhat.com>
e296c6
Date: Sun, 24 Jan 2021 15:04:17 -0500
e296c6
Subject: [PATCH 05/15] subman: Use user locale for registration/subscription
e296c6
 operations
e296c6
e296c6
This makes sure that error messages are in the correct locale.
e296c6
---
e296c6
 plugins/subman/gsd-subman-helper.c | 17 +++++++++++------
e296c6
 1 file changed, 11 insertions(+), 6 deletions(-)
e296c6
e296c6
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
e296c6
index af7a82e9..f84e91bf 100644
e296c6
--- a/plugins/subman/gsd-subman-helper.c
e296c6
+++ b/plugins/subman/gsd-subman-helper.c
e296c6
@@ -1,61 +1,63 @@
e296c6
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
e296c6
  *
e296c6
  * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
e296c6
  *
e296c6
  * Licensed under the GNU General Public License Version 2
e296c6
  *
e296c6
  * This program is free software; you can redistribute it and/or modify
e296c6
  * it under the terms of the GNU General Public License as published by
e296c6
  * the Free Software Foundation; either version 2 of the License, or
e296c6
  * (at your option) any later version.
e296c6
  *
e296c6
  * This program is distributed in the hope that it will be useful,
e296c6
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
e296c6
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e296c6
  * GNU General Public License for more details.
e296c6
  *
e296c6
  * You should have received a copy of the GNU General Public License
e296c6
  * along with this program; if not, write to the Free Software
e296c6
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e296c6
  */
e296c6
 
e296c6
 #include "config.h"
e296c6
 
e296c6
 #include <sys/types.h>
e296c6
 #include <unistd.h>
e296c6
 #include <stdlib.h>
e296c6
+#include <locale.h>
e296c6
 
e296c6
 #include <gio/gio.h>
e296c6
 #include <json-glib/json-glib.h>
e296c6
 
e296c6
 #define DBUS_TIMEOUT 300000 /* 5 minutes */
e296c6
+static const char *locale;
e296c6
 
e296c6
 static void
e296c6
 _helper_convert_error (const gchar *json_txt, GError **error)
e296c6
 {
e296c6
 	JsonNode *json_root;
e296c6
 	JsonObject *json_obj;
e296c6
 	const gchar *message;
e296c6
 	g_autoptr(JsonParser) json_parser = json_parser_new ();
e296c6
 
e296c6
 	/* this may be plain text or JSON :| */
e296c6
 	if (!json_parser_load_from_data (json_parser, json_txt, -1, NULL)) {
e296c6
 		g_set_error_literal (error,
e296c6
 				     G_IO_ERROR,
e296c6
 				     G_IO_ERROR_NOT_SUPPORTED,
e296c6
 				     json_txt);
e296c6
 		return;
e296c6
 	}
e296c6
 	json_root = json_parser_get_root (json_parser);
e296c6
 	json_obj = json_node_get_object (json_root);
e296c6
 	if (!json_object_has_member (json_obj, "message")) {
e296c6
 		g_set_error (error,
e296c6
 			     G_IO_ERROR,
e296c6
 			     G_IO_ERROR_INVALID_DATA,
e296c6
 			     "no message' in %s", json_txt);
e296c6
 		return;
e296c6
 	}
e296c6
 	message = json_object_get_string_member (json_obj, "message");
e296c6
 	if (g_strstr_len (message, -1, "Invalid user credentials") != NULL) {
e296c6
 		g_set_error_literal (error,
e296c6
 				     G_IO_ERROR,
e296c6
@@ -67,184 +69,187 @@ _helper_convert_error (const gchar *json_txt, GError **error)
e296c6
 			     G_IO_ERROR,
e296c6
 			     G_IO_ERROR_NOT_SUPPORTED,
e296c6
 			     message);
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_unregister (GError **error)
e296c6
 {
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 	g_debug ("unregistering");
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Unregister",
e296c6
 					       "com.redhat.RHSM1.Unregister",
e296c6
 					       NULL, error);
e296c6
 	if (proxy == NULL) {
e296c6
 		g_prefix_error (error, "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
e296c6
 	res = g_dbus_proxy_call_sync (proxy,
e296c6
 				      "Unregister",
e296c6
 				      g_variant_new ("(a{sv}s)",
e296c6
 						     proxy_options,
e296c6
-						     ""), /* lang */
e296c6
+						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
 				      NULL, error);
e296c6
 	return res != NULL;
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_auto_attach (GError **error)
e296c6
 {
e296c6
 	const gchar *str = NULL;
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 	g_debug ("auto-attaching subscriptions");
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Attach",
e296c6
 					       "com.redhat.RHSM1.Attach",
e296c6
 					       NULL, error);
e296c6
 	if (proxy == NULL) {
e296c6
 		g_prefix_error (error, "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
e296c6
 	res = g_dbus_proxy_call_sync (proxy,
e296c6
 				      "AutoAttach",
e296c6
 				      g_variant_new ("(sa{sv}s)",
e296c6
 						     "", /* now? */
e296c6
 						     proxy_options,
e296c6
-						     ""), /* lang */
e296c6
+						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
 				      NULL, error);
e296c6
 	if (res == NULL)
e296c6
 		return FALSE;
e296c6
 	g_variant_get (res, "(&s)", &str);
e296c6
 	g_debug ("Attach.AutoAttach: %s", str);
e296c6
 	return TRUE;
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_save_config (const gchar *key, const gchar *value, GError **error)
e296c6
 {
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Config",
e296c6
 					       "com.redhat.RHSM1.Config",
e296c6
 					       NULL, error);
e296c6
 	if (proxy == NULL) {
e296c6
 		g_prefix_error (error, "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	res = g_dbus_proxy_call_sync (proxy, "Set",
e296c6
 				      g_variant_new ("(svs)",
e296c6
 						     key,
e296c6
 						     g_variant_new_string (value),
e296c6
-						     ""), /* lang */
e296c6
+						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
 				      NULL, error);
e296c6
 	return res != NULL;
e296c6
 }
e296c6
 
e296c6
 int
e296c6
 main (int argc, char *argv[])
e296c6
 {
e296c6
-	const gchar *userlang = ""; /* as root, so no translations */
e296c6
 	g_autofree gchar *activation_key = NULL;
e296c6
 	g_autofree gchar *address = NULL;
e296c6
 	g_autofree gchar *hostname = NULL;
e296c6
 	g_autofree gchar *kind = NULL;
e296c6
 	g_autofree gchar *organisation = NULL;
e296c6
 	g_autofree gchar *password = NULL;
e296c6
 	g_autofree gchar *port = NULL;
e296c6
 	g_autofree gchar *prefix = NULL;
e296c6
 	g_autofree gchar *proxy_server = NULL;
e296c6
 	g_autofree gchar *username = NULL;
e296c6
 	g_autoptr(GDBusConnection) conn_private = NULL;
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GError) error = NULL;
e296c6
 	g_autoptr(GOptionContext) context = g_option_context_new (NULL);
e296c6
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
e296c6
 	g_autoptr(GVariantBuilder) subman_conopts = NULL;
e296c6
 	g_autoptr(GVariantBuilder) subman_options = NULL;
e296c6
 
e296c6
 	const GOptionEntry options[] = {
e296c6
 		{ "kind", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&kind, "Kind, e.g. 'username' or 'key'", NULL },
e296c6
 		{ "address", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&address, "UNIX address", NULL },
e296c6
 		{ "username", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&username, "Username", NULL },
e296c6
 		{ "password", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&password, "Password", NULL },
e296c6
 		{ "organisation", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&organisation, "Organisation", NULL },
e296c6
 		{ "activation-key", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
e296c6
 			&activation_key, "Activation keys", NULL },
e296c6
 		{ "hostname", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
e296c6
 			&hostname, "Registration server hostname", NULL },
e296c6
 		{ "prefix", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
e296c6
 			&prefix, "Registration server prefix", NULL },
e296c6
 		{ "port", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
e296c6
 			&port, "Registration server port", NULL },
e296c6
 		{ "proxy", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
e296c6
 			&proxy_server, "Proxy settings", NULL },
e296c6
 		{ NULL}
e296c6
 	};
e296c6
 
e296c6
 	/* check calling UID */
e296c6
 	if (getuid () != 0 || geteuid () != 0) {
e296c6
 		g_printerr ("This program can only be used by the root user\n");
e296c6
 		return G_IO_ERROR_NOT_SUPPORTED;
e296c6
 	}
e296c6
+
e296c6
+	setlocale (LC_ALL, "");
e296c6
+	locale = setlocale (LC_MESSAGES, NULL);
e296c6
+
e296c6
 	g_option_context_add_main_entries (context, options, NULL);
e296c6
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
e296c6
 		g_printerr ("Failed to parse arguments: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_SUPPORTED;
e296c6
 	}
e296c6
 
e296c6
 	/* uncommon actions */
e296c6
 	if (kind == NULL) {
e296c6
 		g_printerr ("No --kind specified\n");
e296c6
 		return G_IO_ERROR_INVALID_DATA;
e296c6
 	}
e296c6
 	if (g_strcmp0 (kind, "unregister") == 0) {
e296c6
 		if (!_helper_unregister (&error)) {
e296c6
 			g_printerr ("Failed to Unregister: %s\n", error->message);
e296c6
 			return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 		}
e296c6
 		return EXIT_SUCCESS;
e296c6
 	}
e296c6
 	if (g_strcmp0 (kind, "auto-attach") == 0) {
e296c6
 		if (!_helper_auto_attach (&error)) {
e296c6
 			g_printerr ("Failed to AutoAttach: %s\n", error->message);
e296c6
 			return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 		}
e296c6
 		return EXIT_SUCCESS;
e296c6
 	}
e296c6
 
e296c6
 	/* connect to abstract socket for reasons */
e296c6
 	if (address == NULL) {
e296c6
 		g_printerr ("No --address specified\n");
e296c6
 		return G_IO_ERROR_INVALID_DATA;
e296c6
@@ -281,96 +286,96 @@ main (int argc, char *argv[])
e296c6
 		port = g_strdup ("443");
e296c6
 	subman_conopts = g_variant_builder_new (G_VARIANT_TYPE("a{ss}"));
e296c6
 	g_variant_builder_add (subman_conopts, "{ss}", "host", hostname);
e296c6
 	g_variant_builder_add (subman_conopts, "{ss}", "handler", prefix);
e296c6
 	g_variant_builder_add (subman_conopts, "{ss}", "port", port);
e296c6
 
e296c6
 	/* call into RHSM */
e296c6
 	if (g_strcmp0 (kind, "register-with-key") == 0) {
e296c6
 		g_auto(GStrv) activation_keys = NULL;
e296c6
 		g_autoptr(GError) error_local = NULL;
e296c6
 		g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 		if (activation_key == NULL) {
e296c6
 			g_printerr ("Required --activation-key\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 		if (organisation == NULL) {
e296c6
 			g_printerr ("Required --organisation\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 
e296c6
 		g_debug ("registering using activation key");
e296c6
 		activation_keys = g_strsplit (activation_key, ",", -1);
e296c6
 		res = g_dbus_proxy_call_sync (proxy,
e296c6
 					      "RegisterWithActivationKeys",
e296c6
 					      g_variant_new ("(s^asa{ss}a{ss}s)",
e296c6
 							     organisation,
e296c6
 							     activation_keys,
e296c6
 							     subman_options,
e296c6
 							     subman_conopts,
e296c6
-							     userlang),
e296c6
+							     locale),
e296c6
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
e296c6
 					      DBUS_TIMEOUT,
e296c6
 					      NULL, &error_local);
e296c6
 		if (res == NULL) {
e296c6
 			g_dbus_error_strip_remote_error (error_local);
e296c6
 			_helper_convert_error (error_local->message, &error);
e296c6
 			g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
e296c6
 			return error->code;
e296c6
 		}
e296c6
 	} else if (g_strcmp0 (kind, "register-with-username") == 0) {
e296c6
 		g_autoptr(GError) error_local = NULL;
e296c6
 		g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 		g_debug ("registering using username and password");
e296c6
 		if (username == NULL) {
e296c6
 			g_printerr ("Required --username\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 		if (password == NULL) {
e296c6
 			g_printerr ("Required --password\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 		if (organisation == NULL) {
e296c6
 			g_printerr ("Required --organisation\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 		res = g_dbus_proxy_call_sync (proxy,
e296c6
 					      "Register",
e296c6
 					      g_variant_new ("(sssa{ss}a{ss}s)",
e296c6
 							     organisation,
e296c6
 							     username,
e296c6
 							     password,
e296c6
 							     subman_options,
e296c6
 							     subman_conopts,
e296c6
-							     userlang),
e296c6
+							     locale),
e296c6
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
e296c6
 					      DBUS_TIMEOUT,
e296c6
 					      NULL, &error_local);
e296c6
 		if (res == NULL) {
e296c6
 			g_dbus_error_strip_remote_error (error_local);
e296c6
 			_helper_convert_error (error_local->message, &error);
e296c6
 			g_printerr ("Failed to Register: %s\n", error->message);
e296c6
 			return error->code;
e296c6
 		}
e296c6
 	} else {
e296c6
 		g_printerr ("Invalid --kind specified: %s\n", kind);
e296c6
 		return G_IO_ERROR_INVALID_DATA;
e296c6
 	}
e296c6
 
e296c6
 	/* set the new hostname */
e296c6
 	if (!_helper_save_config ("server.hostname", hostname, &error)) {
e296c6
 		g_printerr ("Failed to save hostname: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 	if (!_helper_save_config ("server.prefix", prefix, &error)) {
e296c6
 		g_printerr ("Failed to save prefix: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 	if (!_helper_save_config ("server.port", port, &error)) {
e296c6
 		g_printerr ("Failed to save port: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 
e296c6
 	/* wait for rhsmd to notice the new config */
e296c6
 	g_usleep (G_USEC_PER_SEC * 5);
e296c6
-- 
e296c6
2.30.0
e296c6