r3428: switched to using minimal includes for the auto-generated RPC code.
[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
25 static BOOL test_EnumServicesStatus(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
26 {
27         struct svcctl_EnumServicesStatusW r;
28         int i;
29         NTSTATUS status;
30         uint32 resume_handle = 0;
31         struct ENUM_SERVICE_STATUS *service = NULL; 
32
33         r.in.handle = h;
34         r.in.type = SERVICE_TYPE_WIN32;
35         r.in.state = SERVICE_STATE_ALL;
36         r.in.buf_size = 0;
37         r.in.resume_handle = &resume_handle;
38         r.out.service = NULL;
39         r.out.resume_handle = &resume_handle;
40         r.out.services_returned = 0;
41         r.out.bytes_needed = 0;
42
43         status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
44
45         if (!NT_STATUS_IS_OK(status)) {
46                 printf("ËnumServicesStatus failed!\n");
47                 return False;
48         }
49
50         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
51                 r.in.buf_size = r.out.bytes_needed;
52                 r.out.service = talloc(mem_ctx, r.out.bytes_needed);
53                 
54                 status = dcerpc_svcctl_EnumServicesStatusW(p, mem_ctx, &r);
55
56                 if (!NT_STATUS_IS_OK(status)) {
57                         printf("ËnumServicesStatus failed!\n");
58                         return False;
59                 }
60
61                 if (!W_ERROR_IS_OK(r.out.result)) {
62                         printf("EnumServicesStatus failed\n");
63                         return False;
64                 }
65                 service = (struct ENUM_SERVICE_STATUS *)r.out.service;
66         }
67
68         for(i = 0; i < r.out.services_returned; i++) {
69                 printf("Type: %d, State: %d\n", service[i].status.type, service[i].status.state);
70         }
71                 
72         return True;
73 }
74
75 static BOOL test_OpenSCManager(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
76 {
77         struct svcctl_OpenSCManagerW r;
78         NTSTATUS status;
79         
80         r.in.MachineName = NULL;
81         r.in.DatabaseName = NULL;
82         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
83         r.out.handle = h;
84         
85         status = dcerpc_svcctl_OpenSCManagerW(p, mem_ctx, &r);
86         if (!NT_STATUS_IS_OK(status)) {
87                 printf("OpenSCManager failed!\n");
88                 return False;
89         }
90         
91         return True;
92 }
93
94 static BOOL test_CloseServiceHandle(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *h)
95 {
96         struct svcctl_CloseServiceHandle r; 
97         NTSTATUS status;
98         r.in.handle = h;
99         r.out.handle = h;
100         status = dcerpc_svcctl_CloseServiceHandle(p, mem_ctx, &r);
101         if (!NT_STATUS_IS_OK(status)) {
102                 printf("CloseServiceHandle failed\n");
103                 return False;
104         }
105
106         return True;
107 }
108
109 BOOL torture_rpc_svcctl(void)
110 {
111         NTSTATUS status;
112         struct dcerpc_pipe *p;
113                 struct policy_handle h;
114         TALLOC_CTX *mem_ctx;
115         BOOL ret = True;
116
117         mem_ctx = talloc_init("torture_rpc_svcctl");
118
119         status = torture_rpc_connection(&p,
120                                         DCERPC_SVCCTL_NAME,
121                                         DCERPC_SVCCTL_UUID,
122                                         DCERPC_SVCCTL_VERSION);
123         if (!NT_STATUS_IS_OK(status)) {
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_destroy(mem_ctx);
140
141     torture_rpc_close(p);
142
143         return ret;
144 }