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"
30 This module implements directory related functions for Samba.
33 /* "Special" directory offsets. */
34 #define END_OF_DIRECTORY_OFFSET ((long)-1)
35 #define START_OF_DIRECTORY_OFFSET ((long)0)
36 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
38 /* "Special" directory offsets in 32-bit wire format. */
39 #define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
40 #define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
41 #define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
43 /* Make directory handle internals available. */
45 struct name_cache_entry
{
51 connection_struct
*conn
;
55 size_t name_cache_size
;
56 struct name_cache_entry
*name_cache
;
57 unsigned int name_cache_index
;
58 unsigned int file_number
;
59 files_struct
*fsp
; /* Back pointer to containing fsp, only
60 set from OpenDir_fsp(). */
64 struct dptr_struct
*next
, *prev
;
67 struct connection_struct
*conn
;
68 struct smb_Dir
*dir_hnd
;
73 bool has_wild
; /* Set to true if the wcard entry has MS wildcard characters in it. */
74 bool did_stat
; /* Optimisation for non-wcard searches. */
75 bool priv
; /* Directory handle opened with privilege. */
77 struct memcache
*dptr_cache
;
80 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
85 #define INVALID_DPTR_KEY (-3)
87 /****************************************************************************
89 ****************************************************************************/
91 bool make_dir_struct(TALLOC_CTX
*ctx
,
101 char *mask2
= talloc_strdup(ctx
, mask
);
107 if ((mode
& FILE_ATTRIBUTE_DIRECTORY
) != 0) {
111 memset(buf
+1,' ',11);
112 if ((p
= strchr_m(mask2
,'.')) != NULL
) {
114 push_ascii(buf
+1,mask2
,8, 0);
115 push_ascii(buf
+9,p
+1,3, 0);
118 push_ascii(buf
+1,mask2
,11, 0);
121 memset(buf
+21,'\0',DIR_STRUCT_SIZE
-21);
123 srv_put_dos_date(buf
,22,date
);
124 SSVAL(buf
,26,size
& 0xFFFF);
125 SSVAL(buf
,28,(size
>> 16)&0xFFFF);
126 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
127 Strange, but verified on W2K3. Needed for OS/2. JRA. */
128 push_ascii(buf
+30,fname
,12, uc
? STR_UPPER
: 0);
129 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf
+30, fname
));
133 /****************************************************************************
134 Initialise the dir bitmap.
135 ****************************************************************************/
137 bool init_dptrs(struct smbd_server_connection
*sconn
)
139 if (sconn
->searches
.dptr_bmap
) {
143 sconn
->searches
.dptr_bmap
= bitmap_talloc(
144 sconn
, MAX_DIRECTORY_HANDLES
);
146 if (sconn
->searches
.dptr_bmap
== NULL
) {
153 /****************************************************************************
154 Idle a dptr - the directory is closed but the control info is kept.
155 ****************************************************************************/
157 static void dptr_idle(struct dptr_struct
*dptr
)
160 DEBUG(4,("Idling dptr dnum %d\n",dptr
->dnum
));
161 TALLOC_FREE(dptr
->dir_hnd
);
162 TALLOC_FREE(dptr
->dptr_cache
);
167 /****************************************************************************
168 Idle the oldest dptr.
169 ****************************************************************************/
171 static void dptr_idleoldest(struct smbd_server_connection
*sconn
)
173 struct dptr_struct
*dptr
;
176 * Go to the end of the list.
178 dptr
= DLIST_TAIL(sconn
->searches
.dirptrs
);
181 DEBUG(0,("No dptrs available to idle ?\n"));
186 * Idle the oldest pointer.
189 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
197 /****************************************************************************
198 Get the struct dptr_struct for a dir index.
199 ****************************************************************************/
201 static struct dptr_struct
*dptr_get(struct smbd_server_connection
*sconn
,
202 int key
, bool forclose
)
204 struct dptr_struct
*dptr
;
206 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
207 if(dptr
->dnum
== key
) {
208 if (!forclose
&& !dptr
->dir_hnd
) {
209 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
)
210 dptr_idleoldest(sconn
);
211 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key
));
212 if (!(dptr
->dir_hnd
= OpenDir(
213 NULL
, dptr
->conn
, dptr
->path
,
214 dptr
->wcard
, dptr
->attr
))) {
215 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr
->path
,
220 DLIST_PROMOTE(sconn
->searches
.dirptrs
,dptr
);
227 /****************************************************************************
228 Get the dir path for a dir index.
229 ****************************************************************************/
231 const char *dptr_path(struct smbd_server_connection
*sconn
, int key
)
233 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
239 /****************************************************************************
240 Get the dir wcard for a dir index.
241 ****************************************************************************/
243 const char *dptr_wcard(struct smbd_server_connection
*sconn
, int key
)
245 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
251 /****************************************************************************
252 Get the dir attrib for a dir index.
253 ****************************************************************************/
255 uint16
dptr_attr(struct smbd_server_connection
*sconn
, int key
)
257 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
263 /****************************************************************************
264 Close a dptr (internal func).
265 ****************************************************************************/
267 static void dptr_close_internal(struct dptr_struct
*dptr
)
269 struct smbd_server_connection
*sconn
= dptr
->conn
->sconn
;
271 DEBUG(4,("closing dptr key %d\n",dptr
->dnum
));
277 if (sconn
->using_smb2
) {
281 DLIST_REMOVE(sconn
->searches
.dirptrs
, dptr
);
284 * Free the dnum in the bitmap. Remember the dnum value is always
285 * biased by one with respect to the bitmap.
288 if (!bitmap_query(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1)) {
289 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
293 bitmap_clear(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1);
296 TALLOC_FREE(dptr
->dir_hnd
);
300 /****************************************************************************
301 Close a dptr given a key.
302 ****************************************************************************/
304 void dptr_close(struct smbd_server_connection
*sconn
, int *key
)
306 struct dptr_struct
*dptr
;
308 if(*key
== INVALID_DPTR_KEY
)
311 /* OS/2 seems to use -1 to indicate "close all directories" */
313 struct dptr_struct
*next
;
314 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
316 dptr_close_internal(dptr
);
318 *key
= INVALID_DPTR_KEY
;
322 dptr
= dptr_get(sconn
, *key
, true);
325 DEBUG(0,("Invalid key %d given to dptr_close\n", *key
));
329 dptr_close_internal(dptr
);
331 *key
= INVALID_DPTR_KEY
;
334 /****************************************************************************
335 Close all dptrs for a cnum.
336 ****************************************************************************/
338 void dptr_closecnum(connection_struct
*conn
)
340 struct dptr_struct
*dptr
, *next
;
341 struct smbd_server_connection
*sconn
= conn
->sconn
;
347 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
349 if (dptr
->conn
== conn
) {
350 dptr_close_internal(dptr
);
355 /****************************************************************************
356 Idle all dptrs for a cnum.
357 ****************************************************************************/
359 void dptr_idlecnum(connection_struct
*conn
)
361 struct dptr_struct
*dptr
;
362 struct smbd_server_connection
*sconn
= conn
->sconn
;
368 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
369 if (dptr
->conn
== conn
&& dptr
->dir_hnd
) {
375 /****************************************************************************
376 Close a dptr that matches a given path, only if it matches the spid also.
377 ****************************************************************************/
379 void dptr_closepath(struct smbd_server_connection
*sconn
,
380 char *path
,uint16 spid
)
382 struct dptr_struct
*dptr
, *next
;
383 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
385 if (spid
== dptr
->spid
&& strequal(dptr
->path
,path
))
386 dptr_close_internal(dptr
);
390 /****************************************************************************
391 Try and close the oldest handle not marked for
392 expect close in the hope that the client has
393 finished with that one.
394 ****************************************************************************/
396 static void dptr_close_oldest(struct smbd_server_connection
*sconn
,
399 struct dptr_struct
*dptr
;
402 * Go to the end of the list.
404 for(dptr
= sconn
->searches
.dirptrs
; dptr
&& dptr
->next
; dptr
= dptr
->next
)
408 DEBUG(0,("No old dptrs available to close oldest ?\n"));
413 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
414 * does not have expect_close set. If 'old' is false, close
415 * one of the new dnum handles.
418 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
419 if ((old
&& (dptr
->dnum
< 256) && !dptr
->expect_close
) ||
420 (!old
&& (dptr
->dnum
> 255))) {
421 dptr_close_internal(dptr
);
427 /****************************************************************************
428 Safely do an OpenDir as root, ensuring we're in the right place.
429 ****************************************************************************/
431 static struct smb_Dir
*open_dir_with_privilege(connection_struct
*conn
,
432 struct smb_request
*req
,
438 struct smb_Dir
*dir_hnd
= NULL
;
439 struct smb_filename
*smb_fname_cwd
= NULL
;
440 char *saved_dir
= vfs_GetWd(talloc_tos(), conn
);
441 struct privilege_paths
*priv_paths
= req
->priv_paths
;
444 if (saved_dir
== NULL
) {
448 if (vfs_ChDir(conn
, path
) == -1) {
452 /* Now check the stat value is the same. */
453 status
= create_synthetic_smb_fname(talloc_tos(), ".",
457 if (!NT_STATUS_IS_OK(status
)) {
460 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
465 if (!check_same_stat(&smb_fname_cwd
->st
, &priv_paths
->parent_name
.st
)) {
466 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
469 smb_fname_str_dbg(&priv_paths
->parent_name
)));
473 dir_hnd
= OpenDir(NULL
, conn
, ".", wcard
, attr
);
477 vfs_ChDir(conn
, saved_dir
);
481 /****************************************************************************
482 Create a new dir ptr. If the flag old_handle is true then we must allocate
483 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
484 one byte long. If old_handle is false we allocate from the range
485 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
486 a directory handle is never zero.
487 wcard must not be zero.
488 ****************************************************************************/
490 NTSTATUS
dptr_create(connection_struct
*conn
,
491 struct smb_request
*req
,
493 const char *path
, bool old_handle
, bool expect_close
,uint16 spid
,
494 const char *wcard
, bool wcard_has_wild
, uint32 attr
, struct dptr_struct
**dptr_ret
)
496 struct smbd_server_connection
*sconn
= conn
->sconn
;
497 struct dptr_struct
*dptr
= NULL
;
498 struct smb_Dir
*dir_hnd
;
500 if (fsp
&& fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
501 path
= fsp
->fsp_name
->base_name
;
504 DEBUG(5,("dptr_create dir=%s\n", path
));
507 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
508 return NT_STATUS_INTERNAL_ERROR
;
512 return NT_STATUS_INVALID_PARAMETER
;
516 if (!(fsp
->access_mask
& SEC_DIR_LIST
)) {
517 DEBUG(5,("dptr_create: directory %s "
518 "not open for LIST access\n",
520 return NT_STATUS_ACCESS_DENIED
;
522 dir_hnd
= OpenDir_fsp(NULL
, conn
, fsp
, wcard
, attr
);
525 struct smb_filename
*smb_dname
= NULL
;
526 NTSTATUS status
= create_synthetic_smb_fname(talloc_tos(),
531 if (!NT_STATUS_IS_OK(status
)) {
534 if (lp_posix_pathnames()) {
535 ret
= SMB_VFS_LSTAT(conn
, smb_dname
);
537 ret
= SMB_VFS_STAT(conn
, smb_dname
);
540 return map_nt_error_from_unix(errno
);
542 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
543 return NT_STATUS_NOT_A_DIRECTORY
;
545 status
= smbd_check_access_rights(conn
,
548 if (!NT_STATUS_IS_OK(status
)) {
551 if (req
&& req
->priv_paths
) {
552 dir_hnd
= open_dir_with_privilege(conn
,
558 dir_hnd
= OpenDir(NULL
, conn
, path
, wcard
, attr
);
563 return map_nt_error_from_unix(errno
);
566 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
) {
567 dptr_idleoldest(sconn
);
570 dptr
= talloc(NULL
, struct dptr_struct
);
572 DEBUG(0,("talloc fail in dptr_create.\n"));
573 TALLOC_FREE(dir_hnd
);
574 return NT_STATUS_NO_MEMORY
;
579 dptr
->path
= talloc_strdup(dptr
, path
);
582 TALLOC_FREE(dir_hnd
);
583 return NT_STATUS_NO_MEMORY
;
586 dptr
->dir_hnd
= dir_hnd
;
588 dptr
->expect_close
= expect_close
;
589 dptr
->wcard
= talloc_strdup(dptr
, wcard
);
592 TALLOC_FREE(dir_hnd
);
593 return NT_STATUS_NO_MEMORY
;
595 if (lp_posix_pathnames() || (wcard
[0] == '.' && wcard
[1] == 0)) {
596 dptr
->has_wild
= True
;
598 dptr
->has_wild
= wcard_has_wild
;
603 if (sconn
->using_smb2
) {
610 * This is an old-style SMBsearch request. Ensure the
611 * value we return will fit in the range 1-255.
614 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
616 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
619 * Try and close the oldest handle not marked for
620 * expect close in the hope that the client has
621 * finished with that one.
624 dptr_close_oldest(sconn
, true);
626 /* Now try again... */
627 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
628 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
629 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr
->dnum
));
631 TALLOC_FREE(dir_hnd
);
632 return NT_STATUS_TOO_MANY_OPENED_FILES
;
638 * This is a new-style trans2 request. Allocate from
639 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
642 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
644 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
647 * Try and close the oldest handle close in the hope that
648 * the client has finished with that one. This will only
649 * happen in the case of the Win98 client bug where it leaks
653 dptr_close_oldest(sconn
, false);
655 /* Now try again... */
656 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
658 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
659 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr
->dnum
));
661 TALLOC_FREE(dir_hnd
);
662 return NT_STATUS_TOO_MANY_OPENED_FILES
;
667 bitmap_set(sconn
->searches
.dptr_bmap
, dptr
->dnum
);
669 dptr
->dnum
+= 1; /* Always bias the dnum by one - no zero dnums allowed. */
671 DLIST_ADD(sconn
->searches
.dirptrs
, dptr
);
674 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
675 dptr
->dnum
,path
,expect_close
));
683 /****************************************************************************
684 Wrapper functions to access the lower level directory handles.
685 ****************************************************************************/
687 void dptr_CloseDir(files_struct
*fsp
)
691 * The destructor for the struct smb_Dir
692 * (fsp->dptr->dir_hnd) now handles
693 * all resource deallocation.
695 dptr_close_internal(fsp
->dptr
);
700 void dptr_SeekDir(struct dptr_struct
*dptr
, long offset
)
702 SeekDir(dptr
->dir_hnd
, offset
);
705 long dptr_TellDir(struct dptr_struct
*dptr
)
707 return TellDir(dptr
->dir_hnd
);
710 bool dptr_has_wild(struct dptr_struct
*dptr
)
712 return dptr
->has_wild
;
715 int dptr_dnum(struct dptr_struct
*dptr
)
720 bool dptr_get_priv(struct dptr_struct
*dptr
)
725 void dptr_set_priv(struct dptr_struct
*dptr
)
730 /****************************************************************************
731 Return the next visible file name, skipping veto'd and invisible files.
732 ****************************************************************************/
734 static const char *dptr_normal_ReadDirName(struct dptr_struct
*dptr
,
735 long *poffset
, SMB_STRUCT_STAT
*pst
,
738 /* Normal search for the next file. */
740 char *talloced
= NULL
;
742 while ((name
= ReadDirName(dptr
->dir_hnd
, poffset
, pst
, &talloced
))
744 if (is_visible_file(dptr
->conn
, dptr
->path
, name
, pst
, True
)) {
745 *ptalloced
= talloced
;
748 TALLOC_FREE(talloced
);
753 /****************************************************************************
754 Return the next visible file name, skipping veto'd and invisible files.
755 ****************************************************************************/
757 char *dptr_ReadDirName(TALLOC_CTX
*ctx
,
758 struct dptr_struct
*dptr
,
760 SMB_STRUCT_STAT
*pst
)
762 struct smb_filename smb_fname_base
;
764 const char *name_temp
= NULL
;
765 char *talloced
= NULL
;
766 char *pathreal
= NULL
;
767 char *found_name
= NULL
;
770 SET_STAT_INVALID(*pst
);
772 if (dptr
->has_wild
|| dptr
->did_stat
) {
773 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
,
775 if (name_temp
== NULL
) {
778 if (talloced
!= NULL
) {
779 return talloc_move(ctx
, &talloced
);
781 return talloc_strdup(ctx
, name_temp
);
784 /* If poffset is -1 then we know we returned this name before and we
785 * have no wildcards. We're at the end of the directory. */
786 if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
790 /* We know the stored wcard contains no wildcard characters.
791 * See if we can match with a stat call. If we can't, then set
792 * did_stat to true to ensure we only do this once and keep
795 dptr
->did_stat
= true;
797 /* First check if it should be visible. */
798 if (!is_visible_file(dptr
->conn
, dptr
->path
, dptr
->wcard
,
801 /* This only returns false if the file was found, but
802 is explicitly not visible. Set us to end of
803 directory, but return NULL as we know we can't ever
808 if (VALID_STAT(*pst
)) {
809 name
= talloc_strdup(ctx
, dptr
->wcard
);
813 pathreal
= talloc_asprintf(ctx
,
820 /* Create an smb_filename with stream_name == NULL. */
821 ZERO_STRUCT(smb_fname_base
);
822 smb_fname_base
.base_name
= pathreal
;
824 if (SMB_VFS_STAT(dptr
->conn
, &smb_fname_base
) == 0) {
825 *pst
= smb_fname_base
.st
;
826 name
= talloc_strdup(ctx
, dptr
->wcard
);
829 /* If we get any other error than ENOENT or ENOTDIR
830 then the file exists we just can't stat it. */
831 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
832 name
= talloc_strdup(ctx
, dptr
->wcard
);
837 /* Stat failed. We know this is authoratiative if we are
838 * providing case sensitive semantics or the underlying
839 * filesystem is case sensitive.
841 if (dptr
->conn
->case_sensitive
||
842 !(dptr
->conn
->fs_capabilities
& FILE_CASE_SENSITIVE_SEARCH
))
848 * Try case-insensitive stat if the fs has the ability. This avoids
849 * scanning the whole directory.
851 ret
= SMB_VFS_GET_REAL_FILENAME(dptr
->conn
, dptr
->path
, dptr
->wcard
,
856 } else if (errno
== ENOENT
) {
857 /* The case-insensitive lookup was authoritative. */
861 TALLOC_FREE(pathreal
);
863 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
, &talloced
);
864 if (name_temp
== NULL
) {
867 if (talloced
!= NULL
) {
868 return talloc_move(ctx
, &talloced
);
870 return talloc_strdup(ctx
, name_temp
);
873 TALLOC_FREE(pathreal
);
875 /* We need to set the underlying dir_hnd offset to -1
876 * also as this function is usually called with the
877 * output from TellDir. */
878 dptr
->dir_hnd
->offset
= *poffset
= END_OF_DIRECTORY_OFFSET
;
882 /****************************************************************************
883 Search for a file by name, skipping veto'ed and not visible files.
884 ****************************************************************************/
886 bool dptr_SearchDir(struct dptr_struct
*dptr
, const char *name
, long *poffset
, SMB_STRUCT_STAT
*pst
)
888 SET_STAT_INVALID(*pst
);
890 if (!dptr
->has_wild
&& (dptr
->dir_hnd
->offset
== END_OF_DIRECTORY_OFFSET
)) {
891 /* This is a singleton directory and we're already at the end. */
892 *poffset
= END_OF_DIRECTORY_OFFSET
;
896 return SearchDir(dptr
->dir_hnd
, name
, poffset
);
899 /****************************************************************************
900 Add the name we're returning into the underlying cache.
901 ****************************************************************************/
903 void dptr_DirCacheAdd(struct dptr_struct
*dptr
, const char *name
, long offset
)
905 DirCacheAdd(dptr
->dir_hnd
, name
, offset
);
908 /****************************************************************************
909 Initialize variables & state data at the beginning of all search SMB requests.
910 ****************************************************************************/
911 void dptr_init_search_op(struct dptr_struct
*dptr
)
913 SMB_VFS_INIT_SEARCH_OP(dptr
->conn
, dptr
->dir_hnd
->dir
);
916 /****************************************************************************
917 Map a native directory offset to a 32-bit cookie.
918 ****************************************************************************/
920 static uint32_t map_dir_offset_to_wire(struct dptr_struct
*dptr
, long offset
)
925 if (offset
== END_OF_DIRECTORY_OFFSET
) {
926 return WIRE_END_OF_DIRECTORY_OFFSET
;
927 } else if(offset
== START_OF_DIRECTORY_OFFSET
) {
928 return WIRE_START_OF_DIRECTORY_OFFSET
;
929 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
930 return WIRE_DOT_DOT_DIRECTORY_OFFSET
;
932 if (sizeof(long) == 4) {
933 /* 32-bit machine. We can cheat... */
934 return (uint32_t)offset
;
936 if (dptr
->dptr_cache
== NULL
) {
937 /* Lazy initialize cache. */
938 dptr
->dptr_cache
= memcache_init(dptr
, 0);
939 if (dptr
->dptr_cache
== NULL
) {
940 return WIRE_END_OF_DIRECTORY_OFFSET
;
943 /* Have we seen this offset before ? */
944 key
.data
= (void *)&offset
;
945 key
.length
= sizeof(offset
);
946 if (memcache_lookup(dptr
->dptr_cache
,
947 SMB1_SEARCH_OFFSET_MAP
,
950 uint32_t wire_offset
;
951 SMB_ASSERT(val
.length
== sizeof(wire_offset
));
952 memcpy(&wire_offset
, val
.data
, sizeof(wire_offset
));
953 DEBUG(10,("found wire %u <-> offset %ld\n",
954 (unsigned int)wire_offset
,
959 /* Allocate a new wire cookie. */
962 } while (dptr
->counter
== WIRE_START_OF_DIRECTORY_OFFSET
||
963 dptr
->counter
== WIRE_END_OF_DIRECTORY_OFFSET
||
964 dptr
->counter
== WIRE_DOT_DOT_DIRECTORY_OFFSET
);
965 /* Store it in the cache. */
966 key
.data
= (void *)&offset
;
967 key
.length
= sizeof(offset
);
968 val
.data
= (void *)&dptr
->counter
;
969 val
.length
= sizeof(dptr
->counter
); /* MUST BE uint32_t ! */
970 memcache_add(dptr
->dptr_cache
,
971 SMB1_SEARCH_OFFSET_MAP
,
974 /* And the reverse mapping for lookup from
975 map_wire_to_dir_offset(). */
976 memcache_add(dptr
->dptr_cache
,
977 SMB1_SEARCH_OFFSET_MAP
,
980 DEBUG(10,("stored wire %u <-> offset %ld\n",
981 (unsigned int)dptr
->counter
,
983 return dptr
->counter
;
986 /****************************************************************************
987 Fill the 5 byte server reserved dptr field.
988 ****************************************************************************/
990 bool dptr_fill(struct smbd_server_connection
*sconn
,
991 char *buf1
,unsigned int key
)
993 unsigned char *buf
= (unsigned char *)buf1
;
994 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
995 uint32_t wire_offset
;
997 DEBUG(1,("filling null dirptr %d\n",key
));
1000 wire_offset
= map_dir_offset_to_wire(dptr
,TellDir(dptr
->dir_hnd
));
1001 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key
,
1002 (long)dptr
->dir_hnd
,(int)wire_offset
));
1004 SIVAL(buf
,1,wire_offset
);
1008 /****************************************************************************
1009 Map a 32-bit wire cookie to a native directory offset.
1010 ****************************************************************************/
1012 static long map_wire_to_dir_offset(struct dptr_struct
*dptr
, uint32_t wire_offset
)
1017 if (wire_offset
== WIRE_END_OF_DIRECTORY_OFFSET
) {
1018 return END_OF_DIRECTORY_OFFSET
;
1019 } else if(wire_offset
== WIRE_START_OF_DIRECTORY_OFFSET
) {
1020 return START_OF_DIRECTORY_OFFSET
;
1021 } else if (wire_offset
== WIRE_DOT_DOT_DIRECTORY_OFFSET
) {
1022 return DOT_DOT_DIRECTORY_OFFSET
;
1024 if (sizeof(long) == 4) {
1025 /* 32-bit machine. We can cheat... */
1026 return (long)wire_offset
;
1028 if (dptr
->dptr_cache
== NULL
) {
1029 /* Logic error, cache should be initialized. */
1030 return END_OF_DIRECTORY_OFFSET
;
1032 key
.data
= (void *)&wire_offset
;
1033 key
.length
= sizeof(wire_offset
);
1034 if (memcache_lookup(dptr
->dptr_cache
,
1035 SMB1_SEARCH_OFFSET_MAP
,
1038 /* Found mapping. */
1040 SMB_ASSERT(val
.length
== sizeof(offset
));
1041 memcpy(&offset
, val
.data
, sizeof(offset
));
1042 DEBUG(10,("lookup wire %u <-> offset %ld\n",
1043 (unsigned int)wire_offset
,
1047 return END_OF_DIRECTORY_OFFSET
;
1050 /****************************************************************************
1051 Fetch the dir ptr and seek it given the 5 byte server field.
1052 ****************************************************************************/
1054 struct dptr_struct
*dptr_fetch(struct smbd_server_connection
*sconn
,
1055 char *buf
, int *num
)
1057 unsigned int key
= *(unsigned char *)buf
;
1058 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
1059 uint32_t wire_offset
;
1063 DEBUG(3,("fetched null dirptr %d\n",key
));
1067 wire_offset
= IVAL(buf
,1);
1068 seekoff
= map_wire_to_dir_offset(dptr
, wire_offset
);
1069 SeekDir(dptr
->dir_hnd
,seekoff
);
1070 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1071 key
, dptr
->path
, (int)seekoff
));
1075 /****************************************************************************
1077 ****************************************************************************/
1079 struct dptr_struct
*dptr_fetch_lanman2(struct smbd_server_connection
*sconn
,
1082 struct dptr_struct
*dptr
= dptr_get(sconn
, dptr_num
, false);
1085 DEBUG(3,("fetched null dirptr %d\n",dptr_num
));
1088 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num
,dptr
->path
));
1092 /****************************************************************************
1093 Check that a file matches a particular file type.
1094 ****************************************************************************/
1096 bool dir_check_ftype(connection_struct
*conn
, uint32 mode
, uint32 dirtype
)
1100 /* Check the "may have" search bits. */
1101 if (((mode
& ~dirtype
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
)) != 0)
1104 /* Check the "must have" bits, which are the may have bits shifted eight */
1105 /* If must have bit is set, the file/dir can not be returned in search unless the matching
1106 file attribute is set */
1107 mask
= ((dirtype
>> 8) & (FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
)); /* & 0x37 */
1109 if((mask
& (mode
& (FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
))) == mask
) /* check if matching attribute present */
1118 static bool mangle_mask_match(connection_struct
*conn
,
1119 const char *filename
,
1124 if (!name_to_8_3(filename
,mname
,False
,conn
->params
)) {
1127 return mask_match_search(mname
,mask
,False
);
1130 bool smbd_dirptr_get_entry(TALLOC_CTX
*ctx
,
1131 struct dptr_struct
*dirptr
,
1136 bool (*match_fn
)(TALLOC_CTX
*ctx
,
1141 bool (*mode_fn
)(TALLOC_CTX
*ctx
,
1143 struct smb_filename
*smb_fname
,
1147 struct smb_filename
**_smb_fname
,
1151 connection_struct
*conn
= dirptr
->conn
;
1158 pathlen
= strlen(dirptr
->path
);
1159 slashlen
= ( dirptr
->path
[pathlen
-1] != '/') ? 1 : 0;
1164 SMB_STRUCT_STAT sbuf
;
1168 char *pathreal
= NULL
;
1169 struct smb_filename smb_fname
;
1174 cur_offset
= dptr_TellDir(dirptr
);
1175 prev_offset
= cur_offset
;
1176 dname
= dptr_ReadDirName(ctx
, dirptr
, &cur_offset
, &sbuf
);
1178 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1179 (long)dirptr
, cur_offset
));
1181 if (dname
== NULL
) {
1185 isdots
= (ISDOT(dname
) || ISDOTDOT(dname
));
1186 if (dont_descend
&& !isdots
) {
1192 * fname may get mangled, dname is never mangled.
1193 * Whenever we're accessing the filesystem we use
1194 * pathreal which is composed from dname.
1197 ok
= match_fn(ctx
, private_data
, dname
, mask
, &fname
);
1205 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1206 * needslash?"/":"", dname);
1207 * but this was measurably slower than doing the memcpy.
1210 pathreal
= talloc_array(
1212 pathlen
+ slashlen
+ talloc_get_size(dname
));
1219 memcpy(pathreal
, dirptr
->path
, pathlen
);
1220 pathreal
[pathlen
] = '/';
1221 memcpy(pathreal
+ slashlen
+ pathlen
, dname
,
1222 talloc_get_size(dname
));
1224 /* Create smb_fname with NULL stream_name. */
1225 ZERO_STRUCT(smb_fname
);
1226 smb_fname
.base_name
= pathreal
;
1227 smb_fname
.st
= sbuf
;
1229 ok
= mode_fn(ctx
, private_data
, &smb_fname
, &mode
);
1233 TALLOC_FREE(pathreal
);
1237 if (!dir_check_ftype(conn
, mode
, dirtype
)) {
1238 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1239 fname
, (unsigned int)mode
, (unsigned int)dirtype
));
1242 TALLOC_FREE(pathreal
);
1246 if (ask_sharemode
) {
1247 struct timespec write_time_ts
;
1248 struct file_id fileid
;
1250 fileid
= vfs_file_id_from_sbuf(conn
,
1252 get_file_infos(fileid
, 0, NULL
, &write_time_ts
);
1253 if (!null_timespec(write_time_ts
)) {
1254 update_stat_ex_mtime(&smb_fname
.st
,
1259 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1261 mask
, smb_fname_str_dbg(&smb_fname
),
1264 DirCacheAdd(dirptr
->dir_hnd
, dname
, cur_offset
);
1268 status
= copy_smb_filename(ctx
, &smb_fname
, _smb_fname
);
1269 TALLOC_FREE(pathreal
);
1270 if (!NT_STATUS_IS_OK(status
)) {
1275 *_prev_offset
= prev_offset
;
1283 /****************************************************************************
1284 Get an 8.3 directory entry.
1285 ****************************************************************************/
1287 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX
*ctx
,
1293 connection_struct
*conn
= (connection_struct
*)private_data
;
1295 if ((strcmp(mask
,"*.*") == 0) ||
1296 mask_match_search(dname
, mask
, false) ||
1297 mangle_mask_match(conn
, dname
, mask
)) {
1301 if (!mangle_is_8_3(dname
, false, conn
->params
)) {
1302 bool ok
= name_to_8_3(dname
, mname
, false,
1312 *_fname
= talloc_strdup(ctx
, fname
);
1313 if (*_fname
== NULL
) {
1323 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX
*ctx
,
1325 struct smb_filename
*smb_fname
,
1328 connection_struct
*conn
= (connection_struct
*)private_data
;
1330 if (!VALID_STAT(smb_fname
->st
)) {
1331 if ((SMB_VFS_STAT(conn
, smb_fname
)) != 0) {
1332 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1333 "Couldn't stat [%s]. Error "
1335 smb_fname_str_dbg(smb_fname
),
1341 *_mode
= dos_mode(conn
, smb_fname
);
1345 bool get_dir_entry(TALLOC_CTX
*ctx
,
1346 struct dptr_struct
*dirptr
,
1352 struct timespec
*_date
,
1356 connection_struct
*conn
= dirptr
->conn
;
1358 struct smb_filename
*smb_fname
= NULL
;
1363 ok
= smbd_dirptr_get_entry(ctx
,
1369 smbd_dirptr_8_3_match_fn
,
1370 smbd_dirptr_8_3_mode_fn
,
1380 *_fname
= talloc_move(ctx
, &fname
);
1381 *_size
= smb_fname
->st
.st_ex_size
;
1383 *_date
= smb_fname
->st
.st_ex_mtime
;
1384 TALLOC_FREE(smb_fname
);
1388 /*******************************************************************
1389 Check to see if a user can read a file. This is only approximate,
1390 it is used as part of the "hide unreadable" option. Don't
1391 use it for anything security sensitive.
1392 ********************************************************************/
1394 static bool user_can_read_file(connection_struct
*conn
,
1395 struct smb_filename
*smb_fname
)
1398 * Never hide files from the root user.
1399 * We use (uid_t)0 here not sec_initial_uid()
1400 * as make test uses a single user context.
1403 if (get_current_uid(conn
) == (uid_t
)0) {
1407 return NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
1412 /*******************************************************************
1413 Check to see if a user can write a file (and only files, we do not
1414 check dirs on this one). This is only approximate,
1415 it is used as part of the "hide unwriteable" option. Don't
1416 use it for anything security sensitive.
1417 ********************************************************************/
1419 static bool user_can_write_file(connection_struct
*conn
,
1420 const struct smb_filename
*smb_fname
)
1423 * Never hide files from the root user.
1424 * We use (uid_t)0 here not sec_initial_uid()
1425 * as make test uses a single user context.
1428 if (get_current_uid(conn
) == (uid_t
)0) {
1432 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1434 /* Pseudo-open the file */
1436 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1440 return can_write_to_file(conn
, smb_fname
);
1443 /*******************************************************************
1444 Is a file a "special" type ?
1445 ********************************************************************/
1447 static bool file_is_special(connection_struct
*conn
,
1448 const struct smb_filename
*smb_fname
)
1451 * Never hide files from the root user.
1452 * We use (uid_t)0 here not sec_initial_uid()
1453 * as make test uses a single user context.
1456 if (get_current_uid(conn
) == (uid_t
)0) {
1460 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1462 if (S_ISREG(smb_fname
->st
.st_ex_mode
) ||
1463 S_ISDIR(smb_fname
->st
.st_ex_mode
) ||
1464 S_ISLNK(smb_fname
->st
.st_ex_mode
))
1470 /*******************************************************************
1471 Should the file be seen by the client?
1472 NOTE: A successful return is no guarantee of the file's existence.
1473 ********************************************************************/
1475 bool is_visible_file(connection_struct
*conn
, const char *dir_path
,
1476 const char *name
, SMB_STRUCT_STAT
*pst
, bool use_veto
)
1478 bool hide_unreadable
= lp_hideunreadable(SNUM(conn
));
1479 bool hide_unwriteable
= lp_hideunwriteable_files(SNUM(conn
));
1480 bool hide_special
= lp_hide_special_files(SNUM(conn
));
1482 struct smb_filename
*smb_fname_base
= NULL
;
1486 if ((strcmp(".",name
) == 0) || (strcmp("..",name
) == 0)) {
1487 return True
; /* . and .. are always visible. */
1490 /* If it's a vetoed file, pretend it doesn't even exist */
1491 if (use_veto
&& IS_VETO_PATH(conn
, name
)) {
1492 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name
));
1496 if (hide_unreadable
|| hide_unwriteable
|| hide_special
) {
1497 entry
= talloc_asprintf(talloc_tos(), "%s/%s", dir_path
, name
);
1503 /* Create an smb_filename with stream_name == NULL. */
1504 status
= create_synthetic_smb_fname(talloc_tos(), entry
, NULL
,
1505 pst
, &smb_fname_base
);
1506 if (!NT_STATUS_IS_OK(status
)) {
1511 /* If the file name does not exist, there's no point checking
1512 * the configuration options. We succeed, on the basis that the
1513 * checks *might* have passed if the file was present.
1515 if (!VALID_STAT(*pst
)) {
1516 if (SMB_VFS_STAT(conn
, smb_fname_base
) != 0) {
1520 *pst
= smb_fname_base
->st
;
1524 /* Honour _hide unreadable_ option */
1525 if (hide_unreadable
&&
1526 !user_can_read_file(conn
, smb_fname_base
)) {
1527 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1532 /* Honour _hide unwriteable_ option */
1533 if (hide_unwriteable
&& !user_can_write_file(conn
,
1535 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1540 /* Honour _hide_special_ option */
1541 if (hide_special
&& file_is_special(conn
, smb_fname_base
)) {
1542 DEBUG(10,("is_visible_file: file %s is special.\n",
1551 TALLOC_FREE(smb_fname_base
);
1556 static int smb_Dir_destructor(struct smb_Dir
*dirp
)
1558 if (dirp
->dir
!= NULL
) {
1559 SMB_VFS_CLOSEDIR(dirp
->conn
,dirp
->dir
);
1560 if (dirp
->fsp
!= NULL
) {
1562 * The SMB_VFS_CLOSEDIR above
1563 * closes the underlying fd inside
1566 dirp
->fsp
->fh
->fd
= -1;
1567 if (dirp
->fsp
->dptr
!= NULL
) {
1568 SMB_ASSERT(dirp
->fsp
->dptr
->dir_hnd
== dirp
);
1569 dirp
->fsp
->dptr
->dir_hnd
= NULL
;
1574 if (dirp
->conn
->sconn
&& !dirp
->conn
->sconn
->using_smb2
) {
1575 dirp
->conn
->sconn
->searches
.dirhandles_open
--;
1580 /*******************************************************************
1582 ********************************************************************/
1584 struct smb_Dir
*OpenDir(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1589 struct smb_Dir
*dirp
= talloc_zero(mem_ctx
, struct smb_Dir
);
1590 struct smbd_server_connection
*sconn
= conn
->sconn
;
1597 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1599 dirp
->dir_path
= talloc_strdup(dirp
, name
);
1600 if (!dirp
->dir_path
) {
1605 if (sconn
&& !sconn
->using_smb2
) {
1606 sconn
->searches
.dirhandles_open
++;
1608 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1610 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1612 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp
->dir_path
,
1624 /*******************************************************************
1625 Open a directory from an fsp.
1626 ********************************************************************/
1628 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1633 struct smb_Dir
*dirp
= talloc_zero(mem_ctx
, struct smb_Dir
);
1634 struct smbd_server_connection
*sconn
= conn
->sconn
;
1641 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1643 dirp
->dir_path
= talloc_strdup(dirp
, fsp
->fsp_name
->base_name
);
1644 if (!dirp
->dir_path
) {
1649 if (sconn
&& !sconn
->using_smb2
) {
1650 sconn
->searches
.dirhandles_open
++;
1652 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1654 if (fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
1655 dirp
->dir
= SMB_VFS_FDOPENDIR(fsp
, mask
, attr
);
1656 if (dirp
->dir
!= NULL
) {
1659 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1663 if (errno
!= ENOSYS
) {
1669 if (dirp
->dir
== NULL
) {
1670 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1671 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1675 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp
->dir_path
,
1688 /*******************************************************************
1689 Read from a directory.
1690 Return directory entry, current offset, and optional stat information.
1691 Don't check for veto or invisible files.
1692 ********************************************************************/
1694 const char *ReadDirName(struct smb_Dir
*dirp
, long *poffset
,
1695 SMB_STRUCT_STAT
*sbuf
, char **ptalloced
)
1698 char *talloced
= NULL
;
1699 connection_struct
*conn
= dirp
->conn
;
1701 /* Cheat to allow . and .. to be the first entries returned. */
1702 if (((*poffset
== START_OF_DIRECTORY_OFFSET
) ||
1703 (*poffset
== DOT_DOT_DIRECTORY_OFFSET
)) && (dirp
->file_number
< 2))
1705 if (dirp
->file_number
== 0) {
1707 *poffset
= dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1710 *poffset
= dirp
->offset
= DOT_DOT_DIRECTORY_OFFSET
;
1712 dirp
->file_number
++;
1715 } else if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
1716 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1719 /* A real offset, seek to it. */
1720 SeekDir(dirp
, *poffset
);
1723 while ((n
= vfs_readdirname(conn
, dirp
->dir
, sbuf
, &talloced
))) {
1724 /* Ignore . and .. - we've already returned them. */
1726 if ((n
[1] == '\0') || (n
[1] == '.' && n
[2] == '\0')) {
1727 TALLOC_FREE(talloced
);
1731 *poffset
= dirp
->offset
= SMB_VFS_TELLDIR(conn
, dirp
->dir
);
1732 *ptalloced
= talloced
;
1733 dirp
->file_number
++;
1736 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1741 /*******************************************************************
1742 Rewind to the start.
1743 ********************************************************************/
1745 void RewindDir(struct smb_Dir
*dirp
, long *poffset
)
1747 SMB_VFS_REWINDDIR(dirp
->conn
, dirp
->dir
);
1748 dirp
->file_number
= 0;
1749 dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1750 *poffset
= START_OF_DIRECTORY_OFFSET
;
1753 /*******************************************************************
1755 ********************************************************************/
1757 void SeekDir(struct smb_Dir
*dirp
, long offset
)
1759 if (offset
!= dirp
->offset
) {
1760 if (offset
== START_OF_DIRECTORY_OFFSET
) {
1761 RewindDir(dirp
, &offset
);
1763 * Ok we should really set the file number here
1764 * to 1 to enable ".." to be returned next. Trouble
1765 * is I'm worried about callers using SeekDir(dirp,0)
1766 * as equivalent to RewindDir(). So leave this alone
1769 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
1770 RewindDir(dirp
, &offset
);
1772 * Set the file number to 2 - we want to get the first
1773 * real file entry (the one we return after "..")
1774 * on the next ReadDir.
1776 dirp
->file_number
= 2;
1777 } else if (offset
== END_OF_DIRECTORY_OFFSET
) {
1778 ; /* Don't seek in this case. */
1780 SMB_VFS_SEEKDIR(dirp
->conn
, dirp
->dir
, offset
);
1782 dirp
->offset
= offset
;
1786 /*******************************************************************
1787 Tell a dir position.
1788 ********************************************************************/
1790 long TellDir(struct smb_Dir
*dirp
)
1792 return(dirp
->offset
);
1795 /*******************************************************************
1796 Add an entry into the dcache.
1797 ********************************************************************/
1799 void DirCacheAdd(struct smb_Dir
*dirp
, const char *name
, long offset
)
1801 struct name_cache_entry
*e
;
1803 if (dirp
->name_cache_size
== 0) {
1807 if (dirp
->name_cache
== NULL
) {
1808 dirp
->name_cache
= talloc_zero_array(
1809 dirp
, struct name_cache_entry
, dirp
->name_cache_size
);
1811 if (dirp
->name_cache
== NULL
) {
1816 dirp
->name_cache_index
= (dirp
->name_cache_index
+1) %
1817 dirp
->name_cache_size
;
1818 e
= &dirp
->name_cache
[dirp
->name_cache_index
];
1819 TALLOC_FREE(e
->name
);
1820 e
->name
= talloc_strdup(dirp
, name
);
1824 /*******************************************************************
1825 Find an entry by name. Leave us at the offset after it.
1826 Don't check for veto or invisible files.
1827 ********************************************************************/
1829 bool SearchDir(struct smb_Dir
*dirp
, const char *name
, long *poffset
)
1832 const char *entry
= NULL
;
1833 char *talloced
= NULL
;
1834 connection_struct
*conn
= dirp
->conn
;
1836 /* Search back in the name cache. */
1837 if (dirp
->name_cache_size
&& dirp
->name_cache
) {
1838 for (i
= dirp
->name_cache_index
; i
>= 0; i
--) {
1839 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1840 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1841 *poffset
= e
->offset
;
1842 SeekDir(dirp
, e
->offset
);
1846 for (i
= dirp
->name_cache_size
- 1; i
> dirp
->name_cache_index
; i
--) {
1847 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1848 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1849 *poffset
= e
->offset
;
1850 SeekDir(dirp
, e
->offset
);
1856 /* Not found in the name cache. Rewind directory and start from scratch. */
1857 SMB_VFS_REWINDDIR(conn
, dirp
->dir
);
1858 dirp
->file_number
= 0;
1859 *poffset
= START_OF_DIRECTORY_OFFSET
;
1860 while ((entry
= ReadDirName(dirp
, poffset
, NULL
, &talloced
))) {
1861 if (conn
->case_sensitive
? (strcmp(entry
, name
) == 0) : strequal(entry
, name
)) {
1862 TALLOC_FREE(talloced
);
1865 TALLOC_FREE(talloced
);
1870 /*****************************************************************
1871 Is this directory empty ?
1872 *****************************************************************/
1874 NTSTATUS
can_delete_directory_fsp(files_struct
*fsp
)
1876 NTSTATUS status
= NT_STATUS_OK
;
1878 const char *dname
= NULL
;
1879 const char *dirname
= fsp
->fsp_name
->base_name
;
1880 char *talloced
= NULL
;
1882 struct connection_struct
*conn
= fsp
->conn
;
1883 struct smb_Dir
*dir_hnd
= OpenDir_fsp(talloc_tos(),
1890 return map_nt_error_from_unix(errno
);
1893 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
, &talloced
))) {
1894 /* Quick check for "." and ".." */
1895 if (dname
[0] == '.') {
1896 if (!dname
[1] || (dname
[1] == '.' && !dname
[2])) {
1897 TALLOC_FREE(talloced
);
1902 if (!is_visible_file(conn
, dirname
, dname
, &st
, True
)) {
1903 TALLOC_FREE(talloced
);
1907 DEBUG(10,("got name %s - can't delete\n",
1909 status
= NT_STATUS_DIRECTORY_NOT_EMPTY
;
1912 TALLOC_FREE(talloced
);
1913 TALLOC_FREE(dir_hnd
);