smbd: use fsp_set_fd()
authorRalph Boehme <slow@samba.org>
Sat, 26 Sep 2020 19:46:51 +0000 (21:46 +0200)
committerRalph Boehme <slow@samba.org>
Wed, 16 Dec 2020 09:08:30 +0000 (09:08 +0000)
No change in behaviour.

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
19 files changed:
source3/modules/vfs_aio_fork.c
source3/modules/vfs_commit.c
source3/modules/vfs_dirsort.c
source3/modules/vfs_fruit.c
source3/modules/vfs_shadow_copy.c
source3/modules/vfs_shadow_copy2.c
source3/modules/vfs_streams_xattr.c
source3/modules/vfs_xattr_tdb.c
source3/printing/printspoolss.c
source3/smbd/conn.c
source3/smbd/dir.c
source3/smbd/fake_file.c
source3/smbd/files.c
source3/smbd/open.c
source3/smbd/pipes.c
source3/smbd/pysmbd.c
source3/smbd/vfs.c
source3/torture/cmd_vfs.c
source3/wscript_build

index 7c6f4b00fd04b020c68722cd95e3e50a93862b79..0b74e4e05c06d758619083b0590981773d01e5d5 100644 (file)
@@ -421,7 +421,7 @@ static struct files_struct *close_fsp_fd(struct files_struct *fsp,
 {
        if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
                close(fsp->fh->fd);
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
        }
        return NULL;
 }
index fae94329af3bebaf046332e9838cf6fd2ec9701f..4c6142e83dc3a993220702286d795394f6a0ad4d 100644 (file)
@@ -240,7 +240,7 @@ static int commit_openat(struct vfs_handle_struct *handle,
                 * but also practiced elsewhere -
                 * needed for calling the VFS.
                 */
-               fsp->fh->fd = fd;
+               fsp_set_fd(fsp, fd);
                if (SMB_VFS_FSTAT(fsp, &st) == -1) {
                        int saved_errno = errno;
                        SMB_VFS_CLOSE(fsp);
index f43a5f0b98aeabc4d33b777c3e3f6670939771ea..3b3d609563df9e04af9b7c904610e11a3e6070f6 100644 (file)
@@ -165,7 +165,7 @@ static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
                SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
                TALLOC_FREE(data);
                /* fd is now closed. */
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
                return NULL;
        }
 
index 8a843407596ce1e0a538410b35d727fc67cd1af3..31e2ccef3c934e153dbb2bc50f9258d1db630edb 100644 (file)
@@ -1490,10 +1490,10 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
                        goto exit;
                }
 
-               fsp->fh->fd = hostfd;
+               fsp_set_fd(fsp, hostfd);
 
                rc = ad_fset(handle, ad, fsp);
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
                if (rc != 0) {
                        rc = -1;
                        goto exit;
@@ -1514,7 +1514,7 @@ exit:
                         * fd_close_posix here, but we don't have a
                         * full fsp yet
                         */
-                       fsp->fh->fd = hostfd;
+                       fsp_set_fd(fsp, hostfd);
                        SMB_VFS_CLOSE(fsp);
                }
                hostfd = -1;
@@ -1670,7 +1670,7 @@ static int fruit_close_meta(vfs_handle_struct *handle,
 
        case FRUIT_META_NETATALK:
                ret = close(fsp->fh->fd);
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
                break;
 
        default:
@@ -1699,7 +1699,7 @@ static int fruit_close_rsrc(vfs_handle_struct *handle,
 
        case FRUIT_RSRC_XATTR:
                ret = close(fsp->fh->fd);
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
                break;
 
        default:
@@ -2459,10 +2459,10 @@ static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
                int fd;
 
                ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
+               fsp_set_fd(fsp, -1);
                if (ret != 0) {
                        DBG_ERR("Close [%s] failed: %s\n",
                                fsp_str_dbg(fsp), strerror(errno));
-                       fsp->fh->fd = -1;
                        return -1;
                }
 
@@ -2477,7 +2477,7 @@ static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
                                fsp_str_dbg(fsp), strerror(errno));
                        return -1;
                }
-               fsp->fh->fd = fd;
+               fsp_set_fd(fsp, fd);
                fio->fake_fd = false;
        }
 
index 31333dc22395bab6b83b913806bf2c0fb414781b..79f2097912748b0a01f7e3740b9d7252573fe4d5 100644 (file)
@@ -89,7 +89,7 @@ static DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp,
                DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
                SMB_VFS_NEXT_CLOSEDIR(handle,p);
                /* We have now closed the fd in fsp. */
-               fsp->fh->fd = -1;
+               fsp_set_fd(fsp, -1);
                return NULL;
        }
 
@@ -121,7 +121,7 @@ static DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp,
 
        SMB_VFS_NEXT_CLOSEDIR(handle,p);
        /* We have now closed the fd in fsp. */
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
        return((DIR *)dirp);
 }
 
index 665080060d233c3d168ccc02dcb02a7dab8938e2..b449e20ac0d39989037367ecc9ba87574eed6bac 100644 (file)
@@ -1952,6 +1952,7 @@ static int shadow_copy2_get_shadow_copy_data(
        bool get_snaplist = false;
        bool access_granted = false;
        int open_flags = O_RDONLY;
+       int fd;
        int ret = -1;
        NTSTATUS status;
 
@@ -2001,18 +2002,19 @@ static int shadow_copy2_get_shadow_copy_data(
        open_flags |= O_DIRECTORY;
 #endif
 
-       dirfsp->fh->fd = SMB_VFS_NEXT_OPENAT(handle,
-                                            fspcwd,
-                                            snapdir_smb_fname,
-                                            dirfsp,
-                                            open_flags,
-                                            0);
-       if (dirfsp->fh->fd == -1) {
+       fd = SMB_VFS_NEXT_OPENAT(handle,
+                                fspcwd,
+                                snapdir_smb_fname,
+                                dirfsp,
+                                open_flags,
+                                0);
+       if (fd == -1) {
                DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
                            " - %s\n", snapdir, strerror(errno));
                errno = ENOSYS;
                goto done;
        }
+       fsp_set_fd(dirfsp, fd);
 
        p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
        if (!p) {
@@ -2121,7 +2123,7 @@ done:
                         * VFS_CLOSEDIR implicitly
                         * closed the associated fd.
                         */
-                       dirfsp->fh->fd = -1;
+                       fsp_set_fd(dirfsp, -1);
                }
        }
        if (dirfsp != NULL) {
index 2f786327835595f61182e372b52977ce9822758d..f564efcb6e703a7bd23f094adc4233222cd6c71d 100644 (file)
@@ -515,7 +515,7 @@ static int streams_xattr_close(vfs_handle_struct *handle,
        }
 
        ret = close(fd);
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
 
        return ret;
 }
index c8cc247345d8c89a649c28f072bc2a9d62fd324d..67b1a120b3bb07aa6fcf5f724185d3dcdb95ee9c 100644 (file)
@@ -502,21 +502,23 @@ static int xattr_tdb_openat(struct vfs_handle_struct *handle,
        struct db_context *db = NULL;
        TALLOC_CTX *frame = NULL;
        SMB_STRUCT_STAT sbuf;
+       int fd;
        int ret;
 
-       fsp->fh->fd = SMB_VFS_NEXT_OPENAT(handle,
-                                         dirfsp,
-                                         smb_fname,
-                                         fsp,
-                                         flags,
-                                         mode);
+       fd = SMB_VFS_NEXT_OPENAT(handle,
+                                dirfsp,
+                                smb_fname,
+                                fsp,
+                                flags,
+                                mode);
 
-       if (fsp->fh->fd < 0) {
-               return fsp->fh->fd;
+       if (fd == -1) {
+               return -1;
        }
+       fsp_set_fd(fsp, fd);
 
        if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
-               return fsp->fh->fd;
+               return fd;
        }
 
        /*
@@ -546,7 +548,7 @@ static int xattr_tdb_openat(struct vfs_handle_struct *handle,
        xattr_tdb_remove_all_attrs(db, &fsp->file_id);
 
        TALLOC_FREE(frame);
-       return fsp->fh->fd;
+       return fd;
 }
 
 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
index f3376d8f31ee6ff21eb792f7d8bbbe5295f0294a..9c682e59edde5e70742db7042c10e231bcda5339 100644 (file)
@@ -25,6 +25,7 @@
 #include "rpc_server/rpc_ncacn_np.h"
 #include "smbd/globals.h"
 #include "../libcli/security/security.h"
+#include "smbd/fd_handle.h"
 
 struct print_file_data {
        char *svcname;
@@ -230,7 +231,7 @@ NTSTATUS print_spool_open(files_struct *fsp,
        }
 
        fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
-       fsp->fh->fd = fd;
+       fsp_set_fd(fsp, fd);
 
        fsp->vuid = current_vuid;
        fsp->fsp_flags.can_lock = false;
index 547f55db7d8aedc12f5616ccced277e994f7e04c..04a9dfeea9d54b172e499deae2db8c728a4880e2 100644 (file)
@@ -108,7 +108,7 @@ connection_struct *conn_new(struct smbd_server_connection *sconn)
        }
        conn->sconn = sconn;
        conn->force_group_gid = (gid_t)-1;
-       conn->cwd_fsp->fh->fd = -1;
+       fsp_set_fd(conn->cwd_fsp, -1);
        conn->cwd_fsp->fnum = FNUM_FIELD_INVALID;
        conn->cwd_fsp->conn = conn;
 
index 73eb2d1716607af8476b2438ef992e0dcb7dcad8..7c2dbb140569a0729a4c5cd4f0ac971380011a93 100644 (file)
@@ -1339,7 +1339,7 @@ static int smb_Dir_destructor(struct smb_Dir *dir_hnd)
        files_struct *fsp = dir_hnd->fsp;
 
        SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
        if (fsp->dptr != NULL) {
                SMB_ASSERT(fsp->dptr->dir_hnd == dir_hnd);
                fsp->dptr->dir_hnd = NULL;
@@ -1412,7 +1412,7 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
                goto fail;
        }
 
-       if (fsp->fh->fd == -1) {
+       if (fsp_get_io_fd(fsp) == -1) {
                errno = EBADF;
                goto fail;
        }
index 90921a65e36e931878933c166d9e765dd2317927..705fe0c00ec811688b8f8c9a2df59ccd2a35ae4e 100644 (file)
@@ -167,7 +167,7 @@ NTSTATUS open_fake_file(struct smb_request *req, connection_struct *conn,
                 (unsigned int)access_mask));
 
        fsp->conn = conn;
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
        fsp->vuid = current_vuid;
        fsp->fh->pos = -1;
        fsp->fsp_flags.can_lock = false; /* Should this be true ? - No, JRA */
index ada9a5fd473881d9101ba5efc93d5236f99b85b5..02d3b0605523c8b9ce1979eea94eb5991de5c67a 100644 (file)
@@ -57,7 +57,7 @@ NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
 #endif
 
        fsp->fh->ref_count = 1;
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
 
        fsp->fnum = FNUM_FIELD_INVALID;
        fsp->conn = conn;
index c487cd273f1b4eb3accdb8ed2e5902a26743680d..a5e4f5cdc818cc75d576aa028a130ba3f1262553 100644 (file)
@@ -824,7 +824,7 @@ NTSTATUS fd_open(files_struct *fsp,
                return status;
        }
 
-       fsp->fh->fd = fd;
+       fsp_set_fd(fsp, fd);
 
        DBG_DEBUG("name %s, flags = 0%o mode = 0%o, fd = %d\n",
                  smb_fname_str_dbg(smb_fname), flags, (int)mode, fd);
@@ -859,7 +859,7 @@ NTSTATUS fd_close(files_struct *fsp)
        }
 
        ret = SMB_VFS_CLOSE(fsp);
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
        if (ret == -1) {
                return map_nt_error_from_unix(errno);
        }
index 785cbb23b5fac4a016b1f12aaeac1a267f7f730a..4338971d7fac5011effdd10a73887fba2e8caa03 100644 (file)
@@ -50,7 +50,7 @@ NTSTATUS open_np_file(struct smb_request *smb_req, const char *name,
        }
 
        fsp->conn = conn;
-       fsp->fh->fd = -1;
+       fsp_set_fd(fsp, -1);
        fsp->vuid = smb_req->vuid;
        fsp->fsp_flags.can_lock = false;
        fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
index dd4a70ca256992d75101871e72db9289161a885b..034d0621c022ea355b5dda06862a8f4d9087891a 100644 (file)
@@ -155,6 +155,7 @@ static NTSTATUS init_files_struct(TALLOC_CTX *mem_ctx,
                                  struct files_struct **_fsp)
 {
        struct smb_filename *smb_fname = NULL;
+       int fd;
        int ret;
        mode_t saved_umask;
        struct files_struct *fsp;
@@ -191,22 +192,23 @@ static NTSTATUS init_files_struct(TALLOC_CTX *mem_ctx,
         */
        saved_umask = umask(0);
 
-       fsp->fh->fd = SMB_VFS_OPENAT(conn,
-                                    fspcwd,
-                                    smb_fname,
-                                    fsp,
-                                    flags,
-                                    00644);
+       fd = SMB_VFS_OPENAT(conn,
+                           fspcwd,
+                           smb_fname,
+                           fsp,
+                           flags,
+                           00644);
 
        umask(saved_umask);
 
-       if (fsp->fh->fd == -1) {
+       if (fd == -1) {
                int err = errno;
                if (err == ENOENT) {
                        return NT_STATUS_OBJECT_NAME_NOT_FOUND;
                }
                return NT_STATUS_INVALID_PARAMETER;
        }
+       fsp_set_fd(fsp, fd);
 
        ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
        if (ret == -1) {
index 1c8368b060c7e45af0999e918b16f905d6355f4e..b4a265e346d71b62550b5be91e6d13553bfc5d15 100644 (file)
@@ -921,7 +921,7 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
                 * tree connect to a share with the same underlying
                 * path (may or may not the same share).
                 */
-               conn->cwd_fsp->fh->fd = AT_FDCWD;
+               fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
                return 0;
        }
 
@@ -985,7 +985,7 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
        talloc_move(talloc_tos(), &conn->cwd_fsp->fsp_name);
 
        conn->cwd_fsp->fsp_name = talloc_move(conn->cwd_fsp, &cwd);
-       conn->cwd_fsp->fh->fd = AT_FDCWD;
+       fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
 
        DBG_INFO("vfs_ChDir got %s\n", fsp_str_dbg(conn->cwd_fsp));
 
@@ -1620,7 +1620,7 @@ NTSTATUS vfs_at_fspcwd(TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       fsp->fh->fd = AT_FDCWD;
+       fsp_set_fd(fsp, AT_FDCWD);
        fsp->fnum = FNUM_FIELD_INVALID;
        fsp->conn = conn;
 
index 7f3b7e04e48cf8906209067e99e50dc965d2d195..cae2042ae723dd522ff271a3bb46004a53da05c1 100644 (file)
@@ -298,6 +298,7 @@ static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, c
        struct smb_filename *smb_fname = NULL;
        NTSTATUS status;
        int ret;
+       int fd;
 
        mode = 00400;
 
@@ -399,18 +400,19 @@ static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, c
                return status;
        }
 
-       fsp->fh->fd = SMB_VFS_OPENAT(vfs->conn,
-                                    fspcwd,
-                                    smb_fname,
-                                    fsp,
-                                    flags,
-                                    mode);
-       if (fsp->fh->fd == -1) {
+       fd = SMB_VFS_OPENAT(vfs->conn,
+                           fspcwd,
+                           smb_fname,
+                           fsp,
+                           flags,
+                           mode);
+       if (fd == -1) {
                printf("open: error=%d (%s)\n", errno, strerror(errno));
                TALLOC_FREE(fsp);
                TALLOC_FREE(smb_fname);
                return NT_STATUS_UNSUCCESSFUL;
        }
+       fsp_set_fd(fsp, fd);
 
        status = NT_STATUS_OK;
        ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
@@ -1626,6 +1628,7 @@ static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int a
        struct smb_filename *smb_fname = NULL;
        NTSTATUS status;
        struct security_descriptor *sd = NULL;
+       int fd;
 
        if (argc != 3) {
                printf("Usage: set_nt_acl <file> <sddl>\n");
@@ -1667,26 +1670,27 @@ static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int a
                return status;
        }
 
-       fsp->fh->fd = SMB_VFS_OPENAT(vfs->conn,
-                                    fspcwd,
-                                    smb_fname,
-                                    fsp,
-                                    O_RDWR,
-                                    mode);
-       if (fsp->fh->fd == -1 && errno == EISDIR) {
-               fsp->fh->fd = SMB_VFS_OPENAT(vfs->conn,
-                                            fspcwd,
-                                            smb_fname,
-                                            fsp,
-                                            flags,
-                                            mode);
-       }
-       if (fsp->fh->fd == -1) {
+       fd = SMB_VFS_OPENAT(vfs->conn,
+                           fspcwd,
+                           smb_fname,
+                           fsp,
+                           O_RDWR,
+                           mode);
+       if (fd == -1 && errno == EISDIR) {
+               fd = SMB_VFS_OPENAT(vfs->conn,
+                                   fspcwd,
+                                   smb_fname,
+                                   fsp,
+                                   flags,
+                                   mode);
+       }
+       if (fd == -1) {
                printf("open: error=%d (%s)\n", errno, strerror(errno));
                TALLOC_FREE(fsp);
                TALLOC_FREE(smb_fname);
                return NT_STATUS_UNSUCCESSFUL;
        }
+       fsp_set_fd(fsp, fd);
 
        status = NT_STATUS_OK;
        ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
index b0ef06ebcb6da9ed096e7be96ff45c2337da5148..708ddd7ed45b9f972e5e42f844e6eb3b56d3a5a7 100644 (file)
@@ -733,6 +733,7 @@ bld.SAMBA3_LIBRARY('smbd_base',
                         vfs_acl_common
                         NDR_QUOTA
                         GNUTLS_HELPERS
+                        fd_handle
                    ''' +
                    bld.env['dmapi_lib'] +
                    bld.env['legacy_quota_libs'] +