Blame SOURCES/0001-library-use-getaddrinfo-with-AI_CANONNAME-to-find-a-.patch

ee8dc1
From 85b835f8258a57e3b23de47a255dddd822d5bfb3 Mon Sep 17 00:00:00 2001
ee8dc1
From: Sumit Bose <sbose@redhat.com>
ee8dc1
Date: Fri, 15 Mar 2019 17:33:44 +0100
ee8dc1
Subject: [PATCH] library: use getaddrinfo with AI_CANONNAME to find a FQDN
ee8dc1
ee8dc1
Currently adcli creates service principals only with a short name if the
ee8dc1
hostname of the client is a short name. This would fail is
ee8dc1
Kerberos/GSSAPI clients will use the fully-qualified domain name (FQDN)
ee8dc1
to access the host.
ee8dc1
ee8dc1
With this patch adcli tries to expand the short name by calling
ee8dc1
getaddrinfo with the AI_CANONNAME hint.
ee8dc1
ee8dc1
Related to https://gitlab.freedesktop.org/realmd/adcli/issues/1
ee8dc1
---
ee8dc1
 doc/adcli.xml    |  6 +++++-
ee8dc1
 library/adconn.c | 30 +++++++++++++++++++++++++++++-
ee8dc1
 2 files changed, 34 insertions(+), 2 deletions(-)
ee8dc1
ee8dc1
diff --git a/doc/adcli.xml b/doc/adcli.xml
ee8dc1
index 97dec08..4722c3a 100644
ee8dc1
--- a/doc/adcli.xml
ee8dc1
+++ b/doc/adcli.xml
ee8dc1
@@ -228,7 +228,11 @@ Password for Administrator:
ee8dc1
 			<term><option>-H, --host-fqdn=<parameter>host</parameter></option></term>
ee8dc1
 			<listitem><para>Override the local machine's fully qualified
ee8dc1
 			domain name. If not specified, the local machine's hostname
ee8dc1
-			will be retrieved via <function>gethostname()</function>.</para></listitem>
ee8dc1
+			will be retrieved via <function>gethostname()</function>.
ee8dc1
+			If <function>gethostname()</function> only returns a short name
ee8dc1
+			<function>getaddrinfo()</function> with the AI_CANONNAME hint
ee8dc1
+			is called to expand the name to a fully qualified domain
ee8dc1
+			name.</para></listitem>
ee8dc1
 		</varlistentry>
ee8dc1
 		<varlistentry>
ee8dc1
 			<term><option>-K, --host-keytab=<parameter>/path/to/keytab</parameter></option></term>
ee8dc1
diff --git a/library/adconn.c b/library/adconn.c
ee8dc1
index e2250e3..f6c23d3 100644
ee8dc1
--- a/library/adconn.c
ee8dc1
+++ b/library/adconn.c
ee8dc1
@@ -86,11 +86,36 @@ struct _adcli_conn_ctx {
ee8dc1
 	krb5_keytab keytab;
ee8dc1
 };
ee8dc1
 
ee8dc1
+static char *try_to_get_fqdn (const char *host_name)
ee8dc1
+{
ee8dc1
+	int ret;
ee8dc1
+	char *fqdn = NULL;
ee8dc1
+	struct addrinfo *res;
ee8dc1
+	struct addrinfo hints;
ee8dc1
+
ee8dc1
+	memset (&hints, 0, sizeof (struct addrinfo));
ee8dc1
+	hints.ai_socktype = SOCK_DGRAM;
ee8dc1
+	hints.ai_flags = AI_CANONNAME;
ee8dc1
+
ee8dc1
+	ret = getaddrinfo (host_name, NULL, &hints, &res;;
ee8dc1
+	if (ret != 0) {
ee8dc1
+		_adcli_err ("Failed to find FQDN: %s", gai_strerror (ret));
ee8dc1
+		return NULL;
ee8dc1
+	}
ee8dc1
+
ee8dc1
+	fqdn = strdup (res->ai_canonname);
ee8dc1
+
ee8dc1
+	freeaddrinfo (res);
ee8dc1
+
ee8dc1
+	return fqdn;
ee8dc1
+}
ee8dc1
+
ee8dc1
 static adcli_result
ee8dc1
 ensure_host_fqdn (adcli_result res,
ee8dc1
                   adcli_conn *conn)
ee8dc1
 {
ee8dc1
 	char hostname[HOST_NAME_MAX + 1];
ee8dc1
+	char *fqdn = NULL;
ee8dc1
 	int ret;
ee8dc1
 
ee8dc1
 	if (res != ADCLI_SUCCESS)
ee8dc1
@@ -107,7 +132,10 @@ ensure_host_fqdn (adcli_result res,
ee8dc1
 		return ADCLI_ERR_UNEXPECTED;
ee8dc1
 	}
ee8dc1
 
ee8dc1
-	conn->host_fqdn = strdup (hostname);
ee8dc1
+	if (strchr (hostname, '.') == NULL) {
ee8dc1
+		fqdn = try_to_get_fqdn (hostname);
ee8dc1
+	}
ee8dc1
+	conn->host_fqdn = fqdn != NULL ? fqdn : strdup (hostname);
ee8dc1
 	return_unexpected_if_fail (conn->host_fqdn != NULL);
ee8dc1
 	return ADCLI_SUCCESS;
ee8dc1
 }
ee8dc1
-- 
ee8dc1
2.20.1
ee8dc1