Skip to content

Commit 946ed7d

Browse files
committed
Setting user's home path without loading profile
1 parent 4f87e35 commit 946ed7d

File tree

2 files changed

+40
-81
lines changed

2 files changed

+40
-81
lines changed

auth2-pubkey.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ extern u_int session_id2_len;
8686
#ifdef WIN32_FIXME
8787

8888
extern char HomeDirLsaW[MAX_PATH];
89-
wchar_t *GetHomeDir(char *userName);
9089

9190
#endif
9291

contrib/win32/win32compat/pwd.c

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <errno.h>
3939
#include <shlobj.h>
4040
#include <Userenv.h>
41+
#include <sddl.h>
4142

4243
#include "win32auth.h"
4344
#include "homedirhelp.h"
@@ -128,87 +129,46 @@ int GetDomainFromToken ( HANDLE *hAccessToken, UCHAR *domain, DWORD dwSize)
128129

129130
char *GetHomeDirFromToken(char *userName, HANDLE token)
130131
{
131-
UCHAR domain[200];
132-
wchar_t pw_buf[MAX_PATH] = { L'\0' };
133-
PWSTR tmp;
134-
135-
debug("-> GetHomeDirFromToken()...");
136-
137-
PROFILEINFO profileInfo;
138-
139-
// find the server name of the domain controller which created this token
140-
GetDomainFromToken ( &token, domain, sizeof(domain));
141-
//if (MultiByteToWideChar(CP_UTF8, 0, domain, -1, domainW, sizeof(domainW)) == 0)
142-
//{
143-
//debug("DomainServerName encoding conversion failure");
144-
//return NULL;
145-
//}
146-
147-
profileInfo.dwFlags = PI_NOUI;
148-
profileInfo.lpProfilePath = NULL;
149-
profileInfo.lpUserName = userName;
150-
profileInfo.lpDefaultPath = NULL;
151-
profileInfo.lpServerName = domain;
152-
profileInfo.lpPolicyPath = NULL;
153-
profileInfo.hProfile = NULL;
154-
profileInfo.dwSize = sizeof(profileInfo);
155-
156-
/*
157-
* And retrieve homedir from profile.
158-
*/
159-
160-
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_Documents, 0, token, &tmp)))
161-
{
162-
wcscpy_s(pw_homedir, MAX_PATH, tmp);
163-
CoTaskMemFree(tmp);
164-
} else
165-
{
166-
debug("SHGetKnownFolderPath on FOLDERID_Documents failed");
167-
GetWindowsDirectoryW(pw_homedir, MAX_PATH);
168-
}
169-
170-
// update APPDATA user's env variable
171-
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, token, &tmp)))
172-
{
173-
SetEnvironmentVariableW(L"APPDATA", tmp);
174-
CoTaskMemFree(tmp);
175-
}
176-
177-
// update LOCALAPPDATA user's env variable
178-
if (SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, token, &tmp)))
179-
{
180-
SetEnvironmentVariableW(L"LOCALAPPDATA", tmp);
181-
CoTaskMemFree(tmp);
182-
}
183-
184-
debug("<- GetHomeDirFromToken()...");
185-
186-
return pw_homedir;
187-
}
188-
189-
190-
wchar_t *GetHomeDir(char *userName)
191-
{
192-
/*
193-
* Get home directory path (if this fails, the user is invalid, bail)
194-
*/
195-
196-
wchar_t *homeDir = NULL;
197-
198-
homeDir = gethomedir_w(userName, NULL);
199-
200-
if (homeDir == NULL || homeDir[0] == L'\0')
201-
{
202-
return NULL;
203-
}
204-
205-
debug3("GetHomeDir: homedir [%ls]", homeDir);
206-
207-
wcsncpy(pw_homedir, homeDir, sizeof(pw_homedir));
132+
UCHAR InfoBuffer[1000];
133+
PTOKEN_USER pTokenUser = (PTOKEN_USER)InfoBuffer;
134+
DWORD dwInfoBufferSize, tmp_len;
135+
LPWSTR sid_str = NULL;
136+
wchar_t reg_path[MAX_PATH];
137+
HKEY reg_key = 0;
138+
139+
/* set home dir to Windows if any of below fair*/
140+
GetWindowsDirectoryW(pw_homedir, MAX_PATH);
141+
142+
tmp_len = MAX_PATH;
143+
if (GetTokenInformation(token, TokenUser, InfoBuffer,
144+
1000, &dwInfoBufferSize) == FALSE ||
145+
ConvertSidToStringSidW(pTokenUser->User.Sid, &sid_str) == FALSE ||
146+
swprintf(reg_path, MAX_PATH, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%ls", sid_str) == MAX_PATH ||
147+
RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &reg_key) != 0 ||
148+
RegQueryValueExW(reg_key, L"ProfileImagePath", 0, NULL, pw_homedir, &tmp_len) != 0 ){
149+
/* one of the above failed */
150+
debug("cannot retirve profile path - perhaps user profile is not created yet");
151+
}
208152

209-
free(homeDir);
210-
211-
return pw_homedir;
153+
if (sid_str)
154+
LocalFree(sid_str);
155+
156+
if (reg_key)
157+
RegCloseKey(reg_key);
158+
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+
169+
debug("<- GetHomeDirFromToken()...");
170+
171+
return pw_homedir;
212172
}
213173

214174
/*

0 commit comments

Comments
 (0)