s4:dsdb: Fix stack use after scope in gkdi_create_root_key()
[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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_mgmt_c.h"
24 #include "librpc/ndr/ndr_table.h"
25 #include "torture/rpc/torture_rpc.h"
26 #include "param/param.h"
27
28 /*
29   work out how many calls there are for an interface
30  */
31 static bool test_num_calls(struct torture_context *tctx, 
32                            const struct ndr_interface_table *iface,
33                            TALLOC_CTX *mem_ctx,
34                            struct ndr_syntax_id *id)
35 {
36         struct dcerpc_pipe *p;
37         NTSTATUS status;
38         unsigned int i;
39         DATA_BLOB stub_in, stub_out;
40         struct ndr_interface_table _tbl;
41         const struct ndr_interface_table *tbl;
42
43         /* FIXME: This should be fixed when torture_rpc_connection 
44          * takes a ndr_syntax_id */
45         tbl = ndr_table_by_syntax(id);
46         if (tbl == NULL) {
47                 _tbl = *iface;
48                 _tbl.name = "__unknown__";
49                 _tbl.syntax_id = *id;
50                 _tbl.num_calls = UINT32_MAX;
51                 tbl = &_tbl;
52         }
53
54         status = torture_rpc_connection(tctx, &p, tbl);
55         if (!NT_STATUS_IS_OK(status)) {
56                 char *uuid_str = GUID_string(mem_ctx, &id->uuid);
57                 printf("Failed to connect to '%s' on '%s' - %s\n", 
58                        uuid_str, iface->name, nt_errstr(status));
59                 talloc_free(uuid_str);
60                 return true;
61         }
62
63         /* make null calls */
64         stub_in = data_blob_talloc(mem_ctx, NULL, 1000);
65         memset(stub_in.data, 0xFF, stub_in.length);
66
67         for (i=0;i<200;i++) {
68                 bool ok;
69                 uint32_t out_flags = 0;
70
71                 status = dcerpc_binding_handle_raw_call(p->binding_handle,
72                                                         NULL, i,
73                                                         0, /* in_flags */
74                                                         stub_in.data,
75                                                         stub_in.length,
76                                                         mem_ctx,
77                                                         &stub_out.data,
78                                                         &stub_out.length,
79                                                         &out_flags);
80                 ok = dcerpc_binding_handle_is_connected(p->binding_handle);
81                 if (!ok) {
82                         printf("\tpipe disconnected at %u\n", i);
83                         goto done;
84                 }
85
86                 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
87                         break;
88                 }
89
90                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
91                         printf("\taccess denied at %u\n", i);
92                         goto done;
93                 }
94
95                 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
96                         printf("\tprotocol error at %u\n", i);
97                 }
98         }
99
100         printf("\t%d calls available\n", i);
101         if (tbl->num_calls == UINT32_MAX) {
102                 printf("\tinterface not known in local IDL\n");
103         } else if (tbl->num_calls != i) {
104                 printf("\tWARNING: local IDL defines %u calls\n",
105                         (unsigned int)tbl->num_calls);
106         } else {
107                 printf("\tOK: matches num_calls in local IDL\n");
108         }
109
110 done:
111         talloc_free(p);
112         return true;
113 }
114
115
116
117 bool torture_rpc_scanner(struct torture_context *torture)
118 {
119         NTSTATUS status;
120         struct dcerpc_pipe *p;
121         TALLOC_CTX *loop_ctx;
122         bool ret = true;
123         const struct ndr_interface_list *l;
124         struct dcerpc_binding *b;
125         enum dcerpc_transport_t transport;
126
127         status = torture_rpc_binding(torture, &b);
128         if (!NT_STATUS_IS_OK(status)) {
129                 return false;
130         }
131         transport = dcerpc_binding_get_transport(b);
132
133         for (l=ndr_table_list();l;l=l->next) {          
134                 loop_ctx = talloc_named(torture, 0, "torture_rpc_scanner loop context");
135                 /* some interfaces are not mappable */
136                 if (l->table->num_calls == 0 ||
137                     strcmp(l->table->name, "mgmt") == 0) {
138                         talloc_free(loop_ctx);
139                         continue;
140                 }
141
142                 printf("\nTesting pipe '%s'\n", l->table->name);
143
144                 if (transport == NCACN_IP_TCP) {
145                         status = dcerpc_epm_map_binding(torture, b, l->table,
146                                                         torture->ev,
147                                                         torture->lp_ctx);
148                         if (!NT_STATUS_IS_OK(status)) {
149                                 printf("Failed to map port for uuid %s\n", 
150                                            GUID_string(loop_ctx, &l->table->syntax_id.uuid));
151                                 talloc_free(loop_ctx);
152                                 continue;
153                         }
154                 } else {
155                         status = dcerpc_binding_set_string_option(b, "endpoint",
156                                                                   l->table->name);
157                         if (!NT_STATUS_IS_OK(status)) {
158                                 talloc_free(loop_ctx);
159                                 ret = false;
160                                 continue;
161                         }
162                         status = dcerpc_binding_set_abstract_syntax(b,
163                                                         &l->table->syntax_id);
164                         if (!NT_STATUS_IS_OK(status)) {
165                                 talloc_free(loop_ctx);
166                                 ret = false;
167                                 continue;
168                         }
169                 }
170
171                 lpcfg_set_cmdline(torture->lp_ctx, "torture:binding", dcerpc_binding_string(torture, b));
172
173                 status = torture_rpc_connection(torture, &p, &ndr_table_mgmt);
174                 if (!NT_STATUS_IS_OK(status)) {
175                         talloc_free(loop_ctx);
176                         ret = false;
177                         continue;
178                 }
179         
180                 if (!test_inq_if_ids(torture, p->binding_handle, torture, test_num_calls, l->table)) {
181                         ret = false;
182                 }
183         }
184
185         return ret;
186 }
187