Blob Blame History Raw
From 8f417fb7fee088dba728e083bc5553a5f237f660 Mon Sep 17 00:00:00 2001
From: Ondrej Holy <oholy@redhat.com>
Date: Thu, 2 Nov 2023 10:35:48 +0100
Subject: [PATCH] ipa: Propagate hostname error

When a computer hostname is wrong, the `ipa-client-install` cmd fails
with the "invalid hostname" error. However, the join method fails with
the generic `REALM_ERROR_INTERNAL` error. Let's fail with the dedicated
`REALM_ERROR_BAD_HOSTNAME` instead.

Related: https://gitlab.gnome.org/GNOME/gnome-initial-setup/-/issues/123
Related: https://gitlab.gnome.org/GNOME/gnome-initial-setup/-/issues/124
---
 service/realm-sssd-ipa.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/service/realm-sssd-ipa.c b/service/realm-sssd-ipa.c
index 0cb1a5b..fca8e25 100644
--- a/service/realm-sssd-ipa.c
+++ b/service/realm-sssd-ipa.c
@@ -129,6 +129,23 @@ on_restart_done (GObject *source,
 	g_object_unref (task);
 }
 
+static gchar *
+parse_hostname_error (const gchar *output)
+{
+	GRegex* regex;
+	GMatchInfo *match_info = NULL;
+	gchar *reason = NULL;
+
+	regex = g_regex_new ("invalid hostname: (.+)", 0, 0, NULL);
+	if (g_regex_match (regex, output, 0, &match_info))
+		reason = g_match_info_fetch (match_info, 1);
+
+	g_match_info_unref (match_info);
+	g_regex_unref (regex);
+
+	return reason;
+}
+
 static void
 on_ipa_client_do_restart (GObject *source,
                           GAsyncResult *result,
@@ -149,6 +166,7 @@ on_ipa_client_do_restart (GObject *source,
 	gchar *section;
 	gchar *home;
 	gint status;
+	gchar *reason;
 
 	status = realm_command_run_finish (result, &output, &error);
 
@@ -163,6 +181,11 @@ on_ipa_client_do_restart (GObject *source,
 		if (g_pattern_match_simple ("*kinit: Password incorrect*", output->str)) {
 			g_set_error (&error, REALM_ERROR, REALM_ERROR_AUTH_FAILED,
 			             "Password is incorrect");
+		} else if ((reason = parse_hostname_error (output->str)) != NULL) {
+			g_set_error (&error, REALM_ERROR, REALM_ERROR_BAD_HOSTNAME,
+			             "This computer's host name is not set correctly: %s",
+			             reason);
+			g_free (reason);
 		} else {
 			g_set_error (&error, REALM_ERROR, REALM_ERROR_INTERNAL,
 			             "Running ipa-client-install failed");
-- 
2.43.2