It is expected that %SystemRoot% is always defined to the directory of the
Windows system.  If this is undefined, Windows do not work properly.

So instead of hard-coding C:\WINDOWS\Temp as the default temporary directory
if %TEMP% or %TMP% is not found, use %SystemRoot%\Temp instead as the fallback.

Signed-off-by: David Sommerseth <dav...@redhat.com>
---
 win/config.h.in |    3 ---
 win32.c         |   36 ++++++++++++++++++++++++++----------
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/win/config.h.in b/win/config.h.in
index abf71d2..fb349d0 100644
--- a/win/config.h.in
+++ b/win/config.h.in
@@ -286,9 +286,6 @@ typedef unsigned long in_addr_t;
 /* Route command */
 #define ROUTE_PATH "route"

-/* Default temporary directory, if not found via getenv() */
-#define DEFAULT_TMPDIR "C:\\WINDOWS\\Temp"   /* FIXME: Should be configurable 
*/
-
 #ifdef _MSC_VER
 /* MSVC++ hacks */
 #pragma warning(disable:4244) // conversion from 'foo' to 'bar', possible loss 
of data
diff --git a/win32.c b/win32.c
index b65f987..29653da 100644
--- a/win32.c
+++ b/win32.c
@@ -1097,19 +1097,35 @@ env_set_add_win32 (struct env_set *es)
 const char *
 win_get_tempdir()
 {
-  static char *tmpdir = NULL;
+  static char tmpdir[258];
+  char *envptr = NULL;

-  /* Try to use %TEMP% or %TMP% */
-  tmpdir = getenv("TEMP");
-  if( !tmpdir ) {
-    tmpdir = getenv("TMP");
+  CLEAR (tmpdir);
+
+  /* Try to use %TEMP%, %TMP% or %SystemRoot%\Temp */
+  envptr = getenv("TEMP");
+  if( envptr ) {
+    return envptr;
   }
-  if( !tmpdir ) {
-    /* Warn if we're using a hard coded path */
-    msg (M_WARN, "Could not find %TEMP% or %TMP% environment variables.  "
-         "Falling back to %s.  Consider to use --tmp-dir", DEFAULT_TMPDIR);
-    tmpdir = DEFAULT_TMPDIR;
+
+  envptr = getenv("TMP");
+  if( envptr ) {
+    return envptr;
   }
+
+  envptr = getenv(SYS_PATH_ENV_VAR_NAME);
+  if( !envptr ) {
+    /* This indicates something is really wrong with the Windows
+     * environment, and we shouldn't try to start OpenVPN in this case
+     */
+    msg (M_FATAL, "Could not find %%%s%% environment variable",
+         SYS_PATH_ENV_VAR_NAME);
+    return NULL; /* Just in case M_FATAL doesn't exit() */
+  }
+
+  openvpn_snprintf(tmpdir, 256, "%.249s\\Temp", envptr);
+  msg (M_WARN, "Could not find %%TEMP%% or %%TMP%% environment variables.  "
+       "Falling back to %s.  Consider to use --tmp-dir", tmpdir);
   return tmpdir;
 }
 #endif
-- 
1.7.4.2


Reply via email to