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

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