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

6486b0
From db73e2211ecc746434d78d23d801c92581fa8824 Mon Sep 17 00:00:00 2001
6486b0
From: Ray Strode <rstrode@redhat.com>
6486b0
Date: Sun, 24 Jan 2021 15:04:17 -0500
6486b0
Subject: [PATCH 05/15] subman: Use user locale for registration/subscription
6486b0
 operations
6486b0
6486b0
This makes sure that error messages are in the correct locale.
6486b0
---
6486b0
 plugins/subman/gsd-subman-helper.c | 17 +++++++++++------
6486b0
 1 file changed, 11 insertions(+), 6 deletions(-)
6486b0
6486b0
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
6486b0
index af7a82e9..f84e91bf 100644
6486b0
--- a/plugins/subman/gsd-subman-helper.c
6486b0
+++ b/plugins/subman/gsd-subman-helper.c
6486b0
@@ -1,61 +1,63 @@
6486b0
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
6486b0
  *
6486b0
  * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
6486b0
  *
6486b0
  * Licensed under the GNU General Public License Version 2
6486b0
  *
6486b0
  * This program is free software; you can redistribute it and/or modify
6486b0
  * it under the terms of the GNU General Public License as published by
6486b0
  * the Free Software Foundation; either version 2 of the License, or
6486b0
  * (at your option) any later version.
6486b0
  *
6486b0
  * This program is distributed in the hope that it will be useful,
6486b0
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
6486b0
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6486b0
  * GNU General Public License for more details.
6486b0
  *
6486b0
  * You should have received a copy of the GNU General Public License
6486b0
  * along with this program; if not, write to the Free Software
6486b0
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
6486b0
  */
6486b0
 
6486b0
 #include "config.h"
6486b0
 
6486b0
 #include <sys/types.h>
6486b0
 #include <unistd.h>
6486b0
 #include <stdlib.h>
6486b0
+#include <locale.h>
6486b0
 
6486b0
 #include <gio/gio.h>
6486b0
 #include <json-glib/json-glib.h>
6486b0
 
6486b0
 #define DBUS_TIMEOUT 300000 /* 5 minutes */
6486b0
+static const char *locale;
6486b0
 
6486b0
 static void
6486b0
 _helper_convert_error (const gchar *json_txt, GError **error)
6486b0
 {
6486b0
 	JsonNode *json_root;
6486b0
 	JsonObject *json_obj;
6486b0
 	const gchar *message;
6486b0
 	g_autoptr(JsonParser) json_parser = json_parser_new ();
6486b0
 
6486b0
 	/* this may be plain text or JSON :| */
6486b0
 	if (!json_parser_load_from_data (json_parser, json_txt, -1, NULL)) {
6486b0
 		g_set_error_literal (error,
6486b0
 				     G_IO_ERROR,
6486b0
 				     G_IO_ERROR_NOT_SUPPORTED,
6486b0
 				     json_txt);
6486b0
 		return;
6486b0
 	}
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
@@ -67,184 +69,187 @@ _helper_convert_error (const gchar *json_txt, GError **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
-						     ""), /* lang */
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
 	g_autoptr(GVariant) res = NULL;
6486b0
 
6486b0
 	g_debug ("auto-attaching subscriptions");
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/Attach",
6486b0
 					       "com.redhat.RHSM1.Attach",
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
 				      "AutoAttach",
6486b0
 				      g_variant_new ("(sa{sv}s)",
6486b0
 						     "", /* now? */
6486b0
 						     proxy_options,
6486b0
-						     ""), /* lang */
6486b0
+						     locale),
6486b0
 				      G_DBUS_CALL_FLAGS_NONE,
6486b0
 				      DBUS_TIMEOUT,
6486b0
 				      NULL, error);
6486b0
 	if (res == NULL)
6486b0
 		return FALSE;
6486b0
 	g_variant_get (res, "(&s)", &str);
6486b0
 	g_debug ("Attach.AutoAttach: %s", str);
6486b0
 	return TRUE;
6486b0
 }
6486b0
 
6486b0
 static gboolean
6486b0
 _helper_save_config (const gchar *key, const gchar *value, GError **error)
6486b0
 {
6486b0
 	g_autoptr(GDBusProxy) proxy = NULL;
6486b0
 	g_autoptr(GVariant) res = NULL;
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/Config",
6486b0
 					       "com.redhat.RHSM1.Config",
6486b0
 					       NULL, error);
6486b0
 	if (proxy == NULL) {
6486b0
 		g_prefix_error (error, "Failed to get proxy: ");
6486b0
 		return FALSE;
6486b0
 	}
6486b0
 	res = g_dbus_proxy_call_sync (proxy, "Set",
6486b0
 				      g_variant_new ("(svs)",
6486b0
 						     key,
6486b0
 						     g_variant_new_string (value),
6486b0
-						     ""), /* lang */
6486b0
+						     locale),
6486b0
 				      G_DBUS_CALL_FLAGS_NONE,
6486b0
 				      DBUS_TIMEOUT,
6486b0
 				      NULL, error);
6486b0
 	return res != NULL;
6486b0
 }
6486b0
 
6486b0
 int
6486b0
 main (int argc, char *argv[])
6486b0
 {
6486b0
-	const gchar *userlang = ""; /* as root, so no translations */
6486b0
 	g_autofree gchar *activation_key = NULL;
6486b0
 	g_autofree gchar *address = NULL;
6486b0
 	g_autofree gchar *hostname = NULL;
6486b0
 	g_autofree gchar *kind = NULL;
6486b0
 	g_autofree gchar *organisation = NULL;
6486b0
 	g_autofree gchar *password = NULL;
6486b0
 	g_autofree gchar *port = NULL;
6486b0
 	g_autofree gchar *prefix = NULL;
6486b0
 	g_autofree gchar *proxy_server = NULL;
6486b0
 	g_autofree gchar *username = NULL;
6486b0
 	g_autoptr(GDBusConnection) conn_private = NULL;
6486b0
 	g_autoptr(GDBusProxy) proxy = NULL;
6486b0
 	g_autoptr(GError) error = NULL;
6486b0
 	g_autoptr(GOptionContext) context = g_option_context_new (NULL);
6486b0
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
6486b0
 	g_autoptr(GVariantBuilder) subman_conopts = NULL;
6486b0
 	g_autoptr(GVariantBuilder) subman_options = NULL;
6486b0
 
6486b0
 	const GOptionEntry options[] = {
6486b0
 		{ "kind", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&kind, "Kind, e.g. 'username' or 'key'", NULL },
6486b0
 		{ "address", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&address, "UNIX address", NULL },
6486b0
 		{ "username", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&username, "Username", NULL },
6486b0
 		{ "password", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&password, "Password", NULL },
6486b0
 		{ "organisation", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&organisation, "Organisation", NULL },
6486b0
 		{ "activation-key", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
6486b0
 			&activation_key, "Activation keys", NULL },
6486b0
 		{ "hostname", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,
6486b0
 			&hostname, "Registration server hostname", NULL },
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
 		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
@@ -281,96 +286,96 @@ main (int argc, char *argv[])
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 ("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
-							     userlang),
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
 		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
-							     userlang),
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
 		return G_IO_ERROR_NOT_INITIALIZED;
6486b0
 	}
6486b0
 	if (!_helper_save_config ("server.port", port, &error)) {
6486b0
 		g_printerr ("Failed to save port: %s\n", error->message);
6486b0
 		return G_IO_ERROR_NOT_INITIALIZED;
6486b0
 	}
6486b0
 
6486b0
 	/* wait for rhsmd to notice the new config */
6486b0
 	g_usleep (G_USEC_PER_SEC * 5);
6486b0
-- 
6486b0
2.30.0
6486b0