[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug/Problem ${HOME} variable not honored on WIndows


On Tuesday, January 15, 2019 9:41:38 AM CET Andreas Schneider wrote:
> On Tuesday, January 15, 2019 9:21:29 AM CET Duane Ellis wrote:
> > duane>  Effectively - this is described as:  C:\users\<USERNAME> - aka:
> > The
> > duane > windows Home Directory
> > 
> > Andreas> Which seems to be correct as profiles are normally synced when
> > logging in an out.
> > 
> > Hmm - In my specific case - and numerous coworkers, my experience at large
> > and small companies is this: Only a very few files are synced between the
> > network and my local machine.
> > 
> > For me - my "~/.ssh" directory is *NOT* synchronized, if it was - I would
> > not be here talking about it. And turning on SYNC often greatly destroys
> > unix-like "chmod" settings on windows machines.
> > 
> > Andreas> But this only happens in MinGW?
> > 
> > Bottom line - in my case:
> > 	Using "Git Bash" - aka:  https://git-scm.com/
> > 	Both Win 7 and Win 10
> > 
> > I'm not totally sure the difference between "git bash" which I get from
> > here: "https://git-scm.com"; What I know as "MingGW"  = it seems to be very
> > much so identical to "git-bash" but there are little "got-yas" Grrr...
> > 
> > That said, if I set the variable "${HOME}" I expect it to be used. If not
> > that, then at least something like  "${SSH_CONFIG_DIR}" would be an
> > alternate
> > 
> > Others with similar problems:
> > 
> > https://stackoverflow.com/questions/2840871/ssh-is-looking-in-the-wrong-pl
> > ac e-for-the-public-private-key-pair-on-windows
> > 
> > See the *LAST* entry - the poster refers to "Z:\" as his network domain
> > policy That's my example
> 
> Ok, so git-bash is setting the profile path correctly. However why does the
> Windows function not return that path if it is set in the network and what
> is the correct way to do it.
> 
> Currently this sounds like a Windows bug we need to work around here ...
> 
> My guess would be that %HOMEDRIVE% and %HOMEPATH% are set and used.

Could you test the attached patch?


Thanks

-- 
Andreas Schneider                 asn@xxxxxxxxxxxxxx
GPG-ID:     8DFF53E18F2ABC8D8F3C92237EE0FC4DCC014E3D
From 44cb4fe59dc814a837df7e9493e587b1c63d320e Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@xxxxxxxxxxxxxx>
Date: Mon, 14 Jan 2019 11:51:20 +0100
Subject: [PATCH] misc: Improve home dir detection on Windows

Signed-off-by: Andreas Schneider <asn@xxxxxxxxxxxxxx>
---
 src/misc.c | 100 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 74 insertions(+), 26 deletions(-)

diff --git a/src/misc.c b/src/misc.c
index 782b7bc0..e8bf6def 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -24,17 +24,6 @@
 
 #include "config.h"
 
-#ifndef _WIN32
-/* This is needed for a standard getpwuid_r on opensolaris */
-#define _POSIX_PTHREAD_SEMANTICS
-#include <pwd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#endif /* _WIN32 */
-
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
@@ -51,9 +40,10 @@
 
 #ifdef _WIN32
 
-#ifndef _WIN32_IE
-# define _WIN32_IE 0x0501 // SHGetSpecialFolderPath
-#endif
+#undef WINVER
+#define WINVER _WIN32_WINNT_VISTA
+#undef _WIN32_WINNT
+#define _WIN32_WINNT _WIN32_WINNT_VISTA
 
 #include <winsock2.h> // Must be the first to include
 #include <ws2tcpip.h>
@@ -64,6 +54,16 @@
 #include <io.h>
 #endif /* HAVE_IO_H */
 
+#else /* NOT WIN32 */
+
+/* This is needed for a standard getpwuid_r on opensolaris */
+#define _POSIX_PTHREAD_SEMANTICS
+#include <pwd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
 #endif /* _WIN32 */
 
 #include "libssh/priv.h"
@@ -104,21 +104,69 @@
  */
 
 #ifdef _WIN32
-char *ssh_get_user_home_dir(void) {
-  char tmp[MAX_PATH] = {0};
-  char *szPath = NULL;
-
-  if (SHGetSpecialFolderPathA(NULL, tmp, CSIDL_PROFILE, TRUE)) {
-    szPath = malloc(strlen(tmp) + 1);
-    if (szPath == NULL) {
-      return NULL;
+char *ssh_get_user_home_dir(void)
+{
+    size_t home_len = 0;
+    size_t homedrive_len = 0;
+    size_t userprofile_len = 0;
+    char home[MAX_PATH] = {0};
+    char *userprofile = NULL;
+    HRESULT res;
+    int len;
+
+    len = GetEnvironmentVariable("HOME", home, sizeof(home));
+    if (len > 0 && len != sizeof(home)) {
+        if (strlen(home) == 0) {
+            goto homedrive;
+        }
+
+        return strdup(home);
     }
 
-    strcpy(szPath, tmp);
-    return szPath;
-  }
+homedrive:
+    len = GetEnvironmentVariable("HOMEDRIVE", home, sizeof(home));
+    if (len > 0 && len < 4) {
+        char homepath[MAX_PATH] = {0};
+        size_t userprofile_len = 0;
 
-  return NULL;
+        len = GetEnvironmentVariable("HOMEPATH", home, sizeof(home));
+        if (len == 0 || len >= MAX_PATH) {
+            goto userprofile;
+        }
+
+        userprofile_len = strlen(home) + strlen(homepath) + 1;
+        userprofile = malloc(userprofile_len * sizeof(char));
+        if (userprofile == NULL) {
+            return NULL;
+        }
+
+        len = snprintf(userprofile, userprofile_len, "%s%s", home, homepath);
+        if (len < 0) {
+            free(userprofile);
+            return NULL;
+        }
+
+        return userprofile;
+    }
+
+userprofile:
+    res = SHGetKnownFolderPath(&FOLDERID_Profile,
+                               0,
+                               NULL, /* current user */
+                               (PWSTR *)home);
+    if (res != S_OK) {
+        return NULL;
+    }
+
+    userprofile_len = strlen(home) + 1;
+    userprofile = malloc(userprofile_len * sizeof(char));
+    if (userprofile == NULL) {
+        return NULL;
+    }
+
+    snprintf(userprofile, userprofile_len, "%s", home);
+
+    return userprofile;
 }
 
 /* we have read access on file */
-- 
2.20.1


Follow-Ups:
Re: Bug/Problem ${HOME} variable not honored on WIndowsDuane Ellis <duane@xxxxxxxxxxxxxx>
References:
Bug/Problem ${HOME} variable not honored on WIndowsDuane Ellis <duane@xxxxxxxxxxxxxx>
RE: Bug/Problem ${HOME} variable not honored on WIndowsDuane Ellis <duane@xxxxxxxxxxxxxx>
Re: Bug/Problem ${HOME} variable not honored on WIndowsAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org