* fixed byte order in epmapper parsing
[samba.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 0xd:
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                 case 0xb:
46                         printf(" RPC-C");
47                         break;
48
49                 case 0x9:
50                         printf(" IP:");
51                         if (rhs->rhs_data.length == 4) {
52                                 struct in_addr in;
53                                 in.s_addr = IVAL(rhs->rhs_data.data, 0);
54                                 printf("%s", inet_ntoa(in));
55                         }
56                         break;
57
58                 case 0x10:
59                         printf(" PIPE:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
60                         break;
61
62                 case 0x0f:
63                         printf(" SMB:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
64                         break;
65
66                 case 0x11:
67                         printf(" NetBIOS:%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
68                         break;
69
70                 case 0x01:
71                         printf(" UNK(1):%.*s", rhs->rhs_data.length, rhs->rhs_data.data);
72                         break;
73
74                 case 0x1f:
75                         printf(" TCP:");
76                         if (rhs->rhs_data.length == 2) {
77                                 printf("%d", RSVAL(rhs->rhs_data.data, 0));
78                         }
79                         break;
80
81                 default:
82                         printf(" UNK(%02x):", lhs->protocol);
83                         if (rhs->rhs_data.length == 2) {
84                                 printf("%d", RSVAL(rhs->rhs_data.data, 0));
85                         }
86                         break;
87                 }
88         }
89         printf("\n");
90 }
91
92
93 static BOOL test_Map(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
94                      struct epm_twr_t *twr)
95 {
96         NTSTATUS status;
97         struct epm_Map r;
98         GUID uuid;
99         struct policy_handle handle;
100         int i;
101
102         ZERO_STRUCT(uuid);
103         ZERO_STRUCT(handle);
104
105         r.in.object = &uuid;
106         r.in.map_tower = twr;
107         r.in.entry_handle = &handle;    
108         r.out.entry_handle = &handle;
109         r.in.max_towers = 100;
110
111         status = dcerpc_epm_Map(p, mem_ctx, &r);
112         if (!NT_STATUS_IS_OK(status) || r.out.status != 0) {
113                 printf("epm_Map failed - %s/0x%x\n", 
114                        nt_errstr(status), r.out.status);
115                 return False;
116         }
117
118         printf("epm_Map results:\n");
119
120         for (i=0;i<r.out.num_towers;i++) {
121                 if (r.out.towers[i].twr) {
122                         display_tower(mem_ctx, &r.out.towers[i].twr->towers);
123                 }
124         }
125         
126         return True;
127 }
128
129 static BOOL test_Lookup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
130 {
131         NTSTATUS status;
132         struct epm_Lookup r;
133         struct GUID uuid;
134         struct rpc_if_id_t iface;
135         struct policy_handle handle;
136
137         ZERO_STRUCT(uuid);
138         ZERO_STRUCT(iface);
139         ZERO_STRUCT(handle);
140
141         r.in.inquiry_type = 0;
142         r.in.object = &uuid;
143         r.in.interface_id = &iface;
144         r.in.vers_option = 0;
145         r.in.entry_handle = &handle;
146         r.out.entry_handle = &handle;
147         r.in.max_ents = 10;
148
149         do {
150                 int i;
151                 status = dcerpc_epm_Lookup(p, mem_ctx, &r);
152                 if (!NT_STATUS_IS_OK(status) || r.out.status != 0) {
153                         break;
154                 }
155                 for (i=0;i<r.out.num_ents;i++) {
156                         printf("\nFound '%s'\n", r.out.entries[i].annotation);
157                         display_tower(mem_ctx, &r.out.entries[i].tower->towers);
158                         test_Map(p, mem_ctx, r.out.entries[i].tower);
159                 }
160         } while (NT_STATUS_IS_OK(status) && 
161                  r.out.status == 0 && 
162                  r.out.num_ents == r.in.max_ents);
163
164         if (!NT_STATUS_IS_OK(status)) {
165                 printf("Lookup failed - %s\n", nt_errstr(status));
166                 return False;
167         }
168
169
170         return True;
171 }
172
173 BOOL torture_rpc_epmapper(int dummy)
174 {
175         NTSTATUS status;
176         struct dcerpc_pipe *p;
177         TALLOC_CTX *mem_ctx;
178         BOOL ret = True;
179
180         mem_ctx = talloc_init("torture_rpc_epmapper");
181
182         status = torture_rpc_connection(&p, 
183                                         DCERPC_EPMAPPER_NAME,
184                                         DCERPC_EPMAPPER_UUID,
185                                         DCERPC_EPMAPPER_VERSION);
186         if (!NT_STATUS_IS_OK(status)) {
187                 return False;
188         }
189         
190         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
191
192         if (!test_Lookup(p, mem_ctx)) {
193                 ret = False;
194         }
195
196         talloc_destroy(mem_ctx);
197
198         torture_rpc_close(p);
199
200         return ret;
201 }