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

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