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

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