r3428: switched to using minimal includes for the auto-generated RPC code.
[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 #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         const char *uuid;
33
34         for (i=0;i<twr->num_floors;i++) {
35                 struct epm_lhs *lhs = &twr->floors[i].lhs;
36                 union epm_rhs *rhs = &twr->floors[i].rhs;
37
38                 switch(lhs->protocol) {
39                 case EPM_PROTOCOL_UUID:
40                         uuid = GUID_string(mem_ctx, &lhs->info.uuid.uuid);
41                         if (strcasecmp(uuid, NDR_GUID) == 0) {
42                                 printf(" NDR");
43                         } else {
44                                 printf(" uuid %s/0x%02x", uuid, lhs->info.uuid.version);
45                         }
46                         break;
47
48                 case EPM_PROTOCOL_NCACN:
49                         printf(" RPC-C");
50                         break;
51
52                 case EPM_PROTOCOL_NCADG:
53                         printf(" RPC");
54                         break;
55
56                 case EPM_PROTOCOL_NCALRPC:
57                         printf(" NCALRPC");
58                         break;
59
60                 case EPM_PROTOCOL_DNET_NSP:
61                         printf(" DNET/NSP");
62                         break;
63
64                 case EPM_PROTOCOL_IP:
65                         printf(" IP:");
66                         {
67                                 struct in_addr in;
68                                 in.s_addr = htonl(rhs->ip.address);
69                                 printf("%s", inet_ntoa(in));
70                         }
71                         break;
72
73                 case EPM_PROTOCOL_PIPE:
74                         printf(" PIPE:%s", rhs->pipe.path);
75                         break;
76
77                 case EPM_PROTOCOL_SMB:
78                         printf(" SMB:%s", rhs->smb.unc);
79                         break;
80
81                 case EPM_PROTOCOL_UNIX_DS:
82                         printf(" Unix:%s", rhs->unix_ds.path);
83                         break;
84
85                 case EPM_PROTOCOL_NETBIOS:
86                         printf(" NetBIOS:%s", rhs->netbios.name);
87                         break;
88
89                 case EPM_PROTOCOL_NETBEUI:
90                         printf(" NETBeui");
91                         break;
92
93                 case EPM_PROTOCOL_SPX:
94                         printf(" SPX");
95                         break;
96
97                 case EPM_PROTOCOL_NB_IPX:
98                         printf(" NB_IPX");
99                         break;
100
101                 case EPM_PROTOCOL_HTTP:
102                         printf(" HTTP:%d", rhs->http.port);
103                         break;
104
105                 case EPM_PROTOCOL_TCP:
106                         /* what is the difference between this and 0x1f? */
107                         printf(" TCP:%d", rhs->tcp.port);
108                         break;
109
110                 case EPM_PROTOCOL_UDP:
111                         printf(" UDP:%d", rhs->udp.port);
112                         break;
113
114                 default:
115                         printf(" UNK(%02x):", lhs->protocol);
116                         if (rhs->unknown.length == 2) {
117                                 printf("%d", RSVAL(rhs->unknown.data, 0));
118                         }
119                         break;
120                 }
121         }
122         printf("\n");
123 }
124
125
126 static BOOL test_Map(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
127                      struct epm_twr_t *twr)
128 {
129         NTSTATUS status;
130         struct epm_Map r;
131         struct GUID uuid;
132         const char *uuid_str;
133         struct policy_handle handle;
134         int i;
135
136         ZERO_STRUCT(uuid);
137         ZERO_STRUCT(handle);
138
139         r.in.object = &uuid;
140         r.in.map_tower = twr;
141         r.in.entry_handle = &handle;    
142         r.out.entry_handle = &handle;
143         r.in.max_towers = 100;
144
145         uuid_str = GUID_string(mem_ctx, &twr->tower.floors[0].lhs.info.uuid.uuid);
146
147         printf("epm_Map results for '%s':\n", 
148                idl_pipe_name(uuid_str, twr->tower.floors[0].lhs.info.uuid.version));
149
150         twr->tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
151         twr->tower.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
152         twr->tower.floors[2].rhs.ncacn.minor_version = 0;
153
154         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
155         twr->tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
156         twr->tower.floors[3].rhs.tcp.port = 0;
157
158         twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
159         twr->tower.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
160         twr->tower.floors[4].rhs.ip.address = 0;
161
162         status = dcerpc_epm_Map(p, mem_ctx, &r);
163         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
164                 for (i=0;i<r.out.num_towers;i++) {
165                         if (r.out.towers[i].twr) {
166                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
167                         }
168                 }
169         }
170
171         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_HTTP;
172         twr->tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
173         twr->tower.floors[3].rhs.http.port = 0;
174
175         status = dcerpc_epm_Map(p, mem_ctx, &r);
176         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
177                 for (i=0;i<r.out.num_towers;i++) {
178                         if (r.out.towers[i].twr) {
179                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
180                         }
181                 }
182         }
183
184         twr->tower.floors[3].lhs.protocol = EPM_PROTOCOL_SMB;
185         twr->tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
186         twr->tower.floors[3].rhs.smb.unc = "";
187
188         twr->tower.floors[4].lhs.protocol = EPM_PROTOCOL_NETBIOS;
189         twr->tower.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
190         twr->tower.floors[4].rhs.netbios.name = "";
191
192         status = dcerpc_epm_Map(p, mem_ctx, &r);
193         if (NT_STATUS_IS_OK(status) && r.out.result == 0) {
194                 for (i=0;i<r.out.num_towers;i++) {
195                         if (r.out.towers[i].twr) {
196                                 display_tower(mem_ctx, &r.out.towers[i].twr->tower);
197                         }
198                 }
199         }
200
201         /* FIXME: Extend to do other protocols as well (ncacn_unix_stream, ncalrpc) */
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->tower);
235                         if (r.out.entries[i].tower->tower.num_floors == 5) {
236                                 test_Map(p, mem_ctx, r.out.entries[i].tower);
237                         }
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(void)
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 }