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;
55 struct smb_filename *dir_smb_fname;
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;
73 struct smb_filename *smb_dname;
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));
170 if (!(dptr->dir_hnd = OpenDir(NULL,
175 DEBUG(4,("dptr_get: Failed to "
177 dptr->smb_dname->base_name,
182 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
189 /****************************************************************************
190 Get the dir path for a dir index.
191 ****************************************************************************/
193 const char *dptr_path(struct smbd_server_connection *sconn, int key)
195 struct dptr_struct *dptr = dptr_get(sconn, key, false);
197 return(dptr->smb_dname->base_name);
201 /****************************************************************************
202 Get the dir wcard for a dir index.
203 ****************************************************************************/
205 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
207 struct dptr_struct *dptr = dptr_get(sconn, key, false);
213 /****************************************************************************
214 Get the dir attrib for a dir index.
215 ****************************************************************************/
217 uint16_t dptr_attr(struct smbd_server_connection *sconn, int key)
219 struct dptr_struct *dptr = dptr_get(sconn, key, false);
225 /****************************************************************************
226 Close a dptr (internal func).
227 ****************************************************************************/
229 static void dptr_close_internal(struct dptr_struct *dptr)
231 struct smbd_server_connection *sconn = dptr->conn->sconn;
233 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
239 if (sconn->using_smb2) {
243 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
246 * Free the dnum in the bitmap. Remember the dnum value is always
247 * biased by one with respect to the bitmap.
250 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
251 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
255 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
258 TALLOC_FREE(dptr->dir_hnd);
262 /****************************************************************************
263 Close a dptr given a key.
264 ****************************************************************************/
266 void dptr_close(struct smbd_server_connection *sconn, int *key)
268 struct dptr_struct *dptr;
270 if(*key == INVALID_DPTR_KEY)
273 /* OS/2 seems to use -1 to indicate "close all directories" */
275 struct dptr_struct *next;
276 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
278 dptr_close_internal(dptr);
280 *key = INVALID_DPTR_KEY;
284 dptr = dptr_get(sconn, *key, true);
287 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
291 dptr_close_internal(dptr);
293 *key = INVALID_DPTR_KEY;
296 /****************************************************************************
297 Close all dptrs for a cnum.
298 ****************************************************************************/
300 void dptr_closecnum(connection_struct *conn)
302 struct dptr_struct *dptr, *next;
303 struct smbd_server_connection *sconn = conn->sconn;
309 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
311 if (dptr->conn == conn) {
312 dptr_close_internal(dptr);
317 /****************************************************************************
318 Idle all dptrs for a cnum.
319 ****************************************************************************/
321 void dptr_idlecnum(connection_struct *conn)
323 struct dptr_struct *dptr;
324 struct smbd_server_connection *sconn = conn->sconn;
330 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
331 if (dptr->conn == conn && dptr->dir_hnd) {
337 /****************************************************************************
338 Close a dptr that matches a given path, only if it matches the spid also.
339 ****************************************************************************/
341 void dptr_closepath(struct smbd_server_connection *sconn,
342 char *path,uint16_t spid)
344 struct dptr_struct *dptr, *next;
345 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
347 if (spid == dptr->spid &&
348 strequal(dptr->smb_dname->base_name,path)) {
349 dptr_close_internal(dptr);
354 /****************************************************************************
355 Try and close the oldest handle not marked for
356 expect close in the hope that the client has
357 finished with that one.
358 ****************************************************************************/
360 static void dptr_close_oldest(struct smbd_server_connection *sconn,
363 struct dptr_struct *dptr;
366 * Go to the end of the list.
368 for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
372 DEBUG(0,("No old dptrs available to close oldest ?\n"));
377 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
378 * does not have expect_close set. If 'old' is false, close
379 * one of the new dnum handles.
382 for(; dptr; dptr = DLIST_PREV(dptr)) {
383 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
384 (!old && (dptr->dnum > 255))) {
385 dptr_close_internal(dptr);
391 /****************************************************************************
392 Safely do an OpenDir as root, ensuring we're in the right place.
393 ****************************************************************************/
395 static struct smb_Dir *open_dir_with_privilege(connection_struct *conn,
396 struct smb_request *req,
397 const struct smb_filename *smb_dname,
401 struct smb_Dir *dir_hnd = NULL;
402 struct smb_filename *smb_fname_cwd = NULL;
403 struct smb_filename saved_dir_fname = {0};
404 char *saved_dir = vfs_GetWd(talloc_tos(), conn);
405 struct privilege_paths *priv_paths = req->priv_paths;
408 if (saved_dir == NULL) {
412 saved_dir_fname = (struct smb_filename) { .base_name = saved_dir };
414 if (vfs_ChDir(conn, smb_dname) == -1) {
418 /* Now check the stat value is the same. */
419 smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
424 if (smb_fname_cwd == NULL) {
427 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
432 if (!check_same_stat(&smb_fname_cwd->st, &priv_paths->parent_name.st)) {
433 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
435 smb_dname->base_name,
436 smb_fname_str_dbg(&priv_paths->parent_name)));
440 dir_hnd = OpenDir(NULL, conn, smb_fname_cwd, wcard, attr);
444 vfs_ChDir(conn, &saved_dir_fname);
445 TALLOC_FREE(saved_dir);
449 /****************************************************************************
450 Create a new dir ptr. If the flag old_handle is true then we must allocate
451 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
452 one byte long. If old_handle is false we allocate from the range
453 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
454 a directory handle is never zero.
455 wcard must not be zero.
456 ****************************************************************************/
458 NTSTATUS dptr_create(connection_struct *conn,
459 struct smb_request *req,
461 const struct smb_filename *smb_dname,
468 struct dptr_struct **dptr_ret)
470 struct smbd_server_connection *sconn = conn->sconn;
471 struct dptr_struct *dptr = NULL;
472 struct smb_Dir *dir_hnd;
474 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
475 smb_dname = fsp->fsp_name;
478 DEBUG(5,("dptr_create dir=%s\n", smb_dname->base_name));
481 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
482 return NT_STATUS_INTERNAL_ERROR;
486 return NT_STATUS_INVALID_PARAMETER;
490 if (!(fsp->access_mask & SEC_DIR_LIST)) {
491 DEBUG(5,("dptr_create: directory %s "
492 "not open for LIST access\n",
493 smb_dname->base_name));
494 return NT_STATUS_ACCESS_DENIED;
496 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
499 bool backup_intent = (req && req->priv_paths);
501 struct smb_filename *smb_dname_cp =
502 cp_smb_filename(talloc_tos(), smb_dname);
504 if (smb_dname_cp == NULL) {
505 return NT_STATUS_NO_MEMORY;
508 if (req != NULL && req->posix_pathnames) {
509 ret = SMB_VFS_LSTAT(conn, smb_dname_cp);
511 ret = SMB_VFS_STAT(conn, smb_dname_cp);
514 status = map_nt_error_from_unix(errno);
515 TALLOC_FREE(smb_dname_cp);
518 if (!S_ISDIR(smb_dname_cp->st.st_ex_mode)) {
519 TALLOC_FREE(smb_dname_cp);
520 return NT_STATUS_NOT_A_DIRECTORY;
522 status = smbd_check_access_rights(conn,
526 if (!NT_STATUS_IS_OK(status)) {
527 TALLOC_FREE(smb_dname_cp);
531 dir_hnd = open_dir_with_privilege(conn,
537 dir_hnd = OpenDir(NULL,
543 TALLOC_FREE(smb_dname_cp);
547 return map_nt_error_from_unix(errno);
550 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
551 dptr_idleoldest(sconn);
554 dptr = talloc_zero(NULL, struct dptr_struct);
556 DEBUG(0,("talloc fail in dptr_create.\n"));
557 TALLOC_FREE(dir_hnd);
558 return NT_STATUS_NO_MEMORY;
561 dptr->smb_dname = cp_smb_filename(dptr, smb_dname);
562 if (!dptr->smb_dname) {
564 TALLOC_FREE(dir_hnd);
565 return NT_STATUS_NO_MEMORY;
568 dptr->dir_hnd = dir_hnd;
570 dptr->expect_close = expect_close;
571 dptr->wcard = talloc_strdup(dptr, wcard);
574 TALLOC_FREE(dir_hnd);
575 return NT_STATUS_NO_MEMORY;
577 if ((req != NULL && req->posix_pathnames) ||
578 (wcard[0] == '.' && wcard[1] == 0)) {
579 dptr->has_wild = True;
581 dptr->has_wild = wcard_has_wild;
586 if (sconn->using_smb2) {
593 * This is an old-style SMBsearch request. Ensure the
594 * value we return will fit in the range 1-255.
597 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
599 if(dptr->dnum == -1 || dptr->dnum > 254) {
602 * Try and close the oldest handle not marked for
603 * expect close in the hope that the client has
604 * finished with that one.
607 dptr_close_oldest(sconn, true);
609 /* Now try again... */
610 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
611 if(dptr->dnum == -1 || dptr->dnum > 254) {
612 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
614 TALLOC_FREE(dir_hnd);
615 return NT_STATUS_TOO_MANY_OPENED_FILES;
621 * This is a new-style trans2 request. Allocate from
622 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
625 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
627 if(dptr->dnum == -1 || dptr->dnum < 255) {
630 * Try and close the oldest handle close in the hope that
631 * the client has finished with that one. This will only
632 * happen in the case of the Win98 client bug where it leaks
636 dptr_close_oldest(sconn, false);
638 /* Now try again... */
639 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
641 if(dptr->dnum == -1 || dptr->dnum < 255) {
642 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
644 TALLOC_FREE(dir_hnd);
645 return NT_STATUS_TOO_MANY_OPENED_FILES;
650 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
652 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
654 DLIST_ADD(sconn->searches.dirptrs, dptr);
657 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
659 dptr->smb_dname->base_name,
668 /****************************************************************************
669 Wrapper functions to access the lower level directory handles.
670 ****************************************************************************/
672 void dptr_CloseDir(files_struct *fsp)
676 * The destructor for the struct smb_Dir
677 * (fsp->dptr->dir_hnd) now handles
678 * all resource deallocation.
680 dptr_close_internal(fsp->dptr);
685 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
687 SeekDir(dptr->dir_hnd, offset);
690 long dptr_TellDir(struct dptr_struct *dptr)
692 return TellDir(dptr->dir_hnd);
695 bool dptr_has_wild(struct dptr_struct *dptr)
697 return dptr->has_wild;
700 int dptr_dnum(struct dptr_struct *dptr)
705 bool dptr_get_priv(struct dptr_struct *dptr)
710 void dptr_set_priv(struct dptr_struct *dptr)
715 /****************************************************************************
716 Return the next visible file name, skipping veto'd and invisible files.
717 ****************************************************************************/
719 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
720 long *poffset, SMB_STRUCT_STAT *pst,
723 /* Normal search for the next file. */
725 char *talloced = NULL;
727 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
729 if (is_visible_file(dptr->conn,
730 dptr->smb_dname->base_name,
734 *ptalloced = talloced;
737 TALLOC_FREE(talloced);
742 /****************************************************************************
743 Return the next visible file name, skipping veto'd and invisible files.
744 ****************************************************************************/
746 static char *dptr_ReadDirName(TALLOC_CTX *ctx,
747 struct dptr_struct *dptr,
749 SMB_STRUCT_STAT *pst)
751 struct smb_filename smb_fname_base;
753 const char *name_temp = NULL;
754 char *talloced = NULL;
755 char *pathreal = NULL;
756 char *found_name = NULL;
759 SET_STAT_INVALID(*pst);
761 if (dptr->has_wild || dptr->did_stat) {
762 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
764 if (name_temp == NULL) {
767 if (talloced != NULL) {
768 return talloc_move(ctx, &talloced);
770 return talloc_strdup(ctx, name_temp);
773 /* If poffset is -1 then we know we returned this name before and we
774 * have no wildcards. We're at the end of the directory. */
775 if (*poffset == END_OF_DIRECTORY_OFFSET) {
779 /* We know the stored wcard contains no wildcard characters.
780 * See if we can match with a stat call. If we can't, then set
781 * did_stat to true to ensure we only do this once and keep
784 dptr->did_stat = true;
786 /* First check if it should be visible. */
787 if (!is_visible_file(dptr->conn,
788 dptr->smb_dname->base_name,
792 /* This only returns false if the file was found, but
793 is explicitly not visible. Set us to end of
794 directory, but return NULL as we know we can't ever
799 if (VALID_STAT(*pst)) {
800 name = talloc_strdup(ctx, dptr->wcard);
804 pathreal = talloc_asprintf(ctx,
806 dptr->smb_dname->base_name,
811 /* Create an smb_filename with stream_name == NULL. */
812 smb_fname_base = (struct smb_filename) { .base_name = pathreal };
814 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
815 *pst = smb_fname_base.st;
816 name = talloc_strdup(ctx, dptr->wcard);
819 /* If we get any other error than ENOENT or ENOTDIR
820 then the file exists we just can't stat it. */
821 if (errno != ENOENT && errno != ENOTDIR) {
822 name = talloc_strdup(ctx, dptr->wcard);
827 /* Stat failed. We know this is authoratiative if we are
828 * providing case sensitive semantics or the underlying
829 * filesystem is case sensitive.
831 if (dptr->conn->case_sensitive ||
832 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
838 * Try case-insensitive stat if the fs has the ability. This avoids
839 * scanning the whole directory.
841 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn,
842 dptr->smb_dname->base_name,
849 } else if (errno == ENOENT) {
850 /* The case-insensitive lookup was authoritative. */
854 TALLOC_FREE(pathreal);
856 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
857 if (name_temp == NULL) {
860 if (talloced != NULL) {
861 return talloc_move(ctx, &talloced);
863 return talloc_strdup(ctx, name_temp);
866 TALLOC_FREE(pathreal);
868 /* We need to set the underlying dir_hnd offset to -1
869 * also as this function is usually called with the
870 * output from TellDir. */
871 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
875 /****************************************************************************
876 Search for a file by name, skipping veto'ed and not visible files.
877 ****************************************************************************/
879 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
881 SET_STAT_INVALID(*pst);
883 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
884 /* This is a singleton directory and we're already at the end. */
885 *poffset = END_OF_DIRECTORY_OFFSET;
889 return SearchDir(dptr->dir_hnd, name, poffset);
892 /****************************************************************************
893 Initialize variables & state data at the beginning of all search SMB requests.
894 ****************************************************************************/
895 void dptr_init_search_op(struct dptr_struct *dptr)
897 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
900 /****************************************************************************
901 Map a native directory offset to a 32-bit cookie.
902 ****************************************************************************/
904 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
909 if (offset == END_OF_DIRECTORY_OFFSET) {
910 return WIRE_END_OF_DIRECTORY_OFFSET;
911 } else if(offset == START_OF_DIRECTORY_OFFSET) {
912 return WIRE_START_OF_DIRECTORY_OFFSET;
913 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
914 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
916 if (sizeof(long) == 4) {
917 /* 32-bit machine. We can cheat... */
918 return (uint32_t)offset;
920 if (dptr->dptr_cache == NULL) {
921 /* Lazy initialize cache. */
922 dptr->dptr_cache = memcache_init(dptr, 0);
923 if (dptr->dptr_cache == NULL) {
924 return WIRE_END_OF_DIRECTORY_OFFSET;
927 /* Have we seen this offset before ? */
928 key.data = (void *)&offset;
929 key.length = sizeof(offset);
930 if (memcache_lookup(dptr->dptr_cache,
931 SMB1_SEARCH_OFFSET_MAP,
934 uint32_t wire_offset;
935 SMB_ASSERT(val.length == sizeof(wire_offset));
936 memcpy(&wire_offset, val.data, sizeof(wire_offset));
937 DEBUG(10,("found wire %u <-> offset %ld\n",
938 (unsigned int)wire_offset,
943 /* Allocate a new wire cookie. */
946 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
947 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
948 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
949 /* Store it in the cache. */
950 key.data = (void *)&offset;
951 key.length = sizeof(offset);
952 val.data = (void *)&dptr->counter;
953 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
954 memcache_add(dptr->dptr_cache,
955 SMB1_SEARCH_OFFSET_MAP,
958 /* And the reverse mapping for lookup from
959 map_wire_to_dir_offset(). */
960 memcache_add(dptr->dptr_cache,
961 SMB1_SEARCH_OFFSET_MAP,
964 DEBUG(10,("stored wire %u <-> offset %ld\n",
965 (unsigned int)dptr->counter,
967 return dptr->counter;
970 /****************************************************************************
971 Fill the 5 byte server reserved dptr field.
972 ****************************************************************************/
974 bool dptr_fill(struct smbd_server_connection *sconn,
975 char *buf1,unsigned int key)
977 unsigned char *buf = (unsigned char *)buf1;
978 struct dptr_struct *dptr = dptr_get(sconn, key, false);
979 uint32_t wire_offset;
981 DEBUG(1,("filling null dirptr %d\n",key));
984 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
985 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
986 (long)dptr->dir_hnd,(int)wire_offset));
988 SIVAL(buf,1,wire_offset);
992 /****************************************************************************
993 Map a 32-bit wire cookie to a native directory offset.
994 ****************************************************************************/
996 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
1001 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
1002 return END_OF_DIRECTORY_OFFSET;
1003 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
1004 return START_OF_DIRECTORY_OFFSET;
1005 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
1006 return DOT_DOT_DIRECTORY_OFFSET;
1008 if (sizeof(long) == 4) {
1009 /* 32-bit machine. We can cheat... */
1010 return (long)wire_offset;
1012 if (dptr->dptr_cache == NULL) {
1013 /* Logic error, cache should be initialized. */
1014 return END_OF_DIRECTORY_OFFSET;
1016 key.data = (void *)&wire_offset;
1017 key.length = sizeof(wire_offset);
1018 if (memcache_lookup(dptr->dptr_cache,
1019 SMB1_SEARCH_OFFSET_MAP,
1022 /* Found mapping. */
1024 SMB_ASSERT(val.length == sizeof(offset));
1025 memcpy(&offset, val.data, sizeof(offset));
1026 DEBUG(10,("lookup wire %u <-> offset %ld\n",
1027 (unsigned int)wire_offset,
1031 return END_OF_DIRECTORY_OFFSET;
1034 /****************************************************************************
1035 Fetch the dir ptr and seek it given the 5 byte server field.
1036 ****************************************************************************/
1038 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
1039 char *buf, int *num)
1041 unsigned int key = *(unsigned char *)buf;
1042 struct dptr_struct *dptr = dptr_get(sconn, key, false);
1043 uint32_t wire_offset;
1047 DEBUG(3,("fetched null dirptr %d\n",key));
1051 wire_offset = IVAL(buf,1);
1052 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
1053 SeekDir(dptr->dir_hnd,seekoff);
1054 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1055 key, dptr->smb_dname->base_name, (int)seekoff));
1059 /****************************************************************************
1061 ****************************************************************************/
1063 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
1066 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
1069 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
1072 DEBUG(3,("fetching dirptr %d for path %s\n",
1074 dptr->smb_dname->base_name));
1078 static bool mangle_mask_match(connection_struct *conn,
1079 const char *filename,
1084 if (!name_to_8_3(filename,mname,False,conn->params)) {
1087 return mask_match_search(mname,mask,False);
1090 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
1091 struct dptr_struct *dirptr,
1096 bool (*match_fn)(TALLOC_CTX *ctx,
1101 bool (*mode_fn)(TALLOC_CTX *ctx,
1103 struct smb_filename *smb_fname,
1107 struct smb_filename **_smb_fname,
1111 connection_struct *conn = dirptr->conn;
1114 const char *dpath = dirptr->smb_dname->base_name;
1115 bool dirptr_path_is_dot = ISDOT(dpath);
1120 pathlen = strlen(dpath);
1121 slashlen = ( dpath[pathlen-1] != '/') ? 1 : 0;
1126 SMB_STRUCT_STAT sbuf = { 0 };
1130 char *pathreal = NULL;
1131 struct smb_filename smb_fname;
1135 cur_offset = dptr_TellDir(dirptr);
1136 prev_offset = cur_offset;
1137 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
1139 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1140 (long)dirptr, cur_offset));
1142 if (dname == NULL) {
1146 isdots = (ISDOT(dname) || ISDOTDOT(dname));
1147 if (dont_descend && !isdots) {
1153 * fname may get mangled, dname is never mangled.
1154 * Whenever we're accessing the filesystem we use
1155 * pathreal which is composed from dname.
1158 ok = match_fn(ctx, private_data, dname, mask, &fname);
1166 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1167 * needslash?"/":"", dname);
1168 * but this was measurably slower than doing the memcpy.
1171 pathreal = talloc_array(
1173 pathlen + slashlen + talloc_get_size(dname));
1181 * We don't want to pass ./xxx to modules below us so don't
1182 * add the path if it is just . by itself.
1184 if (dirptr_path_is_dot) {
1185 memcpy(pathreal, dname, talloc_get_size(dname));
1187 memcpy(pathreal, dpath, pathlen);
1188 pathreal[pathlen] = '/';
1189 memcpy(pathreal + slashlen + pathlen, dname,
1190 talloc_get_size(dname));
1193 /* Create smb_fname with NULL stream_name. */
1194 smb_fname = (struct smb_filename) {
1195 .base_name = pathreal, .st = sbuf
1198 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
1202 TALLOC_FREE(pathreal);
1206 if (!dir_check_ftype(mode, dirtype)) {
1207 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1208 fname, (unsigned int)mode, (unsigned int)dirtype));
1211 TALLOC_FREE(pathreal);
1215 if (ask_sharemode) {
1216 struct timespec write_time_ts;
1217 struct file_id fileid;
1219 fileid = vfs_file_id_from_sbuf(conn,
1221 get_file_infos(fileid, 0, NULL, &write_time_ts);
1222 if (!null_timespec(write_time_ts)) {
1223 update_stat_ex_mtime(&smb_fname.st,
1228 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1230 mask, smb_fname_str_dbg(&smb_fname),
1233 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1237 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
1238 TALLOC_FREE(pathreal);
1239 if (*_smb_fname == NULL) {
1244 *_prev_offset = prev_offset;
1252 /****************************************************************************
1253 Get an 8.3 directory entry.
1254 ****************************************************************************/
1256 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1262 connection_struct *conn = (connection_struct *)private_data;
1264 if ((strcmp(mask,"*.*") == 0) ||
1265 mask_match_search(dname, mask, false) ||
1266 mangle_mask_match(conn, dname, mask)) {
1270 * Ensure we can push the original name as UCS2. If
1271 * not, then just don't return this name.
1275 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
1276 uint8_t *tmp = talloc_array(talloc_tos(),
1280 status = srvstr_push(NULL,
1281 FLAGS2_UNICODE_STRINGS,
1290 if (!NT_STATUS_IS_OK(status)) {
1294 if (!mangle_is_8_3(dname, false, conn->params)) {
1295 bool ok = name_to_8_3(dname, mname, false,
1305 *_fname = talloc_strdup(ctx, fname);
1306 if (*_fname == NULL) {
1316 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1318 struct smb_filename *smb_fname,
1321 connection_struct *conn = (connection_struct *)private_data;
1323 if (!VALID_STAT(smb_fname->st)) {
1324 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1325 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1326 "Couldn't stat [%s]. Error "
1328 smb_fname_str_dbg(smb_fname),
1334 *_mode = dos_mode(conn, smb_fname);
1338 bool get_dir_entry(TALLOC_CTX *ctx,
1339 struct dptr_struct *dirptr,
1345 struct timespec *_date,
1349 connection_struct *conn = dirptr->conn;
1351 struct smb_filename *smb_fname = NULL;
1356 ok = smbd_dirptr_get_entry(ctx,
1362 smbd_dirptr_8_3_match_fn,
1363 smbd_dirptr_8_3_mode_fn,
1373 *_fname = talloc_move(ctx, &fname);
1374 *_size = smb_fname->st.st_ex_size;
1376 *_date = smb_fname->st.st_ex_mtime;
1377 TALLOC_FREE(smb_fname);
1381 /*******************************************************************
1382 Check to see if a user can read a file. This is only approximate,
1383 it is used as part of the "hide unreadable" option. Don't
1384 use it for anything security sensitive.
1385 ********************************************************************/
1387 static bool user_can_read_file(connection_struct *conn,
1388 struct smb_filename *smb_fname)
1391 uint32_t rejected_share_access = 0;
1392 uint32_t rejected_mask = 0;
1393 struct security_descriptor *sd = NULL;
1394 uint32_t access_mask = FILE_READ_DATA|
1396 FILE_READ_ATTRIBUTES|
1397 SEC_STD_READ_CONTROL;
1400 * Never hide files from the root user.
1401 * We use (uid_t)0 here not sec_initial_uid()
1402 * as make test uses a single user context.
1405 if (get_current_uid(conn) == (uid_t)0) {
1410 * We can't directly use smbd_check_access_rights()
1411 * here, as this implicitly grants FILE_READ_ATTRIBUTES
1412 * which the Windows access-based-enumeration code
1413 * explicitly checks for on the file security descriptor.
1416 * https://bugzilla.samba.org/show_bug.cgi?id=10252
1418 * and the smb2.acl2.ACCESSBASED test for details.
1421 rejected_share_access = access_mask & ~(conn->share_access);
1422 if (rejected_share_access) {
1423 DEBUG(10, ("rejected share access 0x%x "
1425 (unsigned int)access_mask,
1426 smb_fname_str_dbg(smb_fname),
1427 (unsigned int)rejected_share_access ));
1431 status = SMB_VFS_GET_NT_ACL(conn,
1439 if (!NT_STATUS_IS_OK(status)) {
1440 DEBUG(10, ("Could not get acl "
1442 smb_fname_str_dbg(smb_fname),
1443 nt_errstr(status)));
1447 status = se_file_access_check(sd,
1448 get_current_nttok(conn),
1455 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1456 DEBUG(10,("rejected bits 0x%x read access for %s\n",
1457 (unsigned int)rejected_mask,
1458 smb_fname_str_dbg(smb_fname) ));
1464 /*******************************************************************
1465 Check to see if a user can write a file (and only files, we do not
1466 check dirs on this one). This is only approximate,
1467 it is used as part of the "hide unwriteable" option. Don't
1468 use it for anything security sensitive.
1469 ********************************************************************/
1471 static bool user_can_write_file(connection_struct *conn,
1472 const struct smb_filename *smb_fname)
1475 * Never hide files from the root user.
1476 * We use (uid_t)0 here not sec_initial_uid()
1477 * as make test uses a single user context.
1480 if (get_current_uid(conn) == (uid_t)0) {
1484 SMB_ASSERT(VALID_STAT(smb_fname->st));
1486 /* Pseudo-open the file */
1488 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1492 return can_write_to_file(conn, smb_fname);
1495 /*******************************************************************
1496 Is a file a "special" type ?
1497 ********************************************************************/
1499 static bool file_is_special(connection_struct *conn,
1500 const struct smb_filename *smb_fname)
1503 * Never hide files from the root user.
1504 * We use (uid_t)0 here not sec_initial_uid()
1505 * as make test uses a single user context.
1508 if (get_current_uid(conn) == (uid_t)0) {
1512 SMB_ASSERT(VALID_STAT(smb_fname->st));
1514 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1515 S_ISDIR(smb_fname->st.st_ex_mode) ||
1516 S_ISLNK(smb_fname->st.st_ex_mode))
1522 /*******************************************************************
1523 Should the file be seen by the client?
1524 NOTE: A successful return is no guarantee of the file's existence.
1525 ********************************************************************/
1527 bool is_visible_file(connection_struct *conn, const char *dir_path,
1528 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1530 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1531 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1532 bool hide_special = lp_hide_special_files(SNUM(conn));
1534 struct smb_filename *smb_fname_base = NULL;
1537 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1538 return True; /* . and .. are always visible. */
1541 /* If it's a vetoed file, pretend it doesn't even exist */
1542 if (use_veto && IS_VETO_PATH(conn, name)) {
1543 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1547 if (hide_unreadable || hide_unwriteable || hide_special) {
1548 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1554 /* Create an smb_filename with stream_name == NULL. */
1555 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1560 if (smb_fname_base == NULL) {
1565 /* If the file name does not exist, there's no point checking
1566 * the configuration options. We succeed, on the basis that the
1567 * checks *might* have passed if the file was present.
1569 if (!VALID_STAT(*pst)) {
1570 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1574 *pst = smb_fname_base->st;
1577 /* Honour _hide unreadable_ option */
1578 if (hide_unreadable &&
1579 !user_can_read_file(conn, smb_fname_base)) {
1580 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1585 /* Honour _hide unwriteable_ option */
1586 if (hide_unwriteable && !user_can_write_file(conn,
1588 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1593 /* Honour _hide_special_ option */
1594 if (hide_special && file_is_special(conn, smb_fname_base)) {
1595 DEBUG(10,("is_visible_file: file %s is special.\n",
1604 TALLOC_FREE(smb_fname_base);
1609 static int smb_Dir_destructor(struct smb_Dir *dirp)
1611 if (dirp->dir != NULL) {
1612 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1613 if (dirp->fsp != NULL) {
1615 * The SMB_VFS_CLOSEDIR above
1616 * closes the underlying fd inside
1619 dirp->fsp->fh->fd = -1;
1620 if (dirp->fsp->dptr != NULL) {
1621 SMB_ASSERT(dirp->fsp->dptr->dir_hnd == dirp);
1622 dirp->fsp->dptr->dir_hnd = NULL;
1627 if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
1628 dirp->conn->sconn->searches.dirhandles_open--;
1633 /*******************************************************************
1635 ********************************************************************/
1637 static struct smb_Dir *OpenDir_internal(TALLOC_CTX *mem_ctx,
1638 connection_struct *conn,
1639 const struct smb_filename *smb_dname,
1643 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1644 struct smbd_server_connection *sconn = conn->sconn;
1650 dirp->dir = SMB_VFS_OPENDIR(conn, smb_dname, mask, attr);
1653 DEBUG(5,("OpenDir: Can't open %s. %s\n",
1654 smb_dname->base_name,
1660 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1662 if (sconn && !sconn->using_smb2) {
1663 sconn->searches.dirhandles_open++;
1665 talloc_set_destructor(dirp, smb_Dir_destructor);
1674 /****************************************************************************
1675 Open a directory handle by pathname, ensuring it's under the share path.
1676 ****************************************************************************/
1678 static struct smb_Dir *open_dir_safely(TALLOC_CTX *ctx,
1679 connection_struct *conn,
1680 const struct smb_filename *smb_dname,
1684 struct smb_Dir *dir_hnd = NULL;
1685 struct smb_filename *smb_fname_cwd = NULL;
1686 struct smb_filename saved_dir_fname = {0};
1687 char *saved_dir = vfs_GetWd(ctx, conn);
1690 if (saved_dir == NULL) {
1694 saved_dir_fname = (struct smb_filename) { .base_name = saved_dir };
1696 if (vfs_ChDir(conn, smb_dname) == -1) {
1700 smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
1705 if (smb_fname_cwd == NULL) {
1710 * Now the directory is pinned, use
1711 * REALPATH to ensure we can access it.
1713 status = check_name(conn, ".");
1714 if (!NT_STATUS_IS_OK(status)) {
1718 dir_hnd = OpenDir_internal(ctx,
1724 if (dir_hnd == NULL) {
1729 * OpenDir_internal only gets "." as the dir name.
1730 * Store the real dir name here.
1733 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, smb_dname);
1734 if (!dir_hnd->dir_smb_fname) {
1735 TALLOC_FREE(dir_hnd);
1741 vfs_ChDir(conn, &saved_dir_fname);
1742 TALLOC_FREE(saved_dir);
1746 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1747 const struct smb_filename *smb_dname,
1751 return open_dir_safely(mem_ctx,
1758 /*******************************************************************
1759 Open a directory from an fsp.
1760 ********************************************************************/
1762 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1767 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1768 struct smbd_server_connection *sconn = conn->sconn;
1774 if (!fsp->is_directory) {
1779 if (fsp->fh->fd == -1) {
1785 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1787 dirp->dir_smb_fname = cp_smb_filename(dirp, fsp->fsp_name);
1788 if (!dirp->dir_smb_fname) {
1793 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1794 if (dirp->dir != NULL) {
1797 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1799 dirp->dir_smb_fname->base_name,
1801 if (errno != ENOSYS) {
1806 if (dirp->dir == NULL) {
1807 /* FDOPENDIR is not supported. Use OPENDIR instead. */
1809 return open_dir_safely(mem_ctx,
1816 if (sconn && !sconn->using_smb2) {
1817 sconn->searches.dirhandles_open++;
1819 talloc_set_destructor(dirp, smb_Dir_destructor);
1829 /*******************************************************************
1830 Read from a directory.
1831 Return directory entry, current offset, and optional stat information.
1832 Don't check for veto or invisible files.
1833 ********************************************************************/
1835 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1836 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1839 char *talloced = NULL;
1840 connection_struct *conn = dirp->conn;
1842 /* Cheat to allow . and .. to be the first entries returned. */
1843 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1844 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1846 if (dirp->file_number == 0) {
1848 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1851 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1853 dirp->file_number++;
1858 if (*poffset == END_OF_DIRECTORY_OFFSET) {
1859 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1863 /* A real offset, seek to it. */
1864 SeekDir(dirp, *poffset);
1866 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1867 /* Ignore . and .. - we've already returned them. */
1869 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1870 TALLOC_FREE(talloced);
1874 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1875 *ptalloced = talloced;
1876 dirp->file_number++;
1879 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1884 /*******************************************************************
1885 Rewind to the start.
1886 ********************************************************************/
1888 void RewindDir(struct smb_Dir *dirp, long *poffset)
1890 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1891 dirp->file_number = 0;
1892 dirp->offset = START_OF_DIRECTORY_OFFSET;
1893 *poffset = START_OF_DIRECTORY_OFFSET;
1896 /*******************************************************************
1898 ********************************************************************/
1900 void SeekDir(struct smb_Dir *dirp, long offset)
1902 if (offset != dirp->offset) {
1903 if (offset == START_OF_DIRECTORY_OFFSET) {
1904 RewindDir(dirp, &offset);
1906 * Ok we should really set the file number here
1907 * to 1 to enable ".." to be returned next. Trouble
1908 * is I'm worried about callers using SeekDir(dirp,0)
1909 * as equivalent to RewindDir(). So leave this alone
1912 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1913 RewindDir(dirp, &offset);
1915 * Set the file number to 2 - we want to get the first
1916 * real file entry (the one we return after "..")
1917 * on the next ReadDir.
1919 dirp->file_number = 2;
1920 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1921 ; /* Don't seek in this case. */
1923 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1925 dirp->offset = offset;
1929 /*******************************************************************
1930 Tell a dir position.
1931 ********************************************************************/
1933 long TellDir(struct smb_Dir *dirp)
1935 return(dirp->offset);
1938 /*******************************************************************
1939 Add an entry into the dcache.
1940 ********************************************************************/
1942 static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1944 struct name_cache_entry *e;
1946 if (dirp->name_cache_size == 0) {
1950 if (dirp->name_cache == NULL) {
1951 dirp->name_cache = talloc_zero_array(
1952 dirp, struct name_cache_entry, dirp->name_cache_size);
1954 if (dirp->name_cache == NULL) {
1959 dirp->name_cache_index = (dirp->name_cache_index+1) %
1960 dirp->name_cache_size;
1961 e = &dirp->name_cache[dirp->name_cache_index];
1962 TALLOC_FREE(e->name);
1963 e->name = talloc_strdup(dirp, name);
1967 /*******************************************************************
1968 Find an entry by name. Leave us at the offset after it.
1969 Don't check for veto or invisible files.
1970 ********************************************************************/
1972 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1975 const char *entry = NULL;
1976 char *talloced = NULL;
1977 connection_struct *conn = dirp->conn;
1979 /* Search back in the name cache. */
1980 if (dirp->name_cache_size && dirp->name_cache) {
1981 for (i = dirp->name_cache_index; i >= 0; i--) {
1982 struct name_cache_entry *e = &dirp->name_cache[i];
1983 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1984 *poffset = e->offset;
1985 SeekDir(dirp, e->offset);
1989 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1990 struct name_cache_entry *e = &dirp->name_cache[i];
1991 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1992 *poffset = e->offset;
1993 SeekDir(dirp, e->offset);
1999 /* Not found in the name cache. Rewind directory and start from scratch. */
2000 SMB_VFS_REWINDDIR(conn, dirp->dir);
2001 dirp->file_number = 0;
2002 *poffset = START_OF_DIRECTORY_OFFSET;
2003 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
2004 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
2005 TALLOC_FREE(talloced);
2008 TALLOC_FREE(talloced);
2013 struct files_below_forall_state {
2016 int (*fn)(struct file_id fid, const struct share_mode_data *data,
2017 void *private_data);
2021 static int files_below_forall_fn(struct file_id fid,
2022 const struct share_mode_data *data,
2025 struct files_below_forall_state *state = private_data;
2026 char tmpbuf[PATH_MAX];
2027 char *fullpath, *to_free;
2030 len = full_path_tos(data->servicepath, data->base_name,
2031 tmpbuf, sizeof(tmpbuf),
2032 &fullpath, &to_free);
2036 if (state->dirpath_len >= len) {
2038 * Filter files above dirpath
2042 if (fullpath[state->dirpath_len] != '/') {
2044 * Filter file that don't have a path separator at the end of
2050 if (memcmp(state->dirpath, fullpath, state->dirpath_len) != 0) {
2057 return state->fn(fid, data, state->private_data);
2060 static int files_below_forall(connection_struct *conn,
2061 const struct smb_filename *dir_name,
2062 int (*fn)(struct file_id fid,
2063 const struct share_mode_data *data,
2064 void *private_data),
2067 struct files_below_forall_state state = {
2069 .private_data = private_data,
2072 char tmpbuf[PATH_MAX];
2075 state.dirpath_len = full_path_tos(conn->connectpath,
2076 dir_name->base_name,
2077 tmpbuf, sizeof(tmpbuf),
2078 &state.dirpath, &to_free);
2079 if (state.dirpath_len == -1) {
2083 ret = share_mode_forall(files_below_forall_fn, &state);
2084 TALLOC_FREE(to_free);
2088 struct have_file_open_below_state {
2092 static int have_file_open_below_fn(struct file_id fid,
2093 const struct share_mode_data *data,
2096 struct have_file_open_below_state *state = private_data;
2097 state->found_one = true;
2101 bool have_file_open_below(connection_struct *conn,
2102 const struct smb_filename *name)
2104 struct have_file_open_below_state state = {
2109 if (!VALID_STAT(name->st)) {
2112 if (!S_ISDIR(name->st.st_ex_mode)) {
2116 ret = files_below_forall(conn, name, have_file_open_below_fn, &state);
2121 return state.found_one;
2124 /*****************************************************************
2125 Is this directory empty ?
2126 *****************************************************************/
2128 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
2130 NTSTATUS status = NT_STATUS_OK;
2132 const char *dname = NULL;
2133 const char *dirname = fsp->fsp_name->base_name;
2134 char *talloced = NULL;
2136 struct connection_struct *conn = fsp->conn;
2137 struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
2144 return map_nt_error_from_unix(errno);
2147 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
2148 /* Quick check for "." and ".." */
2149 if (dname[0] == '.') {
2150 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
2151 TALLOC_FREE(talloced);
2156 if (!is_visible_file(conn, dirname, dname, &st, True)) {
2157 TALLOC_FREE(talloced);
2161 DEBUG(10,("got name %s - can't delete\n",
2163 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
2166 TALLOC_FREE(talloced);
2167 TALLOC_FREE(dir_hnd);
2169 if (!NT_STATUS_IS_OK(status)) {
2173 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) &&
2174 lp_strict_rename(SNUM(conn)) &&
2175 have_file_open_below(fsp->conn, fsp->fsp_name))
2177 return NT_STATUS_ACCESS_DENIED;
2180 return NT_STATUS_OK;