Classification: mature

This patch makes it possible to keep some environment variables when
calling mach-helper. It is used e.g. for general system variables like
$*_proxy, or for APT_CONFIG.


2004-02-11  Enrico Scholz  <enrico.scholz@informatik.tu-chemnitz.de>
	* initial release
--- mach/src/mach-helper.c~env	2004-01-20 16:54:40.000000000 +0100
+++ mach/src/mach-helper.c	2004-02-11 23:41:07.000000000 +0100
@@ -2,19 +2,29 @@
  * mach-helper.c: help mach perform tasks needing root privileges
  */
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <errno.h>
 #include <stdarg.h>
-
+#include <stdlib.h>
 
 /* pull in configure'd defines */
 char *rootsdir = ROOTSDIR;
 char *statesdir = STATESDIR;
 char *archivesdir = ARCHIVESDIR;
 
+static char const * const	ALLOWED_ENV[] = {
+  "APT_CONFIG", 
+  "dist",
+  "ftp_proxy", "http_proxy", "https_proxy", "no_proxy"
+};
+
+#define ALLOWED_ENV_SIZE	(sizeof(ALLOWED_ENV)/sizeof(ALLOWED_ENV[0]))
+
 /*
  * helper functions
  */
@@ -121,11 +131,13 @@
 do_command (const char *filename, char *const argv[])
 {
   /* do not trust user environment */
-  char *env[] = { "PATH=/bin:/usr/bin:/usr/sbin",
-                  "HOME=/root",
-                  NULL };
+  char *env[3 + ALLOWED_ENV_SIZE] = { [0]="PATH=/bin:/usr/bin:/usr/sbin",
+				      [1]="HOME=/root" };
   int retval;
   char **arg;
+  size_t	idx=2;
+  size_t	i;
+
 
   /* elevate privileges */
   setreuid (geteuid (), geteuid ());
@@ -138,6 +150,14 @@
     printf ("%s ", *arg);
   printf ("\n");
   */
+
+  for (i=0; i<ALLOWED_ENV_SIZE; ++i) {
+    char	*ptr = getenv(ALLOWED_ENV[i]);
+    if (ptr==0) continue;
+    ptr -= strlen(ALLOWED_ENV[i])+1;
+    env[idx++] = ptr;
+  }
+
   retval = execve (filename, argv, env);
   error ("executing %s: %s", filename, strerror (errno));
 }
