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