Blame SOURCES/0001-service-use-additional-dns-hostnames-with-net-ads-jo.patch

871079
From a49994ab4ac36ff39a1e24a228e57a5269bf8fdf Mon Sep 17 00:00:00 2001
871079
From: Sumit Bose <sbose@redhat.com>
871079
Date: Wed, 12 Aug 2020 12:58:27 +0200
871079
Subject: [PATCH] service: use 'additional dns hostnames' with net ads join
871079
871079
With newer versions of Samba the net ads join does not add services
871079
principals with the configured host name anymore but added the new
871079
option 'additional dns hostnames' for this.
871079
871079
realmd will try to figure out a fully-qualified host name and use it
871079
with the new option if it is from a different domain.
871079
871079
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1867912
871079
---
871079
 service/realm-disco.c        |  1 +
871079
 service/realm-disco.h        |  1 +
871079
 service/realm-samba-enroll.c | 57 +++++++++++++++++++++++++++++++++++-
871079
 service/realm-samba.c        |  6 ++++
871079
 4 files changed, 64 insertions(+), 1 deletion(-)
871079
871079
diff --git a/service/realm-disco.c b/service/realm-disco.c
871079
index ab06939..a12be50 100644
871079
--- a/service/realm-disco.c
871079
+++ b/service/realm-disco.c
871079
@@ -62,6 +62,7 @@ realm_disco_unref (gpointer data)
871079
 		g_free (disco->explicit_netbios);
871079
 		g_free (disco->kerberos_realm);
871079
 		g_free (disco->workgroup);
871079
+		g_free (disco->dns_fqdn);
871079
 		if (disco->server_address)
871079
 			g_object_unref (disco->server_address);
871079
 		g_free (disco);
871079
diff --git a/service/realm-disco.h b/service/realm-disco.h
871079
index 5f3e5e9..35532d2 100644
871079
--- a/service/realm-disco.h
871079
+++ b/service/realm-disco.h
871079
@@ -30,6 +30,7 @@ typedef struct {
871079
 	gchar *explicit_server;
871079
 	gchar *explicit_netbios;
871079
 	GSocketAddress *server_address;
871079
+	gchar *dns_fqdn;
871079
 } RealmDisco;
871079
 
871079
 #define        REALM_TYPE_DISCO             (realm_disco_get_type ())
871079
diff --git a/service/realm-samba-enroll.c b/service/realm-samba-enroll.c
871079
index 3f86c51..5624a08 100644
871079
--- a/service/realm-samba-enroll.c
871079
+++ b/service/realm-samba-enroll.c
871079
@@ -33,6 +33,9 @@
871079
 #include <errno.h>
871079
 #include <fcntl.h>
871079
 #include <string.h>
871079
+#include <sys/types.h>
871079
+#include <sys/socket.h>
871079
+#include <netdb.h>
871079
 
871079
 typedef struct {
871079
 	GDBusMethodInvocation *invocation;
871079
@@ -81,6 +84,44 @@ fallback_workgroup (const gchar *realm)
871079
 		return g_utf8_strup (realm, pos - realm);
871079
 }
871079
 
871079
+static char *
871079
+try_to_get_fqdn (void)
871079
+{
871079
+	char hostname[HOST_NAME_MAX + 1];
871079
+	gchar *fqdn = NULL;
871079
+	int ret;
871079
+	struct addrinfo *res;
871079
+	struct addrinfo hints;
871079
+
871079
+	ret = gethostname (hostname, sizeof (hostname));
871079
+	if (ret < 0) {
871079
+		return NULL;
871079
+	}
871079
+
871079
+	if (strchr (hostname, '.') == NULL) {
871079
+		memset (&hints, 0, sizeof (struct addrinfo));
871079
+		hints.ai_socktype = SOCK_DGRAM;
871079
+		hints.ai_flags = AI_CANONNAME;
871079
+
871079
+		ret = getaddrinfo (hostname, NULL, &hints, &res;;
871079
+		if (ret != 0) {
871079
+			return NULL;
871079
+		}
871079
+
871079
+		/* Only use a fully-qualified name */
871079
+		if (strchr (res->ai_canonname, '.') != NULL) {
871079
+			fqdn = g_strdup (res->ai_canonname);
871079
+		}
871079
+
871079
+		freeaddrinfo (res);
871079
+
871079
+	} else {
871079
+		fqdn = g_strdup (hostname);
871079
+	}
871079
+
871079
+	return fqdn;
871079
+}
871079
+
871079
 static JoinClosure *
871079
 join_closure_init (GTask *task,
871079
                    RealmDisco *disco,
871079
@@ -95,6 +136,8 @@ join_closure_init (GTask *task,
871079
 	const gchar *explicit_computer_name = NULL;
871079
 	const gchar *authid = NULL;
871079
 	gchar *name_from_keytab = NULL;
871079
+	gchar *fqdn = NULL;
871079
+	gchar *fqdn_dom = NULL;
871079
 
871079
 	join = g_new0 (JoinClosure, 1);
871079
 	join->disco = realm_disco_ref (disco);
871079
@@ -124,7 +167,7 @@ join_closure_init (GTask *task,
871079
 	                      "netbios name", authid,
871079
 	                      NULL);
871079
 
871079
-    /*
871079
+	/*
871079
 	 * Samba complains if we don't set a 'workgroup' setting for the realm we're
871079
 	 * going to join. If we didn't yet manage to lookup the workgroup, then go ahead
871079
 	 * and assume that the first domain component is the workgroup name.
871079
@@ -144,6 +187,18 @@ join_closure_init (GTask *task,
871079
 			g_free (workgroup);
871079
 	}
871079
 
871079
+	/* Add the fully-qualified DNS hostname as additional name if it is from
871079
+	* a different domain. */
871079
+	fqdn = try_to_get_fqdn ();
871079
+	if (fqdn != NULL && join->disco->domain_name != NULL
871079
+	                 && (fqdn_dom = strchr (fqdn, '.')) != NULL
871079
+	                 && g_ascii_strcasecmp (fqdn_dom + 1, join->disco->domain_name) != 0 ) {
871079
+		disco->dns_fqdn = g_strdup (fqdn);
871079
+		realm_ini_config_set (join->config, REALM_SAMBA_CONFIG_GLOBAL,
871079
+		                      "additional dns hostnames", disco->dns_fqdn, NULL);
871079
+	}
871079
+	g_free (fqdn);
871079
+
871079
 	/* Write out the config file for use by various net commands */
871079
 	join->custom_smb_conf = g_build_filename (g_get_tmp_dir (), "realmd-smb-conf.XXXXXX", NULL);
871079
 	temp_fd = g_mkstemp_full (join->custom_smb_conf, O_WRONLY, S_IRUSR | S_IWUSR);
871079
diff --git a/service/realm-samba.c b/service/realm-samba.c
871079
index 4940b38..fe33600 100644
871079
--- a/service/realm-samba.c
871079
+++ b/service/realm-samba.c
871079
@@ -204,6 +204,11 @@ on_join_do_winbind (GObject *source,
871079
 		                         NULL);
871079
 	}
871079
 
871079
+	if (error == NULL && enroll->disco->dns_fqdn != NULL) {
871079
+		realm_ini_config_change (self->config, REALM_SAMBA_CONFIG_GLOBAL, &error,
871079
+		                         "additional dns hostnames", enroll->disco->dns_fqdn,
871079
+		                         NULL);
871079
+	}
871079
 
871079
 	if (error == NULL) {
871079
 		name = realm_kerberos_get_name (REALM_KERBEROS (self));
871079
@@ -364,6 +369,7 @@ leave_deconfigure_begin (RealmSamba *self,
871079
 	if (!realm_ini_config_change (self->config, REALM_SAMBA_CONFIG_GLOBAL, &error,
871079
 	                              "workgroup", NULL,
871079
 	                              "realm", NULL,
871079
+	                              "additional dns hostnames", NULL,
871079
 	                              "security", "user",
871079
 	                              NULL)) {
871079
 		g_task_return_error (task, error);
871079
-- 
871079
2.28.0
871079