xref: /reactos/base/applications/sc/config.c (revision f4d29a74)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/config.c
5  * PURPOSE:     Query/Set the service configuration
6  * COPYRIGHT:   Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
12 BOOL QueryConfig(LPCTSTR ServiceName)
13 {
14     SC_HANDLE hManager = NULL;
15     SC_HANDLE hService = NULL;
16     BOOL bResult = TRUE;
17     DWORD cbBytesNeeded = 0;
18     LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
19     LPWSTR lpPtr;
20     SSIZE_T nLen, i;
21 
22 #ifdef SCDBG
23     _tprintf(_T("service to show configuration - %s\n\n"), ServiceName);
24 #endif
25 
26     hManager = OpenSCManager(NULL,
27                              NULL,
28                              SC_MANAGER_CONNECT);
29     if (hManager == NULL)
30     {
31         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
32         bResult = FALSE;
33         goto done;
34     }
35 
36     hService = OpenService(hManager, ServiceName, SERVICE_QUERY_CONFIG);
37     if (hService == NULL)
38     {
39         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
40         bResult = FALSE;
41         goto done;
42     }
43 
44     if (!QueryServiceConfig(hService,
45                             NULL,
46                             0,
47                             &cbBytesNeeded))
48     {
49         if (cbBytesNeeded == 0)
50         {
51             _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
52             bResult = FALSE;
53             goto done;
54         }
55     }
56 
57     pServiceConfig = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
58     if (pServiceConfig == 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 (!QueryServiceConfig(hService,
67                             pServiceConfig,
68                             cbBytesNeeded,
69                             &cbBytesNeeded))
70     {
71         _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
72         bResult = FALSE;
73         goto done;
74     }
75 
76     _tprintf(_T("[SC] QueryServiceConfig SUCCESS\n\n"));
77 
78     _tprintf(_T("SERVICE_NAME: %s\n"), ServiceName);
79     _tprintf(_T("        TYPE               : %-3lx "), pServiceConfig->dwServiceType);
80     switch (pServiceConfig->dwServiceType)
81     {
82         case SERVICE_KERNEL_DRIVER:
83             _tprintf(_T("KERNEL_DRIVER\n"));
84             break;
85 
86         case SERVICE_FILE_SYSTEM_DRIVER:
87             _tprintf(_T("FILE_SYSTEM_DRIVER\n"));
88             break;
89 
90         case SERVICE_WIN32_OWN_PROCESS:
91             _tprintf(_T("WIN32_OWN_PROCESS\n"));
92             break;
93 
94         case SERVICE_WIN32_SHARE_PROCESS:
95             _tprintf(_T("WIN32_SHARE_PROCESS\n"));
96             break;
97 
98         case SERVICE_WIN32_OWN_PROCESS + SERVICE_INTERACTIVE_PROCESS:
99             _tprintf(_T("WIN32_OWN_PROCESS (interactive)\n"));
100             break;
101 
102         case SERVICE_WIN32_SHARE_PROCESS + SERVICE_INTERACTIVE_PROCESS:
103             _tprintf(_T("WIN32_SHARE_PROCESS (interactive)\n"));
104             break;
105 
106         default:
107             _tprintf(_T("\n"));
108             break;
109     }
110 
111     _tprintf(_T("        START_TYPE         : %-3lx "), pServiceConfig->dwStartType);
112     switch (pServiceConfig->dwStartType)
113     {
114         case SERVICE_BOOT_START:
115             _tprintf(_T("BOOT_START\n"));
116             break;
117 
118         case SERVICE_SYSTEM_START:
119             _tprintf(_T("SYSTEM_START\n"));
120             break;
121 
122         case SERVICE_AUTO_START:
123             _tprintf(_T("AUTO_START\n"));
124             break;
125 
126         case SERVICE_DEMAND_START:
127             _tprintf(_T("DEMAND_START\n"));
128             break;
129 
130         case SERVICE_DISABLED:
131             _tprintf(_T("DISABLED\n"));
132             break;
133 
134         default:
135             _tprintf(_T("\n"));
136             break;
137     }
138 
139     _tprintf(_T("        ERROR_CONTROL      : %-3lx "), pServiceConfig->dwErrorControl);
140     switch (pServiceConfig->dwErrorControl)
141     {
142         case SERVICE_ERROR_IGNORE:
143             _tprintf(_T("IGNORE\n"));
144             break;
145 
146         case SERVICE_ERROR_NORMAL:
147             _tprintf(_T("NORMAL\n"));
148             break;
149 
150         case SERVICE_ERROR_SEVERE:
151             _tprintf(_T("SEVERE\n"));
152             break;
153 
154         case SERVICE_ERROR_CRITICAL:
155             _tprintf(_T("CRITICAL\n"));
156             break;
157 
158         default:
159             _tprintf(_T("\n"));
160             break;
161     }
162 
163     _tprintf(_T("        BINARY_PATH_NAME   : %s\n"), pServiceConfig->lpBinaryPathName);
164     _tprintf(_T("        LOAD_ORDER_GROUP   : %s\n"), pServiceConfig->lpLoadOrderGroup);
165     _tprintf(_T("        TAG                : %lu\n"), pServiceConfig->dwTagId);
166     _tprintf(_T("        DISPLAY_NAME       : %s\n"), pServiceConfig->lpDisplayName);
167     _tprintf(_T("        DEPENDENCIES       : "));
168     lpPtr = pServiceConfig->lpDependencies;
169     i = 0;
170     while (*lpPtr != _T('\0'))
171     {
172        nLen = _tcslen(lpPtr);
173        if (i != 0)
174            _tprintf(_T("\n                           : "));
175        _tprintf(_T("%s"), lpPtr);
176        lpPtr = lpPtr + nLen + 1;
177        i++;
178     }
179     _tprintf(_T("\n"));
180 
181     _tprintf(_T("        SERVICE_START_NAME : %s\n"), pServiceConfig->lpServiceStartName);
182 
183 done:
184     if (bResult == FALSE)
185         ReportLastError();
186 
187     if (pServiceConfig != NULL)
188         HeapFree(GetProcessHeap(), 0, pServiceConfig);
189 
190     if (hService)
191         CloseServiceHandle(hService);
192 
193     if (hManager)
194         CloseServiceHandle(hManager);
195 
196     return bResult;
197 }
198 
199 BOOL SetConfig(LPCTSTR *ServiceArgs, INT ArgCount)
200 {
201     SC_HANDLE hManager = NULL;
202     SC_HANDLE hService = NULL;
203     BOOL bResult = TRUE;
204     SERVICE_CREATE_INFO ServiceInfo;
205 
206     if (!ParseCreateConfigArguments(ServiceArgs, ArgCount, TRUE, &ServiceInfo))
207     {
208         SetConfigUsage();
209         return FALSE;
210     }
211 
212 #ifdef SCDBG
213     _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
214     _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
215     _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
216     _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
217     _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
218     _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
219     _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
220     _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
221     _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
222     _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
223     _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
224 #endif
225 
226     hManager = OpenSCManager(NULL,
227                              NULL,
228                              SC_MANAGER_CONNECT);
229     if (hManager == NULL)
230     {
231         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
232         bResult = FALSE;
233         goto done;
234     }
235 
236     hService = OpenService(hManager,
237                            ServiceInfo.lpServiceName,
238                            SERVICE_CHANGE_CONFIG);
239     if (hService == NULL)
240     {
241         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
242         bResult = FALSE;
243         goto done;
244     }
245 
246     if (!ChangeServiceConfig(hService,
247                              ServiceInfo.dwServiceType,
248                              ServiceInfo.dwStartType,
249                              ServiceInfo.dwErrorControl,
250                              ServiceInfo.lpBinaryPathName,
251                              ServiceInfo.lpLoadOrderGroup,
252                              ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
253                              ServiceInfo.lpDependencies,
254                              ServiceInfo.lpServiceStartName,
255                              ServiceInfo.lpPassword,
256                              ServiceInfo.lpDisplayName))
257     {
258         _tprintf(_T("[SC] ChangeServiceConfig FAILED %lu:\n\n"), GetLastError());
259         bResult = FALSE;
260         goto done;
261     }
262 
263     _tprintf(_T("[SC] ChangeServiceConfig SUCCESS\n\n"));
264 
265 done:
266     if (bResult == FALSE)
267         ReportLastError();
268 
269     if (hService)
270         CloseServiceHandle(hService);
271 
272     if (hManager)
273         CloseServiceHandle(hManager);
274 
275     return bResult;
276 }
277