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

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