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 Fetch the dir ptr and seek it given the 5 byte server field.
797 ****************************************************************************/
799 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
802 unsigned int key = *(unsigned char *)buf;
803 struct dptr_struct *dptr = dptr_get(sconn, key);
804 uint32_t wire_offset;
808 DEBUG(3,("fetched null dirptr %d\n",key));
812 wire_offset = IVAL(buf,1);
813 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
814 SeekDir(dptr->dir_hnd,seekoff);
815 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
816 key, dptr->smb_dname->base_name, (int)seekoff));
820 /****************************************************************************
822 ****************************************************************************/
824 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
827 struct dptr_struct *dptr = dptr_get(sconn, dptr_num);
830 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
833 DEBUG(3,("fetching dirptr %d for path %s\n",
835 dptr->smb_dname->base_name));
839 static bool mangle_mask_match(connection_struct *conn,
840 const char *filename,
845 if (!name_to_8_3(filename,mname,False,conn->params)) {
848 return mask_match_search(mname,mask,False);
851 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
852 struct dptr_struct *dirptr,
858 bool (*match_fn)(TALLOC_CTX *ctx,
863 bool (*mode_fn)(TALLOC_CTX *ctx,
865 struct smb_filename *smb_fname,
870 struct smb_filename **_smb_fname,
874 connection_struct *conn = dirptr->conn;
877 const char *dpath = dirptr->smb_dname->base_name;
878 bool dirptr_path_is_dot = ISDOT(dpath);
883 pathlen = strlen(dpath);
884 slashlen = ( dpath[pathlen-1] != '/') ? 1 : 0;
889 SMB_STRUCT_STAT sbuf = { 0 };
893 char *pathreal = NULL;
894 struct smb_filename smb_fname;
898 cur_offset = dptr_TellDir(dirptr);
899 prev_offset = cur_offset;
900 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
902 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
903 (long)dirptr, cur_offset));
909 isdots = (ISDOT(dname) || ISDOTDOT(dname));
910 if (dont_descend && !isdots) {
916 * fname may get mangled, dname is never mangled.
917 * Whenever we're accessing the filesystem we use
918 * pathreal which is composed from dname.
921 ok = match_fn(ctx, private_data, dname, mask, &fname);
929 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
930 * needslash?"/":"", dname);
931 * but this was measurably slower than doing the memcpy.
934 pathreal = talloc_array(
936 pathlen + slashlen + talloc_get_size(dname));
944 * We don't want to pass ./xxx to modules below us so don't
945 * add the path if it is just . by itself.
947 if (dirptr_path_is_dot) {
948 memcpy(pathreal, dname, talloc_get_size(dname));
950 memcpy(pathreal, dpath, pathlen);
951 pathreal[pathlen] = '/';
952 memcpy(pathreal + slashlen + pathlen, dname,
953 talloc_get_size(dname));
956 /* Create smb_fname with NULL stream_name. */
957 smb_fname = (struct smb_filename) {
958 .base_name = pathreal, .st = sbuf
961 ok = mode_fn(ctx, private_data, &smb_fname, get_dosmode, &mode);
965 TALLOC_FREE(pathreal);
969 if (!dir_check_ftype(mode, dirtype)) {
970 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
971 fname, (unsigned int)mode, (unsigned int)dirtype));
974 TALLOC_FREE(pathreal);
979 struct timespec write_time_ts;
980 struct file_id fileid;
982 fileid = vfs_file_id_from_sbuf(conn,
984 get_file_infos(fileid, 0, NULL, &write_time_ts);
985 if (!null_timespec(write_time_ts)) {
986 update_stat_ex_mtime(&smb_fname.st,
991 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
993 mask, smb_fname_str_dbg(&smb_fname),
996 if (!conn->sconn->using_smb2) {
998 * The dircache is only needed for SMB1 because SMB1
999 * uses a name for the resume wheras SMB2 always
1000 * continues from the next position (unless it's told to
1001 * restart or close-and-reopen the listing).
1003 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1008 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
1009 TALLOC_FREE(pathreal);
1010 if (*_smb_fname == NULL) {
1015 *_prev_offset = prev_offset;
1023 /****************************************************************************
1024 Get an 8.3 directory entry.
1025 ****************************************************************************/
1027 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1033 connection_struct *conn = (connection_struct *)private_data;
1035 if ((strcmp(mask,"*.*") == 0) ||
1036 mask_match_search(dname, mask, false) ||
1037 mangle_mask_match(conn, dname, mask)) {
1041 * Ensure we can push the original name as UCS2. If
1042 * not, then just don't return this name.
1046 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
1047 uint8_t *tmp = talloc_array(talloc_tos(),
1051 status = srvstr_push(NULL,
1052 FLAGS2_UNICODE_STRINGS,
1061 if (!NT_STATUS_IS_OK(status)) {
1065 if (!mangle_is_8_3(dname, false, conn->params)) {
1066 bool ok = name_to_8_3(dname, mname, false,
1076 *_fname = talloc_strdup(ctx, fname);
1077 if (*_fname == NULL) {
1087 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1089 struct smb_filename *smb_fname,
1093 connection_struct *conn = (connection_struct *)private_data;
1095 if (!VALID_STAT(smb_fname->st)) {
1096 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1097 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1098 "Couldn't stat [%s]. Error "
1100 smb_fname_str_dbg(smb_fname),
1106 *_mode = dos_mode(conn, smb_fname);
1110 bool get_dir_entry(TALLOC_CTX *ctx,
1111 struct dptr_struct *dirptr,
1117 struct timespec *_date,
1121 connection_struct *conn = dirptr->conn;
1123 struct smb_filename *smb_fname = NULL;
1128 ok = smbd_dirptr_get_entry(ctx,
1135 smbd_dirptr_8_3_match_fn,
1136 smbd_dirptr_8_3_mode_fn,
1146 *_fname = talloc_move(ctx, &fname);
1147 *_size = smb_fname->st.st_ex_size;
1149 *_date = smb_fname->st.st_ex_mtime;
1150 TALLOC_FREE(smb_fname);
1154 /*******************************************************************
1155 Check to see if a user can read a file. This is only approximate,
1156 it is used as part of the "hide unreadable" option. Don't
1157 use it for anything security sensitive.
1158 ********************************************************************/
1160 static bool user_can_read_file(connection_struct *conn,
1161 struct smb_filename *smb_fname)
1164 uint32_t rejected_share_access = 0;
1165 uint32_t rejected_mask = 0;
1166 struct security_descriptor *sd = NULL;
1167 uint32_t access_mask = FILE_READ_DATA|
1169 FILE_READ_ATTRIBUTES|
1170 SEC_STD_READ_CONTROL;
1173 * Never hide files from the root user.
1174 * We use (uid_t)0 here not sec_initial_uid()
1175 * as make test uses a single user context.
1178 if (get_current_uid(conn) == (uid_t)0) {
1183 * We can't directly use smbd_check_access_rights()
1184 * here, as this implicitly grants FILE_READ_ATTRIBUTES
1185 * which the Windows access-based-enumeration code
1186 * explicitly checks for on the file security descriptor.
1189 * https://bugzilla.samba.org/show_bug.cgi?id=10252
1191 * and the smb2.acl2.ACCESSBASED test for details.
1194 rejected_share_access = access_mask & ~(conn->share_access);
1195 if (rejected_share_access) {
1196 DEBUG(10, ("rejected share access 0x%x "
1198 (unsigned int)access_mask,
1199 smb_fname_str_dbg(smb_fname),
1200 (unsigned int)rejected_share_access ));
1204 status = SMB_VFS_GET_NT_ACL(conn,
1212 if (!NT_STATUS_IS_OK(status)) {
1213 DEBUG(10, ("Could not get acl "
1215 smb_fname_str_dbg(smb_fname),
1216 nt_errstr(status)));
1220 status = se_file_access_check(sd,
1221 get_current_nttok(conn),
1228 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1229 DEBUG(10,("rejected bits 0x%x read access for %s\n",
1230 (unsigned int)rejected_mask,
1231 smb_fname_str_dbg(smb_fname) ));
1237 /*******************************************************************
1238 Check to see if a user can write a file (and only files, we do not
1239 check dirs on this one). This is only approximate,
1240 it is used as part of the "hide unwriteable" option. Don't
1241 use it for anything security sensitive.
1242 ********************************************************************/
1244 static bool user_can_write_file(connection_struct *conn,
1245 const struct smb_filename *smb_fname)
1248 * Never hide files from the root user.
1249 * We use (uid_t)0 here not sec_initial_uid()
1250 * as make test uses a single user context.
1253 if (get_current_uid(conn) == (uid_t)0) {
1257 SMB_ASSERT(VALID_STAT(smb_fname->st));
1259 /* Pseudo-open the file */
1261 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1265 return can_write_to_file(conn, smb_fname);
1268 /*******************************************************************
1269 Is a file a "special" type ?
1270 ********************************************************************/
1272 static bool file_is_special(connection_struct *conn,
1273 const struct smb_filename *smb_fname)
1276 * Never hide files from the root user.
1277 * We use (uid_t)0 here not sec_initial_uid()
1278 * as make test uses a single user context.
1281 if (get_current_uid(conn) == (uid_t)0) {
1285 SMB_ASSERT(VALID_STAT(smb_fname->st));
1287 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1288 S_ISDIR(smb_fname->st.st_ex_mode) ||
1289 S_ISLNK(smb_fname->st.st_ex_mode))
1295 /*******************************************************************
1296 Should the file be seen by the client?
1297 NOTE: A successful return is no guarantee of the file's existence.
1298 ********************************************************************/
1300 bool is_visible_file(connection_struct *conn, const char *dir_path,
1301 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1303 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1304 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1305 bool hide_special = lp_hide_special_files(SNUM(conn));
1306 int hide_new_files_timeout = lp_hide_new_files_timeout(SNUM(conn));
1308 struct smb_filename *smb_fname_base = NULL;
1311 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1312 return True; /* . and .. are always visible. */
1315 /* If it's a vetoed file, pretend it doesn't even exist */
1316 if (use_veto && IS_VETO_PATH(conn, name)) {
1317 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1321 if (hide_unreadable ||
1324 (hide_new_files_timeout != 0))
1326 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1332 /* Create an smb_filename with stream_name == NULL. */
1333 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1338 if (smb_fname_base == NULL) {
1343 /* If the file name does not exist, there's no point checking
1344 * the configuration options. We succeed, on the basis that the
1345 * checks *might* have passed if the file was present.
1347 if (!VALID_STAT(*pst)) {
1348 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1352 *pst = smb_fname_base->st;
1355 /* Honour _hide unreadable_ option */
1356 if (hide_unreadable &&
1357 !user_can_read_file(conn, smb_fname_base)) {
1358 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1363 /* Honour _hide unwriteable_ option */
1364 if (hide_unwriteable && !user_can_write_file(conn,
1366 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1371 /* Honour _hide_special_ option */
1372 if (hide_special && file_is_special(conn, smb_fname_base)) {
1373 DEBUG(10,("is_visible_file: file %s is special.\n",
1379 if (hide_new_files_timeout != 0) {
1381 double age = timespec_elapsed(
1382 &smb_fname_base->st.st_ex_mtime);
1384 if (age < (double)hide_new_files_timeout) {
1393 TALLOC_FREE(smb_fname_base);
1398 static int smb_Dir_destructor(struct smb_Dir *dir_hnd)
1400 files_struct *fsp = dir_hnd->fsp;
1402 SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
1404 * The SMB_VFS_CLOSEDIR above
1405 * closes the underlying fd inside
1406 * dirp->fsp, unless fallback_opendir
1407 * was set in which case the fd
1408 * in dir_hnd->fsp->fh->fd isn't
1409 * the one being closed. Close
1412 if (dir_hnd->fallback_opendir) {
1416 SMB_ASSERT(fsp->dptr->dir_hnd == dir_hnd);
1417 fsp->dptr->dir_hnd = NULL;
1418 dir_hnd->fsp = NULL;
1422 /*******************************************************************
1424 ********************************************************************/
1426 static struct smb_Dir *OpenDir_internal(TALLOC_CTX *mem_ctx,
1427 connection_struct *conn,
1428 const struct smb_filename *smb_dname,
1432 struct smb_Dir *dir_hnd = talloc_zero(mem_ctx, struct smb_Dir);
1438 dir_hnd->dir = SMB_VFS_OPENDIR(conn, smb_dname, mask, attr);
1440 if (!dir_hnd->dir) {
1441 DEBUG(5,("OpenDir: Can't open %s. %s\n",
1442 smb_dname->base_name,
1447 dir_hnd->conn = conn;
1449 if (!conn->sconn->using_smb2) {
1451 * The dircache is only needed for SMB1 because SMB1 uses a name
1452 * for the resume wheras SMB2 always continues from the next
1453 * position (unless it's told to restart or close-and-reopen the
1456 dir_hnd->name_cache_size =
1457 lp_directory_name_cache_size(SNUM(conn));
1463 TALLOC_FREE(dir_hnd);
1468 * @brief Open a directory handle by pathname, ensuring it's under the share path.
1470 * First stores the $cwd, then changes directory to the passed in pathname
1471 * uses check_name() to ensure this is under the connection struct share path,
1472 * then operates on a pathname of "." to ensure we're in the same place.
1474 * The returned struct smb_Dir * should have a talloc destrctor added to
1475 * ensure that when the struct is freed the internal POSIX DIR * pointer
1480 * static int sample_smb_Dir_destructor(struct smb_Dir *dirp)
1482 * SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1485 * struct smb_Dir *dir_hnd = open_dir_safely(mem_ctx,
1490 * if (dir_hnd == NULL) {
1493 * talloc_set_destructor(dir_hnd, smb_Dir_destructor);
1498 static struct smb_Dir *open_dir_safely(TALLOC_CTX *ctx,
1499 connection_struct *conn,
1500 const struct smb_filename *smb_dname,
1504 struct smb_Dir *dir_hnd = NULL;
1505 struct smb_filename *smb_fname_cwd = NULL;
1506 struct smb_filename *saved_dir_fname = vfs_GetWd(ctx, conn);
1509 if (saved_dir_fname == NULL) {
1513 if (vfs_ChDir(conn, smb_dname) == -1) {
1517 smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
1522 if (smb_fname_cwd == NULL) {
1527 * Now the directory is pinned, use
1528 * REALPATH to ensure we can access it.
1530 status = check_name(conn, smb_fname_cwd);
1531 if (!NT_STATUS_IS_OK(status)) {
1535 dir_hnd = OpenDir_internal(ctx,
1541 if (dir_hnd == NULL) {
1546 * OpenDir_internal only gets "." as the dir name.
1547 * Store the real dir name here.
1550 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, smb_dname);
1551 if (!dir_hnd->dir_smb_fname) {
1552 TALLOC_FREE(dir_hnd);
1553 SMB_VFS_CLOSEDIR(conn, dir_hnd->dir);
1559 vfs_ChDir(conn, saved_dir_fname);
1560 TALLOC_FREE(saved_dir_fname);
1565 * Simple destructor for OpenDir() use. Don't need to
1566 * care about fsp back pointer as we know we have never
1567 * set it in the OpenDir() code path.
1570 static int smb_Dir_OpenDir_destructor(struct smb_Dir *dir_hnd)
1572 SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
1576 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1577 const struct smb_filename *smb_dname,
1581 struct smb_Dir *dir_hnd = open_dir_safely(mem_ctx,
1586 if (dir_hnd == NULL) {
1589 talloc_set_destructor(dir_hnd, smb_Dir_OpenDir_destructor);
1593 /*******************************************************************
1594 Open a directory from an fsp.
1595 ********************************************************************/
1597 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1602 struct smb_Dir *dir_hnd = talloc_zero(mem_ctx, struct smb_Dir);
1608 if (!fsp->is_directory) {
1613 if (fsp->fh->fd == -1) {
1618 dir_hnd->conn = conn;
1620 if (!conn->sconn->using_smb2) {
1622 * The dircache is only needed for SMB1 because SMB1 uses a name
1623 * for the resume wheras SMB2 always continues from the next
1624 * position (unless it's told to restart or close-and-reopen the
1627 dir_hnd->name_cache_size =
1628 lp_directory_name_cache_size(SNUM(conn));
1631 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, fsp->fsp_name);
1632 if (!dir_hnd->dir_smb_fname) {
1637 dir_hnd->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1638 if (dir_hnd->dir != NULL) {
1641 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1643 dir_hnd->dir_smb_fname->base_name,
1645 if (errno != ENOSYS) {
1650 if (dir_hnd->dir == NULL) {
1651 /* FDOPENDIR is not supported. Use OPENDIR instead. */
1652 TALLOC_FREE(dir_hnd);
1653 dir_hnd = open_dir_safely(mem_ctx,
1658 if (dir_hnd == NULL) {
1663 * Remember if we used the fallback.
1664 * We need to change the destructor
1665 * to also close the fsp file descriptor
1666 * in this case as it isn't the same
1667 * one the directory handle uses.
1670 dir_hnd->fallback_opendir = true;
1673 talloc_set_destructor(dir_hnd, smb_Dir_destructor);
1678 TALLOC_FREE(dir_hnd);
1683 /*******************************************************************
1684 Read from a directory.
1685 Return directory entry, current offset, and optional stat information.
1686 Don't check for veto or invisible files.
1687 ********************************************************************/
1689 const char *ReadDirName(struct smb_Dir *dir_hnd, long *poffset,
1690 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1693 char *talloced = NULL;
1694 connection_struct *conn = dir_hnd->conn;
1696 /* Cheat to allow . and .. to be the first entries returned. */
1697 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1698 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dir_hnd->file_number < 2))
1700 if (dir_hnd->file_number == 0) {
1702 *poffset = dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1705 *poffset = dir_hnd->offset = DOT_DOT_DIRECTORY_OFFSET;
1707 dir_hnd->file_number++;
1712 if (*poffset == END_OF_DIRECTORY_OFFSET) {
1713 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1717 /* A real offset, seek to it. */
1718 SeekDir(dir_hnd, *poffset);
1720 while ((n = vfs_readdirname(conn, dir_hnd->dir, sbuf, &talloced))) {
1721 /* Ignore . and .. - we've already returned them. */
1723 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1724 TALLOC_FREE(talloced);
1728 *poffset = dir_hnd->offset = SMB_VFS_TELLDIR(conn, dir_hnd->dir);
1729 *ptalloced = talloced;
1730 dir_hnd->file_number++;
1733 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1738 /*******************************************************************
1739 Rewind to the start.
1740 ********************************************************************/
1742 void RewindDir(struct smb_Dir *dir_hnd, long *poffset)
1744 SMB_VFS_REWINDDIR(dir_hnd->conn, dir_hnd->dir);
1745 dir_hnd->file_number = 0;
1746 dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1747 *poffset = START_OF_DIRECTORY_OFFSET;
1750 /*******************************************************************
1752 ********************************************************************/
1754 void SeekDir(struct smb_Dir *dirp, long offset)
1756 if (offset != dirp->offset) {
1757 if (offset == START_OF_DIRECTORY_OFFSET) {
1758 RewindDir(dirp, &offset);
1760 * Ok we should really set the file number here
1761 * to 1 to enable ".." to be returned next. Trouble
1762 * is I'm worried about callers using SeekDir(dirp,0)
1763 * as equivalent to RewindDir(). So leave this alone
1766 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1767 RewindDir(dirp, &offset);
1769 * Set the file number to 2 - we want to get the first
1770 * real file entry (the one we return after "..")
1771 * on the next ReadDir.
1773 dirp->file_number = 2;
1774 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1775 ; /* Don't seek in this case. */
1777 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1779 dirp->offset = offset;
1783 /*******************************************************************
1784 Tell a dir position.
1785 ********************************************************************/
1787 long TellDir(struct smb_Dir *dir_hnd)
1789 return(dir_hnd->offset);
1792 /*******************************************************************
1793 Add an entry into the dcache.
1794 ********************************************************************/
1796 static void DirCacheAdd(struct smb_Dir *dir_hnd, const char *name, long offset)
1798 struct name_cache_entry *e;
1800 if (dir_hnd->name_cache_size == 0) {
1804 if (dir_hnd->name_cache == NULL) {
1805 dir_hnd->name_cache = talloc_zero_array(dir_hnd,
1806 struct name_cache_entry,
1807 dir_hnd->name_cache_size);
1809 if (dir_hnd->name_cache == NULL) {
1814 dir_hnd->name_cache_index = (dir_hnd->name_cache_index+1) %
1815 dir_hnd->name_cache_size;
1816 e = &dir_hnd->name_cache[dir_hnd->name_cache_index];
1817 TALLOC_FREE(e->name);
1818 e->name = talloc_strdup(dir_hnd, name);
1822 /*******************************************************************
1823 Find an entry by name. Leave us at the offset after it.
1824 Don't check for veto or invisible files.
1825 ********************************************************************/
1827 bool SearchDir(struct smb_Dir *dir_hnd, const char *name, long *poffset)
1830 const char *entry = NULL;
1831 char *talloced = NULL;
1832 connection_struct *conn = dir_hnd->conn;
1834 /* Search back in the name cache. */
1835 if (dir_hnd->name_cache_size && dir_hnd->name_cache) {
1836 for (i = dir_hnd->name_cache_index; i >= 0; i--) {
1837 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1838 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1839 *poffset = e->offset;
1840 SeekDir(dir_hnd, e->offset);
1844 for (i = dir_hnd->name_cache_size - 1;
1845 i > dir_hnd->name_cache_index; i--) {
1846 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1847 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1848 *poffset = e->offset;
1849 SeekDir(dir_hnd, e->offset);
1855 /* Not found in the name cache. Rewind directory and start from scratch. */
1856 SMB_VFS_REWINDDIR(conn, dir_hnd->dir);
1857 dir_hnd->file_number = 0;
1858 *poffset = START_OF_DIRECTORY_OFFSET;
1859 while ((entry = ReadDirName(dir_hnd, poffset, NULL, &talloced))) {
1860 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1861 TALLOC_FREE(talloced);
1864 TALLOC_FREE(talloced);
1869 struct files_below_forall_state {
1872 int (*fn)(struct file_id fid, const struct share_mode_data *data,
1873 void *private_data);
1877 static int files_below_forall_fn(struct file_id fid,
1878 const struct share_mode_data *data,
1881 struct files_below_forall_state *state = private_data;
1882 char tmpbuf[PATH_MAX];
1883 char *fullpath, *to_free;
1886 len = full_path_tos(data->servicepath, data->base_name,
1887 tmpbuf, sizeof(tmpbuf),
1888 &fullpath, &to_free);
1892 if (state->dirpath_len >= len) {
1894 * Filter files above dirpath
1898 if (fullpath[state->dirpath_len] != '/') {
1900 * Filter file that don't have a path separator at the end of
1906 if (memcmp(state->dirpath, fullpath, state->dirpath_len) != 0) {
1913 TALLOC_FREE(to_free);
1914 return state->fn(fid, data, state->private_data);
1917 TALLOC_FREE(to_free);
1921 static int files_below_forall(connection_struct *conn,
1922 const struct smb_filename *dir_name,
1923 int (*fn)(struct file_id fid,
1924 const struct share_mode_data *data,
1925 void *private_data),
1928 struct files_below_forall_state state = {
1930 .private_data = private_data,
1933 char tmpbuf[PATH_MAX];
1936 state.dirpath_len = full_path_tos(conn->connectpath,
1937 dir_name->base_name,
1938 tmpbuf, sizeof(tmpbuf),
1939 &state.dirpath, &to_free);
1940 if (state.dirpath_len == -1) {
1944 ret = share_mode_forall(files_below_forall_fn, &state);
1945 TALLOC_FREE(to_free);
1949 struct have_file_open_below_state {
1953 static int have_file_open_below_fn(struct file_id fid,
1954 const struct share_mode_data *data,
1957 struct have_file_open_below_state *state = private_data;
1958 state->found_one = true;
1962 bool have_file_open_below(connection_struct *conn,
1963 const struct smb_filename *name)
1965 struct have_file_open_below_state state = {
1970 if (!VALID_STAT(name->st)) {
1973 if (!S_ISDIR(name->st.st_ex_mode)) {
1977 ret = files_below_forall(conn, name, have_file_open_below_fn, &state);
1982 return state.found_one;
1985 /*****************************************************************
1986 Is this directory empty ?
1987 *****************************************************************/
1989 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1991 NTSTATUS status = NT_STATUS_OK;
1993 const char *dname = NULL;
1994 const char *dirname = fsp->fsp_name->base_name;
1995 char *talloced = NULL;
1997 struct connection_struct *conn = fsp->conn;
1998 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(),
2005 return map_nt_error_from_unix(errno);
2008 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
2009 /* Quick check for "." and ".." */
2010 if (dname[0] == '.') {
2011 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
2012 TALLOC_FREE(talloced);
2017 if (!is_visible_file(conn, dirname, dname, &st, True)) {
2018 TALLOC_FREE(talloced);
2022 DEBUG(10,("got name %s - can't delete\n",
2024 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
2027 TALLOC_FREE(talloced);
2028 TALLOC_FREE(dir_hnd);
2030 if (!NT_STATUS_IS_OK(status)) {
2034 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) &&
2035 lp_strict_rename(SNUM(conn)) &&
2036 have_file_open_below(fsp->conn, fsp->fsp_name))
2038 return NT_STATUS_ACCESS_DENIED;
2041 return NT_STATUS_OK;