smbd: add twrp arg to synthetic_smb_fname()
[amitay/samba.git] / source3 / smbd / files.c
index 91eddb879b2a8aa7adf488570926bb8915c550c3..a700b3edd271470411f347e9acd6c4b050b22ba4 100644 (file)
@@ -22,7 +22,6 @@
 #include "smbd/globals.h"
 #include "libcli/security/security.h"
 #include "util_tdb.h"
-#include <ccan/hash/hash.h>
 #include "lib/util/bitmap.h"
 
 #define FILE_HANDLE_OFFSET 0x1000
@@ -52,11 +51,21 @@ NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
+#if defined(HAVE_OFD_LOCKS)
+       fsp->fsp_flags.use_ofd_locks = true;
+       if (lp_parm_bool(SNUM(conn),
+                        "smbd",
+                        "force process locks",
+                        false)) {
+               fsp->fsp_flags.use_ofd_locks = false;
+       }
+#endif
        fsp->fh->ref_count = 1;
        fsp->fh->fd = -1;
 
        fsp->fnum = FNUM_FIELD_INVALID;
        fsp->conn = conn;
+       fsp->close_write_time = make_omit_timespec();
 
        DLIST_ADD(sconn->files, fsp);
        sconn->num_files += 1;
@@ -75,6 +84,17 @@ fail:
        return status;
 }
 
+void fsp_set_gen_id(files_struct *fsp)
+{
+       static uint64_t gen_id = 1;
+
+       /*
+        * A billion of 64-bit increments per second gives us
+        * more than 500 years of runtime without wrap.
+        */
+       fsp->fh->gen_id = gen_id++;
+}
+
 /****************************************************************************
  Find first available file slot.
 ****************************************************************************/
@@ -108,18 +128,24 @@ NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
                fsp->op = op;
                op->compat = fsp;
                fsp->fnum = op->local_id;
-               fsp->fh->gen_id = smbXsrv_open_hash(op);
        } else {
                DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
                           "allocated\n", __func__));
        }
 
+       fsp_set_gen_id(fsp);
+
        /*
         * Create an smb_filename with "" for the base_name.  There are very
         * few NULL checks, so make sure it's initialized with something. to
         * be safe until an audit can be done.
         */
-       fsp->fsp_name = synthetic_smb_fname(fsp, "", NULL, NULL);
+       fsp->fsp_name = synthetic_smb_fname(fsp,
+                                           "",
+                                           NULL,
+                                           NULL,
+                                           0,
+                                           0);
        if (fsp->fsp_name == NULL) {
                file_free(NULL, fsp);
                return NT_STATUS_NO_MEMORY;
@@ -144,6 +170,91 @@ NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
        return NT_STATUS_OK;
 }
 
+/*
+ * Create an internal fsp for an *existing* directory.
+ *
+ * This should only be used by callers in the VFS that need to control the
+ * opening of the directory. Otherwise use open_internal_dirfsp_at().
+ */
+NTSTATUS create_internal_dirfsp_at(connection_struct *conn,
+                                  struct files_struct *dirfsp,
+                                  const struct smb_filename *smb_dname,
+                                  struct files_struct **_fsp)
+{
+       struct files_struct *fsp = NULL;
+       NTSTATUS status;
+
+       SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
+
+       status = file_new(NULL, conn, &fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = fsp_set_smb_fname(fsp, smb_dname);
+       if (!NT_STATUS_IS_OK(status)) {
+               file_free(NULL, fsp);
+               return status;
+       }
+
+       if (!VALID_STAT(fsp->fsp_name->st)) {
+               status = vfs_stat_fsp(fsp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
+               DBG_ERR("%s is not a directory!\n",
+                       smb_fname_str_dbg(smb_dname));
+                file_free(NULL, fsp);
+               return NT_STATUS_NOT_A_DIRECTORY;
+       }
+
+       fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
+       fsp->access_mask = FILE_LIST_DIRECTORY;
+       fsp->fsp_flags.is_directory = true;
+
+       *_fsp = fsp;
+       return NT_STATUS_OK;
+}
+
+/*
+ * Open an internal fsp for an *existing* directory.
+ */
+NTSTATUS open_internal_dirfsp_at(connection_struct *conn,
+                                struct files_struct *dirfsp,
+                                const struct smb_filename *smb_dname,
+                                struct files_struct **_fsp)
+{
+       struct files_struct *fsp = NULL;
+       int open_flags = O_RDONLY;
+       NTSTATUS status;
+
+       SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
+
+       status = create_internal_dirfsp_at(conn, dirfsp, smb_dname, &fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+#ifdef O_DIRECTORY
+       open_flags |= O_DIRECTORY;
+#endif
+       status = fd_open(conn, fsp, open_flags, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_INFO("Could not open fd for %s (%s)\n",
+                        smb_fname_str_dbg(smb_dname),
+                        nt_errstr(status));
+               file_free(NULL, fsp);
+               return status;
+       }
+
+
+       *_fsp = fsp;
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
  Close all open files for a connection.
 ****************************************************************************/
@@ -167,23 +278,6 @@ void file_close_conn(connection_struct *conn)
        }
 }
 
-/****************************************************************************
- Close all open files for a pid and a vuid.
-****************************************************************************/
-
-void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
-                   uint64_t vuid)
-{
-       files_struct *fsp, *next;
-
-       for (fsp=sconn->files;fsp;fsp=next) {
-               next = fsp->next;
-               if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
-                       close_file(NULL, fsp, SHUTDOWN_CLOSE);
-               }
-       }
-}
-
 /****************************************************************************
  Initialise file structures.
 ****************************************************************************/
@@ -322,12 +416,14 @@ files_struct *file_find_dif(struct smbd_server_connection *sconn,
                        }
                        /* Paranoia check. */
                        if ((fsp->fh->fd == -1) &&
-                           (fsp->oplock_type != NO_OPLOCK)) {
+                           (fsp->oplock_type != NO_OPLOCK &&
+                            fsp->oplock_type != LEASE_OPLOCK)) {
+                               struct file_id_buf idbuf;
                                DEBUG(0,("file_find_dif: file %s file_id = "
                                         "%s, gen = %u oplock_type = %u is a "
                                         "stat open with oplock type !\n",
                                         fsp_str_dbg(fsp),
-                                        file_id_string_tos(&fsp->file_id),
+                                        file_id_str_buf(fsp->file_id, &idbuf),
                                         (unsigned int)fsp->fh->gen_id,
                                         (unsigned int)fsp->oplock_type ));
                                smb_panic("file_find_dif");
@@ -454,22 +550,6 @@ bool file_find_subpath(files_struct *dir_fsp)
        return false;
 }
 
-/****************************************************************************
- Sync open files on a connection.
-****************************************************************************/
-
-void file_sync_all(connection_struct *conn)
-{
-       files_struct *fsp, *next;
-
-       for (fsp=conn->sconn->files; fsp; fsp=next) {
-               next=fsp->next;
-               if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
-                       sync_file(conn, fsp, True /* write through */);
-               }
-       }
-}
-
 /****************************************************************************
  Free up a fsp.
 ****************************************************************************/
@@ -478,6 +558,10 @@ void fsp_free(files_struct *fsp)
 {
        struct smbd_server_connection *sconn = fsp->conn->sconn;
 
+       if (fsp == sconn->fsp_fi_cache.fsp) {
+               ZERO_STRUCT(sconn->fsp_fi_cache);
+       }
+
        DLIST_REMOVE(sconn->files, fsp);
        SMB_ASSERT(sconn->num_files > 0);
        sconn->num_files--;
@@ -490,6 +574,14 @@ void fsp_free(files_struct *fsp)
                fsp->fh->ref_count--;
        }
 
+       if (fsp->lease != NULL) {
+               if (fsp->lease->ref_count == 1) {
+                       TALLOC_FREE(fsp->lease);
+               } else {
+                       fsp->lease->ref_count--;
+               }
+       }
+
        fsp->conn->num_files_open--;
 
        /* this is paranoia, just in case someone tries to reuse the
@@ -506,9 +598,21 @@ void file_free(struct smb_request *req, files_struct *fsp)
        uint64_t fnum = fsp->fnum;
 
        if (fsp->notify) {
-               struct notify_context *notify_ctx =
-                       fsp->conn->sconn->notify_ctx;
-               notify_remove(notify_ctx, fsp);
+               size_t len = fsp_fullbasepath(fsp, NULL, 0);
+               char fullpath[len+1];
+
+               fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
+
+               /*
+                * Avoid /. at the end of the path name. notify can't
+                * deal with it.
+                */
+               if (len > 1 && fullpath[len-1] == '.' &&
+                   fullpath[len-2] == '/') {
+                       fullpath[len-2] = '\0';
+               }
+
+               notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
                TALLOC_FREE(fsp->notify);
        }
 
@@ -528,14 +632,7 @@ void file_free(struct smb_request *req, files_struct *fsp)
         * Clear all possible chained fsp
         * pointers in the SMB2 request queue.
         */
-       if (req != NULL && req->smb2req) {
-               remove_smb2_chained_fsp(fsp);
-       }
-
-       /* Closing a file can invalidate the positive cache. */
-       if (fsp == sconn->fsp_fi_cache.fsp) {
-               ZERO_STRUCT(sconn->fsp_fi_cache);
-       }
+       remove_smb2_chained_fsp(fsp);
 
        /* Drop all remaining extensions. */
        vfs_remove_all_fsp_extensions(fsp);
@@ -550,7 +647,7 @@ void file_free(struct smb_request *req, files_struct *fsp)
  Get an fsp from a packet given a 16 bit fnum.
 ****************************************************************************/
 
-files_struct *file_fsp(struct smb_request *req, uint16 fid)
+files_struct *file_fsp(struct smb_request *req, uint16_t fid)
 {
        struct smbXsrv_open *op;
        NTSTATUS status;
@@ -571,7 +668,7 @@ files_struct *file_fsp(struct smb_request *req, uint16 fid)
        }
 
        if (req->chain_fsp != NULL) {
-               if (req->chain_fsp->deferred_close) {
+               if (req->chain_fsp->fsp_flags.closing) {
                        return NULL;
                }
                return req->chain_fsp;
@@ -594,7 +691,7 @@ files_struct *file_fsp(struct smb_request *req, uint16 fid)
                return NULL;
        }
 
-       if (fsp->deferred_close) {
+       if (fsp->fsp_flags.closing) {
                return NULL;
        }
 
@@ -637,15 +734,11 @@ struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
                return NULL;
        }
 
-       if (smb2req->session->compat == NULL) {
-               return NULL;
-       }
-
-       if (smb2req->session->compat->vuid != fsp->vuid) {
+       if (smb2req->session->global->session_wire_id != fsp->vuid) {
                return NULL;
        }
 
-       if (fsp->deferred_close) {
+       if (fsp->fsp_flags.closing) {
                return NULL;
        }
 
@@ -659,7 +752,7 @@ struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
        struct files_struct *fsp;
 
        if (smb2req->compat_chain_fsp != NULL) {
-               if (smb2req->compat_chain_fsp->deferred_close) {
+               if (smb2req->compat_chain_fsp->fsp_flags.closing) {
                        return NULL;
                }
                return smb2req->compat_chain_fsp;
@@ -678,9 +771,12 @@ struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
  Duplicate the file handle part for a DOS or FCB open.
 ****************************************************************************/
 
-NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
-                     uint32 access_mask, uint32 share_access,
-                     uint32 create_options, files_struct *to)
+NTSTATUS dup_file_fsp(
+       struct smb_request *req,
+       files_struct *from,
+       uint32_t access_mask,
+       uint32_t create_options,
+       files_struct *to)
 {
        /* this can never happen for print files */
        SMB_ASSERT(from->print_file == NULL);
@@ -696,16 +792,15 @@ NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
        to->vuid = from->vuid;
        to->open_time = from->open_time;
        to->access_mask = access_mask;
-       to->share_access = share_access;
        to->oplock_type = from->oplock_type;
-       to->can_lock = from->can_lock;
-       to->can_read = ((access_mask & FILE_READ_DATA) != 0);
-       to->can_write =
+       to->fsp_flags.can_lock = from->fsp_flags.can_lock;
+       to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
+       to->fsp_flags.can_write =
                CAN_WRITE(from->conn) &&
                ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
-       to->modified = from->modified;
-       to->is_directory = from->is_directory;
-       to->aio_write_behind = from->aio_write_behind;
+       to->fsp_flags.modified = from->fsp_flags.modified;
+       to->fsp_flags.is_directory = from->fsp_flags.is_directory;
+       to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
 
        return fsp_set_smb_fname(to, from->fsp_name);
 }
@@ -719,7 +814,8 @@ NTSTATUS file_name_hash(connection_struct *conn,
 {
        char tmpbuf[PATH_MAX];
        char *fullpath, *to_free;
-       size_t len;
+       ssize_t len;
+       TDB_DATA key;
 
        /* Set the hash of the full pathname. */
 
@@ -728,7 +824,8 @@ NTSTATUS file_name_hash(connection_struct *conn,
        if (len == -1) {
                return NT_STATUS_NO_MEMORY;
        }
-       *p_name_hash = hash(fullpath, len+1, 0);
+       key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
+       *p_name_hash = tdb_jenkins_hash(&key);
 
        DEBUG(10,("file_name_hash: %s hash 0x%x\n",
                  fullpath,
@@ -759,15 +856,24 @@ NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
                        &fsp->name_hash);
 }
 
-const struct GUID *fsp_client_guid(const files_struct *fsp)
+size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
 {
-       return &fsp->conn->sconn->client->connections->smb2.client.guid;
-}
+       int len = 0;
+       char tmp_buf[1] = {'\0'};
 
-uint32_t fsp_lease_type(struct files_struct *fsp)
-{
-       if (fsp->oplock_type == LEASE_OPLOCK) {
-               return fsp->lease->lease.lease_state;
+       /*
+        * Don't pass NULL buffer to snprintf (to satisfy static checker)
+        * Some callers will call this function with NULL for buf and
+        * 0 for buflen in order to get length of fullbasepatch (without
+        * needing to allocate or write to buf)
+        */
+       if (buf == NULL) {
+               buf = tmp_buf;
        }
-       return map_oplock_to_lease_type(fsp->oplock_type);
+
+       len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
+                      fsp->fsp_name->base_name);
+       SMB_ASSERT(len>0);
+
+       return len;
 }