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