r12622: Move table.c prototypes to seperate header to prevent circular dependencies
[samba.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 "librpc/gen_ndr/ndr_mgmt.h"
24 #include "auth/gensec/gensec.h"
25 #include "librpc/rpc/dcerpc_table.h"
26
27
28 /*
29   ask the server what interface IDs are available on this endpoint
30 */
31 static BOOL test_inq_if_ids(struct dcerpc_pipe *p, 
32                             TALLOC_CTX *mem_ctx)
33 {
34         NTSTATUS status;
35         struct mgmt_inq_if_ids r;
36         int i;
37         
38         status = dcerpc_mgmt_inq_if_ids(p, mem_ctx, &r);
39         if (!NT_STATUS_IS_OK(status)) {
40                 printf("inq_if_ids failed - %s\n", nt_errstr(status));
41                 return False;
42         }
43
44         if (!W_ERROR_IS_OK(r.out.result)) {
45                 printf("inq_if_ids gave error code %s\n", win_errstr(r.out.result));
46                 return False;
47         }
48
49         if (!r.out.if_id_vector) {
50                 printf("inq_if_ids gave NULL if_id_vector\n");
51                 return False;
52         }
53
54         for (i=0;i<r.out.if_id_vector->count;i++) {
55                 struct dcerpc_syntax_id *id = r.out.if_id_vector->if_id[i].id;
56                 if (!id) continue;
57
58                 printf("\tuuid %s  version 0x%08x  '%s'\n",
59                        GUID_string(mem_ctx, &id->uuid),
60                        id->if_version, idl_pipe_name(&id->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                         const char *name = gensec_get_name_by_authtype(i);
113                         ret = True;
114                         if (name) {
115                                 printf("\tprinciple name for proto %u (%s) is '%s'\n", 
116                                        i, name, r.out.princ_name);
117                         } else {
118                                 printf("\tprinciple name for proto %u is '%s'\n", 
119                                        i, r.out.princ_name);
120                         }
121                 }
122         }
123
124         if (!ret) {
125                 printf("\tno principle names?\n");
126         }
127
128         return True;
129 }
130
131 static BOOL test_is_server_listening(struct dcerpc_pipe *p, 
132                                      TALLOC_CTX *mem_ctx)
133 {
134         NTSTATUS status;
135         struct mgmt_is_server_listening r;
136
137         status = dcerpc_mgmt_is_server_listening(p, mem_ctx, &r);
138         if (!NT_STATUS_IS_OK(status)) {
139                 printf("is_server_listening failed - %s\n", nt_errstr(status));
140                 return False;
141         }
142
143         if (r.out.status != 0 || r.out.result == 0) {
144                 printf("\tserver is NOT listening\n");
145         } else {
146                 printf("\tserver is listening\n");
147         }
148
149         return True;
150 }
151
152 static BOOL test_stop_server_listening(struct dcerpc_pipe *p, 
153                                        TALLOC_CTX *mem_ctx)
154 {
155         NTSTATUS status;
156         struct mgmt_stop_server_listening r;
157
158         status = dcerpc_mgmt_stop_server_listening(p, mem_ctx, &r);
159         if (!NT_STATUS_IS_OK(status)) {
160                 printf("stop_server_listening failed - %s\n", nt_errstr(status));
161                 return False;
162         }
163
164         if (!W_ERROR_IS_OK(r.out.result)) {
165                 printf("\tserver refused to stop listening - %s\n", win_errstr(r.out.result));
166         } else {
167                 printf("\tserver allowed a stop_server_listening request\n");
168                 return False;
169         }
170
171         return True;
172 }
173
174
175 BOOL torture_rpc_mgmt(void)
176 {
177         NTSTATUS status;
178         struct dcerpc_pipe *p;
179         TALLOC_CTX *mem_ctx, *loop_ctx;
180         BOOL ret = True;
181         const char *binding = lp_parm_string(-1, "torture", "binding");
182         const struct dcerpc_interface_list *l;
183         struct dcerpc_binding *b;
184
185         mem_ctx = talloc_init("torture_rpc_mgmt");
186
187         if (!binding) {
188                 printf("You must supply a ncacn binding string\n");
189                 return False;
190         }
191         
192         status = dcerpc_parse_binding(mem_ctx, binding, &b);
193         if (!NT_STATUS_IS_OK(status)) {
194                 talloc_free(mem_ctx);
195                 printf("Failed to parse binding '%s'\n", binding);
196                 return False;
197         }
198
199         for (l=librpc_dcerpc_pipes();l;l=l->next) {             
200                 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_mgmt loop context");
201                 
202                 /* some interfaces are not mappable */
203                 if (l->table->num_calls == 0 ||
204                     strcmp(l->table->name, "mgmt") == 0) {
205                         talloc_free(loop_ctx);
206                         continue;
207                 }
208
209                 printf("\nTesting pipe '%s'\n", l->table->name);
210
211                 if (b->transport == NCACN_IP_TCP) {
212                         status = dcerpc_epm_map_binding(loop_ctx, b, l->table, NULL);
213                         if (!NT_STATUS_IS_OK(status)) {
214                                 printf("Failed to map port for uuid %s\n", 
215                                            GUID_string(loop_ctx, &l->table->uuid));
216                                 talloc_free(loop_ctx);
217                                 continue;
218                         }
219                 } else {
220                         b->endpoint = talloc_strdup(b, l->table->name);
221                 }
222
223                 lp_set_cmdline("torture:binding", dcerpc_binding_string(loop_ctx, b));
224
225                 status = torture_rpc_connection(loop_ctx, &p, &dcerpc_table_mgmt);
226                 if (!NT_STATUS_IS_OK(status)) {
227                         talloc_free(loop_ctx);
228                         ret = False;
229                         continue;
230                 }
231         
232                 if (!test_is_server_listening(p, loop_ctx)) {
233                         ret = False;
234                 }
235
236                 if (!test_stop_server_listening(p, loop_ctx)) {
237                         ret = False;
238                 }
239
240                 if (!test_inq_stats(p, loop_ctx)) {
241                         ret = False;
242                 }
243
244                 if (!test_inq_princ_name(p, loop_ctx)) {
245                         ret = False;
246                 }
247
248                 if (!test_inq_if_ids(p, loop_ctx)) {
249                         ret = False;
250                 }
251
252         }
253
254         return ret;
255 }