more epmapper and mgmt magic
[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         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         uuid_str = GUID_string(mem_ctx, &twr->towers.floors[0].lhs.info.uuid.uuid);
122
123         printf("epm_Map results for '%s':\n", 
124                idl_pipe_name(uuid_str, twr->towers.floors[0].lhs.info.uuid.version));
125
126         twr->towers.floors[2].lhs.protocol = EPM_PROTOCOL_RPC_C;
127         twr->towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
128         twr->towers.floors[2].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 2);
129
130         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
131         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
132         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 2);
133
134         twr->towers.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
135         twr->towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
136         twr->towers.floors[4].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 4);
137
138         status = dcerpc_epm_Map(p, mem_ctx, &r);
139         if (NT_STATUS_IS_OK(status) && r.out.status == 0) {
140                 for (i=0;i<r.out.num_towers;i++) {
141                         if (r.out.towers[i].twr) {
142                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
143                         }
144                 }
145         }
146
147         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_HTTP;
148         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
149         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 2);
150
151         status = dcerpc_epm_Map(p, mem_ctx, &r);
152         if (NT_STATUS_IS_OK(status) && r.out.status == 0) {
153                 for (i=0;i<r.out.num_towers;i++) {
154                         if (r.out.towers[i].twr) {
155                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
156                         }
157                 }
158         }
159
160         twr->towers.floors[3].lhs.protocol = EPM_PROTOCOL_SMB;
161         twr->towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
162         twr->towers.floors[3].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 2);
163
164         twr->towers.floors[4].lhs.protocol = EPM_PROTOCOL_NETBIOS;
165         twr->towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
166         twr->towers.floors[4].rhs.rhs_data = data_blob_talloc(p->mem_ctx, NULL, 2);
167
168         status = dcerpc_epm_Map(p, mem_ctx, &r);
169         if (NT_STATUS_IS_OK(status) && r.out.status == 0) {
170                 for (i=0;i<r.out.num_towers;i++) {
171                         if (r.out.towers[i].twr) {
172                                 display_tower(mem_ctx, &r.out.towers[i].twr->towers);
173                         }
174                 }
175         }
176         
177         return True;
178 }
179
180 static BOOL test_Lookup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
181 {
182         NTSTATUS status;
183         struct epm_Lookup r;
184         struct GUID uuid;
185         struct rpc_if_id_t iface;
186         struct policy_handle handle;
187
188         ZERO_STRUCT(uuid);
189         ZERO_STRUCT(iface);
190         ZERO_STRUCT(handle);
191
192         r.in.inquiry_type = 0;
193         r.in.object = &uuid;
194         r.in.interface_id = &iface;
195         r.in.vers_option = 0;
196         r.in.entry_handle = &handle;
197         r.out.entry_handle = &handle;
198         r.in.max_ents = 10;
199
200         do {
201                 int i;
202                 status = dcerpc_epm_Lookup(p, mem_ctx, &r);
203                 if (!NT_STATUS_IS_OK(status) || r.out.status != 0) {
204                         break;
205                 }
206                 for (i=0;i<r.out.num_ents;i++) {
207                         printf("\nFound '%s'\n", r.out.entries[i].annotation);
208                         display_tower(mem_ctx, &r.out.entries[i].tower->towers);
209                         test_Map(p, mem_ctx, r.out.entries[i].tower);
210                 }
211         } while (NT_STATUS_IS_OK(status) && 
212                  r.out.status == 0 && 
213                  r.out.num_ents == r.in.max_ents);
214
215         if (!NT_STATUS_IS_OK(status)) {
216                 printf("Lookup failed - %s\n", nt_errstr(status));
217                 return False;
218         }
219
220
221         return True;
222 }
223
224 BOOL torture_rpc_epmapper(int dummy)
225 {
226         NTSTATUS status;
227         struct dcerpc_pipe *p;
228         TALLOC_CTX *mem_ctx;
229         BOOL ret = True;
230
231         mem_ctx = talloc_init("torture_rpc_epmapper");
232
233         status = torture_rpc_connection(&p, 
234                                         DCERPC_EPMAPPER_NAME,
235                                         DCERPC_EPMAPPER_UUID,
236                                         DCERPC_EPMAPPER_VERSION);
237         if (!NT_STATUS_IS_OK(status)) {
238                 return False;
239         }
240         
241         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
242
243         if (!test_Lookup(p, mem_ctx)) {
244                 ret = False;
245         }
246
247         talloc_destroy(mem_ctx);
248
249         torture_rpc_close(p);
250
251         return ret;
252 }