e67517324bdec70d3468027775ac58658dbcdd94
[ira/wip.git] / source4 / torture / smb2 / lease.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 leases
5
6    Copyright (C) Zachary Loafman 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 "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 static void smb2_lease_create(struct smb2_create *io, struct smb2_lease *ls,
45                               bool dir, const char *name, uint64_t leasekey,
46                               uint32_t leasestate)
47 {
48         ZERO_STRUCT(*io);
49         io->in.security_flags           = 0x00;
50         io->in.oplock_level             = SMB2_OPLOCK_LEVEL_LEASE;
51         io->in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
52         io->in.create_flags             = 0x00000000;
53         io->in.reserved                 = 0x00000000;
54         io->in.desired_access           = SEC_RIGHTS_FILE_ALL;
55         io->in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
56         io->in.share_access             = NTCREATEX_SHARE_ACCESS_READ |
57                                           NTCREATEX_SHARE_ACCESS_WRITE |
58                                           NTCREATEX_SHARE_ACCESS_DELETE;
59         io->in.create_disposition       = NTCREATEX_DISP_OPEN_IF;
60         io->in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
61                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
62                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
63                                           0x00200000;
64         io->in.fname                    = name;
65
66         if (dir) {
67                 io->in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
68                 io->in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
69                 io->in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
70                 io->in.create_disposition = NTCREATEX_DISP_CREATE;
71         }
72
73         ZERO_STRUCT(*ls);
74         ls->lease_key[0] = leasekey;
75         ls->lease_key[1] = ~leasekey;
76         ls->lease_state = leasestate;
77         io->in.lease_request = ls;
78 }
79
80 #define CHECK_CREATED(__io, __created, __attribute)                     \
81         do {                                                            \
82                 if (__created) {                                        \
83                         CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_CREATED); \
84                 } else {                                                \
85                         CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_EXISTED); \
86                 }                                                       \
87                 CHECK_VAL((__io)->out.alloc_size, 0);                   \
88                 CHECK_VAL((__io)->out.size, 0);                         \
89                 CHECK_VAL((__io)->out.file_attr, (__attribute));        \
90                 CHECK_VAL((__io)->out.reserved2, 0);                    \
91         } while(0)
92
93 #define CHECK_LEASE(__io, __state, __oplevel, __key)                    \
94         do {                                                            \
95                 if (__oplevel) {                                        \
96                         CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE); \
97                         CHECK_VAL((__io)->out.lease_response.lease_key[0], (__key)); \
98                         CHECK_VAL((__io)->out.lease_response.lease_key[1], ~(__key)); \
99                         CHECK_VAL((__io)->out.lease_response.lease_state, (__state)); \
100                 } else {                                                \
101                         CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_NONE); \
102                         CHECK_VAL((__io)->out.lease_response.lease_key[0], 0); \
103                         CHECK_VAL((__io)->out.lease_response.lease_key[1], 0); \
104                         CHECK_VAL((__io)->out.lease_response.lease_state, 0); \
105                 }                                                       \
106                                                                         \
107                 CHECK_VAL((__io)->out.lease_response.lease_flags, 0);   \
108                 CHECK_VAL((__io)->out.lease_response.lease_duration, 0); \
109         } while(0)                                                      \
110
111 static const uint64_t LEASE1 = 0xBADC0FFEE0DDF00Dull;
112 static const uint64_t LEASE2 = 0xDEADBEEFFEEDBEADull;
113 static const uint64_t LEASE3 = 0xDAD0FFEDD00DF00Dull;
114
115 #define NRESULTS 8
116 static const int request_results[NRESULTS][2] = {
117         { SMB2_LEASE_NONE, SMB2_LEASE_NONE },
118         { SMB2_LEASE_READ, SMB2_LEASE_READ },
119         { SMB2_LEASE_HANDLE, SMB2_LEASE_NONE, },
120         { SMB2_LEASE_WRITE, SMB2_LEASE_NONE },
121         { SMB2_LEASE_READ|SMB2_LEASE_HANDLE,
122           SMB2_LEASE_READ|SMB2_LEASE_HANDLE },
123         { SMB2_LEASE_READ|SMB2_LEASE_WRITE,
124           SMB2_LEASE_READ|SMB2_LEASE_WRITE },
125         { SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE, SMB2_LEASE_NONE },
126         { SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE,
127           SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE },
128 };
129
130 static bool test_lease_request(struct torture_context *tctx,
131                                struct smb2_tree *tree)
132 {
133         TALLOC_CTX *mem_ctx = talloc_new(tctx);
134         struct smb2_create io;
135         struct smb2_lease ls;
136         struct smb2_handle h1, h2;
137         NTSTATUS status;
138         const char *fname = "lease.dat";
139         const char *fname2 = "lease2.dat";
140         const char *sname = "lease.dat:stream";
141         const char *dname = "lease.dir";
142         bool ret = true;
143         int i;
144
145         smb2_util_unlink(tree, fname);
146         smb2_util_unlink(tree, fname2);
147         smb2_util_rmdir(tree, dname);
148
149         /* Win7 is happy to grant RHW leases on files. */
150         smb2_lease_create(&io, &ls, false, fname, LEASE1,
151             SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
152         status = smb2_create(tree, mem_ctx, &io);
153         CHECK_STATUS(status, NT_STATUS_OK);
154         h1 = io.out.file.handle;
155         CHECK_CREATED(&io, true, FILE_ATTRIBUTE_ARCHIVE);
156         CHECK_LEASE(&io, SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE,
157             true, LEASE1);
158
159         /* But will reject leases on directories. */
160         smb2_lease_create(&io, &ls, true, dname, LEASE2,
161             SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
162         status = smb2_create(tree, mem_ctx, &io);
163         CHECK_STATUS(status, NT_STATUS_OK);
164         CHECK_CREATED(&io, true, FILE_ATTRIBUTE_DIRECTORY);
165         CHECK_LEASE(&io, SMB2_LEASE_NONE, false, 0);
166         smb2_util_close(tree, io.out.file.handle);
167
168         /* Also rejects multiple files leased under the same key. */
169         smb2_lease_create(&io, &ls, true, fname2, LEASE1,
170             SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
171         status = smb2_create(tree, mem_ctx, &io);
172         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
173
174         /* And grants leases on streams (with separate leasekey). */
175         smb2_lease_create(&io, &ls, false, sname, LEASE2,
176             SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
177         status = smb2_create(tree, mem_ctx, &io);
178         h2 = io.out.file.handle;
179         CHECK_STATUS(status, NT_STATUS_OK);
180         CHECK_CREATED(&io, true, FILE_ATTRIBUTE_ARCHIVE);
181         CHECK_LEASE(&io, SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE,
182             true, LEASE2);
183         smb2_util_close(tree, h2);
184
185         smb2_util_close(tree, h1);
186
187         /* Now see what combos are actually granted. */
188         for (i = 0; i < NRESULTS; i++) {
189                 torture_comment(tctx, "Testing lease type %x, expecting %x\n",
190                     request_results[i][0], request_results[i][1]);
191                 smb2_lease_create(&io, &ls, false, fname, LEASE1,
192                     request_results[i][0]);
193                 status = smb2_create(tree, mem_ctx, &io);
194                 h2 = io.out.file.handle;
195                 CHECK_STATUS(status, NT_STATUS_OK);
196                 CHECK_CREATED(&io, false, FILE_ATTRIBUTE_ARCHIVE);
197                 CHECK_LEASE(&io, request_results[i][1], true, LEASE1);
198                 smb2_util_close(tree, io.out.file.handle);
199         }
200
201  done:
202         smb2_util_close(tree, h1);
203         smb2_util_close(tree, h2);
204
205         smb2_util_unlink(tree, fname);
206         smb2_util_unlink(tree, fname2);
207         smb2_util_rmdir(tree, dname);
208
209         talloc_free(mem_ctx);
210
211         return ret;
212 }
213
214 static bool test_lease_upgrade(struct torture_context *tctx,
215                                struct smb2_tree *tree)
216 {
217         TALLOC_CTX *mem_ctx = talloc_new(tctx);
218         struct smb2_create io;
219         struct smb2_lease ls;
220         struct smb2_handle h, hnew;
221         NTSTATUS status;
222         const char *fname = "lease.dat";
223         bool ret = true;
224
225         smb2_util_unlink(tree, fname);
226
227         /* Grab a RH lease. */
228         smb2_lease_create(&io, &ls, false, fname, LEASE1,
229             SMB2_LEASE_READ|SMB2_LEASE_HANDLE);
230         status = smb2_create(tree, mem_ctx, &io);
231         CHECK_STATUS(status, NT_STATUS_OK);
232         CHECK_CREATED(&io, true, FILE_ATTRIBUTE_ARCHIVE);
233         CHECK_LEASE(&io,
234             SMB2_LEASE_READ|SMB2_LEASE_HANDLE, true, LEASE1);
235         h = io.out.file.handle;
236
237         /* Upgrades (sidegrades?) to RW leave us with an RH. */
238         smb2_lease_create(&io, &ls, false, fname, LEASE1,
239             SMB2_LEASE_READ|SMB2_LEASE_WRITE);
240         status = smb2_create(tree, mem_ctx, &io);
241         CHECK_STATUS(status, NT_STATUS_OK);
242         CHECK_CREATED(&io, false, FILE_ATTRIBUTE_ARCHIVE);
243         CHECK_LEASE(&io,
244             SMB2_LEASE_READ|SMB2_LEASE_HANDLE, true, LEASE1);
245         hnew = io.out.file.handle;
246
247         smb2_util_close(tree, hnew);
248
249         /* Upgrade to RHW lease. */
250         smb2_lease_create(&io, &ls, false, fname, LEASE1,
251             SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
252         status = smb2_create(tree, mem_ctx, &io);
253         CHECK_STATUS(status, NT_STATUS_OK);
254         CHECK_CREATED(&io, false, FILE_ATTRIBUTE_ARCHIVE);
255         CHECK_LEASE(&io, SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE,
256             true, LEASE1);
257         hnew = io.out.file.handle;
258
259         smb2_util_close(tree, h);
260         h = hnew;
261
262         /* Attempt to downgrade - original lease state is maintained. */
263         smb2_lease_create(&io, &ls, false, fname, LEASE1,
264             SMB2_LEASE_READ|SMB2_LEASE_HANDLE);
265         status = smb2_create(tree, mem_ctx, &io);
266         CHECK_STATUS(status, NT_STATUS_OK);
267         CHECK_CREATED(&io, false, FILE_ATTRIBUTE_ARCHIVE);
268         CHECK_LEASE(&io, SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE,
269             true, LEASE1);
270         hnew = io.out.file.handle;
271
272         smb2_util_close(tree, hnew);
273
274  done:
275         smb2_util_close(tree, h);
276         smb2_util_close(tree, hnew);
277
278         smb2_util_unlink(tree, fname);
279
280         talloc_free(mem_ctx);
281
282         return ret;
283 }
284
285 struct torture_suite *torture_smb2_lease_init(void)
286 {
287         struct torture_suite *suite =
288             torture_suite_create(talloc_autofree_context(), "LEASE");
289
290         torture_suite_add_1smb2_test(suite, "REQUEST", test_lease_request);
291         torture_suite_add_1smb2_test(suite, "UPGRADE", test_lease_upgrade);
292
293         suite->description = talloc_strdup(suite, "SMB2-LEASE tests");
294
295         return suite;
296 }