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