lib/util: move memcache.[ch] to the toplevel 'samba-util' library
[nivanova/samba-autobuild/.git] / source3 / smbd / dir.c
index 81391d0daa0e9f1e7aed6ff902c83e97d003a5de..55d774278bc53f5d2ab3026bf30036a56645277f 100644 (file)
@@ -23,6 +23,8 @@
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "libcli/security/security.h"
+#include "lib/util/bitmap.h"
+#include "../lib/util/memcache.h"
 
 /*
    This module implements directory related functions for Samba.
 #define START_OF_DIRECTORY_OFFSET ((long)0)
 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
 
+/* "Special" directory offsets in 32-bit wire format. */
+#define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
+#define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
+#define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
+
 /* Make directory handle internals available. */
 
 struct name_cache_entry {
@@ -42,13 +49,15 @@ struct name_cache_entry {
 
 struct smb_Dir {
        connection_struct *conn;
-       SMB_STRUCT_DIR *dir;
+       DIR *dir;
        long offset;
        char *dir_path;
        size_t name_cache_size;
        struct name_cache_entry *name_cache;
        unsigned int name_cache_index;
        unsigned int file_number;
+       files_struct *fsp; /* Back pointer to containing fsp, only
+                             set from OpenDir_fsp(). */
 };
 
 struct dptr_struct {
@@ -63,6 +72,9 @@ struct dptr_struct {
        char *path;
        bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
        bool did_stat; /* Optimisation for non-wcard searches. */
+       bool priv;     /* Directory handle opened with privilege. */
+       uint32_t counter;
+       struct memcache *dptr_cache;
 };
 
 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
@@ -70,6 +82,8 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
                        const char *mask,
                        uint32 attr);
 
+static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset);
+
 #define INVALID_DPTR_KEY (-3)
 
 /****************************************************************************
@@ -80,7 +94,7 @@ bool make_dir_struct(TALLOC_CTX *ctx,
                        char *buf,
                        const char *mask,
                        const char *fname,
-                       SMB_OFF_T size,
+                       off_t size,
                        uint32 mode,
                        time_t date,
                        bool uc)
@@ -92,7 +106,7 @@ bool make_dir_struct(TALLOC_CTX *ctx,
                return False;
        }
 
-       if ((mode & aDIR) != 0) {
+       if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
                size = 0;
        }
 
@@ -147,6 +161,8 @@ static void dptr_idle(struct dptr_struct *dptr)
        if (dptr->dir_hnd) {
                DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
                TALLOC_FREE(dptr->dir_hnd);
+               TALLOC_FREE(dptr->dptr_cache);
+               dptr->counter = 0;
        }
 }
 
@@ -200,7 +216,7 @@ static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
                                              dptr->wcard, dptr->attr))) {
                                        DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
                                                strerror(errno)));
-                                       return False;
+                                       return NULL;
                                }
                        }
                        DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
@@ -214,7 +230,7 @@ static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
  Get the dir path for a dir index.
 ****************************************************************************/
 
-char *dptr_path(struct smbd_server_connection *sconn, int key)
+const char *dptr_path(struct smbd_server_connection *sconn, int key)
 {
        struct dptr_struct *dptr = dptr_get(sconn, key, false);
        if (dptr)
@@ -226,7 +242,7 @@ char *dptr_path(struct smbd_server_connection *sconn, int key)
  Get the dir wcard for a dir index.
 ****************************************************************************/
 
-char *dptr_wcard(struct smbd_server_connection *sconn, int key)
+const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
 {
        struct dptr_struct *dptr = dptr_get(sconn, key, false);
        if (dptr)
@@ -260,6 +276,10 @@ static void dptr_close_internal(struct dptr_struct *dptr)
                goto done;
        }
 
+       if (sconn->using_smb2) {
+               goto done;
+       }
+
        DLIST_REMOVE(sconn->searches.dirptrs, dptr);
 
        /*
@@ -276,11 +296,7 @@ static void dptr_close_internal(struct dptr_struct *dptr)
 
 done:
        TALLOC_FREE(dptr->dir_hnd);
-
-       /* Lanman 2 specific code */
-       SAFE_FREE(dptr->wcard);
-       string_set(&dptr->path,"");
-       SAFE_FREE(dptr);
+       TALLOC_FREE(dptr);
 }
 
 /****************************************************************************
@@ -410,6 +426,57 @@ static void dptr_close_oldest(struct smbd_server_connection *sconn,
        }
 }
 
+/****************************************************************************
+ Safely do an OpenDir as root, ensuring we're in the right place.
+****************************************************************************/
+
+static struct smb_Dir *open_dir_with_privilege(connection_struct *conn,
+                                       struct smb_request *req,
+                                       const char *path,
+                                       const char *wcard,
+                                       uint32_t attr)
+{
+       struct smb_Dir *dir_hnd = NULL;
+       struct smb_filename *smb_fname_cwd;
+       char *saved_dir = vfs_GetWd(talloc_tos(), conn);
+       struct privilege_paths *priv_paths = req->priv_paths;
+       int ret;
+
+       if (saved_dir == NULL) {
+               return NULL;
+       }
+
+       if (vfs_ChDir(conn, path) == -1) {
+               return NULL;
+       }
+
+       /* Now check the stat value is the same. */
+       smb_fname_cwd = synthetic_smb_fname(talloc_tos(), ".", NULL, NULL);
+
+       if (smb_fname_cwd == NULL) {
+               goto out;
+       }
+       ret = SMB_VFS_STAT(conn, smb_fname_cwd);
+       if (ret != 0) {
+               goto out;
+       }
+
+       if (!check_same_stat(&smb_fname_cwd->st, &priv_paths->parent_name.st)) {
+               DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
+                       "and %s\n",
+                       path,
+                       smb_fname_str_dbg(&priv_paths->parent_name)));
+               goto out;
+       }
+
+       dir_hnd = OpenDir(NULL, conn, ".", wcard, attr);
+
+  out:
+
+       vfs_ChDir(conn, saved_dir);
+       return dir_hnd;
+}
+
 /****************************************************************************
  Create a new dir ptr. If the flag old_handle is true then we must allocate
  from the bitmap range 0 - 255 as old SMBsearch directory handles are only
@@ -419,14 +486,15 @@ static void dptr_close_oldest(struct smbd_server_connection *sconn,
  wcard must not be zero.
 ****************************************************************************/
 
-NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
+NTSTATUS dptr_create(connection_struct *conn,
+               struct smb_request *req,
+               files_struct *fsp,
                const char *path, bool old_handle, bool expect_close,uint16 spid,
                const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
 {
        struct smbd_server_connection *sconn = conn->sconn;
        struct dptr_struct *dptr = NULL;
        struct smb_Dir *dir_hnd;
-       NTSTATUS status;
 
        if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
                path = fsp->fsp_name->base_name;
@@ -444,13 +512,51 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
        }
 
        if (fsp) {
+               if (!(fsp->access_mask & SEC_DIR_LIST)) {
+                       DEBUG(5,("dptr_create: directory %s "
+                               "not open for LIST access\n",
+                               path));
+                       return NT_STATUS_ACCESS_DENIED;
+               }
                dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
        } else {
-               status = check_name(conn,path);
+               int ret;
+               bool backup_intent = (req && req->priv_paths);
+               struct smb_filename *smb_dname;
+               NTSTATUS status;
+
+               smb_dname = synthetic_smb_fname(talloc_tos(), path,
+                                               NULL, NULL);
+               if (smb_dname == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               if (lp_posix_pathnames()) {
+                       ret = SMB_VFS_LSTAT(conn, smb_dname);
+               } else {
+                       ret = SMB_VFS_STAT(conn, smb_dname);
+               }
+               if (ret == -1) {
+                       return map_nt_error_from_unix(errno);
+               }
+               if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
+                       return NT_STATUS_NOT_A_DIRECTORY;
+               }
+               status = smbd_check_access_rights(conn,
+                                               smb_dname,
+                                               backup_intent,
+                                               SEC_DIR_LIST);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
-               dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
+               if (backup_intent) {
+                       dir_hnd = open_dir_with_privilege(conn,
+                                               req,
+                                               path,
+                                               wcard,
+                                               attr);
+               } else {
+                       dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
+               }
        }
 
        if (!dir_hnd) {
@@ -461,15 +567,43 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                dptr_idleoldest(sconn);
        }
 
-       dptr = SMB_MALLOC_P(struct dptr_struct);
+       dptr = talloc(NULL, struct dptr_struct);
        if(!dptr) {
-               DEBUG(0,("malloc fail in dptr_create.\n"));
+               DEBUG(0,("talloc fail in dptr_create.\n"));
                TALLOC_FREE(dir_hnd);
                return NT_STATUS_NO_MEMORY;
        }
 
        ZERO_STRUCTP(dptr);
 
+       dptr->path = talloc_strdup(dptr, path);
+       if (!dptr->path) {
+               TALLOC_FREE(dptr);
+               TALLOC_FREE(dir_hnd);
+               return NT_STATUS_NO_MEMORY;
+       }
+       dptr->conn = conn;
+       dptr->dir_hnd = dir_hnd;
+       dptr->spid = spid;
+       dptr->expect_close = expect_close;
+       dptr->wcard = talloc_strdup(dptr, wcard);
+       if (!dptr->wcard) {
+               TALLOC_FREE(dptr);
+               TALLOC_FREE(dir_hnd);
+               return NT_STATUS_NO_MEMORY;
+       }
+       if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
+               dptr->has_wild = True;
+       } else {
+               dptr->has_wild = wcard_has_wild;
+       }
+
+       dptr->attr = attr;
+
+       if (sconn->using_smb2) {
+               goto done;
+       }
+
        if(old_handle) {
 
                /*
@@ -493,7 +627,7 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                        dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
                        if(dptr->dnum == -1 || dptr->dnum > 254) {
                                DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
-                               SAFE_FREE(dptr);
+                               TALLOC_FREE(dptr);
                                TALLOC_FREE(dir_hnd);
                                return NT_STATUS_TOO_MANY_OPENED_FILES;
                        }
@@ -523,7 +657,7 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
 
                        if(dptr->dnum == -1 || dptr->dnum < 255) {
                                DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
-                               SAFE_FREE(dptr);
+                               TALLOC_FREE(dptr);
                                TALLOC_FREE(dir_hnd);
                                return NT_STATUS_TOO_MANY_OPENED_FILES;
                        }
@@ -534,28 +668,9 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
 
        dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
 
-       string_set(&dptr->path,path);
-       dptr->conn = conn;
-       dptr->dir_hnd = dir_hnd;
-       dptr->spid = spid;
-       dptr->expect_close = expect_close;
-       dptr->wcard = SMB_STRDUP(wcard);
-       if (!dptr->wcard) {
-               bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
-               SAFE_FREE(dptr);
-               TALLOC_FREE(dir_hnd);
-               return NT_STATUS_NO_MEMORY;
-       }
-       if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
-               dptr->has_wild = True;
-       } else {
-               dptr->has_wild = wcard_has_wild;
-       }
-
-       dptr->attr = attr;
-
        DLIST_ADD(sconn->searches.dirptrs, dptr);
 
+done:
        DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
                dptr->dnum,path,expect_close));  
 
@@ -572,18 +687,11 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
 void dptr_CloseDir(files_struct *fsp)
 {
        if (fsp->dptr) {
-/*
- * Ugly hack. We have defined fdopendir to return ENOSYS if dirfd also isn't
- * present. I hate Solaris. JRA.
- */
-#ifdef HAVE_DIRFD
-               if (fsp->fh->fd != -1 &&
-                               fsp->dptr->dir_hnd &&
-                               dirfd(fsp->dptr->dir_hnd->dir)) {
-                       /* The call below closes the underlying fd. */
-                       fsp->fh->fd = -1;
-               }
-#endif
+               /*
+                * The destructor for the struct smb_Dir
+                * (fsp->dptr->dir_hnd) now handles
+                * all resource deallocation.
+                */
                dptr_close_internal(fsp->dptr);
                fsp->dptr = NULL;
        }
@@ -609,6 +717,16 @@ int dptr_dnum(struct dptr_struct *dptr)
        return dptr->dnum;
 }
 
+bool dptr_get_priv(struct dptr_struct *dptr)
+{
+       return dptr->priv;
+}
+
+void dptr_set_priv(struct dptr_struct *dptr)
+{
+       dptr->priv = true;
+}
+
 /****************************************************************************
  Return the next visible file name, skipping veto'd and invisible files.
 ****************************************************************************/
@@ -636,10 +754,10 @@ static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
  Return the next visible file name, skipping veto'd and invisible files.
 ****************************************************************************/
 
-char *dptr_ReadDirName(TALLOC_CTX *ctx,
-                       struct dptr_struct *dptr,
-                       long *poffset,
-                       SMB_STRUCT_STAT *pst)
+static char *dptr_ReadDirName(TALLOC_CTX *ctx,
+                             struct dptr_struct *dptr,
+                             long *poffset,
+                             SMB_STRUCT_STAT *pst)
 {
        struct smb_filename smb_fname_base;
        char *name = NULL;
@@ -779,20 +897,81 @@ bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, S
 }
 
 /****************************************************************************
Add the name we're returning into the underlying cache.
Initialize variables & state data at the beginning of all search SMB requests.
 ****************************************************************************/
-
-void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
+void dptr_init_search_op(struct dptr_struct *dptr)
 {
-       DirCacheAdd(dptr->dir_hnd, name, offset);
+       SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
 }
 
 /****************************************************************************
Initialize variables & state data at the beginning of all search SMB requests.
Map a native directory offset to a 32-bit cookie.
 ****************************************************************************/
-void dptr_init_search_op(struct dptr_struct *dptr)
+
+static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
 {
-       SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
+       DATA_BLOB key;
+       DATA_BLOB val;
+
+       if (offset == END_OF_DIRECTORY_OFFSET) {
+               return WIRE_END_OF_DIRECTORY_OFFSET;
+       } else if(offset == START_OF_DIRECTORY_OFFSET) {
+               return WIRE_START_OF_DIRECTORY_OFFSET;
+       } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
+               return WIRE_DOT_DOT_DIRECTORY_OFFSET;
+       }
+       if (sizeof(long) == 4) {
+               /* 32-bit machine. We can cheat... */
+               return (uint32_t)offset;
+       }
+       if (dptr->dptr_cache == NULL) {
+               /* Lazy initialize cache. */
+               dptr->dptr_cache = memcache_init(dptr, 0);
+               if (dptr->dptr_cache == NULL) {
+                       return WIRE_END_OF_DIRECTORY_OFFSET;
+               }
+       } else {
+               /* Have we seen this offset before ? */
+               key.data = (void *)&offset;
+               key.length = sizeof(offset);
+               if (memcache_lookup(dptr->dptr_cache,
+                                       SMB1_SEARCH_OFFSET_MAP,
+                                       key,
+                                       &val)) {
+                       uint32_t wire_offset;
+                       SMB_ASSERT(val.length == sizeof(wire_offset));
+                       memcpy(&wire_offset, val.data, sizeof(wire_offset));
+                       DEBUG(10,("found wire %u <-> offset %ld\n",
+                               (unsigned int)wire_offset,
+                               (long)offset));
+                       return wire_offset;
+               }
+       }
+       /* Allocate a new wire cookie. */
+       do {
+               dptr->counter++;
+       } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
+                dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
+                dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
+       /* Store it in the cache. */
+       key.data = (void *)&offset;
+       key.length = sizeof(offset);
+       val.data = (void *)&dptr->counter;
+       val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
+       memcache_add(dptr->dptr_cache,
+                       SMB1_SEARCH_OFFSET_MAP,
+                       key,
+                       val);
+       /* And the reverse mapping for lookup from
+          map_wire_to_dir_offset(). */
+       memcache_add(dptr->dptr_cache,
+                       SMB1_SEARCH_OFFSET_MAP,
+                       val,
+                       key);
+       DEBUG(10,("stored wire %u <-> offset %ld\n",
+               (unsigned int)dptr->counter,
+               (long)offset));
+       return dptr->counter;
 }
 
 /****************************************************************************
@@ -804,19 +983,61 @@ bool dptr_fill(struct smbd_server_connection *sconn,
 {
        unsigned char *buf = (unsigned char *)buf1;
        struct dptr_struct *dptr = dptr_get(sconn, key, false);
-       uint32 offset;
+       uint32_t wire_offset;
        if (!dptr) {
                DEBUG(1,("filling null dirptr %d\n",key));
                return(False);
        }
-       offset = (uint32)TellDir(dptr->dir_hnd);
+       wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
        DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
-               (long)dptr->dir_hnd,(int)offset));
+               (long)dptr->dir_hnd,(int)wire_offset));
        buf[0] = key;
-       SIVAL(buf,1,offset);
+       SIVAL(buf,1,wire_offset);
        return(True);
 }
 
+/****************************************************************************
+ Map a 32-bit wire cookie to a native directory offset.
+****************************************************************************/
+
+static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
+{
+       DATA_BLOB key;
+       DATA_BLOB val;
+
+       if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
+               return END_OF_DIRECTORY_OFFSET;
+       } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
+               return START_OF_DIRECTORY_OFFSET;
+       } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
+               return DOT_DOT_DIRECTORY_OFFSET;
+       }
+       if (sizeof(long) == 4) {
+               /* 32-bit machine. We can cheat... */
+               return (long)wire_offset;
+       }
+       if (dptr->dptr_cache == NULL) {
+               /* Logic error, cache should be initialized. */
+               return END_OF_DIRECTORY_OFFSET;
+       }
+       key.data = (void *)&wire_offset;
+       key.length = sizeof(wire_offset);
+       if (memcache_lookup(dptr->dptr_cache,
+                               SMB1_SEARCH_OFFSET_MAP,
+                               key,
+                               &val)) {
+               /* Found mapping. */
+               long offset;
+               SMB_ASSERT(val.length == sizeof(offset));
+               memcpy(&offset, val.data, sizeof(offset));
+               DEBUG(10,("lookup wire %u <-> offset %ld\n",
+                       (unsigned int)wire_offset,
+                       (long)offset));
+               return offset;
+       }
+       return END_OF_DIRECTORY_OFFSET;
+}
+
 /****************************************************************************
  Fetch the dir ptr and seek it given the 5 byte server field.
 ****************************************************************************/
@@ -826,7 +1047,7 @@ struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
 {
        unsigned int key = *(unsigned char *)buf;
        struct dptr_struct *dptr = dptr_get(sconn, key, false);
-       uint32 offset;
+       uint32_t wire_offset;
        long seekoff;
 
        if (!dptr) {
@@ -834,12 +1055,8 @@ struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
                return(NULL);
        }
        *num = key;
-       offset = IVAL(buf,1);
-       if (offset == (uint32)-1) {
-               seekoff = END_OF_DIRECTORY_OFFSET;
-       } else {
-               seekoff = (long)offset;
-       }
+       wire_offset = IVAL(buf,1);
+       seekoff = map_wire_to_dir_offset(dptr, wire_offset);
        SeekDir(dptr->dir_hnd,seekoff);
        DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
                key, dptr->path, (int)seekoff));
@@ -863,32 +1080,6 @@ struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
        return(dptr);
 }
 
-/****************************************************************************
- Check that a file matches a particular file type.
-****************************************************************************/
-
-bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
-{
-       uint32 mask;
-
-       /* Check the "may have" search bits. */
-       if (((mode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | aSYSTEM | aDIR)) != 0)
-               return False;
-
-       /* Check the "must have" bits, which are the may have bits shifted eight */
-       /* If must have bit is set, the file/dir can not be returned in search unless the matching
-               file attribute is set */
-       mask = ((dirtype >> 8) & (aDIR|aARCH|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|aSYSTEM)); /* & 0x37 */
-       if(mask) {
-               if((mask & (mode & (aDIR|aARCH|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|aSYSTEM))) == mask)   /* check if matching attribute present */
-                       return True;
-               else
-                       return False;
-       }
-
-       return True;
-}
-
 static bool mangle_mask_match(connection_struct *conn,
                const char *filename,
                const char *mask)
@@ -923,17 +1114,19 @@ bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
                           long *_prev_offset)
 {
        connection_struct *conn = dirptr->conn;
-       bool needslash;
+       size_t slashlen;
+       size_t pathlen;
 
        *_smb_fname = NULL;
        *_mode = 0;
 
-       needslash = ( dirptr->path[strlen(dirptr->path) -1] != '/');
+       pathlen = strlen(dirptr->path);
+       slashlen = ( dirptr->path[pathlen-1] != '/') ? 1 : 0;
 
        while (true) {
                long cur_offset;
                long prev_offset;
-               SMB_STRUCT_STAT sbuf;
+               SMB_STRUCT_STAT sbuf = { 0 };
                char *dname = NULL;
                bool isdots;
                char *fname = NULL;
@@ -941,7 +1134,6 @@ bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
                struct smb_filename smb_fname;
                uint32_t mode = 0;
                bool ok;
-               NTSTATUS status;
 
                cur_offset = dptr_TellDir(dirptr);
                prev_offset = cur_offset;
@@ -972,16 +1164,27 @@ bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
                        continue;
                }
 
-               pathreal = talloc_asprintf(ctx, "%s%s%s",
-                                          dirptr->path,
-                                          needslash?"/":"",
-                                          dname);
+               /*
+                * This used to be
+                * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
+                *                            needslash?"/":"", dname);
+                * but this was measurably slower than doing the memcpy.
+                */
+
+               pathreal = talloc_array(
+                       ctx, char,
+                       pathlen + slashlen + talloc_get_size(dname));
                if (!pathreal) {
                        TALLOC_FREE(dname);
                        TALLOC_FREE(fname);
                        return false;
                }
 
+               memcpy(pathreal, dirptr->path, pathlen);
+               pathreal[pathlen] = '/';
+               memcpy(pathreal + slashlen + pathlen, dname,
+                      talloc_get_size(dname));
+
                /* Create smb_fname with NULL stream_name. */
                ZERO_STRUCT(smb_fname);
                smb_fname.base_name = pathreal;
@@ -995,7 +1198,7 @@ bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
                        continue;
                }
 
-               if (!dir_check_ftype(conn, mode, dirtype)) {
+               if (!dir_check_ftype(mode, dirtype)) {
                        DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
                                fname, (unsigned int)mode, (unsigned int)dirtype));
                        TALLOC_FREE(dname);
@@ -1026,9 +1229,9 @@ bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
 
                TALLOC_FREE(dname);
 
-               status = copy_smb_filename(ctx, &smb_fname, _smb_fname);
+               *_smb_fname = cp_smb_filename(ctx, &smb_fname);
                TALLOC_FREE(pathreal);
-               if (!NT_STATUS_IS_OK(status)) {
+               if (*_smb_fname == NULL) {
                        return false;
                }
                *_fname = fname;
@@ -1108,7 +1311,7 @@ bool get_dir_entry(TALLOC_CTX *ctx,
                const char *mask,
                uint32_t dirtype,
                char **_fname,
-               SMB_OFF_T *_size,
+               off_t *_size,
                uint32_t *_mode,
                struct timespec *_date,
                bool check_descend,
@@ -1165,7 +1368,10 @@ static bool user_can_read_file(connection_struct *conn,
                return True;
        }
 
-       return can_access_file_acl(conn, smb_fname, FILE_READ_DATA);
+       return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
+                               smb_fname,
+                               false,
+                               FILE_READ_DATA));
 }
 
 /*******************************************************************
@@ -1234,12 +1440,11 @@ static bool file_is_special(connection_struct *conn,
 bool is_visible_file(connection_struct *conn, const char *dir_path,
                     const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
 {
-       bool hide_unreadable = lp_hideunreadable(SNUM(conn));
-       bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
+       bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
+       bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
        bool hide_special = lp_hide_special_files(SNUM(conn));
        char *entry = NULL;
        struct smb_filename *smb_fname_base = NULL;
-       NTSTATUS status;
        bool ret = false;
 
        if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
@@ -1260,9 +1465,9 @@ bool is_visible_file(connection_struct *conn, const char *dir_path,
                }
 
                /* Create an smb_filename with stream_name == NULL. */
-               status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
-                                                   pst, &smb_fname_base);
-               if (!NT_STATUS_IS_OK(status)) {
+               smb_fname_base = synthetic_smb_fname(talloc_tos(), entry, NULL,
+                                                    pst);
+               if (smb_fname_base == NULL) {
                        ret = false;
                        goto out;
                }
@@ -1314,20 +1519,23 @@ bool is_visible_file(connection_struct *conn, const char *dir_path,
 
 static int smb_Dir_destructor(struct smb_Dir *dirp)
 {
-       if (dirp->dir) {
-#ifdef HAVE_DIRFD
-               if (dirp->conn->sconn) {
-                       files_struct *fsp = file_find_fd(dirp->conn->sconn,
-                                               dirfd(dirp->dir));
-                       if (fsp) {
-                               /* The call below closes the underlying fd. */
-                               fsp->fh->fd = -1;
+       if (dirp->dir != NULL) {
+               SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
+               if (dirp->fsp != NULL) {
+                       /*
+                        * The SMB_VFS_CLOSEDIR above
+                        * closes the underlying fd inside
+                        * dirp->fsp.
+                        */
+                       dirp->fsp->fh->fd = -1;
+                       if (dirp->fsp->dptr != NULL) {
+                               SMB_ASSERT(dirp->fsp->dptr->dir_hnd == dirp);
+                               dirp->fsp->dptr->dir_hnd = NULL;
                        }
+                       dirp->fsp = NULL;
                }
-#endif
-               SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
        }
-       if (dirp->conn->sconn) {
+       if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
                dirp->conn->sconn->searches.dirhandles_open--;
        }
        return 0;
@@ -1342,7 +1550,7 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
                        const char *mask,
                        uint32 attr)
 {
-       struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
+       struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
        struct smbd_server_connection *sconn = conn->sconn;
 
        if (!dirp) {
@@ -1358,7 +1566,7 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
                goto fail;
        }
 
-       if (sconn) {
+       if (sconn && !sconn->using_smb2) {
                sconn->searches.dirhandles_open++;
        }
        talloc_set_destructor(dirp, smb_Dir_destructor);
@@ -1386,7 +1594,7 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
                        const char *mask,
                        uint32 attr)
 {
-       struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
+       struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
        struct smbd_server_connection *sconn = conn->sconn;
 
        if (!dirp) {
@@ -1402,14 +1610,16 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
                goto fail;
        }
 
-       if (sconn) {
+       if (sconn && !sconn->using_smb2) {
                sconn->searches.dirhandles_open++;
        }
        talloc_set_destructor(dirp, smb_Dir_destructor);
 
        if (fsp->is_directory && fsp->fh->fd != -1) {
                dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
-               if (dirp->dir == NULL) {
+               if (dirp->dir != NULL) {
+                       dirp->fsp = fsp;
+               } else {
                        DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
                                "NULL (%s)\n",
                                dirp->dir_path,
@@ -1550,7 +1760,7 @@ long TellDir(struct smb_Dir *dirp)
  Add an entry into the dcache.
 ********************************************************************/
 
-void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
+static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
 {
        struct name_cache_entry *e;
 
@@ -1559,7 +1769,7 @@ void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
        }
 
        if (dirp->name_cache == NULL) {
-               dirp->name_cache = TALLOC_ZERO_ARRAY(
+               dirp->name_cache = talloc_zero_array(
                        dirp, struct name_cache_entry, dirp->name_cache_size);
 
                if (dirp->name_cache == NULL) {
@@ -1625,16 +1835,20 @@ bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
  Is this directory empty ?
 *****************************************************************/
 
-NTSTATUS can_delete_directory(struct connection_struct *conn,
-                               const char *dirname)
+NTSTATUS can_delete_directory_fsp(files_struct *fsp)
 {
        NTSTATUS status = NT_STATUS_OK;
        long dirpos = 0;
        const char *dname = NULL;
+       const char *dirname = fsp->fsp_name->base_name;
        char *talloced = NULL;
        SMB_STRUCT_STAT st;
-       struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
-                                       dirname, NULL, 0);
+       struct connection_struct *conn = fsp->conn;
+       struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
+                                       conn,
+                                       fsp,
+                                       NULL,
+                                       0);
 
        if (!dir_hnd) {
                return map_nt_error_from_unix(errno);
@@ -1654,7 +1868,7 @@ NTSTATUS can_delete_directory(struct connection_struct *conn,
                        continue;
                }
 
-               DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
+               DEBUG(10,("got name %s - can't delete\n",
                         dname ));
                status = NT_STATUS_DIRECTORY_NOT_EMPTY;
                break;