ReactOS  0.4.12-dev-18-gf469aca
sdset.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Services
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: base/applications/sc/sdset.c
5  * PURPOSE: Set a service security descriptor
6  * COPYRIGHT: Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
12 BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
13 {
14  SC_HANDLE hManager = NULL;
15  SC_HANDLE hService = NULL;
16  BOOL bResult = TRUE;
17  ULONG ulSecurityDescriptorSize = 0;
18  PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
19 
20 #ifdef SCDBG
21  _tprintf(_T("service to set sd - %s\n\n"), ServiceName);
22 #endif
23 
24  hManager = OpenSCManager(NULL,
25  NULL,
27  if (hManager == NULL)
28  {
29  _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
30  bResult = FALSE;
31  goto done;
32  }
33 
34  hService = OpenService(hManager, ServiceName, WRITE_DAC);
35  if (hService == NULL)
36  {
37  _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
38  bResult = FALSE;
39  goto done;
40  }
41 
42  if (!ConvertStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor,
44  &pSecurityDescriptor,
45  &ulSecurityDescriptorSize))
46  {
47  _tprintf(_T("[SC] ConvertStringSecurityDescriptorToSecurityDescriptor FAILED %lu:\n\n"), GetLastError());
48  bResult = FALSE;
49  goto done;
50  }
51 
52  if (!SetServiceObjectSecurity(hService,
54  pSecurityDescriptor))
55  {
56  _tprintf(_T("[SC] SetServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
57  bResult = FALSE;
58  goto done;
59  }
60 
61 done:
62  if (bResult == FALSE)
64 
65  if (pSecurityDescriptor != NULL)
66  LocalFree(pSecurityDescriptor);
67 
68  if (hService)
69  CloseServiceHandle(hService);
70 
71  if (hManager)
72  CloseServiceHandle(hManager);
73 
74  return bResult;
75 }
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define OpenService
Definition: winsvc.h:576
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define OpenSCManager
Definition: winsvc.h:575
#define _T(x)
Definition: vfdio.h:22
BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
Definition: sdset.c:12
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define WRITE_DAC
Definition: nt_native.h:59
#define ConvertStringSecurityDescriptorToSecurityDescriptor
Definition: sddl.h:162
VOID ReportLastError(VOID)
Definition: sc.c:15
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
BOOL WINAPI SetServiceObjectSecurity(SC_HANDLE hService, SECURITY_INFORMATION dwSecurityInformation, PSECURITY_DESCRIPTOR lpSecurityDescriptor)
Definition: scm.c:2716
unsigned int ULONG
Definition: retypes.h:1
#define DACL_SECURITY_INFORMATION
Definition: setypes.h:125
#define SDDL_REVISION_1
Definition: sddl.h:30