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