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