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(). */
62 bool fallback_opendir;
66 struct dptr_struct *next, *prev;
69 struct connection_struct *conn;
70 struct smb_Dir *dir_hnd;
74 struct smb_filename *smb_dname;
75 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
76 bool did_stat; /* Optimisation for non-wcard searches. */
77 bool priv; /* Directory handle opened with privilege. */
79 struct memcache *dptr_cache;
82 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
87 static void DirCacheAdd(struct smb_Dir *dir_hnd, const char *name, long offset);
89 static struct smb_Dir *open_dir_safely(TALLOC_CTX *ctx,
90 connection_struct *conn,
91 const struct smb_filename *smb_dname,
94 static int smb_Dir_destructor(struct smb_Dir *dir_hnd);
96 #define INVALID_DPTR_KEY (-3)
98 /****************************************************************************
99 Initialise the dir bitmap.
100 ****************************************************************************/
102 bool init_dptrs(struct smbd_server_connection *sconn)
104 if (sconn->searches.dptr_bmap) {
108 sconn->searches.dptr_bmap = bitmap_talloc(
109 sconn, MAX_DIRECTORY_HANDLES);
111 if (sconn->searches.dptr_bmap == NULL) {
118 /****************************************************************************
119 Get the struct dptr_struct for a dir index.
120 ****************************************************************************/
122 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
125 struct dptr_struct *dptr;
127 for (dptr = sconn->searches.dirptrs; dptr != NULL; dptr = dptr->next) {
128 if(dptr->dnum != key) {
131 DLIST_PROMOTE(sconn->searches.dirptrs, dptr);
137 /****************************************************************************
138 Get the dir path for a dir index.
139 ****************************************************************************/
141 const char *dptr_path(struct smbd_server_connection *sconn, int key)
143 struct dptr_struct *dptr = dptr_get(sconn, key);
145 return(dptr->smb_dname->base_name);
149 /****************************************************************************
150 Get the dir wcard for a dir index.
151 ****************************************************************************/
153 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
155 struct dptr_struct *dptr = dptr_get(sconn, key);
161 /****************************************************************************
162 Get the dir attrib for a dir index.
163 ****************************************************************************/
165 uint16_t dptr_attr(struct smbd_server_connection *sconn, int key)
167 struct dptr_struct *dptr = dptr_get(sconn, key);
173 /****************************************************************************
174 Get the fsp stored in a dptr->dir_hnd.
175 ****************************************************************************/
177 files_struct *dptr_fsp(struct smbd_server_connection *sconn, int key)
179 struct dptr_struct *dptr = dptr_get(sconn, key);
183 if (dptr->dir_hnd == NULL) {
186 return dptr->dir_hnd->fsp;
189 /****************************************************************************
190 Close a dptr (internal func).
191 ****************************************************************************/
193 static void dptr_close_internal(struct dptr_struct *dptr)
195 struct smbd_server_connection *sconn = dptr->conn->sconn;
197 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
203 if (sconn->using_smb2) {
207 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
210 * Free the dnum in the bitmap. Remember the dnum value is always
211 * biased by one with respect to the bitmap.
214 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
215 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
219 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
222 TALLOC_FREE(dptr->dir_hnd);
226 /****************************************************************************
227 Close a dptr given a key. SMB1 *only*.
228 ****************************************************************************/
230 void dptr_close(struct smbd_server_connection *sconn, int *key)
232 struct dptr_struct *dptr;
233 files_struct *fsp = NULL;
234 struct smb_Dir *dir_hnd = NULL;
236 SMB_ASSERT(!sconn->using_smb2);
238 if(*key == INVALID_DPTR_KEY)
241 dptr = dptr_get(sconn, *key);
244 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
248 dir_hnd = dptr->dir_hnd;
250 if (dir_hnd != NULL && dir_hnd->fsp != NULL) {
251 SMB_ASSERT(dir_hnd->fsp->dptr->dir_hnd == dir_hnd);
255 dptr_close_internal(dptr);
261 *key = INVALID_DPTR_KEY;
264 /****************************************************************************
265 Close all dptrs for a cnum.
266 ****************************************************************************/
268 void dptr_closecnum(connection_struct *conn)
270 struct dptr_struct *dptr, *next;
271 struct smbd_server_connection *sconn = conn->sconn;
277 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
279 if (dptr->conn == conn) {
280 int key = dptr->dnum;
281 files_struct *fsp = NULL;
283 if (dptr->dir_hnd != NULL &&
284 dptr->dir_hnd->fsp != NULL) {
285 fsp = dptr->dir_hnd->fsp;
287 dptr_close(sconn, &key);
289 close_file(NULL, fsp, NORMAL_CLOSE);
296 /****************************************************************************
297 Create a new dir ptr. If the flag old_handle is true then we must allocate
298 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
299 one byte long. If old_handle is false we allocate from the range
300 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
301 a directory handle is never zero.
302 wcard must not be zero.
303 ****************************************************************************/
305 NTSTATUS dptr_create(connection_struct *conn,
306 struct smb_request *req,
308 const struct smb_filename *smb_dname,
315 struct dptr_struct **dptr_ret)
317 struct smbd_server_connection *sconn = conn->sconn;
318 struct dptr_struct *dptr = NULL;
319 struct smb_Dir *dir_hnd;
321 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
322 smb_dname = fsp->fsp_name;
325 DEBUG(5,("dptr_create dir=%s\n", smb_dname->base_name));
328 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
329 return NT_STATUS_INTERNAL_ERROR;
333 return NT_STATUS_INVALID_PARAMETER;
336 if (!(fsp->access_mask & SEC_DIR_LIST)) {
337 DBG_INFO("dptr_create: directory %s "
338 "not open for LIST access\n",
339 smb_dname->base_name);
340 return NT_STATUS_ACCESS_DENIED;
342 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
344 return map_nt_error_from_unix(errno);
347 dptr = talloc_zero(NULL, struct dptr_struct);
349 DEBUG(0,("talloc fail in dptr_create.\n"));
350 TALLOC_FREE(dir_hnd);
351 return NT_STATUS_NO_MEMORY;
354 dptr->smb_dname = cp_smb_filename(dptr, smb_dname);
355 if (!dptr->smb_dname) {
357 TALLOC_FREE(dir_hnd);
358 return NT_STATUS_NO_MEMORY;
361 dptr->dir_hnd = dir_hnd;
363 dptr->expect_close = expect_close;
364 dptr->wcard = talloc_strdup(dptr, wcard);
367 TALLOC_FREE(dir_hnd);
368 return NT_STATUS_NO_MEMORY;
370 if ((req != NULL && req->posix_pathnames) ||
371 (wcard[0] == '.' && wcard[1] == 0)) {
372 dptr->has_wild = True;
374 dptr->has_wild = wcard_has_wild;
379 if (sconn->using_smb2) {
386 * This is an old-style SMBsearch request. Ensure the
387 * value we return will fit in the range 1-255.
390 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
392 if(dptr->dnum == -1 || dptr->dnum > 254) {
393 DBG_ERR("returned %d: Error - all old "
394 "dirptrs in use ?\n",
397 TALLOC_FREE(dir_hnd);
398 return NT_STATUS_TOO_MANY_OPENED_FILES;
403 * This is a new-style trans2 request. Allocate from
404 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
407 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
409 if(dptr->dnum == -1 || dptr->dnum < 255) {
410 DBG_ERR("returned %d: Error - all new "
411 "dirptrs in use ?\n",
414 TALLOC_FREE(dir_hnd);
415 return NT_STATUS_TOO_MANY_OPENED_FILES;
419 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
421 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
423 DLIST_ADD(sconn->searches.dirptrs, dptr);
426 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
428 dptr->smb_dname->base_name,
437 /****************************************************************************
438 Wrapper functions to access the lower level directory handles.
439 ****************************************************************************/
441 void dptr_CloseDir(files_struct *fsp)
445 * The destructor for the struct smb_Dir
446 * (fsp->dptr->dir_hnd) now handles
447 * all resource deallocation.
449 dptr_close_internal(fsp->dptr);
454 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
456 SeekDir(dptr->dir_hnd, offset);
459 long dptr_TellDir(struct dptr_struct *dptr)
461 return TellDir(dptr->dir_hnd);
464 bool dptr_has_wild(struct dptr_struct *dptr)
466 return dptr->has_wild;
469 int dptr_dnum(struct dptr_struct *dptr)
474 bool dptr_get_priv(struct dptr_struct *dptr)
479 void dptr_set_priv(struct dptr_struct *dptr)
484 /****************************************************************************
485 Return the next visible file name, skipping veto'd and invisible files.
486 ****************************************************************************/
488 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
489 long *poffset, SMB_STRUCT_STAT *pst,
492 /* Normal search for the next file. */
494 char *talloced = NULL;
496 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
498 if (is_visible_file(dptr->conn,
499 dptr->smb_dname->base_name,
503 *ptalloced = talloced;
506 TALLOC_FREE(talloced);
511 /****************************************************************************
512 Return the next visible file name, skipping veto'd and invisible files.
513 ****************************************************************************/
515 static char *dptr_ReadDirName(TALLOC_CTX *ctx,
516 struct dptr_struct *dptr,
518 SMB_STRUCT_STAT *pst)
520 struct smb_filename smb_fname_base;
522 const char *name_temp = NULL;
523 char *talloced = NULL;
524 char *pathreal = NULL;
525 char *found_name = NULL;
528 SET_STAT_INVALID(*pst);
530 if (dptr->has_wild || dptr->did_stat) {
531 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
533 if (name_temp == NULL) {
536 if (talloced != NULL) {
537 return talloc_move(ctx, &talloced);
539 return talloc_strdup(ctx, name_temp);
542 /* If poffset is -1 then we know we returned this name before and we
543 * have no wildcards. We're at the end of the directory. */
544 if (*poffset == END_OF_DIRECTORY_OFFSET) {
548 /* We know the stored wcard contains no wildcard characters.
549 * See if we can match with a stat call. If we can't, then set
550 * did_stat to true to ensure we only do this once and keep
553 dptr->did_stat = true;
555 /* First check if it should be visible. */
556 if (!is_visible_file(dptr->conn,
557 dptr->smb_dname->base_name,
561 /* This only returns false if the file was found, but
562 is explicitly not visible. Set us to end of
563 directory, but return NULL as we know we can't ever
568 if (VALID_STAT(*pst)) {
569 name = talloc_strdup(ctx, dptr->wcard);
573 pathreal = talloc_asprintf(ctx,
575 dptr->smb_dname->base_name,
580 /* Create an smb_filename with stream_name == NULL. */
581 smb_fname_base = (struct smb_filename) { .base_name = pathreal };
583 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
584 *pst = smb_fname_base.st;
585 name = talloc_strdup(ctx, dptr->wcard);
588 /* If we get any other error than ENOENT or ENOTDIR
589 then the file exists we just can't stat it. */
590 if (errno != ENOENT && errno != ENOTDIR) {
591 name = talloc_strdup(ctx, dptr->wcard);
596 /* Stat failed. We know this is authoratiative if we are
597 * providing case sensitive semantics or the underlying
598 * filesystem is case sensitive.
600 if (dptr->conn->case_sensitive ||
601 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
607 * Try case-insensitive stat if the fs has the ability. This avoids
608 * scanning the whole directory.
610 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn,
611 dptr->smb_dname->base_name,
618 } else if (errno == ENOENT) {
619 /* The case-insensitive lookup was authoritative. */
623 TALLOC_FREE(pathreal);
625 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
626 if (name_temp == NULL) {
629 if (talloced != NULL) {
630 return talloc_move(ctx, &talloced);
632 return talloc_strdup(ctx, name_temp);
635 TALLOC_FREE(pathreal);
637 /* We need to set the underlying dir_hnd offset to -1
638 * also as this function is usually called with the
639 * output from TellDir. */
640 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
644 /****************************************************************************
645 Search for a file by name, skipping veto'ed and not visible files.
646 ****************************************************************************/
648 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
650 SET_STAT_INVALID(*pst);
652 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
653 /* This is a singleton directory and we're already at the end. */
654 *poffset = END_OF_DIRECTORY_OFFSET;
658 return SearchDir(dptr->dir_hnd, name, poffset);
661 /****************************************************************************
662 Map a native directory offset to a 32-bit cookie.
663 ****************************************************************************/
665 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
670 if (offset == END_OF_DIRECTORY_OFFSET) {
671 return WIRE_END_OF_DIRECTORY_OFFSET;
672 } else if(offset == START_OF_DIRECTORY_OFFSET) {
673 return WIRE_START_OF_DIRECTORY_OFFSET;
674 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
675 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
677 if (sizeof(long) == 4) {
678 /* 32-bit machine. We can cheat... */
679 return (uint32_t)offset;
681 if (dptr->dptr_cache == NULL) {
682 /* Lazy initialize cache. */
683 dptr->dptr_cache = memcache_init(dptr, 0);
684 if (dptr->dptr_cache == NULL) {
685 return WIRE_END_OF_DIRECTORY_OFFSET;
688 /* Have we seen this offset before ? */
689 key.data = (void *)&offset;
690 key.length = sizeof(offset);
691 if (memcache_lookup(dptr->dptr_cache,
692 SMB1_SEARCH_OFFSET_MAP,
695 uint32_t wire_offset;
696 SMB_ASSERT(val.length == sizeof(wire_offset));
697 memcpy(&wire_offset, val.data, sizeof(wire_offset));
698 DEBUG(10,("found wire %u <-> offset %ld\n",
699 (unsigned int)wire_offset,
704 /* Allocate a new wire cookie. */
707 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
708 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
709 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
710 /* Store it in the cache. */
711 key.data = (void *)&offset;
712 key.length = sizeof(offset);
713 val.data = (void *)&dptr->counter;
714 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
715 memcache_add(dptr->dptr_cache,
716 SMB1_SEARCH_OFFSET_MAP,
719 /* And the reverse mapping for lookup from
720 map_wire_to_dir_offset(). */
721 memcache_add(dptr->dptr_cache,
722 SMB1_SEARCH_OFFSET_MAP,
725 DEBUG(10,("stored wire %u <-> offset %ld\n",
726 (unsigned int)dptr->counter,
728 return dptr->counter;
731 /****************************************************************************
732 Fill the 5 byte server reserved dptr field.
733 ****************************************************************************/
735 bool dptr_fill(struct smbd_server_connection *sconn,
736 char *buf1,unsigned int key)
738 unsigned char *buf = (unsigned char *)buf1;
739 struct dptr_struct *dptr = dptr_get(sconn, key);
740 uint32_t wire_offset;
742 DEBUG(1,("filling null dirptr %d\n",key));
745 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
746 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
747 (long)dptr->dir_hnd,(int)wire_offset));
749 SIVAL(buf,1,wire_offset);
753 /****************************************************************************
754 Map a 32-bit wire cookie to a native directory offset.
755 ****************************************************************************/
757 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
762 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
763 return END_OF_DIRECTORY_OFFSET;
764 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
765 return START_OF_DIRECTORY_OFFSET;
766 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
767 return DOT_DOT_DIRECTORY_OFFSET;
769 if (sizeof(long) == 4) {
770 /* 32-bit machine. We can cheat... */
771 return (long)wire_offset;
773 if (dptr->dptr_cache == NULL) {
774 /* Logic error, cache should be initialized. */
775 return END_OF_DIRECTORY_OFFSET;
777 key.data = (void *)&wire_offset;
778 key.length = sizeof(wire_offset);
779 if (memcache_lookup(dptr->dptr_cache,
780 SMB1_SEARCH_OFFSET_MAP,
785 SMB_ASSERT(val.length == sizeof(offset));
786 memcpy(&offset, val.data, sizeof(offset));
787 DEBUG(10,("lookup wire %u <-> offset %ld\n",
788 (unsigned int)wire_offset,
792 return END_OF_DIRECTORY_OFFSET;
795 /****************************************************************************
796 Return the associated fsp and seek the dir_hnd on it it given the 5 byte
798 ****************************************************************************/
800 files_struct *dptr_fetch_fsp(struct smbd_server_connection *sconn,
803 unsigned int key = *(unsigned char *)buf;
804 struct dptr_struct *dptr = dptr_get(sconn, key);
805 uint32_t wire_offset;
809 DEBUG(3,("fetched null dirptr %d\n",key));
813 wire_offset = IVAL(buf,1);
814 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
815 SeekDir(dptr->dir_hnd,seekoff);
816 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
817 key, dptr->smb_dname->base_name, (int)seekoff));
818 return dptr->dir_hnd->fsp;
821 /****************************************************************************
822 Fetch the fsp associated with the dptr_num.
823 ****************************************************************************/
825 files_struct *dptr_fetch_lanman2_fsp(struct smbd_server_connection *sconn,
828 struct dptr_struct *dptr = dptr_get(sconn, dptr_num);
832 DBG_NOTICE("fetching dirptr %d for path %s\n",
834 dptr->smb_dname->base_name);
835 return dptr->dir_hnd->fsp;
838 static bool mangle_mask_match(connection_struct *conn,
839 const char *filename,
844 if (!name_to_8_3(filename,mname,False,conn->params)) {
847 return mask_match_search(mname,mask,False);
850 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
851 struct dptr_struct *dirptr,
857 bool (*match_fn)(TALLOC_CTX *ctx,
862 bool (*mode_fn)(TALLOC_CTX *ctx,
864 struct smb_filename *smb_fname,
869 struct smb_filename **_smb_fname,
873 connection_struct *conn = dirptr->conn;
876 const char *dpath = dirptr->smb_dname->base_name;
877 bool dirptr_path_is_dot = ISDOT(dpath);
882 pathlen = strlen(dpath);
883 slashlen = ( dpath[pathlen-1] != '/') ? 1 : 0;
888 SMB_STRUCT_STAT sbuf = { 0 };
892 char *pathreal = NULL;
893 struct smb_filename smb_fname;
897 cur_offset = dptr_TellDir(dirptr);
898 prev_offset = cur_offset;
899 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
901 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
902 (long)dirptr, cur_offset));
908 isdots = (ISDOT(dname) || ISDOTDOT(dname));
909 if (dont_descend && !isdots) {
915 * fname may get mangled, dname is never mangled.
916 * Whenever we're accessing the filesystem we use
917 * pathreal which is composed from dname.
920 ok = match_fn(ctx, private_data, dname, mask, &fname);
928 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
929 * needslash?"/":"", dname);
930 * but this was measurably slower than doing the memcpy.
933 pathreal = talloc_array(
935 pathlen + slashlen + talloc_get_size(dname));
943 * We don't want to pass ./xxx to modules below us so don't
944 * add the path if it is just . by itself.
946 if (dirptr_path_is_dot) {
947 memcpy(pathreal, dname, talloc_get_size(dname));
949 memcpy(pathreal, dpath, pathlen);
950 pathreal[pathlen] = '/';
951 memcpy(pathreal + slashlen + pathlen, dname,
952 talloc_get_size(dname));
955 /* Create smb_fname with NULL stream_name. */
956 smb_fname = (struct smb_filename) {
957 .base_name = pathreal, .st = sbuf
960 ok = mode_fn(ctx, private_data, &smb_fname, get_dosmode, &mode);
964 TALLOC_FREE(pathreal);
968 if (!dir_check_ftype(mode, dirtype)) {
969 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
970 fname, (unsigned int)mode, (unsigned int)dirtype));
973 TALLOC_FREE(pathreal);
978 struct timespec write_time_ts;
979 struct file_id fileid;
981 fileid = vfs_file_id_from_sbuf(conn,
983 get_file_infos(fileid, 0, NULL, &write_time_ts);
984 if (!null_timespec(write_time_ts)) {
985 update_stat_ex_mtime(&smb_fname.st,
990 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
992 mask, smb_fname_str_dbg(&smb_fname),
995 if (!conn->sconn->using_smb2) {
997 * The dircache is only needed for SMB1 because SMB1
998 * uses a name for the resume wheras SMB2 always
999 * continues from the next position (unless it's told to
1000 * restart or close-and-reopen the listing).
1002 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1007 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
1008 TALLOC_FREE(pathreal);
1009 if (*_smb_fname == NULL) {
1014 *_prev_offset = prev_offset;
1022 /****************************************************************************
1023 Get an 8.3 directory entry.
1024 ****************************************************************************/
1026 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1032 connection_struct *conn = (connection_struct *)private_data;
1034 if ((strcmp(mask,"*.*") == 0) ||
1035 mask_match_search(dname, mask, false) ||
1036 mangle_mask_match(conn, dname, mask)) {
1040 * Ensure we can push the original name as UCS2. If
1041 * not, then just don't return this name.
1045 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
1046 uint8_t *tmp = talloc_array(talloc_tos(),
1050 status = srvstr_push(NULL,
1051 FLAGS2_UNICODE_STRINGS,
1060 if (!NT_STATUS_IS_OK(status)) {
1064 if (!mangle_is_8_3(dname, false, conn->params)) {
1065 bool ok = name_to_8_3(dname, mname, false,
1075 *_fname = talloc_strdup(ctx, fname);
1076 if (*_fname == NULL) {
1086 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1088 struct smb_filename *smb_fname,
1092 connection_struct *conn = (connection_struct *)private_data;
1094 if (!VALID_STAT(smb_fname->st)) {
1095 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1096 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1097 "Couldn't stat [%s]. Error "
1099 smb_fname_str_dbg(smb_fname),
1105 *_mode = dos_mode(conn, smb_fname);
1109 bool get_dir_entry(TALLOC_CTX *ctx,
1110 struct dptr_struct *dirptr,
1116 struct timespec *_date,
1120 connection_struct *conn = dirptr->conn;
1122 struct smb_filename *smb_fname = NULL;
1127 ok = smbd_dirptr_get_entry(ctx,
1134 smbd_dirptr_8_3_match_fn,
1135 smbd_dirptr_8_3_mode_fn,
1145 *_fname = talloc_move(ctx, &fname);
1146 *_size = smb_fname->st.st_ex_size;
1148 *_date = smb_fname->st.st_ex_mtime;
1149 TALLOC_FREE(smb_fname);
1153 /*******************************************************************
1154 Check to see if a user can read a file. This is only approximate,
1155 it is used as part of the "hide unreadable" option. Don't
1156 use it for anything security sensitive.
1157 ********************************************************************/
1159 static bool user_can_read_file(connection_struct *conn,
1160 struct smb_filename *smb_fname)
1163 uint32_t rejected_share_access = 0;
1164 uint32_t rejected_mask = 0;
1165 struct security_descriptor *sd = NULL;
1166 uint32_t access_mask = FILE_READ_DATA|
1168 FILE_READ_ATTRIBUTES|
1169 SEC_STD_READ_CONTROL;
1172 * Never hide files from the root user.
1173 * We use (uid_t)0 here not sec_initial_uid()
1174 * as make test uses a single user context.
1177 if (get_current_uid(conn) == (uid_t)0) {
1182 * We can't directly use smbd_check_access_rights()
1183 * here, as this implicitly grants FILE_READ_ATTRIBUTES
1184 * which the Windows access-based-enumeration code
1185 * explicitly checks for on the file security descriptor.
1188 * https://bugzilla.samba.org/show_bug.cgi?id=10252
1190 * and the smb2.acl2.ACCESSBASED test for details.
1193 rejected_share_access = access_mask & ~(conn->share_access);
1194 if (rejected_share_access) {
1195 DEBUG(10, ("rejected share access 0x%x "
1197 (unsigned int)access_mask,
1198 smb_fname_str_dbg(smb_fname),
1199 (unsigned int)rejected_share_access ));
1203 status = SMB_VFS_GET_NT_ACL(conn,
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(10, ("Could not get acl "
1214 smb_fname_str_dbg(smb_fname),
1215 nt_errstr(status)));
1219 status = se_file_access_check(sd,
1220 get_current_nttok(conn),
1227 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1228 DEBUG(10,("rejected bits 0x%x read access for %s\n",
1229 (unsigned int)rejected_mask,
1230 smb_fname_str_dbg(smb_fname) ));
1236 /*******************************************************************
1237 Check to see if a user can write a file (and only files, we do not
1238 check dirs on this one). This is only approximate,
1239 it is used as part of the "hide unwriteable" option. Don't
1240 use it for anything security sensitive.
1241 ********************************************************************/
1243 static bool user_can_write_file(connection_struct *conn,
1244 const struct smb_filename *smb_fname)
1247 * Never hide files from the root user.
1248 * We use (uid_t)0 here not sec_initial_uid()
1249 * as make test uses a single user context.
1252 if (get_current_uid(conn) == (uid_t)0) {
1256 SMB_ASSERT(VALID_STAT(smb_fname->st));
1258 /* Pseudo-open the file */
1260 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1264 return can_write_to_file(conn, smb_fname);
1267 /*******************************************************************
1268 Is a file a "special" type ?
1269 ********************************************************************/
1271 static bool file_is_special(connection_struct *conn,
1272 const struct smb_filename *smb_fname)
1275 * Never hide files from the root user.
1276 * We use (uid_t)0 here not sec_initial_uid()
1277 * as make test uses a single user context.
1280 if (get_current_uid(conn) == (uid_t)0) {
1284 SMB_ASSERT(VALID_STAT(smb_fname->st));
1286 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1287 S_ISDIR(smb_fname->st.st_ex_mode) ||
1288 S_ISLNK(smb_fname->st.st_ex_mode))
1294 /*******************************************************************
1295 Should the file be seen by the client?
1296 NOTE: A successful return is no guarantee of the file's existence.
1297 ********************************************************************/
1299 bool is_visible_file(connection_struct *conn, const char *dir_path,
1300 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1302 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1303 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1304 bool hide_special = lp_hide_special_files(SNUM(conn));
1305 int hide_new_files_timeout = lp_hide_new_files_timeout(SNUM(conn));
1307 struct smb_filename *smb_fname_base = NULL;
1310 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1311 return True; /* . and .. are always visible. */
1314 /* If it's a vetoed file, pretend it doesn't even exist */
1315 if (use_veto && IS_VETO_PATH(conn, name)) {
1316 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1320 if (hide_unreadable ||
1323 (hide_new_files_timeout != 0))
1325 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1331 /* Create an smb_filename with stream_name == NULL. */
1332 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1337 if (smb_fname_base == NULL) {
1342 /* If the file name does not exist, there's no point checking
1343 * the configuration options. We succeed, on the basis that the
1344 * checks *might* have passed if the file was present.
1346 if (!VALID_STAT(*pst)) {
1347 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1351 *pst = smb_fname_base->st;
1354 /* Honour _hide unreadable_ option */
1355 if (hide_unreadable &&
1356 !user_can_read_file(conn, smb_fname_base)) {
1357 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1362 /* Honour _hide unwriteable_ option */
1363 if (hide_unwriteable && !user_can_write_file(conn,
1365 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1370 /* Honour _hide_special_ option */
1371 if (hide_special && file_is_special(conn, smb_fname_base)) {
1372 DEBUG(10,("is_visible_file: file %s is special.\n",
1378 if (hide_new_files_timeout != 0) {
1380 double age = timespec_elapsed(
1381 &smb_fname_base->st.st_ex_mtime);
1383 if (age < (double)hide_new_files_timeout) {
1392 TALLOC_FREE(smb_fname_base);
1397 static int smb_Dir_destructor(struct smb_Dir *dir_hnd)
1399 files_struct *fsp = dir_hnd->fsp;
1401 SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
1403 * The SMB_VFS_CLOSEDIR above
1404 * closes the underlying fd inside
1405 * dirp->fsp, unless fallback_opendir
1406 * was set in which case the fd
1407 * in dir_hnd->fsp->fh->fd isn't
1408 * the one being closed. Close
1411 if (dir_hnd->fallback_opendir) {
1415 SMB_ASSERT(fsp->dptr->dir_hnd == dir_hnd);
1416 fsp->dptr->dir_hnd = NULL;
1417 dir_hnd->fsp = NULL;
1421 /*******************************************************************
1423 ********************************************************************/
1425 static struct smb_Dir *OpenDir_internal(TALLOC_CTX *mem_ctx,
1426 connection_struct *conn,
1427 const struct smb_filename *smb_dname,
1431 struct smb_Dir *dir_hnd = talloc_zero(mem_ctx, struct smb_Dir);
1437 dir_hnd->dir = SMB_VFS_OPENDIR(conn, smb_dname, mask, attr);
1439 if (!dir_hnd->dir) {
1440 DEBUG(5,("OpenDir: Can't open %s. %s\n",
1441 smb_dname->base_name,
1446 dir_hnd->conn = conn;
1448 if (!conn->sconn->using_smb2) {
1450 * The dircache is only needed for SMB1 because SMB1 uses a name
1451 * for the resume wheras SMB2 always continues from the next
1452 * position (unless it's told to restart or close-and-reopen the
1455 dir_hnd->name_cache_size =
1456 lp_directory_name_cache_size(SNUM(conn));
1462 TALLOC_FREE(dir_hnd);
1467 * @brief Open a directory handle by pathname, ensuring it's under the share path.
1469 * First stores the $cwd, then changes directory to the passed in pathname
1470 * uses check_name() to ensure this is under the connection struct share path,
1471 * then operates on a pathname of "." to ensure we're in the same place.
1473 * The returned struct smb_Dir * should have a talloc destrctor added to
1474 * ensure that when the struct is freed the internal POSIX DIR * pointer
1479 * static int sample_smb_Dir_destructor(struct smb_Dir *dirp)
1481 * SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1484 * struct smb_Dir *dir_hnd = open_dir_safely(mem_ctx,
1489 * if (dir_hnd == NULL) {
1492 * talloc_set_destructor(dir_hnd, smb_Dir_destructor);
1497 static struct smb_Dir *open_dir_safely(TALLOC_CTX *ctx,
1498 connection_struct *conn,
1499 const struct smb_filename *smb_dname,
1503 struct smb_Dir *dir_hnd = NULL;
1504 struct smb_filename *smb_fname_cwd = NULL;
1505 struct smb_filename *saved_dir_fname = vfs_GetWd(ctx, conn);
1508 if (saved_dir_fname == NULL) {
1512 if (vfs_ChDir(conn, smb_dname) == -1) {
1516 smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
1521 if (smb_fname_cwd == NULL) {
1526 * Now the directory is pinned, use
1527 * REALPATH to ensure we can access it.
1529 status = check_name(conn, smb_fname_cwd);
1530 if (!NT_STATUS_IS_OK(status)) {
1534 dir_hnd = OpenDir_internal(ctx,
1540 if (dir_hnd == NULL) {
1545 * OpenDir_internal only gets "." as the dir name.
1546 * Store the real dir name here.
1549 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, smb_dname);
1550 if (!dir_hnd->dir_smb_fname) {
1551 TALLOC_FREE(dir_hnd);
1552 SMB_VFS_CLOSEDIR(conn, dir_hnd->dir);
1558 vfs_ChDir(conn, saved_dir_fname);
1559 TALLOC_FREE(saved_dir_fname);
1564 * Simple destructor for OpenDir() use. Don't need to
1565 * care about fsp back pointer as we know we have never
1566 * set it in the OpenDir() code path.
1569 static int smb_Dir_OpenDir_destructor(struct smb_Dir *dir_hnd)
1571 SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
1575 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1576 const struct smb_filename *smb_dname,
1580 struct smb_Dir *dir_hnd = open_dir_safely(mem_ctx,
1585 if (dir_hnd == NULL) {
1588 talloc_set_destructor(dir_hnd, smb_Dir_OpenDir_destructor);
1592 /*******************************************************************
1593 Open a directory from an fsp.
1594 ********************************************************************/
1596 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1601 struct smb_Dir *dir_hnd = talloc_zero(mem_ctx, struct smb_Dir);
1607 if (!fsp->is_directory) {
1612 if (fsp->fh->fd == -1) {
1617 dir_hnd->conn = conn;
1619 if (!conn->sconn->using_smb2) {
1621 * The dircache is only needed for SMB1 because SMB1 uses a name
1622 * for the resume wheras SMB2 always continues from the next
1623 * position (unless it's told to restart or close-and-reopen the
1626 dir_hnd->name_cache_size =
1627 lp_directory_name_cache_size(SNUM(conn));
1630 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, fsp->fsp_name);
1631 if (!dir_hnd->dir_smb_fname) {
1636 dir_hnd->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1637 if (dir_hnd->dir != NULL) {
1640 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1642 dir_hnd->dir_smb_fname->base_name,
1644 if (errno != ENOSYS) {
1649 if (dir_hnd->dir == NULL) {
1650 /* FDOPENDIR is not supported. Use OPENDIR instead. */
1651 TALLOC_FREE(dir_hnd);
1652 dir_hnd = open_dir_safely(mem_ctx,
1657 if (dir_hnd == NULL) {
1662 * Remember if we used the fallback.
1663 * We need to change the destructor
1664 * to also close the fsp file descriptor
1665 * in this case as it isn't the same
1666 * one the directory handle uses.
1669 dir_hnd->fallback_opendir = true;
1672 talloc_set_destructor(dir_hnd, smb_Dir_destructor);
1677 TALLOC_FREE(dir_hnd);
1682 /*******************************************************************
1683 Read from a directory.
1684 Return directory entry, current offset, and optional stat information.
1685 Don't check for veto or invisible files.
1686 ********************************************************************/
1688 const char *ReadDirName(struct smb_Dir *dir_hnd, long *poffset,
1689 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1692 char *talloced = NULL;
1693 connection_struct *conn = dir_hnd->conn;
1695 /* Cheat to allow . and .. to be the first entries returned. */
1696 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1697 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dir_hnd->file_number < 2))
1699 if (dir_hnd->file_number == 0) {
1701 *poffset = dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1704 *poffset = dir_hnd->offset = DOT_DOT_DIRECTORY_OFFSET;
1706 dir_hnd->file_number++;
1711 if (*poffset == END_OF_DIRECTORY_OFFSET) {
1712 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1716 /* A real offset, seek to it. */
1717 SeekDir(dir_hnd, *poffset);
1719 while ((n = vfs_readdirname(conn, dir_hnd->dir, sbuf, &talloced))) {
1720 /* Ignore . and .. - we've already returned them. */
1722 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1723 TALLOC_FREE(talloced);
1727 *poffset = dir_hnd->offset = SMB_VFS_TELLDIR(conn, dir_hnd->dir);
1728 *ptalloced = talloced;
1729 dir_hnd->file_number++;
1732 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1737 /*******************************************************************
1738 Rewind to the start.
1739 ********************************************************************/
1741 void RewindDir(struct smb_Dir *dir_hnd, long *poffset)
1743 SMB_VFS_REWINDDIR(dir_hnd->conn, dir_hnd->dir);
1744 dir_hnd->file_number = 0;
1745 dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1746 *poffset = START_OF_DIRECTORY_OFFSET;
1749 /*******************************************************************
1751 ********************************************************************/
1753 void SeekDir(struct smb_Dir *dirp, long offset)
1755 if (offset != dirp->offset) {
1756 if (offset == START_OF_DIRECTORY_OFFSET) {
1757 RewindDir(dirp, &offset);
1759 * Ok we should really set the file number here
1760 * to 1 to enable ".." to be returned next. Trouble
1761 * is I'm worried about callers using SeekDir(dirp,0)
1762 * as equivalent to RewindDir(). So leave this alone
1765 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1766 RewindDir(dirp, &offset);
1768 * Set the file number to 2 - we want to get the first
1769 * real file entry (the one we return after "..")
1770 * on the next ReadDir.
1772 dirp->file_number = 2;
1773 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1774 ; /* Don't seek in this case. */
1776 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1778 dirp->offset = offset;
1782 /*******************************************************************
1783 Tell a dir position.
1784 ********************************************************************/
1786 long TellDir(struct smb_Dir *dir_hnd)
1788 return(dir_hnd->offset);
1791 /*******************************************************************
1792 Add an entry into the dcache.
1793 ********************************************************************/
1795 static void DirCacheAdd(struct smb_Dir *dir_hnd, const char *name, long offset)
1797 struct name_cache_entry *e;
1799 if (dir_hnd->name_cache_size == 0) {
1803 if (dir_hnd->name_cache == NULL) {
1804 dir_hnd->name_cache = talloc_zero_array(dir_hnd,
1805 struct name_cache_entry,
1806 dir_hnd->name_cache_size);
1808 if (dir_hnd->name_cache == NULL) {
1813 dir_hnd->name_cache_index = (dir_hnd->name_cache_index+1) %
1814 dir_hnd->name_cache_size;
1815 e = &dir_hnd->name_cache[dir_hnd->name_cache_index];
1816 TALLOC_FREE(e->name);
1817 e->name = talloc_strdup(dir_hnd, name);
1821 /*******************************************************************
1822 Find an entry by name. Leave us at the offset after it.
1823 Don't check for veto or invisible files.
1824 ********************************************************************/
1826 bool SearchDir(struct smb_Dir *dir_hnd, const char *name, long *poffset)
1829 const char *entry = NULL;
1830 char *talloced = NULL;
1831 connection_struct *conn = dir_hnd->conn;
1833 /* Search back in the name cache. */
1834 if (dir_hnd->name_cache_size && dir_hnd->name_cache) {
1835 for (i = dir_hnd->name_cache_index; i >= 0; i--) {
1836 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1837 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1838 *poffset = e->offset;
1839 SeekDir(dir_hnd, e->offset);
1843 for (i = dir_hnd->name_cache_size - 1;
1844 i > dir_hnd->name_cache_index; i--) {
1845 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1846 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1847 *poffset = e->offset;
1848 SeekDir(dir_hnd, e->offset);
1854 /* Not found in the name cache. Rewind directory and start from scratch. */
1855 SMB_VFS_REWINDDIR(conn, dir_hnd->dir);
1856 dir_hnd->file_number = 0;
1857 *poffset = START_OF_DIRECTORY_OFFSET;
1858 while ((entry = ReadDirName(dir_hnd, poffset, NULL, &talloced))) {
1859 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1860 TALLOC_FREE(talloced);
1863 TALLOC_FREE(talloced);
1868 struct files_below_forall_state {
1871 int (*fn)(struct file_id fid, const struct share_mode_data *data,
1872 void *private_data);
1876 static int files_below_forall_fn(struct file_id fid,
1877 const struct share_mode_data *data,
1880 struct files_below_forall_state *state = private_data;
1881 char tmpbuf[PATH_MAX];
1882 char *fullpath, *to_free;
1885 len = full_path_tos(data->servicepath, data->base_name,
1886 tmpbuf, sizeof(tmpbuf),
1887 &fullpath, &to_free);
1891 if (state->dirpath_len >= len) {
1893 * Filter files above dirpath
1897 if (fullpath[state->dirpath_len] != '/') {
1899 * Filter file that don't have a path separator at the end of
1905 if (memcmp(state->dirpath, fullpath, state->dirpath_len) != 0) {
1912 TALLOC_FREE(to_free);
1913 return state->fn(fid, data, state->private_data);
1916 TALLOC_FREE(to_free);
1920 static int files_below_forall(connection_struct *conn,
1921 const struct smb_filename *dir_name,
1922 int (*fn)(struct file_id fid,
1923 const struct share_mode_data *data,
1924 void *private_data),
1927 struct files_below_forall_state state = {
1929 .private_data = private_data,
1932 char tmpbuf[PATH_MAX];
1935 state.dirpath_len = full_path_tos(conn->connectpath,
1936 dir_name->base_name,
1937 tmpbuf, sizeof(tmpbuf),
1938 &state.dirpath, &to_free);
1939 if (state.dirpath_len == -1) {
1943 ret = share_mode_forall(files_below_forall_fn, &state);
1944 TALLOC_FREE(to_free);
1948 struct have_file_open_below_state {
1952 static int have_file_open_below_fn(struct file_id fid,
1953 const struct share_mode_data *data,
1956 struct have_file_open_below_state *state = private_data;
1957 state->found_one = true;
1961 bool have_file_open_below(connection_struct *conn,
1962 const struct smb_filename *name)
1964 struct have_file_open_below_state state = {
1969 if (!VALID_STAT(name->st)) {
1972 if (!S_ISDIR(name->st.st_ex_mode)) {
1976 ret = files_below_forall(conn, name, have_file_open_below_fn, &state);
1981 return state.found_one;
1984 /*****************************************************************
1985 Is this directory empty ?
1986 *****************************************************************/
1988 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1990 NTSTATUS status = NT_STATUS_OK;
1992 const char *dname = NULL;
1993 const char *dirname = fsp->fsp_name->base_name;
1994 char *talloced = NULL;
1996 struct connection_struct *conn = fsp->conn;
1997 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(),
2004 return map_nt_error_from_unix(errno);
2007 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
2008 /* Quick check for "." and ".." */
2009 if (dname[0] == '.') {
2010 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
2011 TALLOC_FREE(talloced);
2016 if (!is_visible_file(conn, dirname, dname, &st, True)) {
2017 TALLOC_FREE(talloced);
2021 DEBUG(10,("got name %s - can't delete\n",
2023 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
2026 TALLOC_FREE(talloced);
2027 TALLOC_FREE(dir_hnd);
2029 if (!NT_STATUS_IS_OK(status)) {
2033 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) &&
2034 lp_strict_rename(SNUM(conn)) &&
2035 have_file_open_below(fsp->conn, fsp->fsp_name))
2037 return NT_STATUS_ACCESS_DENIED;
2040 return NT_STATUS_OK;