Blame SOURCES/autofs-5.0.9-amd-lookup-add-lookup-vars.patch

306fa1
autofs-5.0.9 - amd lookup add lookup vars
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
306fa1
---
306fa1
 modules/parse_amd.c |  157 ++++++++++++++++++++++++++++++++++++++++++++++-----
306fa1
 1 file changed, 141 insertions(+), 16 deletions(-)
306fa1
306fa1
diff --git a/modules/parse_amd.c b/modules/parse_amd.c
306fa1
index 295a10f..35cc5dc 100644
306fa1
--- a/modules/parse_amd.c
306fa1
+++ b/modules/parse_amd.c
306fa1
@@ -129,6 +129,100 @@ int parse_init(int argc, const char *const *argv, void **context)
306fa1
 	return 0;
306fa1
 }
306fa1
 
306fa1
+static struct substvar *add_lookup_vars(struct autofs_point *ap,
306fa1
+					const char *key, int key_len,
306fa1
+					struct map_source *source,
306fa1
+					struct substvar *sv)
306fa1
+{
306fa1
+	struct substvar *list = sv;
306fa1
+	struct thread_stdenv_vars *tsv;
306fa1
+	char lkp_key[PATH_MAX + 1];
306fa1
+	char path[PATH_MAX + 1];
306fa1
+	struct mapent *me;
306fa1
+	int len;
306fa1
+
306fa1
+	len = strlen(ap->path) + 1 + key_len + 1;
306fa1
+	if (len > PATH_MAX) {
306fa1
+		error(ap->logopt, MODPREFIX
306fa1
+		      "error: lookup key is greater than PATH_MAX");
306fa1
+		return NULL;
306fa1
+	}
306fa1
+
306fa1
+	if (ap->pref) {
306fa1
+		if (snprintf(lkp_key, sizeof(lkp_key), "%s%s",
306fa1
+			     ap->pref, key) >= sizeof(lkp_key)) {
306fa1
+			error(ap->logopt, MODPREFIX "key too long");
306fa1
+			return NULL;
306fa1
+		}
306fa1
+	} else {
306fa1
+		if (snprintf(lkp_key, sizeof(lkp_key), "%s",
306fa1
+			     key) >= sizeof(lkp_key)) {
306fa1
+			error(ap->logopt, MODPREFIX "key too long");
306fa1
+			return NULL;
306fa1
+		}
306fa1
+	}
306fa1
+
306fa1
+	if (*key == '/')
306fa1
+		strcpy(path, key);
306fa1
+	else {
306fa1
+		strcpy(path, ap->path);
306fa1
+		strcat(path, "/");
306fa1
+		strcat(path, key);
306fa1
+	}
306fa1
+	list = macro_addvar(list, "path", 4, path);
306fa1
+
306fa1
+	me = cache_lookup_distinct(source->mc, lkp_key);
306fa1
+	if (me)
306fa1
+		list = macro_addvar(list, "key", 3, me->key);
306fa1
+
306fa1
+	while (!me) {
306fa1
+		char match[PATH_MAX + 1];
306fa1
+		char *prefix;
306fa1
+
306fa1
+		strcpy(match, lkp_key);
306fa1
+		while ((prefix = strrchr(match, '/'))) {
306fa1
+			*prefix = '\0';
306fa1
+			me = cache_partial_match_wild(source->mc, match);
306fa1
+			if (me) {
306fa1
+				list = macro_addvar(list, "key", 3, lkp_key);
306fa1
+				break;
306fa1
+			}
306fa1
+		}
306fa1
+
306fa1
+		if (!me) {
306fa1
+			me = cache_lookup_distinct(source->mc, "*");
306fa1
+			if (me)
306fa1
+				list = macro_addvar(list, "key", 3, lkp_key);
306fa1
+		}
306fa1
+
306fa1
+		break;
306fa1
+	}
306fa1
+
306fa1
+	if (source->argv[0][0])
306fa1
+		list = macro_addvar(list, "map", 3, source->argv[0]);
306fa1
+
306fa1
+	tsv = pthread_getspecific(key_thread_stdenv_vars);
306fa1
+	if (tsv) {
306fa1
+		char numbuf[16];
306fa1
+		long num;
306fa1
+		int ret;
306fa1
+
306fa1
+		num = (long) tsv->uid;
306fa1
+		ret = sprintf(numbuf, "%ld", num);
306fa1
+		if (ret > 0)
306fa1
+			list = macro_addvar(list, "uid", 3, numbuf);
306fa1
+		num = (long) tsv->gid;
306fa1
+		ret = sprintf(numbuf, "%ld", num);
306fa1
+		if (ret > 0)
306fa1
+			list = macro_addvar(list, "gid", 3, numbuf);
306fa1
+	}
306fa1
+
306fa1
+	list = macro_addvar(list, "fs", 2, "${autodir}/${rhost}${rfs}");
306fa1
+	list = macro_addvar(list, "rfs", 3, path);
306fa1
+
306fa1
+	return list;
306fa1
+}
306fa1
+
306fa1
 static void update_with_defaults(struct amd_entry *defaults,
306fa1
 				 struct amd_entry *entry,
306fa1
 				 struct substvar *sv)
306fa1
@@ -167,22 +261,40 @@ static void update_with_defaults(struct amd_entry *defaults,
306fa1
 			entry->pref = tmp;
306fa1
 	}
306fa1
 
306fa1
-	if (!entry->fs && defaults->fs) {
306fa1
-		tmp = strdup(defaults->fs);
306fa1
-		if (tmp)
306fa1
-			entry->fs = tmp;
306fa1
+	if (!entry->fs) {
306fa1
+		if (defaults->fs) {
306fa1
+			tmp = strdup(defaults->fs);
306fa1
+			if (tmp)
306fa1
+				entry->fs = tmp;
306fa1
+		} else {
306fa1
+			v = macro_findvar(sv, "fs", 2);
306fa1
+			if (v)
306fa1
+				entry->fs = strdup(v->val);
306fa1
+		}
306fa1
 	}
306fa1
 
306fa1
-	if (!entry->rfs && defaults->rfs) {
306fa1
-		tmp = strdup(defaults->rfs);
306fa1
-		if (tmp)
306fa1
-			entry->rfs = tmp;
306fa1
+	if (!entry->rfs) {
306fa1
+		if (defaults->rfs) {
306fa1
+			tmp = strdup(defaults->rfs);
306fa1
+			if (tmp)
306fa1
+				entry->rfs = tmp;
306fa1
+		} else {
306fa1
+			v = macro_findvar(sv, "rfs", 3);
306fa1
+			if (v)
306fa1
+				entry->rfs = strdup(v->val);
306fa1
+		}
306fa1
 	}
306fa1
 
306fa1
-	if (!entry->rhost && defaults->rhost) {
306fa1
-		tmp = strdup(defaults->rhost);
306fa1
-		if (tmp)
306fa1
-			entry->rhost = tmp;
306fa1
+	if (!entry->rhost) {
306fa1
+		if (defaults->rhost) {
306fa1
+			tmp = strdup(defaults->rhost);
306fa1
+			if (tmp)
306fa1
+				entry->rhost = tmp;
306fa1
+		} else {
306fa1
+			v = macro_findvar(sv, "host", 4);
306fa1
+			if (v)
306fa1
+				entry->rhost = strdup(v->val);
306fa1
+		}
306fa1
 	}
306fa1
 
306fa1
 	if (!entry->opts && defaults->opts) {
306fa1
@@ -197,10 +309,16 @@ static void update_with_defaults(struct amd_entry *defaults,
306fa1
 			entry->addopts = tmp;
306fa1
 	}
306fa1
 
306fa1
-	if (!entry->remopts && defaults->remopts) {
306fa1
-		tmp = merge_options(defaults->remopts, entry->remopts);
306fa1
-		if (tmp)
306fa1
-			entry->remopts = tmp;
306fa1
+	if (!entry->remopts) {
306fa1
+		if (defaults->remopts) {
306fa1
+			tmp = strdup(defaults->remopts);
306fa1
+			if (tmp)
306fa1
+				entry->remopts = tmp;
306fa1
+		} else {
306fa1
+			v = macro_findvar(sv, "remopts", 7);
306fa1
+			if (v)
306fa1
+				entry->remopts = strdup(v->val);
306fa1
+		}
306fa1
 	}
306fa1
 
306fa1
 	return;
306fa1
@@ -984,6 +1102,13 @@ int parse_mount(struct autofs_point *ap, const char *name,
306fa1
 
306fa1
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
306fa1
 
306fa1
+	sv = add_lookup_vars(ap, name, name_len, source, sv);
306fa1
+	if (!sv) {
306fa1
+		macro_free_table(sv);
306fa1
+		pthread_setcancelstate(cur_state, NULL);
306fa1
+		return 1;
306fa1
+	}
306fa1
+
306fa1
 	len = expand_selectors(ap, mapent, &pmapent, sv);
306fa1
 	if (!len) {
306fa1
 		macro_free_table(sv);