lib/util: Build iov_buf library only when building samba
[Samba.git] / source4 / torture / rpc / svcctl.c
blob3e6b66dcc5170c2a59394fe1f17422cbde17f3a5
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for svcctl 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 "librpc/gen_ndr/ndr_svcctl_c.h"
24 #include "librpc/gen_ndr/ndr_svcctl.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "torture/rpc/torture_rpc.h"
27 #include "param/param.h"
29 #define TORTURE_DEFAULT_SERVICE "Spooler"
31 static bool test_OpenSCManager(struct dcerpc_binding_handle *b,
32 struct torture_context *tctx,
33 struct policy_handle *h)
35 struct svcctl_OpenSCManagerW r;
37 r.in.MachineName = NULL;
38 r.in.DatabaseName = NULL;
39 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
40 r.out.handle = h;
42 torture_assert_ntstatus_ok(tctx,
43 dcerpc_svcctl_OpenSCManagerW_r(b, tctx, &r),
44 "OpenSCManager failed!");
46 return true;
49 static bool test_CloseServiceHandle(struct dcerpc_binding_handle *b,
50 struct torture_context *tctx,
51 struct policy_handle *h)
53 struct svcctl_CloseServiceHandle r;
55 r.in.handle = h;
56 r.out.handle = h;
57 torture_assert_ntstatus_ok(tctx,
58 dcerpc_svcctl_CloseServiceHandle_r(b, tctx, &r),
59 "CloseServiceHandle failed");
61 return true;
64 static bool test_OpenService(struct dcerpc_binding_handle *b,
65 struct torture_context *tctx,
66 struct policy_handle *h,
67 const char *name,
68 struct policy_handle *s)
70 struct svcctl_OpenServiceW r;
72 r.in.scmanager_handle = h;
73 r.in.ServiceName = name;
74 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
75 r.out.handle = s;
77 torture_assert_ntstatus_ok(tctx,
78 dcerpc_svcctl_OpenServiceW_r(b, tctx, &r),
79 "OpenServiceW failed!");
80 torture_assert_werr_ok(tctx, r.out.result, "OpenServiceW failed!");
82 return true;
86 static bool test_QueryServiceStatus(struct torture_context *tctx,
87 struct dcerpc_pipe *p)
89 struct svcctl_QueryServiceStatus r;
90 struct policy_handle h, s;
91 struct SERVICE_STATUS service_status;
92 NTSTATUS status;
93 struct dcerpc_binding_handle *b = p->binding_handle;
95 if (!test_OpenSCManager(b, tctx, &h))
96 return false;
98 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
99 return false;
101 r.in.handle = &s;
102 r.out.service_status = &service_status;
104 status = dcerpc_svcctl_QueryServiceStatus_r(b, tctx, &r);
105 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatus failed!");
106 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceStatus failed!");
108 if (!test_CloseServiceHandle(b, tctx, &s))
109 return false;
111 if (!test_CloseServiceHandle(b, tctx, &h))
112 return false;
114 return true;
117 static bool test_QueryServiceStatusEx(struct torture_context *tctx, struct dcerpc_pipe *p)
119 struct svcctl_QueryServiceStatusEx r;
120 struct policy_handle h, s;
121 NTSTATUS status;
122 struct dcerpc_binding_handle *b = p->binding_handle;
124 uint32_t info_level = SVC_STATUS_PROCESS_INFO;
125 uint8_t *buffer;
126 uint32_t offered = 0;
127 uint32_t needed = 0;
129 if (!test_OpenSCManager(b, tctx, &h))
130 return false;
132 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
133 return false;
135 buffer = talloc(tctx, uint8_t);
137 r.in.handle = &s;
138 r.in.info_level = info_level;
139 r.in.offered = offered;
140 r.out.buffer = buffer;
141 r.out.needed = &needed;
143 status = dcerpc_svcctl_QueryServiceStatusEx_r(b, tctx, &r);
144 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
146 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
147 r.in.offered = needed;
148 buffer = talloc_array(tctx, uint8_t, needed);
149 r.out.buffer = buffer;
151 status = dcerpc_svcctl_QueryServiceStatusEx_r(b, tctx, &r);
152 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
153 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceStatusEx failed!");
156 if (!test_CloseServiceHandle(b, tctx, &s))
157 return false;
159 if (!test_CloseServiceHandle(b, tctx, &h))
160 return false;
162 return true;
165 static bool test_QueryServiceConfigW(struct torture_context *tctx,
166 struct dcerpc_pipe *p)
168 struct svcctl_QueryServiceConfigW r;
169 struct QUERY_SERVICE_CONFIG query;
170 struct policy_handle h, s;
171 NTSTATUS status;
172 struct dcerpc_binding_handle *b = p->binding_handle;
174 uint32_t offered = 0;
175 uint32_t needed = 0;
177 if (!test_OpenSCManager(b, tctx, &h))
178 return false;
180 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
181 return false;
183 r.in.handle = &s;
184 r.in.offered = offered;
185 r.out.query = &query;
186 r.out.needed = &needed;
188 status = dcerpc_svcctl_QueryServiceConfigW_r(b, tctx, &r);
189 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfigW failed!");
191 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
192 r.in.offered = needed;
193 status = dcerpc_svcctl_QueryServiceConfigW_r(b, tctx, &r);
194 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfigW failed!");
197 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfigW failed!");
199 if (!test_CloseServiceHandle(b, tctx, &s))
200 return false;
202 if (!test_CloseServiceHandle(b, tctx, &h))
203 return false;
205 return true;
208 static bool test_QueryServiceConfig2W(struct torture_context *tctx, struct dcerpc_pipe *p)
210 struct svcctl_QueryServiceConfig2W r;
211 struct policy_handle h, s;
212 NTSTATUS status;
213 struct dcerpc_binding_handle *b = p->binding_handle;
215 uint32_t info_level = SERVICE_CONFIG_DESCRIPTION;
216 uint8_t *buffer;
217 uint32_t offered = 0;
218 uint32_t needed = 0;
220 if (!test_OpenSCManager(b, tctx, &h))
221 return false;
223 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
224 return false;
226 buffer = talloc(tctx, uint8_t);
228 r.in.handle = &s;
229 r.in.info_level = info_level;
230 r.in.offered = offered;
231 r.out.buffer = buffer;
232 r.out.needed = &needed;
234 status = dcerpc_svcctl_QueryServiceConfig2W_r(b, tctx, &r);
235 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
237 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
238 r.in.offered = needed;
239 buffer = talloc_array(tctx, uint8_t, needed);
240 r.out.buffer = buffer;
242 status = dcerpc_svcctl_QueryServiceConfig2W_r(b, tctx, &r);
243 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
244 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
247 r.in.info_level = SERVICE_CONFIG_FAILURE_ACTIONS;
248 r.in.offered = offered;
249 r.out.buffer = buffer;
250 r.out.needed = &needed;
252 status = dcerpc_svcctl_QueryServiceConfig2W_r(b, tctx, &r);
253 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
255 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
256 r.in.offered = needed;
257 buffer = talloc_array(tctx, uint8_t, needed);
258 r.out.buffer = buffer;
260 status = dcerpc_svcctl_QueryServiceConfig2W_r(b, tctx, &r);
261 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
262 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
265 if (!test_CloseServiceHandle(b, tctx, &s))
266 return false;
268 if (!test_CloseServiceHandle(b, tctx, &h))
269 return false;
271 return true;
274 static bool test_QueryServiceObjectSecurity(struct torture_context *tctx,
275 struct dcerpc_pipe *p)
277 struct svcctl_QueryServiceObjectSecurity r;
278 struct policy_handle h, s;
279 struct dcerpc_binding_handle *b = p->binding_handle;
281 uint8_t *buffer = NULL;
282 uint32_t needed;
284 enum ndr_err_code ndr_err;
285 struct security_descriptor sd;
286 DATA_BLOB blob;
288 if (!test_OpenSCManager(b, tctx, &h))
289 return false;
291 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
292 return false;
294 r.in.handle = &s;
295 r.in.security_flags = 0;
296 r.in.offered = 0;
297 r.out.buffer = NULL;
298 r.out.needed = &needed;
300 torture_assert_ntstatus_ok(tctx,
301 dcerpc_svcctl_QueryServiceObjectSecurity_r(b, tctx, &r),
302 "QueryServiceObjectSecurity failed!");
303 torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
304 "QueryServiceObjectSecurity failed!");
306 r.in.security_flags = SECINFO_DACL;
308 torture_assert_ntstatus_ok(tctx,
309 dcerpc_svcctl_QueryServiceObjectSecurity_r(b, tctx, &r),
310 "QueryServiceObjectSecurity failed!");
312 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
313 r.in.offered = needed;
314 buffer = talloc_array(tctx, uint8_t, needed);
315 r.out.buffer = buffer;
316 torture_assert_ntstatus_ok(tctx,
317 dcerpc_svcctl_QueryServiceObjectSecurity_r(b, tctx, &r),
318 "QueryServiceObjectSecurity failed!");
321 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceObjectSecurity failed!");
323 blob = data_blob_const(buffer, needed);
325 ndr_err = ndr_pull_struct_blob(&blob, tctx, &sd,
326 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
327 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
328 return false;
331 if (DEBUGLEVEL >= 1) {
332 NDR_PRINT_DEBUG(security_descriptor, &sd);
335 if (!test_CloseServiceHandle(b, tctx, &s))
336 return false;
338 if (!test_CloseServiceHandle(b, tctx, &h))
339 return false;
341 return true;
344 static bool test_SetServiceObjectSecurity(struct torture_context *tctx,
345 struct dcerpc_pipe *p)
347 struct svcctl_QueryServiceObjectSecurity q;
348 struct svcctl_SetServiceObjectSecurity r;
349 struct policy_handle h, s;
350 struct dcerpc_binding_handle *b = p->binding_handle;
352 uint8_t *buffer;
353 uint32_t needed;
355 if (!test_OpenSCManager(b, tctx, &h))
356 return false;
358 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
359 return false;
361 q.in.handle = &s;
362 q.in.security_flags = SECINFO_DACL;
363 q.in.offered = 0;
364 q.out.buffer = NULL;
365 q.out.needed = &needed;
367 torture_assert_ntstatus_ok(tctx,
368 dcerpc_svcctl_QueryServiceObjectSecurity_r(b, tctx, &q),
369 "QueryServiceObjectSecurity failed!");
371 if (W_ERROR_EQUAL(q.out.result, WERR_INSUFFICIENT_BUFFER)) {
372 q.in.offered = needed;
373 buffer = talloc_array(tctx, uint8_t, needed);
374 q.out.buffer = buffer;
375 torture_assert_ntstatus_ok(tctx,
376 dcerpc_svcctl_QueryServiceObjectSecurity_r(b, tctx, &q),
377 "QueryServiceObjectSecurity failed!");
380 torture_assert_werr_ok(tctx, q.out.result,
381 "QueryServiceObjectSecurity failed!");
383 r.in.handle = &s;
384 r.in.security_flags = SECINFO_DACL;
385 r.in.buffer = q.out.buffer;
386 r.in.offered = *q.out.needed;
388 torture_assert_ntstatus_ok(tctx,
389 dcerpc_svcctl_SetServiceObjectSecurity_r(b, tctx, &r),
390 "SetServiceObjectSecurity failed!");
391 torture_assert_werr_ok(tctx, r.out.result,
392 "SetServiceObjectSecurity failed!");
394 if (!test_CloseServiceHandle(b, tctx, &s))
395 return false;
397 if (!test_CloseServiceHandle(b, tctx, &h))
398 return false;
400 return true;
403 static bool test_StartServiceW(struct torture_context *tctx,
404 struct dcerpc_pipe *p)
406 struct svcctl_StartServiceW r;
407 struct policy_handle h, s;
408 struct dcerpc_binding_handle *b = p->binding_handle;
410 if (!test_OpenSCManager(b, tctx, &h))
411 return false;
413 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
414 return false;
416 r.in.handle = &s;
417 r.in.NumArgs = 0;
418 r.in.Arguments = NULL;
420 torture_assert_ntstatus_ok(tctx,
421 dcerpc_svcctl_StartServiceW_r(b, tctx, &r),
422 "StartServiceW failed!");
423 torture_assert_werr_equal(tctx, r.out.result,
424 WERR_SERVICE_ALREADY_RUNNING,
425 "StartServiceW failed!");
427 if (!test_CloseServiceHandle(b, tctx, &s))
428 return false;
430 if (!test_CloseServiceHandle(b, tctx, &h))
431 return false;
433 return true;
436 static bool test_ControlService(struct torture_context *tctx,
437 struct dcerpc_pipe *p)
439 struct svcctl_ControlService r;
440 struct policy_handle h, s;
441 struct SERVICE_STATUS service_status;
442 struct dcerpc_binding_handle *b = p->binding_handle;
444 if (!test_OpenSCManager(b, tctx, &h))
445 return false;
447 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
448 return false;
450 r.in.handle = &s;
451 r.in.control = 0;
452 r.out.service_status = &service_status;
454 torture_assert_ntstatus_ok(tctx,
455 dcerpc_svcctl_ControlService_r(b, tctx, &r),
456 "ControlService failed!");
457 torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
458 "ControlService failed!");
460 if (!test_CloseServiceHandle(b, tctx, &s))
461 return false;
463 if (!test_CloseServiceHandle(b, tctx, &h))
464 return false;
466 return true;
469 static bool test_EnumServicesStatus(struct torture_context *tctx, struct dcerpc_pipe *p)
471 struct svcctl_EnumServicesStatusW r;
472 struct policy_handle h;
473 int i;
474 NTSTATUS status;
475 uint32_t resume_handle = 0;
476 struct ENUM_SERVICE_STATUSW *service = NULL;
477 uint32_t needed = 0;
478 uint32_t services_returned = 0;
479 struct dcerpc_binding_handle *b = p->binding_handle;
481 if (!test_OpenSCManager(b, tctx, &h))
482 return false;
484 r.in.handle = &h;
485 r.in.type = SERVICE_TYPE_WIN32;
486 r.in.state = SERVICE_STATE_ALL;
487 r.in.offered = 0;
488 r.in.resume_handle = &resume_handle;
489 r.out.service = NULL;
490 r.out.resume_handle = &resume_handle;
491 r.out.services_returned = &services_returned;
492 r.out.needed = &needed;
494 status = dcerpc_svcctl_EnumServicesStatusW_r(b, tctx, &r);
496 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
498 if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
499 r.in.offered = needed;
500 r.out.service = talloc_array(tctx, uint8_t, needed);
502 status = dcerpc_svcctl_EnumServicesStatusW_r(b, tctx, &r);
504 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
505 torture_assert_werr_ok(tctx, r.out.result, "EnumServicesStatus failed");
508 if (services_returned > 0) {
510 enum ndr_err_code ndr_err;
511 DATA_BLOB blob;
512 struct ndr_pull *ndr;
514 blob.length = r.in.offered;
515 blob.data = talloc_steal(tctx, r.out.service);
517 ndr = ndr_pull_init_blob(&blob, tctx);
519 service = talloc_array(tctx, struct ENUM_SERVICE_STATUSW, services_returned);
520 if (!service) {
521 return false;
524 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
525 ndr, services_returned, service);
526 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
527 return false;
531 for(i = 0; i < services_returned; i++) {
533 torture_assert(tctx, service[i].service_name,
534 "Service without name returned!");
536 printf("%-20s \"%s\", Type: %d, State: %d\n",
537 service[i].service_name, service[i].display_name,
538 service[i].status.type, service[i].status.state);
541 if (!test_CloseServiceHandle(b, tctx, &h))
542 return false;
544 return true;
547 static bool test_EnumDependentServicesW(struct torture_context *tctx,
548 struct dcerpc_pipe *p)
550 struct svcctl_EnumDependentServicesW r;
551 struct policy_handle h, s;
552 uint32_t needed;
553 uint32_t services_returned;
554 uint32_t i;
555 uint32_t states[] = { SERVICE_STATE_ACTIVE,
556 SERVICE_STATE_INACTIVE,
557 SERVICE_STATE_ALL };
558 struct dcerpc_binding_handle *b = p->binding_handle;
560 if (!test_OpenSCManager(b, tctx, &h))
561 return false;
563 if (!test_OpenService(b, tctx, &h, TORTURE_DEFAULT_SERVICE, &s))
564 return false;
566 r.in.service = &s;
567 r.in.offered = 0;
568 r.in.state = 0;
569 r.out.service_status = NULL;
570 r.out.services_returned = &services_returned;
571 r.out.needed = &needed;
573 torture_assert_ntstatus_ok(tctx,
574 dcerpc_svcctl_EnumDependentServicesW_r(b, tctx, &r),
575 "EnumDependentServicesW failed!");
577 torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
578 "EnumDependentServicesW failed!");
580 for (i=0; i<ARRAY_SIZE(states); i++) {
582 r.in.state = states[i];
584 torture_assert_ntstatus_ok(tctx,
585 dcerpc_svcctl_EnumDependentServicesW_r(b, tctx, &r),
586 "EnumDependentServicesW failed!");
588 if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
589 r.in.offered = needed;
590 r.out.service_status = talloc_array(tctx, uint8_t, needed);
592 torture_assert_ntstatus_ok(tctx,
593 dcerpc_svcctl_EnumDependentServicesW_r(b, tctx, &r),
594 "EnumDependentServicesW failed!");
598 torture_assert_werr_ok(tctx, r.out.result,
599 "EnumDependentServicesW failed");
602 if (!test_CloseServiceHandle(b, tctx, &s))
603 return false;
605 if (!test_CloseServiceHandle(b, tctx, &h))
606 return false;
608 return true;
611 static bool test_SCManager(struct torture_context *tctx,
612 struct dcerpc_pipe *p)
614 struct policy_handle h;
615 struct dcerpc_binding_handle *b = p->binding_handle;
617 if (!test_OpenSCManager(b, tctx, &h))
618 return false;
620 if (!test_CloseServiceHandle(b, tctx, &h))
621 return false;
623 return true;
626 struct torture_suite *torture_rpc_svcctl(TALLOC_CTX *mem_ctx)
628 struct torture_suite *suite = torture_suite_create(mem_ctx, "svcctl");
629 struct torture_rpc_tcase *tcase;
631 tcase = torture_suite_add_rpc_iface_tcase(suite, "svcctl", &ndr_table_svcctl);
633 torture_rpc_tcase_add_test(tcase, "SCManager",
634 test_SCManager);
635 torture_rpc_tcase_add_test(tcase, "EnumServicesStatus",
636 test_EnumServicesStatus);
637 torture_rpc_tcase_add_test(tcase, "EnumDependentServicesW",
638 test_EnumDependentServicesW);
639 torture_rpc_tcase_add_test(tcase, "QueryServiceStatus",
640 test_QueryServiceStatus);
641 torture_rpc_tcase_add_test(tcase, "QueryServiceStatusEx",
642 test_QueryServiceStatusEx);
643 torture_rpc_tcase_add_test(tcase, "QueryServiceConfigW",
644 test_QueryServiceConfigW);
645 torture_rpc_tcase_add_test(tcase, "QueryServiceConfig2W",
646 test_QueryServiceConfig2W);
647 torture_rpc_tcase_add_test(tcase, "QueryServiceObjectSecurity",
648 test_QueryServiceObjectSecurity);
649 torture_rpc_tcase_add_test(tcase, "SetServiceObjectSecurity",
650 test_SetServiceObjectSecurity);
651 torture_rpc_tcase_add_test(tcase, "StartServiceW",
652 test_StartServiceW);
653 torture_rpc_tcase_add_test(tcase, "ControlService",
654 test_ControlService);
656 return suite;