]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbd: Simplify unix_perms_from_wire()
authorVolker Lendecke <vl@samba.org>
Fri, 20 Sep 2024 19:49:23 +0000 (21:49 +0200)
committerRalph Boehme <slow@samba.org>
Thu, 26 Sep 2024 15:22:46 +0000 (15:22 +0000)
Remove enum perm_type: Only the _NEW_ defines were actually used, and
this made the logic harder for me to understand than necessary.

On the other hand, it forced you to think about what this is. Now you
could theoretically miss applying masks. Still, I like it better with
this patch.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/smbd/open.c
source3/smbd/proto.h
source3/smbd/smb1_trans2.c
source3/smbd/smb2_trans2.c

index 6b6c8b8abdc2f4f936e164fa3cd4295d6da46149..4f69a7749d8c870ebb0c650f0eff3cc52e379e35 100644 (file)
@@ -7031,9 +7031,6 @@ NTSTATUS create_file_default(connection_struct *conn,
                uint32_t wire_mode_bits = 0;
                mode_t mode_bits = 0;
                SMB_STRUCT_STAT sbuf = { 0 };
-               enum perm_type ptype =
-                       (create_options & FILE_DIRECTORY_FILE) ?
-                       PERM_NEW_DIR : PERM_NEW_FILE;
 
                if (posx->data.length != 4) {
                        status = NT_STATUS_INVALID_PARAMETER;
@@ -7041,11 +7038,18 @@ NTSTATUS create_file_default(connection_struct *conn,
                }
 
                wire_mode_bits = IVAL(posx->data.data, 0);
-               status = unix_perms_from_wire(
-                       conn, &sbuf, wire_mode_bits, ptype, &mode_bits);
+               status = unix_perms_from_wire(conn,
+                                             &sbuf,
+                                             wire_mode_bits,
+                                             &mode_bits);
                if (!NT_STATUS_IS_OK(status)) {
                        goto fail;
                }
+               if (create_options & FILE_DIRECTORY_FILE) {
+                       mode_bits = apply_conf_dir_mask(conn, mode_bits);
+               } else {
+                       mode_bits = apply_conf_file_mask(conn, mode_bits);
+               }
                /*
                 * Remove type info from mode, leaving only the
                 * permissions and setuid/gid bits.
index 9265962a2d2ced38af2466783fc23a2f63d5a48c..0661afea1c28bde1f1cf0a69c67638a8ca52da3b 100644 (file)
@@ -1088,17 +1088,9 @@ bool map_info2_flags_to_sbuf(const SMB_STRUCT_STAT *psbuf,
                             const uint32_t smb_fmask,
                             int *stat_fflags);
 
-enum perm_type {
-       PERM_NEW_FILE,
-       PERM_NEW_DIR,
-       PERM_EXISTING_FILE,
-       PERM_EXISTING_DIR
-};
-
 NTSTATUS unix_perms_from_wire(connection_struct *conn,
                              const SMB_STRUCT_STAT *psbuf,
                              uint32_t perms,
-                             enum perm_type ptype,
                              mode_t *ret_perms);
 struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata,
                             size_t data_size);
index e41be9f4d71839a56da2aa5e9408e44fcd8db80b..666a8ebb2a62a09921f1c4d46c5407103125ff29 100644 (file)
@@ -3170,11 +3170,14 @@ static NTSTATUS smb_posix_mkdir(connection_struct *conn,
        raw_unixmode = IVAL(pdata,8);
        /* Next 4 bytes are not yet defined. */
 
-       status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
-                                     PERM_NEW_DIR, &unixmode);
+       status = unix_perms_from_wire(conn,
+                                     &smb_fname->st,
+                                     raw_unixmode,
+                                     &unixmode);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
+       unixmode = apply_conf_dir_mask(conn, unixmode);
 
        status = make_smb2_posix_create_ctx(talloc_tos(), &posx, unixmode);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3389,14 +3392,17 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
        raw_unixmode = IVAL(pdata,8);
        /* Next 4 bytes are not yet defined. */
 
-       status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
-                                     (VALID_STAT(smb_fname->st) ?
-                                         PERM_EXISTING_FILE : PERM_NEW_FILE),
+       status = unix_perms_from_wire(conn,
+                                     &smb_fname->st,
+                                     raw_unixmode,
                                      &unixmode);
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
+       if (!VALID_STAT(smb_fname->st)) {
+               unixmode = apply_conf_dir_mask(conn, unixmode);
+       }
 
        status = make_smb2_posix_create_ctx(talloc_tos(), &posx, unixmode);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3843,11 +3849,14 @@ static NTSTATUS smb_unix_mknod(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
-                                     PERM_NEW_FILE, &unixmode);
+       status = unix_perms_from_wire(conn,
+                                     &smb_fname->st,
+                                     raw_unixmode,
+                                     &unixmode);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
+       unixmode = apply_conf_file_mask(conn, unixmode);
 
 #if defined(HAVE_MAKEDEV)
        dev = makedev(dev_major, dev_minor);
@@ -3951,7 +3960,6 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        uid_t set_owner = (uid_t)SMB_UID_NO_CHANGE;
        gid_t set_grp = (uid_t)SMB_GID_NO_CHANGE;
        NTSTATUS status = NT_STATUS_OK;
-       enum perm_type ptype;
        files_struct *all_fsps = NULL;
        bool modify_mtime = true;
        struct file_id id;
@@ -3979,21 +3987,16 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        set_grp = (gid_t)IVAL(pdata,48);
        raw_unixmode = IVAL(pdata,84);
 
-       if (VALID_STAT(smb_fname->st)) {
-               if (S_ISDIR(smb_fname->st.st_ex_mode)) {
-                       ptype = PERM_EXISTING_DIR;
-               } else {
-                       ptype = PERM_EXISTING_FILE;
-               }
-       } else {
-               ptype = PERM_NEW_FILE;
-       }
-
-       status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
-                                     ptype, &unixmode);
+       status = unix_perms_from_wire(conn,
+                                     &smb_fname->st,
+                                     raw_unixmode,
+                                     &unixmode);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
+       if (!VALID_STAT(smb_fname->st)) {
+               unixmode = apply_conf_file_mask(conn, unixmode);
+       }
 
        DBG_DEBUG("SMB_SET_FILE_UNIX_BASIC: name = "
                  "%s size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
index ad0b85b77c59f2261eb13f015ccd705f1a5f2764..fe19c391e0eefe94cb1ec3fd89f986cde6e0499d 100644 (file)
@@ -894,7 +894,6 @@ static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *
 NTSTATUS unix_perms_from_wire(connection_struct *conn,
                              const SMB_STRUCT_STAT *psbuf,
                              uint32_t perms,
-                             enum perm_type ptype,
                              mode_t *ret_perms)
 {
        mode_t ret = 0;
@@ -909,20 +908,6 @@ NTSTATUS unix_perms_from_wire(connection_struct *conn,
 
        ret = wire_perms_to_unix(perms);
 
-       if (ptype == PERM_NEW_FILE) {
-               /*
-                * "create mask"/"force create mode" are
-                * only applied to new files, not existing ones.
-                */
-               ret = apply_conf_file_mask(conn, ret);
-       } else if (ptype == PERM_NEW_DIR) {
-               /*
-                * "directory mask"/"force directory mode" are
-                * only applied to new directories, not existing ones.
-                */
-               ret = apply_conf_dir_mask(conn, ret);
-       }
-
        *ret_perms = ret;
        return NT_STATUS_OK;
 }