2 Unix SMB/CIFS implementation.
3 Directory handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "libcli/security/security.h"
26 #include "lib/util/bitmap.h"
27 #include "../lib/util/memcache.h"
28 #include "../librpc/gen_ndr/open_files.h"
31 This module implements directory related functions for Samba.
34 /* "Special" directory offsets. */
35 #define END_OF_DIRECTORY_OFFSET ((long)-1)
36 #define START_OF_DIRECTORY_OFFSET ((long)0)
37 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
39 /* "Special" directory offsets in 32-bit wire format. */
40 #define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
41 #define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
42 #define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
44 /* Make directory handle internals available. */
46 struct name_cache_entry {
52 connection_struct *conn;
56 size_t name_cache_size;
57 struct name_cache_entry *name_cache;
58 unsigned int name_cache_index;
59 unsigned int file_number;
60 files_struct *fsp; /* Back pointer to containing fsp, only
61 set from OpenDir_fsp(). */
65 struct dptr_struct *next, *prev;
68 struct connection_struct *conn;
69 struct smb_Dir *dir_hnd;
74 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
75 bool did_stat; /* Optimisation for non-wcard searches. */
76 bool priv; /* Directory handle opened with privilege. */
78 struct memcache *dptr_cache;
81 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
86 static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset);
88 #define INVALID_DPTR_KEY (-3)
90 /****************************************************************************
91 Initialise the dir bitmap.
92 ****************************************************************************/
94 bool init_dptrs(struct smbd_server_connection *sconn)
96 if (sconn->searches.dptr_bmap) {
100 sconn->searches.dptr_bmap = bitmap_talloc(
101 sconn, MAX_DIRECTORY_HANDLES);
103 if (sconn->searches.dptr_bmap == NULL) {
110 /****************************************************************************
111 Idle a dptr - the directory is closed but the control info is kept.
112 ****************************************************************************/
114 static void dptr_idle(struct dptr_struct *dptr)
117 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
118 TALLOC_FREE(dptr->dir_hnd);
119 TALLOC_FREE(dptr->dptr_cache);
124 /****************************************************************************
125 Idle the oldest dptr.
126 ****************************************************************************/
128 static void dptr_idleoldest(struct smbd_server_connection *sconn)
130 struct dptr_struct *dptr;
133 * Go to the end of the list.
135 dptr = DLIST_TAIL(sconn->searches.dirptrs);
138 DEBUG(0,("No dptrs available to idle ?\n"));
143 * Idle the oldest pointer.
146 for(; dptr; dptr = DLIST_PREV(dptr)) {
154 /****************************************************************************
155 Get the struct dptr_struct for a dir index.
156 ****************************************************************************/
158 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
159 int key, bool forclose)
161 struct dptr_struct *dptr;
163 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
164 if(dptr->dnum == key) {
165 if (!forclose && !dptr->dir_hnd) {
166 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
167 dptr_idleoldest(sconn);
168 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
169 if (!(dptr->dir_hnd = OpenDir(
170 NULL, dptr->conn, dptr->path,
171 dptr->wcard, dptr->attr))) {
172 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
177 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
184 /****************************************************************************
185 Get the dir path for a dir index.
186 ****************************************************************************/
188 const char *dptr_path(struct smbd_server_connection *sconn, int key)
190 struct dptr_struct *dptr = dptr_get(sconn, key, false);
196 /****************************************************************************
197 Get the dir wcard for a dir index.
198 ****************************************************************************/
200 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
202 struct dptr_struct *dptr = dptr_get(sconn, key, false);
208 /****************************************************************************
209 Get the dir attrib for a dir index.
210 ****************************************************************************/
212 uint16_t dptr_attr(struct smbd_server_connection *sconn, int key)
214 struct dptr_struct *dptr = dptr_get(sconn, key, false);
220 /****************************************************************************
221 Close a dptr (internal func).
222 ****************************************************************************/
224 static void dptr_close_internal(struct dptr_struct *dptr)
226 struct smbd_server_connection *sconn = dptr->conn->sconn;
228 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
234 if (sconn->using_smb2) {
238 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
241 * Free the dnum in the bitmap. Remember the dnum value is always
242 * biased by one with respect to the bitmap.
245 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
246 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
250 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
253 TALLOC_FREE(dptr->dir_hnd);
257 /****************************************************************************
258 Close a dptr given a key.
259 ****************************************************************************/
261 void dptr_close(struct smbd_server_connection *sconn, int *key)
263 struct dptr_struct *dptr;
265 if(*key == INVALID_DPTR_KEY)
268 /* OS/2 seems to use -1 to indicate "close all directories" */
270 struct dptr_struct *next;
271 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
273 dptr_close_internal(dptr);
275 *key = INVALID_DPTR_KEY;
279 dptr = dptr_get(sconn, *key, true);
282 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
286 dptr_close_internal(dptr);
288 *key = INVALID_DPTR_KEY;
291 /****************************************************************************
292 Close all dptrs for a cnum.
293 ****************************************************************************/
295 void dptr_closecnum(connection_struct *conn)
297 struct dptr_struct *dptr, *next;
298 struct smbd_server_connection *sconn = conn->sconn;
304 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
306 if (dptr->conn == conn) {
307 dptr_close_internal(dptr);
312 /****************************************************************************
313 Idle all dptrs for a cnum.
314 ****************************************************************************/
316 void dptr_idlecnum(connection_struct *conn)
318 struct dptr_struct *dptr;
319 struct smbd_server_connection *sconn = conn->sconn;
325 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
326 if (dptr->conn == conn && dptr->dir_hnd) {
332 /****************************************************************************
333 Close a dptr that matches a given path, only if it matches the spid also.
334 ****************************************************************************/
336 void dptr_closepath(struct smbd_server_connection *sconn,
337 char *path,uint16_t spid)
339 struct dptr_struct *dptr, *next;
340 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
342 if (spid == dptr->spid && strequal(dptr->path,path))
343 dptr_close_internal(dptr);
347 /****************************************************************************
348 Try and close the oldest handle not marked for
349 expect close in the hope that the client has
350 finished with that one.
351 ****************************************************************************/
353 static void dptr_close_oldest(struct smbd_server_connection *sconn,
356 struct dptr_struct *dptr;
359 * Go to the end of the list.
361 for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
365 DEBUG(0,("No old dptrs available to close oldest ?\n"));
370 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
371 * does not have expect_close set. If 'old' is false, close
372 * one of the new dnum handles.
375 for(; dptr; dptr = DLIST_PREV(dptr)) {
376 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
377 (!old && (dptr->dnum > 255))) {
378 dptr_close_internal(dptr);
384 /****************************************************************************
385 Safely do an OpenDir as root, ensuring we're in the right place.
386 ****************************************************************************/
388 static struct smb_Dir *open_dir_with_privilege(connection_struct *conn,
389 struct smb_request *req,
394 struct smb_Dir *dir_hnd = NULL;
395 struct smb_filename *smb_fname_cwd;
396 char *saved_dir = vfs_GetWd(talloc_tos(), conn);
397 struct privilege_paths *priv_paths = req->priv_paths;
400 if (saved_dir == NULL) {
404 if (vfs_ChDir(conn, path) == -1) {
408 /* Now check the stat value is the same. */
409 smb_fname_cwd = synthetic_smb_fname(talloc_tos(), ".", NULL, NULL);
411 if (smb_fname_cwd == NULL) {
414 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
419 if (!check_same_stat(&smb_fname_cwd->st, &priv_paths->parent_name.st)) {
420 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
423 smb_fname_str_dbg(&priv_paths->parent_name)));
427 dir_hnd = OpenDir(NULL, conn, ".", wcard, attr);
431 vfs_ChDir(conn, saved_dir);
435 /****************************************************************************
436 Create a new dir ptr. If the flag old_handle is true then we must allocate
437 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
438 one byte long. If old_handle is false we allocate from the range
439 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
440 a directory handle is never zero.
441 wcard must not be zero.
442 ****************************************************************************/
444 NTSTATUS dptr_create(connection_struct *conn,
445 struct smb_request *req,
447 const char *path, bool old_handle, bool expect_close,uint16_t spid,
448 const char *wcard, bool wcard_has_wild, uint32_t attr, struct dptr_struct **dptr_ret)
450 struct smbd_server_connection *sconn = conn->sconn;
451 struct dptr_struct *dptr = NULL;
452 struct smb_Dir *dir_hnd;
454 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
455 path = fsp->fsp_name->base_name;
458 DEBUG(5,("dptr_create dir=%s\n", path));
461 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
462 return NT_STATUS_INTERNAL_ERROR;
466 return NT_STATUS_INVALID_PARAMETER;
470 if (!(fsp->access_mask & SEC_DIR_LIST)) {
471 DEBUG(5,("dptr_create: directory %s "
472 "not open for LIST access\n",
474 return NT_STATUS_ACCESS_DENIED;
476 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
479 bool backup_intent = (req && req->priv_paths);
480 struct smb_filename *smb_dname;
483 smb_dname = synthetic_smb_fname(talloc_tos(), path,
485 if (smb_dname == NULL) {
486 return NT_STATUS_NO_MEMORY;
488 if (req != NULL && req->posix_pathnames) {
489 ret = SMB_VFS_LSTAT(conn, smb_dname);
491 ret = SMB_VFS_STAT(conn, smb_dname);
494 return map_nt_error_from_unix(errno);
496 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
497 return NT_STATUS_NOT_A_DIRECTORY;
499 status = smbd_check_access_rights(conn,
503 if (!NT_STATUS_IS_OK(status)) {
507 dir_hnd = open_dir_with_privilege(conn,
513 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
518 return map_nt_error_from_unix(errno);
521 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
522 dptr_idleoldest(sconn);
525 dptr = talloc_zero(NULL, struct dptr_struct);
527 DEBUG(0,("talloc fail in dptr_create.\n"));
528 TALLOC_FREE(dir_hnd);
529 return NT_STATUS_NO_MEMORY;
532 dptr->path = talloc_strdup(dptr, path);
535 TALLOC_FREE(dir_hnd);
536 return NT_STATUS_NO_MEMORY;
539 dptr->dir_hnd = dir_hnd;
541 dptr->expect_close = expect_close;
542 dptr->wcard = talloc_strdup(dptr, wcard);
545 TALLOC_FREE(dir_hnd);
546 return NT_STATUS_NO_MEMORY;
548 if ((req != NULL && req->posix_pathnames) ||
549 (wcard[0] == '.' && wcard[1] == 0)) {
550 dptr->has_wild = True;
552 dptr->has_wild = wcard_has_wild;
557 if (sconn->using_smb2) {
564 * This is an old-style SMBsearch request. Ensure the
565 * value we return will fit in the range 1-255.
568 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
570 if(dptr->dnum == -1 || dptr->dnum > 254) {
573 * Try and close the oldest handle not marked for
574 * expect close in the hope that the client has
575 * finished with that one.
578 dptr_close_oldest(sconn, true);
580 /* Now try again... */
581 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
582 if(dptr->dnum == -1 || dptr->dnum > 254) {
583 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
585 TALLOC_FREE(dir_hnd);
586 return NT_STATUS_TOO_MANY_OPENED_FILES;
592 * This is a new-style trans2 request. Allocate from
593 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
596 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
598 if(dptr->dnum == -1 || dptr->dnum < 255) {
601 * Try and close the oldest handle close in the hope that
602 * the client has finished with that one. This will only
603 * happen in the case of the Win98 client bug where it leaks
607 dptr_close_oldest(sconn, false);
609 /* Now try again... */
610 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
612 if(dptr->dnum == -1 || dptr->dnum < 255) {
613 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
615 TALLOC_FREE(dir_hnd);
616 return NT_STATUS_TOO_MANY_OPENED_FILES;
621 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
623 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
625 DLIST_ADD(sconn->searches.dirptrs, dptr);
628 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
629 dptr->dnum,path,expect_close));
637 /****************************************************************************
638 Wrapper functions to access the lower level directory handles.
639 ****************************************************************************/
641 void dptr_CloseDir(files_struct *fsp)
645 * The destructor for the struct smb_Dir
646 * (fsp->dptr->dir_hnd) now handles
647 * all resource deallocation.
649 dptr_close_internal(fsp->dptr);
654 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
656 SeekDir(dptr->dir_hnd, offset);
659 long dptr_TellDir(struct dptr_struct *dptr)
661 return TellDir(dptr->dir_hnd);
664 bool dptr_has_wild(struct dptr_struct *dptr)
666 return dptr->has_wild;
669 int dptr_dnum(struct dptr_struct *dptr)
674 bool dptr_get_priv(struct dptr_struct *dptr)
679 void dptr_set_priv(struct dptr_struct *dptr)
684 /****************************************************************************
685 Return the next visible file name, skipping veto'd and invisible files.
686 ****************************************************************************/
688 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
689 long *poffset, SMB_STRUCT_STAT *pst,
692 /* Normal search for the next file. */
694 char *talloced = NULL;
696 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
698 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
699 *ptalloced = talloced;
702 TALLOC_FREE(talloced);
707 /****************************************************************************
708 Return the next visible file name, skipping veto'd and invisible files.
709 ****************************************************************************/
711 static char *dptr_ReadDirName(TALLOC_CTX *ctx,
712 struct dptr_struct *dptr,
714 SMB_STRUCT_STAT *pst)
716 struct smb_filename smb_fname_base;
718 const char *name_temp = NULL;
719 char *talloced = NULL;
720 char *pathreal = NULL;
721 char *found_name = NULL;
724 SET_STAT_INVALID(*pst);
726 if (dptr->has_wild || dptr->did_stat) {
727 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
729 if (name_temp == NULL) {
732 if (talloced != NULL) {
733 return talloc_move(ctx, &talloced);
735 return talloc_strdup(ctx, name_temp);
738 /* If poffset is -1 then we know we returned this name before and we
739 * have no wildcards. We're at the end of the directory. */
740 if (*poffset == END_OF_DIRECTORY_OFFSET) {
744 /* We know the stored wcard contains no wildcard characters.
745 * See if we can match with a stat call. If we can't, then set
746 * did_stat to true to ensure we only do this once and keep
749 dptr->did_stat = true;
751 /* First check if it should be visible. */
752 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
755 /* This only returns false if the file was found, but
756 is explicitly not visible. Set us to end of
757 directory, but return NULL as we know we can't ever
762 if (VALID_STAT(*pst)) {
763 name = talloc_strdup(ctx, dptr->wcard);
767 pathreal = talloc_asprintf(ctx,
774 /* Create an smb_filename with stream_name == NULL. */
775 smb_fname_base = (struct smb_filename) { .base_name = pathreal };
777 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
778 *pst = smb_fname_base.st;
779 name = talloc_strdup(ctx, dptr->wcard);
782 /* If we get any other error than ENOENT or ENOTDIR
783 then the file exists we just can't stat it. */
784 if (errno != ENOENT && errno != ENOTDIR) {
785 name = talloc_strdup(ctx, dptr->wcard);
790 /* Stat failed. We know this is authoratiative if we are
791 * providing case sensitive semantics or the underlying
792 * filesystem is case sensitive.
794 if (dptr->conn->case_sensitive ||
795 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
801 * Try case-insensitive stat if the fs has the ability. This avoids
802 * scanning the whole directory.
804 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
809 } else if (errno == ENOENT) {
810 /* The case-insensitive lookup was authoritative. */
814 TALLOC_FREE(pathreal);
816 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
817 if (name_temp == NULL) {
820 if (talloced != NULL) {
821 return talloc_move(ctx, &talloced);
823 return talloc_strdup(ctx, name_temp);
826 TALLOC_FREE(pathreal);
828 /* We need to set the underlying dir_hnd offset to -1
829 * also as this function is usually called with the
830 * output from TellDir. */
831 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
835 /****************************************************************************
836 Search for a file by name, skipping veto'ed and not visible files.
837 ****************************************************************************/
839 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
841 SET_STAT_INVALID(*pst);
843 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
844 /* This is a singleton directory and we're already at the end. */
845 *poffset = END_OF_DIRECTORY_OFFSET;
849 return SearchDir(dptr->dir_hnd, name, poffset);
852 /****************************************************************************
853 Initialize variables & state data at the beginning of all search SMB requests.
854 ****************************************************************************/
855 void dptr_init_search_op(struct dptr_struct *dptr)
857 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
860 /****************************************************************************
861 Map a native directory offset to a 32-bit cookie.
862 ****************************************************************************/
864 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
869 if (offset == END_OF_DIRECTORY_OFFSET) {
870 return WIRE_END_OF_DIRECTORY_OFFSET;
871 } else if(offset == START_OF_DIRECTORY_OFFSET) {
872 return WIRE_START_OF_DIRECTORY_OFFSET;
873 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
874 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
876 if (sizeof(long) == 4) {
877 /* 32-bit machine. We can cheat... */
878 return (uint32_t)offset;
880 if (dptr->dptr_cache == NULL) {
881 /* Lazy initialize cache. */
882 dptr->dptr_cache = memcache_init(dptr, 0);
883 if (dptr->dptr_cache == NULL) {
884 return WIRE_END_OF_DIRECTORY_OFFSET;
887 /* Have we seen this offset before ? */
888 key.data = (void *)&offset;
889 key.length = sizeof(offset);
890 if (memcache_lookup(dptr->dptr_cache,
891 SMB1_SEARCH_OFFSET_MAP,
894 uint32_t wire_offset;
895 SMB_ASSERT(val.length == sizeof(wire_offset));
896 memcpy(&wire_offset, val.data, sizeof(wire_offset));
897 DEBUG(10,("found wire %u <-> offset %ld\n",
898 (unsigned int)wire_offset,
903 /* Allocate a new wire cookie. */
906 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
907 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
908 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
909 /* Store it in the cache. */
910 key.data = (void *)&offset;
911 key.length = sizeof(offset);
912 val.data = (void *)&dptr->counter;
913 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
914 memcache_add(dptr->dptr_cache,
915 SMB1_SEARCH_OFFSET_MAP,
918 /* And the reverse mapping for lookup from
919 map_wire_to_dir_offset(). */
920 memcache_add(dptr->dptr_cache,
921 SMB1_SEARCH_OFFSET_MAP,
924 DEBUG(10,("stored wire %u <-> offset %ld\n",
925 (unsigned int)dptr->counter,
927 return dptr->counter;
930 /****************************************************************************
931 Fill the 5 byte server reserved dptr field.
932 ****************************************************************************/
934 bool dptr_fill(struct smbd_server_connection *sconn,
935 char *buf1,unsigned int key)
937 unsigned char *buf = (unsigned char *)buf1;
938 struct dptr_struct *dptr = dptr_get(sconn, key, false);
939 uint32_t wire_offset;
941 DEBUG(1,("filling null dirptr %d\n",key));
944 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
945 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
946 (long)dptr->dir_hnd,(int)wire_offset));
948 SIVAL(buf,1,wire_offset);
952 /****************************************************************************
953 Map a 32-bit wire cookie to a native directory offset.
954 ****************************************************************************/
956 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
961 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
962 return END_OF_DIRECTORY_OFFSET;
963 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
964 return START_OF_DIRECTORY_OFFSET;
965 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
966 return DOT_DOT_DIRECTORY_OFFSET;
968 if (sizeof(long) == 4) {
969 /* 32-bit machine. We can cheat... */
970 return (long)wire_offset;
972 if (dptr->dptr_cache == NULL) {
973 /* Logic error, cache should be initialized. */
974 return END_OF_DIRECTORY_OFFSET;
976 key.data = (void *)&wire_offset;
977 key.length = sizeof(wire_offset);
978 if (memcache_lookup(dptr->dptr_cache,
979 SMB1_SEARCH_OFFSET_MAP,
984 SMB_ASSERT(val.length == sizeof(offset));
985 memcpy(&offset, val.data, sizeof(offset));
986 DEBUG(10,("lookup wire %u <-> offset %ld\n",
987 (unsigned int)wire_offset,
991 return END_OF_DIRECTORY_OFFSET;
994 /****************************************************************************
995 Fetch the dir ptr and seek it given the 5 byte server field.
996 ****************************************************************************/
998 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
1001 unsigned int key = *(unsigned char *)buf;
1002 struct dptr_struct *dptr = dptr_get(sconn, key, false);
1003 uint32_t wire_offset;
1007 DEBUG(3,("fetched null dirptr %d\n",key));
1011 wire_offset = IVAL(buf,1);
1012 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
1013 SeekDir(dptr->dir_hnd,seekoff);
1014 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1015 key, dptr->path, (int)seekoff));
1019 /****************************************************************************
1021 ****************************************************************************/
1023 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
1026 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
1029 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
1032 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
1036 static bool mangle_mask_match(connection_struct *conn,
1037 const char *filename,
1042 if (!name_to_8_3(filename,mname,False,conn->params)) {
1045 return mask_match_search(mname,mask,False);
1048 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
1049 struct dptr_struct *dirptr,
1054 bool (*match_fn)(TALLOC_CTX *ctx,
1059 bool (*mode_fn)(TALLOC_CTX *ctx,
1061 struct smb_filename *smb_fname,
1065 struct smb_filename **_smb_fname,
1069 connection_struct *conn = dirptr->conn;
1072 bool dirptr_path_is_dot = ISDOT(dirptr->path);
1077 pathlen = strlen(dirptr->path);
1078 slashlen = ( dirptr->path[pathlen-1] != '/') ? 1 : 0;
1083 SMB_STRUCT_STAT sbuf = { 0 };
1087 char *pathreal = NULL;
1088 struct smb_filename smb_fname;
1092 cur_offset = dptr_TellDir(dirptr);
1093 prev_offset = cur_offset;
1094 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
1096 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1097 (long)dirptr, cur_offset));
1099 if (dname == NULL) {
1103 isdots = (ISDOT(dname) || ISDOTDOT(dname));
1104 if (dont_descend && !isdots) {
1110 * fname may get mangled, dname is never mangled.
1111 * Whenever we're accessing the filesystem we use
1112 * pathreal which is composed from dname.
1115 ok = match_fn(ctx, private_data, dname, mask, &fname);
1123 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1124 * needslash?"/":"", dname);
1125 * but this was measurably slower than doing the memcpy.
1128 pathreal = talloc_array(
1130 pathlen + slashlen + talloc_get_size(dname));
1138 * We don't want to pass ./xxx to modules below us so don't
1139 * add the path if it is just . by itself.
1141 if (dirptr_path_is_dot) {
1142 memcpy(pathreal, dname, talloc_get_size(dname));
1144 memcpy(pathreal, dirptr->path, pathlen);
1145 pathreal[pathlen] = '/';
1146 memcpy(pathreal + slashlen + pathlen, dname,
1147 talloc_get_size(dname));
1150 /* Create smb_fname with NULL stream_name. */
1151 smb_fname = (struct smb_filename) {
1152 .base_name = pathreal, .st = sbuf
1155 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
1159 TALLOC_FREE(pathreal);
1163 if (!dir_check_ftype(mode, dirtype)) {
1164 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1165 fname, (unsigned int)mode, (unsigned int)dirtype));
1168 TALLOC_FREE(pathreal);
1172 if (ask_sharemode) {
1173 struct timespec write_time_ts;
1174 struct file_id fileid;
1176 fileid = vfs_file_id_from_sbuf(conn,
1178 get_file_infos(fileid, 0, NULL, &write_time_ts);
1179 if (!null_timespec(write_time_ts)) {
1180 update_stat_ex_mtime(&smb_fname.st,
1185 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1187 mask, smb_fname_str_dbg(&smb_fname),
1190 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1194 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
1195 TALLOC_FREE(pathreal);
1196 if (*_smb_fname == NULL) {
1201 *_prev_offset = prev_offset;
1209 /****************************************************************************
1210 Get an 8.3 directory entry.
1211 ****************************************************************************/
1213 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1219 connection_struct *conn = (connection_struct *)private_data;
1221 if ((strcmp(mask,"*.*") == 0) ||
1222 mask_match_search(dname, mask, false) ||
1223 mangle_mask_match(conn, dname, mask)) {
1227 * Ensure we can push the original name as UCS2. If
1228 * not, then just don't return this name.
1232 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
1233 uint8_t *tmp = talloc_array(talloc_tos(),
1237 status = srvstr_push(NULL,
1238 FLAGS2_UNICODE_STRINGS,
1247 if (!NT_STATUS_IS_OK(status)) {
1251 if (!mangle_is_8_3(dname, false, conn->params)) {
1252 bool ok = name_to_8_3(dname, mname, false,
1262 *_fname = talloc_strdup(ctx, fname);
1263 if (*_fname == NULL) {
1273 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1275 struct smb_filename *smb_fname,
1278 connection_struct *conn = (connection_struct *)private_data;
1280 if (!VALID_STAT(smb_fname->st)) {
1281 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1282 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1283 "Couldn't stat [%s]. Error "
1285 smb_fname_str_dbg(smb_fname),
1291 *_mode = dos_mode(conn, smb_fname);
1295 bool get_dir_entry(TALLOC_CTX *ctx,
1296 struct dptr_struct *dirptr,
1302 struct timespec *_date,
1306 connection_struct *conn = dirptr->conn;
1308 struct smb_filename *smb_fname = NULL;
1313 ok = smbd_dirptr_get_entry(ctx,
1319 smbd_dirptr_8_3_match_fn,
1320 smbd_dirptr_8_3_mode_fn,
1330 *_fname = talloc_move(ctx, &fname);
1331 *_size = smb_fname->st.st_ex_size;
1333 *_date = smb_fname->st.st_ex_mtime;
1334 TALLOC_FREE(smb_fname);
1338 /*******************************************************************
1339 Check to see if a user can read a file. This is only approximate,
1340 it is used as part of the "hide unreadable" option. Don't
1341 use it for anything security sensitive.
1342 ********************************************************************/
1344 static bool user_can_read_file(connection_struct *conn,
1345 struct smb_filename *smb_fname)
1348 uint32_t rejected_share_access = 0;
1349 uint32_t rejected_mask = 0;
1350 struct security_descriptor *sd = NULL;
1351 uint32_t access_mask = FILE_READ_DATA|
1353 FILE_READ_ATTRIBUTES|
1354 SEC_STD_READ_CONTROL;
1357 * Never hide files from the root user.
1358 * We use (uid_t)0 here not sec_initial_uid()
1359 * as make test uses a single user context.
1362 if (get_current_uid(conn) == (uid_t)0) {
1367 * We can't directly use smbd_check_access_rights()
1368 * here, as this implicitly grants FILE_READ_ATTRIBUTES
1369 * which the Windows access-based-enumeration code
1370 * explicitly checks for on the file security descriptor.
1373 * https://bugzilla.samba.org/show_bug.cgi?id=10252
1375 * and the smb2.acl2.ACCESSBASED test for details.
1378 rejected_share_access = access_mask & ~(conn->share_access);
1379 if (rejected_share_access) {
1380 DEBUG(10, ("rejected share access 0x%x "
1382 (unsigned int)access_mask,
1383 smb_fname_str_dbg(smb_fname),
1384 (unsigned int)rejected_share_access ));
1388 status = SMB_VFS_GET_NT_ACL(conn,
1396 if (!NT_STATUS_IS_OK(status)) {
1397 DEBUG(10, ("Could not get acl "
1399 smb_fname_str_dbg(smb_fname),
1400 nt_errstr(status)));
1404 status = se_file_access_check(sd,
1405 get_current_nttok(conn),
1412 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1413 DEBUG(10,("rejected bits 0x%x read access for %s\n",
1414 (unsigned int)rejected_mask,
1415 smb_fname_str_dbg(smb_fname) ));
1421 /*******************************************************************
1422 Check to see if a user can write a file (and only files, we do not
1423 check dirs on this one). This is only approximate,
1424 it is used as part of the "hide unwriteable" option. Don't
1425 use it for anything security sensitive.
1426 ********************************************************************/
1428 static bool user_can_write_file(connection_struct *conn,
1429 const struct smb_filename *smb_fname)
1432 * Never hide files from the root user.
1433 * We use (uid_t)0 here not sec_initial_uid()
1434 * as make test uses a single user context.
1437 if (get_current_uid(conn) == (uid_t)0) {
1441 SMB_ASSERT(VALID_STAT(smb_fname->st));
1443 /* Pseudo-open the file */
1445 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1449 return can_write_to_file(conn, smb_fname);
1452 /*******************************************************************
1453 Is a file a "special" type ?
1454 ********************************************************************/
1456 static bool file_is_special(connection_struct *conn,
1457 const struct smb_filename *smb_fname)
1460 * Never hide files from the root user.
1461 * We use (uid_t)0 here not sec_initial_uid()
1462 * as make test uses a single user context.
1465 if (get_current_uid(conn) == (uid_t)0) {
1469 SMB_ASSERT(VALID_STAT(smb_fname->st));
1471 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1472 S_ISDIR(smb_fname->st.st_ex_mode) ||
1473 S_ISLNK(smb_fname->st.st_ex_mode))
1479 /*******************************************************************
1480 Should the file be seen by the client?
1481 NOTE: A successful return is no guarantee of the file's existence.
1482 ********************************************************************/
1484 bool is_visible_file(connection_struct *conn, const char *dir_path,
1485 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1487 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1488 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1489 bool hide_special = lp_hide_special_files(SNUM(conn));
1491 struct smb_filename *smb_fname_base = NULL;
1494 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1495 return True; /* . and .. are always visible. */
1498 /* If it's a vetoed file, pretend it doesn't even exist */
1499 if (use_veto && IS_VETO_PATH(conn, name)) {
1500 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1504 if (hide_unreadable || hide_unwriteable || hide_special) {
1505 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1511 /* Create an smb_filename with stream_name == NULL. */
1512 smb_fname_base = synthetic_smb_fname(talloc_tos(), entry, NULL,
1514 if (smb_fname_base == NULL) {
1519 /* If the file name does not exist, there's no point checking
1520 * the configuration options. We succeed, on the basis that the
1521 * checks *might* have passed if the file was present.
1523 if (!VALID_STAT(*pst)) {
1524 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1528 *pst = smb_fname_base->st;
1531 /* Honour _hide unreadable_ option */
1532 if (hide_unreadable &&
1533 !user_can_read_file(conn, smb_fname_base)) {
1534 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1539 /* Honour _hide unwriteable_ option */
1540 if (hide_unwriteable && !user_can_write_file(conn,
1542 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1547 /* Honour _hide_special_ option */
1548 if (hide_special && file_is_special(conn, smb_fname_base)) {
1549 DEBUG(10,("is_visible_file: file %s is special.\n",
1558 TALLOC_FREE(smb_fname_base);
1563 static int smb_Dir_destructor(struct smb_Dir *dirp)
1565 if (dirp->dir != NULL) {
1566 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1567 if (dirp->fsp != NULL) {
1569 * The SMB_VFS_CLOSEDIR above
1570 * closes the underlying fd inside
1573 dirp->fsp->fh->fd = -1;
1574 if (dirp->fsp->dptr != NULL) {
1575 SMB_ASSERT(dirp->fsp->dptr->dir_hnd == dirp);
1576 dirp->fsp->dptr->dir_hnd = NULL;
1581 if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
1582 dirp->conn->sconn->searches.dirhandles_open--;
1587 /*******************************************************************
1589 ********************************************************************/
1591 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1596 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1597 struct smbd_server_connection *sconn = conn->sconn;
1598 struct smb_filename *smb_fname = NULL;
1605 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1607 dirp->dir_path = talloc_strdup(dirp, name);
1608 if (!dirp->dir_path) {
1613 if (sconn && !sconn->using_smb2) {
1614 sconn->searches.dirhandles_open++;
1616 talloc_set_destructor(dirp, smb_Dir_destructor);
1618 smb_fname = synthetic_smb_fname(talloc_tos(),
1622 if (smb_fname == NULL) {
1627 dirp->dir = SMB_VFS_OPENDIR(conn, smb_fname, mask, attr);
1629 TALLOC_FREE(smb_fname);
1632 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1644 /*******************************************************************
1645 Open a directory from an fsp.
1646 ********************************************************************/
1648 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1653 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1654 struct smbd_server_connection *sconn = conn->sconn;
1661 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1663 dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
1664 if (!dirp->dir_path) {
1669 if (sconn && !sconn->using_smb2) {
1670 sconn->searches.dirhandles_open++;
1672 talloc_set_destructor(dirp, smb_Dir_destructor);
1674 if (fsp->is_directory && fsp->fh->fd != -1) {
1675 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1676 if (dirp->dir != NULL) {
1679 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1683 if (errno != ENOSYS) {
1689 if (dirp->dir == NULL) {
1690 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1691 dirp->dir = SMB_VFS_OPENDIR(conn, fsp->fsp_name, mask, attr);
1695 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1708 /*******************************************************************
1709 Read from a directory.
1710 Return directory entry, current offset, and optional stat information.
1711 Don't check for veto or invisible files.
1712 ********************************************************************/
1714 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1715 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1718 char *talloced = NULL;
1719 connection_struct *conn = dirp->conn;
1721 /* Cheat to allow . and .. to be the first entries returned. */
1722 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1723 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1725 if (dirp->file_number == 0) {
1727 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1730 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1732 dirp->file_number++;
1737 if (*poffset == END_OF_DIRECTORY_OFFSET) {
1738 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1742 /* A real offset, seek to it. */
1743 SeekDir(dirp, *poffset);
1745 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1746 /* Ignore . and .. - we've already returned them. */
1748 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1749 TALLOC_FREE(talloced);
1753 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1754 *ptalloced = talloced;
1755 dirp->file_number++;
1758 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1763 /*******************************************************************
1764 Rewind to the start.
1765 ********************************************************************/
1767 void RewindDir(struct smb_Dir *dirp, long *poffset)
1769 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1770 dirp->file_number = 0;
1771 dirp->offset = START_OF_DIRECTORY_OFFSET;
1772 *poffset = START_OF_DIRECTORY_OFFSET;
1775 /*******************************************************************
1777 ********************************************************************/
1779 void SeekDir(struct smb_Dir *dirp, long offset)
1781 if (offset != dirp->offset) {
1782 if (offset == START_OF_DIRECTORY_OFFSET) {
1783 RewindDir(dirp, &offset);
1785 * Ok we should really set the file number here
1786 * to 1 to enable ".." to be returned next. Trouble
1787 * is I'm worried about callers using SeekDir(dirp,0)
1788 * as equivalent to RewindDir(). So leave this alone
1791 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1792 RewindDir(dirp, &offset);
1794 * Set the file number to 2 - we want to get the first
1795 * real file entry (the one we return after "..")
1796 * on the next ReadDir.
1798 dirp->file_number = 2;
1799 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1800 ; /* Don't seek in this case. */
1802 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1804 dirp->offset = offset;
1808 /*******************************************************************
1809 Tell a dir position.
1810 ********************************************************************/
1812 long TellDir(struct smb_Dir *dirp)
1814 return(dirp->offset);
1817 /*******************************************************************
1818 Add an entry into the dcache.
1819 ********************************************************************/
1821 static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1823 struct name_cache_entry *e;
1825 if (dirp->name_cache_size == 0) {
1829 if (dirp->name_cache == NULL) {
1830 dirp->name_cache = talloc_zero_array(
1831 dirp, struct name_cache_entry, dirp->name_cache_size);
1833 if (dirp->name_cache == NULL) {
1838 dirp->name_cache_index = (dirp->name_cache_index+1) %
1839 dirp->name_cache_size;
1840 e = &dirp->name_cache[dirp->name_cache_index];
1841 TALLOC_FREE(e->name);
1842 e->name = talloc_strdup(dirp, name);
1846 /*******************************************************************
1847 Find an entry by name. Leave us at the offset after it.
1848 Don't check for veto or invisible files.
1849 ********************************************************************/
1851 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1854 const char *entry = NULL;
1855 char *talloced = NULL;
1856 connection_struct *conn = dirp->conn;
1858 /* Search back in the name cache. */
1859 if (dirp->name_cache_size && dirp->name_cache) {
1860 for (i = dirp->name_cache_index; i >= 0; i--) {
1861 struct name_cache_entry *e = &dirp->name_cache[i];
1862 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1863 *poffset = e->offset;
1864 SeekDir(dirp, e->offset);
1868 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1869 struct name_cache_entry *e = &dirp->name_cache[i];
1870 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1871 *poffset = e->offset;
1872 SeekDir(dirp, e->offset);
1878 /* Not found in the name cache. Rewind directory and start from scratch. */
1879 SMB_VFS_REWINDDIR(conn, dirp->dir);
1880 dirp->file_number = 0;
1881 *poffset = START_OF_DIRECTORY_OFFSET;
1882 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
1883 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1884 TALLOC_FREE(talloced);
1887 TALLOC_FREE(talloced);
1892 struct files_below_forall_state {
1895 int (*fn)(struct file_id fid, const struct share_mode_data *data,
1896 void *private_data);
1900 static int files_below_forall_fn(struct file_id fid,
1901 const struct share_mode_data *data,
1904 struct files_below_forall_state *state = private_data;
1905 char tmpbuf[PATH_MAX];
1906 char *fullpath, *to_free;
1909 len = full_path_tos(data->servicepath, data->base_name,
1910 tmpbuf, sizeof(tmpbuf),
1911 &fullpath, &to_free);
1915 if (state->dirpath_len >= len) {
1917 * Filter files above dirpath
1921 if (fullpath[state->dirpath_len] != '/') {
1923 * Filter file that don't have a path separator at the end of
1929 if (memcmp(state->dirpath, fullpath, state->dirpath_len) != 0) {
1936 return state->fn(fid, data, state->private_data);
1939 static int files_below_forall(connection_struct *conn,
1940 const struct smb_filename *dir_name,
1941 int (*fn)(struct file_id fid,
1942 const struct share_mode_data *data,
1943 void *private_data),
1946 struct files_below_forall_state state = {
1948 .private_data = private_data,
1951 char tmpbuf[PATH_MAX];
1954 state.dirpath_len = full_path_tos(conn->connectpath,
1955 dir_name->base_name,
1956 tmpbuf, sizeof(tmpbuf),
1957 &state.dirpath, &to_free);
1958 if (state.dirpath_len == -1) {
1962 ret = share_mode_forall(files_below_forall_fn, &state);
1963 TALLOC_FREE(to_free);
1967 struct have_file_open_below_state {
1971 static int have_file_open_below_fn(struct file_id fid,
1972 const struct share_mode_data *data,
1975 struct have_file_open_below_state *state = private_data;
1976 state->found_one = true;
1980 bool have_file_open_below(connection_struct *conn,
1981 const struct smb_filename *name)
1983 struct have_file_open_below_state state = {
1988 if (!VALID_STAT(name->st)) {
1991 if (!S_ISDIR(name->st.st_ex_mode)) {
1995 ret = files_below_forall(conn, name, have_file_open_below_fn, &state);
2000 return state.found_one;
2003 /*****************************************************************
2004 Is this directory empty ?
2005 *****************************************************************/
2007 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
2009 NTSTATUS status = NT_STATUS_OK;
2011 const char *dname = NULL;
2012 const char *dirname = fsp->fsp_name->base_name;
2013 char *talloced = NULL;
2015 struct connection_struct *conn = fsp->conn;
2016 struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
2023 return map_nt_error_from_unix(errno);
2026 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
2027 /* Quick check for "." and ".." */
2028 if (dname[0] == '.') {
2029 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
2030 TALLOC_FREE(talloced);
2035 if (!is_visible_file(conn, dirname, dname, &st, True)) {
2036 TALLOC_FREE(talloced);
2040 DEBUG(10,("got name %s - can't delete\n",
2042 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
2045 TALLOC_FREE(talloced);
2046 TALLOC_FREE(dir_hnd);
2048 if (!NT_STATUS_IS_OK(status)) {
2052 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) &&
2053 lp_strict_rename(SNUM(conn)) &&
2054 have_file_open_below(fsp->conn, fsp->fsp_name))
2056 return NT_STATUS_ACCESS_DENIED;
2059 return NT_STATUS_OK;