[RAPPS] Bulk install!
[reactos.git] / reactos / base / applications / sc / sdshow.c
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
12 BOOL SdShow(LPCTSTR ServiceName)
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,
27 SC_MANAGER_CONNECT);
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,
44 DACL_SECURITY_INFORMATION,
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 {
60 SetLastError(ERROR_OUTOFMEMORY);
61 _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
62 bResult = FALSE;
63 goto done;
64 }
65
66 if (!QueryServiceObjectSecurity(hService,
67 DACL_SECURITY_INFORMATION,
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
77 if (!ConvertSecurityDescriptorToStringSecurityDescriptor(pSecurityDescriptor,
78 SDDL_REVISION_1,
79 DACL_SECURITY_INFORMATION,
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)
92 ReportLastError();
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 }