Skip to content

Commit ad128b5

Browse files
author
manojampalam
committed
Added user environment variables to session
1 parent 46a6cc9 commit ad128b5

File tree

2 files changed

+152
-60
lines changed

2 files changed

+152
-60
lines changed

contrib/win32/win32compat/pwd.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,6 @@ w32_getpwuid(uid_t uid) {
184184
return ret;
185185
}
186186

187-
#define SET_USER_ENV(folder_id, evn_variable) do { \
188-
if (SHGetKnownFolderPath(&folder_id,0,token,&path) == S_OK) \
189-
{ \
190-
SetEnvironmentVariableW(evn_variable, path); \
191-
CoTaskMemFree(path); \
192-
} \
193-
} while (0)
194-
195187

196188
/* TODO - this is moved from realpath.c in openbsdcompat. Review and finalize its position*/
197189

session.c

Lines changed: 152 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,157 @@ do_authenticated1(Authctxt *authctxt)
490490

491491
#ifdef WINDOWS
492492

493+
#define SET_USER_ENV(folder_id, evn_variable) do { \
494+
if (SHGetKnownFolderPath(&folder_id,0,token,&path) == S_OK) \
495+
{ \
496+
SetEnvironmentVariableW(evn_variable, path); \
497+
CoTaskMemFree(path); \
498+
} \
499+
} while (0)
500+
501+
void setup_session_vars(Session* s)
502+
{
503+
wchar_t* pw_dir_w;
504+
wchar_t* tmp;
505+
char buf[128];
506+
char* laddr;
507+
508+
if ((pw_dir_w = utf8_to_utf16(s->pw->pw_dir)) == NULL)
509+
fatal("%s: out of memory");
510+
511+
512+
513+
if ((tmp = utf8_to_utf16(s->pw->pw_name)) == NULL)
514+
fatal("%s, out of memory");
515+
SetEnvironmentVariableW(L"USERNAME", tmp);
516+
free(tmp);
517+
518+
if (s->display)
519+
SetEnvironmentVariableA("DISPLAY", s->display);
520+
521+
522+
SetEnvironmentVariableW(L"HOMEPATH", pw_dir_w);
523+
SetEnvironmentVariableW(L"USERPROFILE", pw_dir_w);
524+
525+
if (pw_dir_w[1] == L':') {
526+
wchar_t wc = pw_dir_w[2];
527+
pw_dir_w[2] = L'\0';
528+
SetEnvironmentVariableW(L"HOMEDRIVE", pw_dir_w);
529+
pw_dir_w[2] = wc;
530+
}
531+
532+
snprintf(buf, sizeof buf, "%.50s %d %d",
533+
get_remote_ipaddr(), get_remote_port(), get_local_port());
534+
535+
SetEnvironmentVariableA("SSH_CLIENT", buf);
536+
537+
laddr = get_local_ipaddr(packet_get_connection_in());
538+
539+
snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
540+
get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
541+
542+
free(laddr);
543+
544+
SetEnvironmentVariableA("SSH_CONNECTION", buf);
545+
546+
if (original_command)
547+
SetEnvironmentVariableA("SSH_ORIGINAL_COMMAND", original_command);
548+
549+
550+
if ((s->term) && (s->term[0]))
551+
SetEnvironmentVariable("TERM", s->term);
552+
553+
if (!s->is_subsystem) {
554+
snprintf(buf, sizeof buf, "%s@%s $P$G", s->pw->pw_name, getenv("COMPUTERNAME"));
555+
SetEnvironmentVariableA("PROMPT", buf);
556+
}
557+
558+
/*set user environment variables*/
559+
{
560+
UCHAR InfoBuffer[1000];
561+
PTOKEN_USER pTokenUser = (PTOKEN_USER)InfoBuffer;
562+
DWORD dwInfoBufferSize, tmp_len;
563+
LPWSTR sid_str = NULL;
564+
wchar_t reg_path[MAX_PATH];
565+
HKEY reg_key = 0;
566+
HANDLE token = s->authctxt->methoddata;
567+
568+
tmp_len = MAX_PATH;
569+
if (GetTokenInformation(token, TokenUser, InfoBuffer,
570+
1000, &dwInfoBufferSize) == FALSE ||
571+
ConvertSidToStringSidW(pTokenUser->User.Sid, &sid_str) == FALSE ||
572+
swprintf(reg_path, MAX_PATH, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%ls", sid_str) == MAX_PATH ||
573+
RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &reg_key) != 0 ||
574+
RegQueryValueExW(reg_key, L"ProfileImagePath", 0, NULL, pw_dir_w, &tmp_len) != 0) {
575+
/* one of the above failed */
576+
debug("cannot retirve profile path - perhaps user profile is not created yet");
577+
}
578+
579+
if (sid_str)
580+
LocalFree(sid_str);
581+
582+
if (reg_key)
583+
RegCloseKey(reg_key);
584+
585+
{ /* retrieve and set env variables. */
586+
/* TODO - Get away with fixed limits and dynamically allocate required memory, cleanup this logic*/
587+
#define MAX_VALUE_LEN 1000
588+
#define MAX_DATA_LEN 2000
589+
#define MAX_EXPANDED_DATA_LEN 5000
590+
wchar_t *path;
591+
wchar_t value_name[MAX_VALUE_LEN];
592+
wchar_t value_data[MAX_DATA_LEN], value_data_expanded[MAX_EXPANDED_DATA_LEN], *to_apply;
593+
DWORD value_type, name_len, data_len;
594+
int i;
595+
LONG ret;
596+
597+
if (ImpersonateLoggedOnUser(token) == FALSE)
598+
debug("Failed to impersonate user token, %d", GetLastError());
599+
SET_USER_ENV(FOLDERID_LocalAppData, L"LOCALAPPDATA");
600+
SET_USER_ENV(FOLDERID_Profile, L"USERPROFILE");
601+
SET_USER_ENV(FOLDERID_RoamingAppData, L"APPDATA");
602+
reg_key = 0;
603+
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Environment", 0, KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS) {
604+
i = 0;
605+
while (1) {
606+
name_len = MAX_VALUE_LEN * 2;
607+
data_len = MAX_DATA_LEN * 2;
608+
to_apply = NULL;
609+
if (RegEnumValueW(reg_key, i++, &value_name, &name_len, 0, &value_type, &value_data, &data_len) != ERROR_SUCCESS)
610+
break;
611+
if (value_type == REG_SZ)
612+
to_apply = value_data;
613+
else if (value_type == REG_EXPAND_SZ) {
614+
ExpandEnvironmentStringsW(value_data, value_data_expanded, MAX_EXPANDED_DATA_LEN);
615+
to_apply = value_data_expanded;
616+
}
617+
618+
if (wcsicmp(value_name, L"PATH") == 0) {
619+
DWORD size;
620+
if ((size = GetEnvironmentVariableW(L"PATH", NULL, 0)) != ERROR_ENVVAR_NOT_FOUND) {
621+
memcpy(value_data_expanded + size, to_apply, (wcslen(to_apply) + 1) * 2);
622+
GetEnvironmentVariableW(L"PATH", value_data_expanded, MAX_EXPANDED_DATA_LEN);
623+
value_data_expanded[size - 1] = L';';
624+
to_apply = value_data_expanded;
625+
}
626+
627+
}
628+
if (to_apply)
629+
SetEnvironmentVariableW(value_name, to_apply);
630+
631+
632+
}
633+
RegCloseKey(reg_key);
634+
}
635+
636+
637+
RevertToSelf();
638+
}
639+
}
640+
641+
free(pw_dir_w);
642+
}
643+
493644
int do_exec_windows(Session *s, const char *command, int pty) {
494645
int pipein[2], pipeout[2], pipeerr[2], r;
495646
char *exec_command = NULL, *progdir = w32_programdir();
@@ -556,58 +707,7 @@ int do_exec_windows(Session *s, const char *command, int pty) {
556707
}
557708

558709
/* setup Environment varibles */
559-
{
560-
wchar_t* tmp;
561-
char buf[128];
562-
char* laddr;
563-
564-
if ((tmp = utf8_to_utf16(s->pw->pw_name)) == NULL)
565-
fatal("%s, out of memory");
566-
SetEnvironmentVariableW(L"USERNAME", tmp);
567-
free(tmp);
568-
569-
if (s->display)
570-
SetEnvironmentVariableA("DISPLAY", s->display);
571-
572-
573-
//_wchdir(pw_dir_w);
574-
575-
SetEnvironmentVariableW(L"HOMEPATH", pw_dir_w);
576-
SetEnvironmentVariableW(L"USERPROFILE", pw_dir_w);
577-
578-
if (pw_dir_w[1] == L':') {
579-
wchar_t wc = pw_dir_w[2];
580-
pw_dir_w[2] = L'\0';
581-
SetEnvironmentVariableW(L"HOMEDRIVE", pw_dir_w);
582-
pw_dir_w[2] = wc;
583-
}
584-
585-
snprintf(buf, sizeof buf, "%.50s %d %d",
586-
get_remote_ipaddr(), get_remote_port(), get_local_port());
587-
588-
SetEnvironmentVariableA("SSH_CLIENT", buf);
589-
590-
laddr = get_local_ipaddr(packet_get_connection_in());
591-
592-
snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
593-
get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
594-
595-
free(laddr);
596-
597-
SetEnvironmentVariableA("SSH_CONNECTION", buf);
598-
599-
if (original_command)
600-
SetEnvironmentVariableA("SSH_ORIGINAL_COMMAND", original_command);
601-
602-
603-
if ((s->term) && (s->term[0]))
604-
SetEnvironmentVariable("TERM", s->term);
605-
606-
if (!s->is_subsystem) {
607-
snprintf(buf, sizeof buf, "%s@%s $P$G", s->pw->pw_name, getenv("COMPUTERNAME"));
608-
SetEnvironmentVariableA("PROMPT", buf);
609-
}
610-
}
710+
setup_session_vars(s);
611711

612712
extern int debug_flag;
613713

0 commit comments

Comments
 (0)