]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbd: Change open_rootdir_pathref_fsp() to return a smb_filename
authorVolker Lendecke <vl@samba.org>
Wed, 10 Sep 2025 16:12:22 +0000 (18:12 +0200)
committerVolker Lendecke <vl@samba.org>
Mon, 17 Nov 2025 08:33:11 +0000 (08:33 +0000)
Simpler and safer to close in the caller, and
openat_pathref_fsp_rootdir() can use
openat_pathref_fsp_simple_openat(). De-duplicate some logic.

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

index 9c12af42d50e2da09674d51503160fe4602becf0..79328da74d9e962c9acc70e725c73e9cb7a4e3f8 100644 (file)
@@ -100,7 +100,7 @@ static int fake_acls_fstatat(struct vfs_handle_struct *handle,
        connection_struct *conn = handle->conn;
        int ret = -1;
        struct in_pathref_data *prd = NULL;
-       struct files_struct *root_fsp = NULL;
+       struct smb_filename *rootdir = NULL;
        struct files_struct *new_dirfsp = NULL;
        struct smb_filename *smb_fname = NULL;
        struct smb_filename *new_relname = NULL;
@@ -150,13 +150,15 @@ static int fake_acls_fstatat(struct vfs_handle_struct *handle,
                 * paths, make this relative to "/"
                 */
                base_name += 1;
-               status = open_rootdir_pathref_fsp(conn, &root_fsp);
+               status = openat_pathref_fsp_rootdir(talloc_tos(),
+                                                   conn,
+                                                   &rootdir);
                if (!NT_STATUS_IS_OK(status)) {
                        prd->calling_pathref_fsp = false;
                        errno = ENOENT;
                        return -1;
                }
-               dirfsp = root_fsp;
+               dirfsp = rootdir->fsp;
        }
 
        if (ISDOT(base_name)) {
@@ -212,11 +214,7 @@ static int fake_acls_fstatat(struct vfs_handle_struct *handle,
                                &sbuf->st_ex_uid,
                                &sbuf->st_ex_gid);
 
-       if (root_fsp != NULL) {
-               fd_close(root_fsp);
-               file_free(NULL, root_fsp);
-               root_fsp = NULL;
-       }
+       TALLOC_FREE(rootdir);
        fd_close(new_dirfsp);
        file_free(NULL, new_dirfsp);
        new_dirfsp = NULL;
index 178a8f96c5d921f8ead5c79f5b3cfc25213c1351..333661027745178d5d02311b94a0405bd8c685cd 100644 (file)
@@ -694,61 +694,21 @@ static NTSTATUS openat_pathref_fsp_simple_openat(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-NTSTATUS open_rootdir_pathref_fsp(connection_struct *conn,
-                                 struct files_struct **_fsp)
+NTSTATUS openat_pathref_fsp_rootdir(TALLOC_CTX *mem_ctx,
+                                   struct connection_struct *conn,
+                                   struct smb_filename **_root)
 {
-       struct smb_filename slash = { .base_name = discard_const_p(char, "/") };
-       struct vfs_open_how how = { .flags = O_RDONLY|O_DIRECTORY, };
-       struct files_struct *fsp = NULL;
+       struct smb_filename *root = NULL;
        NTSTATUS status;
-       int fd;
-       bool ok;
-
-       fsp = fsp_new(conn, conn);
-       if (fsp == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       GetTimeOfDay(&fsp->open_time);
-       ZERO_STRUCT(conn->sconn->fsp_fi_cache);
-       fsp->fsp_flags.is_pathref = true;
-
-       ok = fsp_set_smb_fname(fsp, &slash);
-       if (!ok) {
-               status = NT_STATUS_NO_MEMORY;
-               goto fail;
-       }
-
-       fd = SMB_VFS_OPENAT(conn,
-                           conn->cwd_fsp,
-                           fsp->fsp_name,
-                           fsp,
-                           &how);
-       if (fd == -1) {
-               status = map_nt_error_from_unix(errno);
-               goto fail;
-       }
-       fsp_set_fd(fsp, fd);
 
-       status = vfs_stat_fsp(fsp);
+       status = openat_pathref_fsp_simple_openat(
+               mem_ctx, conn->cwd_fsp, "/", 0, &root);
        if (!NT_STATUS_IS_OK(status)) {
-               DBG_DEBUG("vfs_stat_fsp(\"/\") failed: %s\n", nt_errstr(status));
-               goto close_fail;
-       }
-       fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
-       if (!fsp->fsp_flags.is_directory) {
-               DBG_DEBUG("\"/\" not a directory\n");
-               status = NT_STATUS_UNEXPECTED_IO_ERROR;
-               goto close_fail;
+               return status;
        }
-       fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
-       *_fsp = fsp;
-       return NT_STATUS_OK;
 
-close_fail:
-       fd_close(fsp);
-fail:
-       file_free(NULL, fsp);
-       return status;
+       *_root = root;
+       return NT_STATUS_OK;
 }
 
 /*
index 26d305a97e98271a25e090019db2930a9507f4b5..5294d0c9c9df4dd5da07630e442404115e2c6af6 100644 (file)
@@ -475,7 +475,7 @@ NTSTATUS fd_openat(const struct files_struct *dirfsp,
        struct files_struct *dirfsp_conv = NULL;
        struct smb_filename *smb_fname_conv = NULL;
        struct smb_filename *smb_fname_rel = NULL;
-       struct files_struct *root_fsp = NULL;
+       struct smb_filename *rootdir = NULL;
        const char *name_in = smb_fname->base_name;
        int fd;
 
@@ -532,11 +532,13 @@ NTSTATUS fd_openat(const struct files_struct *dirfsp,
                 * paths, make this relative to "/"
                 */
                name_in += 1;
-               status = open_rootdir_pathref_fsp(conn, &root_fsp);
+               status = openat_pathref_fsp_rootdir(talloc_tos(),
+                                                   conn,
+                                                   &rootdir);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
-               dirfsp = root_fsp;
+               dirfsp = rootdir->fsp;
        }
 
        if (ISDOT(name_in)) {
@@ -558,11 +560,7 @@ NTSTATUS fd_openat(const struct files_struct *dirfsp,
                &smb_fname_rel);
 
        dirfsp = NULL;
-       if (root_fsp != NULL) {
-               fd_close(root_fsp);
-               file_free(NULL, root_fsp);
-               root_fsp = NULL;
-       }
+       TALLOC_FREE(rootdir);
 
        if (!NT_STATUS_IS_OK(status)) {
                DBG_DEBUG("filename_convert_dirfsp_rel returned %s\n",
index 6a7a524cfc51c68b1a2c411d00be71f0b00f2a5e..34dcd964e9fee966d226c577f55631211e42e19c 100644 (file)
@@ -370,8 +370,9 @@ NTSTATUS open_internal_dirfsp(connection_struct *conn,
                              int open_flags,
                              struct files_struct **_fsp);
 
-NTSTATUS open_rootdir_pathref_fsp(connection_struct *conn,
-                                 struct files_struct **_fsp);
+NTSTATUS openat_pathref_fsp_rootdir(TALLOC_CTX *mem_ctx,
+                                   struct connection_struct *conn,
+                                   struct smb_filename **_root);
 NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
                            struct smb_filename *smb_fname);
 NTSTATUS open_stream_pathref_fsp(