r6961: Add RPC-WINREG to list of win2k3 tests
[samba.git] / source / torture / rpc / svcctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for srvsvc rpc operations
4
5    Copyright (C) Jelmer Vernooij 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_svcctl.h"
24 #include "librpc/gen_ndr/ndr_security.h"
25
26 static BOOL test_EnumServicesStatus(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
27 {
28         struct svcctl_EnumServicesStatusW r;
29         int i;
30         NTSTATUS status;
31         uint32_t resume_handle = 0;
32         struct ENUM_SERVICE_STATUS *service = NULL; 
33
34         r.in.handle = h;
35         r.in.type = SERVICE_TYPE_WIN32;
36         r.in.state = SERVICE_STATE_ALL;
37         r.in.buf_size = 0;
38         r.in.resume_handle = &resume_handle;
39         r.out.service = NULL;
40         r.out.resume_handle = &resume_handle;
41         r.out.services_returned = 0;
42         r.out.bytes_needed = 0;
43
44         status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
45
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("ËnumServicesStatus failed!\n");
48                 return False;
49         }
50
51         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
52                 r.in.buf_size = r.out.bytes_needed;
53                 r.out.service = talloc_size(mem_ctx, r.out.bytes_needed);
54                 
55                 status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
56
57                 if (!NT_STATUS_IS_OK(status)) {
58                         printf("ËnumServicesStatus failed!\n");
59                         return False;
60                 }
61
62                 if (!W_ERROR_IS_OK(r.out.result)) {
63                         printf("EnumServicesStatus failed\n");
64                         return False;
65                 }
66                 service = (struct ENUM_SERVICE_STATUS *)r.out.service;
67         }
68
69         for(i = 0; i < r.out.services_returned; i++) {
70                 printf("Type: %d, State: %d\n", service[i].status.type, service[i].status.state);
71         }
72                 
73         return True;
74 }
75
76 static BOOL test_OpenSCManager(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
77 {
78         struct svcctl_OpenSCManagerW r;
79         NTSTATUS status;
80         
81         r.in.MachineName = NULL;
82         r.in.DatabaseName = NULL;
83         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
84         r.out.handle = h;
85         
86         status = dcerpc_svcctl_OpenSCManagerW(p, mem_ctx, &r);
87         if (!NT_STATUS_IS_OK(status)) {
88                 printf("OpenSCManager failed!\n");
89                 return False;
90         }
91         
92         return True;
93 }
94
95 static BOOL test_CloseServiceHandle(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
96 {
97         struct svcctl_CloseServiceHandle r; 
98         NTSTATUS status;
99         r.in.handle = h;
100         r.out.handle = h;
101         status = dcerpc_svcctl_CloseServiceHandle(p, mem_ctx, &r);
102         if (!NT_STATUS_IS_OK(status)) {
103                 printf("CloseServiceHandle failed\n");
104                 return False;
105         }
106
107         return True;
108 }
109
110 BOOL torture_rpc_svcctl(void)
111 {
112         NTSTATUS status;
113         struct dcerpc_pipe *p;
114                 struct policy_handle h;
115         TALLOC_CTX *mem_ctx;
116         BOOL ret = True;
117
118         mem_ctx = talloc_init("torture_rpc_svcctl");
119
120         status = torture_rpc_connection(mem_ctx, 
121                                         &p,
122                                         DCERPC_SVCCTL_NAME,
123                                         DCERPC_SVCCTL_UUID,
124                                         DCERPC_SVCCTL_VERSION);
125         if (!NT_STATUS_IS_OK(status)) {
126                 talloc_free(mem_ctx);
127                 return False;
128         }
129
130         if (!test_OpenSCManager(p, mem_ctx, &h)) {
131                 ret = False;
132         }
133
134         if (!test_EnumServicesStatus(p, mem_ctx, &h)) {
135                 ret = False;
136         }
137
138         if (!test_CloseServiceHandle(p, mem_ctx, &h)) {
139                 ret = False;
140         }
141
142         talloc_free(mem_ctx);
143
144         return ret;
145 }