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

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