Skip to content

Commit d6d93e6

Browse files
committed
Set user environment variables in remote session
1 parent 8fa2958 commit d6d93e6

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

contrib/win32/win32compat/pwd.c

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ int GetDomainFromToken ( HANDLE *hAccessToken, UCHAR *domain, DWORD dwSize)
127127
* RETURNS: pointer to static string with homedir or NULL if fails.
128128
*/
129129

130+
#define SET_USER_ENV(folder_id, evn_variable) do { \
131+
if (SHGetKnownFolderPath(&folder_id,0,token,&path) == S_OK) \
132+
{ \
133+
SetEnvironmentVariableW(evn_variable, path); \
134+
CoTaskMemFree(path); \
135+
} \
136+
} while (0)
137+
130138
char *GetHomeDirFromToken(char *userName, HANDLE token)
131139
{
132140
UCHAR InfoBuffer[1000];
@@ -156,16 +164,62 @@ char *GetHomeDirFromToken(char *userName, HANDLE token)
156164
if (reg_key)
157165
RegCloseKey(reg_key);
158166

159-
/* TODO - populate APPDATA, LOCALADPPDATA, TEMP, etc */
160-
SetEnvironmentVariableW(L"LOCALAPPDATA", L"");
161-
SetEnvironmentVariableW(L"APPDATA", L"");
162-
SetEnvironmentVariableW(L"TEMP", L"");
163-
SetEnvironmentVariableW(L"TMP", L"");
164-
SetEnvironmentVariableW(L"USERDNSDOMAIN", L"");
165-
SetEnvironmentVariableW(L"USERDOMAIN", L"");
166-
SetEnvironmentVariableW(L"USERDOMAIN_ROAMINGPROFILE", L"");
167-
SetEnvironmentVariableW(L"USERPROFILE", L"");
168-
167+
{ /* retrieve and set env variables. */
168+
/* TODO - Get away with fixed limits and dynamically allocated required memory*/
169+
#define MAX_VALUE_LEN 1000
170+
#define MAX_DATA_LEN 2000
171+
#define MAX_EXPANDED_DATA_LEN 5000
172+
wchar_t *path;
173+
wchar_t value_name[MAX_VALUE_LEN];
174+
wchar_t value_data[MAX_DATA_LEN], value_data_expanded[MAX_EXPANDED_DATA_LEN], *to_apply;
175+
DWORD value_type, name_len, data_len;
176+
int i;
177+
LONG ret;
178+
179+
ImpersonateLoggedOnUser(token);
180+
SET_USER_ENV(FOLDERID_LocalAppData, L"LOCALAPPDATA");
181+
SET_USER_ENV(FOLDERID_Profile, L"USERPROFILE");
182+
SET_USER_ENV(FOLDERID_RoamingAppData, L"APPDATA");
183+
reg_key = 0;
184+
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Environment", 0, KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS) {
185+
i = 0;
186+
while (1) {
187+
name_len = MAX_VALUE_LEN * 2;
188+
data_len = MAX_DATA_LEN * 2;
189+
to_apply = NULL;
190+
if (RegEnumValueW(reg_key, i++, &value_name, &name_len, 0, &value_type, &value_data, &data_len) != ERROR_SUCCESS)
191+
break;
192+
if (value_type == REG_SZ)
193+
to_apply = value_data;
194+
else if (value_type == REG_EXPAND_SZ) {
195+
ExpandEnvironmentStringsW(value_data, value_data_expanded, MAX_EXPANDED_DATA_LEN);
196+
to_apply = value_data_expanded;
197+
}
198+
199+
if (wcsicmp(value_name, L"PATH") == 0) {
200+
DWORD size;
201+
if ((size = GetEnvironmentVariableW(L"PATH", NULL, 0)) != ERROR_ENVVAR_NOT_FOUND) {
202+
memcpy(value_data_expanded + size, to_apply, (wcslen(to_apply) + 1)*2);
203+
GetEnvironmentVariableW(L"PATH", value_data_expanded, MAX_EXPANDED_DATA_LEN);
204+
value_data_expanded[size-1] = L';';
205+
to_apply = value_data_expanded;
206+
}
207+
208+
}
209+
if (to_apply)
210+
SetEnvironmentVariableW(value_name, to_apply);
211+
212+
213+
}
214+
RegCloseKey(reg_key);
215+
}
216+
217+
218+
RevertToSelf();
219+
}
220+
221+
222+
169223
debug("<- GetHomeDirFromToken()...");
170224

171225
return pw_homedir;

contrib/win32/win32compat/ssh-agent/agent.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
#include "agent.h"
3232
#include <sddl.h>
33+
#include <UserEnv.h>
3334
#define BUFSIZE 5 * 1024
3435

3536
static HANDLE ioc_port = NULL;

0 commit comments

Comments
 (0)