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