Blame SOURCES/autofs-5.0.7-setup-program-map-env-from-macro-table.patch

304803
autofs-5.0.7 - setup program map env from macro table
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
The ability to pass parameters to program maps, in some way, is needed.
304803
Standard autofs specifies that program maps have one argument so passing
304803
parameters as arguments shouldn't be done.
304803
304803
This patch sets the existing macro table definitions (for both global and
304803
local table) as environment variables before calling the map. The values
304803
are not checked after return so, at this stage, program maps can't change
304803
macro definitions.
304803
---
304803
 CHANGELOG                |    1 +
304803
 include/macros.h         |    1 +
304803
 lib/macros.c             |   28 ++++++++++++++++++++++++++++
304803
 modules/lookup_program.c |   20 ++++++++++++++++++++
304803
 4 files changed, 50 insertions(+)
304803
304803
--- autofs-5.0.7.orig/CHANGELOG
304803
+++ autofs-5.0.7/CHANGELOG
304803
@@ -76,6 +76,7 @@
304803
 - fix options compare.
304803
 - fix fix options compare.
304803
 - fix max() declaration.
304803
+- setup program map env from macro table.
304803
 
304803
 25/07/2012 autofs-5.0.7
304803
 =======================
304803
--- autofs-5.0.7.orig/include/macros.h
304803
+++ autofs-5.0.7/include/macros.h
304803
@@ -40,5 +40,6 @@ void macro_free_global_table(void);
304803
 void macro_free_table(struct substvar *table);
304803
 const struct substvar *
304803
 macro_findvar(const struct substvar *table, const char *str, int len);
304803
+void macro_setenv(struct substvar *table);
304803
 
304803
 #endif
304803
--- autofs-5.0.7.orig/lib/macros.c
304803
+++ autofs-5.0.7/lib/macros.c
304803
@@ -421,3 +421,31 @@ macro_findvar(const struct substvar *tab
304803
 	return NULL;
304803
 }
304803
 
304803
+/* Set environment from macro variable table */
304803
+void macro_setenv(struct substvar *table)
304803
+{
304803
+	const struct substvar *sv = system_table;
304803
+	const struct substvar *lv = table;
304803
+
304803
+	/*
304803
+	 * First set environment from global table, matching local
304803
+	 * variables will overwrite these.
304803
+	 */
304803
+	while (sv) {
304803
+		if (sv->def)
304803
+			setenv(sv->def, sv->val, 1);
304803
+		sv = sv->next;
304803
+	}
304803
+
304803
+	error(LOGOPT_ANY, "table %p", table);
304803
+	dump_table(table);
304803
+
304803
+	/* Next set environment from the local table */
304803
+	while (lv) {
304803
+		if (lv->def)
304803
+			setenv(lv->def, lv->val, 1);
304803
+		lv = lv->next;
304803
+	}
304803
+
304803
+	return;
304803
+}
304803
--- autofs-5.0.7.orig/modules/lookup_program.c
304803
+++ autofs-5.0.7/modules/lookup_program.c
304803
@@ -36,9 +36,17 @@
304803
 
304803
 struct lookup_context {
304803
 	const char *mapname;
304803
+	char *mapfmt;
304803
 	struct parse_mod *parse;
304803
 };
304803
 
304803
+struct parse_context {
304803
+	char *optstr;		/* Mount options */
304803
+	char *macros;		/* Map wide macro defines */
304803
+	struct substvar *subst;	/* $-substitutions */
304803
+	int slashify_colons;	/* Change colons to slashes? */
304803
+};
304803
+
304803
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
304803
 
304803
 int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **context)
304803
@@ -79,6 +87,8 @@ int lookup_init(const char *mapfmt, int
304803
 	if (!mapfmt)
304803
 		mapfmt = MAPFMT_DEFAULT;
304803
 
304803
+	ctxt->mapfmt = strdup(mapfmt);
304803
+
304803
 	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
304803
 	if (!ctxt->parse) {
304803
 		logmsg(MODPREFIX "failed to open parse context");
304803
@@ -255,6 +265,14 @@ int lookup_mount(struct autofs_point *ap
304803
 			warn(ap->logopt,
304803
 			     MODPREFIX "failed to set PWD to %s for map %s",
304803
 			     ap->path, ctxt->mapname);
304803
+		/*
304803
+		 * MAPFMT_DEFAULT must be "sun" for ->parse_init() to have setup
304803
+		 * the macro table.
304803
+		 */
304803
+		if (ctxt->mapfmt && strcmp(ctxt->mapfmt, "MAPFMT_DEFAULT")) {
304803
+			struct parse_context *pctxt = (struct parse_context *) ctxt->parse->context;
304803
+			macro_setenv(pctxt->subst);
304803
+		}
304803
 		execl(ctxt->mapname, ctxt->mapname, name, NULL);
304803
 		_exit(255);	/* execl() failed */
304803
 	}
304803
@@ -448,6 +466,8 @@ int lookup_done(void *context)
304803
 {
304803
 	struct lookup_context *ctxt = (struct lookup_context *) context;
304803
 	int rv = close_parse(ctxt->parse);
304803
+	if (ctxt->mapfmt)
304803
+		free(ctxt->mapfmt);
304803
 	free(ctxt);
304803
 	return rv;
304803
 }