* PURPOSE: Service control manager functions
* PROGRAMMER: Emanuele Aliberti
* Eric Kohl
+ * Pierre Schweitzer
*/
/* INCLUDES ******************************************************************/
#include <advapi32.h>
WINE_DEFAULT_DEBUG_CHANNEL(advapi);
+NTSTATUS
+WINAPI
+SystemFunction004(
+ const struct ustring *in,
+ const struct ustring *key,
+ struct ustring *out);
+
+NTSTATUS
+WINAPI
+SystemFunction028(
+ IN PVOID ContextHandle,
+ OUT LPBYTE SessionKey);
/* FUNCTIONS *****************************************************************/
}
+static
+DWORD
+ScmEncryptPassword(
+ _In_ PVOID ContextHandle,
+ _In_ PCWSTR pClearTextPassword,
+ _Out_ PBYTE *pEncryptedPassword,
+ _Out_ PDWORD pEncryptedPasswordSize)
+{
+ struct ustring inData, keyData, outData;
+ BYTE SessionKey[16];
+ PBYTE pBuffer;
+ NTSTATUS Status;
+
+ /* Get the session key */
+ Status = SystemFunction028(ContextHandle,
+ SessionKey);
+ if (!NT_SUCCESS(Status))
+ {
+ ERR("SystemFunction028 failed (Status 0x%08lx)\n", Status);
+ return RtlNtStatusToDosError(Status);
+ }
+
+ inData.Length = (wcslen(pClearTextPassword) + 1) * sizeof(WCHAR);
+ inData.MaximumLength = inData.Length;
+ inData.Buffer = (unsigned char *)pClearTextPassword;
+
+ keyData.Length = sizeof(SessionKey);
+ keyData.MaximumLength = keyData.Length;
+ keyData.Buffer = SessionKey;
+
+ outData.Length = 0;
+ outData.MaximumLength = 0;
+ outData.Buffer = NULL;
+
+ /* Get the required buffer size */
+ Status = SystemFunction004(&inData,
+ &keyData,
+ &outData);
+ if (Status != STATUS_BUFFER_TOO_SMALL)
+ {
+ ERR("SystemFunction004 failed (Status 0x%08lx)\n", Status);
+ return RtlNtStatusToDosError(Status);
+ }
+
+ /* Allocate a buffer for the encrypted password */
+ pBuffer = HeapAlloc(GetProcessHeap(), 0, outData.Length);
+ if (pBuffer == NULL)
+ return ERROR_OUTOFMEMORY;
+
+ outData.MaximumLength = outData.Length;
+ outData.Buffer = pBuffer;
+
+ /* Encrypt the password */
+ Status = SystemFunction004(&inData,
+ &keyData,
+ &outData);
+ if (!NT_SUCCESS(Status))
+ {
+ ERR("SystemFunction004 failed (Status 0x%08lx)\n", Status);
+ HeapFree(GetProcessHeap(), 0, pBuffer);
+ return RtlNtStatusToDosError(Status);
+ }
+
+ *pEncryptedPassword = outData.Buffer;
+ *pEncryptedPasswordSize = outData.Length;
+
+ return ERROR_SUCCESS;
+}
+
+
/**********************************************************************
* ChangeServiceConfig2A
*
DWORD dwDependenciesLength = 0;
SIZE_T cchLength;
LPCSTR lpStr;
- DWORD dwPasswordLength = 0;
+ DWORD dwPasswordSize = 0;
LPWSTR lpPasswordW = NULL;
LPBYTE lpEncryptedPassword = NULL;
TRACE("ChangeServiceConfigA(%p %lu %lu %lu %s %s %p %s %s %s %s)\n",
- dwServiceType, dwStartType, dwErrorControl, debugstr_a(lpBinaryPathName),
+ hService, dwServiceType, dwStartType, dwErrorControl, debugstr_a(lpBinaryPathName),
debugstr_a(lpLoadOrderGroup), lpdwTagId, debugstr_a(lpDependencies),
debugstr_a(lpServiceStartName), debugstr_a(lpPassword), debugstr_a(lpDisplayName));
lpPasswordW,
(int)(strlen(lpPassword) + 1));
- /* FIXME: Encrypt the password */
- lpEncryptedPassword = (LPBYTE)lpPasswordW;
- dwPasswordLength = (wcslen(lpPasswordW) + 1) * sizeof(WCHAR);
+ /* Encrypt the unicode password */
+ dwError = ScmEncryptPassword(hService,
+ lpPasswordW,
+ &lpEncryptedPassword,
+ &dwPasswordSize);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
RpcTryExcept
dwDependenciesLength,
(LPSTR)lpServiceStartName,
lpEncryptedPassword,
- dwPasswordLength,
+ dwPasswordSize,
(LPSTR)lpDisplayName);
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER)
}
RpcEndExcept;
+done:
if (lpPasswordW != NULL)
+ {
+ /* Wipe and release the password buffers */
+ SecureZeroMemory(lpPasswordW, (wcslen(lpPasswordW) + 1) * sizeof(WCHAR));
HeapFree(GetProcessHeap(), 0, lpPasswordW);
+ if (lpEncryptedPassword != NULL)
+ {
+ SecureZeroMemory(lpEncryptedPassword, dwPasswordSize);
+ HeapFree(GetProcessHeap(), 0, lpEncryptedPassword);
+ }
+ }
+
if (dwError != ERROR_SUCCESS)
{
TRACE("RChangeServiceConfigA() failed (Error %lu)\n", dwError);
DWORD dwDependenciesLength = 0;
SIZE_T cchLength;
LPCWSTR lpStr;
- DWORD dwPasswordLength = 0;
+ DWORD dwPasswordSize = 0;
LPBYTE lpEncryptedPassword = NULL;
TRACE("ChangeServiceConfigW(%p %lu %lu %lu %s %s %p %s %s %s %s)\n",
- dwServiceType, dwStartType, dwErrorControl, debugstr_w(lpBinaryPathName),
+ hService, dwServiceType, dwStartType, dwErrorControl, debugstr_w(lpBinaryPathName),
debugstr_w(lpLoadOrderGroup), lpdwTagId, debugstr_w(lpDependencies),
debugstr_w(lpServiceStartName), debugstr_w(lpPassword), debugstr_w(lpDisplayName));
if (lpPassword != NULL)
{
- /* FIXME: Encrypt the password */
- lpEncryptedPassword = (LPBYTE)lpPassword;
- dwPasswordLength = (wcslen(lpPassword) + 1) * sizeof(WCHAR);
+ dwError = ScmEncryptPassword(hService,
+ lpPassword,
+ &lpEncryptedPassword,
+ &dwPasswordSize);
+ if (dwError != ERROR_SUCCESS)
+ {
+ ERR("ScmEncryptPassword failed (Error %lu)\n", dwError);
+ goto done;
+ }
}
RpcTryExcept
dwDependenciesLength,
(LPWSTR)lpServiceStartName,
lpEncryptedPassword,
- dwPasswordLength,
+ dwPasswordSize,
(LPWSTR)lpDisplayName);
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER)
}
RpcEndExcept;
+done:
+ if (lpEncryptedPassword != NULL)
+ {
+ /* Wipe and release the password buffer */
+ SecureZeroMemory(lpEncryptedPassword, dwPasswordSize);
+ HeapFree(GetProcessHeap(), 0, lpEncryptedPassword);
+ }
+
if (dwError != ERROR_SUCCESS)
{
TRACE("RChangeServiceConfigW() failed (Error %lu)\n", dwError);
DWORD dwError;
SIZE_T cchLength;
LPCSTR lpStr;
- DWORD dwPasswordLength = 0;
+ DWORD dwPasswordSize = 0;
LPWSTR lpPasswordW = NULL;
LPBYTE lpEncryptedPassword = NULL;
lpPasswordW,
(int)(strlen(lpPassword) + 1));
- /* FIXME: Encrypt the password */
- lpEncryptedPassword = (LPBYTE)lpPasswordW;
- dwPasswordLength = (wcslen(lpPasswordW) + 1) * sizeof(WCHAR);
+ /* Encrypt the password */
+ dwError = ScmEncryptPassword(hSCManager,
+ lpPasswordW,
+ &lpEncryptedPassword,
+ &dwPasswordSize);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
RpcTryExcept
dwDependenciesLength,
(LPSTR)lpServiceStartName,
lpEncryptedPassword,
- dwPasswordLength,
+ dwPasswordSize,
(SC_RPC_HANDLE *)&hService);
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER)
}
RpcEndExcept;
+done:
if (lpPasswordW != NULL)
+ {
+ /* Wipe and release the password buffers */
+ SecureZeroMemory(lpPasswordW, (wcslen(lpPasswordW) + 1) * sizeof(WCHAR));
HeapFree(GetProcessHeap(), 0, lpPasswordW);
+ if (lpEncryptedPassword != NULL)
+ {
+ SecureZeroMemory(lpEncryptedPassword, dwPasswordSize);
+ HeapFree(GetProcessHeap(), 0, lpEncryptedPassword);
+ }
+ }
+
SetLastError(dwError);
if (dwError != ERROR_SUCCESS)
{
DWORD dwError;
SIZE_T cchLength;
LPCWSTR lpStr;
- DWORD dwPasswordLength = 0;
+ DWORD dwPasswordSize = 0;
LPBYTE lpEncryptedPassword = NULL;
TRACE("CreateServiceW(%p %s %s %lx %lu %lu %lu %s %s %p %s %s %s)\n",
if (lpPassword != NULL)
{
- /* FIXME: Encrypt the password */
- lpEncryptedPassword = (LPBYTE)lpPassword;
- dwPasswordLength = (wcslen(lpPassword) + 1) * sizeof(WCHAR);
+ /* Encrypt the password */
+ dwError = ScmEncryptPassword(hSCManager,
+ lpPassword,
+ &lpEncryptedPassword,
+ &dwPasswordSize);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
RpcTryExcept
dwDependenciesLength,
lpServiceStartName,
lpEncryptedPassword,
- dwPasswordLength,
+ dwPasswordSize,
(SC_RPC_HANDLE *)&hService);
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER)
}
RpcEndExcept;
+done:
+ if (lpEncryptedPassword != NULL)
+ {
+ /* Wipe and release the password buffers */
+ SecureZeroMemory(lpEncryptedPassword, dwPasswordSize);
+ HeapFree(GetProcessHeap(), 0, lpEncryptedPassword);
+ }
+
SetLastError(dwError);
if (dwError != ERROR_SUCCESS)
{
return FALSE;
}
+ /*
+ * NOTE: A size of 1 character would be enough, but tests show that
+ * Windows returns 2 characters instead, certainly due to a WCHAR/bytes
+ * mismatch in their code.
+ */
if (!lpDisplayName || *lpcchBuffer < sizeof(WCHAR))
{
lpNameBuffer = szEmptyName;
return FALSE;
}
+ /*
+ * NOTE: A size of 1 character would be enough, but tests show that
+ * Windows returns 2 characters instead, certainly due to a WCHAR/bytes
+ * mismatch in their code.
+ */
if (!lpServiceName || *lpcchBuffer < sizeof(WCHAR))
{
lpNameBuffer = szEmptyName;
}
+/**********************************************************************
+ * I_ScValidatePnpService
+ *
+ * Undocumented
+ *
+ * @implemented
+ */
+DWORD
+WINAPI
+I_ScValidatePnpService(
+ _In_ LPCWSTR pszMachineName,
+ _In_ LPCWSTR pszServiceName,
+ _Out_ SERVICE_STATUS_HANDLE *phServiceStatus)
+{
+ SC_RPC_HANDLE hSCManager = NULL;
+ SERVICE_STATUS_HANDLE hServiceStatus = NULL;
+ DWORD dwError;
+
+ TRACE("I_ScValidatePnpService(%S %S %p)\n",
+ pszMachineName, pszServiceName, phServiceStatus);
+
+ hSCManager = OpenSCManagerW(pszMachineName,
+ SERVICES_ACTIVE_DATABASEW,
+ SC_MANAGER_CONNECT);
+ if (hSCManager == NULL)
+ {
+ dwError = GetLastError();
+ goto done;
+ }
+
+ RpcTryExcept
+ {
+ dwError = RI_ScValidatePnPService(hSCManager,
+ (LPWSTR)pszServiceName,
+ (RPC_SERVICE_STATUS_HANDLE *)&hServiceStatus);
+ }
+ RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+ {
+ dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+ }
+ RpcEndExcept
+
+ *phServiceStatus = hServiceStatus;
+
+done:
+ if (hSCManager != NULL)
+ CloseServiceHandle(hSCManager);
+
+ return dwError;
+}
+
+
/**********************************************************************
* LockServiceDatabase
*
}
RpcEndExcept;
+ SetLastError(dwError);
if (dwError != ERROR_SUCCESS)
{
TRACE("ROpenSCManagerA() failed (Error %lu)\n", dwError);
- SetLastError(dwError);
return NULL;
}
}
RpcEndExcept;
+ SetLastError(dwError);
if (dwError != ERROR_SUCCESS)
{
TRACE("ROpenSCManagerW() failed (Error %lu)\n", dwError);
- SetLastError(dwError);
return NULL;
}
return TRUE;
}
+DWORD
+I_ScQueryServiceTagInfo(PVOID Unused,
+ TAG_INFO_LEVEL dwInfoLevel,
+ PTAG_INFO_NAME_FROM_TAG InOutParams)
+{
+ SC_HANDLE hScm;
+ DWORD dwError;
+ PTAG_INFO_NAME_FROM_TAG_IN_PARAMS InParams;
+ PTAG_INFO_NAME_FROM_TAG_OUT_PARAMS OutParams;
+ LPWSTR lpszName;
+
+ /* We only support one class */
+ if (dwInfoLevel != TagInfoLevelNameFromTag)
+ {
+ return ERROR_RETRY;
+ }
+
+ /* Validate input structure */
+ if (InOutParams->InParams.dwPid == 0 || InOutParams->InParams.dwTag == 0)
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ /* Validate output structure */
+ if (InOutParams->OutParams.pszName != NULL)
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ /* Open service manager */
+ hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
+ if (hScm == NULL)
+ {
+ return GetLastError();
+ }
+
+ /* Setup call parameters */
+ InParams = &InOutParams->InParams;
+ OutParams = NULL;
+
+ /* Call SCM to query tag information */
+ RpcTryExcept
+ {
+ dwError = RI_ScQueryServiceTagInfo(hScm, TagInfoLevelNameFromTag, &InParams, &OutParams);
+ }
+ RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+ {
+ dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+ }
+ RpcEndExcept;
+
+ /* Quit if not a success */
+ if (dwError != ERROR_SUCCESS)
+ {
+ goto Cleanup;
+ }
+
+ /* OutParams must be set now and we must have a name */
+ if (OutParams == NULL ||
+ OutParams->pszName == NULL)
+ {
+ dwError = ERROR_INVALID_DATA;
+ goto Cleanup;
+ }
+
+ /* Copy back what SCM returned */
+ lpszName = LocalAlloc(LPTR,
+ sizeof(WCHAR) * wcslen(OutParams->pszName) + sizeof(UNICODE_NULL));
+ if (lpszName == NULL)
+ {
+ dwError = GetLastError();
+ goto Cleanup;
+ }
+
+ wcscpy(lpszName, OutParams->pszName);
+ InOutParams->OutParams.pszName = lpszName;
+ InOutParams->OutParams.TagType = OutParams->TagType;
+
+Cleanup:
+ CloseServiceHandle(hScm);
+
+ /* Free memory allocated by SCM */
+ if (OutParams != NULL)
+ {
+ if (OutParams->pszName != NULL)
+ {
+ midl_user_free(OutParams->pszName);
+ }
+
+ midl_user_free(OutParams);
+ }
+
+ return dwError;
+}
+
+/**********************************************************************
+ * I_QueryTagInformation
+ *
+ * @implemented
+ */
+DWORD WINAPI
+I_QueryTagInformation(PVOID Unused,
+ TAG_INFO_LEVEL dwInfoLevel,
+ PTAG_INFO_NAME_FROM_TAG InOutParams)
+{
+ /*
+ * We only support one information class and it
+ * needs parameters
+ */
+ if (dwInfoLevel != TagInfoLevelNameFromTag ||
+ InOutParams == NULL)
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ /* Validate input structure */
+ if (InOutParams->InParams.dwPid == 0 || InOutParams->InParams.dwTag == 0)
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ /* Validate output structure */
+ if (InOutParams->OutParams.pszName != NULL)
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ /* Call internal function for the RPC call */
+ return I_ScQueryServiceTagInfo(Unused, TagInfoLevelNameFromTag, InOutParams);
+}
+
/* EOF */