Blame SOURCES/autofs-5.1.5-fix-macro-expansion-in-selector-values.patch

135b98
autofs-5.1.5 - fix macro expansion in selector values
135b98
135b98
From: Ian Kent <raven@themaw.net>
135b98
135b98
Macro expansion is not done in selector values before use, for example
135b98
in "hostd==${/key}.<donain>.<name>" the ${/key} is not expanded before
135b98
use leading to an attempt to use an invalid host name.
135b98
135b98
Signed-off-by: Ian Kent <raven@themaw.net>
135b98
---
135b98
 CHANGELOG           |    1 
135b98
 modules/parse_amd.c |  286 ++++++++++++++++++++++++++++++++++------------------
135b98
 2 files changed, 193 insertions(+), 94 deletions(-)
135b98
135b98
--- autofs-5.1.4.orig/CHANGELOG
135b98
+++ autofs-5.1.4/CHANGELOG
135b98
@@ -52,6 +52,7 @@ xx/xx/2018 autofs-5.1.5
135b98
 - log mount call arguments if mount_verbose is set.
135b98
 - make expire remaining log level debug.
135b98
 - allow period following macro in selector value.
135b98
+- fix macro expansion in selector values.
135b98
 
135b98
 19/12/2017 autofs-5.1.4
135b98
 - fix spec file url.
135b98
--- autofs-5.1.4.orig/modules/parse_amd.c
135b98
+++ autofs-5.1.4/modules/parse_amd.c
135b98
@@ -231,17 +231,29 @@ static struct substvar *add_lookup_vars(
135b98
 	return list;
135b98
 }
135b98
 
135b98
-static int match_my_name(unsigned int logopt, const char *name, struct substvar *sv)
135b98
+static int match_my_name(struct autofs_point *ap, const char *name, struct substvar *sv)
135b98
 {
135b98
 	struct addrinfo hints, *cni, *ni, *haddr;
135b98
 	char host[NI_MAXHOST + 1], numeric[NI_MAXHOST + 1];
135b98
+	unsigned int logopt = ap->logopt;
135b98
 	const struct substvar *v;
135b98
+	char *exp_name = NULL;
135b98
 	int rv = 0, ret;
135b98
 
135b98
+	if (!expand_selectors(ap, name, &exp_name, sv))
135b98
+		exp_name = strdup(name);
135b98
+	if (!exp_name) {
135b98
+		error(logopt,
135b98
+		      MODPREFIX "error: failed to alloc space for name");
135b98
+		goto out;
135b98
+	}
135b98
+
135b98
 	v = macro_findvar(sv, "host", 4);
135b98
 	if (v) {
135b98
-		if (!strcmp(v->val, name))
135b98
-			return 1;
135b98
+		if (!strcmp(v->val, exp_name)) {
135b98
+			rv = 1;
135b98
+			goto out;
135b98
+		}
135b98
 	}
135b98
 
135b98
 	if (!v || !v->val) {
135b98
@@ -268,11 +280,11 @@ static int match_my_name(unsigned int lo
135b98
 	hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME;
135b98
 
135b98
 	/* Resolve comparison name to its names and compare */
135b98
-	ret = getaddrinfo(name, NULL, &hints, &ni);
135b98
+	ret = getaddrinfo(exp_name, NULL, &hints, &ni);
135b98
 	if (ret) {
135b98
 		error(logopt, MODPREFIX
135b98
 		      "hostname lookup for %s failed: %s\n",
135b98
-		      name, gai_strerror(ret));
135b98
+		      exp_name, gai_strerror(ret));
135b98
 		freeaddrinfo(cni);
135b98
 		goto out;
135b98
 	}
135b98
@@ -310,18 +322,180 @@ next:
135b98
 	freeaddrinfo(ni);
135b98
 	freeaddrinfo(cni);
135b98
 out:
135b98
+	if (exp_name)
135b98
+		free(exp_name);
135b98
 	return rv;
135b98
 }
135b98
 
135b98
-static int eval_selector(unsigned int logopt,
135b98
+static int sel_strcmp(struct autofs_point *ap,
135b98
+		      const struct substvar *v, struct selector *s,
135b98
+		      struct substvar *sv)
135b98
+{
135b98
+	char *expand = NULL;
135b98
+	int ret = 0;
135b98
+	int res;
135b98
+
135b98
+	res = expand_selectors(ap, s->comp.value, &expand, sv);
135b98
+	if (res)
135b98
+		res = strcmp(v->val, expand);
135b98
+	else
135b98
+		res = strcmp(v->val, s->comp.value);
135b98
+
135b98
+	if (s->compare & SEL_COMP_EQUAL && !res) {
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "matched selector %s(%s) == %s",
135b98
+		      v->def, v->val, expand ? expand : s->comp.value);
135b98
+		ret = 1;
135b98
+	} else if (s->compare & SEL_COMP_NOTEQUAL && res) {
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "matched selector %s(%s) != %s",
135b98
+		      v->def, v->val, expand ? expand : s->comp.value);
135b98
+		ret = 1;
135b98
+	} else
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "did not match selector %s(%s) %s %s",
135b98
+		      v->def, v->val,
135b98
+		      (s->compare & SEL_COMP_EQUAL ? "==" : "!="),
135b98
+		      expand ? expand : s->comp.value);
135b98
+
135b98
+	if (expand)
135b98
+		free(expand);
135b98
+
135b98
+	return ret;
135b98
+}
135b98
+
135b98
+static int sel_lstat(struct autofs_point *ap,
135b98
+		     struct selector *s, struct substvar *sv)
135b98
+{
135b98
+	struct stat st;
135b98
+	char *expand = NULL;
135b98
+	int res, ret;
135b98
+
135b98
+	/* Sould be OK to fail on any error here */
135b98
+	res = expand_selectors(ap, s->func.arg1, &expand, sv);
135b98
+	if (res)
135b98
+		ret = !lstat(expand, &st);
135b98
+	else
135b98
+		ret = !lstat(s->func.arg1, &st);
135b98
+
135b98
+	if (s->compare == SEL_COMP_NOT)
135b98
+		ret = !ret;
135b98
+	if (ret)
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "matched selector %s(%s)",
135b98
+		      s->sel->name, expand ? expand : s->func.arg1);
135b98
+	else
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "did not match selector %s(%s)",
135b98
+		      s->sel->name, expand ? expand : s->func.arg1);
135b98
+	if (expand)
135b98
+		free(expand);
135b98
+
135b98
+	return ret;
135b98
+}
135b98
+
135b98
+static int sel_in_network(struct autofs_point *ap,
135b98
+			  struct selector *s, struct substvar *sv)
135b98
+{
135b98
+	char *expand = NULL;
135b98
+	int res, ret;
135b98
+
135b98
+	res = expand_selectors(ap, s->func.arg1, &expand, sv);
135b98
+	if (!res)
135b98
+		ret = in_network(s->func.arg1);
135b98
+	else
135b98
+		ret = in_network(expand);
135b98
+
135b98
+	if (s->compare == SEL_COMP_NOT)
135b98
+		ret = !ret;
135b98
+	if (ret)
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "matched selector %s(%s)",
135b98
+		      s->sel->name, expand ? expand : s->func.arg1);
135b98
+	else
135b98
+		debug(ap->logopt, MODPREFIX
135b98
+		      "did not match selector %s(%s)",
135b98
+		      s->sel->name, expand ? expand : s->func.arg1);
135b98
+
135b98
+	if (expand)
135b98
+		free(expand);
135b98
+
135b98
+	return ret;
135b98
+}
135b98
+
135b98
+static int sel_netgrp(struct autofs_point *ap,
135b98
+		      struct selector *s, struct substvar *sv)
135b98
+{
135b98
+	char *exp_arg1 = NULL, *exp_arg2 = NULL;
135b98
+	const struct substvar *v;
135b98
+	int res, ret = 0;
135b98
+	char *host;
135b98
+
135b98
+	if (s->func.arg2) {
135b98
+		res = expand_selectors(ap, s->func.arg2, &exp_arg2, sv);
135b98
+		if (res)
135b98
+			host = exp_arg2;
135b98
+		else
135b98
+			host = s->func.arg2;
135b98
+	} else {
135b98
+		if (s->sel->selector == SEL_NETGRP)
135b98
+			v = macro_findvar(sv, "host", 4);
135b98
+		else
135b98
+			v = macro_findvar(sv, "hostd", 5);
135b98
+		if (!v || !*v->val) {
135b98
+			error(ap->logopt, MODPREFIX
135b98
+			     "failed to get value of ${host}");
135b98
+			goto out;
135b98
+		}
135b98
+		host = v->val;
135b98
+	}
135b98
+
135b98
+	res = expand_selectors(ap, s->func.arg1, &exp_arg1, sv);
135b98
+	if (res)
135b98
+		ret = innetgr(exp_arg1, host, NULL, NULL);
135b98
+	else
135b98
+		ret = innetgr(s->func.arg1, host, NULL, NULL);
135b98
+
135b98
+	if (s->compare == SEL_COMP_NOT)
135b98
+		ret = !ret;
135b98
+	if (ret) {
135b98
+		if (!s->func.arg2)
135b98
+			debug(ap->logopt, MODPREFIX
135b98
+			      "matched selector %s(%s)",
135b98
+			      s->sel->name, exp_arg1 ? exp_arg1 : s->func.arg1);
135b98
+		else
135b98
+			debug(ap->logopt, MODPREFIX
135b98
+			      "matched selector %s(%s,%s)", s->sel->name,
135b98
+			      exp_arg1 ? exp_arg1 : s->func.arg1,
135b98
+			      exp_arg2 ? exp_arg2 : s->func.arg2);
135b98
+	} else {
135b98
+		if (!s->func.arg2)
135b98
+			debug(ap->logopt, MODPREFIX
135b98
+			      "did not match selector %s(%s)",
135b98
+			      s->sel->name, exp_arg1 ? exp_arg1 : s->func.arg1);
135b98
+		else
135b98
+			debug(ap->logopt, MODPREFIX
135b98
+			      "did not match selector %s(%s,%s)", s->sel->name,
135b98
+			      exp_arg1 ? exp_arg1 : s->func.arg1,
135b98
+			      exp_arg2 ? exp_arg2 : s->func.arg2);
135b98
+	}
135b98
+out:
135b98
+	if (exp_arg1)
135b98
+		free(exp_arg1);
135b98
+	if (exp_arg2)
135b98
+		free(exp_arg2);
135b98
+
135b98
+	return ret;
135b98
+}
135b98
+
135b98
+static int eval_selector(struct autofs_point *ap,
135b98
 			 struct amd_entry *this, struct substvar *sv)
135b98
 {
135b98
 	struct selector *s = this->selector;
135b98
+	unsigned int logopt = ap->logopt;
135b98
 	const struct substvar *v;
135b98
 	unsigned int s_type;
135b98
 	unsigned int v_type;
135b98
-	struct stat st;
135b98
-	char *host;
135b98
 	int res, val, ret = 0;
135b98
 
135b98
 	s_type = s->sel->flags & SEL_FLAGS_TYPE_MASK;
135b98
@@ -339,26 +513,7 @@ static int eval_selector(unsigned int lo
135b98
 
135b98
 		switch (v_type) {
135b98
 		case SEL_FLAG_STR:
135b98
-			res = strcmp(v->val, s->comp.value);
135b98
-			if (s->compare & SEL_COMP_EQUAL && !res) {
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "matched selector %s(%s) == %s",
135b98
-				      v->def, v->val, s->comp.value);
135b98
-				ret = 1;
135b98
-				break;
135b98
-			} else if (s->compare & SEL_COMP_NOTEQUAL && res) {
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "matched selector %s(%s) != %s",
135b98
-				      v->def, v->val, s->comp.value);
135b98
-				ret = 1;
135b98
-				break;
135b98
-			}
135b98
-
135b98
-			debug(logopt, MODPREFIX
135b98
-				      "did not match selector %s(%s) %s %s",
135b98
-				      v->def, v->val,
135b98
-				      (s->compare & SEL_COMP_EQUAL ? "==" : "!="),
135b98
-				      s->comp.value);
135b98
+			ret = sel_strcmp(ap, v, s, sv);
135b98
 			break;
135b98
 
135b98
 		case SEL_FLAG_NUM:
135b98
@@ -434,7 +589,7 @@ static int eval_selector(unsigned int lo
135b98
 			break;
135b98
 
135b98
 		case SEL_XHOST:
135b98
-			ret = match_my_name(logopt, s->func.arg1, sv);
135b98
+			ret = match_my_name(ap, s->func.arg1, sv);
135b98
 			if (s->compare == SEL_COMP_NOT)
135b98
 				ret = !ret;
135b98
 			if (ret)
135b98
@@ -448,32 +603,11 @@ static int eval_selector(unsigned int lo
135b98
 			break;
135b98
 
135b98
 		case SEL_EXISTS:
135b98
-			/* Sould be OK to fail on any error here */
135b98
-			ret = !lstat(s->func.arg1, &st);
135b98
-			if (s->compare == SEL_COMP_NOT)
135b98
-				ret = !ret;
135b98
-			if (ret)
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "matched selector %s(%s)",
135b98
-				      s->sel->name, s->func.arg1);
135b98
-			else
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "did not match selector %s(%s)",
135b98
-				      s->sel->name, s->func.arg1);
135b98
+			ret = sel_lstat(ap, s, sv);
135b98
 			break;
135b98
 
135b98
 		case SEL_IN_NETWORK:
135b98
-			ret = in_network(s->func.arg1);
135b98
-			if (s->compare == SEL_COMP_NOT)
135b98
-				ret = !ret;
135b98
-			if (ret)
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "matched selector %s(%s)",
135b98
-				      s->sel->name, s->func.arg1);
135b98
-			else
135b98
-				debug(logopt, MODPREFIX
135b98
-				      "did not match selector %s(%s)",
135b98
-				      s->sel->name, s->func.arg1);
135b98
+			ret = sel_in_network(ap, s, sv);
135b98
 			break;
135b98
 
135b98
 		default:
135b98
@@ -492,43 +626,7 @@ static int eval_selector(unsigned int lo
135b98
 		switch (s->sel->selector) {
135b98
 		case SEL_NETGRP:
135b98
 		case SEL_NETGRPD:
135b98
-			if (s->func.arg2)
135b98
-				host = s->func.arg2;
135b98
-			else {
135b98
-				if (s->sel->selector == SEL_NETGRP)
135b98
-					v = macro_findvar(sv, "host", 4);
135b98
-				else
135b98
-					v = macro_findvar(sv, "hostd", 5);
135b98
-				if (!v || !*v->val) {
135b98
-					error(logopt, MODPREFIX
135b98
-					     "failed to get value of ${host}");
135b98
-					break;
135b98
-				}
135b98
-				host = v->val;
135b98
-			}
135b98
-			ret = innetgr(s->func.arg1, host, NULL, NULL);
135b98
-			if (s->compare == SEL_COMP_NOT)
135b98
-				ret = !ret;
135b98
-			if (ret) {
135b98
-				if (!s->func.arg2)
135b98
-					debug(logopt, MODPREFIX
135b98
-					      "matched selector %s(%s)",
135b98
-					      s->sel->name, s->func.arg1);
135b98
-				else
135b98
-					debug(logopt, MODPREFIX
135b98
-					      "matched selector %s(%s,%s)",
135b98
-					      s->sel->name, s->func.arg1,
135b98
-					      s->func.arg2);
135b98
-			} else {
135b98
-				if (!s->func.arg2)
135b98
-					debug(logopt, MODPREFIX
135b98
-					      "did not match selector %s(%s)",
135b98
-					      s->sel->name, s->func.arg1);
135b98
-				else
135b98
-					debug(logopt, MODPREFIX
135b98
-					      "did not match selector %s(%s,%s)",
135b98
-					      s->sel->name, s->func.arg1, s->func.arg2);
135b98
-			}
135b98
+			ret = sel_netgrp(ap, s, sv);
135b98
 			break;
135b98
 
135b98
 		default:
135b98
@@ -1753,7 +1851,7 @@ static void update_prefix(struct autofs_
135b98
 	return;
135b98
 }
135b98
 
135b98
-static int match_selectors(unsigned int logopt,
135b98
+static int match_selectors(struct autofs_point *ap,
135b98
 			   struct amd_entry *entry, struct substvar *sv)
135b98
 {
135b98
 	struct selector *s = entry->selector;
135b98
@@ -1761,7 +1859,7 @@ static int match_selectors(unsigned int
135b98
 
135b98
 	/* No selectors, always match */
135b98
 	if (!s) {
135b98
-		debug(logopt, MODPREFIX "no selectors found in location");
135b98
+		debug(ap->logopt, MODPREFIX "no selectors found in location");
135b98
 		return 1;
135b98
 	}
135b98
 
135b98
@@ -1769,7 +1867,7 @@ static int match_selectors(unsigned int
135b98
 
135b98
 	/* All selectors must match */
135b98
 	while (s) {
135b98
-		ret = eval_selector(logopt, entry, sv);
135b98
+		ret = eval_selector(ap, entry, sv);
135b98
 		if (!ret)
135b98
 			break;
135b98
 		s = s->next;
135b98
@@ -1939,7 +2037,7 @@ static struct amd_entry *select_default_
135b98
 		if (!this->selector)
135b98
 			continue;
135b98
 
135b98
-		if (match_selectors(ap->logopt, this, sv)) {
135b98
+		if (match_selectors(ap, this, sv)) {
135b98
 			if (entry_default) {
135b98
 				/*update_with_defaults(entry_default, this, sv);*/
135b98
 				free_amd_entry(entry_default);
135b98
@@ -2192,7 +2290,7 @@ int parse_mount(struct autofs_point *ap,
135b98
 			break;
135b98
 		}
135b98
 
135b98
-		if (!match_selectors(ap->logopt, this, sv))
135b98
+		if (!match_selectors(ap, this, sv))
135b98
 			continue;
135b98
 
135b98
 		at_least_one = 1;