svcctl: use offered/needed for buffer sizes as in spoolss.
[ira/wip.git] / source4 / 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    Copyright (C) Guenther Deschner 2008
7
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.
12
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.
17
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/>.
20 */
21
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"
28
29 static bool test_OpenSCManager(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
30 {
31         struct svcctl_OpenSCManagerW r;
32
33         r.in.MachineName = NULL;
34         r.in.DatabaseName = NULL;
35         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
36         r.out.handle = h;
37
38         torture_assert_ntstatus_ok(tctx,
39                                    dcerpc_svcctl_OpenSCManagerW(p, tctx, &r),
40                                    "OpenSCManager failed!");
41
42         return true;
43 }
44
45 static bool test_CloseServiceHandle(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
46 {
47         struct svcctl_CloseServiceHandle r;
48
49         r.in.handle = h;
50         r.out.handle = h;
51         torture_assert_ntstatus_ok(tctx,
52                                    dcerpc_svcctl_CloseServiceHandle(p, tctx, &r),
53                                    "CloseServiceHandle failed");
54
55         return true;
56 }
57
58 static bool test_OpenService(struct dcerpc_pipe *p, struct torture_context *tctx,
59                              struct policy_handle *h, const char *name, struct policy_handle *s)
60 {
61         struct svcctl_OpenServiceW r;
62
63         r.in.scmanager_handle = h;
64         r.in.ServiceName = name;
65         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
66         r.out.handle = s;
67
68         torture_assert_ntstatus_ok(tctx,
69                                    dcerpc_svcctl_OpenServiceW(p, tctx, &r),
70                                    "OpenServiceW failed!");
71         torture_assert_werr_ok(tctx, r.out.result, "OpenServiceW failed!");
72
73         return true;
74
75 }
76
77 static bool test_QueryServiceStatusEx(struct torture_context *tctx, struct dcerpc_pipe *p)
78 {
79         struct svcctl_QueryServiceStatusEx r;
80         struct policy_handle h, s;
81         NTSTATUS status;
82
83         uint32_t info_level = SVC_STATUS_PROCESS_INFO;
84         uint8_t *buffer;
85         uint32_t offered = 0;
86         uint32_t needed = 0;
87
88         if (!test_OpenSCManager(p, tctx, &h))
89                 return false;
90
91         if (!test_OpenService(p, tctx, &h, "Netlogon", &s))
92                 return false;
93
94         buffer = talloc(tctx, uint8_t);
95
96         r.in.handle = &s;
97         r.in.info_level = info_level;
98         r.in.offered = offered;
99         r.out.buffer = buffer;
100         r.out.needed = &needed;
101
102         status = dcerpc_svcctl_QueryServiceStatusEx(p, tctx, &r);
103         torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
104
105         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
106                 r.in.offered = needed;
107                 buffer = talloc_array(tctx, uint8_t, needed);
108                 r.out.buffer = buffer;
109
110                 status = dcerpc_svcctl_QueryServiceStatusEx(p, tctx, &r);
111                 torture_assert_ntstatus_ok(tctx, status, "QueryServiceStatusEx failed!");
112                 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceStatusEx failed!");
113         }
114
115         if (!test_CloseServiceHandle(p, tctx, &s))
116                 return false;
117
118         if (!test_CloseServiceHandle(p, tctx, &h))
119                 return false;
120
121         return true;
122 }
123
124 static bool test_QueryServiceConfig2W(struct torture_context *tctx, struct dcerpc_pipe *p)
125 {
126         struct svcctl_QueryServiceConfig2W r;
127         struct policy_handle h, s;
128         NTSTATUS status;
129
130         uint32_t info_level = SERVICE_CONFIG_DESCRIPTION;
131         uint8_t *buffer;
132         uint32_t offered = 0;
133         uint32_t needed = 0;
134
135         if (!test_OpenSCManager(p, tctx, &h))
136                 return false;
137
138         if (!test_OpenService(p, tctx, &h, "Netlogon", &s))
139                 return false;
140
141         buffer = talloc(tctx, uint8_t);
142
143         r.in.handle = &s;
144         r.in.info_level = info_level;
145         r.in.offered = offered;
146         r.out.buffer = buffer;
147         r.out.needed = &needed;
148
149         status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
150         torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
151
152         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
153                 r.in.offered = needed;
154                 buffer = talloc_array(tctx, uint8_t, needed);
155                 r.out.buffer = buffer;
156
157                 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
158                 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
159                 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
160         }
161
162         r.in.info_level = SERVICE_CONFIG_FAILURE_ACTIONS;
163         r.in.offered = offered;
164         r.out.buffer = buffer;
165         r.out.needed = &needed;
166
167         status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
168         torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
169
170         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
171                 r.in.offered = needed;
172                 buffer = talloc_array(tctx, uint8_t, needed);
173                 r.out.buffer = buffer;
174
175                 status = dcerpc_svcctl_QueryServiceConfig2W(p, tctx, &r);
176                 torture_assert_ntstatus_ok(tctx, status, "QueryServiceConfig2W failed!");
177                 torture_assert_werr_ok(tctx, r.out.result, "QueryServiceConfig2W failed!");
178         }
179
180         if (!test_CloseServiceHandle(p, tctx, &s))
181                 return false;
182
183         if (!test_CloseServiceHandle(p, tctx, &h))
184                 return false;
185
186         return true;
187 }
188
189 static bool test_EnumServicesStatus(struct torture_context *tctx, struct dcerpc_pipe *p)
190 {
191         struct svcctl_EnumServicesStatusW r;
192         struct policy_handle h;
193         int i;
194         NTSTATUS status;
195         uint32_t resume_handle = 0;
196         struct ENUM_SERVICE_STATUSW *service = NULL;
197         uint32_t needed = 0;
198         uint32_t services_returned = 0;
199
200         if (!test_OpenSCManager(p, tctx, &h))
201                 return false;
202
203         r.in.handle = &h;
204         r.in.type = SERVICE_TYPE_WIN32;
205         r.in.state = SERVICE_STATE_ALL;
206         r.in.offered = 0;
207         r.in.resume_handle = &resume_handle;
208         r.out.service = NULL;
209         r.out.resume_handle = &resume_handle;
210         r.out.services_returned = &services_returned;
211         r.out.needed = &needed;
212
213         status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
214
215         torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
216
217         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
218                 r.in.offered = needed;
219                 r.out.service = talloc_array(tctx, uint8_t, needed);
220
221                 status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
222
223                 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
224                 torture_assert_werr_ok(tctx, r.out.result, "EnumServicesStatus failed");
225         }
226
227         if (services_returned > 0) {
228
229                 enum ndr_err_code ndr_err;
230                 DATA_BLOB blob;
231                 struct ndr_pull *ndr;
232
233                 blob.length = r.in.offered;
234                 blob.data = talloc_steal(tctx, r.out.service);
235
236                 ndr = ndr_pull_init_blob(&blob, tctx, lp_iconv_convenience(tctx->lp_ctx));
237
238                 service = talloc_array(tctx, struct ENUM_SERVICE_STATUSW, services_returned);
239                 if (!service) {
240                         return false;
241                 }
242
243                 ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
244                                 ndr, services_returned, service);
245                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
246                         return false;
247                 }
248         }
249
250         for(i = 0; i < services_returned; i++) {
251
252                 printf("%-20s   \"%s\", Type: %d, State: %d\n",
253                         service[i].service_name, service[i].display_name,
254                         service[i].status.type, service[i].status.state);
255         }
256
257         if (!test_CloseServiceHandle(p, tctx, &h))
258                 return false;
259
260         return true;
261 }
262
263 static bool test_SCManager(struct torture_context *tctx,
264                                                    struct dcerpc_pipe *p)
265 {
266         struct policy_handle h;
267
268         if (!test_OpenSCManager(p, tctx, &h))
269                 return false;
270
271         if (!test_CloseServiceHandle(p, tctx, &h))
272                 return false;
273
274         return true;
275 }
276
277 struct torture_suite *torture_rpc_svcctl(TALLOC_CTX *mem_ctx)
278 {
279         struct torture_suite *suite = torture_suite_create(mem_ctx, "SVCCTL");
280         struct torture_rpc_tcase *tcase;
281
282         tcase = torture_suite_add_rpc_iface_tcase(suite, "svcctl", &ndr_table_svcctl);
283
284         torture_rpc_tcase_add_test(tcase, "SCManager",
285                                    test_SCManager);
286         torture_rpc_tcase_add_test(tcase, "EnumServicesStatus",
287                                    test_EnumServicesStatus);
288         torture_rpc_tcase_add_test(tcase, "QueryServiceStatusEx",
289                                    test_QueryServiceStatusEx);
290         torture_rpc_tcase_add_test(tcase, "QueryServiceConfig2W",
291                                    test_QueryServiceConfig2W);
292
293         return suite;
294 }