/* INCLUDES ****************************************************************/
#include "services.h"
-#include "svcctl_s.h"
#define NDEBUG
#include <debug.h>
SERVICE_STOP | \
SERVICE_START)
+#define TAG_ARRAY_SIZE 32
/* VARIABLES ***************************************************************/
ScmServiceMapping = {SERVICE_READ,
SERVICE_WRITE,
SERVICE_EXECUTE,
- SC_MANAGER_ALL_ACCESS};
+ SERVICE_ALL_ACCESS};
/* FUNCTIONS ***************************************************************/
if (_wcsicmp(lpDatabaseName, SERVICES_FAILED_DATABASEW) == 0)
{
- DPRINT("Database %S, does not exist\n",lpDatabaseName);
+ DPRINT("Database %S, does not exist\n", lpDatabaseName);
return ERROR_DATABASE_DOES_NOT_EXIST;
}
else if (_wcsicmp(lpDatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
{
- DPRINT("Invalid Database name %S.\n",lpDatabaseName);
+ DPRINT("Invalid Database name %S.\n", lpDatabaseName);
return ERROR_INVALID_NAME;
}
- Ptr = (MANAGER_HANDLE*) HeapAlloc(GetProcessHeap(),
+ Ptr = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
- sizeof(MANAGER_HANDLE) + (wcslen(lpDatabaseName) + 1) * sizeof(WCHAR));
+ FIELD_OFFSET(MANAGER_HANDLE, DatabaseName[wcslen(lpDatabaseName) + 1]));
if (Ptr == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
{
PSERVICE_HANDLE Ptr;
- Ptr = (SERVICE_HANDLE*) HeapAlloc(GetProcessHeap(),
+ Ptr = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(SERVICE_HANDLE));
if (Ptr == NULL)
DWORD
ScmAssignNewTag(PSERVICE lpService)
{
- /* FIXME */
- DPRINT("Assigning new tag to service %S\n", lpService->lpServiceName);
- lpService->dwTag = 0;
- return ERROR_SUCCESS;
-}
+ HKEY hKey = NULL;
+ DWORD dwError;
+ DWORD dwGroupTagCount = 0;
+ PDWORD pdwGroupTags = NULL;
+ DWORD dwFreeTag = 0;
+ DWORD dwTagUsedBase = 1;
+ BOOLEAN TagUsed[TAG_ARRAY_SIZE];
+ DWORD dwTagOffset;
+ DWORD i;
+ DWORD cbDataSize;
+ PLIST_ENTRY ServiceEntry;
+ PSERVICE CurrentService;
+ ASSERT(lpService != NULL);
+ ASSERT(lpService->lpGroup != NULL);
-/* Internal recursive function */
-/* Need to search for every dependency on every service */
-static DWORD
-Int_EnumDependentServicesW(HKEY hServicesKey,
- PSERVICE lpService,
- DWORD dwServiceState,
- PSERVICE *lpServices,
- LPDWORD pcbBytesNeeded,
- LPDWORD lpServicesReturned)
-{
- DWORD dwError = ERROR_SUCCESS;
- WCHAR szNameBuf[MAX_PATH];
- WCHAR szValueBuf[MAX_PATH];
- WCHAR *lpszNameBuf = szNameBuf;
- WCHAR *lpszValueBuf = szValueBuf;
- DWORD dwSize;
- DWORD dwNumSubKeys;
- DWORD dwIteration;
- PSERVICE lpCurrentService;
- HKEY hServiceEnumKey;
- DWORD dwCurrentServiceState = SERVICE_ACTIVE;
- DWORD dwDependServiceStrPtr = 0;
- DWORD dwRequiredSize = 0;
+ dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+ L"System\\CurrentControlSet\\Control\\GroupOrderList",
+ 0,
+ KEY_READ,
+ &hKey);
- /* Get the number of service keys */
- dwError = RegQueryInfoKeyW(hServicesKey,
- NULL,
- NULL,
- NULL,
- &dwNumSubKeys,
- NULL,
- NULL,
- NULL,
+ if (dwError != ERROR_SUCCESS)
+ goto findFreeTag;
+
+ /* query value length */
+ cbDataSize = 0;
+ dwError = RegQueryValueExW(hKey,
+ lpService->lpGroup->szGroupName,
NULL,
NULL,
NULL,
- NULL);
- if (dwError != ERROR_SUCCESS)
+ &cbDataSize);
+
+ if (dwError != ERROR_SUCCESS && dwError != ERROR_MORE_DATA)
+ goto findFreeTag;
+
+ pdwGroupTags = HeapAlloc(GetProcessHeap(), 0, cbDataSize);
+ if (!pdwGroupTags)
{
- DPRINT("ERROR! Unable to get number of services keys.\n");
- return dwError;
+ dwError = ERROR_NOT_ENOUGH_MEMORY;
+ goto cleanup;
}
- /* Iterate the service keys to see if another service depends on the this service */
- for (dwIteration = 0; dwIteration < dwNumSubKeys; dwIteration++)
- {
- dwSize = MAX_PATH;
- dwError = RegEnumKeyExW(hServicesKey,
- dwIteration,
- lpszNameBuf,
- &dwSize,
- NULL,
- NULL,
- NULL,
- NULL);
- if (dwError != ERROR_SUCCESS)
- return dwError;
+ dwError = RegQueryValueExW(hKey,
+ lpService->lpGroup->szGroupName,
+ NULL,
+ NULL,
+ (LPBYTE)pdwGroupTags,
+ &cbDataSize);
- /* Open the Service key */
- dwError = RegOpenKeyExW(hServicesKey,
- lpszNameBuf,
- 0,
- KEY_READ,
- &hServiceEnumKey);
- if (dwError != ERROR_SUCCESS)
- return dwError;
+ if (dwError != ERROR_SUCCESS)
+ goto findFreeTag;
- dwSize = MAX_PATH;
+ if (cbDataSize < sizeof(pdwGroupTags[0]))
+ goto findFreeTag;
- /* Check for the DependOnService Value */
- dwError = RegQueryValueExW(hServiceEnumKey,
- L"DependOnService",
- NULL,
- NULL,
- (LPBYTE)lpszValueBuf,
- &dwSize);
+ dwGroupTagCount = min(pdwGroupTags[0], cbDataSize / sizeof(pdwGroupTags[0]) - 1);
- /* FIXME: Handle load order. */
+findFreeTag:
+ do
+ {
+ /* mark all tags as unused */
+ for (i = 0; i < TAG_ARRAY_SIZE; i++)
+ TagUsed[i] = FALSE;
- /* If the service found has a DependOnService value */
- if (dwError == ERROR_SUCCESS)
+ /* mark tags in GroupOrderList as used */
+ for (i = 1; i <= dwGroupTagCount; i++)
{
- dwDependServiceStrPtr = 0;
+ dwTagOffset = pdwGroupTags[i] - dwTagUsedBase;
+ if (dwTagOffset >= 0 && dwTagOffset < TAG_ARRAY_SIZE)
+ TagUsed[dwTagOffset] = TRUE;
+ }
- /* Can be more than one Dependencies in the DependOnService string */
- while (wcslen(lpszValueBuf + dwDependServiceStrPtr) > 0)
+ /* mark tags in service list as used */
+ ServiceEntry = lpService->ServiceListEntry.Flink;
+ while (ServiceEntry != &lpService->ServiceListEntry)
+ {
+ ASSERT(ServiceEntry != NULL);
+ CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry);
+ if (CurrentService->lpGroup == lpService->lpGroup)
{
- if (_wcsicmp(lpszValueBuf + dwDependServiceStrPtr, lpService->lpServiceName) == 0)
- {
- /* Get the current enumed service pointer */
- lpCurrentService = ScmGetServiceEntryByName(lpszNameBuf);
-
- /* Check for valid Service */
- if (!lpCurrentService)
- {
- /* This should never happen! */
- DPRINT("This should not happen at this point, report to Developer\n");
- return ERROR_NOT_FOUND;
- }
-
- /* Determine state the service is in */
- if (lpCurrentService->Status.dwCurrentState == SERVICE_STOPPED)
- dwCurrentServiceState = SERVICE_INACTIVE;
-
- /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
- if ((dwCurrentServiceState == dwServiceState) ||
- (dwServiceState == SERVICE_STATE_ALL))
- {
- /* Calculate the required size */
- dwRequiredSize += sizeof(SERVICE_STATUS);
- dwRequiredSize += ((wcslen(lpCurrentService->lpServiceName) + 1) * sizeof(WCHAR));
- dwRequiredSize += ((wcslen(lpCurrentService->lpDisplayName) + 1) * sizeof(WCHAR));
-
- /* Add the size for service name and display name pointers */
- dwRequiredSize += (2 * sizeof(PVOID));
-
- /* increase the BytesNeeded size */
- *pcbBytesNeeded = *pcbBytesNeeded + dwRequiredSize;
+ dwTagOffset = CurrentService->dwTag - dwTagUsedBase;
+ if (dwTagOffset >= 0 && dwTagOffset < TAG_ARRAY_SIZE)
+ TagUsed[dwTagOffset] = TRUE;
+ }
- /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
- comes first */
+ ServiceEntry = ServiceEntry->Flink;
+ }
- /* Recursive call to check for its dependencies */
- Int_EnumDependentServicesW(hServicesKey,
- lpCurrentService,
- dwServiceState,
- lpServices,
- pcbBytesNeeded,
- lpServicesReturned);
+ /* find unused tag, if any */
+ for (i = 0; i < TAG_ARRAY_SIZE; i++)
+ {
+ if (!TagUsed[i])
+ {
+ dwFreeTag = dwTagUsedBase + i;
+ break;
+ }
+ }
- /* If the lpServices is valid set the service pointer */
- if (lpServices)
- lpServices[*lpServicesReturned] = lpCurrentService;
+ dwTagUsedBase += TAG_ARRAY_SIZE;
+ }
+ while (!dwFreeTag);
- *lpServicesReturned = *lpServicesReturned + 1;
- }
- }
+cleanup:
+ if (pdwGroupTags)
+ HeapFree(GetProcessHeap(), 0, pdwGroupTags);
- dwDependServiceStrPtr += (wcslen(lpszValueBuf + dwDependServiceStrPtr) + 1);
- }
- }
- else if (*pcbBytesNeeded)
- {
- dwError = ERROR_SUCCESS;
- }
+ if (hKey)
+ RegCloseKey(hKey);
- RegCloseKey(hServiceEnumKey);
+ if (dwFreeTag)
+ {
+ lpService->dwTag = dwFreeTag;
+ DPRINT("Assigning new tag %lu to service %S in group %S\n",
+ lpService->dwTag, lpService->lpServiceName, lpService->lpGroup->szGroupName);
+ dwError = ERROR_SUCCESS;
+ }
+ else
+ {
+ DPRINT1("Failed to assign new tag to service %S, error=%lu\n",
+ lpService->lpServiceName, dwError);
}
return dwError;
}
-/* Function 0 */
-DWORD RCloseServiceHandle(
- LPSC_RPC_HANDLE hSCObject)
+/* Create a path suitable for the bootloader out of the full path */
+DWORD
+ScmConvertToBootPathName(wchar_t *CanonName, wchar_t **RelativeName)
{
- PMANAGER_HANDLE hManager;
- PSERVICE_HANDLE hService;
- PSERVICE lpService;
- HKEY hServicesKey;
- DWORD dwError;
- DWORD pcbBytesNeeded = 0;
- DWORD dwServicesReturned = 0;
-
- DPRINT("RCloseServiceHandle() called\n");
-
- DPRINT("hSCObject = %p\n", *hSCObject);
+ DWORD ServiceNameLen, BufferSize, ExpandedLen;
+ WCHAR Dest;
+ WCHAR *Expanded;
+ UNICODE_STRING NtPathName, SystemRoot, LinkTarget;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ NTSTATUS Status;
+ HANDLE SymbolicLinkHandle;
- if (*hSCObject == 0)
- return ERROR_INVALID_HANDLE;
+ DPRINT("ScmConvertToBootPathName %S\n", CanonName);
- hManager = ScmGetServiceManagerFromHandle(*hSCObject);
- hService = ScmGetServiceFromHandle(*hSCObject);
+ ServiceNameLen = wcslen(CanonName);
- if (hManager != NULL)
+ /* First check, if it's already good */
+ if (ServiceNameLen > 12 &&
+ !_wcsnicmp(L"\\SystemRoot\\", CanonName, 12))
{
- DPRINT("Found manager handle\n");
-
- /* FIXME: add handle cleanup code */
-
- HeapFree(GetProcessHeap(), 0, hManager);
- hManager = NULL;
+ *RelativeName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen + 1) * sizeof(WCHAR));
+ if (*RelativeName == NULL)
+ {
+ DPRINT("Error allocating memory for boot driver name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- *hSCObject = NULL;
+ /* Copy it */
+ wcscpy(*RelativeName, CanonName);
- DPRINT("RCloseServiceHandle() done\n");
+ DPRINT("Bootdriver name %S\n", *RelativeName);
return ERROR_SUCCESS;
}
- else if (hService != NULL)
+
+ /* If it has %SystemRoot% prefix, substitute it to \System*/
+ if (ServiceNameLen > 13 &&
+ !_wcsnicmp(L"%SystemRoot%\\", CanonName, 13))
{
- DPRINT("Found service handle\n");
+ /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
+ *RelativeName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ ServiceNameLen * sizeof(WCHAR));
- /* Lock the service database exlusively */
- ScmLockDatabaseExclusive();
+ if (*RelativeName == NULL)
+ {
+ DPRINT("Error allocating memory for boot driver name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- /* Get the pointer to the service record */
- lpService = hService->ServiceEntry;
+ /* Copy it */
+ wcscpy(*RelativeName, L"\\SystemRoot\\");
+ wcscat(*RelativeName, CanonName + 13);
- /* FIXME: add handle cleanup code */
+ DPRINT("Bootdriver name %S\n", *RelativeName);
+ return ERROR_SUCCESS;
+ }
- /* Free the handle */
- HeapFree(GetProcessHeap(), 0, hService);
- hService = NULL;
+ /* Get buffer size needed for expanding env strings */
+ BufferSize = ExpandEnvironmentStringsW(L"%SystemRoot%\\", &Dest, 1);
- ASSERT(lpService->dwRefCount > 0);
+ if (BufferSize <= 1)
+ {
+ DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
+ return ERROR_INVALID_ENVIRONMENT;
+ }
- lpService->dwRefCount--;
- DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
- lpService->dwRefCount);
+ /* Allocate memory, since the size is known now */
+ Expanded = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (BufferSize + 1) * sizeof(WCHAR));
+ if (!Expanded)
+ {
+ DPRINT("Error allocating memory for boot driver name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- if (lpService->dwRefCount == 0)
- {
- /* If this service has been marked for deletion */
- if (lpService->bDeleted)
- {
- /* Open the Services Reg key */
- dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
- L"System\\CurrentControlSet\\Services",
- 0,
- KEY_SET_VALUE | KEY_READ,
- &hServicesKey);
- if (dwError != ERROR_SUCCESS)
- {
- DPRINT("Failed to open services key\n");
- ScmUnlockDatabase();
- return dwError;
- }
-
- /* Call the internal function with NULL, just to get bytes we need */
- Int_EnumDependentServicesW(hServicesKey,
- lpService,
- SERVICE_ACTIVE,
- NULL,
- &pcbBytesNeeded,
- &dwServicesReturned);
+ /* Expand it */
+ if (ExpandEnvironmentStringsW(L"%SystemRoot%\\", Expanded, BufferSize) >
+ BufferSize)
+ {
+ DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service*/
- if (pcbBytesNeeded)
- {
- DPRINT("Deletion failed due to running dependencies.\n");
- RegCloseKey(hServicesKey);
- ScmUnlockDatabase();
- return ERROR_SUCCESS;
- }
+ /* Convert to NY-style path */
+ if (!RtlDosPathNameToNtPathName_U(Expanded, &NtPathName, NULL, NULL))
+ {
+ DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
+ return ERROR_INVALID_ENVIRONMENT;
+ }
- /* There are no references and no runnning dependencies,
- it is now safe to delete the service */
+ DPRINT("Converted to NT-style %wZ\n", &NtPathName);
- /* Delete the Service Key */
- dwError = RegDeleteKeyW(hServicesKey,
- lpService->lpServiceName);
+ /* No need to keep the dos-path anymore */
+ HeapFree(GetProcessHeap(), 0, Expanded);
- RegCloseKey(hServicesKey);
+ /* Copy it to the allocated place */
+ Expanded = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ NtPathName.Length + sizeof(UNICODE_NULL));
+ if (!Expanded)
+ {
+ DPRINT("Error allocating memory for boot driver name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- if (dwError != ERROR_SUCCESS)
- {
- DPRINT("Failed to Delete the Service Registry key\n");
- ScmUnlockDatabase();
- return dwError;
- }
+ ExpandedLen = NtPathName.Length / sizeof(WCHAR);
+ wcsncpy(Expanded, NtPathName.Buffer, ExpandedLen);
+ Expanded[ExpandedLen] = UNICODE_NULL;
- /* Delete the Service */
- ScmDeleteServiceRecord(lpService);
- }
+ if (ServiceNameLen > ExpandedLen &&
+ !_wcsnicmp(Expanded, CanonName, ExpandedLen))
+ {
+ /* Only \SystemRoot\ is missing */
+ *RelativeName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen - ExpandedLen) * sizeof(WCHAR) + 13*sizeof(WCHAR));
+ if (*RelativeName == NULL)
+ {
+ DPRINT("Error allocating memory for boot driver name!\n");
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ return ERROR_NOT_ENOUGH_MEMORY;
}
- ScmUnlockDatabase();
-
- *hSCObject = NULL;
+ wcscpy(*RelativeName, L"\\SystemRoot\\");
+ wcscat(*RelativeName, CanonName + ExpandedLen);
- DPRINT("RCloseServiceHandle() done\n");
+ RtlFreeUnicodeString(&NtPathName);
return ERROR_SUCCESS;
}
- DPRINT("Invalid handle tag (Tag %lx)\n", hManager->Handle.Tag);
+ /* The most complex case starts here */
+ RtlInitUnicodeString(&SystemRoot, L"\\SystemRoot");
+ InitializeObjectAttributes(&ObjectAttributes,
+ &SystemRoot,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
- return ERROR_INVALID_HANDLE;
-}
+ /* Open this symlink */
+ Status = NtOpenSymbolicLinkObject(&SymbolicLinkHandle, SYMBOLIC_LINK_QUERY, &ObjectAttributes);
+ if (NT_SUCCESS(Status))
+ {
+ LinkTarget.Length = 0;
+ LinkTarget.MaximumLength = 0;
-/* Function 1 */
-DWORD RControlService(
- SC_RPC_HANDLE hService,
- DWORD dwControl,
- LPSERVICE_STATUS lpServiceStatus)
-{
- PSERVICE_HANDLE hSvc;
- PSERVICE lpService;
- ACCESS_MASK DesiredAccess;
- DWORD dwError = ERROR_SUCCESS;
- DWORD pcbBytesNeeded = 0;
- DWORD dwServicesReturned = 0;
- DWORD dwControlsAccepted;
- DWORD dwCurrentState;
- HKEY hServicesKey = NULL;
+ DPRINT("Opened symbolic link object\n");
- DPRINT("RControlService() called\n");
+ Status = NtQuerySymbolicLinkObject(SymbolicLinkHandle, &LinkTarget, &BufferSize);
+ if (NT_SUCCESS(Status) || Status == STATUS_BUFFER_TOO_SMALL)
+ {
+ /* Check if required buffer size is sane */
+ if (BufferSize > 0xFFFD)
+ {
+ DPRINT("Too large buffer required\n");
+ *RelativeName = 0;
- if (ScmShutdown)
- return ERROR_SHUTDOWN_IN_PROGRESS;
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- /* Check the service handle */
- hSvc = ScmGetServiceFromHandle(hService);
- if (hSvc == NULL)
- {
- DPRINT1("Invalid service handle!\n");
- return ERROR_INVALID_HANDLE;
- }
+ /* Alloc the string */
+ LinkTarget.Length = (USHORT)BufferSize;
+ LinkTarget.MaximumLength = LinkTarget.Length + sizeof(UNICODE_NULL);
+ LinkTarget.Buffer = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ LinkTarget.MaximumLength);
+ if (!LinkTarget.Buffer)
+ {
+ DPRINT("Unable to alloc buffer\n");
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
+ /* Do a real query now */
+ Status = NtQuerySymbolicLinkObject(SymbolicLinkHandle, &LinkTarget, &BufferSize);
+ if (NT_SUCCESS(Status))
+ {
+ DPRINT("LinkTarget: %wZ\n", &LinkTarget);
- /* Check the service entry point */
- lpService = hSvc->ServiceEntry;
- if (lpService == NULL)
- {
- DPRINT1("lpService == NULL!\n");
- return ERROR_INVALID_HANDLE;
- }
+ ExpandedLen = LinkTarget.Length / sizeof(WCHAR);
+ if ((ServiceNameLen > ExpandedLen) &&
+ !_wcsnicmp(LinkTarget.Buffer, CanonName, ExpandedLen))
+ {
+ *RelativeName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen - ExpandedLen) * sizeof(WCHAR) + 13*sizeof(WCHAR));
- /* Check access rights */
- switch (dwControl)
- {
- case SERVICE_CONTROL_STOP:
- DesiredAccess = SERVICE_STOP;
- break;
+ if (*RelativeName == NULL)
+ {
+ DPRINT("Unable to alloc buffer\n");
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ RtlFreeUnicodeString(&NtPathName);
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- case SERVICE_CONTROL_PAUSE:
- case SERVICE_CONTROL_CONTINUE:
- DesiredAccess = SERVICE_PAUSE_CONTINUE;
- break;
+ /* Copy it over, substituting the first part
+ with SystemRoot */
+ wcscpy(*RelativeName, L"\\SystemRoot\\");
+ wcscat(*RelativeName, CanonName+ExpandedLen+1);
- case SERVICE_INTERROGATE:
- DesiredAccess = SERVICE_INTERROGATE;
- break;
+ /* Cleanup */
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ RtlFreeUnicodeString(&NtPathName);
- default:
- if (dwControl >= 128 && dwControl <= 255)
- DesiredAccess = SERVICE_USER_DEFINED_CONTROL;
+ /* Return success */
+ return ERROR_SUCCESS;
+ }
+ else
+ {
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ RtlFreeUnicodeString(&NtPathName);
+ return ERROR_INVALID_PARAMETER;
+ }
+ }
else
- DesiredAccess = SERVICE_QUERY_CONFIG |
- SERVICE_CHANGE_CONFIG |
- SERVICE_QUERY_STATUS |
- SERVICE_START |
- SERVICE_PAUSE_CONTINUE;
- break;
- }
-
- if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- DesiredAccess))
- return ERROR_ACCESS_DENIED;
-
- if (dwControl == SERVICE_CONTROL_STOP)
- {
- /* Check if the service has dependencies running as windows
- doesn't stop a service that does */
-
- /* Open the Services Reg key */
- dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
- L"System\\CurrentControlSet\\Services",
- 0,
- KEY_READ,
- &hServicesKey);
- if (dwError != ERROR_SUCCESS)
- {
- DPRINT("Failed to open services key\n");
- return dwError;
+ {
+ DPRINT("Error, Status = %08X\n", Status);
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ RtlFreeUnicodeString(&NtPathName);
+ return ERROR_INVALID_PARAMETER;
+ }
}
-
- /* Call the internal function with NULL, just to get bytes we need */
- Int_EnumDependentServicesW(hServicesKey,
- lpService,
- SERVICE_ACTIVE,
- NULL,
- &pcbBytesNeeded,
- &dwServicesReturned);
-
- RegCloseKey(hServicesKey);
-
- /* If pcbBytesNeeded is not zero then there are services running that
- are dependent on this service */
- if (pcbBytesNeeded != 0)
+ else
{
- DPRINT("Service has running dependencies. Failed to stop service.\n");
- return ERROR_DEPENDENT_SERVICES_RUNNING;
+ DPRINT("Error, Status = %08X\n", Status);
+ if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ RtlFreeUnicodeString(&NtPathName);
+ return ERROR_INVALID_PARAMETER;
}
}
-
- if (lpService->Status.dwServiceType & SERVICE_DRIVER)
+ else
{
- /* Send control code to the driver */
- dwError = ScmControlDriver(lpService,
- dwControl,
- lpServiceStatus);
+ DPRINT("Error, Status = %08X\n", Status);
+ HeapFree(GetProcessHeap(), 0, Expanded);
+ return ERROR_INVALID_PARAMETER;
}
- else
+
+ /* Failure */
+ *RelativeName = NULL;
+ return ERROR_INVALID_PARAMETER;
+}
+
+
+DWORD
+ScmCanonDriverImagePath(DWORD dwStartType,
+ const wchar_t *lpServiceName,
+ wchar_t **lpCanonName)
+{
+ DWORD ServiceNameLen, Result;
+ UNICODE_STRING NtServiceName;
+ WCHAR *RelativeName;
+ const WCHAR *SourceName = lpServiceName;
+
+ /* Calculate the length of the service's name */
+ ServiceNameLen = wcslen(lpServiceName);
+
+ /* 12 is wcslen(L"\\SystemRoot\\") */
+ if (ServiceNameLen > 12 &&
+ !_wcsnicmp(L"\\SystemRoot\\", lpServiceName, 12))
{
- dwControlsAccepted = lpService->Status.dwControlsAccepted;
- dwCurrentState = lpService->Status.dwCurrentState;
+ /* SystemRoot prefix is already included */
+ *lpCanonName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen + 1) * sizeof(WCHAR));
- /* Check the current state before sending a control request */
- switch (dwCurrentState)
+ if (*lpCanonName == NULL)
{
- case SERVICE_STOP_PENDING:
- case SERVICE_STOPPED:
- return ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
+ DPRINT("Error allocating memory for canonized service name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
- case SERVICE_START_PENDING:
- switch (dwControl)
- {
- case SERVICE_CONTROL_STOP:
- break;
+ /* If it's a boot-time driver, it must be systemroot relative */
+ if (dwStartType == SERVICE_BOOT_START)
+ SourceName += 12;
- case SERVICE_CONTROL_INTERROGATE:
- RtlCopyMemory(lpServiceStatus,
- &lpService->Status,
- sizeof(SERVICE_STATUS));
- return ERROR_SUCCESS;
+ /* Copy it */
+ wcscpy(*lpCanonName, SourceName);
- default:
- return ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
- }
- break;
- }
+ DPRINT("Canonicalized name %S\n", *lpCanonName);
+ return NO_ERROR;
+ }
- /* Check if the control code is acceptable to the service */
- switch (dwControl)
- {
- case SERVICE_CONTROL_STOP:
- if ((dwControlsAccepted & SERVICE_ACCEPT_STOP) == 0)
- return ERROR_INVALID_SERVICE_CONTROL;
- break;
+ /* Check if it has %SystemRoot% (len=13) */
+ if (ServiceNameLen > 13 &&
+ !_wcsnicmp(L"%SystemRoot%\\", lpServiceName, 13))
+ {
+ /* Substitute %SystemRoot% with \\SystemRoot\\ */
+ *lpCanonName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen + 1) * sizeof(WCHAR));
- case SERVICE_CONTROL_PAUSE:
- case SERVICE_CONTROL_CONTINUE:
- if ((dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE) == 0)
- return ERROR_INVALID_SERVICE_CONTROL;
- break;
+ if (*lpCanonName == NULL)
+ {
+ DPRINT("Error allocating memory for canonized service name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
}
- /* Send control code to the service */
- dwError = ScmControlService(lpService,
- dwControl);
+ /* If it's a boot-time driver, it must be systemroot relative */
+ if (dwStartType == SERVICE_BOOT_START)
+ wcscpy(*lpCanonName, L"\\SystemRoot\\");
- /* Return service status information */
- RtlCopyMemory(lpServiceStatus,
- &lpService->Status,
- sizeof(SERVICE_STATUS));
+ wcscat(*lpCanonName, lpServiceName + 13);
+
+ DPRINT("Canonicalized name %S\n", *lpCanonName);
+ return NO_ERROR;
}
- if ((dwError == ERROR_SUCCESS) && (pcbBytesNeeded))
- dwError = ERROR_DEPENDENT_SERVICES_RUNNING;
+ /* Check if it's a relative path name */
+ if (lpServiceName[0] != L'\\' && lpServiceName[1] != L':')
+ {
+ *lpCanonName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ (ServiceNameLen + 1) * sizeof(WCHAR));
- return dwError;
-}
+ if (*lpCanonName == NULL)
+ {
+ DPRINT("Error allocating memory for canonized service name!\n");
+ return ERROR_NOT_ENOUGH_MEMORY;
+ }
+ /* Just copy it over without changing */
+ wcscpy(*lpCanonName, lpServiceName);
-/* Function 2 */
-DWORD RDeleteService(
- SC_RPC_HANDLE hService)
-{
- PSERVICE_HANDLE hSvc;
- PSERVICE lpService;
- DWORD dwError;
+ return NO_ERROR;
+ }
- DPRINT("RDeleteService() called\n");
+ /* It seems to be a DOS path, convert it */
+ if (!RtlDosPathNameToNtPathName_U(lpServiceName, &NtServiceName, NULL, NULL))
+ {
+ DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
+ return ERROR_INVALID_PARAMETER;
+ }
- if (ScmShutdown)
- return ERROR_SHUTDOWN_IN_PROGRESS;
+ *lpCanonName = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ NtServiceName.Length + sizeof(WCHAR));
- hSvc = ScmGetServiceFromHandle(hService);
- if (hSvc == NULL)
+ if (*lpCanonName == NULL)
{
- DPRINT1("Invalid service handle!\n");
- return ERROR_INVALID_HANDLE;
+ DPRINT("Error allocating memory for canonized service name!\n");
+ RtlFreeUnicodeString(&NtServiceName);
+ return ERROR_NOT_ENOUGH_MEMORY;
}
- if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- DELETE))
- return ERROR_ACCESS_DENIED;
+ /* Copy the string */
+ wcsncpy(*lpCanonName, NtServiceName.Buffer, NtServiceName.Length / sizeof(WCHAR));
- lpService = hSvc->ServiceEntry;
- if (lpService == NULL)
+ /* The unicode string is not needed anymore */
+ RtlFreeUnicodeString(&NtServiceName);
+
+ if (dwStartType != SERVICE_BOOT_START)
{
- DPRINT("lpService == NULL!\n");
- return ERROR_INVALID_HANDLE;
+ DPRINT("Canonicalized name %S\n", *lpCanonName);
+ return NO_ERROR;
}
- /* Lock the service database exclusively */
- ScmLockDatabaseExclusive();
-
- if (lpService->bDeleted)
+ /* The service is boot-started, so must be relative */
+ Result = ScmConvertToBootPathName(*lpCanonName, &RelativeName);
+ if (Result)
{
- DPRINT("The service has already been marked for delete!\n");
- dwError = ERROR_SERVICE_MARKED_FOR_DELETE;
- goto Done;
+ /* There is a problem, free name and return */
+ HeapFree(GetProcessHeap(), 0, *lpCanonName);
+ DPRINT("Error converting named!\n");
+ return Result;
}
- /* Mark service for delete */
- lpService->bDeleted = TRUE;
+ ASSERT(RelativeName);
- dwError = ScmMarkServiceForDelete(lpService);
+ /* Copy that string */
+ wcscpy(*lpCanonName, RelativeName + 12);
-Done:;
- /* Unlock the service database */
- ScmUnlockDatabase();
+ /* Free the allocated buffer */
+ HeapFree(GetProcessHeap(), 0, RelativeName);
- DPRINT("RDeleteService() done\n");
+ DPRINT("Canonicalized name %S\n", *lpCanonName);
- return dwError;
+ /* Success */
+ return NO_ERROR;
}
-/* Function 3 */
-DWORD RLockServiceDatabase(
- SC_RPC_HANDLE hSCManager,
- LPSC_RPC_LOCK lpLock)
+/* Internal recursive function */
+/* Need to search for every dependency on every service */
+static DWORD
+Int_EnumDependentServicesW(HKEY hServicesKey,
+ PSERVICE lpService,
+ DWORD dwServiceState,
+ PSERVICE *lpServices,
+ LPDWORD pcbBytesNeeded,
+ LPDWORD lpServicesReturned)
{
- PMANAGER_HANDLE hMgr;
-
- DPRINT("RLockServiceDatabase() called\n");
-
- *lpLock = 0;
+ DWORD dwError = ERROR_SUCCESS;
+ WCHAR szNameBuf[MAX_PATH];
+ WCHAR szValueBuf[MAX_PATH];
+ WCHAR *lpszNameBuf = szNameBuf;
+ WCHAR *lpszValueBuf = szValueBuf;
+ DWORD dwSize;
+ DWORD dwNumSubKeys;
+ DWORD dwIteration;
+ PSERVICE lpCurrentService;
+ HKEY hServiceEnumKey;
+ DWORD dwCurrentServiceState = SERVICE_ACTIVE;
+ DWORD dwDependServiceStrPtr = 0;
+ DWORD dwRequiredSize = 0;
- hMgr = ScmGetServiceManagerFromHandle(hSCManager);
- if (hMgr == NULL)
+ /* Get the number of service keys */
+ dwError = RegQueryInfoKeyW(hServicesKey,
+ NULL,
+ NULL,
+ NULL,
+ &dwNumSubKeys,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+ if (dwError != ERROR_SUCCESS)
{
- DPRINT1("Invalid service manager handle!\n");
- return ERROR_INVALID_HANDLE;
+ DPRINT("ERROR! Unable to get number of services keys.\n");
+ return dwError;
}
- if (!RtlAreAllAccessesGranted(hMgr->Handle.DesiredAccess,
- SC_MANAGER_LOCK))
- return ERROR_ACCESS_DENIED;
-
-// return ScmLockDatabase(0, hMgr->0xC, hLock);
-
- /* FIXME: Lock the database */
- *lpLock = (SC_RPC_LOCK)0x12345678; /* Dummy! */
+ /* Iterate the service keys to see if another service depends on the this service */
+ for (dwIteration = 0; dwIteration < dwNumSubKeys; dwIteration++)
+ {
+ dwSize = MAX_PATH;
+ dwError = RegEnumKeyExW(hServicesKey,
+ dwIteration,
+ lpszNameBuf,
+ &dwSize,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+ if (dwError != ERROR_SUCCESS)
+ return dwError;
- return ERROR_SUCCESS;
-}
+ /* Open the Service key */
+ dwError = RegOpenKeyExW(hServicesKey,
+ lpszNameBuf,
+ 0,
+ KEY_READ,
+ &hServiceEnumKey);
+ if (dwError != ERROR_SUCCESS)
+ return dwError;
+ dwSize = MAX_PATH;
-/* Function 4 */
-DWORD RQueryServiceObjectSecurity(
- SC_RPC_HANDLE hService,
- SECURITY_INFORMATION dwSecurityInformation,
- LPBYTE lpSecurityDescriptor,
- DWORD cbBufSize,
- LPBOUNDED_DWORD_256K pcbBytesNeeded)
-{
- PSERVICE_HANDLE hSvc;
- PSERVICE lpService;
- ULONG DesiredAccess = 0;
- NTSTATUS Status;
- DWORD dwBytesNeeded;
- DWORD dwError;
+ /* Check for the DependOnService Value */
+ dwError = RegQueryValueExW(hServiceEnumKey,
+ L"DependOnService",
+ NULL,
+ NULL,
+ (LPBYTE)lpszValueBuf,
+ &dwSize);
+ /* FIXME: Handle load order. */
- SECURITY_DESCRIPTOR ObjectDescriptor;
+ /* If the service found has a DependOnService value */
+ if (dwError == ERROR_SUCCESS)
+ {
+ dwDependServiceStrPtr = 0;
- DPRINT("RQueryServiceObjectSecurity() called\n");
+ /* Can be more than one Dependencies in the DependOnService string */
+ while (wcslen(lpszValueBuf + dwDependServiceStrPtr) > 0)
+ {
+ if (_wcsicmp(lpszValueBuf + dwDependServiceStrPtr, lpService->lpServiceName) == 0)
+ {
+ /* Get the current enumed service pointer */
+ lpCurrentService = ScmGetServiceEntryByName(lpszNameBuf);
- hSvc = ScmGetServiceFromHandle(hService);
- if (hSvc == NULL)
- {
- DPRINT1("Invalid service handle!\n");
- return ERROR_INVALID_HANDLE;
- }
+ /* Check for valid Service */
+ if (!lpCurrentService)
+ {
+ /* This should never happen! */
+ DPRINT("This should not happen at this point, report to Developer\n");
+ return ERROR_NOT_FOUND;
+ }
- if (dwSecurityInformation & (DACL_SECURITY_INFORMATION |
- GROUP_SECURITY_INFORMATION |
- OWNER_SECURITY_INFORMATION))
- DesiredAccess |= READ_CONTROL;
+ /* Determine state the service is in */
+ if (lpCurrentService->Status.dwCurrentState == SERVICE_STOPPED)
+ dwCurrentServiceState = SERVICE_INACTIVE;
- if (dwSecurityInformation & SACL_SECURITY_INFORMATION)
- DesiredAccess |= ACCESS_SYSTEM_SECURITY;
+ /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
+ if ((dwCurrentServiceState == dwServiceState) ||
+ (dwServiceState == SERVICE_STATE_ALL))
+ {
+ /* Calculate the required size */
+ dwRequiredSize += sizeof(SERVICE_STATUS);
+ dwRequiredSize += ((wcslen(lpCurrentService->lpServiceName) + 1) * sizeof(WCHAR));
+ dwRequiredSize += ((wcslen(lpCurrentService->lpDisplayName) + 1) * sizeof(WCHAR));
- if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- DesiredAccess))
- {
- DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
- return ERROR_ACCESS_DENIED;
- }
+ /* Add the size for service name and display name pointers */
+ dwRequiredSize += (2 * sizeof(PVOID));
- lpService = hSvc->ServiceEntry;
- if (lpService == NULL)
- {
- DPRINT("lpService == NULL!\n");
- return ERROR_INVALID_HANDLE;
- }
+ /* increase the BytesNeeded size */
+ *pcbBytesNeeded = *pcbBytesNeeded + dwRequiredSize;
- /* FIXME: Lock the service list */
+ /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
+ comes first */
- /* hack */
- Status = RtlCreateSecurityDescriptor(&ObjectDescriptor, SECURITY_DESCRIPTOR_REVISION);
+ /* Recursive call to check for its dependencies */
+ Int_EnumDependentServicesW(hServicesKey,
+ lpCurrentService,
+ dwServiceState,
+ lpServices,
+ pcbBytesNeeded,
+ lpServicesReturned);
- Status = RtlQuerySecurityObject(&ObjectDescriptor /* lpService->lpSecurityDescriptor */,
- dwSecurityInformation,
- (PSECURITY_DESCRIPTOR)lpSecurityDescriptor,
- cbBufSize,
- &dwBytesNeeded);
+ /* If the lpServices is valid set the service pointer */
+ if (lpServices)
+ lpServices[*lpServicesReturned] = lpCurrentService;
- /* FIXME: Unlock the service list */
+ *lpServicesReturned = *lpServicesReturned + 1;
+ }
+ }
- if (NT_SUCCESS(Status))
- {
- *pcbBytesNeeded = dwBytesNeeded;
- dwError = STATUS_SUCCESS;
- }
- else if (Status == STATUS_BUFFER_TOO_SMALL)
- {
- *pcbBytesNeeded = dwBytesNeeded;
- dwError = ERROR_INSUFFICIENT_BUFFER;
- }
- else if (Status == STATUS_BAD_DESCRIPTOR_FORMAT)
- {
- dwError = ERROR_GEN_FAILURE;
- }
- else
- {
- dwError = RtlNtStatusToDosError(Status);
+ dwDependServiceStrPtr += (wcslen(lpszValueBuf + dwDependServiceStrPtr) + 1);
+ }
+ }
+ else if (*pcbBytesNeeded)
+ {
+ dwError = ERROR_SUCCESS;
+ }
+
+ RegCloseKey(hServiceEnumKey);
}
return dwError;
}
-/* Function 5 */
-DWORD RSetServiceObjectSecurity(
- SC_RPC_HANDLE hService,
- DWORD dwSecurityInformation,
- LPBYTE lpSecurityDescriptor,
- DWORD dwSecuityDescriptorSize)
+/* Function 0 */
+DWORD RCloseServiceHandle(
+ LPSC_RPC_HANDLE hSCObject)
{
- PSERVICE_HANDLE hSvc;
+ PMANAGER_HANDLE hManager;
+ PSERVICE_HANDLE hService;
PSERVICE lpService;
- ULONG DesiredAccess = 0;
- /* HANDLE hToken = NULL; */
- HKEY hServiceKey;
- /* NTSTATUS Status; */
+ HKEY hServicesKey;
DWORD dwError;
+ DWORD pcbBytesNeeded = 0;
+ DWORD dwServicesReturned = 0;
- DPRINT("RSetServiceObjectSecurity() called\n");
+ DPRINT("RCloseServiceHandle() called\n");
- hSvc = ScmGetServiceFromHandle(hService);
- if (hSvc == NULL)
- {
- DPRINT1("Invalid service handle!\n");
+ DPRINT("hSCObject = %p\n", *hSCObject);
+
+ if (*hSCObject == 0)
return ERROR_INVALID_HANDLE;
- }
- if (dwSecurityInformation == 0 ||
- dwSecurityInformation & ~(OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
- | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION))
- return ERROR_INVALID_PARAMETER;
+ hManager = ScmGetServiceManagerFromHandle(*hSCObject);
+ hService = ScmGetServiceFromHandle(*hSCObject);
- if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR)lpSecurityDescriptor))
- return ERROR_INVALID_PARAMETER;
+ if (hManager != NULL)
+ {
+ DPRINT("Found manager handle\n");
- if (dwSecurityInformation & SACL_SECURITY_INFORMATION)
- DesiredAccess |= ACCESS_SYSTEM_SECURITY;
+ /* FIXME: add handle cleanup code */
- if (dwSecurityInformation & DACL_SECURITY_INFORMATION)
- DesiredAccess |= WRITE_DAC;
+ HeapFree(GetProcessHeap(), 0, hManager);
+ hManager = NULL;
- if (dwSecurityInformation & (OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION))
- DesiredAccess |= WRITE_OWNER;
+ *hSCObject = NULL;
- if ((dwSecurityInformation & OWNER_SECURITY_INFORMATION) &&
- (((PISECURITY_DESCRIPTOR)lpSecurityDescriptor)->Owner == NULL))
- return ERROR_INVALID_PARAMETER;
+ DPRINT("RCloseServiceHandle() done\n");
+ return ERROR_SUCCESS;
+ }
+ else if (hService != NULL)
+ {
+ DPRINT("Found service handle\n");
- if ((dwSecurityInformation & GROUP_SECURITY_INFORMATION) &&
- (((PISECURITY_DESCRIPTOR)lpSecurityDescriptor)->Group == NULL))
- return ERROR_INVALID_PARAMETER;
+ /* Lock the service database exlusively */
+ ScmLockDatabaseExclusive();
- if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- DesiredAccess))
- {
- DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
- return ERROR_ACCESS_DENIED;
- }
+ /* Get the pointer to the service record */
+ lpService = hService->ServiceEntry;
- lpService = hSvc->ServiceEntry;
- if (lpService == NULL)
- {
- DPRINT("lpService == NULL!\n");
- return ERROR_INVALID_HANDLE;
- }
+ /* FIXME: add handle cleanup code */
- if (lpService->bDeleted)
- return ERROR_SERVICE_MARKED_FOR_DELETE;
+ /* Free the handle */
+ HeapFree(GetProcessHeap(), 0, hService);
+ hService = NULL;
-#if 0
- RpcImpersonateClient(NULL);
+ ASSERT(lpService->dwRefCount > 0);
- Status = NtOpenThreadToken(NtCurrentThread(),
- 8,
- TRUE,
- &hToken);
- if (!NT_SUCCESS(Status))
- return RtlNtStatusToDosError(Status);
+ lpService->dwRefCount--;
+ DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
+ lpService->dwRefCount);
- RpcRevertToSelf();
+ if (lpService->dwRefCount == 0)
+ {
+ /* If this service has been marked for deletion */
+ if (lpService->bDeleted)
+ {
+ /* Open the Services Reg key */
+ dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+ L"System\\CurrentControlSet\\Services",
+ 0,
+ KEY_SET_VALUE | KEY_READ,
+ &hServicesKey);
+ if (dwError != ERROR_SUCCESS)
+ {
+ DPRINT("Failed to open services key\n");
+ ScmUnlockDatabase();
+ return dwError;
+ }
- /* FIXME: Lock service database */
+ /* Call the internal function with NULL, just to get bytes we need */
+ Int_EnumDependentServicesW(hServicesKey,
+ lpService,
+ SERVICE_ACTIVE,
+ NULL,
+ &pcbBytesNeeded,
+ &dwServicesReturned);
- Status = RtlSetSecurityObject(dwSecurityInformation,
- (PSECURITY_DESCRIPTOR)lpSecurityDescriptor,
- &lpService->lpSecurityDescriptor,
- &ScmServiceMapping,
- hToken);
- if (!NT_SUCCESS(Status))
- {
- dwError = RtlNtStatusToDosError(Status);
- goto Done;
- }
-#endif
+ /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service */
+ if (pcbBytesNeeded)
+ {
+ DPRINT("Deletion failed due to running dependencies.\n");
+ RegCloseKey(hServicesKey);
+ ScmUnlockDatabase();
+ return ERROR_SUCCESS;
+ }
+
+ /* There are no references and no runnning dependencies,
+ it is now safe to delete the service */
+
+ /* Delete the Service Key */
+ dwError = RegDeleteKeyW(hServicesKey,
+ lpService->lpServiceName);
- dwError = ScmOpenServiceKey(lpService->lpServiceName,
- READ_CONTROL | KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
- &hServiceKey);
- if (dwError != ERROR_SUCCESS)
- goto Done;
+ RegCloseKey(hServicesKey);
- UNIMPLEMENTED;
- dwError = ERROR_SUCCESS;
-// dwError = ScmWriteSecurityDescriptor(hServiceKey,
-// lpService->lpSecurityDescriptor);
+ if (dwError != ERROR_SUCCESS)
+ {
+ DPRINT("Failed to Delete the Service Registry key\n");
+ ScmUnlockDatabase();
+ return dwError;
+ }
- RegFlushKey(hServiceKey);
- RegCloseKey(hServiceKey);
+ /* Delete the Service */
+ ScmDeleteServiceRecord(lpService);
+ }
+ }
-Done:
+ ScmUnlockDatabase();
-#if 0
- if (hToken != NULL)
- NtClose(hToken);
-#endif
+ *hSCObject = NULL;
- /* FIXME: Unlock service database */
+ DPRINT("RCloseServiceHandle() done\n");
+ return ERROR_SUCCESS;
+ }
- DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError);
+ DPRINT("Invalid handle tag (Tag %lx)\n", hManager->Handle.Tag);
- return dwError;
+ return ERROR_INVALID_HANDLE;
}
-/* Function 6 */
-DWORD RQueryServiceStatus(
+/* Function 1 */
+DWORD RControlService(
SC_RPC_HANDLE hService,
+ DWORD dwControl,
LPSERVICE_STATUS lpServiceStatus)
{
PSERVICE_HANDLE hSvc;
PSERVICE lpService;
+ ACCESS_MASK DesiredAccess;
+ DWORD dwError = ERROR_SUCCESS;
+ DWORD pcbBytesNeeded = 0;
+ DWORD dwServicesReturned = 0;
+ DWORD dwControlsAccepted;
+ DWORD dwCurrentState;
+ HKEY hServicesKey = NULL;
- DPRINT("RQueryServiceStatus() called\n");
+ DPRINT("RControlService() called\n");
if (ScmShutdown)
return ERROR_SHUTDOWN_IN_PROGRESS;
+ /* Check the service handle */
hSvc = ScmGetServiceFromHandle(hService);
if (hSvc == NULL)
{
return ERROR_INVALID_HANDLE;
}
- if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- SERVICE_QUERY_STATUS))
- {
- DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
- return ERROR_ACCESS_DENIED;
- }
-
+ /* Check the service entry point */
lpService = hSvc->ServiceEntry;
if (lpService == NULL)
{
- DPRINT("lpService == NULL!\n");
+ DPRINT1("lpService == NULL!\n");
return ERROR_INVALID_HANDLE;
}
- /* Lock the srevice database shared */
- ScmLockDatabaseShared();
+ /* Check access rights */
+ switch (dwControl)
+ {
+ case SERVICE_CONTROL_STOP:
+ DesiredAccess = SERVICE_STOP;
+ break;
- /* Return service status information */
+ case SERVICE_CONTROL_PAUSE:
+ case SERVICE_CONTROL_CONTINUE:
+ DesiredAccess = SERVICE_PAUSE_CONTINUE;
+ break;
+
+ case SERVICE_INTERROGATE:
+ DesiredAccess = SERVICE_INTERROGATE;
+ break;
+
+ default:
+ if (dwControl >= 128 && dwControl <= 255)
+ DesiredAccess = SERVICE_USER_DEFINED_CONTROL;
+ else
+ return ERROR_INVALID_PARAMETER;
+ break;
+ }
+
+ if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
+ DesiredAccess))
+ return ERROR_ACCESS_DENIED;
+
+ /* Return the current service status information */
RtlCopyMemory(lpServiceStatus,
&lpService->Status,
sizeof(SERVICE_STATUS));
- /* Unlock the service database */
- ScmUnlockDatabase();
+ if (dwControl == SERVICE_CONTROL_STOP)
+ {
+ /* Check if the service has dependencies running as windows
+ doesn't stop a service that does */
- return ERROR_SUCCESS;
-}
+ /* Open the Services Reg key */
+ dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+ L"System\\CurrentControlSet\\Services",
+ 0,
+ KEY_READ,
+ &hServicesKey);
+ if (dwError != ERROR_SUCCESS)
+ {
+ DPRINT("Failed to open services key\n");
+ return dwError;
+ }
+ /* Call the internal function with NULL, just to get bytes we need */
+ Int_EnumDependentServicesW(hServicesKey,
+ lpService,
+ SERVICE_ACTIVE,
+ NULL,
+ &pcbBytesNeeded,
+ &dwServicesReturned);
-static BOOL
-ScmIsValidServiceState(DWORD dwCurrentState)
-{
- switch (dwCurrentState)
+ RegCloseKey(hServicesKey);
+
+ /* If pcbBytesNeeded is not zero then there are services running that
+ are dependent on this service */
+ if (pcbBytesNeeded != 0)
+ {
+ DPRINT("Service has running dependencies. Failed to stop service.\n");
+ return ERROR_DEPENDENT_SERVICES_RUNNING;
+ }
+ }
+
+ if (lpService->Status.dwServiceType & SERVICE_DRIVER)
{
- case SERVICE_STOPPED:
- case SERVICE_START_PENDING:
- case SERVICE_STOP_PENDING:
- case SERVICE_RUNNING:
- case SERVICE_CONTINUE_PENDING:
- case SERVICE_PAUSE_PENDING:
- case SERVICE_PAUSED:
- return TRUE;
+ /* Send control code to the driver */
+ dwError = ScmControlDriver(lpService,
+ dwControl,
+ lpServiceStatus);
+ }
+ else
+ {
+ dwControlsAccepted = lpService->Status.dwControlsAccepted;
+ dwCurrentState = lpService->Status.dwCurrentState;
- default:
- return FALSE;
+ /* Return ERROR_SERVICE_NOT_ACTIVE if the service has not been started */
+ if (lpService->lpImage == NULL || dwCurrentState == SERVICE_STOPPED)
+ return ERROR_SERVICE_NOT_ACTIVE;
+
+ /* Check the current state before sending a control request */
+ switch (dwCurrentState)
+ {
+ case SERVICE_STOP_PENDING:
+ case SERVICE_STOPPED:
+ return ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
+
+ case SERVICE_START_PENDING:
+ switch (dwControl)
+ {
+ case SERVICE_CONTROL_STOP:
+ break;
+
+ case SERVICE_CONTROL_INTERROGATE:
+ RtlCopyMemory(lpServiceStatus,
+ &lpService->Status,
+ sizeof(SERVICE_STATUS));
+ return ERROR_SUCCESS;
+
+ default:
+ return ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
+ }
+ break;
+ }
+
+ /* Check if the control code is acceptable to the service */
+ switch (dwControl)
+ {
+ case SERVICE_CONTROL_STOP:
+ if ((dwControlsAccepted & SERVICE_ACCEPT_STOP) == 0)
+ return ERROR_INVALID_SERVICE_CONTROL;
+ break;
+
+ case SERVICE_CONTROL_PAUSE:
+ case SERVICE_CONTROL_CONTINUE:
+ if ((dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE) == 0)
+ return ERROR_INVALID_SERVICE_CONTROL;
+ break;
+ }
+
+ /* Send control code to the service */
+ dwError = ScmControlService(lpService,
+ dwControl);
+
+ /* Return service status information */
+ RtlCopyMemory(lpServiceStatus,
+ &lpService->Status,
+ sizeof(SERVICE_STATUS));
}
+
+ if ((dwError == ERROR_SUCCESS) && (pcbBytesNeeded))
+ dwError = ERROR_DEPENDENT_SERVICES_RUNNING;
+
+ return dwError;
}
-/* Function 7 */
-DWORD RSetServiceStatus(
- RPC_SERVICE_STATUS_HANDLE hServiceStatus,
- LPSERVICE_STATUS lpServiceStatus)
+/* Function 2 */
+DWORD RDeleteService(
+ SC_RPC_HANDLE hService)
{
+ PSERVICE_HANDLE hSvc;
PSERVICE lpService;
+ DWORD dwError;
- DPRINT("RSetServiceStatus() called\n");
- DPRINT("hServiceStatus = %p\n", hServiceStatus);
- DPRINT("dwServiceType = %lu\n", lpServiceStatus->dwServiceType);
- DPRINT("dwCurrentState = %lu\n", lpServiceStatus->dwCurrentState);
- DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus->dwControlsAccepted);
- DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus->dwWin32ExitCode);
- DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus->dwServiceSpecificExitCode);
- DPRINT("dwCheckPoint = %lu\n", lpServiceStatus->dwCheckPoint);
- DPRINT("dwWaitHint = %lu\n", lpServiceStatus->dwWaitHint);
+ DPRINT("RDeleteService() called\n");
- if (hServiceStatus == 0)
- {
- DPRINT("hServiceStatus == NULL!\n");
- return ERROR_INVALID_HANDLE;
- }
+ if (ScmShutdown)
+ return ERROR_SHUTDOWN_IN_PROGRESS;
- lpService = (PSERVICE)hServiceStatus;
- if (lpService == NULL)
+ hSvc = ScmGetServiceFromHandle(hService);
+ if (hSvc == NULL)
{
- DPRINT("lpService == NULL!\n");
+ DPRINT1("Invalid service handle!\n");
return ERROR_INVALID_HANDLE;
}
- /* Check current state */
- if (!ScmIsValidServiceState(lpServiceStatus->dwCurrentState))
- {
- DPRINT("Invalid service state!\n");
- return ERROR_INVALID_DATA;
- }
-
- /* Check service type */
- if (!(lpServiceStatus->dwServiceType & SERVICE_WIN32) &&
- (lpServiceStatus->dwServiceType & SERVICE_DRIVER))
- {
- DPRINT("Invalid service type!\n");
- return ERROR_INVALID_DATA;
- }
+ if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
+ DELETE))
+ return ERROR_ACCESS_DENIED;
- /* Check accepted controls */
- if (lpServiceStatus->dwControlsAccepted & ~0xFF)
+ lpService = hSvc->ServiceEntry;
+ if (lpService == NULL)
{
- DPRINT("Invalid controls accepted!\n");
- return ERROR_INVALID_DATA;
+ DPRINT("lpService == NULL!\n");
+ return ERROR_INVALID_HANDLE;
}
/* Lock the service database exclusively */
ScmLockDatabaseExclusive();
- RtlCopyMemory(&lpService->Status,
- lpServiceStatus,
- sizeof(SERVICE_STATUS));
+ if (lpService->bDeleted)
+ {
+ DPRINT("The service has already been marked for delete!\n");
+ dwError = ERROR_SERVICE_MARKED_FOR_DELETE;
+ goto Done;
+ }
+
+ /* Mark service for delete */
+ lpService->bDeleted = TRUE;
+
+ dwError = ScmMarkServiceForDelete(lpService);
+Done:;
/* Unlock the service database */
ScmUnlockDatabase();
- DPRINT("Set %S to %lu\n", lpService->lpDisplayName, lpService->Status.dwCurrentState);
- DPRINT("RSetServiceStatus() done\n");
+ DPRINT("RDeleteService() done\n");
- return ERROR_SUCCESS;
+ return dwError;
}
-/* Function 8 */
-DWORD RUnlockServiceDatabase(
- LPSC_RPC_LOCK Lock)
+/* Function 3 */
+DWORD RLockServiceDatabase(
+ SC_RPC_HANDLE hSCManager,
+ LPSC_RPC_LOCK lpLock)
{
- UNIMPLEMENTED;
- return ERROR_SUCCESS;
-}
+ PMANAGER_HANDLE hMgr;
+ DPRINT("RLockServiceDatabase() called\n");
-/* Function 9 */
-DWORD RNotifyBootConfigStatus(
- SVCCTL_HANDLEW lpMachineName,
- DWORD BootAcceptable)
-{
- DPRINT1("RNotifyBootConfigStatus(%p %lu) called\n", lpMachineName, BootAcceptable);
- return ERROR_SUCCESS;
+ *lpLock = 0;
-// UNIMPLEMENTED;
-// return ERROR_CALL_NOT_IMPLEMENTED;
-}
+ hMgr = ScmGetServiceManagerFromHandle(hSCManager);
+ if (hMgr == NULL)
+ {
+ DPRINT1("Invalid service manager handle!\n");
+ return ERROR_INVALID_HANDLE;
+ }
+ if (!RtlAreAllAccessesGranted(hMgr->Handle.DesiredAccess,
+ SC_MANAGER_LOCK))
+ return ERROR_ACCESS_DENIED;
-/* Function 10 */
-DWORD RI_ScSetServiceBitsW(
- RPC_SERVICE_STATUS_HANDLE hServiceStatus,
- DWORD dwServiceBits,
- int bSetBitsOn,
- int bUpdateImmediately,
- wchar_t *lpString)
-{
- UNIMPLEMENTED;
- return ERROR_CALL_NOT_IMPLEMENTED;
+// return ScmLockDatabase(0, hMgr->0xC, hLock);
+
+ /* FIXME: Lock the database */
+ *lpLock = (SC_RPC_LOCK)0x12345678; /* Dummy! */
+
+ return ERROR_SUCCESS;
}
-/* Function 11 */
-DWORD RChangeServiceConfigW(
+/* Function 4 */
+DWORD RQueryServiceObjectSecurity(
SC_RPC_HANDLE hService,
- DWORD dwServiceType,
- DWORD dwStartType,
- DWORD dwErrorControl,
- LPWSTR lpBinaryPathName,
- LPWSTR lpLoadOrderGroup,
- LPDWORD lpdwTagId,
- LPBYTE lpDependencies,
- DWORD dwDependSize,
- LPWSTR lpServiceStartName,
- LPBYTE lpPassword,
- DWORD dwPwSize,
- LPWSTR lpDisplayName)
+ SECURITY_INFORMATION dwSecurityInformation,
+ LPBYTE lpSecurityDescriptor,
+ DWORD cbBufSize,
+ LPBOUNDED_DWORD_256K pcbBytesNeeded)
{
- DWORD dwError = ERROR_SUCCESS;
PSERVICE_HANDLE hSvc;
- PSERVICE lpService = NULL;
- HKEY hServiceKey = NULL;
- LPWSTR lpDisplayNameW = NULL;
+ PSERVICE lpService;
+ ULONG DesiredAccess = 0;
+ NTSTATUS Status;
+ DWORD dwBytesNeeded;
+ DWORD dwError;
- DPRINT("RChangeServiceConfigW() called\n");
- DPRINT("dwServiceType = %lu\n", dwServiceType);
- DPRINT("dwStartType = %lu\n", dwStartType);
- DPRINT("dwErrorControl = %lu\n", dwErrorControl);
- DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName);
- DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup);
- DPRINT("lpDisplayName = %S\n", lpDisplayName);
- if (ScmShutdown)
- return ERROR_SHUTDOWN_IN_PROGRESS;
+ SECURITY_DESCRIPTOR ObjectDescriptor;
+
+ DPRINT("RQueryServiceObjectSecurity() called\n");
hSvc = ScmGetServiceFromHandle(hService);
if (hSvc == NULL)
return ERROR_INVALID_HANDLE;
}
+ if (dwSecurityInformation & (DACL_SECURITY_INFORMATION |
+ GROUP_SECURITY_INFORMATION |
+ OWNER_SECURITY_INFORMATION))
+ DesiredAccess |= READ_CONTROL;
+
+ if (dwSecurityInformation & SACL_SECURITY_INFORMATION)
+ DesiredAccess |= ACCESS_SYSTEM_SECURITY;
+
if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
- SERVICE_CHANGE_CONFIG))
+ DesiredAccess))
{
DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
return ERROR_ACCESS_DENIED;
return ERROR_INVALID_HANDLE;
}
- /* Lock the service database exclusively */
- ScmLockDatabaseExclusive();
+ /* Lock the service database */
+ ScmLockDatabaseShared();
- if (lpService->bDeleted)
- {
- DPRINT("The service has already been marked for delete!\n");
- dwError = ERROR_SERVICE_MARKED_FOR_DELETE;
- goto done;
- }
- /* Open the service key */
- dwError = ScmOpenServiceKey(lpService->szServiceName,
- KEY_SET_VALUE,
- &hServiceKey);
- if (dwError != ERROR_SUCCESS)
- goto done;
+ /* hack */
+ Status = RtlCreateSecurityDescriptor(&ObjectDescriptor, SECURITY_DESCRIPTOR_REVISION);
- /* Write service data to the registry */
- /* Set the display name */
- if (lpDisplayName != NULL && *lpDisplayName != 0)
+ Status = RtlQuerySecurityObject(&ObjectDescriptor /* lpService->lpSecurityDescriptor */,
+ dwSecurityInformation,
+ (PSECURITY_DESCRIPTOR)lpSecurityDescriptor,
+ cbBufSize,
+ &dwBytesNeeded);
+
+ /* Unlock the service database */
+ ScmUnlockDatabase();
+
+ if (NT_SUCCESS(Status))
{
- RegSetValueExW(hServiceKey,
- L"DisplayName",
- 0,
- REG_SZ,
- (LPBYTE)lpDisplayName,
- (wcslen(lpDisplayName) + 1) * sizeof(WCHAR));
+ *pcbBytesNeeded = dwBytesNeeded;
+ dwError = STATUS_SUCCESS;
+ }
+ else if (Status == STATUS_BUFFER_TOO_SMALL)
+ {
+ *pcbBytesNeeded = dwBytesNeeded;
+ dwError = ERROR_INSUFFICIENT_BUFFER;
+ }
+ else if (Status == STATUS_BAD_DESCRIPTOR_FORMAT)
+ {
+ dwError = ERROR_GEN_FAILURE;
+ }
+ else
+ {
+ dwError = RtlNtStatusToDosError(Status);
+ }
- /* Update the display name */
- lpDisplayNameW = (LPWSTR)HeapAlloc(GetProcessHeap(),
- 0,
- (wcslen(lpDisplayName) + 1) * sizeof(WCHAR));
- if (lpDisplayNameW == NULL)
- {
- dwError = ERROR_NOT_ENOUGH_MEMORY;
- goto done;
- }
+ return dwError;
+}
- if (lpService->lpDisplayName != lpService->lpServiceName)
- HeapFree(GetProcessHeap(), 0, lpService->lpDisplayName);
- lpService->lpDisplayName = lpDisplayNameW;
- }
+/* Function 5 */
+DWORD RSetServiceObjectSecurity(
+ SC_RPC_HANDLE hService,
+ DWORD dwSecurityInformation,
+ LPBYTE lpSecurityDescriptor,
+ DWORD dwSecuityDescriptorSize)
+{
+ PSERVICE_HANDLE hSvc;
+ PSERVICE lpService;
+ ULONG DesiredAccess = 0;
+ /* HANDLE hToken = NULL; */
+ HKEY hServiceKey;
+ /* NTSTATUS Status; */
+ DWORD dwError;
- if (dwServiceType != SERVICE_NO_CHANGE)
- {
- /* Set the service type */
- dwError = RegSetValueExW(hServiceKey,
- L"Type",
- 0,
- REG_DWORD,
- (LPBYTE)&dwServiceType,
- sizeof(DWORD));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ DPRINT("RSetServiceObjectSecurity() called\n");
- lpService->Status.dwServiceType = dwServiceType;
+ hSvc = ScmGetServiceFromHandle(hService);
+ if (hSvc == NULL)
+ {
+ DPRINT1("Invalid service handle!\n");
+ return ERROR_INVALID_HANDLE;
}
- if (dwStartType != SERVICE_NO_CHANGE)
- {
- /* Set the start value */
- dwError = RegSetValueExW(hServiceKey,
- L"Start",
- 0,
- REG_DWORD,
- (LPBYTE)&dwStartType,
- sizeof(DWORD));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ if (dwSecurityInformation == 0 ||
+ dwSecurityInformation & ~(OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
+ | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION))
+ return ERROR_INVALID_PARAMETER;
- lpService->dwStartType = dwStartType;
- }
+ if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR)lpSecurityDescriptor))
+ return ERROR_INVALID_PARAMETER;
- if (dwErrorControl != SERVICE_NO_CHANGE)
- {
- /* Set the error control value */
- dwError = RegSetValueExW(hServiceKey,
- L"ErrorControl",
- 0,
- REG_DWORD,
- (LPBYTE)&dwErrorControl,
- sizeof(DWORD));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ if (dwSecurityInformation & SACL_SECURITY_INFORMATION)
+ DesiredAccess |= ACCESS_SYSTEM_SECURITY;
- lpService->dwErrorControl = dwErrorControl;
- }
+ if (dwSecurityInformation & DACL_SECURITY_INFORMATION)
+ DesiredAccess |= WRITE_DAC;
-#if 0
- /* FIXME: set the new ImagePath value */
+ if (dwSecurityInformation & (OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION))
+ DesiredAccess |= WRITE_OWNER;
- /* Set the image path */
- if (dwServiceType & SERVICE_WIN32)
+ if ((dwSecurityInformation & OWNER_SECURITY_INFORMATION) &&
+ (((PISECURITY_DESCRIPTOR)lpSecurityDescriptor)->Owner == NULL))
+ return ERROR_INVALID_PARAMETER;
+
+ if ((dwSecurityInformation & GROUP_SECURITY_INFORMATION) &&
+ (((PISECURITY_DESCRIPTOR)lpSecurityDescriptor)->Group == NULL))
+ return ERROR_INVALID_PARAMETER;
+
+ if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
+ DesiredAccess))
{
- if (lpBinaryPathName != NULL && *lpBinaryPathName != 0)
- {
- dwError = RegSetValueExW(hServiceKey,
- L"ImagePath",
- 0,
- REG_EXPAND_SZ,
- (LPBYTE)lpBinaryPathName,
- (wcslen(lpBinaryPathName) + 1) * sizeof(WCHAR));
- if (dwError != ERROR_SUCCESS)
- goto done;
- }
+ DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
+ return ERROR_ACCESS_DENIED;
}
- else if (dwServiceType & SERVICE_DRIVER)
+
+ lpService = hSvc->ServiceEntry;
+ if (lpService == NULL)
{
- if (lpImagePath != NULL && *lpImagePath != 0)
- {
- dwError = RegSetValueExW(hServiceKey,
- L"ImagePath",
- 0,
- REG_EXPAND_SZ,
- (LPBYTE)lpImagePath,
- (wcslen(lpImagePath) + 1) *sizeof(WCHAR));
- if (dwError != ERROR_SUCCESS)
- goto done;
- }
+ DPRINT("lpService == NULL!\n");
+ return ERROR_INVALID_HANDLE;
}
-#endif
- /* Set the group name */
- if (lpLoadOrderGroup != NULL && *lpLoadOrderGroup != 0)
- {
- dwError = RegSetValueExW(hServiceKey,
- L"Group",
- 0,
- REG_SZ,
- (LPBYTE)lpLoadOrderGroup,
- (wcslen(lpLoadOrderGroup) + 1) * sizeof(WCHAR));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ if (lpService->bDeleted)
+ return ERROR_SERVICE_MARKED_FOR_DELETE;
- dwError = ScmSetServiceGroup(lpService,
- lpLoadOrderGroup);
- if (dwError != ERROR_SUCCESS)
- goto done;
- }
+#if 0
+ RpcImpersonateClient(NULL);
- if (lpdwTagId != NULL)
- {
- dwError = ScmAssignNewTag(lpService);
- if (dwError != ERROR_SUCCESS)
- goto done;
+ Status = NtOpenThreadToken(NtCurrentThread(),
+ 8,
+ TRUE,
+ &hToken);
+ if (!NT_SUCCESS(Status))
+ return RtlNtStatusToDosError(Status);
- dwError = RegSetValueExW(hServiceKey,
- L"Tag",
- 0,
- REG_DWORD,
- (LPBYTE)&lpService->dwTag,
- sizeof(DWORD));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ RpcRevertToSelf();
+#endif
- *lpdwTagId = lpService->dwTag;
- }
+ /* Lock the service database exclusive */
+ ScmLockDatabaseExclusive();
- /* Write dependencies */
- if (lpDependencies != NULL && *lpDependencies != 0)
+#if 0
+ Status = RtlSetSecurityObject(dwSecurityInformation,
+ (PSECURITY_DESCRIPTOR)lpSecurityDescriptor,
+ &lpService->lpSecurityDescriptor,
+ &ScmServiceMapping,
+ hToken);
+ if (!NT_SUCCESS(Status))
{
- dwError = ScmWriteDependencies(hServiceKey,
- (LPWSTR)lpDependencies,
- dwDependSize);
- if (dwError != ERROR_SUCCESS)
- goto done;
+ dwError = RtlNtStatusToDosError(Status);
+ goto Done;
}
+#endif
- if (lpPassword != NULL)
- {
- /* FIXME: Write password */
- }
+ dwError = ScmOpenServiceKey(lpService->lpServiceName,
+ READ_CONTROL | KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
+ &hServiceKey);
+ if (dwError != ERROR_SUCCESS)
+ goto Done;
-done:
- if (hServiceKey != NULL)
- RegCloseKey(hServiceKey);
+ UNIMPLEMENTED;
+ dwError = ERROR_SUCCESS;
+// dwError = ScmWriteSecurityDescriptor(hServiceKey,
+// lpService->lpSecurityDescriptor);
- /* Unlock the service database */
+ RegFlushKey(hServiceKey);
+ RegCloseKey(hServiceKey);
+
+Done:
+
+#if 0
+ if (hToken != NULL)
+ NtClose(hToken);
+#endif
+
+ /* Unlock service database */
ScmUnlockDatabase();
- DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError);
+ DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError);
return dwError;
}
-/* Create a path suitable for the bootloader out of the full path */
-DWORD
-ScmConvertToBootPathName(wchar_t *CanonName, wchar_t **RelativeName)
+/* Function 6 */
+DWORD RQueryServiceStatus(
+ SC_RPC_HANDLE hService,
+ LPSERVICE_STATUS lpServiceStatus)
{
- DWORD ServiceNameLen, BufferSize, ExpandedLen;
- WCHAR Dest;
- WCHAR *Expanded;
- UNICODE_STRING NtPathName, SystemRoot, LinkTarget;
- OBJECT_ATTRIBUTES ObjectAttributes;
- NTSTATUS Status;
- HANDLE SymbolicLinkHandle;
+ PSERVICE_HANDLE hSvc;
+ PSERVICE lpService;
- DPRINT("ScmConvertToBootPathName %S\n", CanonName);
+ DPRINT("RQueryServiceStatus() called\n");
- ServiceNameLen = wcslen(CanonName);
+ if (ScmShutdown)
+ return ERROR_SHUTDOWN_IN_PROGRESS;
- /* First check, if it's already good */
- if (ServiceNameLen > 12 &&
- !_wcsnicmp(L"\\SystemRoot\\", CanonName, 12))
+ hSvc = ScmGetServiceFromHandle(hService);
+ if (hSvc == NULL)
{
- *RelativeName = LocalAlloc(LMEM_ZEROINIT, ServiceNameLen * sizeof(WCHAR) + sizeof(WCHAR));
- if (*RelativeName == NULL)
- {
- DPRINT("Error allocating memory for boot driver name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
- }
-
- /* Copy it */
- wcscpy(*RelativeName, CanonName);
+ DPRINT1("Invalid service handle!\n");
+ return ERROR_INVALID_HANDLE;
+ }
- DPRINT("Bootdriver name %S\n", *RelativeName);
- return ERROR_SUCCESS;
+ if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
+ SERVICE_QUERY_STATUS))
+ {
+ DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
+ return ERROR_ACCESS_DENIED;
}
- /* If it has %SystemRoot% prefix, substitute it to \System*/
- if (ServiceNameLen > 13 &&
- !_wcsnicmp(L"%SystemRoot%\\", CanonName, 13))
+ lpService = hSvc->ServiceEntry;
+ if (lpService == NULL)
{
- /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
- *RelativeName = LocalAlloc(LMEM_ZEROINIT, ServiceNameLen * sizeof(WCHAR));
+ DPRINT("lpService == NULL!\n");
+ return ERROR_INVALID_HANDLE;
+ }
- if (*RelativeName == NULL)
- {
- DPRINT("Error allocating memory for boot driver name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+ /* Lock the service database shared */
+ ScmLockDatabaseShared();
- /* Copy it */
- wcscpy(*RelativeName, L"\\SystemRoot\\");
- wcscat(*RelativeName, CanonName + 13);
+ /* Return service status information */
+ RtlCopyMemory(lpServiceStatus,
+ &lpService->Status,
+ sizeof(SERVICE_STATUS));
- DPRINT("Bootdriver name %S\n", *RelativeName);
- return ERROR_SUCCESS;
+ /* Unlock the service database */
+ ScmUnlockDatabase();
+
+ return ERROR_SUCCESS;
+}
+
+
+static BOOL
+ScmIsValidServiceState(DWORD dwCurrentState)
+{
+ switch (dwCurrentState)
+ {
+ case SERVICE_STOPPED:
+ case SERVICE_START_PENDING:
+ case SERVICE_STOP_PENDING:
+ case SERVICE_RUNNING:
+ case SERVICE_CONTINUE_PENDING:
+ case SERVICE_PAUSE_PENDING:
+ case SERVICE_PAUSED:
+ return TRUE;
+
+ default:
+ return FALSE;
}
+}
- /* Get buffer size needed for expanding env strings */
- BufferSize = ExpandEnvironmentStringsW(L"%SystemRoot%\\", &Dest, 1);
- if (BufferSize <= 1)
+/* Function 7 */
+DWORD RSetServiceStatus(
+ RPC_SERVICE_STATUS_HANDLE hServiceStatus,
+ LPSERVICE_STATUS lpServiceStatus)
+{
+ PSERVICE lpService;
+
+ DPRINT("RSetServiceStatus() called\n");
+ DPRINT("hServiceStatus = %p\n", hServiceStatus);
+ DPRINT("dwServiceType = %lu\n", lpServiceStatus->dwServiceType);
+ DPRINT("dwCurrentState = %lu\n", lpServiceStatus->dwCurrentState);
+ DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus->dwControlsAccepted);
+ DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus->dwWin32ExitCode);
+ DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus->dwServiceSpecificExitCode);
+ DPRINT("dwCheckPoint = %lu\n", lpServiceStatus->dwCheckPoint);
+ DPRINT("dwWaitHint = %lu\n", lpServiceStatus->dwWaitHint);
+
+ if (hServiceStatus == 0)
{
- DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
- return ERROR_INVALID_ENVIRONMENT;
+ DPRINT("hServiceStatus == NULL!\n");
+ return ERROR_INVALID_HANDLE;
}
- /* Allocate memory, since the size is known now */
- Expanded = LocalAlloc(LMEM_ZEROINIT, BufferSize * sizeof(WCHAR) + sizeof(WCHAR));
- if (!Expanded)
+ lpService = (PSERVICE)hServiceStatus;
+ if (lpService == NULL)
{
- DPRINT("Error allocating memory for boot driver name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
+ DPRINT("lpService == NULL!\n");
+ return ERROR_INVALID_HANDLE;
}
- /* Expand it */
- if (ExpandEnvironmentStringsW(L"%SystemRoot%\\", Expanded, BufferSize) >
- BufferSize)
+ /* Check current state */
+ if (!ScmIsValidServiceState(lpServiceStatus->dwCurrentState))
{
- DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
- LocalFree(Expanded);
- return ERROR_NOT_ENOUGH_MEMORY;
+ DPRINT("Invalid service state!\n");
+ return ERROR_INVALID_DATA;
}
- /* Convert to NY-style path */
- if (!RtlDosPathNameToNtPathName_U(Expanded, &NtPathName, NULL, NULL))
+ /* Check service type */
+ if (!(lpServiceStatus->dwServiceType & SERVICE_WIN32) &&
+ (lpServiceStatus->dwServiceType & SERVICE_DRIVER))
{
- DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
- return ERROR_INVALID_ENVIRONMENT;
+ DPRINT("Invalid service type!\n");
+ return ERROR_INVALID_DATA;
}
- DPRINT("Converted to NT-style %wZ\n", &NtPathName);
-
- /* No need to keep the dos-path anymore */
- LocalFree(Expanded);
-
- /* Copy it to the allocated place */
- Expanded = LocalAlloc(LMEM_ZEROINIT, NtPathName.Length + sizeof(WCHAR));
- if (!Expanded)
+ /* Check accepted controls */
+ if (lpServiceStatus->dwControlsAccepted & ~0xFF)
{
- DPRINT("Error allocating memory for boot driver name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
+ DPRINT("Invalid controls accepted!\n");
+ return ERROR_INVALID_DATA;
}
- ExpandedLen = NtPathName.Length / sizeof(WCHAR);
- wcsncpy(Expanded, NtPathName.Buffer, ExpandedLen);
- Expanded[ExpandedLen] = 0;
+ /* Lock the service database exclusively */
+ ScmLockDatabaseExclusive();
- if (ServiceNameLen > ExpandedLen &&
- !_wcsnicmp(Expanded, CanonName, ExpandedLen))
- {
- /* Only \SystemRoot\ is missing */
- *RelativeName = LocalAlloc(LMEM_ZEROINIT,
- (ServiceNameLen - ExpandedLen) * sizeof(WCHAR) + 13*sizeof(WCHAR));
- if (*RelativeName == NULL)
- {
- DPRINT("Error allocating memory for boot driver name!\n");
- LocalFree(Expanded);
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+ RtlCopyMemory(&lpService->Status,
+ lpServiceStatus,
+ sizeof(SERVICE_STATUS));
- wcscpy(*RelativeName, L"\\SystemRoot\\");
- wcscat(*RelativeName, CanonName + ExpandedLen);
+ /* Unlock the service database */
+ ScmUnlockDatabase();
- RtlFreeUnicodeString(&NtPathName);
- return ERROR_SUCCESS;
- }
+ DPRINT("Set %S to %lu\n", lpService->lpDisplayName, lpService->Status.dwCurrentState);
+ DPRINT("RSetServiceStatus() done\n");
- /* The most complex case starts here */
- RtlInitUnicodeString(&SystemRoot, L"\\SystemRoot");
- InitializeObjectAttributes(&ObjectAttributes,
- &SystemRoot,
- OBJ_CASE_INSENSITIVE,
- NULL,
- NULL);
+ return ERROR_SUCCESS;
+}
- /* Open this symlink */
- Status = NtOpenSymbolicLinkObject(&SymbolicLinkHandle, SYMBOLIC_LINK_QUERY, &ObjectAttributes);
- if (NT_SUCCESS(Status))
- {
- LinkTarget.Length = 0;
- LinkTarget.MaximumLength = 0;
+/* Function 8 */
+DWORD RUnlockServiceDatabase(
+ LPSC_RPC_LOCK Lock)
+{
+ UNIMPLEMENTED;
+ return ERROR_SUCCESS;
+}
- DPRINT("Opened symbolic link object\n");
- Status = NtQuerySymbolicLinkObject(SymbolicLinkHandle, &LinkTarget, &BufferSize);
- if (NT_SUCCESS(Status) || Status == STATUS_BUFFER_TOO_SMALL)
- {
- /* Check if required buffer size is sane */
- if (BufferSize > 0xFFFD)
- {
- DPRINT("Too large buffer required\n");
- *RelativeName = 0;
+/* Function 9 */
+DWORD RNotifyBootConfigStatus(
+ SVCCTL_HANDLEW lpMachineName,
+ DWORD BootAcceptable)
+{
+ DPRINT1("RNotifyBootConfigStatus(%p %lu) called\n", lpMachineName, BootAcceptable);
+ return ERROR_SUCCESS;
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+// UNIMPLEMENTED;
+// return ERROR_CALL_NOT_IMPLEMENTED;
+}
- /* Alloc the string */
- LinkTarget.Buffer = LocalAlloc(LMEM_ZEROINIT, BufferSize + sizeof(WCHAR));
- if (!LinkTarget.Buffer)
- {
- DPRINT("Unable to alloc buffer\n");
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- return ERROR_NOT_ENOUGH_MEMORY;
- }
- /* Do a real query now */
- LinkTarget.Length = (USHORT)BufferSize;
- LinkTarget.MaximumLength = LinkTarget.Length + sizeof(WCHAR);
+/* Function 10 */
+DWORD RI_ScSetServiceBitsW(
+ RPC_SERVICE_STATUS_HANDLE hServiceStatus,
+ DWORD dwServiceBits,
+ int bSetBitsOn,
+ int bUpdateImmediately,
+ wchar_t *lpString)
+{
+ UNIMPLEMENTED;
+ return ERROR_CALL_NOT_IMPLEMENTED;
+}
- Status = NtQuerySymbolicLinkObject(SymbolicLinkHandle, &LinkTarget, &BufferSize);
- if (NT_SUCCESS(Status))
- {
- DPRINT("LinkTarget: %wZ\n", &LinkTarget);
- ExpandedLen = LinkTarget.Length / sizeof(WCHAR);
- if ((ServiceNameLen > ExpandedLen) &&
- !_wcsnicmp(LinkTarget.Buffer, CanonName, ExpandedLen))
- {
- *RelativeName = LocalAlloc(LMEM_ZEROINIT,
- (ServiceNameLen - ExpandedLen) * sizeof(WCHAR) + 13*sizeof(WCHAR));
+/* Function 11 */
+DWORD RChangeServiceConfigW(
+ SC_RPC_HANDLE hService,
+ DWORD dwServiceType,
+ DWORD dwStartType,
+ DWORD dwErrorControl,
+ LPWSTR lpBinaryPathName,
+ LPWSTR lpLoadOrderGroup,
+ LPDWORD lpdwTagId,
+ LPBYTE lpDependencies,
+ DWORD dwDependSize,
+ LPWSTR lpServiceStartName,
+ LPBYTE lpPassword,
+ DWORD dwPwSize,
+ LPWSTR lpDisplayName)
+{
+ DWORD dwError = ERROR_SUCCESS;
+ PSERVICE_HANDLE hSvc;
+ PSERVICE lpService = NULL;
+ HKEY hServiceKey = NULL;
+ LPWSTR lpDisplayNameW = NULL;
+ LPWSTR lpImagePathW = NULL;
- if (*RelativeName == NULL)
- {
- DPRINT("Unable to alloc buffer\n");
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- RtlFreeUnicodeString(&NtPathName);
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+ DPRINT("RChangeServiceConfigW() called\n");
+ DPRINT("dwServiceType = %lu\n", dwServiceType);
+ DPRINT("dwStartType = %lu\n", dwStartType);
+ DPRINT("dwErrorControl = %lu\n", dwErrorControl);
+ DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName);
+ DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup);
+ DPRINT("lpDisplayName = %S\n", lpDisplayName);
- /* Copy it over, substituting the first part
- with SystemRoot */
- wcscpy(*RelativeName, L"\\SystemRoot\\");
- wcscat(*RelativeName, CanonName+ExpandedLen+1);
+ if (ScmShutdown)
+ return ERROR_SHUTDOWN_IN_PROGRESS;
- /* Cleanup */
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- RtlFreeUnicodeString(&NtPathName);
+ hSvc = ScmGetServiceFromHandle(hService);
+ if (hSvc == NULL)
+ {
+ DPRINT1("Invalid service handle!\n");
+ return ERROR_INVALID_HANDLE;
+ }
- /* Return success */
- return ERROR_SUCCESS;
- }
- else
- {
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- RtlFreeUnicodeString(&NtPathName);
- return ERROR_INVALID_PARAMETER;
- }
- }
- else
- {
- DPRINT("Error, Status = %08X\n", Status);
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- RtlFreeUnicodeString(&NtPathName);
- return ERROR_INVALID_PARAMETER;
- }
- }
- else
- {
- DPRINT("Error, Status = %08X\n", Status);
- if (SymbolicLinkHandle) NtClose(SymbolicLinkHandle);
- LocalFree(Expanded);
- RtlFreeUnicodeString(&NtPathName);
- return ERROR_INVALID_PARAMETER;
- }
+ if (!RtlAreAllAccessesGranted(hSvc->Handle.DesiredAccess,
+ SERVICE_CHANGE_CONFIG))
+ {
+ DPRINT("Insufficient access rights! 0x%lx\n", hSvc->Handle.DesiredAccess);
+ return ERROR_ACCESS_DENIED;
}
- else
+
+ lpService = hSvc->ServiceEntry;
+ if (lpService == NULL)
{
- DPRINT("Error, Status = %08X\n", Status);
- LocalFree(Expanded);
- return ERROR_INVALID_PARAMETER;
+ DPRINT("lpService == NULL!\n");
+ return ERROR_INVALID_HANDLE;
}
- /* Failure */
- *RelativeName = NULL;
- return ERROR_INVALID_PARAMETER;
-}
+ /* Lock the service database exclusively */
+ ScmLockDatabaseExclusive();
-DWORD
-ScmCanonDriverImagePath(DWORD dwStartType,
- const wchar_t *lpServiceName,
- wchar_t **lpCanonName)
-{
- DWORD ServiceNameLen, Result;
- UNICODE_STRING NtServiceName;
- WCHAR *RelativeName;
- const WCHAR *SourceName = lpServiceName;
+ if (lpService->bDeleted)
+ {
+ DPRINT("The service has already been marked for delete!\n");
+ dwError = ERROR_SERVICE_MARKED_FOR_DELETE;
+ goto done;
+ }
- /* Calculate the length of the service's name */
- ServiceNameLen = wcslen(lpServiceName);
+ /* Open the service key */
+ dwError = ScmOpenServiceKey(lpService->szServiceName,
+ KEY_SET_VALUE,
+ &hServiceKey);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- /* 12 is wcslen(L"\\SystemRoot\\") */
- if (ServiceNameLen > 12 &&
- !_wcsnicmp(L"\\SystemRoot\\", lpServiceName, 12))
+ /* Write service data to the registry */
+ /* Set the display name */
+ if (lpDisplayName != NULL && *lpDisplayName != 0)
{
- /* SystemRoot prefix is already included */
-
- *lpCanonName = LocalAlloc(LMEM_ZEROINIT, ServiceNameLen * sizeof(WCHAR) + sizeof(WCHAR));
+ RegSetValueExW(hServiceKey,
+ L"DisplayName",
+ 0,
+ REG_SZ,
+ (LPBYTE)lpDisplayName,
+ (wcslen(lpDisplayName) + 1) * sizeof(WCHAR));
- if (*lpCanonName == NULL)
+ /* Update the display name */
+ lpDisplayNameW = HeapAlloc(GetProcessHeap(),
+ 0,
+ (wcslen(lpDisplayName) + 1) * sizeof(WCHAR));
+ if (lpDisplayNameW == NULL)
{
- DPRINT("Error allocating memory for canonized service name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
+ dwError = ERROR_NOT_ENOUGH_MEMORY;
+ goto done;
}
- /* If it's a boot-time driver, it must be systemroot relative */
- if (dwStartType == SERVICE_BOOT_START)
- SourceName += 12;
-
- /* Copy it */
- wcscpy(*lpCanonName, SourceName);
+ if (lpService->lpDisplayName != lpService->lpServiceName)
+ HeapFree(GetProcessHeap(), 0, lpService->lpDisplayName);
- DPRINT("Canonicalized name %S\n", *lpCanonName);
- return NO_ERROR;
+ lpService->lpDisplayName = lpDisplayNameW;
}
- /* Check if it has %SystemRoot% (len=13) */
- if (ServiceNameLen > 13 &&
- !_wcsnicmp(L"%%SystemRoot%%\\", lpServiceName, 13))
+ if (dwServiceType != SERVICE_NO_CHANGE)
{
- /* Substitute %SystemRoot% with \\SystemRoot\\ */
- *lpCanonName = LocalAlloc(LMEM_ZEROINIT, ServiceNameLen * sizeof(WCHAR) + sizeof(WCHAR));
+ /* Set the service type */
+ dwError = RegSetValueExW(hServiceKey,
+ L"Type",
+ 0,
+ REG_DWORD,
+ (LPBYTE)&dwServiceType,
+ sizeof(DWORD));
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- if (*lpCanonName == NULL)
- {
- DPRINT("Error allocating memory for canonized service name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+ lpService->Status.dwServiceType = dwServiceType;
+ }
- /* If it's a boot-time driver, it must be systemroot relative */
- if (dwStartType == SERVICE_BOOT_START)
- wcscpy(*lpCanonName, L"\\SystemRoot\\");
+ if (dwStartType != SERVICE_NO_CHANGE)
+ {
+ /* Set the start value */
+ dwError = RegSetValueExW(hServiceKey,
+ L"Start",
+ 0,
+ REG_DWORD,
+ (LPBYTE)&dwStartType,
+ sizeof(DWORD));
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- wcscat(*lpCanonName, lpServiceName + 13);
+ lpService->dwStartType = dwStartType;
+ }
- DPRINT("Canonicalized name %S\n", *lpCanonName);
- return NO_ERROR;
+ if (dwErrorControl != SERVICE_NO_CHANGE)
+ {
+ /* Set the error control value */
+ dwError = RegSetValueExW(hServiceKey,
+ L"ErrorControl",
+ 0,
+ REG_DWORD,
+ (LPBYTE)&dwErrorControl,
+ sizeof(DWORD));
+ if (dwError != ERROR_SUCCESS)
+ goto done;
+
+ lpService->dwErrorControl = dwErrorControl;
}
- /* Check if it's a relative path name */
- if (lpServiceName[0] != L'\\' && lpServiceName[1] != L':')
+ if (lpBinaryPathName != NULL && *lpBinaryPathName != 0)
{
- *lpCanonName = LocalAlloc(LMEM_ZEROINIT, ServiceNameLen * sizeof(WCHAR) + sizeof(WCHAR));
+ /* Set the image path */
+ lpImagePathW = lpBinaryPathName;
- if (*lpCanonName == NULL)
+ if (lpService->Status.dwServiceType & SERVICE_DRIVER)
{
- DPRINT("Error allocating memory for canonized service name!\n");
- return ERROR_NOT_ENOUGH_MEMORY;
+ dwError = ScmCanonDriverImagePath(lpService->dwStartType,
+ lpBinaryPathName,
+ &lpImagePathW);
+
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
- /* Just copy it over without changing */
- wcscpy(*lpCanonName, lpServiceName);
+ dwError = RegSetValueExW(hServiceKey,
+ L"ImagePath",
+ 0,
+ REG_EXPAND_SZ,
+ (LPBYTE)lpImagePathW,
+ (wcslen(lpImagePathW) + 1) * sizeof(WCHAR));
- return NO_ERROR;
+ if (lpImagePathW != lpBinaryPathName)
+ HeapFree(GetProcessHeap(), 0, lpImagePathW);
+
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
- /* It seems to be a DOS path, convert it */
- if (!RtlDosPathNameToNtPathName_U(lpServiceName, &NtServiceName, NULL, NULL))
+ /* Set the group name */
+ if (lpLoadOrderGroup != NULL && *lpLoadOrderGroup != 0)
{
- DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
- return ERROR_INVALID_PARAMETER;
- }
+ dwError = RegSetValueExW(hServiceKey,
+ L"Group",
+ 0,
+ REG_SZ,
+ (LPBYTE)lpLoadOrderGroup,
+ (wcslen(lpLoadOrderGroup) + 1) * sizeof(WCHAR));
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- *lpCanonName = LocalAlloc(LMEM_ZEROINIT, NtServiceName.Length + sizeof(WCHAR));
+ dwError = ScmSetServiceGroup(lpService,
+ lpLoadOrderGroup);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
+ }
- if (*lpCanonName == NULL)
+ if (lpdwTagId != NULL)
{
- DPRINT("Error allocating memory for canonized service name!\n");
- RtlFreeUnicodeString(&NtServiceName);
- return ERROR_NOT_ENOUGH_MEMORY;
- }
+ dwError = ScmAssignNewTag(lpService);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- /* Copy the string */
- wcsncpy(*lpCanonName, NtServiceName.Buffer, NtServiceName.Length / sizeof(WCHAR));
+ dwError = RegSetValueExW(hServiceKey,
+ L"Tag",
+ 0,
+ REG_DWORD,
+ (LPBYTE)&lpService->dwTag,
+ sizeof(DWORD));
+ if (dwError != ERROR_SUCCESS)
+ goto done;
- /* The unicode string is not needed anymore */
- RtlFreeUnicodeString(&NtServiceName);
+ *lpdwTagId = lpService->dwTag;
+ }
- if (dwStartType != SERVICE_BOOT_START)
+ /* Write dependencies */
+ if (lpDependencies != NULL && *lpDependencies != 0)
{
- DPRINT("Canonicalized name %S\n", *lpCanonName);
- return NO_ERROR;
+ dwError = ScmWriteDependencies(hServiceKey,
+ (LPWSTR)lpDependencies,
+ dwDependSize);
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
- /* The service is boot-started, so must be relative */
- Result = ScmConvertToBootPathName(*lpCanonName, &RelativeName);
- if (Result)
+ if (lpPassword != NULL)
{
- /* There is a problem, free name and return */
- LocalFree(*lpCanonName);
- DPRINT("Error converting named!\n");
- return Result;
+ /* FIXME: Write password */
}
- ASSERT(RelativeName);
-
- /* Copy that string */
- wcscpy(*lpCanonName, RelativeName + 12);
+done:
+ if (hServiceKey != NULL)
+ RegCloseKey(hServiceKey);
- /* Free the allocated buffer */
- LocalFree(RelativeName);
+ /* Unlock the service database */
+ ScmUnlockDatabase();
- DPRINT("Canonicalized name %S\n", *lpCanonName);
+ DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError);
- /* Success */
- return NO_ERROR;
+ return dwError;
}
DPRINT("dwErrorControl = %lu\n", dwErrorControl);
DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName);
DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup);
+ DPRINT("lpdwTagId = %p\n", lpdwTagId);
if (ScmShutdown)
return ERROR_SHUTDOWN_IN_PROGRESS;
return ERROR_INVALID_PARAMETER;
}
- if ((dwServiceType == (SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS)) &&
- (lpServiceStartName))
- {
+ /* Check for invalid service type value */
+ if ((dwServiceType != SERVICE_KERNEL_DRIVER) &&
+ (dwServiceType != SERVICE_FILE_SYSTEM_DRIVER) &&
+ ((dwServiceType & ~SERVICE_INTERACTIVE_PROCESS) != SERVICE_WIN32_OWN_PROCESS) &&
+ ((dwServiceType & ~SERVICE_INTERACTIVE_PROCESS) != SERVICE_WIN32_SHARE_PROCESS))
+ return ERROR_INVALID_PARAMETER;
+
+ /* Check for invalid start type value */
+ if ((dwStartType != SERVICE_BOOT_START) &&
+ (dwStartType != SERVICE_SYSTEM_START) &&
+ (dwStartType != SERVICE_AUTO_START) &&
+ (dwStartType != SERVICE_DEMAND_START) &&
+ (dwStartType != SERVICE_DISABLED))
return ERROR_INVALID_PARAMETER;
+
+ /* Only drivers can be boot start or system start services */
+ if ((dwStartType == SERVICE_BOOT_START) ||
+ (dwStartType == SERVICE_SYSTEM_START))
+ {
+ if ((dwServiceType != SERVICE_KERNEL_DRIVER) &&
+ (dwServiceType != SERVICE_FILE_SYSTEM_DRIVER))
+ return ERROR_INVALID_PARAMETER;
}
- if ((dwServiceType > SERVICE_WIN32_SHARE_PROCESS) &&
- (dwServiceType != (SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS)) &&
- (dwServiceType != (SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS)))
+ /* Check for invalid error control value */
+ if ((dwErrorControl != SERVICE_ERROR_IGNORE) &&
+ (dwErrorControl != SERVICE_ERROR_NORMAL) &&
+ (dwErrorControl != SERVICE_ERROR_SEVERE) &&
+ (dwErrorControl != SERVICE_ERROR_CRITICAL))
+ return ERROR_INVALID_PARAMETER;
+
+ if ((dwServiceType == (SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS)) &&
+ (lpServiceStartName))
{
return ERROR_INVALID_PARAMETER;
}
- if (dwStartType > SERVICE_DISABLED)
+ if (lpdwTagId && (!lpLoadOrderGroup || !*lpLoadOrderGroup))
{
return ERROR_INVALID_PARAMETER;
}
/* Unlock the service database */
ScmUnlockDatabase();
- /* check if it is marked for deletion */
+ /* Check if it is marked for deletion */
if (lpService->bDeleted)
return ERROR_SERVICE_MARKED_FOR_DELETE;
+
/* Return Error exist */
return ERROR_SERVICE_EXISTS;
}
*lpDisplayName != 0 &&
_wcsicmp(lpService->lpDisplayName, lpDisplayName) != 0)
{
- lpService->lpDisplayName = (WCHAR*) HeapAlloc(GetProcessHeap(), 0,
+ lpService->lpDisplayName = HeapAlloc(GetProcessHeap(), 0,
(wcslen(lpDisplayName) + 1) * sizeof(WCHAR));
if (lpService->lpDisplayName == NULL)
{
if (lpDependencies != NULL && *lpDependencies != 0)
{
dwError = ScmWriteDependencies(hServiceKey,
- (LPWSTR)lpDependencies,
+ (LPCWSTR)lpDependencies,
dwDependSize);
if (dwError != ERROR_SUCCESS)
goto done;
}
else
{
- /* Release the display name buffer */
- if (lpService->lpServiceName != NULL)
+ if (lpService != NULL &&
+ lpService->lpServiceName != NULL)
+ {
+ /* Release the display name buffer */
HeapFree(GetProcessHeap(), 0, lpService->lpDisplayName);
+ }
if (hServiceHandle)
{
(dwServicesReturned + 1) * sizeof(PSERVICE));
if (!lpServicesArray)
{
- DPRINT("Could not allocate a buffer!!\n");
+ DPRINT1("Could not allocate a buffer!!\n");
dwError = ERROR_NOT_ENOUGH_MEMORY;
goto Done;
}
DWORD dwError = ERROR_SUCCESS;
PSERVICE_HANDLE hSvc;
PSERVICE lpService = NULL;
+ DWORD i;
- DPRINT("RStartServiceW() called\n");
+ DPRINT("RStartServiceW(%p %lu %p) called\n", hService, argc, argv);
+ DPRINT(" argc: %lu\n", argc);
+ if (argv != NULL)
+ {
+ for (i = 0; i < argc; i++)
+ {
+ DPRINT(" argv[%lu]: %S\n", i, argv[i]);
+ }
+ }
if (ScmShutdown)
return ERROR_SHUTDOWN_IN_PROGRESS;
if (lpService->bDeleted)
return ERROR_SERVICE_MARKED_FOR_DELETE;
- if (argv) {
- UNIMPLEMENTED;
- argv = NULL;
- }
-
/* Start the service */
- dwError = ScmStartService(lpService, argc, (LPWSTR *)argv);
+ dwError = ScmStartService(lpService, argc, (LPWSTR*)argv);
return dwError;
}
PSERVICE lpService = NULL;
HKEY hServiceKey = NULL;
LPWSTR lpDisplayNameW = NULL;
- // LPWSTR lpBinaryPathNameW = NULL;
+ LPWSTR lpBinaryPathNameW = NULL;
+ LPWSTR lpCanonicalImagePathW = NULL;
LPWSTR lpLoadOrderGroupW = NULL;
LPWSTR lpDependenciesW = NULL;
// LPWSTR lpPasswordW = NULL;
lpService->dwErrorControl = dwErrorControl;
}
-#if 0
- /* FIXME: set the new ImagePath value */
-
- /* Set the image path */
- if (dwServiceType & SERVICE_WIN32)
+ if (lpBinaryPathName != NULL && *lpBinaryPathName != 0)
{
- if (lpBinaryPathName != NULL && *lpBinaryPathName != 0)
+ /* Set the image path */
+ lpBinaryPathNameW = HeapAlloc(GetProcessHeap(),
+ 0,
+ (strlen(lpBinaryPathName) + 1) * sizeof(WCHAR));
+ if (lpBinaryPathNameW == NULL)
{
- lpBinaryPathNameW=HeapAlloc(GetProcessHeap(),0, (strlen(lpBinaryPathName)+1) * sizeof(WCHAR));
- MultiByteToWideChar(CP_ACP, 0, lpBinaryPathName, -1, lpBinaryPathNameW, strlen(lpBinaryPathName)+1);
- dwError = RegSetValueExW(hServiceKey,
- L"ImagePath",
- 0,
- REG_EXPAND_SZ,
- (LPBYTE)lpBinaryPathNameW,
- (wcslen(lpBinaryPathNameW) + 1) * sizeof(WCHAR));
- if (dwError != ERROR_SUCCESS)
- goto done;
+ dwError = ERROR_NOT_ENOUGH_MEMORY;
+ goto done;
}
- }
- else if (dwServiceType & SERVICE_DRIVER)
- {
- if (lpImagePath != NULL && *lpImagePath != 0)
+
+ MultiByteToWideChar(CP_ACP,
+ 0,
+ lpBinaryPathName,
+ -1,
+ lpBinaryPathNameW,
+ strlen(lpBinaryPathName) + 1);
+
+ if (lpService->Status.dwServiceType & SERVICE_DRIVER)
{
- dwError = RegSetValueExW(hServiceKey,
- L"ImagePath",
- 0,
- REG_EXPAND_SZ,
- (LPBYTE)lpImagePath,
- (wcslen(lpImagePath) + 1) *sizeof(WCHAR));
+ dwError = ScmCanonDriverImagePath(lpService->dwStartType,
+ lpBinaryPathNameW,
+ &lpCanonicalImagePathW);
+
+ HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW);
+
if (dwError != ERROR_SUCCESS)
goto done;
+
+ lpBinaryPathNameW = lpCanonicalImagePathW;
}
+
+ dwError = RegSetValueExW(hServiceKey,
+ L"ImagePath",
+ 0,
+ REG_EXPAND_SZ,
+ (LPBYTE)lpBinaryPathNameW,
+ (wcslen(lpBinaryPathNameW) + 1) * sizeof(WCHAR));
+
+ HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW);
+
+ if (dwError != ERROR_SUCCESS)
+ goto done;
}
-#endif
/* Set the group name */
if (lpLoadOrderGroup != NULL && *lpLoadOrderGroup != 0)
DWORD dwDependenciesLength = 0;
DWORD dwLength;
int len;
- LPSTR lpStr;
+ LPCSTR lpStr;
if (lpServiceName)
{
if (lpDependencies)
{
- lpStr = (LPSTR)lpDependencies;
+ lpStr = (LPCSTR)lpDependencies;
while (*lpStr)
{
dwLength = strlen(lpStr) + 1;
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto cleanup;
}
- MultiByteToWideChar(CP_ACP, 0, (LPSTR)lpDependencies, dwDependenciesLength, lpDependenciesW, dwDependenciesLength);
+ MultiByteToWideChar(CP_ACP, 0, (LPCSTR)lpDependencies, dwDependenciesLength, lpDependenciesW, dwDependenciesLength);
}
if (lpServiceStartName)
DWORD dwError = ERROR_SUCCESS;
PSERVICE_HANDLE hSvc;
PSERVICE lpService = NULL;
+ LPWSTR *lpVector = NULL;
+ DWORD i;
+ DWORD dwLength;
DPRINT("RStartServiceA() called\n");
if (lpService->bDeleted)
return ERROR_SERVICE_MARKED_FOR_DELETE;
- /* FIXME: Convert argument vector to Unicode */
+ /* Build a Unicode argument vector */
+ if (argc > 0)
+ {
+ lpVector = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ argc * sizeof(LPWSTR));
+ if (lpVector == NULL)
+ return ERROR_NOT_ENOUGH_MEMORY;
+
+ for (i = 0; i < argc; i++)
+ {
+ dwLength = MultiByteToWideChar(CP_ACP,
+ 0,
+ ((LPSTR*)argv)[i],
+ -1,
+ NULL,
+ 0);
+
+ lpVector[i] = HeapAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY,
+ dwLength * sizeof(WCHAR));
+ if (lpVector[i] == NULL)
+ {
+ dwError = ERROR_NOT_ENOUGH_MEMORY;
+ goto done;
+ }
+
+ MultiByteToWideChar(CP_ACP,
+ 0,
+ ((LPSTR*)argv)[i],
+ -1,
+ lpVector[i],
+ dwLength);
+ }
+ }
/* Start the service */
- dwError = ScmStartService(lpService, 0, NULL);
+ dwError = ScmStartService(lpService, argc, lpVector);
- /* FIXME: Free argument vector */
+done:
+ /* Free the Unicode argument vector */
+ if (lpVector != NULL)
+ {
+ for (i = 0; i < argc; i++)
+ {
+ if (lpVector[i] != NULL)
+ HeapFree(GetProcessHeap(), 0, lpVector[i]);
+ }
+ HeapFree(GetProcessHeap(), 0, lpVector);
+ }
return dwError;
}
if (InfoW.dwInfoLevel == SERVICE_CONFIG_DESCRIPTION)
{
LPSERVICE_DESCRIPTIONW lpServiceDescriptonW;
- LPSERVICE_DESCRIPTIONA lpServiceDescriptonA;
+ //LPSERVICE_DESCRIPTIONA lpServiceDescriptonA;
- lpServiceDescriptonA = Info.psd;
+ //lpServiceDescriptonA = Info.psd;
///if (lpServiceDescriptonA &&
///lpServiceDescriptonA->lpDescription)
dwLength = (strlen(Info.lpDescription) + 1) * sizeof(WCHAR);
lpServiceDescriptonW = HeapAlloc(GetProcessHeap(),
- 0,
- dwLength + sizeof(SERVICE_DESCRIPTIONW));
+ 0,
+ dwLength + sizeof(SERVICE_DESCRIPTIONW));
if (!lpServiceDescriptonW)
{
return ERROR_NOT_ENOUGH_MEMORY;
*pcbBytesNeeded = 0;
*lpServicesReturned = 0;
- if ((dwServiceType!=SERVICE_DRIVER) && (dwServiceType!=SERVICE_WIN32))
+ if ((dwServiceType == 0) ||
+ ((dwServiceType & ~(SERVICE_DRIVER | SERVICE_WIN32)) != 0))
{
DPRINT("Not a valid Service Type!\n");
return ERROR_INVALID_PARAMETER;
}
- if ((dwServiceState<SERVICE_ACTIVE) || (dwServiceState>SERVICE_STATE_ALL))
+ if ((dwServiceState != SERVICE_ACTIVE) &&
+ (dwServiceState != SERVICE_INACTIVE) &&
+ (dwServiceState != SERVICE_STATE_ALL))
{
DPRINT("Not a valid Service State!\n");
return ERROR_INVALID_PARAMETER;