ReactOS  0.4.13-dev-698-g77671f0
config.c File Reference
#include "services.h"
#include <ntsecapi.h>
#include <debug.h>
Include dependency graph for config.c:

Go to the source code of this file.

Classes

struct  ustring
 

Macros

#define NDEBUG
 

Functions

NTSTATUS WINAPI SystemFunction005 (const struct ustring *in, const struct ustring *key, struct ustring *out)
 
NTSTATUS WINAPI SystemFunction028 (IN PVOID ContextHandle, OUT LPBYTE SessionKey)
 
DWORD ScmOpenServiceKey (LPWSTR lpServiceName, REGSAM samDesired, PHKEY phKey)
 
DWORD ScmCreateServiceKey (LPCWSTR lpServiceName, REGSAM samDesired, PHKEY phKey)
 
DWORD ScmWriteDependencies (HKEY hServiceKey, LPCWSTR lpDependencies, DWORD dwDependenciesLength)
 
DWORD ScmMarkServiceForDelete (PSERVICE pService)
 
BOOL ScmIsDeleteFlagSet (HKEY hServiceKey)
 
DWORD ScmReadString (HKEY hServiceKey, LPCWSTR lpValueName, LPWSTR *lpValue)
 
DWORD ScmReadDependencies (HKEY hServiceKey, LPWSTR *lpDependencies, DWORD *lpdwDependenciesLength)
 
DWORD ScmSetServicePassword (IN PCWSTR pszServiceName, IN PCWSTR pszPassword)
 
DWORD ScmWriteSecurityDescriptor (_In_ HKEY hServiceKey, _In_ PSECURITY_DESCRIPTOR pSecurityDescriptor)
 
DWORD ScmReadSecurityDescriptor (_In_ HKEY hServiceKey, _Out_ PSECURITY_DESCRIPTOR *ppSecurityDescriptor)
 
DWORD ScmDeleteRegKey (_In_ HKEY hKey, _In_ PCWSTR pszSubKey)
 
DWORD ScmDecryptPassword (_In_ PBYTE pPassword, _In_ DWORD dwPasswordSize, _Out_ PWSTR *pClearTextPassword)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 15 of file config.c.

Function Documentation

◆ ScmCreateServiceKey()

DWORD ScmCreateServiceKey ( LPCWSTR  lpServiceName,
REGSAM  samDesired,
PHKEY  phKey 
)

Definition at line 72 of file config.c.

75 {
77  DWORD dwDisposition;
78  DWORD dwError;
79 
80  *phKey = NULL;
81 
83  L"System\\CurrentControlSet\\Services",
84  0,
86  &hServicesKey);
87  if (dwError != ERROR_SUCCESS)
88  return dwError;
89 
90  dwError = RegCreateKeyExW(hServicesKey,
91  lpServiceName,
92  0,
93  NULL,
95  samDesired,
96  NULL,
97  phKey,
98  &dwDisposition);
99 #if 0
100  if ((dwError == ERROR_SUCCESS) &&
101  (dwDisposition == REG_OPENED_EXISTING_KEY))
102  {
103  RegCloseKey(*phKey);
104  *phKey = NULL;
105  dwError = ERROR_SERVICE_EXISTS;
106  }
107 #endif
108 
110 
111  return dwError;
112 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_READ
Definition: nt_native.h:1023
#define REG_OPENED_EXISTING_KEY
Definition: nt_native.h:1085
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1091
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
smooth NULL
Definition: ftsmooth.c:416
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
static HANDLE hServicesKey
Definition: devinst.c:21
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_SERVICE_EXISTS
Definition: winerror.h:624
static const WCHAR L[]
Definition: oid.c:1250
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by RCreateServiceW().

◆ ScmDecryptPassword()

DWORD ScmDecryptPassword ( _In_ PBYTE  pPassword,
_In_ DWORD  dwPasswordSize,
_Out_ PWSTR pClearTextPassword 
)

Definition at line 701 of file config.c.

705 {
706  struct ustring inData, keyData, outData;
707  BYTE SessionKey[16];
708  PWSTR pBuffer;
710 
711  /* Get the session key */
713  SessionKey);
714  if (!NT_SUCCESS(Status))
715  {
716  DPRINT1("SystemFunction028 failed (Status 0x%08lx)\n", Status);
718  }
719 
720  inData.Length = dwPasswordSize;
721  inData.MaximumLength = inData.Length;
722  inData.Buffer = pPassword;
723 
724  keyData.Length = sizeof(SessionKey);
725  keyData.MaximumLength = keyData.Length;
726  keyData.Buffer = SessionKey;
727 
728  outData.Length = 0;
729  outData.MaximumLength = 0;
730  outData.Buffer = NULL;
731 
732  /* Get the required buffer size */
733  Status = SystemFunction005(&inData,
734  &keyData,
735  &outData);
737  {
738  DPRINT1("SystemFunction005 failed (Status 0x%08lx)\n", Status);
740  }
741 
742  /* Allocate a buffer for the clear text password */
743  pBuffer = HeapAlloc(GetProcessHeap(), 0, outData.Length);
744  if (pBuffer == NULL)
745  return ERROR_OUTOFMEMORY;
746 
747  outData.MaximumLength = outData.Length;
748  outData.Buffer = (unsigned char *)pBuffer;
749 
750  /* Decrypt the password */
751  Status = SystemFunction005(&inData,
752  &keyData,
753  &outData);
754  if (!NT_SUCCESS(Status))
755  {
756  DPRINT1("SystemFunction005 failed (Status 0x%08lx)\n", Status);
759  }
760 
761  *pClearTextPassword = pBuffer;
762 
763  return ERROR_SUCCESS;
764 }
NTSTATUS WINAPI SystemFunction005(const struct ustring *in, const struct ustring *key, struct ustring *out)
Definition: sysfunc.c:182
#define ERROR_SUCCESS
Definition: deptool.c:10
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned char BYTE
Definition: mem.h:68
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
NTSTATUS WINAPI SystemFunction028(IN PVOID ContextHandle, OUT LPBYTE SessionKey)
Definition: config.c:18
#define DPRINT1
Definition: precomp.h:8
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13

Referenced by RChangeServiceConfigW(), and RCreateServiceW().

◆ ScmDeleteRegKey()

DWORD ScmDeleteRegKey ( _In_ HKEY  hKey,
_In_ PCWSTR  pszSubKey 
)

Definition at line 639 of file config.c.

642 {
643  DWORD dwMaxSubkeyLen, dwMaxValueLen;
644  DWORD dwMaxLen, dwSize;
645  PWSTR pszName = NULL;
646  HKEY hSubKey;
647  DWORD dwError;
648 
649  dwError = RegOpenKeyExW(hKey, pszSubKey, 0, KEY_READ, &hSubKey);
650  if (dwError != ERROR_SUCCESS)
651  return dwError;
652 
653  /* Get maximum length of key and value names */
654  dwError = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
655  &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
656  if (dwError != ERROR_SUCCESS)
657  goto done;
658 
659  dwMaxSubkeyLen++;
660  dwMaxValueLen++;
661  dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
662 
663  /* Allocate the name buffer */
664  pszName = HeapAlloc(GetProcessHeap(), 0, dwMaxLen * sizeof(WCHAR));
665  if (pszName == NULL)
666  {
667  dwError = ERROR_NOT_ENOUGH_MEMORY;
668  goto done;
669  }
670 
671  /* Recursively delete all the subkeys */
672  while (TRUE)
673  {
674  dwSize = dwMaxLen;
675  if (RegEnumKeyExW(hSubKey, 0, pszName, &dwSize,
677  {
678  break;
679  }
680 
681  dwError = ScmDeleteRegKey(hSubKey, pszName);
682  if (dwError != ERROR_SUCCESS)
683  goto done;
684  }
685 
686 done:
687  if (pszName != NULL)
688  HeapFree(GetProcessHeap(), 0, pszName);
689 
690  RegCloseKey(hSubKey);
691 
692  /* Finally delete the key */
693  if (dwError == ERROR_SUCCESS)
694  dwError = RegDeleteKeyW(hKey, pszSubKey);
695 
696  return dwError;
697 }
#define max(a, b)
Definition: svc.c:63
#define TRUE
Definition: types.h:120
DWORD ScmDeleteRegKey(_In_ HKEY hKey, _In_ PCWSTR pszSubKey)
Definition: config.c:639
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_READ
Definition: nt_native.h:1023
uint16_t * PWSTR
Definition: typedefs.h:54
LONG WINAPI RegDeleteKeyW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey)
Definition: reg.c:1237
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
LONG WINAPI RegQueryInfoKeyW(HKEY hKey, LPWSTR lpClass, LPDWORD lpcClass, LPDWORD lpReserved, LPDWORD lpcSubKeys, LPDWORD lpcMaxSubKeyLen, LPDWORD lpcMaxClassLen, LPDWORD lpcValues, LPDWORD lpcMaxValueNameLen, LPDWORD lpcMaxValueLen, LPDWORD lpcbSecurityDescriptor, PFILETIME lpftLastWriteTime)
Definition: reg.c:3686
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
LONG WINAPI RegEnumKeyExW(_In_ HKEY hKey, _In_ DWORD dwIndex, _Out_ LPWSTR lpName, _Inout_ LPDWORD lpcbName, _Reserved_ LPDWORD lpReserved, _Out_opt_ LPWSTR lpClass, _Inout_opt_ LPDWORD lpcbClass, _Out_opt_ PFILETIME lpftLastWriteTime)
Definition: reg.c:2527
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by RCloseServiceHandle(), ScmDeleteMarkedServices(), and ScmDeleteRegKey().

◆ ScmIsDeleteFlagSet()

BOOL ScmIsDeleteFlagSet ( HKEY  hServiceKey)

Definition at line 251 of file config.c.

252 {
253  DWORD dwError;
254  DWORD dwType;
255  DWORD dwFlag;
256  DWORD dwSize = sizeof(DWORD);
257 
258  dwError = RegQueryValueExW(hServiceKey,
259  L"DeleteFlag",
260  0,
261  &dwType,
262  (LPBYTE)&dwFlag,
263  &dwSize);
264 
265  return (dwError == ERROR_SUCCESS);
266 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define DWORD
Definition: nt_native.h:44
unsigned char * LPBYTE
Definition: typedefs.h:52
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by CreateServiceListEntry().

◆ ScmMarkServiceForDelete()

DWORD ScmMarkServiceForDelete ( PSERVICE  pService)

Definition at line 223 of file config.c.

224 {
225  HKEY hServiceKey = NULL;
226  DWORD dwValue = 1;
227  DWORD dwError;
228 
229  DPRINT("ScmMarkServiceForDelete() called\n");
230 
231  dwError = ScmOpenServiceKey(pService->lpServiceName,
232  KEY_WRITE,
233  &hServiceKey);
234  if (dwError != ERROR_SUCCESS)
235  return dwError;
236 
237  dwError = RegSetValueExW(hServiceKey,
238  L"DeleteFlag",
239  0,
240  REG_DWORD,
241  (LPBYTE)&dwValue,
242  sizeof(DWORD));
243 
244  RegCloseKey(hServiceKey);
245 
246  return dwError;
247 }
#define ERROR_SUCCESS
Definition: deptool.c:10
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define KEY_WRITE
Definition: nt_native.h:1031
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
LPWSTR lpServiceName
Definition: services.h:61
DWORD ScmOpenServiceKey(LPWSTR lpServiceName, REGSAM samDesired, PHKEY phKey)
Definition: config.c:42
#define REG_DWORD
Definition: sdbapi.c:596

Referenced by RDeleteService().

◆ ScmOpenServiceKey()

DWORD ScmOpenServiceKey ( LPWSTR  lpServiceName,
REGSAM  samDesired,
PHKEY  phKey 
)

Definition at line 42 of file config.c.

45 {
47  DWORD dwError;
48 
49  *phKey = NULL;
50 
52  L"System\\CurrentControlSet\\Services",
53  0,
54  KEY_READ,
55  &hServicesKey);
56  if (dwError != ERROR_SUCCESS)
57  return dwError;
58 
59  dwError = RegOpenKeyExW(hServicesKey,
60  lpServiceName,
61  0,
62  samDesired,
63  phKey);
64 
66 
67  return dwError;
68 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_READ
Definition: nt_native.h:1023
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
smooth NULL
Definition: ftsmooth.c:416
static HANDLE hServicesKey
Definition: devinst.c:21
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by RChangeServiceConfig2W(), RChangeServiceConfigW(), RQueryServiceConfig2A(), RQueryServiceConfig2W(), RQueryServiceConfigA(), RQueryServiceConfigW(), RSetServiceObjectSecurity(), and ScmMarkServiceForDelete().

◆ ScmReadDependencies()

DWORD ScmReadDependencies ( HKEY  hServiceKey,
LPWSTR lpDependencies,
DWORD lpdwDependenciesLength 
)

Definition at line 349 of file config.c.

352 {
353  LPWSTR lpGroups = NULL;
354  LPWSTR lpServices = NULL;
355  SIZE_T cchGroupsLength = 0;
356  SIZE_T cchServicesLength = 0;
357  LPWSTR lpSrc;
358  LPWSTR lpDest;
359  SIZE_T cchLength;
360  SIZE_T cchTotalLength;
361 
362  *lpDependencies = NULL;
363  *lpdwDependenciesLength = 0;
364 
365  /* Read the dependency values */
366  ScmReadString(hServiceKey,
367  L"DependOnGroup",
368  &lpGroups);
369 
370  ScmReadString(hServiceKey,
371  L"DependOnService",
372  &lpServices);
373 
374  /* Leave, if there are no dependencies */
375  if (lpGroups == NULL && lpServices == NULL)
376  return ERROR_SUCCESS;
377 
378  /* Determine the total buffer size for the dependencies */
379  if (lpGroups)
380  {
381  DPRINT("Groups:\n");
382  lpSrc = lpGroups;
383  while (*lpSrc != 0)
384  {
385  DPRINT(" %S\n", lpSrc);
386 
387  cchLength = wcslen(lpSrc) + 1;
388  cchGroupsLength += cchLength + 1;
389 
390  lpSrc = lpSrc + cchLength;
391  }
392  }
393 
394  if (lpServices)
395  {
396  DPRINT("Services:\n");
397  lpSrc = lpServices;
398  while (*lpSrc != 0)
399  {
400  DPRINT(" %S\n", lpSrc);
401 
402  cchLength = wcslen(lpSrc) + 1;
403  cchServicesLength += cchLength;
404 
405  lpSrc = lpSrc + cchLength;
406  }
407  }
408 
409  cchTotalLength = cchGroupsLength + cchServicesLength + 1;
410  DPRINT("cchTotalLength: %lu\n", cchTotalLength);
411 
412  /* Allocate the common buffer for the dependencies */
413  *lpDependencies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cchTotalLength * sizeof(WCHAR));
414  if (*lpDependencies == NULL)
415  {
416  if (lpGroups)
417  HeapFree(GetProcessHeap(), 0, lpGroups);
418 
419  if (lpServices)
420  HeapFree(GetProcessHeap(), 0, lpServices);
421 
423  }
424 
425  /* Return the allocated buffer length in characters */
426  *lpdwDependenciesLength = (DWORD)cchTotalLength;
427 
428  /* Copy the service dependencies into the common buffer */
429  lpDest = *lpDependencies;
430  if (lpServices)
431  {
432  memcpy(lpDest,
433  lpServices,
434  cchServicesLength * sizeof(WCHAR));
435 
436  lpDest = lpDest + cchServicesLength;
437  }
438 
439  /* Copy the group dependencies into the common buffer */
440  if (lpGroups)
441  {
442  lpSrc = lpGroups;
443  while (*lpSrc != 0)
444  {
445  cchLength = wcslen(lpSrc) + 1;
446 
447  *lpDest = SC_GROUP_IDENTIFIERW;
448  lpDest++;
449 
450  wcscpy(lpDest, lpSrc);
451 
452  lpDest = lpDest + cchLength;
453  lpSrc = lpSrc + cchLength;
454  }
455  }
456 
457  /* Free the temporary buffers */
458  if (lpGroups)
459  HeapFree(GetProcessHeap(), 0, lpGroups);
460 
461  if (lpServices)
462  HeapFree(GetProcessHeap(), 0, lpServices);
463 
464  return ERROR_SUCCESS;
465 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define SC_GROUP_IDENTIFIERW
Definition: winsvc.h:12
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define DWORD
Definition: nt_native.h:44
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
DWORD ScmReadString(HKEY hServiceKey, LPCWSTR lpValueName, LPWSTR *lpValue)
Definition: config.c:270
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
WCHAR * LPWSTR
Definition: xmlstorage.h:184
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394

Referenced by RQueryServiceConfigA(), and RQueryServiceConfigW().

◆ ScmReadSecurityDescriptor()

DWORD ScmReadSecurityDescriptor ( _In_ HKEY  hServiceKey,
_Out_ PSECURITY_DESCRIPTOR ppSecurityDescriptor 
)

Definition at line 559 of file config.c.

562 {
563  PSECURITY_DESCRIPTOR pRelativeSD = NULL;
564  HKEY hSecurityKey = NULL;
565  DWORD dwBufferLength = 0;
566  DWORD dwType;
567  DWORD dwError;
568 
569  DPRINT("ScmReadSecurityDescriptor(%p %p)\n", hServiceKey, ppSecurityDescriptor);
570 
571  *ppSecurityDescriptor = NULL;
572 
573  dwError = RegOpenKeyExW(hServiceKey,
574  L"Security",
575  0,
577  &hSecurityKey);
578  if (dwError != ERROR_SUCCESS)
579  {
580  DPRINT("RegOpenKeyExW() failed (Error %lu)\n", dwError);
581 
582  /* Do not fail if the Security key does not exist */
583  if (dwError == ERROR_FILE_NOT_FOUND)
584  dwError = ERROR_SUCCESS;
585  goto done;
586  }
587 
588  dwError = RegQueryValueExW(hSecurityKey,
589  L"Security",
590  0,
591  &dwType,
592  NULL,
593  &dwBufferLength);
594  if (dwError != ERROR_SUCCESS)
595  {
596  DPRINT("RegQueryValueExW() failed (Error %lu)\n", dwError);
597 
598  /* Do not fail if the Security value does not exist */
599  if (dwError == ERROR_FILE_NOT_FOUND)
600  dwError = ERROR_SUCCESS;
601  goto done;
602  }
603 
604  DPRINT("dwBufferLength: %lu\n", dwBufferLength);
605  pRelativeSD = RtlAllocateHeap(RtlGetProcessHeap(),
607  dwBufferLength);
608  if (pRelativeSD == NULL)
609  {
610  return ERROR_OUTOFMEMORY;
611  }
612 
613  DPRINT("pRelativeSD: %lu\n", pRelativeSD);
614  dwError = RegQueryValueExW(hSecurityKey,
615  L"Security",
616  0,
617  &dwType,
618  (LPBYTE)pRelativeSD,
619  &dwBufferLength);
620  if (dwError != ERROR_SUCCESS)
621  {
622  goto done;
623  }
624 
625  *ppSecurityDescriptor = pRelativeSD;
626 
627 done:
628  if (dwError != ERROR_SUCCESS && pRelativeSD != NULL)
629  RtlFreeHeap(RtlGetProcessHeap(), 0, pRelativeSD);
630 
631  if (hSecurityKey != NULL)
632  RegCloseKey(hSecurityKey);
633 
634  return dwError;
635 }
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13

Referenced by CreateServiceListEntry().

◆ ScmReadString()

DWORD ScmReadString ( HKEY  hServiceKey,
LPCWSTR  lpValueName,
LPWSTR lpValue 
)

Definition at line 270 of file config.c.

273 {
274  DWORD dwError = 0;
275  DWORD dwSize = 0;
276  DWORD dwType = 0;
277  LPWSTR ptr = NULL;
278  LPWSTR expanded = NULL;
279 
280  *lpValue = NULL;
281 
282  dwError = RegQueryValueExW(hServiceKey,
283  lpValueName,
284  0,
285  &dwType,
286  NULL,
287  &dwSize);
288  if (dwError != ERROR_SUCCESS)
289  return dwError;
290 
292  if (ptr == NULL)
294 
295  dwError = RegQueryValueExW(hServiceKey,
296  lpValueName,
297  0,
298  &dwType,
299  (LPBYTE)ptr,
300  &dwSize);
301  if (dwError != ERROR_SUCCESS)
302  {
303  HeapFree(GetProcessHeap(), 0, ptr);
304  return dwError;
305  }
306 
307  if (dwType == REG_EXPAND_SZ)
308  {
309  /* Expand the value... */
311  if (dwSize > 0)
312  {
313  expanded = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize * sizeof(WCHAR));
314  if (expanded)
315  {
316  if (dwSize == ExpandEnvironmentStringsW(ptr, expanded, dwSize))
317  {
318  *lpValue = expanded;
319  dwError = ERROR_SUCCESS;
320  }
321  else
322  {
323  dwError = GetLastError();
324  HeapFree(GetProcessHeap(), 0, expanded);
325  }
326  }
327  else
328  {
329  dwError = ERROR_NOT_ENOUGH_MEMORY;
330  }
331  }
332  else
333  {
334  dwError = GetLastError();
335  }
336 
337  HeapFree(GetProcessHeap(), 0, ptr);
338  }
339  else
340  {
341  *lpValue = ptr;
342  }
343 
344  return dwError;
345 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
unsigned char * LPBYTE
Definition: typedefs.h:52
static PVOID ptr
Definition: dispmode.c:27
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
DWORD WINAPI ExpandEnvironmentStringsW(IN LPCWSTR lpSrc, IN LPWSTR lpDst, IN DWORD nSize)
Definition: environ.c:519
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by CreateServiceListEntry(), RQueryServiceConfig2A(), RQueryServiceConfig2W(), RQueryServiceConfigA(), RQueryServiceConfigW(), and ScmReadDependencies().

◆ ScmSetServicePassword()

DWORD ScmSetServicePassword ( IN PCWSTR  pszServiceName,
IN PCWSTR  pszPassword 
)

Definition at line 469 of file config.c.

472 {
474  LSA_HANDLE PolicyHandle = NULL;
475  UNICODE_STRING ServiceName = {0, 0, NULL};
478  DWORD dwError = ERROR_SUCCESS;
479 
481 
485  &PolicyHandle);
486  if (!NT_SUCCESS(Status))
488 
489  ServiceName.Length = (wcslen(pszServiceName) + 4) * sizeof(WCHAR);
490  ServiceName.MaximumLength = ServiceName.Length + sizeof(WCHAR);
493  ServiceName.MaximumLength);
494  if (ServiceName.Buffer == NULL)
496 
497  wcscpy(ServiceName.Buffer, L"_SC_");
498  wcscat(ServiceName.Buffer, pszServiceName);
499 
500  RtlInitUnicodeString(&Password, pszPassword);
501 
502  Status = LsaStorePrivateData(PolicyHandle,
503  &ServiceName,
504  pszPassword ? &Password : NULL);
505  if (!NT_SUCCESS(Status))
506  {
507  dwError = RtlNtStatusToDosError(Status);
508  goto done;
509  }
510 
511 done:
512  if (ServiceName.Buffer != NULL)
513  HeapFree(GetProcessHeap(), 0, ServiceName.Buffer);
514 
515  if (PolicyHandle != NULL)
516  LsaClose(PolicyHandle);
517 
518  return dwError;
519 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define ERROR_SUCCESS
Definition: deptool.c:10
NTSTATUS WINAPI LsaStorePrivateData(IN LSA_HANDLE PolicyHandle, IN PLSA_UNICODE_STRING KeyName, IN PLSA_UNICODE_STRING PrivateData OPTIONAL)
Definition: lsa.c:2207
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS WINAPI LsaClose(IN LSA_HANDLE ObjectHandle)
Definition: lsa.c:192
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
NTSTATUS WINAPI LsaOpenPolicy(IN PLSA_UNICODE_STRING SystemName OPTIONAL, IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes, IN ACCESS_MASK DesiredAccess, OUT PLSA_HANDLE PolicyHandle)
Definition: lsa.c:1181
smooth NULL
Definition: ftsmooth.c:416
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
#define POLICY_CREATE_SECRET
Definition: ntsecapi.h:66
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394

Referenced by RChangeServiceConfigW(), and RCreateServiceW().

◆ ScmWriteDependencies()

DWORD ScmWriteDependencies ( HKEY  hServiceKey,
LPCWSTR  lpDependencies,
DWORD  dwDependenciesLength 
)

Definition at line 117 of file config.c.

120 {
121  DWORD dwError = ERROR_SUCCESS;
122  SIZE_T cchGroupLength = 0;
123  SIZE_T cchServiceLength = 0;
124  SIZE_T cchLength;
125  LPWSTR lpGroupDeps;
126  LPWSTR lpServiceDeps;
127  LPCWSTR lpSrc;
128  LPWSTR lpDst;
129 
130  if (*lpDependencies == 0)
131  {
132  RegDeleteValueW(hServiceKey,
133  L"DependOnService");
134  RegDeleteValueW(hServiceKey,
135  L"DependOnGroup");
136  }
137  else
138  {
139  lpGroupDeps = HeapAlloc(GetProcessHeap(),
141  (dwDependenciesLength + 2) * sizeof(WCHAR));
142  if (lpGroupDeps == NULL)
144 
145  lpSrc = lpDependencies;
146  lpDst = lpGroupDeps;
147  while (*lpSrc != 0)
148  {
149  cchLength = wcslen(lpSrc) + 1;
150  if (*lpSrc == SC_GROUP_IDENTIFIERW)
151  {
152  lpSrc++;
153  cchLength--;
154  cchGroupLength += cchLength;
155  wcscpy(lpDst, lpSrc);
156  lpDst = lpDst + cchLength;
157  }
158 
159  lpSrc = lpSrc + cchLength;
160  }
161  *lpDst = 0;
162  lpDst++;
163  cchGroupLength++;
164 
165  lpSrc = lpDependencies;
166  lpServiceDeps = lpDst;
167  while (*lpSrc != 0)
168  {
169  cchLength = wcslen(lpSrc) + 1;
170  if (*lpSrc != SC_GROUP_IDENTIFIERW)
171  {
172  cchServiceLength += cchLength;
173  wcscpy(lpDst, lpSrc);
174  lpDst = lpDst + cchLength;
175  }
176 
177  lpSrc = lpSrc + cchLength;
178  }
179  *lpDst = 0;
180  cchServiceLength++;
181 
182  if (cchGroupLength > 1)
183  {
184  dwError = RegSetValueExW(hServiceKey,
185  L"DependOnGroup",
186  0,
187  REG_MULTI_SZ,
188  (LPBYTE)lpGroupDeps,
189  (DWORD)(cchGroupLength * sizeof(WCHAR)));
190  }
191  else
192  {
193  RegDeleteValueW(hServiceKey,
194  L"DependOnGroup");
195  }
196 
197  if (dwError == ERROR_SUCCESS)
198  {
199  if (cchServiceLength > 1)
200  {
201  dwError = RegSetValueExW(hServiceKey,
202  L"DependOnService",
203  0,
204  REG_MULTI_SZ,
205  (LPBYTE)lpServiceDeps,
206  (DWORD)(cchServiceLength * sizeof(WCHAR)));
207  }
208  else
209  {
210  RegDeleteValueW(hServiceKey,
211  L"DependOnService");
212  }
213  }
214 
215  HeapFree(GetProcessHeap(), 0, lpGroupDeps);
216  }
217 
218  return dwError;
219 }
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define SC_GROUP_IDENTIFIERW
Definition: winsvc.h:12
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
unsigned char * LPBYTE
Definition: typedefs.h:52
#define REG_MULTI_SZ
Definition: nt_native.h:1501
smooth NULL
Definition: ftsmooth.c:416
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
ULONG_PTR SIZE_T
Definition: typedefs.h:78
LONG WINAPI RegDeleteValueW(HKEY hKey, LPCWSTR lpValueName)
Definition: reg.c:2355
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
WCHAR * LPWSTR
Definition: xmlstorage.h:184
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394

Referenced by RChangeServiceConfigW(), and RCreateServiceW().

◆ ScmWriteSecurityDescriptor()

DWORD ScmWriteSecurityDescriptor ( _In_ HKEY  hServiceKey,
_In_ PSECURITY_DESCRIPTOR  pSecurityDescriptor 
)

Definition at line 523 of file config.c.

526 {
527  HKEY hSecurityKey = NULL;
528  DWORD dwDisposition;
529  DWORD dwError;
530 
531  DPRINT("ScmWriteSecurityDescriptor(%p %p)\n", hServiceKey, pSecurityDescriptor);
532 
533  dwError = RegCreateKeyExW(hServiceKey,
534  L"Security",
535  0,
536  NULL,
539  NULL,
540  &hSecurityKey,
541  &dwDisposition);
542  if (dwError != ERROR_SUCCESS)
543  return dwError;
544 
545  dwError = RegSetValueExW(hSecurityKey,
546  L"Security",
547  0,
548  REG_BINARY,
549  (LPBYTE)pSecurityDescriptor,
550  RtlLengthSecurityDescriptor(pSecurityDescriptor));
551 
552  RegCloseKey(hSecurityKey);
553 
554  return dwError;
555 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define REG_BINARY
Definition: nt_native.h:1496
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1091
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
NTSYSAPI ULONG WINAPI RtlLengthSecurityDescriptor(PSECURITY_DESCRIPTOR)

Referenced by CreateServiceListEntry(), RCreateServiceW(), and RSetServiceObjectSecurity().

◆ SystemFunction005()

NTSTATUS WINAPI SystemFunction005 ( const struct ustring in,
const struct ustring key,
struct ustring out 
)

Definition at line 182 of file sysfunc.c.

185 {
186  union {
187  unsigned char uc[8];
188  unsigned int ui[2];
189  } data;
190  unsigned char deskey[7];
191  unsigned int ofs, crypt_len;
192 
193  if (key->Length<=0)
195 
196  if (key->Length<sizeof deskey)
197  {
198  memset(deskey, 0, sizeof deskey);
199  memcpy(deskey, key->Buffer, key->Length);
200  }
201  else
202  memcpy(deskey, key->Buffer, sizeof deskey);
203 
204  CRYPT_DESunhash(data.uc, deskey, in->Buffer);
205 
206  if (data.ui[1] != 1)
208 
209  crypt_len = data.ui[0];
210  if (crypt_len > out->MaximumLength)
211  {
212  out->Length = crypt_len;
214  }
215 
216  for (ofs=0; (ofs+8)<crypt_len; ofs+=8)
217  CRYPT_DESunhash(out->Buffer+ofs, deskey, in->Buffer+ofs+8);
218 
219  if (ofs<crypt_len)
220  {
221  CRYPT_DESunhash(data.uc, deskey, in->Buffer+ofs+8);
222  memcpy(out->Buffer+ofs, data.uc, crypt_len-ofs);
223  }
224 
225  out->Length = crypt_len;
226 
227  return STATUS_SUCCESS;
228 }
static void deskey(const unsigned char *key, short edf, ulong32 *keyout)
Definition: des.c:1285
UINT ui
Definition: oleauto.h:49
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:462
static FILE * out
Definition: regtests2xml.c:44
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
unsigned char * CRYPT_DESunhash(unsigned char *dst, const unsigned char *key, const unsigned char *src) DECLSPEC_HIDDEN
Definition: crypt_des.c:299
GLuint in
Definition: glext.h:9616
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define memset(x, y, z)
Definition: compat.h:39
#define STATUS_UNKNOWN_REVISION
Definition: ntstatus.h:310
Definition: path.c:42

Referenced by ScmDecryptPassword().

◆ SystemFunction028()

NTSTATUS WINAPI SystemFunction028 ( IN PVOID  ContextHandle,
OUT LPBYTE  SessionKey 
)

Referenced by ScmDecryptPassword().