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