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