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