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

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