Blame SOURCES/autofs-5.1.4-add-fedfs-getsrvinfo_c.patch

aea863
autofs-5.1.4 - add fedfs-getsrvinfo.c
aea863
aea863
From: Ian Kent <raven@themaw.net>
aea863
aea863
Add the fedfs domain information discovery library functions.
aea863
aea863
Signed-off-by: Ian Kent <raven@themaw.net>
aea863
---
aea863
 CHANGELOG                |    1 
aea863
 fedfs/fedfs-getsrvinfo.c |  311 ++++++++++++++++++++++++++++++++++++++++++++++
aea863
 fedfs/fedfs-getsrvinfo.h |   52 ++++++++
aea863
 3 files changed, 364 insertions(+)
aea863
 create mode 100644 fedfs/fedfs-getsrvinfo.c
aea863
 create mode 100644 fedfs/fedfs-getsrvinfo.h
aea863
aea863
diff --git a/CHANGELOG b/CHANGELOG
aea863
index dbfb8389..8d6c737c 100644
aea863
--- a/CHANGELOG
aea863
+++ b/CHANGELOG
aea863
@@ -14,6 +14,7 @@ xx/xx/2018 autofs-5.1.5
aea863
 - Makefiles.rules: remove 'samples' from SUBDIRS.
aea863
 - dont allow trailing slash in master map mount points.
aea863
 - fix libresolv configure check.
aea863
+- add fedfs-getsrvinfo.c.
aea863
 
aea863
 19/12/2017 autofs-5.1.4
aea863
 - fix spec file url.
aea863
diff --git a/fedfs/fedfs-getsrvinfo.c b/fedfs/fedfs-getsrvinfo.c
aea863
new file mode 100644
aea863
index 00000000..02ad16b5
aea863
--- /dev/null
aea863
+++ b/fedfs/fedfs-getsrvinfo.c
aea863
@@ -0,0 +1,311 @@
aea863
+/*
aea863
+ * Copyright 2011 Oracle.  All rights reserved.
aea863
+ *
aea863
+ * This file is part of fedfs-utils.
aea863
+ *
aea863
+ * fedfs-utils is free software; you can redistribute it and/or modify
aea863
+ * it under the terms of the GNU General Public License version 2.0 as
aea863
+ * published by the Free Software Foundation.
aea863
+ *
aea863
+ * fedfs-utils is distributed in the hope that it will be useful, but
aea863
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
aea863
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aea863
+ * GNU General Public License version 2.0 for more details.
aea863
+ *
aea863
+ * You should have received a copy of the GNU General Public License
aea863
+ * version 2.0 along with fedfs-utils.  If not, see:
aea863
+ *
aea863
+ *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
aea863
+ */
aea863
+
aea863
+#ifdef HAVE_CONFIG_H
aea863
+#include <config.h>
aea863
+#endif
aea863
+
aea863
+#include <sys/types.h>
aea863
+#include <sys/socket.h>
aea863
+
aea863
+#include <stdbool.h>
aea863
+#include <string.h>
aea863
+#include <unistd.h>
aea863
+#include <stdlib.h>
aea863
+#include <stdint.h>
aea863
+
aea863
+#include <stdio.h>
aea863
+#include <errno.h>
aea863
+#include <netdb.h>
aea863
+#include <netinet/in.h>
aea863
+#include <arpa/inet.h>
aea863
+#include <arpa/nameser.h>
aea863
+#include <arpa/nameser_compat.h>
aea863
+#include <resolv.h>
aea863
+
aea863
+#include "fedfs-getsrvinfo.h"
aea863
+
aea863
+/**
aea863
+ * Parsing overlay for DNS query result record header.  Fields are
aea863
+ * in network byte order.
aea863
+ */
aea863
+struct rechdr {
aea863
+	uint16_t		 type;
aea863
+	uint16_t		 class;
aea863
+	uint32_t		 ttl;
aea863
+	uint16_t		 length;
aea863
+} __attribute__((__packed__));
aea863
+
aea863
+/**
aea863
+ * Parsing overlay for DNS query result SRV record data.  Fields
aea863
+ * are in network byte order.
aea863
+ */
aea863
+struct srv {
aea863
+	uint16_t		 priority;
aea863
+	uint16_t		 weight;
aea863
+	uint16_t		 port;
aea863
+	unsigned char		*target;
aea863
+} __attribute__((__packed__));
aea863
+
aea863
+/**
aea863
+ * Return C string matching error value of "status"
aea863
+ *
aea863
+ * @param status error status returned by getsrvinfo
aea863
+ * @return pointer to static NUL-terminated C string containing error message
aea863
+ */
aea863
+const char *
aea863
+gsi_strerror(int status)
aea863
+{
aea863
+	static char buf[256];
aea863
+
aea863
+	switch (status) {
aea863
+	case ESI_SUCCESS:
aea863
+		return "Success";
aea863
+	case ESI_NONAME:
aea863
+		return "Name not found";
aea863
+	case ESI_AGAIN:
aea863
+		return "Temporary failure in name resolution";
aea863
+	case ESI_FAIL:
aea863
+		return "Non-recoverable failure in name resolution";
aea863
+	case ESI_NODATA:
aea863
+		return "No SRV record returned";
aea863
+	case ESI_SERVICE:
aea863
+		return "Service is not available";
aea863
+	case ESI_MEMORY:
aea863
+		return "Memory allocation failure";
aea863
+	case ESI_SYSTEM:
aea863
+		snprintf(buf, sizeof(buf), "System error (%d): %s",
aea863
+				status, strerror(errno));
aea863
+		return buf;
aea863
+	case ESI_PARSE:
aea863
+		return "Failed to parse server response";
aea863
+	}
aea863
+
aea863
+	snprintf(buf, sizeof(buf), "Unknown error (%d)", status);
aea863
+	return buf;
aea863
+}
aea863
+
aea863
+/**
aea863
+ * Release a list of SRV records allocated by getsrvinfo()
aea863
+ *
aea863
+ * @param si pointer to first element of a list of struct srvinfo
aea863
+ *
aea863
+ */
aea863
+void freesrvinfo(struct srvinfo *si)
aea863
+{
aea863
+	struct srvinfo *tmp;
aea863
+
aea863
+	while (si != NULL) {
aea863
+		tmp = si;
aea863
+		si = si->si_next;
aea863
+		free(tmp->si_target);
aea863
+		free(tmp);
aea863
+	}
aea863
+}
aea863
+
aea863
+/**
aea863
+ * Ordering predicate for srvinfo lists
aea863
+ *
aea863
+ * @param a a srvinfo list element to compare
aea863
+ * @param b another srvinfo list element to compare
aea863
+ * @return true if "b" should fall later in the list than "a"
aea863
+ *
aea863
+ * See RFC 2782.   The list is ordered as follows:
aea863
+ *
aea863
+ *  o Lowest priority first.
aea863
+ *  o In each priority class, highest weight first.
aea863
+ */
aea863
+static _Bool
aea863
+srvinfo_is_after(const struct srvinfo *a, const struct srvinfo *b)
aea863
+{
aea863
+	if (a->si_priority > b->si_priority)
aea863
+		return true;
aea863
+	if (a->si_priority < b->si_priority)
aea863
+		return false;
aea863
+
aea863
+	if (a->si_weight < b->si_weight)
aea863
+		return true;
aea863
+	return false;
aea863
+}
aea863
+
aea863
+/**
aea863
+ * Insert a srvinfo element into a list
aea863
+ *
aea863
+ * @param head pointer to head of list of elements
aea863
+ * @param entry new list element to insert
aea863
+ *
aea863
+ */
aea863
+static void
aea863
+insert_srvinfo(struct srvinfo **head, struct srvinfo *entry)
aea863
+{
aea863
+	entry->si_next = *head;
aea863
+	*head = entry;
aea863
+}
aea863
+
aea863
+/**
aea863
+ * Insert a srvinfo element into a list, in order
aea863
+ *
aea863
+ * @param head pointer to head of list of elements
aea863
+ * @param entry new list element to insert
aea863
+ *
aea863
+ */
aea863
+static void
aea863
+insert_srvinfo_sorted(struct srvinfo **head, struct srvinfo *entry)
aea863
+{
aea863
+	struct srvinfo *spot, *back;
aea863
+
aea863
+	spot = *head;
aea863
+	back = NULL;
aea863
+	while (spot && srvinfo_is_after(spot, entry)) {
aea863
+		back = spot;
aea863
+		spot = spot->si_next;
aea863
+	}
aea863
+
aea863
+	if (spot == (*head))
aea863
+		insert_srvinfo(head, entry);
aea863
+	else {
aea863
+		insert_srvinfo(&spot, entry);
aea863
+		/* off the end of the list? */
aea863
+		if (spot == entry)
aea863
+			back->si_next = entry;
aea863
+	}
aea863
+}
aea863
+
aea863
+/**
aea863
+ * Retrieve list of SRV records from DNS service
aea863
+ *
aea863
+ * @param srvname NUL-terminated C string containing record type to look up
aea863
+ * @param domainname NUL-terminated C string containing domain name to look up
aea863
+ * @param si OUT: list of SRV record information retrieved
aea863
+ * @return zero on success, or an ESI_ status code describing the error
aea863
+ *
aea863
+ * Caller must free list of records using freesrvinfo().
aea863
+ */
aea863
+int
aea863
+getsrvinfo(const char *srvname, const char *domainname, struct srvinfo **si)
aea863
+{
aea863
+	unsigned char *msg, *eom, *comp_dn;
aea863
+	struct srvinfo *results;
aea863
+	unsigned short count, i;
aea863
+	int status, len;
aea863
+	char *exp_dn;
aea863
+	HEADER *hdr;
aea863
+
aea863
+	msg = calloc(1, NS_MAXMSG);
aea863
+	exp_dn = calloc(1, NS_MAXDNAME);
aea863
+	if (msg == NULL || exp_dn == NULL) {
aea863
+		status = ESI_MEMORY;
aea863
+		goto out;
aea863
+	}
aea863
+
aea863
+	_res.options |= RES_AAONLY;
aea863
+	len = res_querydomain(srvname, domainname, C_IN, T_SRV, msg, NS_MAXMSG);
aea863
+	if (len == -1) {
aea863
+		switch (h_errno) {
aea863
+		case HOST_NOT_FOUND:
aea863
+			status = ESI_NONAME;
aea863
+			break;
aea863
+		case TRY_AGAIN:
aea863
+			status = ESI_AGAIN;
aea863
+			break;
aea863
+		case NO_RECOVERY:
aea863
+			status = ESI_FAIL;
aea863
+			break;
aea863
+		case NO_DATA:
aea863
+			status = ESI_NODATA;
aea863
+			break;
aea863
+		default:
aea863
+			fprintf(stderr, "SRV query failed for %s.%s: %s\n",
aea863
+				srvname, domainname, hstrerror(h_errno));
aea863
+			status = ESI_FAIL;
aea863
+		}
aea863
+		goto out;
aea863
+	}
aea863
+
aea863
+	hdr = (HEADER *)msg;
aea863
+	count = ntohs(hdr->ancount);
aea863
+	if (count == 0) {
aea863
+		status = ESI_NODATA;
aea863
+		goto out;
aea863
+	}
aea863
+	eom = msg + len;
aea863
+
aea863
+	comp_dn = &msg[HFIXEDSZ];
aea863
+	comp_dn += dn_skipname(comp_dn, eom) + QFIXEDSZ;
aea863
+
aea863
+	results = NULL;
aea863
+	for (i = 0; i < count; i++) {
aea863
+		struct srvinfo *new;
aea863
+		struct srv *record;
aea863
+		int l;
aea863
+
aea863
+		l = dn_expand(msg, eom, comp_dn, exp_dn, NS_MAXDNAME);
aea863
+		if (l == -1) {
aea863
+			status = ESI_PARSE;
aea863
+			goto out_free;
aea863
+		}
aea863
+		comp_dn += l;
aea863
+
aea863
+		record = (struct srv *)&comp_dn[10];
aea863
+		comp_dn += 16;
aea863
+
aea863
+		l = dn_expand(msg, eom, comp_dn, exp_dn, NS_MAXDNAME);
aea863
+		if (l == -1) {
aea863
+			status = ESI_PARSE;
aea863
+			goto out_free;
aea863
+		}
aea863
+		comp_dn += l;
aea863
+
aea863
+		if (count == 1 && strcmp(exp_dn, ".") == 0) {
aea863
+			status = ESI_SERVICE;
aea863
+			goto out_free;
aea863
+		}
aea863
+
aea863
+		new = calloc(1, sizeof(*new));
aea863
+		if (new == NULL) {
aea863
+			status = ESI_MEMORY;
aea863
+			goto out;
aea863
+		}
aea863
+
aea863
+		new->si_target = strdup(exp_dn);
aea863
+		if (new->si_target == NULL) {
aea863
+			free(new);
aea863
+			status = ESI_MEMORY;
aea863
+			goto out;
aea863
+		}
aea863
+		new->si_priority = ntohs(record->priority);
aea863
+		new->si_weight = ntohs(record->weight);
aea863
+		new->si_port = ntohs(record->port);
aea863
+
aea863
+		insert_srvinfo_sorted(&results, new);
aea863
+	}
aea863
+
aea863
+	status = ESI_SUCCESS;
aea863
+	*si = results;
aea863
+
aea863
+out:
aea863
+	free(exp_dn);
aea863
+	free(msg);
aea863
+	return status;
aea863
+
aea863
+out_free:
aea863
+	freesrvinfo(results);
aea863
+	goto out;
aea863
+}
aea863
diff --git a/fedfs/fedfs-getsrvinfo.h b/fedfs/fedfs-getsrvinfo.h
aea863
new file mode 100644
aea863
index 00000000..13170359
aea863
--- /dev/null
aea863
+++ b/fedfs/fedfs-getsrvinfo.h
aea863
@@ -0,0 +1,52 @@
aea863
+/*
aea863
+ * Copyright 2011 Oracle.  All rights reserved.
aea863
+ *
aea863
+ * This file is part of fedfs-utils.
aea863
+ *
aea863
+ * fedfs-utils is free software; you can redistribute it and/or modify
aea863
+ * it under the terms of the GNU General Public License version 2.0 as
aea863
+ * published by the Free Software Foundation.
aea863
+ *
aea863
+ * fedfs-utils is distributed in the hope that it will be useful, but
aea863
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
aea863
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aea863
+ * GNU General Public License version 2.0 for more details.
aea863
+ *
aea863
+ * You should have received a copy of the GNU General Public License
aea863
+ * version 2.0 along with fedfs-utils.  If not, see:
aea863
+ *
aea863
+ *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
aea863
+ */
aea863
+
aea863
+#ifndef _FEDFS_GETSRVINFO_H_
aea863
+#define _FEDFS_GETSRVINFO_H_
aea863
+
aea863
+/**
aea863
+ * Single list element containing SRV record data
aea863
+ */
aea863
+struct srvinfo {
aea863
+	struct srvinfo		*si_next;
aea863
+	char			*si_target;
aea863
+	unsigned short		 si_priority;
aea863
+	unsigned short		 si_weight;
aea863
+	unsigned short		 si_port;
aea863
+};
aea863
+
aea863
+enum {
aea863
+	ESI_SUCCESS	= 0,
aea863
+	ESI_NONAME	= -2,
aea863
+	ESI_AGAIN	= -3,
aea863
+	ESI_FAIL	= -4,
aea863
+	ESI_NODATA	= -5,
aea863
+	ESI_SERVICE	= -8,
aea863
+	ESI_MEMORY	= -10,
aea863
+	ESI_SYSTEM	= -11,
aea863
+	ESI_PARSE	= -1000,
aea863
+};
aea863
+
aea863
+int		 getsrvinfo(const char *srvname, const char *domainname,
aea863
+				struct srvinfo **si);
aea863
+void		 freesrvinfo(struct srvinfo *si);
aea863
+const char	*gsi_strerror(int status);
aea863
+
aea863
+#endif	/* !_FEDFS_GETSRVINFO_H_ */