Blame SOURCES/libnfsidmap-0.25-dns-resolved.patch

da1e07
diff --git a/configure.ac b/configure.ac
da1e07
index 5179e96..db1cee2 100644
da1e07
--- a/configure.ac
da1e07
+++ b/configure.ac
da1e07
@@ -13,6 +13,8 @@ AC_PROG_CC
da1e07
 
da1e07
 # Checks for libraries.
da1e07
 
da1e07
+AC_CHECK_LIB([resolv], [__res_querydomain], , AC_MSG_ERROR(res_querydomain needed))
da1e07
+
da1e07
 AC_ARG_ENABLE([ldap],
da1e07
 	[AS_HELP_STRING([--disable-ldap],[Disable support for LDAP @<:@default=detect@:>@])])
da1e07
 if test "x$enable_ldap" != "xno" ; then
da1e07
diff --git a/libnfsidmap.c b/libnfsidmap.c
da1e07
index b9c0db3..d484101 100644
da1e07
--- a/libnfsidmap.c
da1e07
+++ b/libnfsidmap.c
da1e07
@@ -53,6 +53,10 @@
da1e07
 #include <stdarg.h>
da1e07
 #include <dlfcn.h>
da1e07
 #include <ctype.h>
da1e07
+#include <resolv.h>
da1e07
+#include <arpa/nameser.h>
da1e07
+#include <arpa/nameser_compat.h>
da1e07
+
da1e07
 #include "nfsidmap.h"
da1e07
 #include "nfsidmap_internal.h"
da1e07
 #include "cfg.h"
da1e07
@@ -81,6 +85,11 @@ gid_t nobody_gid = (gid_t)-1;
da1e07
 #define IDMAPD_DEFAULT_DOMAIN "localdomain"
da1e07
 #endif
da1e07
 
da1e07
+#ifndef NFS4DNSTXTREC
da1e07
+#define NFS4DNSTXTREC "_nfsv4idmapdomain"
da1e07
+#endif
da1e07
+
da1e07
+
da1e07
 /* Default logging fuction */
da1e07
 static void default_logger(const char *fmt, ...)
da1e07
 {
da1e07
@@ -116,6 +125,93 @@ static int id_as_chars(char *name, uid_t *id)
da1e07
 	return 1;
da1e07
 }
da1e07
 
da1e07
+static int dns_txt_query(char *domain, char **nfs4domain)
da1e07
+{
da1e07
+	char *txtname = NFS4DNSTXTREC;
da1e07
+	char *msg, *answ, *eom, *mptr; 
da1e07
+	int len, status = -1;
da1e07
+	HEADER *hdr;
da1e07
+	
da1e07
+	msg = calloc(1, NS_MAXMSG);
da1e07
+	if (msg == NULL)
da1e07
+		return -1;
da1e07
+
da1e07
+	answ = calloc(1, NS_MAXMSG);
da1e07
+	if (answ == NULL) {
da1e07
+		free(msg);
da1e07
+		return -1;
da1e07
+	}
da1e07
+
da1e07
+	if (res_init() < 0) {
da1e07
+		IDMAP_LOG(2, ("libnfsidmap: res_init() failed for %s.%s: %s\n",
da1e07
+			txtname, domain, hstrerror(h_errno)));
da1e07
+		goto freemem;
da1e07
+	}
da1e07
+	len = res_querydomain(txtname, domain, C_IN, T_TXT, msg, NS_MAXMSG);
da1e07
+	if (len < 0) {
da1e07
+		IDMAP_LOG(2, ("libnfsidmap: res_querydomain() failed for %s.%s: %s\n",
da1e07
+			txtname, domain, hstrerror(h_errno)));
da1e07
+		goto freemem;
da1e07
+	}
da1e07
+	hdr = (HEADER *)msg;
da1e07
+
da1e07
+	/* See if there is an answer */
da1e07
+	if (ntohs(hdr->ancount) < 1) {
da1e07
+		IDMAP_LOG(2, ("libnfsidmap: No TXT record for %s.%s\n",
da1e07
+			txtname, domain));
da1e07
+		goto freemem;
da1e07
+	}
da1e07
+	/* find the EndOfMessage */
da1e07
+	eom = msg + len;
da1e07
+
da1e07
+	/* skip header */
da1e07
+	mptr = &msg[HFIXEDSZ];
da1e07
+
da1e07
+	/* skip name field in question section */
da1e07
+	mptr += dn_skipname(mptr, eom) + QFIXEDSZ;
da1e07
+
da1e07
+	/* read in the question */
da1e07
+	len = dn_expand(msg, eom, mptr, answ, NS_MAXDNAME);
da1e07
+	if (len < 0) { /* does this really matter?? */
da1e07
+		IDMAP_LOG(2, ("libnfsidmap: No question section for %s.%s: %s\n",
da1e07
+			txtname, domain, hstrerror(h_errno)));
da1e07
+		goto freemem;
da1e07
+	}
da1e07
+
da1e07
+	/*
da1e07
+	 * Now, dissect the answer section, Note: if there
da1e07
+	 * are more than one answer only the first
da1e07
+	 * one will be used. 
da1e07
+	 */
da1e07
+
da1e07
+	/* skip passed the name field  */
da1e07
+	mptr += dn_skipname(mptr, eom);
da1e07
+	/* skip pass the type class and ttl fields */
da1e07
+	mptr += 2 + 2 + 4;
da1e07
+
da1e07
+	/* make sure there is some data */
da1e07
+	GETSHORT(len, mptr);
da1e07
+	if (len < 0) {
da1e07
+		IDMAP_LOG(2, ("libnfsidmap: No data in answer for %s.%s\n",
da1e07
+			txtname, domain));
da1e07
+		goto freemem;
da1e07
+	}
da1e07
+	/* get the lenght field */
da1e07
+	len = (int)*mptr++;
da1e07
+	/* copy the data */
da1e07
+	memcpy(answ, mptr, len);
da1e07
+	answ[len] = '\0';
da1e07
+	
da1e07
+	*nfs4domain = strdup(answ);
da1e07
+	status = 0;
da1e07
+
da1e07
+freemem:
da1e07
+	free(msg);
da1e07
+	free(answ);
da1e07
+
da1e07
+	return (status);
da1e07
+}
da1e07
+
da1e07
 static int domain_from_dns(char **domain)
da1e07
 {
da1e07
 	struct hostent *he;
da1e07
@@ -127,7 +223,13 @@ static int domain_from_dns(char **domain)
da1e07
 		return -1;
da1e07
 	if ((c = strchr(he->h_name, '.')) == NULL || *++c == '\0')
da1e07
 		return -1;
da1e07
-	*domain = strdup(c);
da1e07
+	/* 
da1e07
+	 * Query DNS to see if the _nfsv4idmapdomain TXT record exists
da1e07
+	 * If so use it... 
da1e07
+	 */
da1e07
+	if (dns_txt_query(c, domain) < 0)
da1e07
+		*domain = strdup(c);
da1e07
+
da1e07
 	return 0;
da1e07
 }
da1e07