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

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