smbd: add twrp arg to synthetic_smb_fname()
[amitay/samba.git] / source3 / smbd / files.c
index 0550b3115521d6310544847807c1979ffe19c115..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.
 ****************************************************************************/
@@ -93,11 +113,12 @@ NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
 
        GetTimeOfDay(&fsp->open_time);
 
-       if (sconn->conn) {
+       if (req) {
+               struct smbXsrv_connection *xconn = req->xconn;
                struct smbXsrv_open *op = NULL;
                NTTIME now = timeval_to_nttime(&fsp->open_time);
 
-               status = smbXsrv_open_create(sconn->conn,
+               status = smbXsrv_open_create(xconn,
                                             conn->session_info,
                                             now, &op);
                if (!NT_STATUS_IS_OK(status)) {
@@ -107,19 +128,27 @@ 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.
         */
-       status = create_synthetic_smb_fname(fsp, "", NULL, NULL,
-                                           &fsp->fsp_name);
-       if (!NT_STATUS_IS_OK(status)) {
+       fsp->fsp_name = synthetic_smb_fname(fsp,
+                                           "",
+                                           NULL,
+                                           NULL,
+                                           0,
+                                           0);
+       if (fsp->fsp_name == NULL) {
                file_free(NULL, fsp);
-               return status;
+               return NT_STATUS_NO_MEMORY;
        }
 
        DEBUG(5,("allocated file structure %s (%u used)\n",
@@ -141,36 +170,111 @@ NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
        return NT_STATUS_OK;
 }
 
-/****************************************************************************
- Close all open files for a connection.
-****************************************************************************/
-
-void file_close_conn(connection_struct *conn)
+/*
+ * 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)
 {
-       files_struct *fsp, *next;
+       struct files_struct *fsp = NULL;
+       NTSTATUS status;
 
-       for (fsp=conn->sconn->files; fsp; fsp=next) {
-               next = fsp->next;
-               if (fsp->conn == conn) {
-                       close_file(NULL, fsp, SHUTDOWN_CLOSE);
+       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 pid and a vuid.
+ Close all open files for a connection.
 ****************************************************************************/
 
-void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
-                   uint64_t vuid)
+void file_close_conn(connection_struct *conn)
 {
        files_struct *fsp, *next;
 
-       for (fsp=sconn->files;fsp;fsp=next) {
+       for (fsp=conn->sconn->files; fsp; fsp=next) {
                next = fsp->next;
-               if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
-                       close_file(NULL, fsp, SHUTDOWN_CLOSE);
+               if (fsp->conn != conn) {
+                       continue;
+               }
+               if (fsp->op != NULL && fsp->op->global->durable) {
+                       /*
+                        * A tree disconnect closes a durable handle
+                        */
+                       fsp->op->global->durable = false;
                }
+               close_file(NULL, fsp, SHUTDOWN_CLOSE);
        }
 }
 
@@ -312,13 +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 != FAKE_LEVEL_II_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");
@@ -378,6 +483,24 @@ files_struct *file_find_di_next(files_struct *start_fsp)
        return NULL;
 }
 
+struct files_struct *file_find_one_fsp_from_lease_key(
+       struct smbd_server_connection *sconn,
+       const struct smb2_lease_key *lease_key)
+{
+       struct files_struct *fsp;
+
+       for (fsp = sconn->files; fsp; fsp=fsp->next) {
+               if ((fsp->lease != NULL) &&
+                   (fsp->lease->lease.lease_key.data[0] ==
+                    lease_key->data[0]) &&
+                   (fsp->lease->lease.lease_key.data[1] ==
+                    lease_key->data[1])) {
+                       return fsp;
+               }
+       }
+       return NULL;
+}
+
 /****************************************************************************
  Find any fsp open with a pathname below that of an already open path.
 ****************************************************************************/
@@ -427,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.
 ****************************************************************************/
@@ -451,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--;
@@ -463,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
@@ -479,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);
        }
 
@@ -501,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);
@@ -523,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;
@@ -544,19 +668,19 @@ 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;
        }
 
-       if (req->sconn->conn == NULL) {
+       if (req->xconn == NULL) {
                return NULL;
        }
 
        now = timeval_to_nttime(&req->request_time);
 
-       status = smb1srv_open_lookup(req->sconn->conn,
+       status = smb1srv_open_lookup(req->xconn,
                                     fid, now, &op);
        if (!NT_STATUS_IS_OK(status)) {
                return NULL;
@@ -567,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;
        }
 
@@ -575,25 +699,18 @@ files_struct *file_fsp(struct smb_request *req, uint16 fid)
        return fsp;
 }
 
-struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
-                                  uint64_t persistent_id,
-                                  uint64_t volatile_id)
+struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
+                                 uint64_t persistent_id,
+                                 uint64_t volatile_id)
 {
        struct smbXsrv_open *op;
        NTSTATUS status;
        NTTIME now = 0;
        struct files_struct *fsp;
 
-       if (smb2req->compat_chain_fsp != NULL) {
-               if (smb2req->compat_chain_fsp->deferred_close) {
-                       return NULL;
-               }
-               return smb2req->compat_chain_fsp;
-       }
-
        now = timeval_to_nttime(&smb2req->request_time);
 
-       status = smb2srv_open_lookup(smb2req->sconn->conn,
+       status = smb2srv_open_lookup(smb2req->xconn,
                                     persistent_id, volatile_id,
                                     now, &op);
        if (!NT_STATUS_IS_OK(status)) {
@@ -617,15 +734,32 @@ struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
                return NULL;
        }
 
-       if (smb2req->session->compat == NULL) {
+       if (smb2req->session->global->session_wire_id != fsp->vuid) {
                return NULL;
        }
 
-       if (smb2req->session->compat->vuid != fsp->vuid) {
+       if (fsp->fsp_flags.closing) {
                return NULL;
        }
 
-       if (fsp->deferred_close) {
+       return fsp;
+}
+
+struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
+                                  uint64_t persistent_id,
+                                  uint64_t volatile_id)
+{
+       struct files_struct *fsp;
+
+       if (smb2req->compat_chain_fsp != NULL) {
+               if (smb2req->compat_chain_fsp->fsp_flags.closing) {
+                       return NULL;
+               }
+               return smb2req->compat_chain_fsp;
+       }
+
+       fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
+       if (fsp == NULL) {
                return NULL;
        }
 
@@ -637,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);
@@ -655,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);
 }
@@ -676,23 +812,26 @@ NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
 NTSTATUS file_name_hash(connection_struct *conn,
                        const char *name, uint32_t *p_name_hash)
 {
-       char *fullpath = NULL;
+       char tmpbuf[PATH_MAX];
+       char *fullpath, *to_free;
+       ssize_t len;
+       TDB_DATA key;
 
        /* Set the hash of the full pathname. */
-       fullpath = talloc_asprintf(talloc_tos(),
-                       "%s/%s",
-                       conn->connectpath,
-                       name);
-       if (!fullpath) {
+
+       len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
+                           &fullpath, &to_free);
+       if (len == -1) {
                return NT_STATUS_NO_MEMORY;
        }
-       *p_name_hash = hash(fullpath, talloc_get_size(fullpath), 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,
+                 fullpath,
                (unsigned int)*p_name_hash ));
 
-       TALLOC_FREE(fullpath);
+       TALLOC_FREE(to_free);
        return NT_STATUS_OK;
 }
 
@@ -702,12 +841,11 @@ NTSTATUS file_name_hash(connection_struct *conn,
 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
                           const struct smb_filename *smb_fname_in)
 {
-       NTSTATUS status;
        struct smb_filename *smb_fname_new;
 
-       status = copy_smb_filename(fsp, smb_fname_in, &smb_fname_new);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
+       if (smb_fname_new == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        TALLOC_FREE(fsp->fsp_name);
@@ -717,3 +855,25 @@ NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
                        smb_fname_str_dbg(fsp->fsp_name),
                        &fsp->name_hash);
 }
+
+size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
+{
+       int len = 0;
+       char tmp_buf[1] = {'\0'};
+
+       /*
+        * 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;
+       }
+
+       len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
+                      fsp->fsp_name->base_name);
+       SMB_ASSERT(len>0);
+
+       return len;
+}