Add durable open / oplock interaction test. Rather than give it a chance to disconnec...
[ira/wip.git] / source4 / torture / smb2 / durable_open.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 durable opens
5
6    Copyright (C) Stefan Metzmacher 2008
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 "librpc/gen_ndr/security.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28
29 #define CHECK_VAL(v, correct) do { \
30         if ((v) != (correct)) { \
31                 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%x - should be 0x%x\n", \
32                                 __location__, #v, (int)v, (int)correct); \
33                 ret = false; \
34         }} while (0)
35
36 #define CHECK_STATUS(status, correct) do { \
37         if (!NT_STATUS_EQUAL(status, correct)) { \
38                 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
39                        nt_errstr(status), nt_errstr(correct)); \
40                 ret = false; \
41                 goto done; \
42         }} while (0)
43
44 /*
45    basic testing of SMB2 durable opens
46    regarding the position information on the handle
47 */
48 bool test_durable_open_file_position(struct torture_context *tctx,
49                                      struct smb2_tree *tree1,
50                                      struct smb2_tree *tree2)
51 {
52         TALLOC_CTX *mem_ctx = talloc_new(tctx);
53         struct smb2_handle h1, h2;
54         struct smb2_create io1, io2;
55         NTSTATUS status;
56         const char *fname = "durable_open_position.dat";
57         DATA_BLOB b;
58         union smb_fileinfo qfinfo;
59         union smb_setfileinfo sfinfo;
60         bool ret = true;
61         uint64_t pos;
62
63         smb2_util_unlink(tree1, fname);
64
65         ZERO_STRUCT(io1);
66         io1.in.security_flags           = 0x00;
67         io1.in.oplock_level             = SMB2_OPLOCK_LEVEL_BATCH;
68         io1.in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
69         io1.in.create_flags             = 0x00000000;
70         io1.in.reserved                 = 0x00000000;
71         io1.in.desired_access           = SEC_RIGHTS_FILE_ALL;
72         io1.in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
73         io1.in.share_access             = NTCREATEX_SHARE_ACCESS_READ |
74                                           NTCREATEX_SHARE_ACCESS_WRITE |
75                                           NTCREATEX_SHARE_ACCESS_DELETE;
76         io1.in.create_disposition       = NTCREATEX_DISP_OPEN_IF;
77         io1.in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
78                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
79                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
80                                           0x00200000;
81         io1.in.fname                    = fname;
82
83         b = data_blob_talloc(mem_ctx, NULL, 16);
84         SBVAL(b.data, 0, 0);
85         SBVAL(b.data, 8, 0);
86
87         status = smb2_create_blob_add(tree1, &io1.in.blobs,
88                                       SMB2_CREATE_TAG_DHNQ,
89                                       b);
90         CHECK_STATUS(status, NT_STATUS_OK);
91
92         status = smb2_create(tree1, mem_ctx, &io1);
93         CHECK_STATUS(status, NT_STATUS_OK);
94         CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
95         /*CHECK_VAL(io1.out.reserved, 0);*/
96         CHECK_VAL(io1.out.create_action, NTCREATEX_ACTION_CREATED);
97         CHECK_VAL(io1.out.alloc_size, 0);
98         CHECK_VAL(io1.out.size, 0);
99         CHECK_VAL(io1.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
100         CHECK_VAL(io1.out.reserved2, 0);
101
102         /* TODO: check extra blob content */
103
104         h1 = io1.out.file.handle;
105
106         ZERO_STRUCT(qfinfo);
107         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
108         qfinfo.generic.in.file.handle = h1;
109         status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
110         CHECK_STATUS(status, NT_STATUS_OK);
111         CHECK_VAL(qfinfo.position_information.out.position, 0);
112         pos = qfinfo.position_information.out.position;
113         torture_comment(tctx, "position: %llu\n",
114                         (unsigned long long)pos);
115
116         ZERO_STRUCT(sfinfo);
117         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
118         sfinfo.generic.in.file.handle = h1;
119         sfinfo.position_information.in.position = 0x1000;
120         status = smb2_setinfo_file(tree1, &sfinfo);
121         CHECK_STATUS(status, NT_STATUS_OK);
122
123         ZERO_STRUCT(qfinfo);
124         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
125         qfinfo.generic.in.file.handle = h1;
126         status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
127         CHECK_STATUS(status, NT_STATUS_OK);
128         CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
129         pos = qfinfo.position_information.out.position;
130         torture_comment(tctx, "position: %llu\n",
131                         (unsigned long long)pos);
132
133         talloc_free(tree1);
134         tree1 = NULL;
135
136         ZERO_STRUCT(qfinfo);
137         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
138         qfinfo.generic.in.file.handle = h1;
139         status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
140         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
141
142         ZERO_STRUCT(io2);
143         io2.in.fname = fname;
144
145         b = data_blob_talloc(tctx, NULL, 16);
146         SBVAL(b.data, 0, h1.data[0]);
147         SBVAL(b.data, 8, h1.data[1]);
148
149         status = smb2_create_blob_add(tree2, &io2.in.blobs,
150                                       SMB2_CREATE_TAG_DHNC,
151                                       b);
152         CHECK_STATUS(status, NT_STATUS_OK);
153
154         status = smb2_create(tree2, mem_ctx, &io2);
155         CHECK_STATUS(status, NT_STATUS_OK);
156         CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
157         CHECK_VAL(io2.out.reserved, 0x00);
158         CHECK_VAL(io2.out.create_action, NTCREATEX_ACTION_EXISTED);
159         CHECK_VAL(io2.out.alloc_size, 0);
160         CHECK_VAL(io2.out.size, 0);
161         CHECK_VAL(io2.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
162         CHECK_VAL(io2.out.reserved2, 0);
163
164         h2 = io2.out.file.handle;
165
166         ZERO_STRUCT(qfinfo);
167         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
168         qfinfo.generic.in.file.handle = h2;
169         status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
170         CHECK_STATUS(status, NT_STATUS_OK);
171         CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
172         pos = qfinfo.position_information.out.position;
173         torture_comment(tctx, "position: %llu\n",
174                         (unsigned long long)pos);
175
176         smb2_util_close(tree2, h2);
177
178         talloc_free(mem_ctx);
179
180         smb2_util_unlink(tree2, fname);
181 done:
182         return ret;
183 }
184
185 /*
186   Open, disconnect, oplock break, reconnect.
187 */
188 bool test_durable_open_oplock(struct torture_context *tctx,
189                               struct smb2_tree *tree1,
190                               struct smb2_tree *tree2)
191 {
192         TALLOC_CTX *mem_ctx = talloc_new(tctx);
193         struct smb2_create io1, io2, io3;
194         struct smb2_handle h1;
195         NTSTATUS status;
196         const char *fname = "durable_open_oplock.dat";
197         DATA_BLOB b;
198         bool ret = true;
199
200         /* Clean slate */
201         smb2_util_unlink(tree1, fname);
202
203         /* Create with batch oplock */
204         ZERO_STRUCT(io1);
205         io1.in.security_flags           = 0x00;
206         io1.in.oplock_level             = SMB2_OPLOCK_LEVEL_BATCH;
207         io1.in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
208         io1.in.create_flags             = 0x00000000;
209         io1.in.reserved                 = 0x00000000;
210         io1.in.desired_access           = SEC_RIGHTS_FILE_ALL;
211         io1.in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
212         io1.in.share_access             = NTCREATEX_SHARE_ACCESS_READ |
213                                           NTCREATEX_SHARE_ACCESS_WRITE |
214                                           NTCREATEX_SHARE_ACCESS_DELETE;
215         io1.in.create_disposition       = NTCREATEX_DISP_OPEN_IF;
216         io1.in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
217                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
218                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
219                                           0x00200000;
220         io1.in.fname                    = fname;
221
222         io2 = io1;
223         io2.in.create_disposition       = NTCREATEX_DISP_OPEN;
224
225         b = data_blob_talloc(mem_ctx, NULL, 16);
226         SBVAL(b.data, 0, 0);
227         SBVAL(b.data, 8, 0);
228
229         status = smb2_create_blob_add(tree1, &io1.in.blobs,
230                                       SMB2_CREATE_TAG_DHNQ,
231                                       b);
232         CHECK_STATUS(status, NT_STATUS_OK);
233
234         status = smb2_create(tree1, mem_ctx, &io1);
235         CHECK_STATUS(status, NT_STATUS_OK);
236         CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
237         CHECK_VAL(io1.out.create_action, NTCREATEX_ACTION_CREATED);
238         CHECK_VAL(io1.out.alloc_size, 0);
239         CHECK_VAL(io1.out.size, 0);
240         CHECK_VAL(io1.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
241         CHECK_VAL(io1.out.reserved2, 0);
242
243         h1 = io1.out.file.handle;
244
245         /* Disconnect after getting the batch */
246         talloc_free(tree1);
247         tree1 = NULL;
248
249         /*
250          * Windows7 (build 7000) will break a batch oplock immediately if the
251          * original client is gone. (ZML: This seems like a bug. It should give
252          * some time for the client to reconnect!)
253          */
254         status = smb2_create(tree2, mem_ctx, &io2);
255         CHECK_STATUS(status, NT_STATUS_OK);
256         CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
257         CHECK_VAL(io2.out.create_action, NTCREATEX_ACTION_EXISTED);
258         CHECK_VAL(io2.out.alloc_size, 0);
259         CHECK_VAL(io2.out.size, 0);
260         CHECK_VAL(io2.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
261         CHECK_VAL(io2.out.reserved2, 0);
262
263         /* What if tree1 tries to come back and reclaim? */
264         if (!torture_smb2_connection(tctx, &tree1)) {
265                 torture_warning(tctx, "couldn't reconnect, bailing\n");
266                 ret = false;
267                 goto done;
268         }
269
270         ZERO_STRUCT(io2);
271         io2.in.fname = fname;
272
273         b = data_blob_talloc(tctx, NULL, 16);
274         SBVAL(b.data, 0, h1.data[0]);
275         SBVAL(b.data, 8, h1.data[1]);
276
277         status = smb2_create_blob_add(tree2, &io2.in.blobs,
278                                       SMB2_CREATE_TAG_DHNC,
279                                       b);
280         CHECK_STATUS(status, NT_STATUS_OK);
281
282         status = smb2_create(tree2, mem_ctx, &io2);
283         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
284
285 done:
286         return ret;
287
288 }
289
290 struct torture_suite *torture_smb2_durable_open_init(void)
291 {
292         struct torture_suite *suite =
293             torture_suite_create(talloc_autofree_context(), "DURABLE-OPEN");
294
295         torture_suite_add_2smb2_test(suite, "FILE-POSITION",
296             test_durable_open_file_position);
297         torture_suite_add_2smb2_test(suite, "OPLOCK", test_durable_open_oplock);
298
299         suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
300 }