From 51881431dd24b23da3d04a99d0c1978b413156f1 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Mon, 23 May 2016 21:48:32 +0000 Subject: [PATCH] [SERVICES] - RChangeServiceConfigA/W: Modify or delete password secrets. [ADVAPI32] - ChangeServiceConfigA, CreateServiceA: Convert passwords to Unicode before passing them to the remote functions. svn path=/trunk/; revision=71394 --- reactos/base/system/services/config.c | 2 +- reactos/base/system/services/rpcserver.c | 47 ++++++++++++++++++- reactos/dll/win32/advapi32/service/scm.c | 60 +++++++++++++++++++++--- 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/reactos/base/system/services/config.c b/reactos/base/system/services/config.c index c3310cdccec..23dc45f811a 100644 --- a/reactos/base/system/services/config.c +++ b/reactos/base/system/services/config.c @@ -481,7 +481,7 @@ ScmSetServicePassword( Status = LsaStorePrivateData(PolicyHandle, &ServiceName, - &Password); + pszPassword ? &Password : NULL); if (!NT_SUCCESS(Status)) { dwError = RtlNtStatusToDosError(Status); diff --git a/reactos/base/system/services/rpcserver.c b/reactos/base/system/services/rpcserver.c index 118b760123d..1c1167f5bb5 100644 --- a/reactos/base/system/services/rpcserver.c +++ b/reactos/base/system/services/rpcserver.c @@ -2022,7 +2022,27 @@ DWORD RChangeServiceConfigW( if (lpPassword != NULL) { - /* FIXME: Decrypt and write password */ + if (wcslen((LPWSTR)lpPassword) != 0) + { + /* FIXME: Decrypt the password */ + + /* Write the password */ + dwError = ScmSetServicePassword(lpService->szServiceName, + (LPCWSTR)lpPassword); + if (dwError != ERROR_SUCCESS) + goto done; + } + else + { + /* Delete the password */ + dwError = ScmSetServicePassword(lpService->szServiceName, + NULL); + if (dwError == ERROR_FILE_NOT_FOUND) + dwError = ERROR_SUCCESS; + + if (dwError != ERROR_SUCCESS) + goto done; + } } done: @@ -3460,11 +3480,34 @@ DWORD RChangeServiceConfigA( dwDependSize); HeapFree(GetProcessHeap(), 0, lpDependenciesW); + + if (dwError != ERROR_SUCCESS) + goto done; } if (lpPassword != NULL) { - /* FIXME: Decrypt and write password */ + if (wcslen((LPWSTR)lpPassword) != 0) + { + /* FIXME: Decrypt the password */ + + /* Write the password */ + dwError = ScmSetServicePassword(lpService->szServiceName, + (LPCWSTR)lpPassword); + if (dwError != ERROR_SUCCESS) + goto done; + } + else + { + /* Delete the password */ + dwError = ScmSetServicePassword(lpService->szServiceName, + NULL); + if (dwError == ERROR_FILE_NOT_FOUND) + dwError = ERROR_SUCCESS; + + if (dwError != ERROR_SUCCESS) + goto done; + } } done: diff --git a/reactos/dll/win32/advapi32/service/scm.c b/reactos/dll/win32/advapi32/service/scm.c index 33e1949c309..dfa43cd51db 100644 --- a/reactos/dll/win32/advapi32/service/scm.c +++ b/reactos/dll/win32/advapi32/service/scm.c @@ -287,6 +287,7 @@ ChangeServiceConfigA(SC_HANDLE hService, SIZE_T cchLength; LPCSTR lpStr; DWORD dwPasswordLength = 0; + LPWSTR lpPasswordW = NULL; LPBYTE lpEncryptedPassword = NULL; TRACE("ChangeServiceConfigA() called\n"); @@ -304,9 +305,29 @@ ChangeServiceConfigA(SC_HANDLE hService, dwDependenciesLength++; } - /* FIXME: Encrypt the password */ - lpEncryptedPassword = (LPBYTE)lpPassword; - dwPasswordLength = (DWORD)(lpPassword ? (strlen(lpPassword) + 1) * sizeof(CHAR) : 0); + if (lpPassword != NULL) + { + /* Convert the password to unicode */ + lpPasswordW = HeapAlloc(GetProcessHeap(), + HEAP_ZERO_MEMORY, + (strlen(lpPassword) + 1) * sizeof(WCHAR)); + if (lpPasswordW == NULL) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return FALSE; + } + + MultiByteToWideChar(CP_ACP, + 0, + lpPassword, + -1, + lpPasswordW, + (int)(strlen(lpPassword) + 1)); + + /* FIXME: Encrypt the password */ + lpEncryptedPassword = (LPBYTE)lpPasswordW; + dwPasswordLength = (DWORD)(lpPasswordW ? (wcslen(lpPasswordW) + 1) * sizeof(WCHAR) : 0); + } RpcTryExcept { @@ -331,6 +352,9 @@ ChangeServiceConfigA(SC_HANDLE hService, } RpcEndExcept; + if (lpPasswordW != NULL) + HeapFree(GetProcessHeap(), 0, lpPasswordW); + if (dwError != ERROR_SUCCESS) { TRACE("RChangeServiceConfigA() failed (Error %lu)\n", dwError); @@ -548,6 +572,7 @@ CreateServiceA(SC_HANDLE hSCManager, SIZE_T cchLength; LPCSTR lpStr; DWORD dwPasswordLength = 0; + LPWSTR lpPasswordW = NULL; LPBYTE lpEncryptedPassword = NULL; TRACE("CreateServiceA() called\n"); @@ -573,9 +598,29 @@ CreateServiceA(SC_HANDLE hSCManager, dwDependenciesLength++; } - /* FIXME: Encrypt the password */ - lpEncryptedPassword = (LPBYTE)lpPassword; - dwPasswordLength = (DWORD)(lpPassword ? (strlen(lpPassword) + 1) * sizeof(CHAR) : 0); + if (lpPassword != NULL) + { + /* Convert the password to unicode */ + lpPasswordW = HeapAlloc(GetProcessHeap(), + HEAP_ZERO_MEMORY, + (strlen(lpPassword) + 1) * sizeof(WCHAR)); + if (lpPasswordW == NULL) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return FALSE; + } + + MultiByteToWideChar(CP_ACP, + 0, + lpPassword, + -1, + lpPasswordW, + (int)(strlen(lpPassword) + 1)); + + /* FIXME: Encrypt the password */ + lpEncryptedPassword = (LPBYTE)lpPasswordW; + dwPasswordLength = (DWORD)(lpPasswordW ? (wcslen(lpPasswordW) + 1) * sizeof(WCHAR) : 0); + } RpcTryExcept { @@ -603,6 +648,9 @@ CreateServiceA(SC_HANDLE hSCManager, } RpcEndExcept; + if (lpPasswordW != NULL) + HeapFree(GetProcessHeap(), 0, lpPasswordW); + if (dwError != ERROR_SUCCESS) { TRACE("RCreateServiceA() failed (Error %lu)\n", dwError); -- 2.17.1