s4-smbtorture: define TORTURE_DEFAULT_SERVICE and set to netlogon.
[Samba.git] / source4 / torture / rpc / svcctl.c
bloba2e32f221fee5d9a22cd0e7e5dcb1331ad423004
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for srvsvc rpc operations
5 Copyright (C) Jelmer Vernooij 2004
6 Copyright (C) Guenther Deschner 2008,2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_svcctl_c.h"
25 #include "librpc/gen_ndr/ndr_svcctl.h"
26 #include "torture/rpc/rpc.h"
27 #include "param/param.h"
29 #define TORTURE_DEFAULT_SERVICE "NetLogon"
31 static bool test_OpenSCManager(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
33 struct svcctl_OpenSCManagerW r;
35 r.in.MachineName = NULL;
36 r.in.DatabaseName = NULL;
37 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
38 r.out.handle = h;
40 torture_assert_ntstatus_ok(tctx,
41 dcerpc_svcctl_OpenSCManagerW(p, tctx, &r),
42 "OpenSCManager failed!");
44 return true;
47 static bool test_CloseServiceHandle(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
49 struct svcctl_CloseServiceHandle r;
51 r.in.handle = h;
52 r.out.handle = h;
53 torture_assert_ntstatus_ok(tctx,
54 dcerpc_svcctl_CloseServiceHandle(p, tctx, &r),
55 "CloseServiceHandle failed");
57 return true;
60 static bool test_OpenService(struct dcerpc_pipe *p, struct torture_context *tctx,
61 struct policy_handle *h, const char *name, struct policy_handle *s)
63 struct svcctl_OpenServiceW r;
65 r.in.scmanager_handle = h;
66 r.in.ServiceName = name;
67 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
68 r.out.handle = s;
70 torture_assert_ntstatus_ok(tctx,
71 dcerpc_svcctl_OpenServiceW(p, tctx, &r),
72 "OpenServiceW failed!");
73 torture_assert_werr_ok(tctx, r.out.result, "OpenServiceW failed!");
75 return true;
79 static bool test_QueryServiceStatus(struct torture_context *tctx,
80 struct dcerpc_pipe *p)
82 struct svcctl_QueryServiceStatus r;
83 struct policy_handle h, s;
84 struct SERVICE_STATUS service_status;
85 NTSTATUS status;
87 if (!test_OpenSCManager(p, tctx, &h))
88 return false;
90 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
91 return false;
93 r.in.handle = &s;
94 r.out.service_status = &service_status;
96 status = dcerpc_svcctl_QueryServiceStatus(p, tctx, &r);
97 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatus failed!");
98 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceStatus failed!");
100 if (!test_CloseServiceHandle(p, tctx, &s))
101 return false;
103 if (!test_CloseServiceHandle(p, tctx, &h))
104 return false;
106 return true;
109 static bool test_QueryServiceStatusEx(struct torture_context *tctx, struct dcerpc_pipe *p)
111 struct svcctl_QueryServiceStatusEx r;
112 struct policy_handle h, s;
113 NTSTATUS status;
115 uint32_t info_level = SVC_STATUS_PROCESS_INFO;
116 uint8_t *buffer;
117 uint32_t offered = 0;
118 uint32_t needed = 0;
120 if (!test_OpenSCManager(p, tctx, &h))
121 return false;
123 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
124 return false;
126 buffer = talloc(tctx, uint8_t);
128 r.in.handle = &s;
129 r.in.info_level = info_level;
130 r.in.offered = offered;
131 r.out.buffer = buffer;
132 r.out.needed = &needed;
134 status = dcerpc_svcctl_QueryServiceStatusEx(p, tctx, &r);
135 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
137 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
138 r.in.offered = needed;
139 buffer = talloc_array(tctx, uint8_t, needed);
140 r.out.buffer = buffer;
142 status = dcerpc_svcctl_QueryServiceStatusEx(p, tctx, &r);
143 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
144 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceStatusEx failed!");
147 if (!test_CloseServiceHandle(p, tctx, &s))
148 return false;
150 if (!test_CloseServiceHandle(p, tctx, &h))
151 return false;
153 return true;
156 static bool test_QueryServiceConfigW(struct torture_context *tctx,
157 struct dcerpc_pipe *p)
159 struct svcctl_QueryServiceConfigW r;
160 struct QUERY_SERVICE_CONFIG query;
161 struct policy_handle h, s;
162 NTSTATUS status;
164 uint32_t offered = 0;
165 uint32_t needed = 0;
167 if (!test_OpenSCManager(p, tctx, &h))
168 return false;
170 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
171 return false;
173 r.in.handle = &s;
174 r.in.offered = offered;
175 r.out.query = &query;
176 r.out.needed = &needed;
178 status = dcerpc_svcctl_QueryServiceConfigW(p, tctx, &r);
179 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfigW failed!");
181 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
182 r.in.offered = needed;
183 status = dcerpc_svcctl_QueryServiceConfigW(p, tctx, &r);
184 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfigW failed!");
187 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfigW failed!");
189 if (!test_CloseServiceHandle(p, tctx, &s))
190 return false;
192 if (!test_CloseServiceHandle(p, tctx, &h))
193 return false;
195 return true;
198 static bool test_QueryServiceConfig2W(struct torture_context *tctx, struct dcerpc_pipe *p)
200 struct svcctl_QueryServiceConfig2W r;
201 struct policy_handle h, s;
202 NTSTATUS status;
204 uint32_t info_level = SERVICE_CONFIG_DESCRIPTION;
205 uint8_t *buffer;
206 uint32_t offered = 0;
207 uint32_t needed = 0;
209 if (!test_OpenSCManager(p, tctx, &h))
210 return false;
212 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
213 return false;
215 buffer = talloc(tctx, uint8_t);
217 r.in.handle = &s;
218 r.in.info_level = info_level;
219 r.in.offered = offered;
220 r.out.buffer = buffer;
221 r.out.needed = &needed;
223 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
224 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
226 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
227 r.in.offered = needed;
228 buffer = talloc_array(tctx, uint8_t, needed);
229 r.out.buffer = buffer;
231 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
232 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
233 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
236 r.in.info_level = SERVICE_CONFIG_FAILURE_ACTIONS;
237 r.in.offered = offered;
238 r.out.buffer = buffer;
239 r.out.needed = &needed;
241 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
242 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
244 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
245 r.in.offered = needed;
246 buffer = talloc_array(tctx, uint8_t, needed);
247 r.out.buffer = buffer;
249 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
250 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
251 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
254 if (!test_CloseServiceHandle(p, tctx, &s))
255 return false;
257 if (!test_CloseServiceHandle(p, tctx, &h))
258 return false;
260 return true;
263 static bool test_QueryServiceObjectSecurity(struct torture_context *tctx,
264 struct dcerpc_pipe *p)
266 struct svcctl_QueryServiceObjectSecurity r;
267 struct policy_handle h, s;
269 uint8_t *buffer;
270 uint32_t needed;
272 if (!test_OpenSCManager(p, tctx, &h))
273 return false;
275 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
276 return false;
278 r.in.handle = &s;
279 r.in.security_flags = 0;
280 r.in.offered = 0;
281 r.out.buffer = NULL;
282 r.out.needed = &needed;
284 torture_assert_ntstatus_ok(tctx,
285 dcerpc_svcctl_QueryServiceObjectSecurity(p, tctx, &r),
286 "QueryServiceObjectSecurity failed!");
287 torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
288 "QueryServiceObjectSecurity failed!");
290 r.in.security_flags = SECINFO_DACL;
292 torture_assert_ntstatus_ok(tctx,
293 dcerpc_svcctl_QueryServiceObjectSecurity(p, tctx, &r),
294 "QueryServiceObjectSecurity failed!");
296 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
297 r.in.offered = needed;
298 buffer = talloc_array(tctx, uint8_t, needed);
299 r.out.buffer = buffer;
300 torture_assert_ntstatus_ok(tctx,
301 dcerpc_svcctl_QueryServiceObjectSecurity(p, tctx, &r),
302 "QueryServiceObjectSecurity failed!");
305 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceObjectSecurity failed!");
307 if (!test_CloseServiceHandle(p, tctx, &s))
308 return false;
310 if (!test_CloseServiceHandle(p, tctx, &h))
311 return false;
313 return true;
316 static bool test_EnumServicesStatus(struct torture_context *tctx, struct dcerpc_pipe *p)
318 struct svcctl_EnumServicesStatusW r;
319 struct policy_handle h;
320 int i;
321 NTSTATUS status;
322 uint32_t resume_handle = 0;
323 struct ENUM_SERVICE_STATUSW *service = NULL;
324 uint32_t needed = 0;
325 uint32_t services_returned = 0;
327 if (!test_OpenSCManager(p, tctx, &h))
328 return false;
330 r.in.handle = &h;
331 r.in.type = SERVICE_TYPE_WIN32;
332 r.in.state = SERVICE_STATE_ALL;
333 r.in.offered = 0;
334 r.in.resume_handle = &resume_handle;
335 r.out.service = NULL;
336 r.out.resume_handle = &resume_handle;
337 r.out.services_returned = &services_returned;
338 r.out.needed = &needed;
340 status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
342 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
344 if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
345 r.in.offered = needed;
346 r.out.service = talloc_array(tctx, uint8_t, needed);
348 status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
350 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
351 torture_assert_werr_ok(tctx, r.out.result, "EnumServicesStatus failed");
354 if (services_returned > 0) {
356 enum ndr_err_code ndr_err;
357 DATA_BLOB blob;
358 struct ndr_pull *ndr;
360 blob.length = r.in.offered;
361 blob.data = talloc_steal(tctx, r.out.service);
363 ndr = ndr_pull_init_blob(&blob, tctx, lp_iconv_convenience(tctx->lp_ctx));
365 service = talloc_array(tctx, struct ENUM_SERVICE_STATUSW, services_returned);
366 if (!service) {
367 return false;
370 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
371 ndr, services_returned, service);
372 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
373 return false;
377 for(i = 0; i < services_returned; i++) {
379 torture_assert(tctx, service[i].service_name,
380 "Service without name returned!");
382 printf("%-20s \"%s\", Type: %d, State: %d\n",
383 service[i].service_name, service[i].display_name,
384 service[i].status.type, service[i].status.state);
387 if (!test_CloseServiceHandle(p, tctx, &h))
388 return false;
390 return true;
393 static bool test_EnumDependentServicesW(struct torture_context *tctx,
394 struct dcerpc_pipe *p)
396 struct svcctl_EnumDependentServicesW r;
397 struct policy_handle h, s;
398 uint32_t needed;
399 uint32_t services_returned;
400 uint32_t i;
401 uint32_t states[] = { SERVICE_STATE_ACTIVE,
402 SERVICE_STATE_INACTIVE,
403 SERVICE_STATE_ALL };
405 if (!test_OpenSCManager(p, tctx, &h))
406 return false;
408 if (!test_OpenService(p, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
409 return false;
411 r.in.service = &s;
412 r.in.offered = 0;
413 r.in.state = 0;
414 r.out.service_status = NULL;
415 r.out.services_returned = &services_returned;
416 r.out.needed = &needed;
418 torture_assert_ntstatus_ok(tctx,
419 dcerpc_svcctl_EnumDependentServicesW(p, tctx, &r),
420 "EnumDependentServicesW failed!");
422 torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
423 "EnumDependentServicesW failed!");
425 for (i=0; i<ARRAY_SIZE(states); i++) {
427 r.in.state = states[i];
429 torture_assert_ntstatus_ok(tctx,
430 dcerpc_svcctl_EnumDependentServicesW(p, tctx, &r),
431 "EnumDependentServicesW failed!");
433 if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
434 r.in.offered = needed;
435 r.out.service_status = talloc_array(tctx, uint8_t, needed);
437 torture_assert_ntstatus_ok(tctx,
438 dcerpc_svcctl_EnumDependentServicesW(p, tctx, &r),
439 "EnumDependentServicesW failed!");
443 torture_assert_werr_ok(tctx, r.out.result,
444 "EnumDependentServicesW failed");
447 if (!test_CloseServiceHandle(p, tctx, &s))
448 return false;
450 if (!test_CloseServiceHandle(p, tctx, &h))
451 return false;
453 return true;
456 static bool test_SCManager(struct torture_context *tctx,
457 struct dcerpc_pipe *p)
459 struct policy_handle h;
461 if (!test_OpenSCManager(p, tctx, &h))
462 return false;
464 if (!test_CloseServiceHandle(p, tctx, &h))
465 return false;
467 return true;
470 struct torture_suite *torture_rpc_svcctl(TALLOC_CTX *mem_ctx)
472 struct torture_suite *suite = torture_suite_create(mem_ctx, "SVCCTL");
473 struct torture_rpc_tcase *tcase;
475 tcase = torture_suite_add_rpc_iface_tcase(suite, "svcctl", &ndr_table_svcctl);
477 torture_rpc_tcase_add_test(tcase, "SCManager",
478 test_SCManager);
479 torture_rpc_tcase_add_test(tcase, "EnumServicesStatus",
480 test_EnumServicesStatus);
481 torture_rpc_tcase_add_test(tcase, "EnumDependentServicesW",
482 test_EnumDependentServicesW);
483 torture_rpc_tcase_add_test(tcase, "QueryServiceStatus",
484 test_QueryServiceStatus);
485 torture_rpc_tcase_add_test(tcase, "QueryServiceStatusEx",
486 test_QueryServiceStatusEx);
487 torture_rpc_tcase_add_test(tcase, "QueryServiceConfigW",
488 test_QueryServiceConfigW);
489 torture_rpc_tcase_add_test(tcase, "QueryServiceConfig2W",
490 test_QueryServiceConfig2W);
491 torture_rpc_tcase_add_test(tcase, "QueryServiceObjectSecurity",
492 test_QueryServiceObjectSecurity);
494 return suite;