8ab61c7934e31a0992471562c4f2c28843c855a1
[samba.git] / source4 / torture / smb2 / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    helper functions for SMB2 test suite
5
6    Copyright (C) Andrew Tridgell 2005
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/security/security_descriptor.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "system/time.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
30 #include "libcli/resolve/resolve.h"
31
32 #include "torture/torture.h"
33 #include "torture/smb2/proto.h"
34
35
36 /*
37   write to a file on SMB2
38 */
39 NTSTATUS smb2_util_write(struct smb2_tree *tree,
40                          struct smb2_handle handle, 
41                          const void *buf, off_t offset, size_t size)
42 {
43         struct smb2_write w;
44
45         ZERO_STRUCT(w);
46         w.in.file.handle = handle;
47         w.in.offset      = offset;
48         w.in.data        = data_blob_const(buf, size);
49
50         return smb2_write(tree, &w);
51 }
52
53 /*
54   create a complex file/dir using the SMB2 protocol
55 */
56 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname, 
57                                          struct smb2_handle *handle, bool dir)
58 {
59         TALLOC_CTX *tmp_ctx = talloc_new(tree);
60         char buf[7] = "abc";
61         struct smb2_create io;
62         union smb_setfileinfo setfile;
63         union smb_fileinfo fileinfo;
64         time_t t = (time(NULL) & ~1);
65         NTSTATUS status;
66
67         smb2_util_unlink(tree, fname);
68         ZERO_STRUCT(io);
69         io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
70         io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
71         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
72         io.in.share_access = 
73                 NTCREATEX_SHARE_ACCESS_DELETE|
74                 NTCREATEX_SHARE_ACCESS_READ|
75                 NTCREATEX_SHARE_ACCESS_WRITE;
76         io.in.create_options = 0;
77         io.in.fname = fname;
78         if (dir) {
79                 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
80                 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
81                 io.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
82                 io.in.create_disposition = NTCREATEX_DISP_CREATE;
83         }
84
85         /* it seems vista is now fussier about alignment? */
86         if (strchr(fname, ':') == NULL) {
87                 /* setup some EAs */
88                 io.in.eas.num_eas = 2;
89                 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
90                 io.in.eas.eas[0].flags = 0;
91                 io.in.eas.eas[0].name.s = "EAONE";
92                 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
93                 io.in.eas.eas[1].flags = 0;
94                 io.in.eas.eas[1].name.s = "SECONDEA";
95                 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
96         }
97
98         status = smb2_create(tree, tmp_ctx, &io);
99         talloc_free(tmp_ctx);
100         NT_STATUS_NOT_OK_RETURN(status);
101
102         *handle = io.out.file.handle;
103
104         if (!dir) {
105                 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
106                 NT_STATUS_NOT_OK_RETURN(status);
107         }
108
109         /* make sure all the timestamps aren't the same, and are also 
110            in different DST zones*/
111         setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
112         setfile.generic.in.file.handle = *handle;
113
114         unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
115         unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
116         unix_to_nt_time(&setfile.basic_info.in.write_time,  t + 3*30*24*60*60);
117         unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
118         setfile.basic_info.in.attrib      = FILE_ATTRIBUTE_NORMAL;
119
120         status = smb2_setinfo_file(tree, &setfile);
121         if (!NT_STATUS_IS_OK(status)) {
122                 printf("Failed to setup file times - %s\n", nt_errstr(status));
123                 return status;
124         }
125
126         /* make sure all the timestamps aren't the same */
127         fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
128         fileinfo.generic.in.file.handle = *handle;
129
130         status = smb2_getinfo_file(tree, tree, &fileinfo);
131         if (!NT_STATUS_IS_OK(status)) {
132                 printf("Failed to query file times - %s\n", nt_errstr(status));
133                 return status;
134                 
135         }
136
137 #define CHECK_TIME(field) do {\
138         if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
139                 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
140                         __location__, \
141                         nt_time_string(tree, setfile.basic_info.in.field), \
142                         (unsigned long long)setfile.basic_info.in.field, \
143                         nt_time_string(tree, fileinfo.basic_info.out.field), \
144                         (unsigned long long)fileinfo.basic_info.out.field); \
145                 status = NT_STATUS_INVALID_PARAMETER; \
146         } \
147 } while (0)
148
149         CHECK_TIME(create_time);
150         CHECK_TIME(access_time);
151         CHECK_TIME(write_time);
152         CHECK_TIME(change_time);
153
154         return status;
155 }
156
157 /*
158   create a complex file using the SMB2 protocol
159 */
160 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname, 
161                                          struct smb2_handle *handle)
162 {
163         return smb2_create_complex(tree, fname, handle, false);
164 }
165
166 /*
167   create a complex dir using the SMB2 protocol
168 */
169 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname, 
170                                  struct smb2_handle *handle)
171 {
172         return smb2_create_complex(tree, fname, handle, true);
173 }
174
175 /*
176   show lots of information about a file
177 */
178 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
179 {
180         NTSTATUS status;
181         TALLOC_CTX *tmp_ctx = talloc_new(tree);
182         union smb_fileinfo io;
183
184         io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
185         io.generic.in.file.handle = handle;
186
187         status = smb2_getinfo_file(tree, tmp_ctx, &io);
188         if (!NT_STATUS_IS_OK(status)) {
189                 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
190                 talloc_free(tmp_ctx);
191                 return;
192         }
193
194         d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
195         d_printf("\tcreate_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
196         d_printf("\taccess_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
197         d_printf("\twrite_time:     %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
198         d_printf("\tchange_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
199         d_printf("\tattrib:         0x%x\n", io.all_info2.out.attrib);
200         d_printf("\tunknown1:       0x%x\n", io.all_info2.out.unknown1);
201         d_printf("\talloc_size:     %llu\n", (long long)io.all_info2.out.alloc_size);
202         d_printf("\tsize:           %llu\n", (long long)io.all_info2.out.size);
203         d_printf("\tnlink:          %u\n", io.all_info2.out.nlink);
204         d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
205         d_printf("\tdirectory:      %u\n", io.all_info2.out.directory);
206         d_printf("\tfile_id:        %llu\n", (long long)io.all_info2.out.file_id);
207         d_printf("\tea_size:        %u\n", io.all_info2.out.ea_size);
208         d_printf("\taccess_mask:    0x%08x\n", io.all_info2.out.access_mask);
209         d_printf("\tposition:       0x%llx\n", (long long)io.all_info2.out.position);
210         d_printf("\tmode:           0x%llx\n", (long long)io.all_info2.out.mode);
211
212         /* short name, if any */
213         io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
214         status = smb2_getinfo_file(tree, tmp_ctx, &io);
215         if (NT_STATUS_IS_OK(status)) {
216                 d_printf("\tshort name:     '%s'\n", io.alt_name_info.out.fname.s);
217         }
218
219         /* the EAs, if any */
220         io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
221         status = smb2_getinfo_file(tree, tmp_ctx, &io);
222         if (NT_STATUS_IS_OK(status)) {
223                 int i;
224                 for (i=0;i<io.all_eas.out.num_eas;i++) {
225                         d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
226                                  io.all_eas.out.eas[i].flags,
227                                  (int)io.all_eas.out.eas[i].value.length,
228                                  io.all_eas.out.eas[i].name.s);
229                 }
230         }
231
232         /* streams, if available */
233         io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
234         status = smb2_getinfo_file(tree, tmp_ctx, &io);
235         if (NT_STATUS_IS_OK(status)) {
236                 int i;
237                 for (i=0;i<io.stream_info.out.num_streams;i++) {
238                         d_printf("\tstream %d:\n", i);
239                         d_printf("\t\tsize       %ld\n", 
240                                  (long)io.stream_info.out.streams[i].size);
241                         d_printf("\t\talloc size %ld\n", 
242                                  (long)io.stream_info.out.streams[i].alloc_size);
243                         d_printf("\t\tname       %s\n", io.stream_info.out.streams[i].stream_name.s);
244                 }
245         }       
246
247         if (DEBUGLVL(1)) {
248                 /* the security descriptor */
249                 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
250                 io.query_secdesc.in.secinfo_flags = 
251                         SECINFO_OWNER|SECINFO_GROUP|
252                         SECINFO_DACL;
253                 status = smb2_getinfo_file(tree, tmp_ctx, &io);
254                 if (NT_STATUS_IS_OK(status)) {
255                         NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
256                 }
257         }
258
259         talloc_free(tmp_ctx);   
260 }
261
262 /**
263  * open a smb2 tree connect
264  */
265 bool torture_smb2_tree_connect(struct torture_context *tctx,
266                                struct smb2_session *session,
267                                TALLOC_CTX *mem_ctx,
268                                struct smb2_tree **_tree)
269 {
270         NTSTATUS status;
271         const char *host = torture_setting_string(tctx, "host", NULL);
272         const char *share = torture_setting_string(tctx, "share", NULL);
273         struct smb2_tree_connect tcon;
274         struct smb2_tree *tree;
275
276         ZERO_STRUCT(tcon);
277         tcon.in.reserved = 0;
278         tcon.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
279         if (tcon.in.path == NULL) {
280                 printf("talloc failed\n");
281                 return false;
282         }
283
284         status = smb2_tree_connect(session, &tcon);
285         if (!NT_STATUS_IS_OK(status)) {
286                 printf("Failed to tree_connect to SMB2 share \\\\%s\\%s - %s\n",
287                        host, share, nt_errstr(status));
288                 return false;
289         }
290
291         tree = smb2_tree_init(session, mem_ctx, true);
292         if (tree == NULL) {
293                 printf("talloc failed\n");
294                 return false;
295         }
296
297         tree->tid = tcon.out.tid;
298
299         *_tree = tree;
300
301         return true;
302 }
303
304
305 /*
306   open a smb2 connection
307 */
308 bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tree)
309 {
310         NTSTATUS status;
311         const char *host = torture_setting_string(tctx, "host", NULL);
312         const char *share = torture_setting_string(tctx, "share", NULL);
313         struct cli_credentials *credentials = cmdline_credentials;
314         struct smbcli_options options;
315
316         lpcfg_smbcli_options(tctx->lp_ctx, &options);
317
318         status = smb2_connect(tctx,
319                               host,
320                               lpcfg_smb_ports(tctx->lp_ctx),
321                               share,
322                               lpcfg_resolve_context(tctx->lp_ctx),
323                               credentials,
324                               tree,
325                               tctx->ev,
326                               &options,
327                               lpcfg_socket_options(tctx->lp_ctx),
328                               lpcfg_gensec_settings(tctx, tctx->lp_ctx)
329                               );
330         if (!NT_STATUS_IS_OK(status)) {
331                 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
332                        host, share, nt_errstr(status));
333                 return false;
334         }
335         return true;
336 }
337
338
339 /*
340   create and return a handle to a test file
341 */
342 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname, 
343                                struct smb2_handle *handle)
344 {
345         struct smb2_create io;
346         NTSTATUS status;
347
348         ZERO_STRUCT(io);
349         io.in.oplock_level = 0;
350         io.in.desired_access = SEC_RIGHTS_FILE_ALL;
351         io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
352         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
353         io.in.share_access = 
354                 NTCREATEX_SHARE_ACCESS_DELETE|
355                 NTCREATEX_SHARE_ACCESS_READ|
356                 NTCREATEX_SHARE_ACCESS_WRITE;
357         io.in.create_options = 0;
358         io.in.fname = fname;
359
360         status = smb2_create(tree, tree, &io);
361         NT_STATUS_NOT_OK_RETURN(status);
362
363         *handle = io.out.file.handle;
364
365         return NT_STATUS_OK;
366 }
367
368 /*
369   create and return a handle to a test directory
370 */
371 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname, 
372                               struct smb2_handle *handle)
373 {
374         struct smb2_create io;
375         NTSTATUS status;
376
377         ZERO_STRUCT(io);
378         io.in.oplock_level = 0;
379         io.in.desired_access = SEC_RIGHTS_DIR_ALL;
380         io.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
381         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
382         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
383         io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
384         io.in.fname = fname;
385
386         status = smb2_create(tree, tree, &io);
387         NT_STATUS_NOT_OK_RETURN(status);
388
389         *handle = io.out.file.handle;
390
391         return NT_STATUS_OK;
392 }
393
394
395 /*
396   create a complex file using SMB2, to make it easier to
397   find fields in SMB2 getinfo levels
398 */
399 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
400 {
401         struct smb2_handle handle;
402         NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
403         NT_STATUS_NOT_OK_RETURN(status);
404         return smb2_util_close(tree, handle);
405 }
406
407
408 /*
409   create a complex dir using SMB2, to make it easier to
410   find fields in SMB2 getinfo levels
411 */
412 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
413 {
414         struct smb2_handle handle;
415         NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
416         NT_STATUS_NOT_OK_RETURN(status);
417         return smb2_util_close(tree, handle);
418 }
419
420
421 /*
422   return a handle to the root of the share
423 */
424 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
425 {
426         struct smb2_create io;
427         NTSTATUS status;
428
429         ZERO_STRUCT(io);
430         io.in.oplock_level = 0;
431         io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
432         io.in.file_attributes   = 0;
433         io.in.create_disposition = NTCREATEX_DISP_OPEN;
434         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
435         io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
436         io.in.fname = NULL;
437
438         status = smb2_create(tree, tree, &io);
439         NT_STATUS_NOT_OK_RETURN(status);
440
441         *handle = io.out.file.handle;
442
443         return NT_STATUS_OK;
444 }
445
446 /* Comparable to torture_setup_dir, but for SMB2. */
447 bool smb2_util_setup_dir(struct torture_context *tctx, struct smb2_tree *tree,
448     const char *dname)
449 {
450         NTSTATUS status;
451
452         /* XXX: smb_raw_exit equivalent?
453         smb_raw_exit(cli->session); */
454         if (smb2_deltree(tree, dname) == -1) {
455                 torture_result(tctx, TORTURE_ERROR, "Unable to deltree when setting up %s.\n", dname);
456                 return false;
457         }
458
459         status = smb2_util_mkdir(tree, dname);
460         if (NT_STATUS_IS_ERR(status)) {
461                 torture_result(tctx, TORTURE_ERROR, "Unable to mkdir when setting up %s - %s\n", dname,
462                     nt_errstr(status));
463                 return false;
464         }
465
466         return true;
467 }
468
469 #define CHECK_STATUS(status, correct) do { \
470         if (!NT_STATUS_EQUAL(status, correct)) { \
471                 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
472                        __location__, nt_errstr(status), nt_errstr(correct)); \
473                 ret = false; \
474                 goto done; \
475         }} while (0)
476
477 /*
478  * Helper function to verify a security descriptor, by querying
479  * and comparing against the passed in sd.
480  */
481 bool smb2_util_verify_sd(TALLOC_CTX *tctx, struct smb2_tree *tree,
482     struct smb2_handle handle, struct security_descriptor *sd)
483 {
484         NTSTATUS status;
485         bool ret = true;
486         union smb_fileinfo q = {};
487
488         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
489         q.query_secdesc.in.file.handle = handle;
490         q.query_secdesc.in.secinfo_flags =
491             SECINFO_OWNER |
492             SECINFO_GROUP |
493             SECINFO_DACL;
494         status = smb2_getinfo_file(tree, tctx, &q);
495         CHECK_STATUS(status, NT_STATUS_OK);
496
497         if (!security_acl_equal(
498             q.query_secdesc.out.sd->dacl, sd->dacl)) {
499                 torture_warning(tctx, "%s: security descriptors don't match!\n",
500                     __location__);
501                 torture_warning(tctx, "got:\n");
502                 NDR_PRINT_DEBUG(security_descriptor,
503                     q.query_secdesc.out.sd);
504                 torture_warning(tctx, "expected:\n");
505                 NDR_PRINT_DEBUG(security_descriptor, sd);
506                 ret = false;
507         }
508
509  done:
510         return ret;
511 }
512
513 /*
514  * Helper function to verify attributes, by querying
515  * and comparing against the passed in attrib.
516  */
517 bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
518     struct smb2_handle handle, uint32_t attrib)
519 {
520         NTSTATUS status;
521         bool ret = true;
522         union smb_fileinfo q = {};
523
524         q.standard.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
525         q.standard.in.file.handle = handle;
526         status = smb2_getinfo_file(tree, tctx, &q);
527         CHECK_STATUS(status, NT_STATUS_OK);
528
529         q.all_info2.out.attrib &= ~FILE_ATTRIBUTE_ARCHIVE;
530
531         if (q.all_info2.out.attrib != attrib) {
532                 torture_warning(tctx, "%s: attributes don't match! "
533                     "got %x, expected %x\n", __location__,
534                     (uint32_t)q.standard.out.attrib,
535                     (uint32_t)attrib);
536                 ret = false;
537         }
538
539  done:
540         return ret;
541 }
542
543
544 uint32_t smb2_util_lease_state(const char *ls)
545 {
546         uint32_t val = 0;
547         int i;
548
549         for (i = 0; i < strlen(ls); i++) {
550                 switch (ls[i]) {
551                 case 'R':
552                         val |= SMB2_LEASE_READ;
553                         break;
554                 case 'H':
555                         val |= SMB2_LEASE_HANDLE;
556                         break;
557                 case 'W':
558                         val |= SMB2_LEASE_WRITE;
559                         break;
560                 }
561         }
562
563         return val;
564 }
565
566
567 uint32_t smb2_util_share_access(const char *sharemode)
568 {
569         uint32_t val = NTCREATEX_SHARE_ACCESS_NONE; /* 0 */
570         int i;
571
572         for (i = 0; i < strlen(sharemode); i++) {
573                 switch(sharemode[i]) {
574                 case 'R':
575                         val |= NTCREATEX_SHARE_ACCESS_READ;
576                         break;
577                 case 'W':
578                         val |= NTCREATEX_SHARE_ACCESS_WRITE;
579                         break;
580                 case 'D':
581                         val |= NTCREATEX_SHARE_ACCESS_DELETE;
582                         break;
583                 }
584         }
585
586         return val;
587 }
588
589 uint8_t smb2_util_oplock_level(const char *op)
590 {
591         uint8_t val = SMB2_OPLOCK_LEVEL_NONE;
592         int i;
593
594         for (i = 0; i < strlen(op); i++) {
595                 switch (op[i]) {
596                 case 's':
597                         return SMB2_OPLOCK_LEVEL_II;
598                 case 'x':
599                         return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
600                 case 'b':
601                         return SMB2_OPLOCK_LEVEL_BATCH;
602                 default:
603                         continue;
604                 }
605         }
606
607         return val;
608 }
609
610 /**
611  * Helper functions to fill a smb2_create struct for several
612  * open scenarios.
613  */
614 void smb2_generic_create_share(struct smb2_create *io, struct smb2_lease *ls,
615                                bool dir, const char *name, uint32_t disposition,
616                                uint32_t share_access,
617                                uint8_t oplock, uint64_t leasekey,
618                                uint32_t leasestate)
619 {
620         ZERO_STRUCT(*io);
621         io->in.security_flags           = 0x00;
622         io->in.oplock_level             = oplock;
623         io->in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
624         io->in.create_flags             = 0x00000000;
625         io->in.reserved                 = 0x00000000;
626         io->in.desired_access           = SEC_RIGHTS_FILE_ALL;
627         io->in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
628         io->in.share_access             = share_access;
629         io->in.create_disposition       = disposition;
630         io->in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
631                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
632                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
633                                           0x00200000;
634         io->in.fname                    = name;
635
636         if (dir) {
637                 io->in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
638                 io->in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
639                 io->in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
640                 io->in.create_disposition = NTCREATEX_DISP_CREATE;
641         }
642
643         if (ls) {
644                 ZERO_STRUCT(*ls);
645                 ls->lease_key.data[0] = leasekey;
646                 ls->lease_key.data[1] = ~leasekey;
647                 ls->lease_state = leasestate;
648                 io->in.lease_request = ls;
649         }
650 }
651
652 void smb2_generic_create(struct smb2_create *io, struct smb2_lease *ls,
653                          bool dir, const char *name, uint32_t disposition,
654                          uint8_t oplock, uint64_t leasekey,
655                          uint32_t leasestate)
656 {
657         smb2_generic_create_share(io, ls, dir, name, disposition,
658                                   smb2_util_share_access("RWD"),
659                                   oplock,
660                                   leasekey, leasestate);
661 }
662
663 void smb2_lease_create_share(struct smb2_create *io, struct smb2_lease *ls,
664                              bool dir, const char *name, uint32_t share_access,
665                              uint64_t leasekey, uint32_t leasestate)
666 {
667         smb2_generic_create_share(io, ls, dir, name, NTCREATEX_DISP_OPEN_IF,
668                                   share_access, SMB2_OPLOCK_LEVEL_LEASE,
669                                   leasekey, leasestate);
670 }
671
672 void smb2_lease_create(struct smb2_create *io, struct smb2_lease *ls,
673                        bool dir, const char *name, uint64_t leasekey,
674                        uint32_t leasestate)
675 {
676         smb2_lease_create_share(io, ls, dir, name,
677                                 smb2_util_share_access("RWD"),
678                                 leasekey, leasestate);
679 }
680
681 void smb2_oplock_create_share(struct smb2_create *io, const char *name,
682                               uint32_t share_access, uint8_t oplock)
683 {
684         smb2_generic_create_share(io, NULL, false, name, NTCREATEX_DISP_OPEN_IF,
685                                   share_access, oplock, 0, 0);
686 }
687 void smb2_oplock_create(struct smb2_create *io, const char *name, uint8_t oplock)
688 {
689         smb2_oplock_create_share(io, name, smb2_util_share_access("RWD"),
690                                  oplock);
691 }
692