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