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

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