fed432f31c5f1555a78f1f0887455c05cd3b764f
[abartlet/samba.git/.git] / source4 / torture / rpc / mgmt.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for mgmt rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    
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 3 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "librpc/gen_ndr/ndr_mgmt_c.h"
24 #include "auth/gensec/gensec.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "torture/rpc/rpc.h"
27 #include "param/param.h"
28 #include "librpc/rpc/dcerpc_proto.h"
29
30
31 /*
32   ask the server what interface IDs are available on this endpoint
33 */
34 bool test_inq_if_ids(struct torture_context *tctx, 
35                      struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
36                      bool (*per_id_test)(struct torture_context *,
37                                          const struct ndr_interface_table *iface,
38                                          TALLOC_CTX *mem_ctx,
39                                          struct ndr_syntax_id *id),
40                      const void *priv)
41 {
42         NTSTATUS status;
43         struct mgmt_inq_if_ids r;
44         struct rpc_if_id_vector_t *vector;
45         int i;
46
47         vector = talloc(mem_ctx, struct rpc_if_id_vector_t);
48         r.out.if_id_vector = &vector;
49         
50         status = dcerpc_mgmt_inq_if_ids(p, mem_ctx, &r);
51         if (!NT_STATUS_IS_OK(status)) {
52                 printf("inq_if_ids failed - %s\n", nt_errstr(status));
53                 return false;
54         }
55
56         if (!W_ERROR_IS_OK(r.out.result)) {
57                 printf("inq_if_ids gave error code %s\n", win_errstr(r.out.result));
58                 return false;
59         }
60
61         if (!vector) {
62                 printf("inq_if_ids gave NULL if_id_vector\n");
63                 return false;
64         }
65
66         for (i=0;i<vector->count;i++) {
67                 struct ndr_syntax_id *id = vector->if_id[i].id;
68                 if (!id) continue;
69
70                 printf("\tuuid %s  version 0x%08x  '%s'\n",
71                        GUID_string(mem_ctx, &id->uuid),
72                        id->if_version,
73                        ndr_interface_name(&id->uuid, id->if_version));
74
75                 if (per_id_test) {
76                         per_id_test(tctx, priv, mem_ctx, id);
77                 }
78         }
79
80         return true;
81 }
82
83 static bool test_inq_stats(struct dcerpc_pipe *p, 
84                            TALLOC_CTX *mem_ctx)
85 {
86         NTSTATUS status;
87         struct mgmt_inq_stats r;
88         struct mgmt_statistics statistics;
89
90         r.in.max_count = MGMT_STATS_ARRAY_MAX_SIZE;
91         r.in.unknown = 0;
92         r.out.statistics = &statistics;
93
94         status = dcerpc_mgmt_inq_stats(p, mem_ctx, &r);
95         if (!NT_STATUS_IS_OK(status)) {
96                 printf("inq_stats failed - %s\n", nt_errstr(status));
97                 return false;
98         }
99
100         if (statistics.count != MGMT_STATS_ARRAY_MAX_SIZE) {
101                 printf("Unexpected array size %d\n", statistics.count);
102                 return false;
103         }
104
105         printf("\tcalls_in %6d  calls_out %6d\n\tpkts_in  %6d  pkts_out  %6d\n",
106                statistics.statistics[MGMT_STATS_CALLS_IN],
107                statistics.statistics[MGMT_STATS_CALLS_OUT],
108                statistics.statistics[MGMT_STATS_PKTS_IN],
109                statistics.statistics[MGMT_STATS_PKTS_OUT]);
110
111         return true;
112 }
113
114 static bool test_inq_princ_name(struct dcerpc_pipe *p, 
115                                 TALLOC_CTX *mem_ctx)
116 {
117         NTSTATUS status;
118         struct mgmt_inq_princ_name r;
119         int i;
120         bool ret = false;
121
122         for (i=0;i<100;i++) {
123                 r.in.authn_proto = i;  /* DCERPC_AUTH_TYPE_* */
124                 r.in.princ_name_size = 100;
125
126                 status = dcerpc_mgmt_inq_princ_name(p, mem_ctx, &r);
127                 if (!NT_STATUS_IS_OK(status)) {
128                         continue;
129                 }
130                 if (W_ERROR_IS_OK(r.out.result)) {
131                         const char *name = gensec_get_name_by_authtype(i);
132                         ret = true;
133                         if (name) {
134                                 printf("\tprinciple name for proto %u (%s) is '%s'\n", 
135                                        i, name, r.out.princ_name);
136                         } else {
137                                 printf("\tprinciple name for proto %u is '%s'\n", 
138                                        i, r.out.princ_name);
139                         }
140                 }
141         }
142
143         if (!ret) {
144                 printf("\tno principle names?\n");
145         }
146
147         return true;
148 }
149
150 static bool test_is_server_listening(struct dcerpc_pipe *p, 
151                                      TALLOC_CTX *mem_ctx)
152 {
153         NTSTATUS status;
154         struct mgmt_is_server_listening r;
155         r.out.status = talloc(mem_ctx, uint32_t);
156
157         status = dcerpc_mgmt_is_server_listening(p, mem_ctx, &r);
158         if (!NT_STATUS_IS_OK(status)) {
159                 printf("is_server_listening failed - %s\n", nt_errstr(status));
160                 return false;
161         }
162
163         if (*r.out.status != 0 || r.out.result == 0) {
164                 printf("\tserver is NOT listening\n");
165         } else {
166                 printf("\tserver is listening\n");
167         }
168
169         return true;
170 }
171
172 static bool test_stop_server_listening(struct dcerpc_pipe *p, 
173                                        TALLOC_CTX *mem_ctx)
174 {
175         NTSTATUS status;
176         struct mgmt_stop_server_listening r;
177
178         status = dcerpc_mgmt_stop_server_listening(p, mem_ctx, &r);
179         if (!NT_STATUS_IS_OK(status)) {
180                 printf("stop_server_listening failed - %s\n", nt_errstr(status));
181                 return false;
182         }
183
184         if (!W_ERROR_IS_OK(r.out.result)) {
185                 printf("\tserver refused to stop listening - %s\n", win_errstr(r.out.result));
186         } else {
187                 printf("\tserver allowed a stop_server_listening request\n");
188                 return false;
189         }
190
191         return true;
192 }
193
194
195 bool torture_rpc_mgmt(struct torture_context *torture)
196 {
197         NTSTATUS status;
198         struct dcerpc_pipe *p;
199         TALLOC_CTX *mem_ctx, *loop_ctx;
200         bool ret = true;
201         const struct ndr_interface_list *l;
202         struct dcerpc_binding *b;
203
204         mem_ctx = talloc_init("torture_rpc_mgmt");
205
206         status = torture_rpc_binding(torture, &b);
207         if (!NT_STATUS_IS_OK(status)) {
208                 talloc_free(mem_ctx);
209                 return false;
210         }
211
212         for (l=ndr_table_list();l;l=l->next) {          
213                 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_mgmt loop context");
214                 
215                 /* some interfaces are not mappable */
216                 if (l->table->num_calls == 0 ||
217                     strcmp(l->table->name, "mgmt") == 0) {
218                         talloc_free(loop_ctx);
219                         continue;
220                 }
221
222                 printf("\nTesting pipe '%s'\n", l->table->name);
223
224                 status = dcerpc_epm_map_binding(loop_ctx, b, l->table, NULL, torture->lp_ctx);
225                 if (!NT_STATUS_IS_OK(status)) {
226                         printf("Failed to map port for uuid %s\n", 
227                                    GUID_string(loop_ctx, &l->table->syntax_id.uuid));
228                         talloc_free(loop_ctx);
229                         continue;
230                 }
231
232                 lp_set_cmdline(torture->lp_ctx, "torture:binding", dcerpc_binding_string(loop_ctx, b));
233
234                 status = torture_rpc_connection(torture, &p, &ndr_table_mgmt);
235                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
236                         printf("Interface not available - skipping\n");
237                         talloc_free(loop_ctx);
238                         continue;
239                 }
240
241                 if (!NT_STATUS_IS_OK(status)) {
242                         talloc_free(loop_ctx);
243                         ret = false;
244                         continue;
245                 }
246
247                 if (!test_is_server_listening(p, loop_ctx)) {
248                         ret = false;
249                 }
250
251                 if (!test_stop_server_listening(p, loop_ctx)) {
252                         ret = false;
253                 }
254
255                 if (!test_inq_stats(p, loop_ctx)) {
256                         ret = false;
257                 }
258
259                 if (!test_inq_princ_name(p, loop_ctx)) {
260                         ret = false;
261                 }
262
263                 if (!test_inq_if_ids(torture, p, loop_ctx, NULL, NULL)) {
264                         ret = false;
265                 }
266
267         }
268
269         return ret;
270 }