Blob Blame History Raw
From 5e28cf702ad338e399f8fff0b3fa18736a297318 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Tue, 21 Aug 2018 13:09:20 +0200
Subject: [PATCH 3/3] discover: try to get domain name from hostname

If there is no domain name returned by DHCP check if the hostname
contains a domain part and use this to discover a realm.

Related to https://bugzilla.redhat.com/show_bug.cgi?id=1619162
---
 service/realm-provider.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/service/realm-provider.c b/service/realm-provider.c
index d647c7a..258e8e1 100644
--- a/service/realm-provider.c
+++ b/service/realm-provider.c
@@ -28,6 +28,8 @@
 #include <glib/gi18n.h>
 #include <gio/gio.h>
 
+#include <errno.h>
+
 #define TIMEOUT_SECONDS 15
 
 G_DEFINE_TYPE (RealmProvider, realm_provider, G_TYPE_DBUS_OBJECT_SKELETON);
@@ -181,6 +183,25 @@ on_discover_complete (GObject *source,
 	return_discover_result (method, realms, relevance, error);
 }
 
+static gchar *
+get_domain_from_hostname (void)
+{
+	gchar hostname[HOST_NAME_MAX + 1];
+	gchar *dot;
+
+	if (gethostname (hostname, sizeof (hostname)) < 0) {
+		g_warning ("Couldn't get the computer host name: %s", g_strerror (errno));
+		return NULL;
+	}
+
+	dot = strchr (hostname, '.');
+	if (dot != NULL) {
+		return g_strdup (dot + 1);
+	}
+
+	return NULL;
+}
+
 static void
 on_discover_default (GObject *source,
                      GAsyncResult *result,
@@ -195,6 +216,10 @@ on_discover_default (GObject *source,
 		g_clear_error (&error);
 	}
 
+	if (method->string == NULL) {
+		method->string = get_domain_from_hostname ();
+	}
+
 	if (method->string) {
 		g_strstrip (method->string);
 		if (g_str_equal (method->string, "")) {
@@ -210,7 +235,8 @@ on_discover_default (GObject *source,
 		                         on_discover_complete, method);
 
 	} else {
-		realm_diagnostics_info (method->invocation, "No default domain received via DHCP");
+		realm_diagnostics_info (method->invocation,
+		                        "No default domain received via DHCP or given by hostname");
 		return_discover_result (method, NULL, 0, NULL);
 	}
 }
-- 
2.17.1