s4:torture:smb2: add smb2_oplock_create_share() that takes sharemode as parameter
[bbaumbach/samba-autobuild/.git] / source4 / torture / smb2 / util.c
index 1dc8fca2b0f93fcce8415001ed4888480392f2e3..094161e6eb9ab0e8281d46c71ec7dc93da8f7fbd 100644 (file)
@@ -30,6 +30,7 @@
 #include "libcli/resolve/resolve.h"
 
 #include "torture/torture.h"
+#include "torture/smb2/proto.h"
 
 
 /*
@@ -270,17 +271,20 @@ bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tr
        struct cli_credentials *credentials = cmdline_credentials;
        struct smbcli_options options;
 
-       lp_smbcli_options(tctx->lp_ctx, &options);
-
-       status = smb2_connect(tctx, host, 
-                                                 lp_smb_ports(tctx->lp_ctx),
-                                                 share, 
-                             lp_resolve_context(tctx->lp_ctx),
-                             credentials, tree, 
-                             tctx->ev, &options,
-                                 lp_socket_options(tctx->lp_ctx),
-                                 lp_gensec_settings(tctx, tctx->lp_ctx)
-                                 );
+       lpcfg_smbcli_options(tctx->lp_ctx, &options);
+
+       status = smb2_connect(tctx,
+                             host,
+                             lpcfg_smb_ports(tctx->lp_ctx),
+                             share,
+                             lpcfg_resolve_context(tctx->lp_ctx),
+                             credentials,
+                             tree,
+                             tctx->ev,
+                             &options,
+                             lpcfg_socket_options(tctx->lp_ctx),
+                             lpcfg_gensec_settings(tctx, tctx->lp_ctx)
+                             );
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
                       host, share, nt_errstr(status));
@@ -297,7 +301,6 @@ NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
                               struct smb2_handle *handle)
 {
        struct smb2_create io;
-       struct smb2_read r;
        NTSTATUS status;
 
        ZERO_STRUCT(io);
@@ -317,13 +320,6 @@ NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
 
        *handle = io.out.file.handle;
 
-       ZERO_STRUCT(r);
-       r.in.file.handle = *handle;
-       r.in.length      = 5;
-       r.in.offset      = 0;
-
-       smb2_read(tree, tree, &r);
-
        return NT_STATUS_OK;
 }
 
@@ -503,3 +499,152 @@ bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
 }
 
 
+uint32_t smb2_util_lease_state(const char *ls)
+{
+       uint32_t val = 0;
+       int i;
+
+       for (i = 0; i < strlen(ls); i++) {
+               switch (ls[i]) {
+               case 'R':
+                       val |= SMB2_LEASE_READ;
+                       break;
+               case 'H':
+                       val |= SMB2_LEASE_HANDLE;
+                       break;
+               case 'W':
+                       val |= SMB2_LEASE_WRITE;
+                       break;
+               }
+       }
+
+       return val;
+}
+
+
+uint32_t smb2_util_share_access(const char *sharemode)
+{
+       uint32_t val = NTCREATEX_SHARE_ACCESS_NONE; /* 0 */
+       int i;
+
+       for (i = 0; i < strlen(sharemode); i++) {
+               switch(sharemode[i]) {
+               case 'R':
+                       val |= NTCREATEX_SHARE_ACCESS_READ;
+                       break;
+               case 'W':
+                       val |= NTCREATEX_SHARE_ACCESS_WRITE;
+                       break;
+               case 'D':
+                       val |= NTCREATEX_SHARE_ACCESS_DELETE;
+                       break;
+               }
+       }
+
+       return val;
+}
+
+uint8_t smb2_util_oplock_level(const char *op)
+{
+       uint8_t val = SMB2_OPLOCK_LEVEL_NONE;
+       int i;
+
+       for (i = 0; i < strlen(op); i++) {
+               switch (op[i]) {
+               case 's':
+                       return SMB2_OPLOCK_LEVEL_II;
+               case 'x':
+                       return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+               case 'b':
+                       return SMB2_OPLOCK_LEVEL_BATCH;
+               default:
+                       continue;
+               }
+       }
+
+       return val;
+}
+
+/**
+ * Helper functions to fill a smb2_create struct for several
+ * open scenarios.
+ */
+void smb2_generic_create_share(struct smb2_create *io, struct smb2_lease *ls,
+                              bool dir, const char *name, uint32_t disposition,
+                              uint32_t share_access,
+                              uint8_t oplock, uint64_t leasekey,
+                              uint32_t leasestate)
+{
+       ZERO_STRUCT(*io);
+       io->in.security_flags           = 0x00;
+       io->in.oplock_level             = oplock;
+       io->in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
+       io->in.create_flags             = 0x00000000;
+       io->in.reserved                 = 0x00000000;
+       io->in.desired_access           = SEC_RIGHTS_FILE_ALL;
+       io->in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
+       io->in.share_access             = share_access;
+       io->in.create_disposition       = disposition;
+       io->in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
+                                         NTCREATEX_OPTIONS_ASYNC_ALERT |
+                                         NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
+                                         0x00200000;
+       io->in.fname                    = name;
+
+       if (dir) {
+               io->in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
+               io->in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
+               io->in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
+               io->in.create_disposition = NTCREATEX_DISP_CREATE;
+       }
+
+       if (ls) {
+               ZERO_STRUCT(*ls);
+               ls->lease_key.data[0] = leasekey;
+               ls->lease_key.data[1] = ~leasekey;
+               ls->lease_state = leasestate;
+               io->in.lease_request = ls;
+       }
+}
+
+void smb2_generic_create(struct smb2_create *io, struct smb2_lease *ls,
+                        bool dir, const char *name, uint32_t disposition,
+                        uint8_t oplock, uint64_t leasekey,
+                        uint32_t leasestate)
+{
+       smb2_generic_create_share(io, ls, dir, name, disposition,
+                                 smb2_util_share_access("RWD"),
+                                 oplock,
+                                 leasekey, leasestate);
+}
+
+void smb2_lease_create_share(struct smb2_create *io, struct smb2_lease *ls,
+                            bool dir, const char *name, uint32_t share_access,
+                            uint64_t leasekey, uint32_t leasestate)
+{
+       smb2_generic_create_share(io, ls, dir, name, NTCREATEX_DISP_OPEN_IF,
+                                 share_access, SMB2_OPLOCK_LEVEL_LEASE,
+                                 leasekey, leasestate);
+}
+
+void smb2_lease_create(struct smb2_create *io, struct smb2_lease *ls,
+                      bool dir, const char *name, uint64_t leasekey,
+                      uint32_t leasestate)
+{
+       smb2_lease_create_share(io, ls, dir, name,
+                               smb2_util_share_access("RWD"),
+                               leasekey, leasestate);
+}
+
+void smb2_oplock_create_share(struct smb2_create *io, const char *name,
+                             uint32_t share_access, uint8_t oplock)
+{
+       smb2_generic_create_share(io, NULL, false, name, NTCREATEX_DISP_OPEN_IF,
+                                 share_access, oplock, 0, 0);
+}
+void smb2_oplock_create(struct smb2_create *io, const char *name, uint8_t oplock)
+{
+       smb2_oplock_create_share(io, name, smb2_util_share_access("RWD"),
+                                oplock);
+}
+