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

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