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