6c15249950f3cdd1218a2cab2edbf8521e2ac7c7
[jra/samba/.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    
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 "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_svcctl_c.h"
25 #include "torture/rpc/rpc.h"
26
27 static BOOL test_EnumServicesStatus(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
28 {
29         struct svcctl_EnumServicesStatusW r;
30         int i;
31         NTSTATUS status;
32         uint32_t resume_handle = 0;
33         struct ENUM_SERVICE_STATUS *service = NULL; 
34
35         r.in.handle = h;
36         r.in.type = SERVICE_TYPE_WIN32;
37         r.in.state = SERVICE_STATE_ALL;
38         r.in.buf_size = 0;
39         r.in.resume_handle = &resume_handle;
40         r.out.service = NULL;
41         r.out.resume_handle = &resume_handle;
42         r.out.services_returned = 0;
43         r.out.bytes_needed = 0;
44
45         status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
46
47         if (!NT_STATUS_IS_OK(status)) {
48                 printf("ËnumServicesStatus failed!\n");
49                 return False;
50         }
51
52         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
53                 r.in.buf_size = r.out.bytes_needed;
54                 r.out.service = talloc_size(mem_ctx, r.out.bytes_needed);
55                 
56                 status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
57
58                 if (!NT_STATUS_IS_OK(status)) {
59                         printf("ËnumServicesStatus failed!\n");
60                         return False;
61                 }
62
63                 if (!W_ERROR_IS_OK(r.out.result)) {
64                         printf("EnumServicesStatus failed\n");
65                         return False;
66                 }
67                 service = (struct ENUM_SERVICE_STATUS *)r.out.service;
68         }
69
70         for(i = 0; i < r.out.services_returned; i++) {
71                 printf("Type: %d, State: %d\n", service[i].status.type, service[i].status.state);
72         }
73                 
74         return True;
75 }
76
77 static BOOL test_OpenSCManager(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
78 {
79         struct svcctl_OpenSCManagerW r;
80         NTSTATUS status;
81         
82         r.in.MachineName = NULL;
83         r.in.DatabaseName = NULL;
84         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
85         r.out.handle = h;
86         
87         status = dcerpc_svcctl_OpenSCManagerW(p, mem_ctx, &r);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("OpenSCManager failed!\n");
90                 return False;
91         }
92         
93         return True;
94 }
95
96 static BOOL test_CloseServiceHandle(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
97 {
98         struct svcctl_CloseServiceHandle r; 
99         NTSTATUS status;
100         r.in.handle = h;
101         r.out.handle = h;
102         status = dcerpc_svcctl_CloseServiceHandle(p, mem_ctx, &r);
103         if (!NT_STATUS_IS_OK(status)) {
104                 printf("CloseServiceHandle failed\n");
105                 return False;
106         }
107
108         return True;
109 }
110
111 BOOL torture_rpc_svcctl(struct torture_context *torture)
112 {
113         NTSTATUS status;
114         struct dcerpc_pipe *p;
115                 struct policy_handle h;
116         TALLOC_CTX *mem_ctx;
117         BOOL ret = True;
118
119         mem_ctx = talloc_init("torture_rpc_svcctl");
120
121         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_svcctl);
122         if (!NT_STATUS_IS_OK(status)) {
123                 talloc_free(mem_ctx);
124                 return False;
125         }
126
127         if (!test_OpenSCManager(p, mem_ctx, &h)) {
128                 ret = False;
129         }
130
131         if (!test_EnumServicesStatus(p, mem_ctx, &h)) {
132                 ret = False;
133         }
134
135         if (!test_CloseServiceHandle(p, mem_ctx, &h)) {
136                 ret = False;
137         }
138
139         talloc_free(mem_ctx);
140
141         return ret;
142 }