autofs-5.0.7 - add std vars to program map invocation

From: Ian Kent <ikent@redhat.com>

Program maps currently don't have the standard macro variables set
in the environemt when they run.

Also, program maps aren't necessarily executed within a shell so
setting the standard variables in the environment needs to be done
the same way it's done by the Sun map parser.
---
 CHANGELOG                |    1 +
 include/mounts.h         |    3 +++
 lib/mounts.c             |   51 ++++++++++++++++++++++++++++++++++++++++++++++
 modules/lookup_program.c |    2 ++
 modules/parse_sun.c      |   51 ----------------------------------------------
 5 files changed, 57 insertions(+), 51 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index ecd29e7..c920c9d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -80,6 +80,7 @@
 - add short host name standard marco variable.
 - fix get_nfs_info() probe.
 - fix portmap lookup.
+- add std vars to program map invocation.
 
 25/07/2012 autofs-5.0.7
 =======================
diff --git a/include/mounts.h b/include/mounts.h
index d1cfdc4..c2f92ec 100644
--- a/include/mounts.h
+++ b/include/mounts.h
@@ -85,6 +85,9 @@ unsigned int linux_version_code(void);
 int check_nfs_mount_version(struct nfs_mount_vers *, struct nfs_mount_vers *);
 extern unsigned int nfs_mount_uses_string_options;
 
+struct substvar *addstdenv(struct substvar *sv);
+struct substvar *removestdenv(struct substvar *sv);
+
 unsigned int query_kproto_ver(void);
 unsigned int get_kver_major(void);
 unsigned int get_kver_minor(void);
diff --git a/lib/mounts.c b/lib/mounts.c
index a6f560e..f666f29 100644
--- a/lib/mounts.c
+++ b/lib/mounts.c
@@ -303,6 +303,57 @@ int check_nfs_mount_version(struct nfs_mount_vers *vers,
 }
 #endif
 
+struct substvar *addstdenv(struct substvar *sv)
+{
+	struct substvar *list = sv;
+	struct thread_stdenv_vars *tsv;
+	char numbuf[16];
+
+	tsv = pthread_getspecific(key_thread_stdenv_vars);
+	if (tsv) {
+		const struct substvar *mv;
+		int ret;
+		long num;
+
+		num = (long) tsv->uid;
+		ret = sprintf(numbuf, "%ld", num);
+		if (ret > 0)
+			list = macro_addvar(list, "UID", 3, numbuf);
+		num = (long) tsv->gid;
+		ret = sprintf(numbuf, "%ld", num);
+		if (ret > 0)
+			list = macro_addvar(list, "GID", 3, numbuf);
+		list = macro_addvar(list, "USER", 4, tsv->user);
+		list = macro_addvar(list, "GROUP", 5, tsv->group);
+		list = macro_addvar(list, "HOME", 4, tsv->home);
+		mv = macro_findvar(list, "HOST", 4);
+		if (mv) {
+			char *shost = strdup(mv->val);
+			if (shost) {
+				char *dot = strchr(shost, '.');
+				if (dot)
+					*dot = '\0';
+				list = macro_addvar(list, "SHOST", 5, shost);
+				free(shost);
+			}
+		}
+	}
+	return list;
+}
+
+struct substvar *removestdenv(struct substvar *sv)
+{
+	struct substvar *list = sv;
+
+	list = macro_removevar(list, "UID", 3);
+	list = macro_removevar(list, "USER", 4);
+	list = macro_removevar(list, "HOME", 4);
+	list = macro_removevar(list, "GID", 3);
+	list = macro_removevar(list, "GROUP", 5);
+	list = macro_removevar(list, "SHOST", 5);
+	return list;
+}
+
 /*
  * Make common autofs mount options string
  */
diff --git a/modules/lookup_program.c b/modules/lookup_program.c
index 7e22b38..5b87b9c 100644
--- a/modules/lookup_program.c
+++ b/modules/lookup_program.c
@@ -271,6 +271,8 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 		 */
 		if (ctxt->mapfmt && strcmp(ctxt->mapfmt, "MAPFMT_DEFAULT")) {
 			struct parse_context *pctxt = (struct parse_context *) ctxt->parse->context;
+			/* Add standard environment as seen by sun map parser */
+			pctxt->subst = addstdenv(pctxt->subst);
 			macro_setenv(pctxt->subst);
 		}
 		execl(ctxt->mapname, ctxt->mapname, name, NULL);
diff --git a/modules/parse_sun.c b/modules/parse_sun.c
index 5ab451e..30820b5 100644
--- a/modules/parse_sun.c
+++ b/modules/parse_sun.c
@@ -99,57 +99,6 @@ static void kill_context(struct parse_context *ctxt)
 	free(ctxt);
 }
 
-static struct substvar *addstdenv(struct substvar *sv)
-{
-	struct substvar *list = sv;
-	struct thread_stdenv_vars *tsv;
-	char numbuf[16];
-
-	tsv = pthread_getspecific(key_thread_stdenv_vars);
-	if (tsv) {
-		struct substvar *mv;
-		int ret;
-		long num;
-
-		num = (long) tsv->uid;
-		ret = sprintf(numbuf, "%ld", num);
-		if (ret > 0)
-			list = macro_addvar(list, "UID", 3, numbuf);
-		num = (long) tsv->gid;
-		ret = sprintf(numbuf, "%ld", num);
-		if (ret > 0)
-			list = macro_addvar(list, "GID", 3, numbuf);
-		list = macro_addvar(list, "USER", 4, tsv->user);
-		list = macro_addvar(list, "GROUP", 5, tsv->group);
-		list = macro_addvar(list, "HOME", 4, tsv->home);
-		mv = macro_findvar(list, "HOST", 4);
-		if (mv) {
-			char *shost = strdup(mv->val);
-			if (shost) {
-				char *dot = strchr(shost, '.');
-				if (dot)
-					*dot = '\0';
-				list = macro_addvar(list, "SHOST", 5, shost);
-				free(shost);
-			}
-		}
-	}
-	return list;
-}
-
-static struct substvar *removestdenv(struct substvar *sv)
-{
-	struct substvar *list = sv;
-
-	list = macro_removevar(list, "UID", 3);
-	list = macro_removevar(list, "USER", 4);
-	list = macro_removevar(list, "HOME", 4);
-	list = macro_removevar(list, "GID", 3);
-	list = macro_removevar(list, "GROUP", 5);
-	list = macro_removevar(list, "SHOST", 5);
-	return list;
-}
-
 /* 
  * $- and &-expand a Sun-style map entry and return the length of the entry.
  * If "dst" is NULL, just count the length.