s4:torture:smb2: fix the compound.invalid3 test to work against windows
[kai/samba.git] / source4 / torture / smb2 / compound.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 compounded requests
5
6    Copyright (C) Stefan Metzmacher 2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "torture/torture.h"
26 #include "torture/smb2/proto.h"
27 #include "../libcli/smb/smbXcli_base.h"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
32                        nt_errstr(status), nt_errstr(correct)); \
33                 ret = false; \
34                 goto done; \
35         }} while (0)
36
37 #define TARGET_IS_W2K8(_tctx) (torture_setting_bool(_tctx, "w2k8", false))
38 #define TARGET_IS_WIN7(_tctx) (torture_setting_bool(_tctx, "win7", false))
39
40 static bool test_compound_related1(struct torture_context *tctx,
41                                    struct smb2_tree *tree)
42 {
43         struct smb2_handle hd;
44         struct smb2_create cr;
45         NTSTATUS status;
46         const char *fname = "compound_related1.dat";
47         struct smb2_close cl;
48         bool ret = true;
49         struct smb2_request *req[2];
50         struct smbXcli_tcon *saved_tcon = tree->smbXcli;
51         struct smbXcli_session *saved_session = tree->session->smbXcli;
52
53         smb2_transport_credits_ask_num(tree->session->transport, 2);
54
55         smb2_util_unlink(tree, fname);
56
57         smb2_transport_credits_ask_num(tree->session->transport, 1);
58
59         ZERO_STRUCT(cr);
60         cr.in.security_flags            = 0x00;
61         cr.in.oplock_level              = 0;
62         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
63         cr.in.create_flags              = 0x00000000;
64         cr.in.reserved                  = 0x00000000;
65         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
66         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
67         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
68                                           NTCREATEX_SHARE_ACCESS_WRITE |
69                                           NTCREATEX_SHARE_ACCESS_DELETE;
70         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
71         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
72                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
73                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
74                                           0x00200000;
75         cr.in.fname                     = fname;
76
77         smb2_transport_compound_start(tree->session->transport, 2);
78
79         req[0] = smb2_create_send(tree, &cr);
80
81         smb2_transport_compound_set_related(tree->session->transport, true);
82
83         hd.data[0] = UINT64_MAX;
84         hd.data[1] = UINT64_MAX;
85
86         ZERO_STRUCT(cl);
87         cl.in.file.handle = hd;
88
89         tree->smbXcli = smbXcli_tcon_create(tree);
90         smb2cli_tcon_set_values(tree->smbXcli,
91                                 NULL, /* session */
92                                 0xFFFFFFFF, /* tcon_id */
93                                 0, /* type */
94                                 0, /* flags */
95                                 0, /* capabilities */
96                                 0 /* maximal_access */);
97
98         tree->session->smbXcli = smbXcli_session_create(tree->session,
99                                                         tree->session->transport->conn);
100         smb2cli_session_set_id_and_flags(tree->session->smbXcli, UINT64_MAX, 0);
101
102         req[1] = smb2_close_send(tree, &cl);
103
104         status = smb2_create_recv(req[0], tree, &cr);
105         CHECK_STATUS(status, NT_STATUS_OK);
106         status = smb2_close_recv(req[1], &cl);
107         CHECK_STATUS(status, NT_STATUS_OK);
108
109         TALLOC_FREE(tree->smbXcli);
110         tree->smbXcli = saved_tcon;
111         TALLOC_FREE(tree->session->smbXcli);
112         tree->session->smbXcli = saved_session;
113
114         smb2_util_unlink(tree, fname);
115 done:
116         return ret;
117 }
118
119 static bool test_compound_related2(struct torture_context *tctx,
120                                    struct smb2_tree *tree)
121 {
122         struct smb2_handle hd;
123         struct smb2_create cr;
124         NTSTATUS status;
125         const char *fname = "compound_related2.dat";
126         struct smb2_close cl;
127         bool ret = true;
128         struct smb2_request *req[5];
129         struct smbXcli_tcon *saved_tcon = tree->smbXcli;
130         struct smbXcli_session *saved_session = tree->session->smbXcli;
131
132         smb2_transport_credits_ask_num(tree->session->transport, 5);
133
134         smb2_util_unlink(tree, fname);
135
136         smb2_transport_credits_ask_num(tree->session->transport, 1);
137
138         ZERO_STRUCT(cr);
139         cr.in.security_flags            = 0x00;
140         cr.in.oplock_level              = 0;
141         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
142         cr.in.create_flags              = 0x00000000;
143         cr.in.reserved                  = 0x00000000;
144         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
145         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
146         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
147                                           NTCREATEX_SHARE_ACCESS_WRITE |
148                                           NTCREATEX_SHARE_ACCESS_DELETE;
149         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
150         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
151                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
152                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
153                                           0x00200000;
154         cr.in.fname                     = fname;
155
156         smb2_transport_compound_start(tree->session->transport, 5);
157
158         req[0] = smb2_create_send(tree, &cr);
159
160         hd.data[0] = UINT64_MAX;
161         hd.data[1] = UINT64_MAX;
162
163         smb2_transport_compound_set_related(tree->session->transport, true);
164
165         ZERO_STRUCT(cl);
166         cl.in.file.handle = hd;
167
168         tree->smbXcli = smbXcli_tcon_create(tree);
169         smb2cli_tcon_set_values(tree->smbXcli,
170                                 NULL, /* session */
171                                 0xFFFFFFFF, /* tcon_id */
172                                 0, /* type */
173                                 0, /* flags */
174                                 0, /* capabilities */
175                                 0 /* maximal_access */);
176
177         tree->session->smbXcli = smbXcli_session_create(tree->session,
178                                                         tree->session->transport->conn);
179         smb2cli_session_set_id_and_flags(tree->session->smbXcli, UINT64_MAX, 0);
180
181         req[1] = smb2_close_send(tree, &cl);
182         req[2] = smb2_close_send(tree, &cl);
183         req[3] = smb2_close_send(tree, &cl);
184         req[4] = smb2_close_send(tree, &cl);
185
186         status = smb2_create_recv(req[0], tree, &cr);
187         CHECK_STATUS(status, NT_STATUS_OK);
188         status = smb2_close_recv(req[1], &cl);
189         CHECK_STATUS(status, NT_STATUS_OK);
190         status = smb2_close_recv(req[2], &cl);
191         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
192         status = smb2_close_recv(req[3], &cl);
193         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
194         status = smb2_close_recv(req[4], &cl);
195         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
196
197         TALLOC_FREE(tree->smbXcli);
198         tree->smbXcli = saved_tcon;
199         TALLOC_FREE(tree->session->smbXcli);
200         tree->session->smbXcli = saved_session;
201
202         smb2_util_unlink(tree, fname);
203 done:
204         return ret;
205 }
206
207 static bool test_compound_related3(struct torture_context *tctx,
208                                    struct smb2_tree *tree)
209 {
210         struct smb2_handle hd;
211         struct smb2_ioctl io;
212         struct smb2_create cr;
213         struct smb2_close cl;
214         const char *fname = "compound_related3.dat";
215         struct smb2_request *req[3];
216         NTSTATUS status;
217         bool ret = false;
218
219         smb2_util_unlink(tree, fname);
220
221         ZERO_STRUCT(cr);
222         cr.in.security_flags    = 0x00;
223         cr.in.oplock_level      = 0;
224         cr.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
225         cr.in.create_flags      = 0x00000000;
226         cr.in.reserved          = 0x00000000;
227         cr.in.desired_access    = SEC_RIGHTS_FILE_ALL;
228         cr.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
229         cr.in.share_access      = NTCREATEX_SHARE_ACCESS_READ |
230                                   NTCREATEX_SHARE_ACCESS_WRITE |
231                                   NTCREATEX_SHARE_ACCESS_DELETE;
232         cr.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
233         cr.in.create_options    = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
234                                   NTCREATEX_OPTIONS_ASYNC_ALERT |
235                                   NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
236                                   0x00200000;
237         cr.in.fname             = fname;
238
239         smb2_transport_compound_start(tree->session->transport, 3);
240
241         req[0] = smb2_create_send(tree, &cr);
242
243         hd.data[0] = UINT64_MAX;
244         hd.data[1] = UINT64_MAX;
245
246         smb2_transport_compound_set_related(tree->session->transport, true);
247
248         ZERO_STRUCT(io);
249         io.in.function = FSCTL_CREATE_OR_GET_OBJECT_ID;
250         io.in.file.handle = hd;
251         io.in.unknown2 = 0;
252         io.in.max_response_size = 64;
253         io.in.flags = 1;
254
255         req[1] = smb2_ioctl_send(tree, &io);
256
257         ZERO_STRUCT(cl);
258         cl.in.file.handle = hd;
259
260         req[2] = smb2_close_send(tree, &cl);
261
262         status = smb2_create_recv(req[0], tree, &cr);
263         CHECK_STATUS(status, NT_STATUS_OK);
264         status = smb2_ioctl_recv(req[1], tree, &io);
265         CHECK_STATUS(status, NT_STATUS_OK);
266         status = smb2_close_recv(req[2], &cl);
267         CHECK_STATUS(status, NT_STATUS_OK);
268
269         status = smb2_util_unlink(tree, fname);
270         CHECK_STATUS(status, NT_STATUS_OK);
271
272         ret = true;
273 done:
274         return ret;
275 }
276
277 static bool test_compound_unrelated1(struct torture_context *tctx,
278                                      struct smb2_tree *tree)
279 {
280         struct smb2_handle hd;
281         struct smb2_create cr;
282         NTSTATUS status;
283         const char *fname = "compound_unrelated1.dat";
284         struct smb2_close cl;
285         bool ret = true;
286         struct smb2_request *req[5];
287
288         smb2_transport_credits_ask_num(tree->session->transport, 5);
289
290         smb2_util_unlink(tree, fname);
291
292         smb2_transport_credits_ask_num(tree->session->transport, 1);
293
294         ZERO_STRUCT(cr);
295         cr.in.security_flags            = 0x00;
296         cr.in.oplock_level              = 0;
297         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
298         cr.in.create_flags              = 0x00000000;
299         cr.in.reserved                  = 0x00000000;
300         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
301         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
302         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
303                                           NTCREATEX_SHARE_ACCESS_WRITE |
304                                           NTCREATEX_SHARE_ACCESS_DELETE;
305         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
306         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
307                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
308                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
309                                           0x00200000;
310         cr.in.fname                     = fname;
311
312         smb2_transport_compound_start(tree->session->transport, 5);
313
314         req[0] = smb2_create_send(tree, &cr);
315
316         hd.data[0] = UINT64_MAX;
317         hd.data[1] = UINT64_MAX;
318
319         ZERO_STRUCT(cl);
320         cl.in.file.handle = hd;
321         req[1] = smb2_close_send(tree, &cl);
322         req[2] = smb2_close_send(tree, &cl);
323         req[3] = smb2_close_send(tree, &cl);
324         req[4] = smb2_close_send(tree, &cl);
325
326         status = smb2_create_recv(req[0], tree, &cr);
327         CHECK_STATUS(status, NT_STATUS_OK);
328         status = smb2_close_recv(req[1], &cl);
329         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
330         status = smb2_close_recv(req[2], &cl);
331         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
332         status = smb2_close_recv(req[3], &cl);
333         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
334         status = smb2_close_recv(req[4], &cl);
335         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
336
337         smb2_util_unlink(tree, fname);
338 done:
339         return ret;
340 }
341
342 static bool test_compound_invalid1(struct torture_context *tctx,
343                                    struct smb2_tree *tree)
344 {
345         struct smb2_handle hd;
346         struct smb2_create cr;
347         NTSTATUS status;
348         const char *fname = "compound_invalid1.dat";
349         struct smb2_close cl;
350         bool ret = true;
351         struct smb2_request *req[2];
352
353         smb2_transport_credits_ask_num(tree->session->transport, 2);
354
355         smb2_util_unlink(tree, fname);
356
357         smb2_transport_credits_ask_num(tree->session->transport, 1);
358
359         ZERO_STRUCT(cr);
360         cr.in.security_flags            = 0x00;
361         cr.in.oplock_level              = 0;
362         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
363         cr.in.create_flags              = 0x00000000;
364         cr.in.reserved                  = 0x00000000;
365         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
366         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
367         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
368                                           NTCREATEX_SHARE_ACCESS_WRITE |
369                                           NTCREATEX_SHARE_ACCESS_DELETE;
370         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
371         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
372                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
373                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
374                                           0x00200000;
375         cr.in.fname                     = fname;
376
377         smb2_transport_compound_start(tree->session->transport, 2);
378
379         /* passing the first request with the related flag is invalid */
380         smb2_transport_compound_set_related(tree->session->transport, true);
381
382         req[0] = smb2_create_send(tree, &cr);
383
384         hd.data[0] = UINT64_MAX;
385         hd.data[1] = UINT64_MAX;
386
387         ZERO_STRUCT(cl);
388         cl.in.file.handle = hd;
389         req[1] = smb2_close_send(tree, &cl);
390
391         status = smb2_create_recv(req[0], tree, &cr);
392         /* TODO: check why this fails with --signing=required */
393         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
394         status = smb2_close_recv(req[1], &cl);
395         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
396
397         smb2_util_unlink(tree, fname);
398 done:
399         return ret;
400 }
401
402 static bool test_compound_invalid2(struct torture_context *tctx,
403                                    struct smb2_tree *tree)
404 {
405         struct smb2_handle hd;
406         struct smb2_create cr;
407         NTSTATUS status;
408         const char *fname = "compound_invalid2.dat";
409         struct smb2_close cl;
410         bool ret = true;
411         struct smb2_request *req[5];
412         struct smbXcli_tcon *saved_tcon = tree->smbXcli;
413         struct smbXcli_session *saved_session = tree->session->smbXcli;
414
415         smb2_transport_credits_ask_num(tree->session->transport, 5);
416
417         smb2_util_unlink(tree, fname);
418
419         smb2_transport_credits_ask_num(tree->session->transport, 1);
420
421         ZERO_STRUCT(cr);
422         cr.in.security_flags            = 0x00;
423         cr.in.oplock_level              = 0;
424         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
425         cr.in.create_flags              = 0x00000000;
426         cr.in.reserved                  = 0x00000000;
427         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
428         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
429         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
430                                           NTCREATEX_SHARE_ACCESS_WRITE |
431                                           NTCREATEX_SHARE_ACCESS_DELETE;
432         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
433         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
434                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
435                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
436                                           0x00200000;
437         cr.in.fname                     = fname;
438
439         smb2_transport_compound_start(tree->session->transport, 5);
440
441         req[0] = smb2_create_send(tree, &cr);
442
443         hd.data[0] = UINT64_MAX;
444         hd.data[1] = UINT64_MAX;
445
446         smb2_transport_compound_set_related(tree->session->transport, true);
447
448         ZERO_STRUCT(cl);
449         cl.in.file.handle = hd;
450
451         tree->smbXcli = smbXcli_tcon_create(tree);
452         smb2cli_tcon_set_values(tree->smbXcli,
453                                 NULL, /* session */
454                                 0xFFFFFFFF, /* tcon_id */
455                                 0, /* type */
456                                 0, /* flags */
457                                 0, /* capabilities */
458                                 0 /* maximal_access */);
459
460         tree->session->smbXcli = smbXcli_session_create(tree->session,
461                                                         tree->session->transport->conn);
462         smb2cli_session_set_id_and_flags(tree->session->smbXcli, UINT64_MAX, 0);
463
464         req[1] = smb2_close_send(tree, &cl);
465         /* strange that this is not generating invalid parameter */
466         smb2_transport_compound_set_related(tree->session->transport, false);
467         req[2] = smb2_close_send(tree, &cl);
468         req[3] = smb2_close_send(tree, &cl);
469         smb2_transport_compound_set_related(tree->session->transport, true);
470         req[4] = smb2_close_send(tree, &cl);
471
472         status = smb2_create_recv(req[0], tree, &cr);
473         CHECK_STATUS(status, NT_STATUS_OK);
474         status = smb2_close_recv(req[1], &cl);
475         CHECK_STATUS(status, NT_STATUS_OK);
476         status = smb2_close_recv(req[2], &cl);
477         CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
478         status = smb2_close_recv(req[3], &cl);
479         CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
480         status = smb2_close_recv(req[4], &cl);
481         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
482
483         TALLOC_FREE(tree->smbXcli);
484         tree->smbXcli = saved_tcon;
485         TALLOC_FREE(tree->session->smbXcli);
486         tree->session->smbXcli = saved_session;
487
488         smb2_util_unlink(tree, fname);
489 done:
490         return ret;
491 }
492
493 static bool test_compound_invalid3(struct torture_context *tctx,
494                                    struct smb2_tree *tree)
495 {
496         struct smb2_handle hd;
497         struct smb2_create cr;
498         NTSTATUS status;
499         const char *fname = "compound_invalid3.dat";
500         struct smb2_close cl;
501         bool ret = true;
502         struct smb2_request *req[5];
503
504         smb2_transport_credits_ask_num(tree->session->transport, 5);
505
506         smb2_util_unlink(tree, fname);
507
508         smb2_transport_credits_ask_num(tree->session->transport, 1);
509
510         ZERO_STRUCT(cr);
511         cr.in.security_flags            = 0x00;
512         cr.in.oplock_level              = 0;
513         cr.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
514         cr.in.create_flags              = 0x00000000;
515         cr.in.reserved                  = 0x00000000;
516         cr.in.desired_access            = SEC_RIGHTS_FILE_ALL;
517         cr.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
518         cr.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
519                                           NTCREATEX_SHARE_ACCESS_WRITE |
520                                           NTCREATEX_SHARE_ACCESS_DELETE;
521         cr.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
522         cr.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
523                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
524                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
525                                           0x00200000;
526         cr.in.fname                     = fname;
527
528         smb2_transport_compound_start(tree->session->transport, 5);
529
530         req[0] = smb2_create_send(tree, &cr);
531
532         hd.data[0] = UINT64_MAX;
533         hd.data[1] = UINT64_MAX;
534
535         ZERO_STRUCT(cl);
536         cl.in.file.handle = hd;
537         req[1] = smb2_close_send(tree, &cl);
538         req[2] = smb2_close_send(tree, &cl);
539         /* flipping the related flag is invalid */
540         smb2_transport_compound_set_related(tree->session->transport, true);
541         req[3] = smb2_close_send(tree, &cl);
542         req[4] = smb2_close_send(tree, &cl);
543
544         status = smb2_create_recv(req[0], tree, &cr);
545         CHECK_STATUS(status, NT_STATUS_OK);
546         status = smb2_close_recv(req[1], &cl);
547         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
548         status = smb2_close_recv(req[2], &cl);
549         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
550         status = smb2_close_recv(req[3], &cl);
551         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
552         status = smb2_close_recv(req[4], &cl);
553         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
554
555         smb2_util_unlink(tree, fname);
556 done:
557         return ret;
558 }
559
560 /* Send a compound request where we expect the last request (Create, Notify)
561  * to go asynchronous. This works against a Win7 server and the reply is
562  * sent in two different packets. */
563 static bool test_compound_interim1(struct torture_context *tctx,
564                                    struct smb2_tree *tree)
565 {
566     struct smb2_handle hd;
567     struct smb2_create cr;
568     NTSTATUS status = NT_STATUS_OK;
569     const char *dname = "compound_interim_dir";
570     struct smb2_notify nt;
571     bool ret = true;
572     struct smb2_request *req[2];
573
574     /* Win7 compound request implementation deviates substantially from the
575      * SMB2 spec as noted in MS-SMB2 <159>, <162>.  This, test currently
576      * verifies the Windows behavior, not the general spec behavior. */
577     if (!TARGET_IS_WIN7(tctx) && !TARGET_IS_W2K8(tctx)) {
578             torture_skip(tctx, "Interim test is specific to Windows server "
579                                "behavior.\n");
580     }
581
582     smb2_transport_credits_ask_num(tree->session->transport, 5);
583
584     smb2_deltree(tree, dname);
585
586     smb2_transport_credits_ask_num(tree->session->transport, 1);
587
588     ZERO_STRUCT(cr);
589     cr.in.desired_access        = SEC_RIGHTS_FILE_ALL;
590     cr.in.create_options        = NTCREATEX_OPTIONS_DIRECTORY;
591     cr.in.file_attributes       = FILE_ATTRIBUTE_DIRECTORY;
592     cr.in.share_access          = NTCREATEX_SHARE_ACCESS_READ |
593                                   NTCREATEX_SHARE_ACCESS_WRITE |
594                                   NTCREATEX_SHARE_ACCESS_DELETE;
595     cr.in.create_disposition    = NTCREATEX_DISP_CREATE;
596     cr.in.fname                 = dname;
597
598     smb2_transport_compound_start(tree->session->transport, 2);
599
600     req[0] = smb2_create_send(tree, &cr);
601
602     smb2_transport_compound_set_related(tree->session->transport, true);
603
604     hd.data[0] = UINT64_MAX;
605     hd.data[1] = UINT64_MAX;
606
607     ZERO_STRUCT(nt);
608     nt.in.recursive          = true;
609     nt.in.buffer_size        = 0x1000;
610     nt.in.file.handle        = hd;
611     nt.in.completion_filter  = FILE_NOTIFY_CHANGE_NAME;
612     nt.in.unknown            = 0x00000000;
613
614     req[1] = smb2_notify_send(tree, &nt);
615
616     status = smb2_create_recv(req[0], tree, &cr);
617     CHECK_STATUS(status, NT_STATUS_OK);
618
619     smb2_cancel(req[1]);
620     status = smb2_notify_recv(req[1], tree, &nt);
621     CHECK_STATUS(status, NT_STATUS_CANCELLED);
622
623     smb2_util_close(tree, cr.out.file.handle);
624
625     smb2_deltree(tree, dname);
626 done:
627     return ret;
628 }
629
630 /* Send a compound request where we expect the middle request (Create, Notify,
631  * GetInfo) to go asynchronous. Against Win7 the sync request succeed while
632  * the async fails. All are returned in the same compound response. */
633 static bool test_compound_interim2(struct torture_context *tctx,
634                                    struct smb2_tree *tree)
635 {
636     struct smb2_handle hd;
637     struct smb2_create cr;
638     NTSTATUS status = NT_STATUS_OK;
639     const char *dname = "compound_interim_dir";
640     struct smb2_getinfo gf;
641     struct smb2_notify  nt;
642     bool ret = true;
643     struct smb2_request *req[3];
644
645     /* Win7 compound request implementation deviates substantially from the
646      * SMB2 spec as noted in MS-SMB2 <159>, <162>.  This, test currently
647      * verifies the Windows behavior, not the general spec behavior. */
648     if (!TARGET_IS_WIN7(tctx) && !TARGET_IS_W2K8(tctx)) {
649             torture_skip(tctx, "Interim test is specific to Windows server "
650                                "behavior.\n");
651     }
652
653     smb2_transport_credits_ask_num(tree->session->transport, 5);
654
655     smb2_deltree(tree, dname);
656
657     smb2_transport_credits_ask_num(tree->session->transport, 1);
658
659     ZERO_STRUCT(cr);
660     cr.in.desired_access        = SEC_RIGHTS_FILE_ALL;
661     cr.in.create_options        = NTCREATEX_OPTIONS_DIRECTORY;
662     cr.in.file_attributes       = FILE_ATTRIBUTE_DIRECTORY;
663     cr.in.share_access      = NTCREATEX_SHARE_ACCESS_READ |
664                       NTCREATEX_SHARE_ACCESS_WRITE |
665                       NTCREATEX_SHARE_ACCESS_DELETE;
666     cr.in.create_disposition    = NTCREATEX_DISP_CREATE;
667     cr.in.fname         = dname;
668
669     smb2_transport_compound_start(tree->session->transport, 3);
670
671     req[0] = smb2_create_send(tree, &cr);
672
673     smb2_transport_compound_set_related(tree->session->transport, true);
674
675     hd.data[0] = UINT64_MAX;
676     hd.data[1] = UINT64_MAX;
677
678     ZERO_STRUCT(nt);
679     nt.in.recursive          = true;
680     nt.in.buffer_size        = 0x1000;
681     nt.in.file.handle        = hd;
682     nt.in.completion_filter  = FILE_NOTIFY_CHANGE_NAME;
683     nt.in.unknown            = 0x00000000;
684
685     req[1] = smb2_notify_send(tree, &nt);
686
687     ZERO_STRUCT(gf);
688     gf.in.file.handle = hd;
689     gf.in.info_type   = SMB2_GETINFO_FILE;
690     gf.in.info_class  = 0x04; /* FILE_BASIC_INFORMATION */
691     gf.in.output_buffer_length = 0x1000;
692     gf.in.input_buffer_length = 0;
693
694     req[2] = smb2_getinfo_send(tree, &gf);
695
696     status = smb2_create_recv(req[0], tree, &cr);
697     CHECK_STATUS(status, NT_STATUS_OK);
698
699     status = smb2_notify_recv(req[1], tree, &nt);
700     CHECK_STATUS(status, NT_STATUS_INTERNAL_ERROR);
701
702     status = smb2_getinfo_recv(req[2], tree, &gf);
703     CHECK_STATUS(status, NT_STATUS_OK);
704
705     smb2_util_close(tree, cr.out.file.handle);
706
707     smb2_deltree(tree, dname);
708 done:
709     return ret;
710 }
711
712 struct torture_suite *torture_smb2_compound_init(void)
713 {
714         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "compound");
715
716         torture_suite_add_1smb2_test(suite, "related1", test_compound_related1);
717         torture_suite_add_1smb2_test(suite, "related2", test_compound_related2);
718         torture_suite_add_1smb2_test(suite, "related3",
719                                      test_compound_related3);
720         torture_suite_add_1smb2_test(suite, "unrelated1", test_compound_unrelated1);
721         torture_suite_add_1smb2_test(suite, "invalid1", test_compound_invalid1);
722         torture_suite_add_1smb2_test(suite, "invalid2", test_compound_invalid2);
723         torture_suite_add_1smb2_test(suite, "invalid3", test_compound_invalid3);
724         torture_suite_add_1smb2_test(suite, "interim1",  test_compound_interim1);
725         torture_suite_add_1smb2_test(suite, "interim2",  test_compound_interim2);
726
727         suite->description = talloc_strdup(suite, "SMB2-COMPOUND tests");
728
729         return suite;
730 }