build: Remove SMB_OFF_T, replace with off_t
[kai/samba.git] / source3 / smbd / dir.c
index edc1ffe65f342524eb6605b3868ac40e1bb5ced0..cb27110f26b4b2f70d629f96f4fd7e59d4e89827 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "libcli/security/security.h"
+#include "lib/util/bitmap.h"
 
 /*
    This module implements directory related functions for Samba.
@@ -40,7 +43,7 @@ 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;
@@ -61,8 +64,13 @@ 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. */
 };
 
+static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
+                       files_struct *fsp,
+                       const char *mask,
+                       uint32 attr);
 
 #define INVALID_DPTR_KEY (-3)
 
@@ -74,7 +82,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)
@@ -86,7 +94,7 @@ bool make_dir_struct(TALLOC_CTX *ctx,
                return False;
        }
 
-       if ((mode & aDIR) != 0) {
+       if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
                size = 0;
        }
 
@@ -118,14 +126,14 @@ bool make_dir_struct(TALLOC_CTX *ctx,
 
 bool init_dptrs(struct smbd_server_connection *sconn)
 {
-       if (sconn->smb1.searches.dptr_bmap) {
+       if (sconn->searches.dptr_bmap) {
                return true;
        }
 
-       sconn->smb1.searches.dptr_bmap = bitmap_talloc(
+       sconn->searches.dptr_bmap = bitmap_talloc(
                sconn, MAX_DIRECTORY_HANDLES);
 
-       if (sconn->smb1.searches.dptr_bmap == NULL) {
+       if (sconn->searches.dptr_bmap == NULL) {
                return false;
        }
 
@@ -155,7 +163,7 @@ static void dptr_idleoldest(struct smbd_server_connection *sconn)
        /*
         * Go to the end of the list.
         */
-       dptr = DLIST_TAIL(sconn->smb1.searches.dirptrs);
+       dptr = DLIST_TAIL(sconn->searches.dirptrs);
 
        if(!dptr) {
                DEBUG(0,("No dptrs available to idle ?\n"));
@@ -183,21 +191,21 @@ static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
 {
        struct dptr_struct *dptr;
 
-       for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = dptr->next) {
+       for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
                if(dptr->dnum == key) {
                        if (!forclose && !dptr->dir_hnd) {
-                               if (sconn->smb1.searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
+                               if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
                                        dptr_idleoldest(sconn);
                                DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
                                if (!(dptr->dir_hnd = OpenDir(
-                                             NULL, dptr->conn, NULL, dptr->path,
+                                             NULL, dptr->conn, dptr->path,
                                              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->smb1.searches.dirptrs,dptr);
+                       DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
                        return dptr;
                }
        }
@@ -208,7 +216,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)
@@ -220,7 +228,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)
@@ -254,27 +262,27 @@ static void dptr_close_internal(struct dptr_struct *dptr)
                goto done;
        }
 
-       DLIST_REMOVE(sconn->smb1.searches.dirptrs, dptr);
+       if (sconn->using_smb2) {
+               goto done;
+       }
+
+       DLIST_REMOVE(sconn->searches.dirptrs, dptr);
 
        /*
         * Free the dnum in the bitmap. Remember the dnum value is always 
         * biased by one with respect to the bitmap.
         */
 
-       if (!bitmap_query(sconn->smb1.searches.dptr_bmap, dptr->dnum - 1)) {
+       if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
                DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
                        dptr->dnum ));
        }
 
-       bitmap_clear(sconn->smb1.searches.dptr_bmap, dptr->dnum - 1);
+       bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
 
 done:
        TALLOC_FREE(dptr->dir_hnd);
-
-       /* Lanman 2 specific code */
-       SAFE_FREE(dptr->wcard);
-       string_set(&dptr->path,"");
-       SAFE_FREE(dptr);
+       TALLOC_FREE(dptr);
 }
 
 /****************************************************************************
@@ -291,7 +299,7 @@ void dptr_close(struct smbd_server_connection *sconn, int *key)
        /* OS/2 seems to use -1 to indicate "close all directories" */
        if (*key == -1) {
                struct dptr_struct *next;
-               for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
+               for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
                        next = dptr->next;
                        dptr_close_internal(dptr);
                }
@@ -324,7 +332,7 @@ void dptr_closecnum(connection_struct *conn)
                return;
        }
 
-       for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
+       for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
                next = dptr->next;
                if (dptr->conn == conn) {
                        dptr_close_internal(dptr);
@@ -345,7 +353,7 @@ void dptr_idlecnum(connection_struct *conn)
                return;
        }
 
-       for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = dptr->next) {
+       for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
                if (dptr->conn == conn && dptr->dir_hnd) {
                        dptr_idle(dptr);
                }
@@ -360,7 +368,7 @@ void dptr_closepath(struct smbd_server_connection *sconn,
                    char *path,uint16 spid)
 {
        struct dptr_struct *dptr, *next;
-       for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
+       for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
                next = dptr->next;
                if (spid == dptr->spid && strequal(dptr->path,path))
                        dptr_close_internal(dptr);
@@ -381,7 +389,7 @@ static void dptr_close_oldest(struct smbd_server_connection *sconn,
        /*
         * Go to the end of the list.
         */
-       for(dptr = sconn->smb1.searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
+       for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
                ;
 
        if(!dptr) {
@@ -404,6 +412,60 @@ 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)
+{
+       NTSTATUS status;
+       struct smb_Dir *dir_hnd = NULL;
+       struct smb_filename *smb_fname_cwd = NULL;
+       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. */
+       status = create_synthetic_smb_fname(talloc_tos(), ".",
+                                       NULL, NULL,
+                                       &smb_fname_cwd);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               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
@@ -413,14 +475,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;
@@ -437,31 +500,98 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!fsp) {
-               status = check_name(conn,path);
+       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 {
+               int ret;
+               struct smb_filename *smb_dname = NULL;
+               NTSTATUS status = create_synthetic_smb_fname(talloc_tos(),
+                                               path,
+                                               NULL,
+                                               NULL,
+                                               &smb_dname);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
+               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,
+                                               SEC_DIR_LIST);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               if (req && req->priv_paths) {
+                       dir_hnd = open_dir_with_privilege(conn,
+                                               req,
+                                               path,
+                                               wcard,
+                                               attr);
+               } else {
+                       dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
+               }
        }
 
-       dir_hnd = OpenDir(NULL, conn, fsp, path, wcard, attr);
        if (!dir_hnd) {
                return map_nt_error_from_unix(errno);
        }
 
-       if (sconn->smb1.searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
+       if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
                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) {
 
                /*
@@ -469,7 +599,7 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                 * value we return will fit in the range 1-255.
                 */
 
-               dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 0);
+               dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
 
                if(dptr->dnum == -1 || dptr->dnum > 254) {
 
@@ -482,10 +612,10 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                        dptr_close_oldest(sconn, true);
 
                        /* Now try again... */
-                       dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 0);
+                       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;
                        }
@@ -497,7 +627,7 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
                 */
 
-               dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 255);
+               dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
 
                if(dptr->dnum == -1 || dptr->dnum < 255) {
 
@@ -511,43 +641,24 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
                        dptr_close_oldest(sconn, false);
 
                        /* Now try again... */
-                       dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 255);
+                       dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
 
                        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;
                        }
                }
        }
 
-       bitmap_set(sconn->smb1.searches.dptr_bmap, dptr->dnum);
+       bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
 
        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->smb1.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->smb1.searches.dirptrs, dptr);
+       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));  
 
@@ -564,10 +675,18 @@ NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
 void dptr_CloseDir(files_struct *fsp)
 {
        if (fsp->dptr) {
-               if (fsp->fh->fd == dirfd(fsp->dptr->dir_hnd->dir)) {
+/*
+ * 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
                dptr_close_internal(fsp->dptr);
                fsp->dptr = NULL;
        }
@@ -593,6 +712,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.
 ****************************************************************************/
@@ -856,15 +985,15 @@ bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
        uint32 mask;
 
        /* Check the "may have" search bits. */
-       if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
+       if (((mode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)) != 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|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
+       mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
        if(mask) {
-               if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask)   /* check if matching attribute present */
+               if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))) == mask)   /* check if matching attribute present */
                        return True;
                else
                        return False;
@@ -1092,7 +1221,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,
@@ -1149,7 +1278,9 @@ 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,
+                               FILE_READ_DATA));
 }
 
 /*******************************************************************
@@ -1299,10 +1430,20 @@ 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;
+                       }
+               }
+#endif
                SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
        }
-       if (dirp->conn->sconn) {
-               dirp->conn->sconn->smb1.searches.dirhandles_open--;
+       if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
+               dirp->conn->sconn->searches.dirhandles_open--;
        }
        return 0;
 }
@@ -1312,12 +1453,11 @@ static int smb_Dir_destructor(struct smb_Dir *dirp)
 ********************************************************************/
 
 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
-                       files_struct *fsp,
                        const char *name,
                        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) {
@@ -1327,25 +1467,65 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
        dirp->conn = conn;
        dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
 
-       if (fsp) {
-               dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
-       } else {
-               dirp->dir_path = talloc_strdup(dirp, name);
+       dirp->dir_path = talloc_strdup(dirp, name);
+       if (!dirp->dir_path) {
+               errno = ENOMEM;
+               goto fail;
+       }
+
+       if (sconn && !sconn->using_smb2) {
+               sconn->searches.dirhandles_open++;
+       }
+       talloc_set_destructor(dirp, smb_Dir_destructor);
+
+       dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
+       if (!dirp->dir) {
+               DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
+                        strerror(errno) ));
+               goto fail;
        }
+
+       return dirp;
+
+  fail:
+       TALLOC_FREE(dirp);
+       return NULL;
+}
+
+/*******************************************************************
+ Open a directory from an fsp.
+********************************************************************/
+
+static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
+                       files_struct *fsp,
+                       const char *mask,
+                       uint32 attr)
+{
+       struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
+       struct smbd_server_connection *sconn = conn->sconn;
+
+       if (!dirp) {
+               return NULL;
+       }
+
+       dirp->conn = conn;
+       dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
+
+       dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
        if (!dirp->dir_path) {
                errno = ENOMEM;
                goto fail;
        }
 
-       if (sconn) {
-               sconn->smb1.searches.dirhandles_open++;
+       if (sconn && !sconn->using_smb2) {
+               sconn->searches.dirhandles_open++;
        }
        talloc_set_destructor(dirp, smb_Dir_destructor);
 
-       if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
+       if (fsp->is_directory && fsp->fh->fd != -1) {
                dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
                if (dirp->dir == NULL) {
-                       DEBUG(10,("OpenDir: SMB_VFS_FDOPENDIR on %s returned "
+                       DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
                                "NULL (%s)\n",
                                dirp->dir_path,
                                strerror(errno)));
@@ -1356,13 +1536,12 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
        }
 
        if (dirp->dir == NULL) {
-               /* FDOPENDIR didn't work - or fsp == NULL. Use
-                  OPENDIR instead. */
+               /* FDOPENDIR didn't work. Use OPENDIR instead. */
                dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
        }
 
        if (!dirp->dir) {
-               DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
+               DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
                         strerror(errno) ));
                goto fail;
        }
@@ -1374,6 +1553,7 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
        return NULL;
 }
 
+
 /*******************************************************************
  Read from a directory.
  Return directory entry, current offset, and optional stat information.
@@ -1494,7 +1674,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) {
@@ -1561,14 +1741,14 @@ bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
 *****************************************************************/
 
 NTSTATUS can_delete_directory(struct connection_struct *conn,
-                               const char *dirname)
+                             const char *dirname)
 {
        NTSTATUS status = NT_STATUS_OK;
        long dirpos = 0;
        const char *dname = NULL;
        char *talloced = NULL;
        SMB_STRUCT_STAT st;
-       struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, NULL,
+       struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
                                        dirname, NULL, 0);
 
        if (!dir_hnd) {