2 Unix SMB/CIFS implementation.
3 Directory handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "libcli/security/security.h"
26 #include "lib/util/bitmap.h"
27 #include "../lib/util/memcache.h"
28 #include "../librpc/gen_ndr/open_files.h"
31 This module implements directory related functions for Samba.
34 /* "Special" directory offsets. */
35 #define END_OF_DIRECTORY_OFFSET ((long)-1)
36 #define START_OF_DIRECTORY_OFFSET ((long)0)
37 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
39 /* "Special" directory offsets in 32-bit wire format. */
40 #define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
41 #define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
42 #define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
44 /* Make directory handle internals available. */
46 struct name_cache_entry
{
52 connection_struct
*conn
;
56 size_t name_cache_size
;
57 struct name_cache_entry
*name_cache
;
58 unsigned int name_cache_index
;
59 unsigned int file_number
;
60 files_struct
*fsp
; /* Back pointer to containing fsp, only
61 set from OpenDir_fsp(). */
65 struct dptr_struct
*next
, *prev
;
68 struct connection_struct
*conn
;
69 struct smb_Dir
*dir_hnd
;
74 bool has_wild
; /* Set to true if the wcard entry has MS wildcard characters in it. */
75 bool did_stat
; /* Optimisation for non-wcard searches. */
76 bool priv
; /* Directory handle opened with privilege. */
78 struct memcache
*dptr_cache
;
81 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
86 static void DirCacheAdd(struct smb_Dir
*dirp
, const char *name
, long offset
);
88 #define INVALID_DPTR_KEY (-3)
90 /****************************************************************************
91 Initialise the dir bitmap.
92 ****************************************************************************/
94 bool init_dptrs(struct smbd_server_connection
*sconn
)
96 if (sconn
->searches
.dptr_bmap
) {
100 sconn
->searches
.dptr_bmap
= bitmap_talloc(
101 sconn
, MAX_DIRECTORY_HANDLES
);
103 if (sconn
->searches
.dptr_bmap
== NULL
) {
110 /****************************************************************************
111 Idle a dptr - the directory is closed but the control info is kept.
112 ****************************************************************************/
114 static void dptr_idle(struct dptr_struct
*dptr
)
117 DEBUG(4,("Idling dptr dnum %d\n",dptr
->dnum
));
118 TALLOC_FREE(dptr
->dir_hnd
);
119 TALLOC_FREE(dptr
->dptr_cache
);
124 /****************************************************************************
125 Idle the oldest dptr.
126 ****************************************************************************/
128 static void dptr_idleoldest(struct smbd_server_connection
*sconn
)
130 struct dptr_struct
*dptr
;
133 * Go to the end of the list.
135 dptr
= DLIST_TAIL(sconn
->searches
.dirptrs
);
138 DEBUG(0,("No dptrs available to idle ?\n"));
143 * Idle the oldest pointer.
146 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
154 /****************************************************************************
155 Get the struct dptr_struct for a dir index.
156 ****************************************************************************/
158 static struct dptr_struct
*dptr_get(struct smbd_server_connection
*sconn
,
159 int key
, bool forclose
)
161 struct dptr_struct
*dptr
;
163 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
164 if(dptr
->dnum
== key
) {
165 if (!forclose
&& !dptr
->dir_hnd
) {
166 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
)
167 dptr_idleoldest(sconn
);
168 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key
));
169 if (!(dptr
->dir_hnd
= OpenDir(
170 NULL
, dptr
->conn
, dptr
->path
,
171 dptr
->wcard
, dptr
->attr
))) {
172 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr
->path
,
177 DLIST_PROMOTE(sconn
->searches
.dirptrs
,dptr
);
184 /****************************************************************************
185 Get the dir path for a dir index.
186 ****************************************************************************/
188 const char *dptr_path(struct smbd_server_connection
*sconn
, int key
)
190 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
196 /****************************************************************************
197 Get the dir wcard for a dir index.
198 ****************************************************************************/
200 const char *dptr_wcard(struct smbd_server_connection
*sconn
, int key
)
202 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
208 /****************************************************************************
209 Get the dir attrib for a dir index.
210 ****************************************************************************/
212 uint16
dptr_attr(struct smbd_server_connection
*sconn
, int key
)
214 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
220 /****************************************************************************
221 Close a dptr (internal func).
222 ****************************************************************************/
224 static void dptr_close_internal(struct dptr_struct
*dptr
)
226 struct smbd_server_connection
*sconn
= dptr
->conn
->sconn
;
228 DEBUG(4,("closing dptr key %d\n",dptr
->dnum
));
234 if (sconn
->using_smb2
) {
238 DLIST_REMOVE(sconn
->searches
.dirptrs
, dptr
);
241 * Free the dnum in the bitmap. Remember the dnum value is always
242 * biased by one with respect to the bitmap.
245 if (!bitmap_query(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1)) {
246 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
250 bitmap_clear(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1);
253 TALLOC_FREE(dptr
->dir_hnd
);
257 /****************************************************************************
258 Close a dptr given a key.
259 ****************************************************************************/
261 void dptr_close(struct smbd_server_connection
*sconn
, int *key
)
263 struct dptr_struct
*dptr
;
265 if(*key
== INVALID_DPTR_KEY
)
268 /* OS/2 seems to use -1 to indicate "close all directories" */
270 struct dptr_struct
*next
;
271 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
273 dptr_close_internal(dptr
);
275 *key
= INVALID_DPTR_KEY
;
279 dptr
= dptr_get(sconn
, *key
, true);
282 DEBUG(0,("Invalid key %d given to dptr_close\n", *key
));
286 dptr_close_internal(dptr
);
288 *key
= INVALID_DPTR_KEY
;
291 /****************************************************************************
292 Close all dptrs for a cnum.
293 ****************************************************************************/
295 void dptr_closecnum(connection_struct
*conn
)
297 struct dptr_struct
*dptr
, *next
;
298 struct smbd_server_connection
*sconn
= conn
->sconn
;
304 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
306 if (dptr
->conn
== conn
) {
307 dptr_close_internal(dptr
);
312 /****************************************************************************
313 Idle all dptrs for a cnum.
314 ****************************************************************************/
316 void dptr_idlecnum(connection_struct
*conn
)
318 struct dptr_struct
*dptr
;
319 struct smbd_server_connection
*sconn
= conn
->sconn
;
325 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
326 if (dptr
->conn
== conn
&& dptr
->dir_hnd
) {
332 /****************************************************************************
333 Close a dptr that matches a given path, only if it matches the spid also.
334 ****************************************************************************/
336 void dptr_closepath(struct smbd_server_connection
*sconn
,
337 char *path
,uint16 spid
)
339 struct dptr_struct
*dptr
, *next
;
340 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
342 if (spid
== dptr
->spid
&& strequal(dptr
->path
,path
))
343 dptr_close_internal(dptr
);
347 /****************************************************************************
348 Try and close the oldest handle not marked for
349 expect close in the hope that the client has
350 finished with that one.
351 ****************************************************************************/
353 static void dptr_close_oldest(struct smbd_server_connection
*sconn
,
356 struct dptr_struct
*dptr
;
359 * Go to the end of the list.
361 for(dptr
= sconn
->searches
.dirptrs
; dptr
&& dptr
->next
; dptr
= dptr
->next
)
365 DEBUG(0,("No old dptrs available to close oldest ?\n"));
370 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
371 * does not have expect_close set. If 'old' is false, close
372 * one of the new dnum handles.
375 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
376 if ((old
&& (dptr
->dnum
< 256) && !dptr
->expect_close
) ||
377 (!old
&& (dptr
->dnum
> 255))) {
378 dptr_close_internal(dptr
);
384 /****************************************************************************
385 Safely do an OpenDir as root, ensuring we're in the right place.
386 ****************************************************************************/
388 static struct smb_Dir
*open_dir_with_privilege(connection_struct
*conn
,
389 struct smb_request
*req
,
394 struct smb_Dir
*dir_hnd
= NULL
;
395 struct smb_filename
*smb_fname_cwd
;
396 char *saved_dir
= vfs_GetWd(talloc_tos(), conn
);
397 struct privilege_paths
*priv_paths
= req
->priv_paths
;
400 if (saved_dir
== NULL
) {
404 if (vfs_ChDir(conn
, path
) == -1) {
408 /* Now check the stat value is the same. */
409 smb_fname_cwd
= synthetic_smb_fname(talloc_tos(), ".", NULL
, NULL
);
411 if (smb_fname_cwd
== NULL
) {
414 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
419 if (!check_same_stat(&smb_fname_cwd
->st
, &priv_paths
->parent_name
.st
)) {
420 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
423 smb_fname_str_dbg(&priv_paths
->parent_name
)));
427 dir_hnd
= OpenDir(NULL
, conn
, ".", wcard
, attr
);
431 vfs_ChDir(conn
, saved_dir
);
435 /****************************************************************************
436 Create a new dir ptr. If the flag old_handle is true then we must allocate
437 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
438 one byte long. If old_handle is false we allocate from the range
439 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
440 a directory handle is never zero.
441 wcard must not be zero.
442 ****************************************************************************/
444 NTSTATUS
dptr_create(connection_struct
*conn
,
445 struct smb_request
*req
,
447 const char *path
, bool old_handle
, bool expect_close
,uint16 spid
,
448 const char *wcard
, bool wcard_has_wild
, uint32 attr
, struct dptr_struct
**dptr_ret
)
450 struct smbd_server_connection
*sconn
= conn
->sconn
;
451 struct dptr_struct
*dptr
= NULL
;
452 struct smb_Dir
*dir_hnd
;
454 if (fsp
&& fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
455 path
= fsp
->fsp_name
->base_name
;
458 DEBUG(5,("dptr_create dir=%s\n", path
));
461 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
462 return NT_STATUS_INTERNAL_ERROR
;
466 return NT_STATUS_INVALID_PARAMETER
;
470 if (!(fsp
->access_mask
& SEC_DIR_LIST
)) {
471 DEBUG(5,("dptr_create: directory %s "
472 "not open for LIST access\n",
474 return NT_STATUS_ACCESS_DENIED
;
476 dir_hnd
= OpenDir_fsp(NULL
, conn
, fsp
, wcard
, attr
);
479 bool backup_intent
= (req
&& req
->priv_paths
);
480 struct smb_filename
*smb_dname
;
483 smb_dname
= synthetic_smb_fname(talloc_tos(), path
,
485 if (smb_dname
== NULL
) {
486 return NT_STATUS_NO_MEMORY
;
488 if (lp_posix_pathnames()) {
489 ret
= SMB_VFS_LSTAT(conn
, smb_dname
);
491 ret
= SMB_VFS_STAT(conn
, smb_dname
);
494 return map_nt_error_from_unix(errno
);
496 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
497 return NT_STATUS_NOT_A_DIRECTORY
;
499 status
= smbd_check_access_rights(conn
,
503 if (!NT_STATUS_IS_OK(status
)) {
507 dir_hnd
= open_dir_with_privilege(conn
,
513 dir_hnd
= OpenDir(NULL
, conn
, path
, wcard
, attr
);
518 return map_nt_error_from_unix(errno
);
521 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
) {
522 dptr_idleoldest(sconn
);
525 dptr
= talloc_zero(NULL
, struct dptr_struct
);
527 DEBUG(0,("talloc fail in dptr_create.\n"));
528 TALLOC_FREE(dir_hnd
);
529 return NT_STATUS_NO_MEMORY
;
532 dptr
->path
= talloc_strdup(dptr
, path
);
535 TALLOC_FREE(dir_hnd
);
536 return NT_STATUS_NO_MEMORY
;
539 dptr
->dir_hnd
= dir_hnd
;
541 dptr
->expect_close
= expect_close
;
542 dptr
->wcard
= talloc_strdup(dptr
, wcard
);
545 TALLOC_FREE(dir_hnd
);
546 return NT_STATUS_NO_MEMORY
;
548 if (lp_posix_pathnames() || (wcard
[0] == '.' && wcard
[1] == 0)) {
549 dptr
->has_wild
= True
;
551 dptr
->has_wild
= wcard_has_wild
;
556 if (sconn
->using_smb2
) {
563 * This is an old-style SMBsearch request. Ensure the
564 * value we return will fit in the range 1-255.
567 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
569 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
572 * Try and close the oldest handle not marked for
573 * expect close in the hope that the client has
574 * finished with that one.
577 dptr_close_oldest(sconn
, true);
579 /* Now try again... */
580 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
581 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
582 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr
->dnum
));
584 TALLOC_FREE(dir_hnd
);
585 return NT_STATUS_TOO_MANY_OPENED_FILES
;
591 * This is a new-style trans2 request. Allocate from
592 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
595 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
597 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
600 * Try and close the oldest handle close in the hope that
601 * the client has finished with that one. This will only
602 * happen in the case of the Win98 client bug where it leaks
606 dptr_close_oldest(sconn
, false);
608 /* Now try again... */
609 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
611 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
612 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr
->dnum
));
614 TALLOC_FREE(dir_hnd
);
615 return NT_STATUS_TOO_MANY_OPENED_FILES
;
620 bitmap_set(sconn
->searches
.dptr_bmap
, dptr
->dnum
);
622 dptr
->dnum
+= 1; /* Always bias the dnum by one - no zero dnums allowed. */
624 DLIST_ADD(sconn
->searches
.dirptrs
, dptr
);
627 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
628 dptr
->dnum
,path
,expect_close
));
636 /****************************************************************************
637 Wrapper functions to access the lower level directory handles.
638 ****************************************************************************/
640 void dptr_CloseDir(files_struct
*fsp
)
644 * The destructor for the struct smb_Dir
645 * (fsp->dptr->dir_hnd) now handles
646 * all resource deallocation.
648 dptr_close_internal(fsp
->dptr
);
653 void dptr_SeekDir(struct dptr_struct
*dptr
, long offset
)
655 SeekDir(dptr
->dir_hnd
, offset
);
658 long dptr_TellDir(struct dptr_struct
*dptr
)
660 return TellDir(dptr
->dir_hnd
);
663 bool dptr_has_wild(struct dptr_struct
*dptr
)
665 return dptr
->has_wild
;
668 int dptr_dnum(struct dptr_struct
*dptr
)
673 bool dptr_get_priv(struct dptr_struct
*dptr
)
678 void dptr_set_priv(struct dptr_struct
*dptr
)
683 /****************************************************************************
684 Return the next visible file name, skipping veto'd and invisible files.
685 ****************************************************************************/
687 static const char *dptr_normal_ReadDirName(struct dptr_struct
*dptr
,
688 long *poffset
, SMB_STRUCT_STAT
*pst
,
691 /* Normal search for the next file. */
693 char *talloced
= NULL
;
695 while ((name
= ReadDirName(dptr
->dir_hnd
, poffset
, pst
, &talloced
))
697 if (is_visible_file(dptr
->conn
, dptr
->path
, name
, pst
, True
)) {
698 *ptalloced
= talloced
;
701 TALLOC_FREE(talloced
);
706 /****************************************************************************
707 Return the next visible file name, skipping veto'd and invisible files.
708 ****************************************************************************/
710 static char *dptr_ReadDirName(TALLOC_CTX
*ctx
,
711 struct dptr_struct
*dptr
,
713 SMB_STRUCT_STAT
*pst
)
715 struct smb_filename smb_fname_base
;
717 const char *name_temp
= NULL
;
718 char *talloced
= NULL
;
719 char *pathreal
= NULL
;
720 char *found_name
= NULL
;
723 SET_STAT_INVALID(*pst
);
725 if (dptr
->has_wild
|| dptr
->did_stat
) {
726 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
,
728 if (name_temp
== NULL
) {
731 if (talloced
!= NULL
) {
732 return talloc_move(ctx
, &talloced
);
734 return talloc_strdup(ctx
, name_temp
);
737 /* If poffset is -1 then we know we returned this name before and we
738 * have no wildcards. We're at the end of the directory. */
739 if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
743 /* We know the stored wcard contains no wildcard characters.
744 * See if we can match with a stat call. If we can't, then set
745 * did_stat to true to ensure we only do this once and keep
748 dptr
->did_stat
= true;
750 /* First check if it should be visible. */
751 if (!is_visible_file(dptr
->conn
, dptr
->path
, dptr
->wcard
,
754 /* This only returns false if the file was found, but
755 is explicitly not visible. Set us to end of
756 directory, but return NULL as we know we can't ever
761 if (VALID_STAT(*pst
)) {
762 name
= talloc_strdup(ctx
, dptr
->wcard
);
766 pathreal
= talloc_asprintf(ctx
,
773 /* Create an smb_filename with stream_name == NULL. */
774 smb_fname_base
= (struct smb_filename
) { .base_name
= pathreal
};
776 if (SMB_VFS_STAT(dptr
->conn
, &smb_fname_base
) == 0) {
777 *pst
= smb_fname_base
.st
;
778 name
= talloc_strdup(ctx
, dptr
->wcard
);
781 /* If we get any other error than ENOENT or ENOTDIR
782 then the file exists we just can't stat it. */
783 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
784 name
= talloc_strdup(ctx
, dptr
->wcard
);
789 /* Stat failed. We know this is authoratiative if we are
790 * providing case sensitive semantics or the underlying
791 * filesystem is case sensitive.
793 if (dptr
->conn
->case_sensitive
||
794 !(dptr
->conn
->fs_capabilities
& FILE_CASE_SENSITIVE_SEARCH
))
800 * Try case-insensitive stat if the fs has the ability. This avoids
801 * scanning the whole directory.
803 ret
= SMB_VFS_GET_REAL_FILENAME(dptr
->conn
, dptr
->path
, dptr
->wcard
,
808 } else if (errno
== ENOENT
) {
809 /* The case-insensitive lookup was authoritative. */
813 TALLOC_FREE(pathreal
);
815 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
, &talloced
);
816 if (name_temp
== NULL
) {
819 if (talloced
!= NULL
) {
820 return talloc_move(ctx
, &talloced
);
822 return talloc_strdup(ctx
, name_temp
);
825 TALLOC_FREE(pathreal
);
827 /* We need to set the underlying dir_hnd offset to -1
828 * also as this function is usually called with the
829 * output from TellDir. */
830 dptr
->dir_hnd
->offset
= *poffset
= END_OF_DIRECTORY_OFFSET
;
834 /****************************************************************************
835 Search for a file by name, skipping veto'ed and not visible files.
836 ****************************************************************************/
838 bool dptr_SearchDir(struct dptr_struct
*dptr
, const char *name
, long *poffset
, SMB_STRUCT_STAT
*pst
)
840 SET_STAT_INVALID(*pst
);
842 if (!dptr
->has_wild
&& (dptr
->dir_hnd
->offset
== END_OF_DIRECTORY_OFFSET
)) {
843 /* This is a singleton directory and we're already at the end. */
844 *poffset
= END_OF_DIRECTORY_OFFSET
;
848 return SearchDir(dptr
->dir_hnd
, name
, poffset
);
851 /****************************************************************************
852 Initialize variables & state data at the beginning of all search SMB requests.
853 ****************************************************************************/
854 void dptr_init_search_op(struct dptr_struct
*dptr
)
856 SMB_VFS_INIT_SEARCH_OP(dptr
->conn
, dptr
->dir_hnd
->dir
);
859 /****************************************************************************
860 Map a native directory offset to a 32-bit cookie.
861 ****************************************************************************/
863 static uint32_t map_dir_offset_to_wire(struct dptr_struct
*dptr
, long offset
)
868 if (offset
== END_OF_DIRECTORY_OFFSET
) {
869 return WIRE_END_OF_DIRECTORY_OFFSET
;
870 } else if(offset
== START_OF_DIRECTORY_OFFSET
) {
871 return WIRE_START_OF_DIRECTORY_OFFSET
;
872 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
873 return WIRE_DOT_DOT_DIRECTORY_OFFSET
;
875 if (sizeof(long) == 4) {
876 /* 32-bit machine. We can cheat... */
877 return (uint32_t)offset
;
879 if (dptr
->dptr_cache
== NULL
) {
880 /* Lazy initialize cache. */
881 dptr
->dptr_cache
= memcache_init(dptr
, 0);
882 if (dptr
->dptr_cache
== NULL
) {
883 return WIRE_END_OF_DIRECTORY_OFFSET
;
886 /* Have we seen this offset before ? */
887 key
.data
= (void *)&offset
;
888 key
.length
= sizeof(offset
);
889 if (memcache_lookup(dptr
->dptr_cache
,
890 SMB1_SEARCH_OFFSET_MAP
,
893 uint32_t wire_offset
;
894 SMB_ASSERT(val
.length
== sizeof(wire_offset
));
895 memcpy(&wire_offset
, val
.data
, sizeof(wire_offset
));
896 DEBUG(10,("found wire %u <-> offset %ld\n",
897 (unsigned int)wire_offset
,
902 /* Allocate a new wire cookie. */
905 } while (dptr
->counter
== WIRE_START_OF_DIRECTORY_OFFSET
||
906 dptr
->counter
== WIRE_END_OF_DIRECTORY_OFFSET
||
907 dptr
->counter
== WIRE_DOT_DOT_DIRECTORY_OFFSET
);
908 /* Store it in the cache. */
909 key
.data
= (void *)&offset
;
910 key
.length
= sizeof(offset
);
911 val
.data
= (void *)&dptr
->counter
;
912 val
.length
= sizeof(dptr
->counter
); /* MUST BE uint32_t ! */
913 memcache_add(dptr
->dptr_cache
,
914 SMB1_SEARCH_OFFSET_MAP
,
917 /* And the reverse mapping for lookup from
918 map_wire_to_dir_offset(). */
919 memcache_add(dptr
->dptr_cache
,
920 SMB1_SEARCH_OFFSET_MAP
,
923 DEBUG(10,("stored wire %u <-> offset %ld\n",
924 (unsigned int)dptr
->counter
,
926 return dptr
->counter
;
929 /****************************************************************************
930 Fill the 5 byte server reserved dptr field.
931 ****************************************************************************/
933 bool dptr_fill(struct smbd_server_connection
*sconn
,
934 char *buf1
,unsigned int key
)
936 unsigned char *buf
= (unsigned char *)buf1
;
937 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
938 uint32_t wire_offset
;
940 DEBUG(1,("filling null dirptr %d\n",key
));
943 wire_offset
= map_dir_offset_to_wire(dptr
,TellDir(dptr
->dir_hnd
));
944 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key
,
945 (long)dptr
->dir_hnd
,(int)wire_offset
));
947 SIVAL(buf
,1,wire_offset
);
951 /****************************************************************************
952 Map a 32-bit wire cookie to a native directory offset.
953 ****************************************************************************/
955 static long map_wire_to_dir_offset(struct dptr_struct
*dptr
, uint32_t wire_offset
)
960 if (wire_offset
== WIRE_END_OF_DIRECTORY_OFFSET
) {
961 return END_OF_DIRECTORY_OFFSET
;
962 } else if(wire_offset
== WIRE_START_OF_DIRECTORY_OFFSET
) {
963 return START_OF_DIRECTORY_OFFSET
;
964 } else if (wire_offset
== WIRE_DOT_DOT_DIRECTORY_OFFSET
) {
965 return DOT_DOT_DIRECTORY_OFFSET
;
967 if (sizeof(long) == 4) {
968 /* 32-bit machine. We can cheat... */
969 return (long)wire_offset
;
971 if (dptr
->dptr_cache
== NULL
) {
972 /* Logic error, cache should be initialized. */
973 return END_OF_DIRECTORY_OFFSET
;
975 key
.data
= (void *)&wire_offset
;
976 key
.length
= sizeof(wire_offset
);
977 if (memcache_lookup(dptr
->dptr_cache
,
978 SMB1_SEARCH_OFFSET_MAP
,
983 SMB_ASSERT(val
.length
== sizeof(offset
));
984 memcpy(&offset
, val
.data
, sizeof(offset
));
985 DEBUG(10,("lookup wire %u <-> offset %ld\n",
986 (unsigned int)wire_offset
,
990 return END_OF_DIRECTORY_OFFSET
;
993 /****************************************************************************
994 Fetch the dir ptr and seek it given the 5 byte server field.
995 ****************************************************************************/
997 struct dptr_struct
*dptr_fetch(struct smbd_server_connection
*sconn
,
1000 unsigned int key
= *(unsigned char *)buf
;
1001 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
1002 uint32_t wire_offset
;
1006 DEBUG(3,("fetched null dirptr %d\n",key
));
1010 wire_offset
= IVAL(buf
,1);
1011 seekoff
= map_wire_to_dir_offset(dptr
, wire_offset
);
1012 SeekDir(dptr
->dir_hnd
,seekoff
);
1013 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1014 key
, dptr
->path
, (int)seekoff
));
1018 /****************************************************************************
1020 ****************************************************************************/
1022 struct dptr_struct
*dptr_fetch_lanman2(struct smbd_server_connection
*sconn
,
1025 struct dptr_struct
*dptr
= dptr_get(sconn
, dptr_num
, false);
1028 DEBUG(3,("fetched null dirptr %d\n",dptr_num
));
1031 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num
,dptr
->path
));
1035 static bool mangle_mask_match(connection_struct
*conn
,
1036 const char *filename
,
1041 if (!name_to_8_3(filename
,mname
,False
,conn
->params
)) {
1044 return mask_match_search(mname
,mask
,False
);
1047 bool smbd_dirptr_get_entry(TALLOC_CTX
*ctx
,
1048 struct dptr_struct
*dirptr
,
1053 bool (*match_fn
)(TALLOC_CTX
*ctx
,
1058 bool (*mode_fn
)(TALLOC_CTX
*ctx
,
1060 struct smb_filename
*smb_fname
,
1064 struct smb_filename
**_smb_fname
,
1068 connection_struct
*conn
= dirptr
->conn
;
1075 pathlen
= strlen(dirptr
->path
);
1076 slashlen
= ( dirptr
->path
[pathlen
-1] != '/') ? 1 : 0;
1081 SMB_STRUCT_STAT sbuf
= { 0 };
1085 char *pathreal
= NULL
;
1086 struct smb_filename smb_fname
;
1090 cur_offset
= dptr_TellDir(dirptr
);
1091 prev_offset
= cur_offset
;
1092 dname
= dptr_ReadDirName(ctx
, dirptr
, &cur_offset
, &sbuf
);
1094 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1095 (long)dirptr
, cur_offset
));
1097 if (dname
== NULL
) {
1101 isdots
= (ISDOT(dname
) || ISDOTDOT(dname
));
1102 if (dont_descend
&& !isdots
) {
1108 * fname may get mangled, dname is never mangled.
1109 * Whenever we're accessing the filesystem we use
1110 * pathreal which is composed from dname.
1113 ok
= match_fn(ctx
, private_data
, dname
, mask
, &fname
);
1121 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1122 * needslash?"/":"", dname);
1123 * but this was measurably slower than doing the memcpy.
1126 pathreal
= talloc_array(
1128 pathlen
+ slashlen
+ talloc_get_size(dname
));
1135 memcpy(pathreal
, dirptr
->path
, pathlen
);
1136 pathreal
[pathlen
] = '/';
1137 memcpy(pathreal
+ slashlen
+ pathlen
, dname
,
1138 talloc_get_size(dname
));
1140 /* Create smb_fname with NULL stream_name. */
1141 smb_fname
= (struct smb_filename
) {
1142 .base_name
= pathreal
, .st
= sbuf
1145 ok
= mode_fn(ctx
, private_data
, &smb_fname
, &mode
);
1149 TALLOC_FREE(pathreal
);
1153 if (!dir_check_ftype(mode
, dirtype
)) {
1154 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1155 fname
, (unsigned int)mode
, (unsigned int)dirtype
));
1158 TALLOC_FREE(pathreal
);
1162 if (ask_sharemode
) {
1163 struct timespec write_time_ts
;
1164 struct file_id fileid
;
1166 fileid
= vfs_file_id_from_sbuf(conn
,
1168 get_file_infos(fileid
, 0, NULL
, &write_time_ts
);
1169 if (!null_timespec(write_time_ts
)) {
1170 update_stat_ex_mtime(&smb_fname
.st
,
1175 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1177 mask
, smb_fname_str_dbg(&smb_fname
),
1180 DirCacheAdd(dirptr
->dir_hnd
, dname
, cur_offset
);
1184 *_smb_fname
= cp_smb_filename(ctx
, &smb_fname
);
1185 TALLOC_FREE(pathreal
);
1186 if (*_smb_fname
== NULL
) {
1191 *_prev_offset
= prev_offset
;
1199 /****************************************************************************
1200 Get an 8.3 directory entry.
1201 ****************************************************************************/
1203 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX
*ctx
,
1209 connection_struct
*conn
= (connection_struct
*)private_data
;
1211 if ((strcmp(mask
,"*.*") == 0) ||
1212 mask_match_search(dname
, mask
, false) ||
1213 mangle_mask_match(conn
, dname
, mask
)) {
1217 * Ensure we can push the original name as UCS2. If
1218 * not, then just don't return this name.
1222 size_t len
= (strlen(dname
) + 2) * 4; /* Allow enough space. */
1223 uint8_t *tmp
= talloc_array(talloc_tos(),
1227 status
= srvstr_push(NULL
,
1228 FLAGS2_UNICODE_STRINGS
,
1237 if (!NT_STATUS_IS_OK(status
)) {
1241 if (!mangle_is_8_3(dname
, false, conn
->params
)) {
1242 bool ok
= name_to_8_3(dname
, mname
, false,
1252 *_fname
= talloc_strdup(ctx
, fname
);
1253 if (*_fname
== NULL
) {
1263 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX
*ctx
,
1265 struct smb_filename
*smb_fname
,
1268 connection_struct
*conn
= (connection_struct
*)private_data
;
1270 if (!VALID_STAT(smb_fname
->st
)) {
1271 if ((SMB_VFS_STAT(conn
, smb_fname
)) != 0) {
1272 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1273 "Couldn't stat [%s]. Error "
1275 smb_fname_str_dbg(smb_fname
),
1281 *_mode
= dos_mode(conn
, smb_fname
);
1285 bool get_dir_entry(TALLOC_CTX
*ctx
,
1286 struct dptr_struct
*dirptr
,
1292 struct timespec
*_date
,
1296 connection_struct
*conn
= dirptr
->conn
;
1298 struct smb_filename
*smb_fname
= NULL
;
1303 ok
= smbd_dirptr_get_entry(ctx
,
1309 smbd_dirptr_8_3_match_fn
,
1310 smbd_dirptr_8_3_mode_fn
,
1320 *_fname
= talloc_move(ctx
, &fname
);
1321 *_size
= smb_fname
->st
.st_ex_size
;
1323 *_date
= smb_fname
->st
.st_ex_mtime
;
1324 TALLOC_FREE(smb_fname
);
1328 /*******************************************************************
1329 Check to see if a user can read a file. This is only approximate,
1330 it is used as part of the "hide unreadable" option. Don't
1331 use it for anything security sensitive.
1332 ********************************************************************/
1334 static bool user_can_read_file(connection_struct
*conn
,
1335 struct smb_filename
*smb_fname
)
1338 * Never hide files from the root user.
1339 * We use (uid_t)0 here not sec_initial_uid()
1340 * as make test uses a single user context.
1343 if (get_current_uid(conn
) == (uid_t
)0) {
1347 return NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
1353 /*******************************************************************
1354 Check to see if a user can write a file (and only files, we do not
1355 check dirs on this one). This is only approximate,
1356 it is used as part of the "hide unwriteable" option. Don't
1357 use it for anything security sensitive.
1358 ********************************************************************/
1360 static bool user_can_write_file(connection_struct
*conn
,
1361 const struct smb_filename
*smb_fname
)
1364 * Never hide files from the root user.
1365 * We use (uid_t)0 here not sec_initial_uid()
1366 * as make test uses a single user context.
1369 if (get_current_uid(conn
) == (uid_t
)0) {
1373 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1375 /* Pseudo-open the file */
1377 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1381 return can_write_to_file(conn
, smb_fname
);
1384 /*******************************************************************
1385 Is a file a "special" type ?
1386 ********************************************************************/
1388 static bool file_is_special(connection_struct
*conn
,
1389 const struct smb_filename
*smb_fname
)
1392 * Never hide files from the root user.
1393 * We use (uid_t)0 here not sec_initial_uid()
1394 * as make test uses a single user context.
1397 if (get_current_uid(conn
) == (uid_t
)0) {
1401 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1403 if (S_ISREG(smb_fname
->st
.st_ex_mode
) ||
1404 S_ISDIR(smb_fname
->st
.st_ex_mode
) ||
1405 S_ISLNK(smb_fname
->st
.st_ex_mode
))
1411 /*******************************************************************
1412 Should the file be seen by the client?
1413 NOTE: A successful return is no guarantee of the file's existence.
1414 ********************************************************************/
1416 bool is_visible_file(connection_struct
*conn
, const char *dir_path
,
1417 const char *name
, SMB_STRUCT_STAT
*pst
, bool use_veto
)
1419 bool hide_unreadable
= lp_hide_unreadable(SNUM(conn
));
1420 bool hide_unwriteable
= lp_hide_unwriteable_files(SNUM(conn
));
1421 bool hide_special
= lp_hide_special_files(SNUM(conn
));
1423 struct smb_filename
*smb_fname_base
= NULL
;
1426 if ((strcmp(".",name
) == 0) || (strcmp("..",name
) == 0)) {
1427 return True
; /* . and .. are always visible. */
1430 /* If it's a vetoed file, pretend it doesn't even exist */
1431 if (use_veto
&& IS_VETO_PATH(conn
, name
)) {
1432 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name
));
1436 if (hide_unreadable
|| hide_unwriteable
|| hide_special
) {
1437 entry
= talloc_asprintf(talloc_tos(), "%s/%s", dir_path
, name
);
1443 /* Create an smb_filename with stream_name == NULL. */
1444 smb_fname_base
= synthetic_smb_fname(talloc_tos(), entry
, NULL
,
1446 if (smb_fname_base
== NULL
) {
1451 /* If the file name does not exist, there's no point checking
1452 * the configuration options. We succeed, on the basis that the
1453 * checks *might* have passed if the file was present.
1455 if (!VALID_STAT(*pst
)) {
1456 if (SMB_VFS_STAT(conn
, smb_fname_base
) != 0) {
1460 *pst
= smb_fname_base
->st
;
1464 /* Honour _hide unreadable_ option */
1465 if (hide_unreadable
&&
1466 !user_can_read_file(conn
, smb_fname_base
)) {
1467 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1472 /* Honour _hide unwriteable_ option */
1473 if (hide_unwriteable
&& !user_can_write_file(conn
,
1475 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1480 /* Honour _hide_special_ option */
1481 if (hide_special
&& file_is_special(conn
, smb_fname_base
)) {
1482 DEBUG(10,("is_visible_file: file %s is special.\n",
1491 TALLOC_FREE(smb_fname_base
);
1496 static int smb_Dir_destructor(struct smb_Dir
*dirp
)
1498 if (dirp
->dir
!= NULL
) {
1499 SMB_VFS_CLOSEDIR(dirp
->conn
,dirp
->dir
);
1500 if (dirp
->fsp
!= NULL
) {
1502 * The SMB_VFS_CLOSEDIR above
1503 * closes the underlying fd inside
1506 dirp
->fsp
->fh
->fd
= -1;
1507 if (dirp
->fsp
->dptr
!= NULL
) {
1508 SMB_ASSERT(dirp
->fsp
->dptr
->dir_hnd
== dirp
);
1509 dirp
->fsp
->dptr
->dir_hnd
= NULL
;
1514 if (dirp
->conn
->sconn
&& !dirp
->conn
->sconn
->using_smb2
) {
1515 dirp
->conn
->sconn
->searches
.dirhandles_open
--;
1520 /*******************************************************************
1522 ********************************************************************/
1524 struct smb_Dir
*OpenDir(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1529 struct smb_Dir
*dirp
= talloc_zero(mem_ctx
, struct smb_Dir
);
1530 struct smbd_server_connection
*sconn
= conn
->sconn
;
1537 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1539 dirp
->dir_path
= talloc_strdup(dirp
, name
);
1540 if (!dirp
->dir_path
) {
1545 if (sconn
&& !sconn
->using_smb2
) {
1546 sconn
->searches
.dirhandles_open
++;
1548 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1550 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1552 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp
->dir_path
,
1564 /*******************************************************************
1565 Open a directory from an fsp.
1566 ********************************************************************/
1568 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1573 struct smb_Dir
*dirp
= talloc_zero(mem_ctx
, struct smb_Dir
);
1574 struct smbd_server_connection
*sconn
= conn
->sconn
;
1581 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1583 dirp
->dir_path
= talloc_strdup(dirp
, fsp
->fsp_name
->base_name
);
1584 if (!dirp
->dir_path
) {
1589 if (sconn
&& !sconn
->using_smb2
) {
1590 sconn
->searches
.dirhandles_open
++;
1592 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1594 if (fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
1595 dirp
->dir
= SMB_VFS_FDOPENDIR(fsp
, mask
, attr
);
1596 if (dirp
->dir
!= NULL
) {
1599 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1603 if (errno
!= ENOSYS
) {
1609 if (dirp
->dir
== NULL
) {
1610 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1611 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1615 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp
->dir_path
,
1628 /*******************************************************************
1629 Read from a directory.
1630 Return directory entry, current offset, and optional stat information.
1631 Don't check for veto or invisible files.
1632 ********************************************************************/
1634 const char *ReadDirName(struct smb_Dir
*dirp
, long *poffset
,
1635 SMB_STRUCT_STAT
*sbuf
, char **ptalloced
)
1638 char *talloced
= NULL
;
1639 connection_struct
*conn
= dirp
->conn
;
1641 /* Cheat to allow . and .. to be the first entries returned. */
1642 if (((*poffset
== START_OF_DIRECTORY_OFFSET
) ||
1643 (*poffset
== DOT_DOT_DIRECTORY_OFFSET
)) && (dirp
->file_number
< 2))
1645 if (dirp
->file_number
== 0) {
1647 *poffset
= dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1650 *poffset
= dirp
->offset
= DOT_DOT_DIRECTORY_OFFSET
;
1652 dirp
->file_number
++;
1657 if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
1658 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1662 /* A real offset, seek to it. */
1663 SeekDir(dirp
, *poffset
);
1665 while ((n
= vfs_readdirname(conn
, dirp
->dir
, sbuf
, &talloced
))) {
1666 /* Ignore . and .. - we've already returned them. */
1668 if ((n
[1] == '\0') || (n
[1] == '.' && n
[2] == '\0')) {
1669 TALLOC_FREE(talloced
);
1673 *poffset
= dirp
->offset
= SMB_VFS_TELLDIR(conn
, dirp
->dir
);
1674 *ptalloced
= talloced
;
1675 dirp
->file_number
++;
1678 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1683 /*******************************************************************
1684 Rewind to the start.
1685 ********************************************************************/
1687 void RewindDir(struct smb_Dir
*dirp
, long *poffset
)
1689 SMB_VFS_REWINDDIR(dirp
->conn
, dirp
->dir
);
1690 dirp
->file_number
= 0;
1691 dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1692 *poffset
= START_OF_DIRECTORY_OFFSET
;
1695 /*******************************************************************
1697 ********************************************************************/
1699 void SeekDir(struct smb_Dir
*dirp
, long offset
)
1701 if (offset
!= dirp
->offset
) {
1702 if (offset
== START_OF_DIRECTORY_OFFSET
) {
1703 RewindDir(dirp
, &offset
);
1705 * Ok we should really set the file number here
1706 * to 1 to enable ".." to be returned next. Trouble
1707 * is I'm worried about callers using SeekDir(dirp,0)
1708 * as equivalent to RewindDir(). So leave this alone
1711 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
1712 RewindDir(dirp
, &offset
);
1714 * Set the file number to 2 - we want to get the first
1715 * real file entry (the one we return after "..")
1716 * on the next ReadDir.
1718 dirp
->file_number
= 2;
1719 } else if (offset
== END_OF_DIRECTORY_OFFSET
) {
1720 ; /* Don't seek in this case. */
1722 SMB_VFS_SEEKDIR(dirp
->conn
, dirp
->dir
, offset
);
1724 dirp
->offset
= offset
;
1728 /*******************************************************************
1729 Tell a dir position.
1730 ********************************************************************/
1732 long TellDir(struct smb_Dir
*dirp
)
1734 return(dirp
->offset
);
1737 /*******************************************************************
1738 Add an entry into the dcache.
1739 ********************************************************************/
1741 static void DirCacheAdd(struct smb_Dir
*dirp
, const char *name
, long offset
)
1743 struct name_cache_entry
*e
;
1745 if (dirp
->name_cache_size
== 0) {
1749 if (dirp
->name_cache
== NULL
) {
1750 dirp
->name_cache
= talloc_zero_array(
1751 dirp
, struct name_cache_entry
, dirp
->name_cache_size
);
1753 if (dirp
->name_cache
== NULL
) {
1758 dirp
->name_cache_index
= (dirp
->name_cache_index
+1) %
1759 dirp
->name_cache_size
;
1760 e
= &dirp
->name_cache
[dirp
->name_cache_index
];
1761 TALLOC_FREE(e
->name
);
1762 e
->name
= talloc_strdup(dirp
, name
);
1766 /*******************************************************************
1767 Find an entry by name. Leave us at the offset after it.
1768 Don't check for veto or invisible files.
1769 ********************************************************************/
1771 bool SearchDir(struct smb_Dir
*dirp
, const char *name
, long *poffset
)
1774 const char *entry
= NULL
;
1775 char *talloced
= NULL
;
1776 connection_struct
*conn
= dirp
->conn
;
1778 /* Search back in the name cache. */
1779 if (dirp
->name_cache_size
&& dirp
->name_cache
) {
1780 for (i
= dirp
->name_cache_index
; i
>= 0; i
--) {
1781 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1782 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1783 *poffset
= e
->offset
;
1784 SeekDir(dirp
, e
->offset
);
1788 for (i
= dirp
->name_cache_size
- 1; i
> dirp
->name_cache_index
; i
--) {
1789 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1790 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1791 *poffset
= e
->offset
;
1792 SeekDir(dirp
, e
->offset
);
1798 /* Not found in the name cache. Rewind directory and start from scratch. */
1799 SMB_VFS_REWINDDIR(conn
, dirp
->dir
);
1800 dirp
->file_number
= 0;
1801 *poffset
= START_OF_DIRECTORY_OFFSET
;
1802 while ((entry
= ReadDirName(dirp
, poffset
, NULL
, &talloced
))) {
1803 if (conn
->case_sensitive
? (strcmp(entry
, name
) == 0) : strequal(entry
, name
)) {
1804 TALLOC_FREE(talloced
);
1807 TALLOC_FREE(talloced
);
1812 struct files_below_forall_state
{
1815 int (*fn
)(struct file_id fid
, const struct share_mode_data
*data
,
1816 void *private_data
);
1820 static int files_below_forall_fn(struct file_id fid
,
1821 const struct share_mode_data
*data
,
1824 struct files_below_forall_state
*state
= private_data
;
1825 char tmpbuf
[PATH_MAX
];
1826 char *fullpath
, *to_free
;
1829 len
= full_path_tos(data
->servicepath
, data
->base_name
,
1830 tmpbuf
, sizeof(tmpbuf
),
1831 &fullpath
, &to_free
);
1835 if (state
->dirpath_len
>= len
) {
1837 * Filter files above dirpath
1841 if (fullpath
[state
->dirpath_len
] != '/') {
1843 * Filter file that don't have a path separator at the end of
1849 if (memcmp(state
->dirpath
, fullpath
, len
) != 0) {
1856 return state
->fn(fid
, data
, private_data
);
1859 static int files_below_forall(connection_struct
*conn
,
1860 const struct smb_filename
*dir_name
,
1861 int (*fn
)(struct file_id fid
,
1862 const struct share_mode_data
*data
,
1863 void *private_data
),
1866 struct files_below_forall_state state
= {};
1868 char tmpbuf
[PATH_MAX
];
1871 state
.dirpath_len
= full_path_tos(conn
->connectpath
,
1872 dir_name
->base_name
,
1873 tmpbuf
, sizeof(tmpbuf
),
1874 &state
.dirpath
, &to_free
);
1875 if (state
.dirpath_len
== -1) {
1880 ret
= share_mode_forall(files_below_forall_fn
, &state
);
1881 TALLOC_FREE(to_free
);
1885 struct have_file_open_below_state
{
1889 static int have_file_open_below_fn(struct file_id fid
,
1890 const struct share_mode_data
*data
,
1893 struct have_file_open_below_state
*state
= private_data
;
1894 state
->found_one
= true;
1898 static bool have_file_open_below(connection_struct
*conn
,
1899 const struct smb_filename
*name
)
1901 struct have_file_open_below_state state
= {};
1904 if (!VALID_STAT(name
->st
)) {
1907 if (!S_ISDIR(name
->st
.st_ex_mode
)) {
1911 ret
= files_below_forall(conn
, name
, have_file_open_below_fn
, &state
);
1916 return state
.found_one
;
1919 /*****************************************************************
1920 Is this directory empty ?
1921 *****************************************************************/
1923 NTSTATUS
can_delete_directory_fsp(files_struct
*fsp
)
1925 NTSTATUS status
= NT_STATUS_OK
;
1927 const char *dname
= NULL
;
1928 const char *dirname
= fsp
->fsp_name
->base_name
;
1929 char *talloced
= NULL
;
1931 struct connection_struct
*conn
= fsp
->conn
;
1932 struct smb_Dir
*dir_hnd
= OpenDir_fsp(talloc_tos(),
1939 return map_nt_error_from_unix(errno
);
1942 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
, &talloced
))) {
1943 /* Quick check for "." and ".." */
1944 if (dname
[0] == '.') {
1945 if (!dname
[1] || (dname
[1] == '.' && !dname
[2])) {
1946 TALLOC_FREE(talloced
);
1951 if (!is_visible_file(conn
, dirname
, dname
, &st
, True
)) {
1952 TALLOC_FREE(talloced
);
1956 DEBUG(10,("got name %s - can't delete\n",
1958 status
= NT_STATUS_DIRECTORY_NOT_EMPTY
;
1961 TALLOC_FREE(talloced
);
1962 TALLOC_FREE(dir_hnd
);
1964 if (!NT_STATUS_IS_OK(status
)) {
1968 if (!lp_posix_pathnames() &&
1969 lp_strict_rename(SNUM(conn
)) &&
1970 have_file_open_below(fsp
->conn
, fsp
->fsp_name
))
1972 return NT_STATUS_ACCESS_DENIED
;
1975 return NT_STATUS_OK
;