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