libsmb: Introduce type-safe struct cli_smb2_create_flags
authorVolker Lendecke <vl@samba.org>
Wed, 12 Apr 2023 13:31:03 +0000 (15:31 +0200)
committerVolker Lendecke <vl@samba.org>
Tue, 18 Apr 2023 14:58:36 +0000 (14:58 +0000)
This makes it clearer what to pass into the create_flags argument to
cli_smb2_create_fnum(). There was already confusion in
source3/torture/test_smb2.c: It passed in
SMB2_OPLOCK_LEVEL_NONE (which was okay because it #defines to 0), but
it should have been a straight 0, for example
SMB2_OPLOCK_LEVEL_EXCLUSIVE would have been wrong.

This way adding other flags (.nofollow comes to mind) will be much
easier to handle.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
examples/fuse/clifuse.c
source3/libsmb/cli_smb2_fnum.c
source3/libsmb/cli_smb2_fnum.h
source3/libsmb/clifile.c
source3/libsmb/pylibsmb.c
source3/torture/test_smb2.c

index 75cb72f7f870dbfd344f1e67a171ade6c0fca21e..94ec3911ef65b22563ebd322563fee77cceec433 100644 (file)
@@ -154,7 +154,7 @@ static void cli_ll_create(fuse_req_t freq, fuse_ino_t parent, const char *name,
                state,
                mstate->ev,
                mstate->cli, state->path,
-               0,
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                FILE_GENERIC_READ|FILE_GENERIC_WRITE,
                FILE_ATTRIBUTE_NORMAL,
@@ -853,7 +853,7 @@ static void cli_ll_open(fuse_req_t freq, fuse_ino_t ino,
                mstate->ev,
                mstate->cli,
                istate->path,
-               0,
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                acc,
                FILE_ATTRIBUTE_NORMAL,
index 5852f9177fd3d2e00abf7b30ae35d6463f60cfb7..2339ca4170200da9c3250b89c5736bc86ae9ea12 100644 (file)
@@ -142,11 +142,11 @@ static NTSTATUS delete_smb2_handle_mapping(struct cli_state *cli,
  Oplock mapping code.
 ***************************************************************/
 
-static uint8_t flags_to_smb2_oplock(uint32_t create_flags)
+static uint8_t flags_to_smb2_oplock(struct cli_smb2_create_flags create_flags)
 {
-       if (create_flags & REQUEST_BATCH_OPLOCK) {
+       if (create_flags.batch_oplock) {
                return SMB2_OPLOCK_LEVEL_BATCH;
-       } else if (create_flags & REQUEST_OPLOCK) {
+       } else if (create_flags.exclusive_oplock) {
                return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
        }
 
@@ -212,7 +212,7 @@ struct tevent_req *cli_smb2_create_fnum_send(
        struct tevent_context *ev,
        struct cli_state *cli,
        const char *fname_in,
-       uint32_t create_flags,
+       struct cli_smb2_create_flags create_flags,
        uint32_t impersonation_level,
        uint32_t desired_access,
        uint32_t file_attributes,
@@ -392,7 +392,7 @@ NTSTATUS cli_smb2_create_fnum_recv(
 NTSTATUS cli_smb2_create_fnum(
        struct cli_state *cli,
        const char *fname,
-       uint32_t create_flags,
+       struct cli_smb2_create_flags create_flags,
        uint32_t impersonation_level,
        uint32_t desired_access,
        uint32_t file_attributes,
@@ -810,7 +810,7 @@ struct tevent_req *cli_smb2_mkdir_send(
                ev,                                /* ev */
                cli,                               /* cli */
                dname,                             /* fname */
-               0,                                 /* create_flags */
+               (struct cli_smb2_create_flags){0}, /* create_flags */
                SMB2_IMPERSONATION_IMPERSONATION,  /* impersonation_level */
                FILE_READ_ATTRIBUTES,              /* desired_access */
                FILE_ATTRIBUTE_DIRECTORY,          /* file_attributes */
@@ -898,7 +898,7 @@ struct tevent_req *cli_smb2_rmdir_send(
                state->ev,
                state->cli,
                state->dname,
-               0,                      /* create_flags */
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                DELETE_ACCESS,          /* desired_access */
                FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -937,7 +937,7 @@ static void cli_smb2_rmdir_opened1(struct tevent_req *subreq)
                        state->ev,
                        state->cli,
                        state->dname,
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        DELETE_ACCESS,          /* desired_access */
                        FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -1068,7 +1068,7 @@ struct tevent_req *cli_smb2_unlink_send(
                state->ev,      /* tevent_context */
                state->cli,     /* cli_struct */
                state->fname,   /* filename */
-               0,                      /* create_flags */
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                DELETE_ACCESS,          /* desired_access */
                FILE_ATTRIBUTE_NORMAL, /* file attributes */
@@ -1111,7 +1111,7 @@ static void cli_smb2_unlink_opened1(struct tevent_req *subreq)
                        state->ev,      /* tevent_context */
                        state->cli,     /* cli_struct */
                        state->fname,   /* filename */
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        DELETE_ACCESS,          /* desired_access */
                        FILE_ATTRIBUTE_NORMAL, /* file attributes */
@@ -1473,7 +1473,7 @@ struct tevent_req *cli_smb2_list_send(
                ev,                                     /* ev */
                cli,                                    /* cli */
                parent,                                 /* fname */
-               0,                                      /* create_flags */
+               (struct cli_smb2_create_flags){0},      /* create_flags */
                SMB2_IMPERSONATION_IMPERSONATION,       /* impersonation_level */
                SEC_DIR_LIST|SEC_DIR_READ_ATTRIBUTE,    /* desired_access */
                FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
@@ -1722,7 +1722,7 @@ NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
 
        status = cli_smb2_create_fnum(cli,
                        name,
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        FILE_READ_ATTRIBUTES,   /* desired_access */
                        FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -1739,7 +1739,7 @@ NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
                /* Maybe a file ? */
                status = cli_smb2_create_fnum(cli,
                        name,
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        FILE_READ_ATTRIBUTES,           /* desired_access */
                        0, /* file attributes */
@@ -1757,7 +1757,7 @@ NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
                /* Maybe a reparse point ? */
                status = cli_smb2_create_fnum(cli,
                        name,
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        FILE_READ_ATTRIBUTES,           /* desired_access */
                        0, /* file attributes */
@@ -1996,7 +1996,7 @@ static struct tevent_req *get_fnum_from_path_send(
                ev,             /* ev */
                cli,            /* cli */
                state->name,    /* fname */
-               0,              /* create_flags */
+               (struct cli_smb2_create_flags){0}, /* create_flags */
                SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level */
                desired_access, /* desired_access */
                0,              /* file_attributes */
@@ -2037,7 +2037,7 @@ static void get_fnum_from_path_opened_file(struct tevent_req *subreq)
                        state->ev,      /* ev */
                        state->cli,     /* cli */
                        state->name,    /* fname */
-                       0,              /* create_flags */
+                       (struct cli_smb2_create_flags){0}, /* create_flags */
                        SMB2_IMPERSONATION_IMPERSONATION, /* impersonation */
                        state->desired_access, /* desired_access */
                        0,              /* file_attributes */
@@ -2061,7 +2061,7 @@ static void get_fnum_from_path_opened_file(struct tevent_req *subreq)
                        state->ev,      /* ev */
                        state->cli,     /* cli */
                        state->name,    /* fname */
-                       0,              /* create_flags */
+                       (struct cli_smb2_create_flags){0}, /* create_flags */
                        SMB2_IMPERSONATION_IMPERSONATION, /* impersonation */
                        state->desired_access, /* desired_access */
                        0,              /* file_attributes */
@@ -2630,7 +2630,7 @@ NTSTATUS cli_smb2_dskattr(struct cli_state *cli, const char *path,
        /* First open the top level directory. */
        status = cli_smb2_create_fnum(cli,
                        path,
-                       0,                      /* create_flags */
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        FILE_READ_ATTRIBUTES,   /* desired_access */
                        FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -2727,7 +2727,8 @@ NTSTATUS cli_smb2_get_fs_full_size_info(struct cli_state *cli,
 
        /* First open the top level directory. */
        status =
-           cli_smb2_create_fnum(cli, "", 0,               /* create_flags */
+           cli_smb2_create_fnum(cli, "",
+                                (struct cli_smb2_create_flags){0},
                                 SMB2_IMPERSONATION_IMPERSONATION,
                                 FILE_READ_ATTRIBUTES,     /* desired_access */
                                 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -2808,7 +2809,8 @@ NTSTATUS cli_smb2_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
 
        /* First open the top level directory. */
        status =
-           cli_smb2_create_fnum(cli, "", 0,               /* create_flags */
+           cli_smb2_create_fnum(cli, "",
+                                (struct cli_smb2_create_flags){0},
                                 SMB2_IMPERSONATION_IMPERSONATION,
                                 FILE_READ_ATTRIBUTES,     /* desired_access */
                                 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -2888,7 +2890,8 @@ NTSTATUS cli_smb2_get_fs_volume_info(struct cli_state *cli,
 
        /* First open the top level directory. */
        status =
-           cli_smb2_create_fnum(cli, "", 0,               /* create_flags */
+           cli_smb2_create_fnum(cli, "",
+                                (struct cli_smb2_create_flags){0},
                                 SMB2_IMPERSONATION_IMPERSONATION,
                                 FILE_READ_ATTRIBUTES,     /* desired_access */
                                 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
@@ -3024,7 +3027,7 @@ struct tevent_req *cli_smb2_query_mxac_send(TALLOC_CTX *mem_ctx,
                state->ev,
                state->cli,
                state->fname,
-               0,                      /* create_flags */
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                FILE_READ_ATTRIBUTES,
                0,                      /* file attributes */
index bb1a61ecfd5e433f3f8a3c946f4b233abfef0cf4..4be84db72b066f3f3020c410dae81843ba6a5a23 100644 (file)
@@ -26,12 +26,17 @@ struct cli_state;
 struct file_info;
 struct symlink_reparse_struct;
 
+struct cli_smb2_create_flags {
+       bool batch_oplock:1;
+       bool exclusive_oplock:1;
+};
+
 struct tevent_req *cli_smb2_create_fnum_send(
        TALLOC_CTX *mem_ctx,
        struct tevent_context *ev,
        struct cli_state *cli,
        const char *fname,
-       uint32_t create_flags,
+       struct cli_smb2_create_flags create_flags,
        uint32_t impersonation_level,
        uint32_t desired_access,
        uint32_t file_attributes,
@@ -49,7 +54,7 @@ NTSTATUS cli_smb2_create_fnum_recv(
 NTSTATUS cli_smb2_create_fnum(
        struct cli_state *cli,
        const char *fname,
-       uint32_t create_flags,
+       struct cli_smb2_create_flags create_flags,
        uint32_t impersonation_level,
        uint32_t desired_access,
        uint32_t file_attributes,
index 5c2e8208d75d5fc704897e0a2b7cc91df6ed9c53..ecc652c81e6ef0835d5b10ad9c8b76b422e79dbf 100644 (file)
@@ -1746,7 +1746,7 @@ static struct tevent_req *cli_smb2_hardlink_send(
                ev,
                cli,
                fname_src,
-               0,                      /* create_flags */
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                FILE_WRITE_ATTRIBUTES,
                0,                      /* file attributes */
@@ -2669,16 +2669,23 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
        }
 
        if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               struct cli_smb2_create_flags cflags = {0};
+
                if (cli->use_oplocks) {
                        create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
                }
 
+               cflags = (struct cli_smb2_create_flags) {
+                       .batch_oplock = (create_flags & REQUEST_BATCH_OPLOCK),
+                       .exclusive_oplock = (create_flags & REQUEST_OPLOCK),
+               };
+
                subreq = cli_smb2_create_fnum_send(
                        state,
                        ev,
                        cli,
                        fname,
-                       create_flags,
+                       cflags,
                        impersonation_level,
                        desired_access,
                        file_attributes,
index 7952a6ec4832507d13639e8ee6e8d28467254a40..0dd5e2583d45aae8f8f770a3dcc9e3e2faca2264 100644 (file)
@@ -1135,12 +1135,17 @@ static PyObject *py_cli_create_ex(
        }
 
        if (smbXcli_conn_protocol(self->cli->conn) >= PROTOCOL_SMB2_02) {
+               struct cli_smb2_create_flags cflags = {
+                       .batch_oplock = (CreateFlags & REQUEST_BATCH_OPLOCK),
+                       .exclusive_oplock = (CreateFlags & REQUEST_OPLOCK),
+               };
+
                req = cli_smb2_create_fnum_send(
                        NULL,
                        self->ev,
                        self->cli,
                        fname,
-                       CreateFlags,
+                       cflags,
                        ImpersonationLevel,
                        DesiredAccess,
                        FileAttributes,
index dc249643aa651ba8974cde8928fb2253325c3ba3..ca8cea23a6b6639b02ad2e341c76e6825dcc80aa 100644 (file)
@@ -2840,7 +2840,7 @@ bool run_smb2_sacl(int dummy)
 
        status = cli_smb2_create_fnum(cli,
                        fname,
-                       SMB2_OPLOCK_LEVEL_NONE,
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        SEC_FLAG_SYSTEM_SECURITY, /* desired access */
                        0, /* file_attributes, */
@@ -2878,7 +2878,7 @@ bool run_smb2_sacl(int dummy)
 
        status = cli_smb2_create_fnum(cli,
                        fname,
-                       SMB2_OPLOCK_LEVEL_NONE,
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        SEC_FLAG_SYSTEM_SECURITY|
                                FILE_WRITE_ATTRIBUTES, /* desired access */
@@ -2947,7 +2947,7 @@ bool run_smb2_sacl(int dummy)
 
        status = cli_smb2_create_fnum(cli,
                        fname,
-                       SMB2_OPLOCK_LEVEL_NONE,
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        SEC_FLAG_SYSTEM_SECURITY|
                                SEC_STD_WRITE_DAC, /* desired access */
@@ -3003,7 +3003,7 @@ bool run_smb2_sacl(int dummy)
 
        status = cli_smb2_create_fnum(cli,
                        fname,
-                       SMB2_OPLOCK_LEVEL_NONE,
+                       (struct cli_smb2_create_flags){0},
                        SMB2_IMPERSONATION_IMPERSONATION,
                        SEC_FLAG_SYSTEM_SECURITY|
                                FILE_READ_ATTRIBUTES, /* desired access */
@@ -3117,7 +3117,7 @@ bool run_smb2_quota1(int dummy)
        status = cli_smb2_create_fnum(
                cli,
                "\\",
-               SMB2_OPLOCK_LEVEL_NONE,
+               (struct cli_smb2_create_flags){0},
                SMB2_IMPERSONATION_IMPERSONATION,
                SEC_GENERIC_READ, /* desired access */
                0, /* file_attributes, */