[SERVICES]

- Add loading (not used yet) and unloading of user profiles.
- Create the service password secret name and pass it to LogonUserW.
Patch by Hermès BÉLUSCA - MAÏTO.

svn path=/trunk/; revision=73501
This commit is contained in:
Eric Kohl 2017-01-02 20:45:36 +00:00
parent d915c39751
commit ade0d2cda5
2 changed files with 64 additions and 15 deletions

View file

@ -192,12 +192,15 @@ ScmLogonService(
IN PSERVICE_IMAGE pImage) IN PSERVICE_IMAGE pImage)
{ {
#if 0 #if 0
PWSTR pUserName = NULL; PROFILEINFOW ProfileInfo;
PWSTR pDomainName = NULL; PWSTR pszUserName = NULL;
PWSTR pszDomainName = NULL;
PWSTR pszPassword = NULL;
PWSTR ptr; PWSTR ptr;
DWORD dwError = ERROR_SUCCESS; DWORD dwError = ERROR_SUCCESS;
#endif #endif
DPRINT("ScmLogonService()\n");
DPRINT("ScmLogonService(%p %p)\n", pService, pImage);
DPRINT("Service %S\n", pService->lpServiceName); DPRINT("Service %S\n", pService->lpServiceName);
@ -208,40 +211,77 @@ ScmLogonService(
return ERROR_SUCCESS; return ERROR_SUCCESS;
#if 0 #if 0
/* Get the user and domain names */
ptr = wcschr(pImage->pszAccountName, L'\\'); ptr = wcschr(pImage->pszAccountName, L'\\');
if (ptr != NULL) if (ptr != NULL)
{ {
*ptr = L'\0'; *ptr = L'\0';
pUserName = ptr + 1; pszUserName = ptr + 1;
pDomainName = pImage->pszAccountName; pszDomainName = pImage->pszAccountName;
} }
else else
{ {
pUserName = pImage->pszAccountName; pszUserName = pImage->pszAccountName;
pDomainName = NULL; pszDomainName = NULL;
} }
if (pDomainName == NULL || wcscmp(pDomainName, L".") == 0) /* Build the service 'password' */
pszPassword = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
(wcslen(pService->lpServiceName) + 5) * sizeof(WCHAR));
if (pszPassword == NULL)
{ {
// pDomainName = computer name dwError = ERROR_NOT_ENOUGH_MEMORY;
goto done;
} }
DPRINT("Domain: %S User: %S\n", pDomainName, pUserName); wcscpy(pszPassword, L"_SC_");
wcscat(pszPassword, pService->lpServiceName);
/* Logon the user */ DPRINT("Domain: %S User: %S Password: %S\n", pszDomainName, pszUserName, pszPassword);
// FIXME: Use the password!!
if (!LogonUserW(pUserName, /* Service logon */
pDomainName, if (!LogonUserW(pszUserName,
L"", // FIXME: lpszPassword, pszDomainName,
pszPassword,
LOGON32_LOGON_SERVICE, LOGON32_LOGON_SERVICE,
LOGON32_PROVIDER_DEFAULT, LOGON32_PROVIDER_DEFAULT,
&pImage->hToken)) &pImage->hToken))
{ {
dwError = GetLastError(); dwError = GetLastError();
DPRINT1("LogonUserW() failed (Error %lu)\n", dwError); DPRINT1("LogonUserW() failed (Error %lu)\n", dwError);
goto done;
} }
// FIXME: Call LoadUserProfileW to be able to initialize a per-user
// environment block, with user-specific environment variables as
// %USERNAME%, %USERPROFILE%, and %ALLUSERSPROFILE% correctly initialized!!
/* Load the user profile, so that the per-user environment variables can be initialized */
ZeroMemory(&ProfileInfo, sizeof(ProfileInfo));
ProfileInfo.dwSize = sizeof(ProfileInfo);
ProfileInfo.dwFlags = PI_NOUI;
ProfileInfo.lpUserName = pszUserName;
// ProfileInfo.lpProfilePath = NULL;
// ProfileInfo.lpDefaultPath = NULL;
// ProfileInfo.lpServerName = NULL;
// ProfileInfo.lpPolicyPath = NULL;
// ProfileInfo.hProfile = NULL;
if (!LoadUserProfileW(pImage->hToken, &ProfileInfo))
{
dwError = GetLastError();
DPRINT1("LoadUserProfileW() failed (Error %lu)\n", dwError);
goto done;
}
pImage->hProfile = ProfileInfo.hProfile;
done:
if (pszPassword != NULL)
HeapFree(GetProcessHeap(), 0, pszPassword);
if (ptr != NULL) if (ptr != NULL)
*ptr = L'\\'; *ptr = L'\\';
@ -348,6 +388,10 @@ ScmCreateOrReferenceServiceImage(PSERVICE pService)
{ {
DPRINT1("ScmCreateNewControlPipe() failed (Error %lu)\n", dwError); DPRINT1("ScmCreateNewControlPipe() failed (Error %lu)\n", dwError);
/* Unload the user profile */
if (pServiceImage->hProfile != NULL)
UnloadUserProfile(pServiceImage->hToken, pServiceImage->hProfile);
/* Close the logon token */ /* Close the logon token */
if (pServiceImage->hToken != NULL) if (pServiceImage->hToken != NULL)
CloseHandle(pServiceImage->hToken); CloseHandle(pServiceImage->hToken);
@ -421,6 +465,10 @@ ScmDereferenceServiceImage(PSERVICE_IMAGE pServiceImage)
if (pServiceImage->hControlPipe != INVALID_HANDLE_VALUE) if (pServiceImage->hControlPipe != INVALID_HANDLE_VALUE)
CloseHandle(pServiceImage->hControlPipe); CloseHandle(pServiceImage->hControlPipe);
/* Unload the user profile */
if (pServiceImage->hProfile != NULL)
UnloadUserProfile(pServiceImage->hToken, pServiceImage->hProfile);
/* Close the logon token */ /* Close the logon token */
if (pServiceImage->hToken != NULL) if (pServiceImage->hToken != NULL)
CloseHandle(pServiceImage->hToken); CloseHandle(pServiceImage->hToken);

View file

@ -51,6 +51,7 @@ typedef struct _SERVICE_IMAGE
HANDLE hProcess; HANDLE hProcess;
DWORD dwProcessId; DWORD dwProcessId;
HANDLE hToken; HANDLE hToken;
HANDLE hProfile;
} SERVICE_IMAGE, *PSERVICE_IMAGE; } SERVICE_IMAGE, *PSERVICE_IMAGE;