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

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