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/>.
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
;
42 torture_assert_ntstatus_ok(tctx
,
43 dcerpc_svcctl_OpenSCManagerW_r(b
, tctx
, &r
),
44 "OpenSCManager failed!");
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
;
57 torture_assert_ntstatus_ok(tctx
,
58 dcerpc_svcctl_CloseServiceHandle_r(b
, tctx
, &r
),
59 "CloseServiceHandle failed");
64 static bool test_OpenService(struct dcerpc_binding_handle
*b
,
65 struct torture_context
*tctx
,
66 struct policy_handle
*h
,
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
;
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!");
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
;
93 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
95 if (!test_OpenSCManager(b
, tctx
, &h
))
98 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &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
))
111 if (!test_CloseServiceHandle(b
, tctx
, &h
))
117 static bool test_QueryServiceStatusEx(struct torture_context
*tctx
, struct dcerpc_pipe
*p
)
119 struct svcctl_QueryServiceStatusEx r
;
120 struct policy_handle h
, s
;
122 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
124 uint32_t info_level
= SVC_STATUS_PROCESS_INFO
;
126 uint32_t offered
= 0;
129 if (!test_OpenSCManager(b
, tctx
, &h
))
132 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
135 buffer
= talloc(tctx
, uint8_t);
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
))
159 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
;
172 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
174 uint32_t offered
= 0;
177 if (!test_OpenSCManager(b
, tctx
, &h
))
180 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &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
))
202 if (!test_CloseServiceHandle(b
, tctx
, &h
))
208 static bool test_QueryServiceConfig2W(struct torture_context
*tctx
, struct dcerpc_pipe
*p
)
210 struct svcctl_QueryServiceConfig2W r
;
211 struct policy_handle h
, s
;
213 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
215 uint32_t info_level
= SERVICE_CONFIG_DESCRIPTION
;
217 uint32_t offered
= 0;
220 if (!test_OpenSCManager(b
, tctx
, &h
))
223 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
226 buffer
= talloc(tctx
, uint8_t);
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
))
268 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
;
284 enum ndr_err_code ndr_err
;
285 struct security_descriptor sd
;
288 if (!test_OpenSCManager(b
, tctx
, &h
))
291 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
295 r
.in
.security_flags
= 0;
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
)) {
331 if (DEBUGLEVEL
>= 1) {
332 NDR_PRINT_DEBUG(security_descriptor
, &sd
);
335 if (!test_CloseServiceHandle(b
, tctx
, &s
))
338 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
;
355 if (!test_OpenSCManager(b
, tctx
, &h
))
358 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
362 q
.in
.security_flags
= SECINFO_DACL
;
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!");
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
))
397 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
))
413 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
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
))
430 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
))
447 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
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
))
463 if (!test_CloseServiceHandle(b
, tctx
, &h
))
469 static bool test_EnumServicesStatus(struct torture_context
*tctx
, struct dcerpc_pipe
*p
)
471 struct svcctl_EnumServicesStatusW r
;
472 struct policy_handle h
;
475 uint32_t resume_handle
= 0;
476 struct ENUM_SERVICE_STATUSW
*service
= NULL
;
478 uint32_t services_returned
= 0;
479 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
481 if (!test_OpenSCManager(b
, tctx
, &h
))
485 r
.in
.type
= SERVICE_TYPE_WIN32
;
486 r
.in
.state
= SERVICE_STATE_ALL
;
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
;
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
);
524 ndr_err
= ndr_pull_ENUM_SERVICE_STATUSW_array(
525 ndr
, services_returned
, service
);
526 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
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
))
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
;
553 uint32_t services_returned
;
555 uint32_t states
[] = { SERVICE_STATE_ACTIVE
,
556 SERVICE_STATE_INACTIVE
,
558 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
560 if (!test_OpenSCManager(b
, tctx
, &h
))
563 if (!test_OpenService(b
, tctx
, &h
, TORTURE_DEFAULT_SERVICE
, &s
))
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
))
605 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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
))
620 if (!test_CloseServiceHandle(b
, tctx
, &h
))
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",
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",
653 torture_rpc_tcase_add_test(tcase
, "ControlService",
654 test_ControlService
);