r3513: Add (the infrastructure for) DCOM support. Contents:
[samba.git] / source4 / torture / rpc / scanner.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    scanner for rpc calls
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_mgmt.h"
25 #include "librpc/gen_ndr/tables.h"
26
27 /*
28   work out how many calls there are for an interface
29  */
30 static BOOL test_num_calls(const struct dcerpc_interface_table *iface,
31                            TALLOC_CTX *mem_ctx,
32                            struct dcerpc_syntax_id *id)
33 {
34         struct dcerpc_pipe *p;
35         NTSTATUS status;
36         const char *uuid;
37         int i;
38         DATA_BLOB stub_in, stub_out;
39         int idl_calls;
40
41         uuid = GUID_string(mem_ctx, &id->uuid);
42
43         status = torture_rpc_connection(&p, iface->name,
44                                         uuid, id->if_version);
45         if (!NT_STATUS_IS_OK(status)) {
46                 printf("Failed to connect to '%s' on '%s' - %s\n", 
47                        uuid, iface->name, nt_errstr(status));
48                 return False;
49         }
50
51         /* make null calls */
52         stub_in = data_blob(NULL, 1000);
53         memset(stub_in.data, 0xFF, stub_in.length);
54
55         for (i=0;i<200;i++) {
56                 status = dcerpc_request(p, NULL, i, mem_ctx, &stub_in, &stub_out);
57                 if (!NT_STATUS_IS_OK(status) &&
58                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
59                         break;
60                 }
61
62                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 5) {
63                         printf("\tpipe disconnected at %d\n", i);
64                         goto done;
65                 }
66
67                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 0x80010111) {
68                         printf("\terr 0x80010111 at %d\n", i);
69                         goto done;
70                 }
71         }
72
73         printf("\t%d calls available\n", i);
74         idl_calls = idl_num_calls(uuid, id->if_version);
75         if (idl_calls == -1) {
76                 printf("\tinterface not known in local IDL\n");
77         } else if (i != idl_calls) {
78                 printf("\tWARNING: local IDL defines %u calls\n", idl_calls);
79         } else {
80                 printf("\tOK: matches num_calls in local IDL\n");
81         }
82
83 done:
84         torture_rpc_close(p);
85         return True;
86 }
87
88 /*
89   ask the server what interface IDs are available on this endpoint
90 */
91 static BOOL test_inq_if_ids(struct dcerpc_pipe *p, 
92                             TALLOC_CTX *mem_ctx,
93                             const struct dcerpc_interface_table *iface)
94 {
95         NTSTATUS status;
96         struct mgmt_inq_if_ids r;
97         int i;
98         
99         status = dcerpc_mgmt_inq_if_ids(p, mem_ctx, &r);
100         if (!NT_STATUS_IS_OK(status)) {
101                 printf("inq_if_ids failed - %s\n", nt_errstr(status));
102                 return False;
103         }
104
105         if (!W_ERROR_IS_OK(r.out.result)) {
106                 printf("inq_if_ids gave error code %s\n", win_errstr(r.out.result));
107                 return False;
108         }
109
110         if (!r.out.if_id_vector) {
111                 printf("inq_if_ids gave NULL if_id_vector\n");
112                 return False;
113         }
114
115         for (i=0;i<r.out.if_id_vector->count;i++) {
116                 const char *uuid;
117                 struct dcerpc_syntax_id *id = r.out.if_id_vector->if_id[i].id;
118                 if (!id) continue;
119
120                 uuid = GUID_string(mem_ctx, &id->uuid),
121
122                 printf("\n\tuuid %s  version 0x%08x '%s'\n",
123                        uuid,
124                        id->if_version, idl_pipe_name(uuid, id->if_version));
125                 test_num_calls(iface, mem_ctx, id);
126         }
127
128         return True;
129 }
130
131
132 BOOL torture_rpc_scanner(void)
133 {
134         NTSTATUS status;
135         struct dcerpc_pipe *p;
136         TALLOC_CTX *mem_ctx;
137         BOOL ret = True;
138         int i;
139         const char *binding = lp_parm_string(-1, "torture", "binding");
140         struct dcerpc_binding b;
141
142         mem_ctx = talloc_init("torture_rpc_scanner");
143
144         if (!binding) {
145                 printf("You must supply a ncacn binding string\n");
146                 return False;
147         }
148         
149         status = dcerpc_parse_binding(mem_ctx, binding, &b);
150         if (!NT_STATUS_IS_OK(status)) {
151                 printf("Failed to parse binding '%s'\n", binding);
152                 return False;
153         }
154
155         for (i=0;dcerpc_pipes[i];i++) {         
156                 /* some interfaces are not mappable */
157                 if (dcerpc_pipes[i]->num_calls == 0 ||
158                     strcmp(dcerpc_pipes[i]->name, "mgmt") == 0) {
159                         continue;
160                 }
161
162                 printf("\nTesting pipe '%s'\n", dcerpc_pipes[i]->name);
163
164                 if (b.transport == NCACN_IP_TCP) {
165                         status = dcerpc_epm_map_binding(mem_ctx, &b, 
166                                                          dcerpc_pipes[i]->uuid,
167                                                          dcerpc_pipes[i]->if_version);
168                         if (!NT_STATUS_IS_OK(status)) {
169                                 printf("Failed to map port for uuid %s\n", dcerpc_pipes[i]->uuid);
170                                 continue;
171                         }
172                 } else {
173                         b.endpoint = dcerpc_pipes[i]->name;
174                 }
175
176                 lp_set_cmdline("torture:binding", dcerpc_binding_string(mem_ctx, &b));
177
178                 status = torture_rpc_connection(&p, 
179                                                 dcerpc_pipes[i]->name,
180                                                 DCERPC_MGMT_UUID,
181                                                 DCERPC_MGMT_VERSION);
182                 if (!NT_STATUS_IS_OK(status)) {
183                         ret = False;
184                         continue;
185                 }
186         
187                 if (!test_inq_if_ids(p, mem_ctx, dcerpc_pipes[i])) {
188                         ret = False;
189                 }
190
191                 torture_rpc_close(p);
192         }
193
194         return ret;
195 }