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