r25554: Convert last instances of BOOL, True and False to the standard types.
[kai/samba-autobuild/.git] / source4 / torture / rpc / dfs.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for rpc dfs 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 "torture/rpc/rpc.h"
25 #include "librpc/gen_ndr/ndr_dfs_c.h"
26 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
27 #include "libnet/libnet.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "torture/util.h"
30 #include "libcli/libcli.h"
31 #include "lib/cmdline/popt_common.h"
32
33 #define SMBTORTURE_DFS_SHARENAME "smbtorture_dfs_share"
34 #define SMBTORTURE_DFS_DIRNAME "\\smbtorture_dfs_dir"
35 #define SMBTORTURE_DFS_PATHNAME "C:"SMBTORTURE_DFS_DIRNAME
36
37 #define IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(x,y)\
38         if (x == DFS_MANAGER_VERSION_W2K3) {\
39                 if (!W_ERROR_EQUAL(y,WERR_NOT_SUPPORTED)) {\
40                         printf("expected WERR_NOT_SUPPORTED\n");\
41                         return false;\
42                 }\
43                 return true;\
44         }\
45
46 static bool test_NetShareAdd(TALLOC_CTX *mem_ctx, const char *host, const char *sharename, const char *dir)
47 {
48         NTSTATUS status;
49         struct srvsvc_NetShareInfo2 i;
50         struct libnet_context* libnetctx;
51         struct libnet_AddShare r;
52
53         printf("Creating share %s\n", sharename);
54
55         if (!(libnetctx = libnet_context_init(NULL))) {
56                 return false;
57         }
58
59         libnetctx->cred = cmdline_credentials;
60
61         i.name                  = sharename;
62         i.type                  = STYPE_DISKTREE;
63         i.path                  = dir;
64         i.max_users             = (uint32_t) -1;
65         i.comment               = "created by smbtorture";
66         i.password              = NULL;
67         i.permissions           = 0x0;
68         i.current_users         = 0x0;
69
70         r.level                 = 2;
71         r.in.server_name        = host;
72         r.in.share              = i;
73
74         status = libnet_AddShare(libnetctx, mem_ctx, &r);
75         if (!NT_STATUS_IS_OK(status)) {
76                 d_printf("Failed to add new share: %s (%s)\n", 
77                         nt_errstr(status), r.out.error_string);
78                 return false;
79         }
80
81         return true;
82 }
83
84 static bool test_NetShareDel(TALLOC_CTX *mem_ctx, const char *host, const char *sharename)
85 {
86         NTSTATUS status;
87         struct libnet_context* libnetctx;
88         struct libnet_DelShare r;
89
90         printf("Deleting share %s\n", sharename);
91
92         if (!(libnetctx = libnet_context_init(NULL))) {
93                 return false;
94         }
95
96         libnetctx->cred = cmdline_credentials;
97
98         r.in.share_name         = sharename;
99         r.in.server_name        = host;
100
101         status = libnet_DelShare(libnetctx, mem_ctx, &r);
102         if (!NT_STATUS_IS_OK(status)) {
103                 d_printf("Failed to delete share: %s (%s)\n", 
104                         nt_errstr(status), r.out.error_string);
105                 return false;
106         }
107
108         return true;
109 }
110
111 static bool test_CreateDir(TALLOC_CTX *mem_ctx, 
112                            struct smbcli_state **cli, 
113                            const char *host, 
114                            const char *share, 
115                            const char *dir)
116 {
117         printf("Creating directory %s\n", dir);
118
119         if (!torture_open_connection_share(mem_ctx, cli, host, share, NULL)) {
120                 return false;
121         }
122
123         if (!torture_setup_dir(*cli, dir)) {
124                 return false;
125         }
126
127         return true;
128 }
129
130 static bool test_DeleteDir(struct smbcli_state *cli, const char *dir)
131 {
132         printf("Deleting directory %s\n", dir);
133
134         if (smbcli_deltree(cli->tree, dir) == -1) {
135                 printf("Unable to delete dir %s - %s\n", dir, smbcli_errstr(cli->tree));
136                 return false;
137         }
138
139         return true;
140 }
141
142 static bool test_GetManagerVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, enum dfs_ManagerVersion *version)
143 {
144         NTSTATUS status;
145         struct dfs_GetManagerVersion r;
146
147         r.out.version = version;
148
149         status = dcerpc_dfs_GetManagerVersion(p, mem_ctx, &r);
150         if (!NT_STATUS_IS_OK(status)) {
151                 printf("GetManagerVersion failed - %s\n", nt_errstr(status));
152                 return false;
153         }
154
155         return true;
156 }
157
158 static bool test_ManagerInitialize(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host)
159 {
160         NTSTATUS status;
161         enum dfs_ManagerVersion version;
162         struct dfs_ManagerInitialize r;
163
164         printf("Testing ManagerInitialize\n");
165
166         if (!test_GetManagerVersion(p, mem_ctx, &version)) {
167                 return false;
168         }
169
170         r.in.servername = host;
171         r.in.flags = 0;
172
173         status = dcerpc_dfs_ManagerInitialize(p, mem_ctx, &r);
174         if (!NT_STATUS_IS_OK(status)) {
175                 printf("ManagerInitialize failed - %s\n", nt_errstr(status));
176                 return false;
177         } else if (!W_ERROR_IS_OK(r.out.result)) {
178                 printf("dfs_ManagerInitialize failed - %s\n", win_errstr(r.out.result));
179                 IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
180                 return false;
181         }
182
183         return true;
184 }
185
186 static bool test_GetInfoLevel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, uint16_t level,
187                               const char *root)
188 {
189         NTSTATUS status;
190         struct dfs_GetInfo r;
191
192         printf("Testing GetInfo level %u on '%s'\n", level, root);
193
194         r.in.dfs_entry_path = talloc_strdup(mem_ctx, root);
195         r.in.servername = NULL;
196         r.in.sharename = NULL;
197         r.in.level = level;
198
199         status = dcerpc_dfs_GetInfo(p, mem_ctx, &r);
200         if (!NT_STATUS_IS_OK(status)) {
201                 printf("GetInfo failed - %s\n", nt_errstr(status));
202                 return false;
203         } else if (!W_ERROR_IS_OK(r.out.result) && 
204                    !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
205                 printf("dfs_GetInfo failed - %s\n", win_errstr(r.out.result));
206                 return false;
207         }
208
209         return true;
210 }
211
212 static bool test_GetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *root)
213 {
214         bool ret = true;
215         /* 103, 104, 105, 106 is only available on Set */
216         uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 100, 101, 102, 103, 104, 105, 106};
217         int i;
218
219         for (i=0;i<ARRAY_SIZE(levels);i++) {
220                 if (!test_GetInfoLevel(p, mem_ctx, levels[i], root)) {
221                         ret = false;
222                 }
223         }
224         return ret;
225 }
226
227 static bool test_EnumLevelEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, uint16_t level, const char *dfs_name)
228 {
229         NTSTATUS status;
230         struct dfs_EnumEx rex;
231         uint32_t total=0;
232         struct dfs_EnumStruct e;
233         struct dfs_Info1 s;
234         struct dfs_EnumArray1 e1;
235         bool ret = true;
236         
237         rex.in.level = level;
238         rex.in.bufsize = (uint32_t)-1;
239         rex.in.total = &total;
240         rex.in.info = &e;
241         rex.in.dfs_name = dfs_name;
242
243         e.level = rex.in.level;
244         e.e.info1 = &e1;
245         e.e.info1->count = 0;
246         e.e.info1->s = &s;
247         s.path = NULL;
248
249         printf("Testing EnumEx level %u on '%s'\n", level, dfs_name);
250
251         status = dcerpc_dfs_EnumEx(p, mem_ctx, &rex);
252         if (!NT_STATUS_IS_OK(status)) {
253                 printf("EnumEx failed - %s\n", nt_errstr(status));
254                 return false;
255         }
256
257         if (level == 1 && rex.out.total) {
258                 int i;
259                 for (i=0;i<*rex.out.total;i++) {
260                         const char *root = talloc_strdup(mem_ctx, rex.out.info->e.info1->s[i].path);
261                         if (!test_GetInfo(p, mem_ctx, root)) {
262                                 ret = false;
263                         }
264                 }
265         }
266
267         if (level == 300 && rex.out.total) {
268                 int i,k;
269                 for (i=0;i<*rex.out.total;i++) {
270                         uint16_t levels[] = {1, 2, 3, 4, 200}; /* 300 */
271                         const char *root = talloc_strdup(mem_ctx, rex.out.info->e.info300->s[i].dom_root);
272                         for (k=0;k<ARRAY_SIZE(levels);k++) {
273                                 if (!test_EnumLevelEx(p, mem_ctx, levels[k], root)) {
274                                         ret = false;
275                                 }
276                         }
277                         if (!test_GetInfo(p, mem_ctx, root)) {
278                                 ret = false;
279                         }
280                 }
281         }
282
283         return ret;
284 }
285
286
287 static bool test_EnumLevel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, uint16_t level)
288 {
289         NTSTATUS status;
290         struct dfs_Enum r;
291         uint32_t total=0;
292         struct dfs_EnumStruct e;
293         struct dfs_Info1 s;
294         struct dfs_EnumArray1 e1;
295         bool ret = true;
296         
297         r.in.level = level;
298         r.in.bufsize = (uint32_t)-1;
299         r.in.total = &total;
300         r.in.info = &e;
301
302         e.level = r.in.level;
303         e.e.info1 = &e1;
304         e.e.info1->count = 0;
305         e.e.info1->s = &s;
306         s.path = NULL;
307         
308         printf("Testing Enum level %u\n", level);
309
310         status = dcerpc_dfs_Enum(p, mem_ctx, &r);
311         if (!NT_STATUS_IS_OK(status)) {
312                 printf("Enum failed - %s\n", nt_errstr(status));
313                 return false;
314         } else if (!W_ERROR_IS_OK(r.out.result) && 
315                    !W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, r.out.result)) {
316                 printf("dfs_Enum failed - %s\n", win_errstr(r.out.result));
317                 return false;
318         }
319
320         if (level == 1 && r.out.total) {
321                 int i;
322                 for (i=0;i<*r.out.total;i++) {
323                         const char *root = r.out.info->e.info1->s[i].path;
324                         if (!test_GetInfo(p, mem_ctx, root)) {
325                                 ret = false;
326                         }
327                 }
328                 
329         }
330
331         return ret;
332 }
333
334
335 static bool test_Enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
336 {
337         bool ret = true;
338         uint16_t levels[] = {1, 2, 3, 4, 200, 300};
339         int i;
340
341         for (i=0;i<ARRAY_SIZE(levels);i++) {
342                 if (!test_EnumLevel(p, mem_ctx, levels[i])) {
343                         ret = false;
344                 }
345         }
346
347         return ret;
348 }
349
350 static bool test_EnumEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host)
351 {
352         bool ret = true;
353         uint16_t levels[] = {1, 2, 3, 4, 200, 300};
354         int i;
355
356         for (i=0;i<ARRAY_SIZE(levels);i++) {
357                 if (!test_EnumLevelEx(p, mem_ctx, levels[i], host)) {
358                         ret = false;
359                 }
360         }
361
362         return ret;
363 }
364
365 static bool test_RemoveStdRoot(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host, const char *sharename)
366 {
367         struct dfs_RemoveStdRoot r;
368         NTSTATUS status;
369
370         printf("Testing RemoveStdRoot\n");
371
372         r.in.servername = host;
373         r.in.rootshare  = sharename;
374         r.in.flags      = 0;
375
376         status = dcerpc_dfs_RemoveStdRoot(p, mem_ctx, &r);
377         if (!NT_STATUS_IS_OK(status)) {
378                 printf("RemoveStdRoot failed - %s\n", nt_errstr(status));
379                 return false;
380         } else if (!W_ERROR_IS_OK(r.out.result)) {
381                 printf("dfs_RemoveStdRoot failed - %s\n", win_errstr(r.out.result));
382                 return false;
383         }
384
385         return true;
386 }
387
388 static bool test_AddStdRoot(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host, const char *sharename)
389 {
390         NTSTATUS status;
391         struct dfs_AddStdRoot r;
392
393         printf("Testing AddStdRoot\n");
394
395         r.in.servername = host;
396         r.in.rootshare  = sharename;
397         r.in.comment    = "standard dfs standalone DFS root created by smbtorture (dfs_AddStdRoot)";
398         r.in.flags      = 0;
399
400         status = dcerpc_dfs_AddStdRoot(p, mem_ctx, &r);
401         if (!NT_STATUS_IS_OK(status)) {
402                 printf("AddStdRoot failed - %s\n", nt_errstr(status));
403                 return false;
404         } else if (!W_ERROR_IS_OK(r.out.result)) {
405                 printf("dfs_AddStdRoot failed - %s\n", win_errstr(r.out.result));
406                 return false;
407         }
408
409         return true;
410 }
411
412 static bool test_AddStdRootForced(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host, const char *sharename)
413 {
414         NTSTATUS status;
415         struct dfs_AddStdRootForced r;
416         enum dfs_ManagerVersion version;
417
418         printf("Testing AddStdRootForced\n");
419
420         if (!test_GetManagerVersion(p, mem_ctx, &version)) {
421                 return false;
422         }
423
424         r.in.servername = host;
425         r.in.rootshare  = sharename;
426         r.in.comment    = "standard dfs forced standalone DFS root created by smbtorture (dfs_AddStdRootForced)";
427         r.in.store      = SMBTORTURE_DFS_PATHNAME;
428
429         status = dcerpc_dfs_AddStdRootForced(p, mem_ctx, &r);
430         if (!NT_STATUS_IS_OK(status)) {
431                 printf("AddStdRootForced failed - %s\n", nt_errstr(status));
432                 return false;
433         } else if (!W_ERROR_IS_OK(r.out.result)) {
434                 printf("dfs_AddStdRootForced failed - %s\n", win_errstr(r.out.result));
435                 IS_DFS_VERSION_UNSUPPORTED_CALL_W2K3(version, r.out.result);
436                 return false;
437         }
438
439         return test_RemoveStdRoot(p, mem_ctx, host, sharename);
440 }
441
442 static void test_cleanup_stdroot(struct dcerpc_pipe *p, 
443                                  TALLOC_CTX *mem_ctx, 
444                                  const char *host, 
445                                  const char *sharename, 
446                                  const char *dir)
447 {
448         struct smbcli_state *cli;
449
450         printf("Cleaning up StdRoot\n");
451
452         test_RemoveStdRoot(p, mem_ctx, host, sharename);
453         test_NetShareDel(mem_ctx, host, sharename);
454         torture_open_connection_share(mem_ctx, &cli, host, "C$", NULL);
455         test_DeleteDir(cli, dir);
456         torture_close_connection(cli);
457 }
458
459 static bool test_StdRoot(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *host)
460 {
461         const char *sharename = SMBTORTURE_DFS_SHARENAME;
462         const char *dir = SMBTORTURE_DFS_DIRNAME;
463         const char *path = SMBTORTURE_DFS_PATHNAME;
464         struct smbcli_state *cli;
465         bool ret;
466
467         printf("Testing StdRoot\n");
468
469         test_cleanup_stdroot(p, mem_ctx, host, sharename, dir);
470
471         ret &= test_CreateDir(mem_ctx, &cli, host, "C$", dir);
472         ret &= test_NetShareAdd(mem_ctx, host, sharename, path);
473         ret &= test_AddStdRoot(p, mem_ctx, host, sharename);
474         ret &= test_RemoveStdRoot(p, mem_ctx, host, sharename);
475         ret &= test_AddStdRootForced(p, mem_ctx, host, sharename);
476         ret &= test_NetShareDel(mem_ctx, host, sharename);
477         ret &= test_DeleteDir(cli, dir);
478
479         torture_close_connection(cli);
480
481         return ret;
482 }
483
484 bool torture_rpc_dfs(struct torture_context *torture)
485 {
486         NTSTATUS status;
487         struct dcerpc_pipe *p;
488         bool ret = true;
489         enum dfs_ManagerVersion version;
490         const char *host = torture_setting_string(torture, "host", NULL);
491
492         status = torture_rpc_connection(torture, &p, &ndr_table_netdfs);
493         torture_assert_ntstatus_ok(torture, status, "Unable to connect");
494
495         ret &= test_GetManagerVersion(p, torture, &version);
496         ret &= test_ManagerInitialize(p, torture, host);
497         ret &= test_Enum(p, torture);
498         ret &= test_EnumEx(p, torture, host);
499         ret &= test_StdRoot(p, torture, host);
500
501         return ret;
502 }