224a9e4ffa7b321043a6d4a3f6de8c0109e22d1e
[metze/samba/wip.git] / source4 / torture / rpc / epmapper.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for epmapper rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
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_epmapper_c.h"
25 #include "librpc/rpc/dcerpc_table.h"
26 #include "torture/rpc/rpc.h"
27
28
29 /*
30   display any protocol tower
31  */
32 static void display_tower(TALLOC_CTX *mem_ctx, struct epm_tower *twr)
33 {
34         int i;
35
36         for (i=0;i<twr->num_floors;i++) {
37                 printf(" %s", epm_floor_string(mem_ctx, &twr->floors[i]));
38         }
39         printf("\n");
40 }
41
42
43 static BOOL test_Map(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
44                      struct epm_twr_t *twr)
45 {
46         NTSTATUS status;
47         struct epm_Map r;
48         struct GUID uuid;
49         struct policy_handle handle;
50         int i;
51         struct dcerpc_syntax_id syntax;
52         uint32_t num_towers;
53
54         ZERO_STRUCT(uuid);
55         ZERO_STRUCT(handle);
56
57         r.in.object = &uuid;
58         r.in.map_tower = twr;
59         r.in.entry_handle = &handle;    
60         r.out.entry_handle = &handle;
61         r.in.max_towers = 100;
62         r.out.num_towers = &num_towers;
63
64         dcerpc_floor_get_lhs_data(&twr->tower.floors[0], &syntax);
65
66         printf("epm_Map results for '%s':\n", 
67                idl_pipe_name(&syntax.uuid, syntax.if_version));
68
69         twr->tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
70         twr->tower.floors[2].lhs.lhs_data = data_blob(NULL, 0);
71         twr->tower.floors[2].rhs.ncacn.minor_version = 0;
72
73         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
74         twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
75         twr->tower.floors[3].rhs.tcp.port = 0;
76
77         twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
78         twr->tower.floors[4].lhs.lhs_data = data_blob(NULL, 0);
79         twr->tower.floors[4].rhs.ip.ipaddr = "0.0.0.0";
80
81         status = dcerpc_epm_Map(p, mem_ctx, &r);
82         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
83                 for (i=0;i<*r.out.num_towers;i++) {
84                         if (r.out.towers[i].twr) {
85                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
86                         }
87                 }
88         }
89
90         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_HTTP;
91         twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
92         twr->tower.floors[3].rhs.http.port = 0;
93
94         status = dcerpc_epm_Map(p, mem_ctx, &r);
95         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
96                 for (i=0;i<*r.out.num_towers;i++) {
97                         if (r.out.towers[i].twr) {
98                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
99                         }
100                 }
101         }
102
103         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_UDP;
104         twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
105         twr->tower.floors[3].rhs.http.port = 0;
106
107         status = dcerpc_epm_Map(p, mem_ctx, &r);
108         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
109                 for (i=0;i<*r.out.num_towers;i++) {
110                         if (r.out.towers[i].twr) {
111                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
112                         }
113                 }
114         }
115
116         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_SMB;
117         twr->tower.floors[3].lhs.lhs_data = data_blob(NULL, 0);
118         twr->tower.floors[3].rhs.smb.unc = "";
119
120         twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_NETBIOS;
121         twr->tower.floors[4].lhs.lhs_data = data_blob(NULL, 0);
122         twr->tower.floors[4].rhs.netbios.name = "";
123
124         status = dcerpc_epm_Map(p, mem_ctx, &r);
125         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
126                 for (i=0;i<*r.out.num_towers;i++) {
127                         if (r.out.towers[i].twr) {
128                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
129                         }
130                 }
131         }
132
133         /* FIXME: Extend to do other protocols as well (ncacn_unix_stream, ncalrpc) */
134         
135         return True;
136 }
137
138 static BOOL test_Lookup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
139 {
140         NTSTATUS status;
141         struct epm_Lookup r;
142         struct GUID uuid;
143         struct rpc_if_id_t iface;
144         struct policy_handle handle;
145         uint32_t num_ents;
146
147         ZERO_STRUCT(handle);
148
149         r.in.inquiry_type = 0;
150         r.in.object = &uuid;
151         r.in.interface_id = &iface;
152         r.in.vers_option = 0;
153         r.in.entry_handle = &handle;
154         r.out.entry_handle = &handle;
155         r.in.max_ents = 10;
156         r.out.num_ents = &num_ents;
157
158         do {
159                 int i;
160
161                 ZERO_STRUCT(uuid);
162                 ZERO_STRUCT(iface);
163
164                 status = dcerpc_epm_Lookup(p, mem_ctx, &r);
165                 if (!NT_STATUS_IS_OK(status) || r.out.result != 0) {
166                         break;
167                 }
168
169                 printf("epm_Lookup returned %d events GUID %s\n", 
170                        *r.out.num_ents, GUID_string(mem_ctx, &handle.uuid));
171
172                 for (i=0;i<*r.out.num_ents;i++) {
173                         printf("\nFound '%s'\n", r.out.entries[i].annotation);
174                         display_tower(mem_ctx, &r.out.entries[i].tower->tower);
175                         if (r.out.entries[i].tower->tower.num_floors == 5) {
176                                 test_Map(p, mem_ctx, r.out.entries[i].tower);
177                         }
178                 }
179         } while (NT_STATUS_IS_OK(status) && 
180                  r.out.result == 0 && 
181                  *r.out.num_ents == r.in.max_ents &&
182                  !policy_handle_empty(&handle));
183
184         if (!NT_STATUS_IS_OK(status)) {
185                 printf("Lookup failed - %s\n", nt_errstr(status));
186                 return False;
187         }
188
189
190         return True;
191 }
192
193 static BOOL test_Delete(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct epm_entry_t *entries)
194 {
195         NTSTATUS status;
196         struct epm_Delete r;
197
198         r.in.num_ents = 1;
199         r.in.entries = entries;
200         
201         status = dcerpc_epm_Delete(p, mem_ctx, &r);
202         if (NT_STATUS_IS_ERR(status)) {
203                 printf("Delete failed - %s\n", nt_errstr(status));
204                 return False;
205         }
206
207         if (r.out.result != 0) {
208                 printf("Delete failed - %d\n", r.out.result);
209                 return False;
210         }
211
212         return True;
213 }
214
215 static BOOL test_Insert(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
216 {
217         NTSTATUS status;
218         struct epm_Insert r;
219         struct dcerpc_binding *bd;
220
221         r.in.num_ents = 1;
222
223         r.in.entries = talloc_array(mem_ctx, struct epm_entry_t, 1);
224         ZERO_STRUCT(r.in.entries[0].object);
225         r.in.entries[0].annotation = "smbtorture endpoint";
226         status = dcerpc_parse_binding(mem_ctx, "ncalrpc:[SMBTORTURE]", &bd);
227         if (NT_STATUS_IS_ERR(status)) {
228                 printf("Unable to generate dcerpc_binding struct\n");
229                 return False;
230         }
231
232         r.in.entries[0].tower = talloc(mem_ctx, struct epm_twr_t);
233
234         status = dcerpc_binding_build_tower(mem_ctx, bd, &r.in.entries[0].tower->tower);
235         if (NT_STATUS_IS_ERR(status)) {
236                 printf("Unable to build tower from binding struct\n");
237                 return False;
238         }
239         
240         r.in.replace = 0;
241
242         status = dcerpc_epm_Insert(p, mem_ctx, &r);
243         if (NT_STATUS_IS_ERR(status)) {
244                 printf("Insert failed - %s\n", nt_errstr(status));
245                 return False;
246         }
247
248         if (r.out.result != 0) {
249                 printf("Insert failed - %d\n", r.out.result);
250                 printf("NOT CONSIDERING AS A FAILURE\n");
251                 return True;
252         }
253
254         if (!test_Delete(p, mem_ctx, r.in.entries)) {
255                 return False; 
256         }
257
258         return True;
259 }
260
261 static BOOL test_InqObject(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
262 {
263         NTSTATUS status;
264         struct epm_InqObject r;
265
266         r.in.epm_object = talloc(mem_ctx, struct GUID);
267         *r.in.epm_object = dcerpc_table_epmapper.syntax_id.uuid;
268
269         status = dcerpc_epm_InqObject(p, mem_ctx, &r);
270         if (NT_STATUS_IS_ERR(status)) {
271                 printf("InqObject failed - %s\n", nt_errstr(status));
272                 return False;
273         }
274
275         return True;
276 }
277
278 BOOL torture_rpc_epmapper(struct torture_context *torture)
279 {
280         NTSTATUS status;
281         struct dcerpc_pipe *p;
282         TALLOC_CTX *mem_ctx;
283         BOOL ret = True;
284
285         mem_ctx = talloc_init("torture_rpc_epmapper");
286
287         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_epmapper);
288         if (!NT_STATUS_IS_OK(status)) {
289                 talloc_free(mem_ctx);
290                 return False;
291         }
292
293         if (!test_Lookup(p, mem_ctx)) {
294                 ret = False;
295         }
296
297         if (!test_Insert(p, mem_ctx)) {
298                 ret = False;
299         }
300
301         if (!test_InqObject(p, mem_ctx)) {
302                 ret = False;
303         }
304
305         talloc_free(mem_ctx);
306
307         return ret;
308 }