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

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