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"
28 This module implements directory related functions for Samba.
31 /* "Special" directory offsets. */
32 #define END_OF_DIRECTORY_OFFSET ((long)-1)
33 #define START_OF_DIRECTORY_OFFSET ((long)0)
34 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
36 /* Make directory handle internals available. */
38 struct name_cache_entry
{
44 connection_struct
*conn
;
48 size_t name_cache_size
;
49 struct name_cache_entry
*name_cache
;
50 unsigned int name_cache_index
;
51 unsigned int file_number
;
55 struct dptr_struct
*next
, *prev
;
58 struct connection_struct
*conn
;
59 struct smb_Dir
*dir_hnd
;
64 bool has_wild
; /* Set to true if the wcard entry has MS wildcard characters in it. */
65 bool did_stat
; /* Optimisation for non-wcard searches. */
68 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
73 #define INVALID_DPTR_KEY (-3)
75 /****************************************************************************
77 ****************************************************************************/
79 bool make_dir_struct(TALLOC_CTX
*ctx
,
89 char *mask2
= talloc_strdup(ctx
, mask
);
95 if ((mode
& FILE_ATTRIBUTE_DIRECTORY
) != 0) {
100 if ((p
= strchr_m(mask2
,'.')) != NULL
) {
102 push_ascii(buf
+1,mask2
,8, 0);
103 push_ascii(buf
+9,p
+1,3, 0);
106 push_ascii(buf
+1,mask2
,11, 0);
109 memset(buf
+21,'\0',DIR_STRUCT_SIZE
-21);
111 srv_put_dos_date(buf
,22,date
);
112 SSVAL(buf
,26,size
& 0xFFFF);
113 SSVAL(buf
,28,(size
>> 16)&0xFFFF);
114 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
115 Strange, but verified on W2K3. Needed for OS/2. JRA. */
116 push_ascii(buf
+30,fname
,12, uc
? STR_UPPER
: 0);
117 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf
+30, fname
));
121 /****************************************************************************
122 Initialise the dir bitmap.
123 ****************************************************************************/
125 bool init_dptrs(struct smbd_server_connection
*sconn
)
127 if (sconn
->searches
.dptr_bmap
) {
131 sconn
->searches
.dptr_bmap
= bitmap_talloc(
132 sconn
, MAX_DIRECTORY_HANDLES
);
134 if (sconn
->searches
.dptr_bmap
== NULL
) {
141 /****************************************************************************
142 Idle a dptr - the directory is closed but the control info is kept.
143 ****************************************************************************/
145 static void dptr_idle(struct dptr_struct
*dptr
)
148 DEBUG(4,("Idling dptr dnum %d\n",dptr
->dnum
));
149 TALLOC_FREE(dptr
->dir_hnd
);
153 /****************************************************************************
154 Idle the oldest dptr.
155 ****************************************************************************/
157 static void dptr_idleoldest(struct smbd_server_connection
*sconn
)
159 struct dptr_struct
*dptr
;
162 * Go to the end of the list.
164 dptr
= DLIST_TAIL(sconn
->searches
.dirptrs
);
167 DEBUG(0,("No dptrs available to idle ?\n"));
172 * Idle the oldest pointer.
175 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
183 /****************************************************************************
184 Get the struct dptr_struct for a dir index.
185 ****************************************************************************/
187 static struct dptr_struct
*dptr_get(struct smbd_server_connection
*sconn
,
188 int key
, bool forclose
)
190 struct dptr_struct
*dptr
;
192 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
193 if(dptr
->dnum
== key
) {
194 if (!forclose
&& !dptr
->dir_hnd
) {
195 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
)
196 dptr_idleoldest(sconn
);
197 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key
));
198 if (!(dptr
->dir_hnd
= OpenDir(
199 NULL
, dptr
->conn
, dptr
->path
,
200 dptr
->wcard
, dptr
->attr
))) {
201 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr
->path
,
206 DLIST_PROMOTE(sconn
->searches
.dirptrs
,dptr
);
213 /****************************************************************************
214 Get the dir path for a dir index.
215 ****************************************************************************/
217 char *dptr_path(struct smbd_server_connection
*sconn
, int key
)
219 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
225 /****************************************************************************
226 Get the dir wcard for a dir index.
227 ****************************************************************************/
229 char *dptr_wcard(struct smbd_server_connection
*sconn
, int key
)
231 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
237 /****************************************************************************
238 Get the dir attrib for a dir index.
239 ****************************************************************************/
241 uint16
dptr_attr(struct smbd_server_connection
*sconn
, int key
)
243 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
249 /****************************************************************************
250 Close a dptr (internal func).
251 ****************************************************************************/
253 static void dptr_close_internal(struct dptr_struct
*dptr
)
255 struct smbd_server_connection
*sconn
= dptr
->conn
->sconn
;
257 DEBUG(4,("closing dptr key %d\n",dptr
->dnum
));
263 if (sconn
->using_smb2
) {
267 DLIST_REMOVE(sconn
->searches
.dirptrs
, dptr
);
270 * Free the dnum in the bitmap. Remember the dnum value is always
271 * biased by one with respect to the bitmap.
274 if (!bitmap_query(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1)) {
275 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
279 bitmap_clear(sconn
->searches
.dptr_bmap
, dptr
->dnum
- 1);
282 TALLOC_FREE(dptr
->dir_hnd
);
284 /* Lanman 2 specific code */
285 SAFE_FREE(dptr
->wcard
);
286 SAFE_FREE(dptr
->path
);
290 /****************************************************************************
291 Close a dptr given a key.
292 ****************************************************************************/
294 void dptr_close(struct smbd_server_connection
*sconn
, int *key
)
296 struct dptr_struct
*dptr
;
298 if(*key
== INVALID_DPTR_KEY
)
301 /* OS/2 seems to use -1 to indicate "close all directories" */
303 struct dptr_struct
*next
;
304 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
306 dptr_close_internal(dptr
);
308 *key
= INVALID_DPTR_KEY
;
312 dptr
= dptr_get(sconn
, *key
, true);
315 DEBUG(0,("Invalid key %d given to dptr_close\n", *key
));
319 dptr_close_internal(dptr
);
321 *key
= INVALID_DPTR_KEY
;
324 /****************************************************************************
325 Close all dptrs for a cnum.
326 ****************************************************************************/
328 void dptr_closecnum(connection_struct
*conn
)
330 struct dptr_struct
*dptr
, *next
;
331 struct smbd_server_connection
*sconn
= conn
->sconn
;
337 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
339 if (dptr
->conn
== conn
) {
340 dptr_close_internal(dptr
);
345 /****************************************************************************
346 Idle all dptrs for a cnum.
347 ****************************************************************************/
349 void dptr_idlecnum(connection_struct
*conn
)
351 struct dptr_struct
*dptr
;
352 struct smbd_server_connection
*sconn
= conn
->sconn
;
358 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
359 if (dptr
->conn
== conn
&& dptr
->dir_hnd
) {
365 /****************************************************************************
366 Close a dptr that matches a given path, only if it matches the spid also.
367 ****************************************************************************/
369 void dptr_closepath(struct smbd_server_connection
*sconn
,
370 char *path
,uint16 spid
)
372 struct dptr_struct
*dptr
, *next
;
373 for(dptr
= sconn
->searches
.dirptrs
; dptr
; dptr
= next
) {
375 if (spid
== dptr
->spid
&& strequal(dptr
->path
,path
))
376 dptr_close_internal(dptr
);
380 /****************************************************************************
381 Try and close the oldest handle not marked for
382 expect close in the hope that the client has
383 finished with that one.
384 ****************************************************************************/
386 static void dptr_close_oldest(struct smbd_server_connection
*sconn
,
389 struct dptr_struct
*dptr
;
392 * Go to the end of the list.
394 for(dptr
= sconn
->searches
.dirptrs
; dptr
&& dptr
->next
; dptr
= dptr
->next
)
398 DEBUG(0,("No old dptrs available to close oldest ?\n"));
403 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
404 * does not have expect_close set. If 'old' is false, close
405 * one of the new dnum handles.
408 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
409 if ((old
&& (dptr
->dnum
< 256) && !dptr
->expect_close
) ||
410 (!old
&& (dptr
->dnum
> 255))) {
411 dptr_close_internal(dptr
);
417 /****************************************************************************
418 Create a new dir ptr. If the flag old_handle is true then we must allocate
419 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
420 one byte long. If old_handle is false we allocate from the range
421 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
422 a directory handle is never zero.
423 wcard must not be zero.
424 ****************************************************************************/
426 NTSTATUS
dptr_create(connection_struct
*conn
, files_struct
*fsp
,
427 const char *path
, bool old_handle
, bool expect_close
,uint16 spid
,
428 const char *wcard
, bool wcard_has_wild
, uint32 attr
, struct dptr_struct
**dptr_ret
)
430 struct smbd_server_connection
*sconn
= conn
->sconn
;
431 struct dptr_struct
*dptr
= NULL
;
432 struct smb_Dir
*dir_hnd
;
435 if (fsp
&& fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
436 path
= fsp
->fsp_name
->base_name
;
439 DEBUG(5,("dptr_create dir=%s\n", path
));
442 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
443 return NT_STATUS_INTERNAL_ERROR
;
447 return NT_STATUS_INVALID_PARAMETER
;
451 dir_hnd
= OpenDir_fsp(NULL
, conn
, fsp
, wcard
, attr
);
453 status
= check_name(conn
,path
);
454 if (!NT_STATUS_IS_OK(status
)) {
457 dir_hnd
= OpenDir(NULL
, conn
, path
, wcard
, attr
);
461 return map_nt_error_from_unix(errno
);
464 if (sconn
->searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
) {
465 dptr_idleoldest(sconn
);
468 dptr
= SMB_MALLOC_P(struct dptr_struct
);
470 DEBUG(0,("malloc fail in dptr_create.\n"));
471 TALLOC_FREE(dir_hnd
);
472 return NT_STATUS_NO_MEMORY
;
477 dptr
->path
= SMB_STRDUP(path
);
480 TALLOC_FREE(dir_hnd
);
481 return NT_STATUS_NO_MEMORY
;
484 dptr
->dir_hnd
= dir_hnd
;
486 dptr
->expect_close
= expect_close
;
487 dptr
->wcard
= SMB_STRDUP(wcard
);
489 SAFE_FREE(dptr
->path
);
491 TALLOC_FREE(dir_hnd
);
492 return NT_STATUS_NO_MEMORY
;
494 if (lp_posix_pathnames() || (wcard
[0] == '.' && wcard
[1] == 0)) {
495 dptr
->has_wild
= True
;
497 dptr
->has_wild
= wcard_has_wild
;
502 if (sconn
->using_smb2
) {
509 * This is an old-style SMBsearch request. Ensure the
510 * value we return will fit in the range 1-255.
513 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
515 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
518 * Try and close the oldest handle not marked for
519 * expect close in the hope that the client has
520 * finished with that one.
523 dptr_close_oldest(sconn
, true);
525 /* Now try again... */
526 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 0);
527 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
528 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr
->dnum
));
529 SAFE_FREE(dptr
->path
);
530 SAFE_FREE(dptr
->wcard
);
532 TALLOC_FREE(dir_hnd
);
533 return NT_STATUS_TOO_MANY_OPENED_FILES
;
539 * This is a new-style trans2 request. Allocate from
540 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
543 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
545 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
548 * Try and close the oldest handle close in the hope that
549 * the client has finished with that one. This will only
550 * happen in the case of the Win98 client bug where it leaks
554 dptr_close_oldest(sconn
, false);
556 /* Now try again... */
557 dptr
->dnum
= bitmap_find(sconn
->searches
.dptr_bmap
, 255);
559 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
560 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr
->dnum
));
561 SAFE_FREE(dptr
->path
);
562 SAFE_FREE(dptr
->wcard
);
564 TALLOC_FREE(dir_hnd
);
565 return NT_STATUS_TOO_MANY_OPENED_FILES
;
570 bitmap_set(sconn
->searches
.dptr_bmap
, dptr
->dnum
);
572 dptr
->dnum
+= 1; /* Always bias the dnum by one - no zero dnums allowed. */
574 DLIST_ADD(sconn
->searches
.dirptrs
, dptr
);
577 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
578 dptr
->dnum
,path
,expect_close
));
586 /****************************************************************************
587 Wrapper functions to access the lower level directory handles.
588 ****************************************************************************/
590 void dptr_CloseDir(files_struct
*fsp
)
594 * Ugly hack. We have defined fdopendir to return ENOSYS if dirfd also isn't
595 * present. I hate Solaris. JRA.
598 if (fsp
->fh
->fd
!= -1 &&
599 fsp
->dptr
->dir_hnd
&&
600 dirfd(fsp
->dptr
->dir_hnd
->dir
)) {
601 /* The call below closes the underlying fd. */
605 dptr_close_internal(fsp
->dptr
);
610 void dptr_SeekDir(struct dptr_struct
*dptr
, long offset
)
612 SeekDir(dptr
->dir_hnd
, offset
);
615 long dptr_TellDir(struct dptr_struct
*dptr
)
617 return TellDir(dptr
->dir_hnd
);
620 bool dptr_has_wild(struct dptr_struct
*dptr
)
622 return dptr
->has_wild
;
625 int dptr_dnum(struct dptr_struct
*dptr
)
630 /****************************************************************************
631 Return the next visible file name, skipping veto'd and invisible files.
632 ****************************************************************************/
634 static const char *dptr_normal_ReadDirName(struct dptr_struct
*dptr
,
635 long *poffset
, SMB_STRUCT_STAT
*pst
,
638 /* Normal search for the next file. */
640 char *talloced
= NULL
;
642 while ((name
= ReadDirName(dptr
->dir_hnd
, poffset
, pst
, &talloced
))
644 if (is_visible_file(dptr
->conn
, dptr
->path
, name
, pst
, True
)) {
645 *ptalloced
= talloced
;
648 TALLOC_FREE(talloced
);
653 /****************************************************************************
654 Return the next visible file name, skipping veto'd and invisible files.
655 ****************************************************************************/
657 char *dptr_ReadDirName(TALLOC_CTX
*ctx
,
658 struct dptr_struct
*dptr
,
660 SMB_STRUCT_STAT
*pst
)
662 struct smb_filename smb_fname_base
;
664 const char *name_temp
= NULL
;
665 char *talloced
= NULL
;
666 char *pathreal
= NULL
;
667 char *found_name
= NULL
;
670 SET_STAT_INVALID(*pst
);
672 if (dptr
->has_wild
|| dptr
->did_stat
) {
673 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
,
675 if (name_temp
== NULL
) {
678 if (talloced
!= NULL
) {
679 return talloc_move(ctx
, &talloced
);
681 return talloc_strdup(ctx
, name_temp
);
684 /* If poffset is -1 then we know we returned this name before and we
685 * have no wildcards. We're at the end of the directory. */
686 if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
690 /* We know the stored wcard contains no wildcard characters.
691 * See if we can match with a stat call. If we can't, then set
692 * did_stat to true to ensure we only do this once and keep
695 dptr
->did_stat
= true;
697 /* First check if it should be visible. */
698 if (!is_visible_file(dptr
->conn
, dptr
->path
, dptr
->wcard
,
701 /* This only returns false if the file was found, but
702 is explicitly not visible. Set us to end of
703 directory, but return NULL as we know we can't ever
708 if (VALID_STAT(*pst
)) {
709 name
= talloc_strdup(ctx
, dptr
->wcard
);
713 pathreal
= talloc_asprintf(ctx
,
720 /* Create an smb_filename with stream_name == NULL. */
721 ZERO_STRUCT(smb_fname_base
);
722 smb_fname_base
.base_name
= pathreal
;
724 if (SMB_VFS_STAT(dptr
->conn
, &smb_fname_base
) == 0) {
725 *pst
= smb_fname_base
.st
;
726 name
= talloc_strdup(ctx
, dptr
->wcard
);
729 /* If we get any other error than ENOENT or ENOTDIR
730 then the file exists we just can't stat it. */
731 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
732 name
= talloc_strdup(ctx
, dptr
->wcard
);
737 /* Stat failed. We know this is authoratiative if we are
738 * providing case sensitive semantics or the underlying
739 * filesystem is case sensitive.
741 if (dptr
->conn
->case_sensitive
||
742 !(dptr
->conn
->fs_capabilities
& FILE_CASE_SENSITIVE_SEARCH
))
748 * Try case-insensitive stat if the fs has the ability. This avoids
749 * scanning the whole directory.
751 ret
= SMB_VFS_GET_REAL_FILENAME(dptr
->conn
, dptr
->path
, dptr
->wcard
,
756 } else if (errno
== ENOENT
) {
757 /* The case-insensitive lookup was authoritative. */
761 TALLOC_FREE(pathreal
);
763 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
, &talloced
);
764 if (name_temp
== NULL
) {
767 if (talloced
!= NULL
) {
768 return talloc_move(ctx
, &talloced
);
770 return talloc_strdup(ctx
, name_temp
);
773 TALLOC_FREE(pathreal
);
775 /* We need to set the underlying dir_hnd offset to -1
776 * also as this function is usually called with the
777 * output from TellDir. */
778 dptr
->dir_hnd
->offset
= *poffset
= END_OF_DIRECTORY_OFFSET
;
782 /****************************************************************************
783 Search for a file by name, skipping veto'ed and not visible files.
784 ****************************************************************************/
786 bool dptr_SearchDir(struct dptr_struct
*dptr
, const char *name
, long *poffset
, SMB_STRUCT_STAT
*pst
)
788 SET_STAT_INVALID(*pst
);
790 if (!dptr
->has_wild
&& (dptr
->dir_hnd
->offset
== END_OF_DIRECTORY_OFFSET
)) {
791 /* This is a singleton directory and we're already at the end. */
792 *poffset
= END_OF_DIRECTORY_OFFSET
;
796 return SearchDir(dptr
->dir_hnd
, name
, poffset
);
799 /****************************************************************************
800 Add the name we're returning into the underlying cache.
801 ****************************************************************************/
803 void dptr_DirCacheAdd(struct dptr_struct
*dptr
, const char *name
, long offset
)
805 DirCacheAdd(dptr
->dir_hnd
, name
, offset
);
808 /****************************************************************************
809 Initialize variables & state data at the beginning of all search SMB requests.
810 ****************************************************************************/
811 void dptr_init_search_op(struct dptr_struct
*dptr
)
813 SMB_VFS_INIT_SEARCH_OP(dptr
->conn
, dptr
->dir_hnd
->dir
);
816 /****************************************************************************
817 Fill the 5 byte server reserved dptr field.
818 ****************************************************************************/
820 bool dptr_fill(struct smbd_server_connection
*sconn
,
821 char *buf1
,unsigned int key
)
823 unsigned char *buf
= (unsigned char *)buf1
;
824 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
827 DEBUG(1,("filling null dirptr %d\n",key
));
830 offset
= (uint32
)TellDir(dptr
->dir_hnd
);
831 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key
,
832 (long)dptr
->dir_hnd
,(int)offset
));
838 /****************************************************************************
839 Fetch the dir ptr and seek it given the 5 byte server field.
840 ****************************************************************************/
842 struct dptr_struct
*dptr_fetch(struct smbd_server_connection
*sconn
,
845 unsigned int key
= *(unsigned char *)buf
;
846 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
851 DEBUG(3,("fetched null dirptr %d\n",key
));
855 offset
= IVAL(buf
,1);
856 if (offset
== (uint32
)-1) {
857 seekoff
= END_OF_DIRECTORY_OFFSET
;
859 seekoff
= (long)offset
;
861 SeekDir(dptr
->dir_hnd
,seekoff
);
862 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
863 key
, dptr
->path
, (int)seekoff
));
867 /****************************************************************************
869 ****************************************************************************/
871 struct dptr_struct
*dptr_fetch_lanman2(struct smbd_server_connection
*sconn
,
874 struct dptr_struct
*dptr
= dptr_get(sconn
, dptr_num
, false);
877 DEBUG(3,("fetched null dirptr %d\n",dptr_num
));
880 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num
,dptr
->path
));
884 /****************************************************************************
885 Check that a file matches a particular file type.
886 ****************************************************************************/
888 bool dir_check_ftype(connection_struct
*conn
, uint32 mode
, uint32 dirtype
)
892 /* Check the "may have" search bits. */
893 if (((mode
& ~dirtype
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
)) != 0)
896 /* Check the "must have" bits, which are the may have bits shifted eight */
897 /* If must have bit is set, the file/dir can not be returned in search unless the matching
898 file attribute is set */
899 mask
= ((dirtype
>> 8) & (FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
)); /* & 0x37 */
901 if((mask
& (mode
& (FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
))) == mask
) /* check if matching attribute present */
910 static bool mangle_mask_match(connection_struct
*conn
,
911 const char *filename
,
916 if (!name_to_8_3(filename
,mname
,False
,conn
->params
)) {
919 return mask_match_search(mname
,mask
,False
);
922 bool smbd_dirptr_get_entry(TALLOC_CTX
*ctx
,
923 struct dptr_struct
*dirptr
,
928 bool (*match_fn
)(TALLOC_CTX
*ctx
,
933 bool (*mode_fn
)(TALLOC_CTX
*ctx
,
935 struct smb_filename
*smb_fname
,
939 struct smb_filename
**_smb_fname
,
943 connection_struct
*conn
= dirptr
->conn
;
949 needslash
= ( dirptr
->path
[strlen(dirptr
->path
) -1] != '/');
954 SMB_STRUCT_STAT sbuf
;
958 char *pathreal
= NULL
;
959 struct smb_filename smb_fname
;
964 cur_offset
= dptr_TellDir(dirptr
);
965 prev_offset
= cur_offset
;
966 dname
= dptr_ReadDirName(ctx
, dirptr
, &cur_offset
, &sbuf
);
968 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
969 (long)dirptr
, cur_offset
));
975 isdots
= (ISDOT(dname
) || ISDOTDOT(dname
));
976 if (dont_descend
&& !isdots
) {
982 * fname may get mangled, dname is never mangled.
983 * Whenever we're accessing the filesystem we use
984 * pathreal which is composed from dname.
987 ok
= match_fn(ctx
, private_data
, dname
, mask
, &fname
);
993 pathreal
= talloc_asprintf(ctx
, "%s%s%s",
1003 /* Create smb_fname with NULL stream_name. */
1004 ZERO_STRUCT(smb_fname
);
1005 smb_fname
.base_name
= pathreal
;
1006 smb_fname
.st
= sbuf
;
1008 ok
= mode_fn(ctx
, private_data
, &smb_fname
, &mode
);
1012 TALLOC_FREE(pathreal
);
1016 if (!dir_check_ftype(conn
, mode
, dirtype
)) {
1017 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1018 fname
, (unsigned int)mode
, (unsigned int)dirtype
));
1021 TALLOC_FREE(pathreal
);
1025 if (ask_sharemode
) {
1026 struct timespec write_time_ts
;
1027 struct file_id fileid
;
1029 fileid
= vfs_file_id_from_sbuf(conn
,
1031 get_file_infos(fileid
, 0, NULL
, &write_time_ts
);
1032 if (!null_timespec(write_time_ts
)) {
1033 update_stat_ex_mtime(&smb_fname
.st
,
1038 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1040 mask
, smb_fname_str_dbg(&smb_fname
),
1043 DirCacheAdd(dirptr
->dir_hnd
, dname
, cur_offset
);
1047 status
= copy_smb_filename(ctx
, &smb_fname
, _smb_fname
);
1048 TALLOC_FREE(pathreal
);
1049 if (!NT_STATUS_IS_OK(status
)) {
1054 *_prev_offset
= prev_offset
;
1062 /****************************************************************************
1063 Get an 8.3 directory entry.
1064 ****************************************************************************/
1066 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX
*ctx
,
1072 connection_struct
*conn
= (connection_struct
*)private_data
;
1074 if ((strcmp(mask
,"*.*") == 0) ||
1075 mask_match_search(dname
, mask
, false) ||
1076 mangle_mask_match(conn
, dname
, mask
)) {
1080 if (!mangle_is_8_3(dname
, false, conn
->params
)) {
1081 bool ok
= name_to_8_3(dname
, mname
, false,
1091 *_fname
= talloc_strdup(ctx
, fname
);
1092 if (*_fname
== NULL
) {
1102 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX
*ctx
,
1104 struct smb_filename
*smb_fname
,
1107 connection_struct
*conn
= (connection_struct
*)private_data
;
1109 if (!VALID_STAT(smb_fname
->st
)) {
1110 if ((SMB_VFS_STAT(conn
, smb_fname
)) != 0) {
1111 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1112 "Couldn't stat [%s]. Error "
1114 smb_fname_str_dbg(smb_fname
),
1120 *_mode
= dos_mode(conn
, smb_fname
);
1124 bool get_dir_entry(TALLOC_CTX
*ctx
,
1125 struct dptr_struct
*dirptr
,
1131 struct timespec
*_date
,
1135 connection_struct
*conn
= dirptr
->conn
;
1137 struct smb_filename
*smb_fname
= NULL
;
1142 ok
= smbd_dirptr_get_entry(ctx
,
1148 smbd_dirptr_8_3_match_fn
,
1149 smbd_dirptr_8_3_mode_fn
,
1159 *_fname
= talloc_move(ctx
, &fname
);
1160 *_size
= smb_fname
->st
.st_ex_size
;
1162 *_date
= smb_fname
->st
.st_ex_mtime
;
1163 TALLOC_FREE(smb_fname
);
1167 /*******************************************************************
1168 Check to see if a user can read a file. This is only approximate,
1169 it is used as part of the "hide unreadable" option. Don't
1170 use it for anything security sensitive.
1171 ********************************************************************/
1173 static bool user_can_read_file(connection_struct
*conn
,
1174 struct smb_filename
*smb_fname
)
1177 * Never hide files from the root user.
1178 * We use (uid_t)0 here not sec_initial_uid()
1179 * as make test uses a single user context.
1182 if (get_current_uid(conn
) == (uid_t
)0) {
1186 return can_access_file_acl(conn
, smb_fname
, FILE_READ_DATA
);
1189 /*******************************************************************
1190 Check to see if a user can write a file (and only files, we do not
1191 check dirs on this one). This is only approximate,
1192 it is used as part of the "hide unwriteable" option. Don't
1193 use it for anything security sensitive.
1194 ********************************************************************/
1196 static bool user_can_write_file(connection_struct
*conn
,
1197 const struct smb_filename
*smb_fname
)
1200 * Never hide files from the root user.
1201 * We use (uid_t)0 here not sec_initial_uid()
1202 * as make test uses a single user context.
1205 if (get_current_uid(conn
) == (uid_t
)0) {
1209 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1211 /* Pseudo-open the file */
1213 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1217 return can_write_to_file(conn
, smb_fname
);
1220 /*******************************************************************
1221 Is a file a "special" type ?
1222 ********************************************************************/
1224 static bool file_is_special(connection_struct
*conn
,
1225 const struct smb_filename
*smb_fname
)
1228 * Never hide files from the root user.
1229 * We use (uid_t)0 here not sec_initial_uid()
1230 * as make test uses a single user context.
1233 if (get_current_uid(conn
) == (uid_t
)0) {
1237 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1239 if (S_ISREG(smb_fname
->st
.st_ex_mode
) ||
1240 S_ISDIR(smb_fname
->st
.st_ex_mode
) ||
1241 S_ISLNK(smb_fname
->st
.st_ex_mode
))
1247 /*******************************************************************
1248 Should the file be seen by the client?
1249 NOTE: A successful return is no guarantee of the file's existence.
1250 ********************************************************************/
1252 bool is_visible_file(connection_struct
*conn
, const char *dir_path
,
1253 const char *name
, SMB_STRUCT_STAT
*pst
, bool use_veto
)
1255 bool hide_unreadable
= lp_hideunreadable(SNUM(conn
));
1256 bool hide_unwriteable
= lp_hideunwriteable_files(SNUM(conn
));
1257 bool hide_special
= lp_hide_special_files(SNUM(conn
));
1259 struct smb_filename
*smb_fname_base
= NULL
;
1263 if ((strcmp(".",name
) == 0) || (strcmp("..",name
) == 0)) {
1264 return True
; /* . and .. are always visible. */
1267 /* If it's a vetoed file, pretend it doesn't even exist */
1268 if (use_veto
&& IS_VETO_PATH(conn
, name
)) {
1269 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name
));
1273 if (hide_unreadable
|| hide_unwriteable
|| hide_special
) {
1274 entry
= talloc_asprintf(talloc_tos(), "%s/%s", dir_path
, name
);
1280 /* Create an smb_filename with stream_name == NULL. */
1281 status
= create_synthetic_smb_fname(talloc_tos(), entry
, NULL
,
1282 pst
, &smb_fname_base
);
1283 if (!NT_STATUS_IS_OK(status
)) {
1288 /* If the file name does not exist, there's no point checking
1289 * the configuration options. We succeed, on the basis that the
1290 * checks *might* have passed if the file was present.
1292 if (!VALID_STAT(*pst
)) {
1293 if (SMB_VFS_STAT(conn
, smb_fname_base
) != 0) {
1297 *pst
= smb_fname_base
->st
;
1301 /* Honour _hide unreadable_ option */
1302 if (hide_unreadable
&&
1303 !user_can_read_file(conn
, smb_fname_base
)) {
1304 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1309 /* Honour _hide unwriteable_ option */
1310 if (hide_unwriteable
&& !user_can_write_file(conn
,
1312 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1317 /* Honour _hide_special_ option */
1318 if (hide_special
&& file_is_special(conn
, smb_fname_base
)) {
1319 DEBUG(10,("is_visible_file: file %s is special.\n",
1328 TALLOC_FREE(smb_fname_base
);
1333 static int smb_Dir_destructor(struct smb_Dir
*dirp
)
1337 if (dirp
->conn
->sconn
) {
1338 files_struct
*fsp
= file_find_fd(dirp
->conn
->sconn
,
1341 /* The call below closes the underlying fd. */
1346 SMB_VFS_CLOSEDIR(dirp
->conn
,dirp
->dir
);
1348 if (dirp
->conn
->sconn
&& !dirp
->conn
->sconn
->using_smb2
) {
1349 dirp
->conn
->sconn
->searches
.dirhandles_open
--;
1354 /*******************************************************************
1356 ********************************************************************/
1358 struct smb_Dir
*OpenDir(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1363 struct smb_Dir
*dirp
= TALLOC_ZERO_P(mem_ctx
, struct smb_Dir
);
1364 struct smbd_server_connection
*sconn
= conn
->sconn
;
1371 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1373 dirp
->dir_path
= talloc_strdup(dirp
, name
);
1374 if (!dirp
->dir_path
) {
1379 if (sconn
&& !sconn
->using_smb2
) {
1380 sconn
->searches
.dirhandles_open
++;
1382 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1384 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1386 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp
->dir_path
,
1398 /*******************************************************************
1399 Open a directory from an fsp.
1400 ********************************************************************/
1402 static struct smb_Dir
*OpenDir_fsp(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1407 struct smb_Dir
*dirp
= TALLOC_ZERO_P(mem_ctx
, struct smb_Dir
);
1408 struct smbd_server_connection
*sconn
= conn
->sconn
;
1415 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1417 dirp
->dir_path
= talloc_strdup(dirp
, fsp
->fsp_name
->base_name
);
1418 if (!dirp
->dir_path
) {
1423 if (sconn
&& !sconn
->using_smb2
) {
1424 sconn
->searches
.dirhandles_open
++;
1426 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1428 if (fsp
->is_directory
&& fsp
->fh
->fd
!= -1) {
1429 dirp
->dir
= SMB_VFS_FDOPENDIR(fsp
, mask
, attr
);
1430 if (dirp
->dir
== NULL
) {
1431 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1435 if (errno
!= ENOSYS
) {
1441 if (dirp
->dir
== NULL
) {
1442 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1443 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1447 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp
->dir_path
,
1460 /*******************************************************************
1461 Read from a directory.
1462 Return directory entry, current offset, and optional stat information.
1463 Don't check for veto or invisible files.
1464 ********************************************************************/
1466 const char *ReadDirName(struct smb_Dir
*dirp
, long *poffset
,
1467 SMB_STRUCT_STAT
*sbuf
, char **ptalloced
)
1470 char *talloced
= NULL
;
1471 connection_struct
*conn
= dirp
->conn
;
1473 /* Cheat to allow . and .. to be the first entries returned. */
1474 if (((*poffset
== START_OF_DIRECTORY_OFFSET
) ||
1475 (*poffset
== DOT_DOT_DIRECTORY_OFFSET
)) && (dirp
->file_number
< 2))
1477 if (dirp
->file_number
== 0) {
1479 *poffset
= dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1482 *poffset
= dirp
->offset
= DOT_DOT_DIRECTORY_OFFSET
;
1484 dirp
->file_number
++;
1487 } else if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
1488 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1491 /* A real offset, seek to it. */
1492 SeekDir(dirp
, *poffset
);
1495 while ((n
= vfs_readdirname(conn
, dirp
->dir
, sbuf
, &talloced
))) {
1496 /* Ignore . and .. - we've already returned them. */
1498 if ((n
[1] == '\0') || (n
[1] == '.' && n
[2] == '\0')) {
1499 TALLOC_FREE(talloced
);
1503 *poffset
= dirp
->offset
= SMB_VFS_TELLDIR(conn
, dirp
->dir
);
1504 *ptalloced
= talloced
;
1505 dirp
->file_number
++;
1508 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1513 /*******************************************************************
1514 Rewind to the start.
1515 ********************************************************************/
1517 void RewindDir(struct smb_Dir
*dirp
, long *poffset
)
1519 SMB_VFS_REWINDDIR(dirp
->conn
, dirp
->dir
);
1520 dirp
->file_number
= 0;
1521 dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1522 *poffset
= START_OF_DIRECTORY_OFFSET
;
1525 /*******************************************************************
1527 ********************************************************************/
1529 void SeekDir(struct smb_Dir
*dirp
, long offset
)
1531 if (offset
!= dirp
->offset
) {
1532 if (offset
== START_OF_DIRECTORY_OFFSET
) {
1533 RewindDir(dirp
, &offset
);
1535 * Ok we should really set the file number here
1536 * to 1 to enable ".." to be returned next. Trouble
1537 * is I'm worried about callers using SeekDir(dirp,0)
1538 * as equivalent to RewindDir(). So leave this alone
1541 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
1542 RewindDir(dirp
, &offset
);
1544 * Set the file number to 2 - we want to get the first
1545 * real file entry (the one we return after "..")
1546 * on the next ReadDir.
1548 dirp
->file_number
= 2;
1549 } else if (offset
== END_OF_DIRECTORY_OFFSET
) {
1550 ; /* Don't seek in this case. */
1552 SMB_VFS_SEEKDIR(dirp
->conn
, dirp
->dir
, offset
);
1554 dirp
->offset
= offset
;
1558 /*******************************************************************
1559 Tell a dir position.
1560 ********************************************************************/
1562 long TellDir(struct smb_Dir
*dirp
)
1564 return(dirp
->offset
);
1567 /*******************************************************************
1568 Add an entry into the dcache.
1569 ********************************************************************/
1571 void DirCacheAdd(struct smb_Dir
*dirp
, const char *name
, long offset
)
1573 struct name_cache_entry
*e
;
1575 if (dirp
->name_cache_size
== 0) {
1579 if (dirp
->name_cache
== NULL
) {
1580 dirp
->name_cache
= TALLOC_ZERO_ARRAY(
1581 dirp
, struct name_cache_entry
, dirp
->name_cache_size
);
1583 if (dirp
->name_cache
== NULL
) {
1588 dirp
->name_cache_index
= (dirp
->name_cache_index
+1) %
1589 dirp
->name_cache_size
;
1590 e
= &dirp
->name_cache
[dirp
->name_cache_index
];
1591 TALLOC_FREE(e
->name
);
1592 e
->name
= talloc_strdup(dirp
, name
);
1596 /*******************************************************************
1597 Find an entry by name. Leave us at the offset after it.
1598 Don't check for veto or invisible files.
1599 ********************************************************************/
1601 bool SearchDir(struct smb_Dir
*dirp
, const char *name
, long *poffset
)
1604 const char *entry
= NULL
;
1605 char *talloced
= NULL
;
1606 connection_struct
*conn
= dirp
->conn
;
1608 /* Search back in the name cache. */
1609 if (dirp
->name_cache_size
&& dirp
->name_cache
) {
1610 for (i
= dirp
->name_cache_index
; i
>= 0; i
--) {
1611 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1612 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1613 *poffset
= e
->offset
;
1614 SeekDir(dirp
, e
->offset
);
1618 for (i
= dirp
->name_cache_size
- 1; i
> dirp
->name_cache_index
; i
--) {
1619 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1620 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1621 *poffset
= e
->offset
;
1622 SeekDir(dirp
, e
->offset
);
1628 /* Not found in the name cache. Rewind directory and start from scratch. */
1629 SMB_VFS_REWINDDIR(conn
, dirp
->dir
);
1630 dirp
->file_number
= 0;
1631 *poffset
= START_OF_DIRECTORY_OFFSET
;
1632 while ((entry
= ReadDirName(dirp
, poffset
, NULL
, &talloced
))) {
1633 if (conn
->case_sensitive
? (strcmp(entry
, name
) == 0) : strequal(entry
, name
)) {
1634 TALLOC_FREE(talloced
);
1637 TALLOC_FREE(talloced
);
1642 /*****************************************************************
1643 Is this directory empty ?
1644 *****************************************************************/
1646 NTSTATUS
can_delete_directory(struct connection_struct
*conn
,
1647 const char *dirname
)
1649 NTSTATUS status
= NT_STATUS_OK
;
1651 const char *dname
= NULL
;
1652 char *talloced
= NULL
;
1654 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
1658 return map_nt_error_from_unix(errno
);
1661 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
, &talloced
))) {
1662 /* Quick check for "." and ".." */
1663 if (dname
[0] == '.') {
1664 if (!dname
[1] || (dname
[1] == '.' && !dname
[2])) {
1665 TALLOC_FREE(talloced
);
1670 if (!is_visible_file(conn
, dirname
, dname
, &st
, True
)) {
1671 TALLOC_FREE(talloced
);
1675 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1677 status
= NT_STATUS_DIRECTORY_NOT_EMPTY
;
1680 TALLOC_FREE(talloced
);
1681 TALLOC_FREE(dir_hnd
);