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

306fa1
autofs-5.1.5 - fix macro expansion in selector values
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
Macro expansion is not done in selector values before use, for example
306fa1
in "hostd==${/key}.<donain>.<name>" the ${/key} is not expanded before
306fa1
use leading to an attempt to use an invalid host name.
306fa1
306fa1
Signed-off-by: Ian Kent <raven@themaw.net>
306fa1
---
306fa1
 CHANGELOG           |    1 
306fa1
 modules/parse_amd.c |  286 ++++++++++++++++++++++++++++++++++------------------
306fa1
 2 files changed, 193 insertions(+), 94 deletions(-)
306fa1
306fa1
--- autofs-5.0.7.orig/CHANGELOG
306fa1
+++ autofs-5.0.7/CHANGELOG
306fa1
@@ -327,6 +327,7 @@
306fa1
 - workaround getaddrinfo(3) ai_canonname bug
306fa1
 - improve hostname lookup error logging.
306fa1
 - allow period following macro in selector value.
306fa1
+- fix macro expansion in selector values.
306fa1
 
306fa1
 25/07/2012 autofs-5.0.7
306fa1
 =======================
306fa1
--- autofs-5.0.7.orig/modules/parse_amd.c
306fa1
+++ autofs-5.0.7/modules/parse_amd.c
306fa1
@@ -231,17 +231,29 @@ static struct substvar *add_lookup_vars(
306fa1
 	return list;
306fa1
 }
306fa1
 
306fa1
-static int match_my_name(unsigned int logopt, const char *name, struct substvar *sv)
306fa1
+static int match_my_name(struct autofs_point *ap, const char *name, struct substvar *sv)
306fa1
 {
306fa1
 	struct addrinfo hints, *cni, *ni, *haddr;
306fa1
 	char host[NI_MAXHOST + 1], numeric[NI_MAXHOST + 1];
306fa1
+	unsigned int logopt = ap->logopt;
306fa1
 	const struct substvar *v;
306fa1
+	char *exp_name = NULL;
306fa1
 	int rv = 0, ret;
306fa1
 
306fa1
+	if (!expand_selectors(ap, name, &exp_name, sv))
306fa1
+		exp_name = strdup(name);
306fa1
+	if (!exp_name) {
306fa1
+		error(logopt,
306fa1
+		      MODPREFIX "error: failed to alloc space for name");
306fa1
+		goto out;
306fa1
+	}
306fa1
+
306fa1
 	v = macro_findvar(sv, "host", 4);
306fa1
 	if (v) {
306fa1
-		if (!strcmp(v->val, name))
306fa1
-			return 1;
306fa1
+		if (!strcmp(v->val, exp_name)) {
306fa1
+			rv = 1;
306fa1
+			goto out;
306fa1
+		}
306fa1
 	}
306fa1
 
306fa1
 	if (!v || !v->val) {
306fa1
@@ -268,11 +280,11 @@ static int match_my_name(unsigned int lo
306fa1
 	hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME;
306fa1
 
306fa1
 	/* Resolve comparison name to its names and compare */
306fa1
-	ret = getaddrinfo(name, NULL, &hints, &ni);
306fa1
+	ret = getaddrinfo(exp_name, NULL, &hints, &ni);
306fa1
 	if (ret) {
306fa1
 		error(logopt, MODPREFIX
306fa1
 		      "hostname lookup for %s failed: %s\n",
306fa1
-		      name, gai_strerror(ret));
306fa1
+		      exp_name, gai_strerror(ret));
306fa1
 		freeaddrinfo(cni);
306fa1
 		goto out;
306fa1
 	}
306fa1
@@ -312,18 +324,180 @@ next:
306fa1
 	freeaddrinfo(ni);
306fa1
 	freeaddrinfo(cni);
306fa1
 out:
306fa1
+	if (exp_name)
306fa1
+		free(exp_name);
306fa1
 	return rv;
306fa1
 }
306fa1
 
306fa1
-static int eval_selector(unsigned int logopt,
306fa1
+static int sel_strcmp(struct autofs_point *ap,
306fa1
+		      const struct substvar *v, struct selector *s,
306fa1
+		      struct substvar *sv)
306fa1
+{
306fa1
+	char *expand = NULL;
306fa1
+	int ret = 0;
306fa1
+	int res;
306fa1
+
306fa1
+	res = expand_selectors(ap, s->comp.value, &expand, sv);
306fa1
+	if (res)
306fa1
+		res = strcmp(v->val, expand);
306fa1
+	else
306fa1
+		res = strcmp(v->val, s->comp.value);
306fa1
+
306fa1
+	if (s->compare & SEL_COMP_EQUAL && !res) {
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "matched selector %s(%s) == %s",
306fa1
+		      v->def, v->val, expand ? expand : s->comp.value);
306fa1
+		ret = 1;
306fa1
+	} else if (s->compare & SEL_COMP_NOTEQUAL && res) {
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "matched selector %s(%s) != %s",
306fa1
+		      v->def, v->val, expand ? expand : s->comp.value);
306fa1
+		ret = 1;
306fa1
+	} else
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "did not match selector %s(%s) %s %s",
306fa1
+		      v->def, v->val,
306fa1
+		      (s->compare & SEL_COMP_EQUAL ? "==" : "!="),
306fa1
+		      expand ? expand : s->comp.value);
306fa1
+
306fa1
+	if (expand)
306fa1
+		free(expand);
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
+static int sel_lstat(struct autofs_point *ap,
306fa1
+		     struct selector *s, struct substvar *sv)
306fa1
+{
306fa1
+	struct stat st;
306fa1
+	char *expand = NULL;
306fa1
+	int res, ret;
306fa1
+
306fa1
+	/* Sould be OK to fail on any error here */
306fa1
+	res = expand_selectors(ap, s->func.arg1, &expand, sv);
306fa1
+	if (res)
306fa1
+		ret = !lstat(expand, &st);
306fa1
+	else
306fa1
+		ret = !lstat(s->func.arg1, &st);
306fa1
+
306fa1
+	if (s->compare == SEL_COMP_NOT)
306fa1
+		ret = !ret;
306fa1
+	if (ret)
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "matched selector %s(%s)",
306fa1
+		      s->sel->name, expand ? expand : s->func.arg1);
306fa1
+	else
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "did not match selector %s(%s)",
306fa1
+		      s->sel->name, expand ? expand : s->func.arg1);
306fa1
+	if (expand)
306fa1
+		free(expand);
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
+static int sel_in_network(struct autofs_point *ap,
306fa1
+			  struct selector *s, struct substvar *sv)
306fa1
+{
306fa1
+	char *expand = NULL;
306fa1
+	int res, ret;
306fa1
+
306fa1
+	res = expand_selectors(ap, s->func.arg1, &expand, sv);
306fa1
+	if (!res)
306fa1
+		ret = in_network(s->func.arg1);
306fa1
+	else
306fa1
+		ret = in_network(expand);
306fa1
+
306fa1
+	if (s->compare == SEL_COMP_NOT)
306fa1
+		ret = !ret;
306fa1
+	if (ret)
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "matched selector %s(%s)",
306fa1
+		      s->sel->name, expand ? expand : s->func.arg1);
306fa1
+	else
306fa1
+		debug(ap->logopt, MODPREFIX
306fa1
+		      "did not match selector %s(%s)",
306fa1
+		      s->sel->name, expand ? expand : s->func.arg1);
306fa1
+
306fa1
+	if (expand)
306fa1
+		free(expand);
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
+static int sel_netgrp(struct autofs_point *ap,
306fa1
+		      struct selector *s, struct substvar *sv)
306fa1
+{
306fa1
+	char *exp_arg1 = NULL, *exp_arg2 = NULL;
306fa1
+	const struct substvar *v;
306fa1
+	int res, ret = 0;
306fa1
+	char *host;
306fa1
+
306fa1
+	if (s->func.arg2) {
306fa1
+		res = expand_selectors(ap, s->func.arg2, &exp_arg2, sv);
306fa1
+		if (res)
306fa1
+			host = exp_arg2;
306fa1
+		else
306fa1
+			host = s->func.arg2;
306fa1
+	} else {
306fa1
+		if (s->sel->selector == SEL_NETGRP)
306fa1
+			v = macro_findvar(sv, "host", 4);
306fa1
+		else
306fa1
+			v = macro_findvar(sv, "hostd", 5);
306fa1
+		if (!v || !*v->val) {
306fa1
+			error(ap->logopt, MODPREFIX
306fa1
+			     "failed to get value of ${host}");
306fa1
+			goto out;
306fa1
+		}
306fa1
+		host = v->val;
306fa1
+	}
306fa1
+
306fa1
+	res = expand_selectors(ap, s->func.arg1, &exp_arg1, sv);
306fa1
+	if (res)
306fa1
+		ret = innetgr(exp_arg1, host, NULL, NULL);
306fa1
+	else
306fa1
+		ret = innetgr(s->func.arg1, host, NULL, NULL);
306fa1
+
306fa1
+	if (s->compare == SEL_COMP_NOT)
306fa1
+		ret = !ret;
306fa1
+	if (ret) {
306fa1
+		if (!s->func.arg2)
306fa1
+			debug(ap->logopt, MODPREFIX
306fa1
+			      "matched selector %s(%s)",
306fa1
+			      s->sel->name, exp_arg1 ? exp_arg1 : s->func.arg1);
306fa1
+		else
306fa1
+			debug(ap->logopt, MODPREFIX
306fa1
+			      "matched selector %s(%s,%s)", s->sel->name,
306fa1
+			      exp_arg1 ? exp_arg1 : s->func.arg1,
306fa1
+			      exp_arg2 ? exp_arg2 : s->func.arg2);
306fa1
+	} else {
306fa1
+		if (!s->func.arg2)
306fa1
+			debug(ap->logopt, MODPREFIX
306fa1
+			      "did not match selector %s(%s)",
306fa1
+			      s->sel->name, exp_arg1 ? exp_arg1 : s->func.arg1);
306fa1
+		else
306fa1
+			debug(ap->logopt, MODPREFIX
306fa1
+			      "did not match selector %s(%s,%s)", s->sel->name,
306fa1
+			      exp_arg1 ? exp_arg1 : s->func.arg1,
306fa1
+			      exp_arg2 ? exp_arg2 : s->func.arg2);
306fa1
+	}
306fa1
+out:
306fa1
+	if (exp_arg1)
306fa1
+		free(exp_arg1);
306fa1
+	if (exp_arg2)
306fa1
+		free(exp_arg2);
306fa1
+
306fa1
+	return ret;
306fa1
+}
306fa1
+
306fa1
+static int eval_selector(struct autofs_point *ap,
306fa1
 			 struct amd_entry *this, struct substvar *sv)
306fa1
 {
306fa1
 	struct selector *s = this->selector;
306fa1
+	unsigned int logopt = ap->logopt;
306fa1
 	const struct substvar *v;
306fa1
 	unsigned int s_type;
306fa1
 	unsigned int v_type;
306fa1
-	struct stat st;
306fa1
-	char *host;
306fa1
 	int res, val, ret = 0;
306fa1
 
306fa1
 	s_type = s->sel->flags & SEL_FLAGS_TYPE_MASK;
306fa1
@@ -341,26 +515,7 @@ static int eval_selector(unsigned int lo
306fa1
 
306fa1
 		switch (v_type) {
306fa1
 		case SEL_FLAG_STR:
306fa1
-			res = strcmp(v->val, s->comp.value);
306fa1
-			if (s->compare & SEL_COMP_EQUAL && !res) {
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "matched selector %s(%s) == %s",
306fa1
-				      v->def, v->val, s->comp.value);
306fa1
-				ret = 1;
306fa1
-				break;
306fa1
-			} else if (s->compare & SEL_COMP_NOTEQUAL && res) {
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "matched selector %s(%s) != %s",
306fa1
-				      v->def, v->val, s->comp.value);
306fa1
-				ret = 1;
306fa1
-				break;
306fa1
-			}
306fa1
-
306fa1
-			debug(logopt, MODPREFIX
306fa1
-				      "did not match selector %s(%s) %s %s",
306fa1
-				      v->def, v->val,
306fa1
-				      (s->compare & SEL_COMP_EQUAL ? "==" : "!="),
306fa1
-				      s->comp.value);
306fa1
+			ret = sel_strcmp(ap, v, s, sv);
306fa1
 			break;
306fa1
 
306fa1
 		case SEL_FLAG_NUM:
306fa1
@@ -436,7 +591,7 @@ static int eval_selector(unsigned int lo
306fa1
 			break;
306fa1
 
306fa1
 		case SEL_XHOST:
306fa1
-			ret = match_my_name(logopt, s->func.arg1, sv);
306fa1
+			ret = match_my_name(ap, s->func.arg1, sv);
306fa1
 			if (s->compare == SEL_COMP_NOT)
306fa1
 				ret = !ret;
306fa1
 			if (ret)
306fa1
@@ -450,32 +605,11 @@ static int eval_selector(unsigned int lo
306fa1
 			break;
306fa1
 
306fa1
 		case SEL_EXISTS:
306fa1
-			/* Sould be OK to fail on any error here */
306fa1
-			ret = !lstat(s->func.arg1, &st);
306fa1
-			if (s->compare == SEL_COMP_NOT)
306fa1
-				ret = !ret;
306fa1
-			if (ret)
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "matched selector %s(%s)",
306fa1
-				      s->sel->name, s->func.arg1);
306fa1
-			else
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "did not match selector %s(%s)",
306fa1
-				      s->sel->name, s->func.arg1);
306fa1
+			ret = sel_lstat(ap, s, sv);
306fa1
 			break;
306fa1
 
306fa1
 		case SEL_IN_NETWORK:
306fa1
-			ret = in_network(s->func.arg1);
306fa1
-			if (s->compare == SEL_COMP_NOT)
306fa1
-				ret = !ret;
306fa1
-			if (ret)
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "matched selector %s(%s)",
306fa1
-				      s->sel->name, s->func.arg1);
306fa1
-			else
306fa1
-				debug(logopt, MODPREFIX
306fa1
-				      "did not match selector %s(%s)",
306fa1
-				      s->sel->name, s->func.arg1);
306fa1
+			ret = sel_in_network(ap, s, sv);
306fa1
 			break;
306fa1
 
306fa1
 		default:
306fa1
@@ -494,43 +628,7 @@ static int eval_selector(unsigned int lo
306fa1
 		switch (s->sel->selector) {
306fa1
 		case SEL_NETGRP:
306fa1
 		case SEL_NETGRPD:
306fa1
-			if (s->func.arg2)
306fa1
-				host = s->func.arg2;
306fa1
-			else {
306fa1
-				if (s->sel->selector == SEL_NETGRP)
306fa1
-					v = macro_findvar(sv, "host", 4);
306fa1
-				else
306fa1
-					v = macro_findvar(sv, "hostd", 5);
306fa1
-				if (!v || !*v->val) {
306fa1
-					error(logopt, MODPREFIX
306fa1
-					     "failed to get value of ${host}");
306fa1
-					break;
306fa1
-				}
306fa1
-				host = v->val;
306fa1
-			}
306fa1
-			ret = innetgr(s->func.arg1, host, NULL, NULL);
306fa1
-			if (s->compare == SEL_COMP_NOT)
306fa1
-				ret = !ret;
306fa1
-			if (ret) {
306fa1
-				if (!s->func.arg2)
306fa1
-					debug(logopt, MODPREFIX
306fa1
-					      "matched selector %s(%s)",
306fa1
-					      s->sel->name, s->func.arg1);
306fa1
-				else
306fa1
-					debug(logopt, MODPREFIX
306fa1
-					      "matched selector %s(%s,%s)",
306fa1
-					      s->sel->name, s->func.arg1,
306fa1
-					      s->func.arg2);
306fa1
-			} else {
306fa1
-				if (!s->func.arg2)
306fa1
-					debug(logopt, MODPREFIX
306fa1
-					      "did not match selector %s(%s)",
306fa1
-					      s->sel->name, s->func.arg1);
306fa1
-				else
306fa1
-					debug(logopt, MODPREFIX
306fa1
-					      "did not match selector %s(%s,%s)",
306fa1
-					      s->sel->name, s->func.arg1, s->func.arg2);
306fa1
-			}
306fa1
+			ret = sel_netgrp(ap, s, sv);
306fa1
 			break;
306fa1
 
306fa1
 		default:
306fa1
@@ -1737,7 +1835,7 @@ static void update_prefix(struct autofs_
306fa1
 	return;
306fa1
 }
306fa1
 
306fa1
-static int match_selectors(unsigned int logopt,
306fa1
+static int match_selectors(struct autofs_point *ap,
306fa1
 			   struct amd_entry *entry, struct substvar *sv)
306fa1
 {
306fa1
 	struct selector *s = entry->selector;
306fa1
@@ -1745,7 +1843,7 @@ static int match_selectors(unsigned int
306fa1
 
306fa1
 	/* No selectors, always match */
306fa1
 	if (!s) {
306fa1
-		debug(logopt, MODPREFIX "no selectors found in location");
306fa1
+		debug(ap->logopt, MODPREFIX "no selectors found in location");
306fa1
 		return 1;
306fa1
 	}
306fa1
 
306fa1
@@ -1753,7 +1851,7 @@ static int match_selectors(unsigned int
306fa1
 
306fa1
 	/* All selectors must match */
306fa1
 	while (s) {
306fa1
-		ret = eval_selector(logopt, entry, sv);
306fa1
+		ret = eval_selector(ap, entry, sv);
306fa1
 		if (!ret)
306fa1
 			break;
306fa1
 		s = s->next;
306fa1
@@ -1913,7 +2011,7 @@ static struct amd_entry *select_default_
306fa1
 		if (!this->selector)
306fa1
 			continue;
306fa1
 
306fa1
-		if (match_selectors(ap->logopt, this, sv)) {
306fa1
+		if (match_selectors(ap, this, sv)) {
306fa1
 			if (entry_default) {
306fa1
 				/*update_with_defaults(entry_default, this, sv);*/
306fa1
 				free_amd_entry(entry_default);
306fa1
@@ -2154,7 +2252,7 @@ int parse_mount(struct autofs_point *ap,
306fa1
 			break;
306fa1
 		}
306fa1
 
306fa1
-		if (!match_selectors(ap->logopt, this, sv))
306fa1
+		if (!match_selectors(ap, this, sv))
306fa1
 			continue;
306fa1
 
306fa1
 		at_least_one = 1;