r21693: Fix the RPC-SCANNER test. Share some code with the RPC-MGMT test to
[Samba.git] / source / torture / rpc / mgmt.c
blobf28a0a9342d65021cf3245109dfd8efce60f562d
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for mgmt rpc operations
5 Copyright (C) Andrew Tridgell 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_mgmt_c.h"
25 #include "auth/gensec/gensec.h"
26 #include "librpc/rpc/dcerpc_table.h"
27 #include "torture/rpc/rpc.h"
31 ask the server what interface IDs are available on this endpoint
33 BOOL test_inq_if_ids(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
34 BOOL (*per_id_test)(const struct dcerpc_interface_table *iface,
35 TALLOC_CTX *mem_ctx,
36 struct dcerpc_syntax_id *id),
37 const void *priv)
39 NTSTATUS status;
40 struct mgmt_inq_if_ids r;
41 struct rpc_if_id_vector_t *vector;
42 int i;
44 vector = talloc(mem_ctx, struct rpc_if_id_vector_t);
45 r.out.if_id_vector = &vector;
47 status = dcerpc_mgmt_inq_if_ids(p, mem_ctx, &r);
48 if (!NT_STATUS_IS_OK(status)) {
49 printf("inq_if_ids failed - %s\n", nt_errstr(status));
50 return False;
53 if (!W_ERROR_IS_OK(r.out.result)) {
54 printf("inq_if_ids gave error code %s\n", win_errstr(r.out.result));
55 return False;
58 if (!vector) {
59 printf("inq_if_ids gave NULL if_id_vector\n");
60 return False;
63 for (i=0;i<vector->count;i++) {
64 struct dcerpc_syntax_id *id = vector->if_id[i].id;
65 if (!id) continue;
67 printf("\tuuid %s version 0x%08x '%s'\n",
68 GUID_string(mem_ctx, &id->uuid),
69 id->if_version, idl_pipe_name(&id->uuid, id->if_version));
71 if (per_id_test) {
72 per_id_test(priv, mem_ctx, id);
76 return True;
79 static BOOL test_inq_stats(struct dcerpc_pipe *p,
80 TALLOC_CTX *mem_ctx)
82 NTSTATUS status;
83 struct mgmt_inq_stats r;
84 struct mgmt_statistics statistics;
86 r.in.max_count = MGMT_STATS_ARRAY_MAX_SIZE;
87 r.in.unknown = 0;
88 r.out.statistics = &statistics;
90 status = dcerpc_mgmt_inq_stats(p, mem_ctx, &r);
91 if (!NT_STATUS_IS_OK(status)) {
92 printf("inq_stats failed - %s\n", nt_errstr(status));
93 return False;
96 if (statistics.count != MGMT_STATS_ARRAY_MAX_SIZE) {
97 printf("Unexpected array size %d\n", statistics.count);
98 return False;
101 printf("\tcalls_in %6d calls_out %6d\n\tpkts_in %6d pkts_out %6d\n",
102 statistics.statistics[MGMT_STATS_CALLS_IN],
103 statistics.statistics[MGMT_STATS_CALLS_OUT],
104 statistics.statistics[MGMT_STATS_PKTS_IN],
105 statistics.statistics[MGMT_STATS_PKTS_OUT]);
107 return True;
110 static BOOL test_inq_princ_name(struct dcerpc_pipe *p,
111 TALLOC_CTX *mem_ctx)
113 NTSTATUS status;
114 struct mgmt_inq_princ_name r;
115 int i;
116 BOOL ret = False;
118 for (i=0;i<100;i++) {
119 r.in.authn_proto = i; /* DCERPC_AUTH_TYPE_* */
120 r.in.princ_name_size = 100;
122 status = dcerpc_mgmt_inq_princ_name(p, mem_ctx, &r);
123 if (!NT_STATUS_IS_OK(status)) {
124 continue;
126 if (W_ERROR_IS_OK(r.out.result)) {
127 const char *name = gensec_get_name_by_authtype(i);
128 ret = True;
129 if (name) {
130 printf("\tprinciple name for proto %u (%s) is '%s'\n",
131 i, name, r.out.princ_name);
132 } else {
133 printf("\tprinciple name for proto %u is '%s'\n",
134 i, r.out.princ_name);
139 if (!ret) {
140 printf("\tno principle names?\n");
143 return True;
146 static BOOL test_is_server_listening(struct dcerpc_pipe *p,
147 TALLOC_CTX *mem_ctx)
149 NTSTATUS status;
150 struct mgmt_is_server_listening r;
151 r.out.status = talloc(mem_ctx, uint32_t);
153 status = dcerpc_mgmt_is_server_listening(p, mem_ctx, &r);
154 if (!NT_STATUS_IS_OK(status)) {
155 printf("is_server_listening failed - %s\n", nt_errstr(status));
156 return False;
159 if (*r.out.status != 0 || r.out.result == 0) {
160 printf("\tserver is NOT listening\n");
161 } else {
162 printf("\tserver is listening\n");
165 return True;
168 static BOOL test_stop_server_listening(struct dcerpc_pipe *p,
169 TALLOC_CTX *mem_ctx)
171 NTSTATUS status;
172 struct mgmt_stop_server_listening r;
174 status = dcerpc_mgmt_stop_server_listening(p, mem_ctx, &r);
175 if (!NT_STATUS_IS_OK(status)) {
176 printf("stop_server_listening failed - %s\n", nt_errstr(status));
177 return False;
180 if (!W_ERROR_IS_OK(r.out.result)) {
181 printf("\tserver refused to stop listening - %s\n", win_errstr(r.out.result));
182 } else {
183 printf("\tserver allowed a stop_server_listening request\n");
184 return False;
187 return True;
191 BOOL torture_rpc_mgmt(struct torture_context *torture)
193 NTSTATUS status;
194 struct dcerpc_pipe *p;
195 TALLOC_CTX *mem_ctx, *loop_ctx;
196 BOOL ret = True;
197 const char *binding = torture_setting_string(torture, "binding", NULL);
198 const struct dcerpc_interface_list *l;
199 struct dcerpc_binding *b;
201 mem_ctx = talloc_init("torture_rpc_mgmt");
203 if (!binding) {
204 printf("You must supply a ncacn binding string\n");
205 return False;
208 status = dcerpc_parse_binding(mem_ctx, binding, &b);
209 if (!NT_STATUS_IS_OK(status)) {
210 talloc_free(mem_ctx);
211 printf("Failed to parse binding '%s'\n", binding);
212 return False;
215 for (l=librpc_dcerpc_pipes();l;l=l->next) {
216 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_mgmt loop context");
218 /* some interfaces are not mappable */
219 if (l->table->num_calls == 0 ||
220 strcmp(l->table->name, "mgmt") == 0) {
221 talloc_free(loop_ctx);
222 continue;
225 printf("\nTesting pipe '%s'\n", l->table->name);
227 status = dcerpc_epm_map_binding(loop_ctx, b, l->table, NULL);
228 if (!NT_STATUS_IS_OK(status)) {
229 printf("Failed to map port for uuid %s\n",
230 GUID_string(loop_ctx, &l->table->syntax_id.uuid));
231 talloc_free(loop_ctx);
232 continue;
235 lp_set_cmdline("torture:binding", dcerpc_binding_string(loop_ctx, b));
237 status = torture_rpc_connection(loop_ctx, &p, &dcerpc_table_mgmt);
238 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
239 printf("Interface not available - skipping\n");
240 talloc_free(loop_ctx);
241 continue;
244 if (!NT_STATUS_IS_OK(status)) {
245 talloc_free(loop_ctx);
246 ret = False;
247 continue;
250 if (!test_is_server_listening(p, loop_ctx)) {
251 ret = False;
254 if (!test_stop_server_listening(p, loop_ctx)) {
255 ret = False;
258 if (!test_inq_stats(p, loop_ctx)) {
259 ret = False;
262 if (!test_inq_princ_name(p, loop_ctx)) {
263 ret = False;
266 if (!test_inq_if_ids(p, loop_ctx, NULL, NULL)) {
267 ret = False;
272 return ret;