r23792: convert Samba4 to GPLv3
[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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "librpc/gen_ndr/ndr_epmapper_c.h"
24 #include "librpc/rpc/dcerpc_table.h"
25 #include "torture/rpc/rpc.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 dcerpc_syntax_id syntax;
51         uint32_t num_towers;
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         r.out.num_towers = &num_towers;
62
63         dcerpc_floor_get_lhs_data(&twr->tower.floors[0], &syntax);
64
65         printf("epm_Map results for '%s':\n", 
66                idl_pipe_name(&syntax.uuid, syntax.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         uint32_t num_ents;
145
146         ZERO_STRUCT(handle);
147
148         r.in.inquiry_type = 0;
149         r.in.object = &uuid;
150         r.in.interface_id = &iface;
151         r.in.vers_option = 0;
152         r.in.entry_handle = &handle;
153         r.out.entry_handle = &handle;
154         r.in.max_ents = 10;
155         r.out.num_ents = &num_ents;
156
157         do {
158                 int i;
159
160                 ZERO_STRUCT(uuid);
161                 ZERO_STRUCT(iface);
162
163                 status = dcerpc_epm_Lookup(p, mem_ctx, &r);
164                 if (!NT_STATUS_IS_OK(status) || r.out.result != 0) {
165                         break;
166                 }
167
168                 printf("epm_Lookup returned %d events GUID %s\n", 
169                        *r.out.num_ents, GUID_string(mem_ctx, &handle.uuid));
170
171                 for (i=0;i<*r.out.num_ents;i++) {
172                         printf("\nFound '%s'\n", r.out.entries[i].annotation);
173                         display_tower(mem_ctx, &r.out.entries[i].tower->tower);
174                         if (r.out.entries[i].tower->tower.num_floors == 5) {
175                                 test_Map(p, mem_ctx, r.out.entries[i].tower);
176                         }
177                 }
178         } while (NT_STATUS_IS_OK(status) && 
179                  r.out.result == 0 && 
180                  *r.out.num_ents == r.in.max_ents &&
181                  !policy_handle_empty(&handle));
182
183         if (!NT_STATUS_IS_OK(status)) {
184                 printf("Lookup failed - %s\n", nt_errstr(status));
185                 return False;
186         }
187
188
189         return True;
190 }
191
192 static BOOL test_Delete(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct epm_entry_t *entries)
193 {
194         NTSTATUS status;
195         struct epm_Delete r;
196
197         r.in.num_ents = 1;
198         r.in.entries = entries;
199         
200         status = dcerpc_epm_Delete(p, mem_ctx, &r);
201         if (NT_STATUS_IS_ERR(status)) {
202                 printf("Delete failed - %s\n", nt_errstr(status));
203                 return False;
204         }
205
206         if (r.out.result != 0) {
207                 printf("Delete failed - %d\n", r.out.result);
208                 return False;
209         }
210
211         return True;
212 }
213
214 static BOOL test_Insert(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
215 {
216         NTSTATUS status;
217         struct epm_Insert r;
218         struct dcerpc_binding *bd;
219
220         r.in.num_ents = 1;
221
222         r.in.entries = talloc_array(mem_ctx, struct epm_entry_t, 1);
223         ZERO_STRUCT(r.in.entries[0].object);
224         r.in.entries[0].annotation = "smbtorture endpoint";
225         status = dcerpc_parse_binding(mem_ctx, "ncalrpc:[SMBTORTURE]", &bd);
226         if (NT_STATUS_IS_ERR(status)) {
227                 printf("Unable to generate dcerpc_binding struct\n");
228                 return False;
229         }
230
231         r.in.entries[0].tower = talloc(mem_ctx, struct epm_twr_t);
232
233         status = dcerpc_binding_build_tower(mem_ctx, bd, &r.in.entries[0].tower->tower);
234         if (NT_STATUS_IS_ERR(status)) {
235                 printf("Unable to build tower from binding struct\n");
236                 return False;
237         }
238         
239         r.in.replace = 0;
240
241         status = dcerpc_epm_Insert(p, mem_ctx, &r);
242         if (NT_STATUS_IS_ERR(status)) {
243                 printf("Insert failed - %s\n", nt_errstr(status));
244                 return False;
245         }
246
247         if (r.out.result != 0) {
248                 printf("Insert failed - %d\n", r.out.result);
249                 printf("NOT CONSIDERING AS A FAILURE\n");
250                 return True;
251         }
252
253         if (!test_Delete(p, mem_ctx, r.in.entries)) {
254                 return False; 
255         }
256
257         return True;
258 }
259
260 static BOOL test_InqObject(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
261 {
262         NTSTATUS status;
263         struct epm_InqObject r;
264
265         r.in.epm_object = talloc(mem_ctx, struct GUID);
266         *r.in.epm_object = dcerpc_table_epmapper.syntax_id.uuid;
267
268         status = dcerpc_epm_InqObject(p, mem_ctx, &r);
269         if (NT_STATUS_IS_ERR(status)) {
270                 printf("InqObject failed - %s\n", nt_errstr(status));
271                 return False;
272         }
273
274         return True;
275 }
276
277 BOOL torture_rpc_epmapper(struct torture_context *torture)
278 {
279         NTSTATUS status;
280         struct dcerpc_pipe *p;
281         TALLOC_CTX *mem_ctx;
282         BOOL ret = True;
283
284         mem_ctx = talloc_init("torture_rpc_epmapper");
285
286         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_epmapper);
287         if (!NT_STATUS_IS_OK(status)) {
288                 talloc_free(mem_ctx);
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         return ret;
307 }