e8203061e7fc3362bc757d03affcd59740cb590e
[kai/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 "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_mgmt_c.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "torture/rpc/rpc.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         int i;
39         DATA_BLOB stub_in, stub_out;
40         int idl_calls;
41         struct ndr_interface_table tbl;
42
43         /* FIXME: This should be fixed when torture_rpc_connection 
44          * takes a ndr_syntax_id */
45         tbl.name = iface->name;
46         tbl.syntax_id = *id;
47
48         status = torture_rpc_connection(tctx, &p, iface);
49         if (!NT_STATUS_IS_OK(status)) {
50                 char *uuid_str = GUID_string(mem_ctx, &id->uuid);
51                 printf("Failed to connect to '%s' on '%s' - %s\n", 
52                        uuid_str, iface->name, nt_errstr(status));
53                 talloc_free(uuid_str);
54                 return True;
55         }
56
57         /* make null calls */
58         stub_in = data_blob(NULL, 1000);
59         memset(stub_in.data, 0xFF, stub_in.length);
60
61         for (i=0;i<200;i++) {
62                 status = dcerpc_request(p, NULL, i, False, mem_ctx, &stub_in, &stub_out);
63                 if (!NT_STATUS_IS_OK(status) &&
64                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
65                         break;
66                 }
67
68                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 5) {
69                         printf("\tpipe disconnected at %d\n", i);
70                         goto done;
71                 }
72
73                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == 0x80010111) {
74                         printf("\terr 0x80010111 at %d\n", i);
75                         goto done;
76                 }
77         }
78
79         printf("\t%d calls available\n", i);
80         idl_calls = ndr_interface_num_calls(&id->uuid, id->if_version);
81         if (idl_calls == -1) {
82                 printf("\tinterface not known in local IDL\n");
83         } else if (i != idl_calls) {
84                 printf("\tWARNING: local IDL defines %u calls\n", idl_calls);
85         } else {
86                 printf("\tOK: matches num_calls in local IDL\n");
87         }
88
89 done:
90         talloc_free(p);
91         return true;
92 }
93
94
95
96 bool torture_rpc_scanner(struct torture_context *torture)
97 {
98         NTSTATUS status;
99         struct dcerpc_pipe *p;
100         TALLOC_CTX *mem_ctx, *loop_ctx;
101         BOOL ret = True;
102         const struct ndr_interface_list *l;
103         struct dcerpc_binding *b;
104
105         mem_ctx = talloc_init("torture_rpc_scanner");
106
107         status = torture_rpc_binding(torture, &b);
108         if (!NT_STATUS_IS_OK(status)) {
109                 talloc_free(mem_ctx);
110                 return False;
111         }
112
113         for (l=ndr_table_list();l;l=l->next) {          
114                 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_scanner loop context");
115                 /* some interfaces are not mappable */
116                 if (l->table->num_calls == 0 ||
117                     strcmp(l->table->name, "mgmt") == 0) {
118                         talloc_free(loop_ctx);
119                         continue;
120                 }
121
122                 printf("\nTesting pipe '%s'\n", l->table->name);
123
124                 if (b->transport == NCACN_IP_TCP) {
125                         status = dcerpc_epm_map_binding(mem_ctx, b, l->table, NULL);
126                         if (!NT_STATUS_IS_OK(status)) {
127                                 printf("Failed to map port for uuid %s\n", 
128                                            GUID_string(loop_ctx, &l->table->syntax_id.uuid));
129                                 talloc_free(loop_ctx);
130                                 continue;
131                         }
132                 } else {
133                         b->endpoint = talloc_strdup(b, l->table->name);
134                 }
135
136                 lp_set_cmdline("torture:binding", dcerpc_binding_string(mem_ctx, b));
137
138                 status = torture_rpc_connection(torture, &p, &ndr_table_mgmt);
139                 if (!NT_STATUS_IS_OK(status)) {
140                         talloc_free(loop_ctx);
141                         ret = False;
142                         continue;
143                 }
144         
145                 if (!test_inq_if_ids(torture, p, mem_ctx, test_num_calls, l->table)) {
146                         ret = False;
147                 }
148         }
149
150         return ret;
151 }
152