944bb4372d63b8e9cebb524c4b3be8df974d2db6
[nivanova/samba-autobuild/.git] / source / 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
25 /*
26   work out how many calls there are for an interface
27  */
28 static BOOL test_num_calls(const struct dcerpc_interface_table *iface,
29                            TALLOC_CTX *mem_ctx,
30                            struct dcerpc_syntax_id *id)
31 {
32         struct dcerpc_pipe *p;
33         NTSTATUS status;
34         const char *uuid;
35         int i;
36         DATA_BLOB stub_in, stub_out;
37         int idl_calls;
38
39         uuid = GUID_string(mem_ctx, &id->uuid);
40
41         status = torture_rpc_connection(&p, iface->name,
42                                         uuid, id->if_version);
43         if (!NT_STATUS_IS_OK(status)) {
44                 printf("Failed to connect to '%s' on '%s' - %s\n", 
45                        uuid, iface->name, nt_errstr(status));
46                 return False;
47         }
48
49         /* make null calls */
50         stub_in = data_blob(NULL, 1000);
51         memset(stub_in.data, 0xFF, stub_in.length);
52
53         for (i=0;i<200;i++) {
54                 status = dcerpc_request(p, i, mem_ctx, &stub_in, &stub_out);
55                 if (!NT_STATUS_IS_OK(status) &&
56                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
57                         break;
58                 }
59
60                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 5) {
61                         printf("\tpipe disconnected at %d\n", i);
62                         goto done;
63                 }
64
65                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 0x80010111) {
66                         printf("\terr 0x80010111 at %d\n", i);
67                         goto done;
68                 }
69         }
70
71         printf("\t%d calls available\n", i);
72         idl_calls = idl_num_calls(uuid, id->if_version);
73         if (idl_calls == -1) {
74                 printf("\tinterface not known in local IDL\n");
75         } else if (i != idl_calls) {
76                 printf("\tWARNING: local IDL defines %u calls\n", idl_calls);
77         } else {
78                 printf("\tOK: matches num_calls in local IDL\n");
79         }
80
81 done:
82         torture_rpc_close(p);
83         return True;
84 }
85
86 /*
87   ask the server what interface IDs are available on this endpoint
88 */
89 static BOOL test_inq_if_ids(struct dcerpc_pipe *p, 
90                             TALLOC_CTX *mem_ctx,
91                             const struct dcerpc_interface_table *iface)
92 {
93         NTSTATUS status;
94         struct mgmt_inq_if_ids r;
95         int i;
96         
97         status = dcerpc_mgmt_inq_if_ids(p, mem_ctx, &r);
98         if (!NT_STATUS_IS_OK(status)) {
99                 printf("inq_if_ids failed - %s\n", nt_errstr(status));
100                 return False;
101         }
102
103         if (!W_ERROR_IS_OK(r.out.result)) {
104                 printf("inq_if_ids gave error code %s\n", win_errstr(r.out.result));
105                 return False;
106         }
107
108         if (!r.out.if_id_vector) {
109                 printf("inq_if_ids gave NULL if_id_vector\n");
110                 return False;
111         }
112
113         for (i=0;i<r.out.if_id_vector->count;i++) {
114                 const char *uuid;
115                 struct dcerpc_syntax_id *id = r.out.if_id_vector->if_id[i].id;
116                 if (!id) continue;
117
118                 uuid = GUID_string(mem_ctx, &id->uuid),
119
120                 printf("\n\tuuid %s  version 0x%08x '%s'\n",
121                        uuid,
122                        id->if_version, idl_pipe_name(uuid, id->if_version));
123                 test_num_calls(iface, mem_ctx, id);
124         }
125
126         return True;
127 }
128
129
130 BOOL torture_rpc_scanner(int dummy)
131 {
132         NTSTATUS status;
133         struct dcerpc_pipe *p;
134         TALLOC_CTX *mem_ctx;
135         BOOL ret = True;
136         int i;
137         char *binding = lp_parm_string(-1, "torture", "binding");
138         struct dcerpc_binding b;
139
140         mem_ctx = talloc_init("torture_rpc_scanner");
141
142         if (!binding) {
143                 printf("You must supply a ncacn binding string\n");
144                 return False;
145         }
146         
147         status = dcerpc_parse_binding(mem_ctx, binding, &b);
148         if (!NT_STATUS_IS_OK(status)) {
149                 printf("Failed to parse binding '%s'\n", binding);
150                 return False;
151         }
152
153         b.options = talloc_array_p(mem_ctx, const char *, 2);
154         if (!b.options) {
155                 return False;
156         }
157
158         for (i=0;dcerpc_pipes[i];i++) {         
159                 /* some interfaces are not mappable */
160                 if (dcerpc_pipes[i]->num_calls == 0 ||
161                     strcmp(dcerpc_pipes[i]->name, "mgmt") == 0) {
162                         continue;
163                 }
164
165                 printf("\nTesting pipe '%s'\n", dcerpc_pipes[i]->name);
166
167                 if (b.transport == NCACN_IP_TCP) {
168                         uint32 port;
169                         status = dcerpc_epm_map_tcp_port(b.host, 
170                                                          dcerpc_pipes[i]->uuid,
171                                                          dcerpc_pipes[i]->if_version,
172                                                          &port);
173                         if (!NT_STATUS_IS_OK(status)) {
174                                 printf("Failed to map port for uuid %s\n", dcerpc_pipes[i]->uuid);
175                                 continue;
176                         }
177                         b.options[0] = talloc_asprintf(mem_ctx, "%u", port);
178                 } else {
179                         b.options[0] = dcerpc_pipes[i]->name;
180                 }
181                 b.options[1] = NULL;
182
183                 lp_set_cmdline("torture:binding", dcerpc_binding_string(mem_ctx, &b));
184
185                 status = torture_rpc_connection(&p, 
186                                                 dcerpc_pipes[i]->name,
187                                                 DCERPC_MGMT_UUID,
188                                                 DCERPC_MGMT_VERSION);
189                 if (!NT_STATUS_IS_OK(status)) {
190                         ret = False;
191                         continue;
192                 }
193         
194                 if (!test_inq_if_ids(p, mem_ctx, dcerpc_pipes[i])) {
195                         ret = False;
196                 }
197
198                 torture_rpc_close(p);
199         }
200
201         return ret;
202 }