ReactOS  0.4.12-dev-57-g7050ac4
sdshow.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/sdshow.c
5  * PURPOSE: Show a service security descriptor
6  * COPYRIGHT: Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
13 {
14  SC_HANDLE hManager = NULL;
15  SC_HANDLE hService = NULL;
16  BOOL bResult = TRUE;
17  DWORD cbBytesNeeded = 0;
18  PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
19  LPTSTR pStringBuffer = NULL;
20 
21 #ifdef SCDBG
22  _tprintf(_T("service to show sd - %s\n\n"), ServiceName);
23 #endif
24 
25  hManager = OpenSCManager(NULL,
26  NULL,
28  if (hManager == NULL)
29  {
30  _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
31  bResult = FALSE;
32  goto done;
33  }
34 
35  hService = OpenService(hManager, ServiceName, READ_CONTROL);
36  if (hService == NULL)
37  {
38  _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
39  bResult = FALSE;
40  goto done;
41  }
42 
43  if (!QueryServiceObjectSecurity(hService,
45  (PSECURITY_DESCRIPTOR)&pSecurityDescriptor,
46  sizeof(PSECURITY_DESCRIPTOR),
47  &cbBytesNeeded))
48  {
49  if (cbBytesNeeded == 0)
50  {
51  _tprintf(_T("[SC] QueryServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
52  bResult = FALSE;
53  goto done;
54  }
55  }
56 
57  pSecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
58  if (pSecurityDescriptor == NULL)
59  {
61  _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
62  bResult = FALSE;
63  goto done;
64  }
65 
66  if (!QueryServiceObjectSecurity(hService,
68  pSecurityDescriptor,
69  cbBytesNeeded,
70  &cbBytesNeeded))
71  {
72  _tprintf(_T("[SC] QueryServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
73  bResult = FALSE;
74  goto done;
75  }
76 
80  &pStringBuffer,
81  NULL))
82  {
83  _tprintf(_T("[SC] ConvertSecurityDescriptorToStringSecurityDescriptor FAILED %lu:\n\n"), GetLastError());
84  bResult = FALSE;
85  goto done;
86  }
87 
88  _tprintf(_T("\n%s\n"), pStringBuffer);
89 
90 done:
91  if (bResult == FALSE)
93 
94  if (pStringBuffer != NULL)
95  LocalFree(pStringBuffer);
96 
97  if (pSecurityDescriptor != NULL)
98  HeapFree(GetProcessHeap(), 0, pSecurityDescriptor);
99 
100  if (hService)
101  CloseServiceHandle(hService);
102 
103  if (hManager)
104  CloseServiceHandle(hManager);
105 
106  return bResult;
107 }
#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
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define ConvertSecurityDescriptorToStringSecurityDescriptor
Definition: sddl.h:164
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
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
#define READ_CONTROL
Definition: nt_native.h:58
BOOL SdShow(LPCTSTR ServiceName)
Definition: sdshow.c:12
VOID ReportLastError(VOID)
Definition: sc.c:15
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define HeapFree(x, y, z)
Definition: compat.h:394
#define DACL_SECURITY_INFORMATION
Definition: setypes.h:125
#define SDDL_REVISION_1
Definition: sddl.h:30
BOOL WINAPI QueryServiceObjectSecurity(SC_HANDLE hService, SECURITY_INFORMATION dwSecurityInformation, PSECURITY_DESCRIPTOR lpSecurityDescriptor, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2674
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13