r1818: _really_ fixed epmapper this time, it was using more than one old rpc interfac...
[jelmer/samba4-debian.git] / source / 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
24
25 /*
26   display any protocol tower
27  */
28 static void display_tower(TALLOC_CTX *mem_ctx, struct epm_towers *twr)
29 {
30         int i;
31         const char *uuid;
32
33         for (i=0;i<twr->num_floors;i++) {
34                 struct epm_lhs *lhs = &twr->floors[i].lhs;
35                 struct epm_rhs *rhs = &twr->floors[i].rhs;
36                 switch (lhs->protocol) {
37                 case EPM_PROTOCOL_NCACN_DNET_NSP:
38                         printf(" DNET/NSP");
39                         break;
40                         
41                 case EPM_PROTOCOL_UUID:
42                         uuid = GUID_string(mem_ctx, &lhs->info.uuid.uuid);
43                         if (strcasecmp(uuid, NDR_GUID) == 0) {
44                                 printf(" NDR");
45                         } else {
46                                 printf(" uuid %s/0x%02x", uuid, lhs->info.uuid.version);
47                         }
48                         break;
49
50                 case EPM_PROTOCOL_NCACN_RPC_C:
51                         printf(" RPC-C");
52                         break;
53
54                 case EPM_PROTOCOL_NCACN_IP:
55                         printf(" IP:");
56                         if (rhs->rhs_data.length == 4) {
57                                 struct in_addr in;
58                                 in.s_addr = IVAL(rhs->rhs_data.data, 0);
59                                 printf("%s", inet_ntoa(in));
60                         }
61                         break;
62
63                 case EPM_PROTOCOL_NCACN_PIPE:
64                         printf(" PIPE:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
65                         break;
66
67                 case EPM_PROTOCOL_NCACN_SMB:
68                         printf(" SMB:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
69                         break;
70
71                 case EPM_PROTOCOL_NCACN_NETBIOS:
72                         printf(" NetBIOS:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
73                         break;
74
75                 case EPM_PROTOCOL_NCACN_NB_NB:
76                         printf(" NB_NB");
77                         break;
78
79                 case EPM_PROTOCOL_NCACN_SPX:
80                         printf(" SPX");
81                         break;
82
83                         /*
84                 case EPM_PROTOCOL_NCACN_NB_IPX:
85                         printf(" NB_IPX");
86                         break;*/
87
88                 case 0x01:
89                         printf(" UNK(1):%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
90                         break;
91
92                 case EPM_PROTOCOL_NCACN_HTTP:
93                         printf(" HTTP:");
94                         if (rhs->rhs_data.length == 2) {
95                                 printf("%d", RSVAL(rhs->rhs_data.data, 0));
96                         }
97                         break;
98
99                 case EPM_PROTOCOL_NCACN_TCP:
100                         /* what is the difference between this and 0x1f? */
101                         printf(" TCP:");
102                         if (rhs->rhs_data.length == 2) {
103                                 printf("%d", RSVAL(rhs->rhs_data.data, 0));
104                         }
105                         break;
106
107                 case EPM_PROTOCOL_NCADG_UDP:
108                         printf(" UDP:");
109                         break;
110
111                 default:
112                         printf(" UNK(%02x):", lhs->protocol);
113                         if (rhs->rhs_data.length == 2) {
114                                 printf("%d", RSVAL(rhs->rhs_data.data, 0));
115                         }
116                         break;
117                 }
118         }
119         printf("\n");
120 }
121
122
123 static BOOL test_Map(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
124                      struct epm_twr_t *twr)
125 {
126         NTSTATUS status;
127         struct epm_Map r;
128         struct GUID uuid;
129         const char *uuid_str;
130         struct policy_handle handle;
131         int i;
132
133         ZERO_STRUCT(uuid);
134         ZERO_STRUCT(handle);
135
136         r.in.object = &uuid;
137         r.in.map_tower = twr;
138         r.in.entry_handle = &handle;    
139         r.out.entry_handle = &handle;
140         r.in.max_towers = 100;
141
142         if (twr->towers.num_floors != 5) {
143                 printf(" tower has %d floors - skipping test_Map\n", twr->towers.num_floors);
144                 return True;
145         }
146
147         uuid_str = GUID_string(mem_ctx, &twr->towers.floors[0].lhs.info.uuid.uuid);
148
149         printf("epm_Map results for '%s':\n", 
150                idl_pipe_name(uuid_str, twr->towers.floors[0].lhs.info.uuid.version));
151
152         twr->towers.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN_RPC_C;
153         twr->towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
154         twr->towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
155
156         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_TCP;
157         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
158         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
159
160         twr->towers.floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_IP;
161         twr->towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
162         twr->towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 4);
163
164         status = dcerpc_epm_Map(p, mem_ctx, &r);
165         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
166                 for (i=0;i<r.out.num_towers;i++) {
167                         if (r.out.towers[i].twr) {
168                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
169                         }
170                 }
171         }
172
173         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_HTTP;
174         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
175         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
176
177         status = dcerpc_epm_Map(p, mem_ctx, &r);
178         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
179                 for (i=0;i<r.out.num_towers;i++) {
180                         if (r.out.towers[i].twr) {
181                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
182                         }
183                 }
184         }
185
186         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_SMB;
187         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
188         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
189
190         twr->towers.floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_NETBIOS;
191         twr->towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
192         twr->towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
193
194         status = dcerpc_epm_Map(p, mem_ctx, &r);
195         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
196                 for (i=0;i<r.out.num_towers;i++) {
197                         if (r.out.towers[i].twr) {
198                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
199                         }
200                 }
201         }
202         
203         return True;
204 }
205
206 static BOOL test_Lookup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
207 {
208         NTSTATUS status;
209         struct epm_Lookup r;
210         struct GUID uuid;
211         struct rpc_if_id_t iface;
212         struct policy_handle handle;
213
214         ZERO_STRUCT(uuid);
215         ZERO_STRUCT(iface);
216         ZERO_STRUCT(handle);
217
218         r.in.inquiry_type = 0;
219         r.in.object = &uuid;
220         r.in.interface_id = &iface;
221         r.in.vers_option = 0;
222         r.in.entry_handle = &handle;
223         r.out.entry_handle = &handle;
224         r.in.max_ents = 10;
225
226         do {
227                 int i;
228                 status = dcerpc_epm_Lookup(p, mem_ctx, &r);
229                 if (!NT_STATUS_IS_OK(status) || r.out.result != 0) {
230                         break;
231                 }
232                 for (i=0;i<r.out.num_ents;i++) {
233                         printf("\nFound '%s'\n", r.out.entries[i].annotation);
234                         display_tower(mem_ctx, &r.out.entries[i].tower->towers);
235                         test_Map(p, mem_ctx, r.out.entries[i].tower);
236                 }
237         } while (NT_STATUS_IS_OK(status) && 
238                  r.out.result == 0 && 
239                  r.out.num_ents == r.in.max_ents);
240
241         if (!NT_STATUS_IS_OK(status)) {
242                 printf("Lookup failed - %s\n", nt_errstr(status));
243                 return False;
244         }
245
246
247         return True;
248 }
249
250 BOOL torture_rpc_epmapper(int dummy)
251 {
252         NTSTATUS status;
253         struct dcerpc_pipe *p;
254         TALLOC_CTX *mem_ctx;
255         BOOL ret = True;
256
257         mem_ctx = talloc_init("torture_rpc_epmapper");
258
259         status = torture_rpc_connection(&p, 
260                                         DCERPC_EPMAPPER_NAME,
261                                         DCERPC_EPMAPPER_UUID,
262                                         DCERPC_EPMAPPER_VERSION);
263         if (!NT_STATUS_IS_OK(status)) {
264                 return False;
265         }
266
267         if (!test_Lookup(p, mem_ctx)) {
268                 ret = False;
269         }
270
271         talloc_destroy(mem_ctx);
272
273         torture_rpc_close(p);
274
275         return ret;
276 }