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