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 "smbd/globals.h"
25 This module implements directory related functions for Samba.
28 /* "Special" directory offsets. */
29 #define END_OF_DIRECTORY_OFFSET ((long)-1)
30 #define START_OF_DIRECTORY_OFFSET ((long)0)
31 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
33 /* Make directory handle internals available. */
35 struct name_cache_entry
{
41 connection_struct
*conn
;
45 size_t name_cache_size
;
46 struct name_cache_entry
*name_cache
;
47 unsigned int name_cache_index
;
48 unsigned int file_number
;
52 struct dptr_struct
*next
, *prev
;
55 struct connection_struct
*conn
;
56 struct smb_Dir
*dir_hnd
;
61 bool has_wild
; /* Set to true if the wcard entry has MS wildcard characters in it. */
62 bool did_stat
; /* Optimisation for non-wcard searches. */
66 #define INVALID_DPTR_KEY (-3)
68 /****************************************************************************
70 ****************************************************************************/
72 bool make_dir_struct(TALLOC_CTX
*ctx
,
82 char *mask2
= talloc_strdup(ctx
, mask
);
88 if ((mode
& aDIR
) != 0) {
93 if ((p
= strchr_m(mask2
,'.')) != NULL
) {
95 push_ascii(buf
+1,mask2
,8, 0);
96 push_ascii(buf
+9,p
+1,3, 0);
99 push_ascii(buf
+1,mask2
,11, 0);
102 memset(buf
+21,'\0',DIR_STRUCT_SIZE
-21);
104 srv_put_dos_date(buf
,22,date
);
105 SSVAL(buf
,26,size
& 0xFFFF);
106 SSVAL(buf
,28,(size
>> 16)&0xFFFF);
107 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
108 Strange, but verified on W2K3. Needed for OS/2. JRA. */
109 push_ascii(buf
+30,fname
,12, uc
? STR_UPPER
: 0);
110 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf
+30, fname
));
114 /****************************************************************************
115 Initialise the dir bitmap.
116 ****************************************************************************/
118 bool init_dptrs(struct smbd_server_connection
*sconn
)
120 if (sconn
->smb1
.searches
.dptr_bmap
) {
124 sconn
->smb1
.searches
.dptr_bmap
= bitmap_talloc(
125 sconn
, MAX_DIRECTORY_HANDLES
);
127 if (sconn
->smb1
.searches
.dptr_bmap
== NULL
) {
134 /****************************************************************************
135 Idle a dptr - the directory is closed but the control info is kept.
136 ****************************************************************************/
138 static void dptr_idle(struct dptr_struct
*dptr
)
141 DEBUG(4,("Idling dptr dnum %d\n",dptr
->dnum
));
142 TALLOC_FREE(dptr
->dir_hnd
);
146 /****************************************************************************
147 Idle the oldest dptr.
148 ****************************************************************************/
150 static void dptr_idleoldest(struct smbd_server_connection
*sconn
)
152 struct dptr_struct
*dptr
;
155 * Go to the end of the list.
157 dptr
= DLIST_TAIL(sconn
->smb1
.searches
.dirptrs
);
160 DEBUG(0,("No dptrs available to idle ?\n"));
165 * Idle the oldest pointer.
168 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
176 /****************************************************************************
177 Get the struct dptr_struct for a dir index.
178 ****************************************************************************/
180 static struct dptr_struct
*dptr_get(struct smbd_server_connection
*sconn
,
181 int key
, bool forclose
)
183 struct dptr_struct
*dptr
;
185 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
186 if(dptr
->dnum
== key
) {
187 if (!forclose
&& !dptr
->dir_hnd
) {
188 if (sconn
->smb1
.searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
)
189 dptr_idleoldest(sconn
);
190 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key
));
191 if (!(dptr
->dir_hnd
= OpenDir(
192 NULL
, dptr
->conn
, dptr
->path
,
193 dptr
->wcard
, dptr
->attr
))) {
194 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr
->path
,
199 DLIST_PROMOTE(sconn
->smb1
.searches
.dirptrs
,dptr
);
206 /****************************************************************************
207 Get the dir path for a dir index.
208 ****************************************************************************/
210 char *dptr_path(struct smbd_server_connection
*sconn
, int key
)
212 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
218 /****************************************************************************
219 Get the dir wcard for a dir index.
220 ****************************************************************************/
222 char *dptr_wcard(struct smbd_server_connection
*sconn
, int key
)
224 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
230 /****************************************************************************
231 Get the dir attrib for a dir index.
232 ****************************************************************************/
234 uint16
dptr_attr(struct smbd_server_connection
*sconn
, int key
)
236 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
242 /****************************************************************************
243 Close a dptr (internal func).
244 ****************************************************************************/
246 static void dptr_close_internal(struct dptr_struct
*dptr
)
248 struct smbd_server_connection
*sconn
= dptr
->conn
->sconn
;
250 DEBUG(4,("closing dptr key %d\n",dptr
->dnum
));
256 DLIST_REMOVE(sconn
->smb1
.searches
.dirptrs
, dptr
);
259 * Free the dnum in the bitmap. Remember the dnum value is always
260 * biased by one with respect to the bitmap.
263 if(bitmap_query(sconn
->smb1
.searches
.dptr_bmap
, dptr
->dnum
- 1) != true) {
264 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
268 bitmap_clear(sconn
->smb1
.searches
.dptr_bmap
, dptr
->dnum
- 1);
271 TALLOC_FREE(dptr
->dir_hnd
);
273 /* Lanman 2 specific code */
274 SAFE_FREE(dptr
->wcard
);
275 string_set(&dptr
->path
,"");
279 /****************************************************************************
280 Close a dptr given a key.
281 ****************************************************************************/
283 void dptr_close(struct smbd_server_connection
*sconn
, int *key
)
285 struct dptr_struct
*dptr
;
287 if(*key
== INVALID_DPTR_KEY
)
290 /* OS/2 seems to use -1 to indicate "close all directories" */
292 struct dptr_struct
*next
;
293 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
; dptr
= next
) {
295 dptr_close_internal(dptr
);
297 *key
= INVALID_DPTR_KEY
;
301 dptr
= dptr_get(sconn
, *key
, true);
304 DEBUG(0,("Invalid key %d given to dptr_close\n", *key
));
308 dptr_close_internal(dptr
);
310 *key
= INVALID_DPTR_KEY
;
313 /****************************************************************************
314 Close all dptrs for a cnum.
315 ****************************************************************************/
317 void dptr_closecnum(connection_struct
*conn
)
319 struct dptr_struct
*dptr
, *next
;
320 struct smbd_server_connection
*sconn
= conn
->sconn
;
326 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
; dptr
= next
) {
328 if (dptr
->conn
== conn
) {
329 dptr_close_internal(dptr
);
334 /****************************************************************************
335 Idle all dptrs for a cnum.
336 ****************************************************************************/
338 void dptr_idlecnum(connection_struct
*conn
)
340 struct dptr_struct
*dptr
;
341 struct smbd_server_connection
*sconn
= conn
->sconn
;
347 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
; dptr
= dptr
->next
) {
348 if (dptr
->conn
== conn
&& dptr
->dir_hnd
) {
354 /****************************************************************************
355 Close a dptr that matches a given path, only if it matches the spid also.
356 ****************************************************************************/
358 void dptr_closepath(struct smbd_server_connection
*sconn
,
359 char *path
,uint16 spid
)
361 struct dptr_struct
*dptr
, *next
;
362 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
; dptr
= next
) {
364 if (spid
== dptr
->spid
&& strequal(dptr
->path
,path
))
365 dptr_close_internal(dptr
);
369 /****************************************************************************
370 Try and close the oldest handle not marked for
371 expect close in the hope that the client has
372 finished with that one.
373 ****************************************************************************/
375 static void dptr_close_oldest(struct smbd_server_connection
*sconn
,
378 struct dptr_struct
*dptr
;
381 * Go to the end of the list.
383 for(dptr
= sconn
->smb1
.searches
.dirptrs
; dptr
&& dptr
->next
; dptr
= dptr
->next
)
387 DEBUG(0,("No old dptrs available to close oldest ?\n"));
392 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
393 * does not have expect_close set. If 'old' is false, close
394 * one of the new dnum handles.
397 for(; dptr
; dptr
= DLIST_PREV(dptr
)) {
398 if ((old
&& (dptr
->dnum
< 256) && !dptr
->expect_close
) ||
399 (!old
&& (dptr
->dnum
> 255))) {
400 dptr_close_internal(dptr
);
406 /****************************************************************************
407 Create a new dir ptr. If the flag old_handle is true then we must allocate
408 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
409 one byte long. If old_handle is false we allocate from the range
410 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
411 a directory handle is never zero.
412 wcard must not be zero.
413 ****************************************************************************/
415 NTSTATUS
dptr_create(connection_struct
*conn
, const char *path
, bool old_handle
, bool expect_close
,uint16 spid
,
416 const char *wcard
, bool wcard_has_wild
, uint32 attr
, struct dptr_struct
**dptr_ret
)
418 struct smbd_server_connection
*sconn
= conn
->sconn
;
419 struct dptr_struct
*dptr
= NULL
;
420 struct smb_Dir
*dir_hnd
;
423 DEBUG(5,("dptr_create dir=%s\n", path
));
426 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
427 return NT_STATUS_INTERNAL_ERROR
;
431 return NT_STATUS_INVALID_PARAMETER
;
434 status
= check_name(conn
,path
);
435 if (!NT_STATUS_IS_OK(status
)) {
439 dir_hnd
= OpenDir(NULL
, conn
, path
, wcard
, attr
);
441 return map_nt_error_from_unix(errno
);
444 if (sconn
->smb1
.searches
.dirhandles_open
>= MAX_OPEN_DIRECTORIES
) {
445 dptr_idleoldest(sconn
);
448 dptr
= SMB_MALLOC_P(struct dptr_struct
);
450 DEBUG(0,("malloc fail in dptr_create.\n"));
451 TALLOC_FREE(dir_hnd
);
452 return NT_STATUS_NO_MEMORY
;
460 * This is an old-style SMBsearch request. Ensure the
461 * value we return will fit in the range 1-255.
464 dptr
->dnum
= bitmap_find(sconn
->smb1
.searches
.dptr_bmap
, 0);
466 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
469 * Try and close the oldest handle not marked for
470 * expect close in the hope that the client has
471 * finished with that one.
474 dptr_close_oldest(sconn
, true);
476 /* Now try again... */
477 dptr
->dnum
= bitmap_find(sconn
->smb1
.searches
.dptr_bmap
, 0);
478 if(dptr
->dnum
== -1 || dptr
->dnum
> 254) {
479 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr
->dnum
));
481 TALLOC_FREE(dir_hnd
);
482 return NT_STATUS_TOO_MANY_OPENED_FILES
;
488 * This is a new-style trans2 request. Allocate from
489 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
492 dptr
->dnum
= bitmap_find(sconn
->smb1
.searches
.dptr_bmap
, 255);
494 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
497 * Try and close the oldest handle close in the hope that
498 * the client has finished with that one. This will only
499 * happen in the case of the Win98 client bug where it leaks
503 dptr_close_oldest(sconn
, false);
505 /* Now try again... */
506 dptr
->dnum
= bitmap_find(sconn
->smb1
.searches
.dptr_bmap
, 255);
508 if(dptr
->dnum
== -1 || dptr
->dnum
< 255) {
509 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr
->dnum
));
511 TALLOC_FREE(dir_hnd
);
512 return NT_STATUS_TOO_MANY_OPENED_FILES
;
517 bitmap_set(sconn
->smb1
.searches
.dptr_bmap
, dptr
->dnum
);
519 dptr
->dnum
+= 1; /* Always bias the dnum by one - no zero dnums allowed. */
521 string_set(&dptr
->path
,path
);
523 dptr
->dir_hnd
= dir_hnd
;
525 dptr
->expect_close
= expect_close
;
526 dptr
->wcard
= SMB_STRDUP(wcard
);
528 bitmap_clear(sconn
->smb1
.searches
.dptr_bmap
, dptr
->dnum
- 1);
530 TALLOC_FREE(dir_hnd
);
531 return NT_STATUS_NO_MEMORY
;
533 if (lp_posix_pathnames() || (wcard
[0] == '.' && wcard
[1] == 0)) {
534 dptr
->has_wild
= True
;
536 dptr
->has_wild
= wcard_has_wild
;
541 DLIST_ADD(sconn
->smb1
.searches
.dirptrs
, dptr
);
543 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
544 dptr
->dnum
,path
,expect_close
));
552 /****************************************************************************
553 Wrapper functions to access the lower level directory handles.
554 ****************************************************************************/
556 int dptr_CloseDir(struct dptr_struct
*dptr
)
558 struct smbd_server_connection
*sconn
= dptr
->conn
->sconn
;
559 DLIST_REMOVE(sconn
->smb1
.searches
.dirptrs
, dptr
);
560 TALLOC_FREE(dptr
->dir_hnd
);
564 void dptr_SeekDir(struct dptr_struct
*dptr
, long offset
)
566 SeekDir(dptr
->dir_hnd
, offset
);
569 long dptr_TellDir(struct dptr_struct
*dptr
)
571 return TellDir(dptr
->dir_hnd
);
574 bool dptr_has_wild(struct dptr_struct
*dptr
)
576 return dptr
->has_wild
;
579 int dptr_dnum(struct dptr_struct
*dptr
)
584 /****************************************************************************
585 Return the next visible file name, skipping veto'd and invisible files.
586 ****************************************************************************/
588 static const char *dptr_normal_ReadDirName(struct dptr_struct
*dptr
,
589 long *poffset
, SMB_STRUCT_STAT
*pst
,
592 /* Normal search for the next file. */
594 char *talloced
= NULL
;
596 while ((name
= ReadDirName(dptr
->dir_hnd
, poffset
, pst
, &talloced
))
598 if (is_visible_file(dptr
->conn
, dptr
->path
, name
, pst
, True
)) {
599 *ptalloced
= talloced
;
602 TALLOC_FREE(talloced
);
607 /****************************************************************************
608 Return the next visible file name, skipping veto'd and invisible files.
609 ****************************************************************************/
611 char *dptr_ReadDirName(TALLOC_CTX
*ctx
,
612 struct dptr_struct
*dptr
,
614 SMB_STRUCT_STAT
*pst
)
616 struct smb_filename smb_fname_base
;
618 const char *name_temp
= NULL
;
619 char *talloced
= NULL
;
620 char *pathreal
= NULL
;
621 char *found_name
= NULL
;
624 SET_STAT_INVALID(*pst
);
626 if (dptr
->has_wild
|| dptr
->did_stat
) {
627 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
,
629 if (name_temp
== NULL
) {
632 if (talloced
!= NULL
) {
633 return talloc_move(ctx
, &talloced
);
635 return talloc_strdup(ctx
, name_temp
);
638 /* If poffset is -1 then we know we returned this name before and we
639 * have no wildcards. We're at the end of the directory. */
640 if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
644 /* We know the stored wcard contains no wildcard characters.
645 * See if we can match with a stat call. If we can't, then set
646 * did_stat to true to ensure we only do this once and keep
649 dptr
->did_stat
= true;
651 /* First check if it should be visible. */
652 if (!is_visible_file(dptr
->conn
, dptr
->path
, dptr
->wcard
,
655 /* This only returns false if the file was found, but
656 is explicitly not visible. Set us to end of
657 directory, but return NULL as we know we can't ever
662 if (VALID_STAT(*pst
)) {
663 name
= talloc_strdup(ctx
, dptr
->wcard
);
667 pathreal
= talloc_asprintf(ctx
,
674 /* Create an smb_filename with stream_name == NULL. */
675 ZERO_STRUCT(smb_fname_base
);
676 smb_fname_base
.base_name
= pathreal
;
678 if (SMB_VFS_STAT(dptr
->conn
, &smb_fname_base
) == 0) {
679 *pst
= smb_fname_base
.st
;
680 name
= talloc_strdup(ctx
, dptr
->wcard
);
683 /* If we get any other error than ENOENT or ENOTDIR
684 then the file exists we just can't stat it. */
685 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
686 name
= talloc_strdup(ctx
, dptr
->wcard
);
691 /* Stat failed. We know this is authoratiative if we are
692 * providing case sensitive semantics or the underlying
693 * filesystem is case sensitive.
695 if (dptr
->conn
->case_sensitive
||
696 !(dptr
->conn
->fs_capabilities
& FILE_CASE_SENSITIVE_SEARCH
))
702 * Try case-insensitive stat if the fs has the ability. This avoids
703 * scanning the whole directory.
705 ret
= SMB_VFS_GET_REAL_FILENAME(dptr
->conn
, dptr
->path
, dptr
->wcard
,
710 } else if (errno
== ENOENT
) {
711 /* The case-insensitive lookup was authoritative. */
715 TALLOC_FREE(pathreal
);
717 name_temp
= dptr_normal_ReadDirName(dptr
, poffset
, pst
, &talloced
);
718 if (name_temp
== NULL
) {
721 if (talloced
!= NULL
) {
722 return talloc_move(ctx
, &talloced
);
724 return talloc_strdup(ctx
, name_temp
);
727 TALLOC_FREE(pathreal
);
729 /* We need to set the underlying dir_hnd offset to -1
730 * also as this function is usually called with the
731 * output from TellDir. */
732 dptr
->dir_hnd
->offset
= *poffset
= END_OF_DIRECTORY_OFFSET
;
736 /****************************************************************************
737 Search for a file by name, skipping veto'ed and not visible files.
738 ****************************************************************************/
740 bool dptr_SearchDir(struct dptr_struct
*dptr
, const char *name
, long *poffset
, SMB_STRUCT_STAT
*pst
)
742 SET_STAT_INVALID(*pst
);
744 if (!dptr
->has_wild
&& (dptr
->dir_hnd
->offset
== END_OF_DIRECTORY_OFFSET
)) {
745 /* This is a singleton directory and we're already at the end. */
746 *poffset
= END_OF_DIRECTORY_OFFSET
;
750 return SearchDir(dptr
->dir_hnd
, name
, poffset
);
753 /****************************************************************************
754 Add the name we're returning into the underlying cache.
755 ****************************************************************************/
757 void dptr_DirCacheAdd(struct dptr_struct
*dptr
, const char *name
, long offset
)
759 DirCacheAdd(dptr
->dir_hnd
, name
, offset
);
762 /****************************************************************************
763 Initialize variables & state data at the beginning of all search SMB requests.
764 ****************************************************************************/
765 void dptr_init_search_op(struct dptr_struct
*dptr
)
767 SMB_VFS_INIT_SEARCH_OP(dptr
->conn
, dptr
->dir_hnd
->dir
);
770 /****************************************************************************
771 Fill the 5 byte server reserved dptr field.
772 ****************************************************************************/
774 bool dptr_fill(struct smbd_server_connection
*sconn
,
775 char *buf1
,unsigned int key
)
777 unsigned char *buf
= (unsigned char *)buf1
;
778 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
781 DEBUG(1,("filling null dirptr %d\n",key
));
784 offset
= (uint32
)TellDir(dptr
->dir_hnd
);
785 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key
,
786 (long)dptr
->dir_hnd
,(int)offset
));
792 /****************************************************************************
793 Fetch the dir ptr and seek it given the 5 byte server field.
794 ****************************************************************************/
796 struct dptr_struct
*dptr_fetch(struct smbd_server_connection
*sconn
,
799 unsigned int key
= *(unsigned char *)buf
;
800 struct dptr_struct
*dptr
= dptr_get(sconn
, key
, false);
805 DEBUG(3,("fetched null dirptr %d\n",key
));
809 offset
= IVAL(buf
,1);
810 if (offset
== (uint32
)-1) {
811 seekoff
= END_OF_DIRECTORY_OFFSET
;
813 seekoff
= (long)offset
;
815 SeekDir(dptr
->dir_hnd
,seekoff
);
816 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
817 key
, dptr
->path
, (int)seekoff
));
821 /****************************************************************************
823 ****************************************************************************/
825 struct dptr_struct
*dptr_fetch_lanman2(struct smbd_server_connection
*sconn
,
828 struct dptr_struct
*dptr
= dptr_get(sconn
, dptr_num
, false);
831 DEBUG(3,("fetched null dirptr %d\n",dptr_num
));
834 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num
,dptr
->path
));
838 /****************************************************************************
839 Check that a file matches a particular file type.
840 ****************************************************************************/
842 bool dir_check_ftype(connection_struct
*conn
, uint32 mode
, uint32 dirtype
)
846 /* Check the "may have" search bits. */
847 if (((mode
& ~dirtype
) & (aHIDDEN
| aSYSTEM
| aDIR
)) != 0)
850 /* Check the "must have" bits, which are the may have bits shifted eight */
851 /* If must have bit is set, the file/dir can not be returned in search unless the matching
852 file attribute is set */
853 mask
= ((dirtype
>> 8) & (aDIR
|aARCH
|aRONLY
|aHIDDEN
|aSYSTEM
)); /* & 0x37 */
855 if((mask
& (mode
& (aDIR
|aARCH
|aRONLY
|aHIDDEN
|aSYSTEM
))) == mask
) /* check if matching attribute present */
864 static bool mangle_mask_match(connection_struct
*conn
,
865 const char *filename
,
870 if (!name_to_8_3(filename
,mname
,False
,conn
->params
)) {
873 return mask_match_search(mname
,mask
,False
);
876 bool smbd_dirptr_get_entry(TALLOC_CTX
*ctx
,
877 struct dptr_struct
*dirptr
,
882 bool (*match_fn
)(TALLOC_CTX
*ctx
,
887 bool (*mode_fn
)(TALLOC_CTX
*ctx
,
889 struct smb_filename
*smb_fname
,
893 struct smb_filename
**_smb_fname
,
897 connection_struct
*conn
= dirptr
->conn
;
903 needslash
= ( dirptr
->path
[strlen(dirptr
->path
) -1] != '/');
908 SMB_STRUCT_STAT sbuf
;
912 char *pathreal
= NULL
;
913 struct smb_filename smb_fname
;
918 cur_offset
= dptr_TellDir(dirptr
);
919 prev_offset
= cur_offset
;
920 dname
= dptr_ReadDirName(ctx
, dirptr
, &cur_offset
, &sbuf
);
922 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
923 (long)dirptr
, cur_offset
));
929 isdots
= (ISDOT(dname
) || ISDOTDOT(dname
));
930 if (dont_descend
&& !isdots
) {
936 * fname may get mangled, dname is never mangled.
937 * Whenever we're accessing the filesystem we use
938 * pathreal which is composed from dname.
941 ok
= match_fn(ctx
, private_data
, dname
, mask
, &fname
);
947 pathreal
= talloc_asprintf(ctx
, "%s%s%s",
957 /* Create smb_fname with NULL stream_name. */
958 ZERO_STRUCT(smb_fname
);
959 smb_fname
.base_name
= pathreal
;
962 ok
= mode_fn(ctx
, private_data
, &smb_fname
, &mode
);
966 TALLOC_FREE(pathreal
);
970 if (!dir_check_ftype(conn
, mode
, dirtype
)) {
971 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
972 fname
, (unsigned int)mode
, (unsigned int)dirtype
));
975 TALLOC_FREE(pathreal
);
980 struct timespec write_time_ts
;
981 struct file_id fileid
;
983 fileid
= vfs_file_id_from_sbuf(conn
,
985 get_file_infos(fileid
, NULL
, &write_time_ts
);
986 if (!null_timespec(write_time_ts
)) {
987 update_stat_ex_mtime(&smb_fname
.st
,
992 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
994 mask
, smb_fname_str_dbg(&smb_fname
),
997 DirCacheAdd(dirptr
->dir_hnd
, dname
, cur_offset
);
1001 status
= copy_smb_filename(ctx
, &smb_fname
, _smb_fname
);
1002 TALLOC_FREE(pathreal
);
1003 if (!NT_STATUS_IS_OK(status
)) {
1008 *_prev_offset
= prev_offset
;
1016 /****************************************************************************
1017 Get an 8.3 directory entry.
1018 ****************************************************************************/
1020 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX
*ctx
,
1026 connection_struct
*conn
= (connection_struct
*)private_data
;
1028 if ((strcmp(mask
,"*.*") == 0) ||
1029 mask_match_search(dname
, mask
, false) ||
1030 mangle_mask_match(conn
, dname
, mask
)) {
1034 if (!mangle_is_8_3(dname
, false, conn
->params
)) {
1035 bool ok
= name_to_8_3(dname
, mname
, false,
1045 *_fname
= talloc_strdup(ctx
, fname
);
1046 if (*_fname
== NULL
) {
1056 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX
*ctx
,
1058 struct smb_filename
*smb_fname
,
1061 connection_struct
*conn
= (connection_struct
*)private_data
;
1063 if (!VALID_STAT(smb_fname
->st
)) {
1064 if ((SMB_VFS_STAT(conn
, smb_fname
)) != 0) {
1065 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1066 "Couldn't stat [%s]. Error "
1068 smb_fname_str_dbg(smb_fname
),
1074 *_mode
= dos_mode(conn
, smb_fname
);
1078 bool get_dir_entry(TALLOC_CTX
*ctx
,
1079 struct dptr_struct
*dirptr
,
1085 struct timespec
*_date
,
1089 connection_struct
*conn
= dirptr
->conn
;
1091 struct smb_filename
*smb_fname
= NULL
;
1096 ok
= smbd_dirptr_get_entry(ctx
,
1102 smbd_dirptr_8_3_match_fn
,
1103 smbd_dirptr_8_3_mode_fn
,
1113 *_fname
= talloc_move(ctx
, &fname
);
1114 *_size
= smb_fname
->st
.st_ex_size
;
1116 *_date
= smb_fname
->st
.st_ex_mtime
;
1117 TALLOC_FREE(smb_fname
);
1121 /*******************************************************************
1122 Check to see if a user can read a file. This is only approximate,
1123 it is used as part of the "hide unreadable" option. Don't
1124 use it for anything security sensitive.
1125 ********************************************************************/
1127 static bool user_can_read_file(connection_struct
*conn
,
1128 struct smb_filename
*smb_fname
)
1131 * Never hide files from the root user.
1132 * We use (uid_t)0 here not sec_initial_uid()
1133 * as make test uses a single user context.
1136 if (get_current_uid(conn
) == (uid_t
)0) {
1140 return can_access_file_acl(conn
, smb_fname
, FILE_READ_DATA
);
1143 /*******************************************************************
1144 Check to see if a user can write a file (and only files, we do not
1145 check dirs on this one). This is only approximate,
1146 it is used as part of the "hide unwriteable" option. Don't
1147 use it for anything security sensitive.
1148 ********************************************************************/
1150 static bool user_can_write_file(connection_struct
*conn
,
1151 const struct smb_filename
*smb_fname
)
1154 * Never hide files from the root user.
1155 * We use (uid_t)0 here not sec_initial_uid()
1156 * as make test uses a single user context.
1159 if (get_current_uid(conn
) == (uid_t
)0) {
1163 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1165 /* Pseudo-open the file */
1167 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1171 return can_write_to_file(conn
, smb_fname
);
1174 /*******************************************************************
1175 Is a file a "special" type ?
1176 ********************************************************************/
1178 static bool file_is_special(connection_struct
*conn
,
1179 const struct smb_filename
*smb_fname
)
1182 * Never hide files from the root user.
1183 * We use (uid_t)0 here not sec_initial_uid()
1184 * as make test uses a single user context.
1187 if (get_current_uid(conn
) == (uid_t
)0) {
1191 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
1193 if (S_ISREG(smb_fname
->st
.st_ex_mode
) ||
1194 S_ISDIR(smb_fname
->st
.st_ex_mode
) ||
1195 S_ISLNK(smb_fname
->st
.st_ex_mode
))
1201 /*******************************************************************
1202 Should the file be seen by the client?
1203 NOTE: A successful return is no guarantee of the file's existence.
1204 ********************************************************************/
1206 bool is_visible_file(connection_struct
*conn
, const char *dir_path
,
1207 const char *name
, SMB_STRUCT_STAT
*pst
, bool use_veto
)
1209 bool hide_unreadable
= lp_hideunreadable(SNUM(conn
));
1210 bool hide_unwriteable
= lp_hideunwriteable_files(SNUM(conn
));
1211 bool hide_special
= lp_hide_special_files(SNUM(conn
));
1213 struct smb_filename
*smb_fname_base
= NULL
;
1217 if ((strcmp(".",name
) == 0) || (strcmp("..",name
) == 0)) {
1218 return True
; /* . and .. are always visible. */
1221 /* If it's a vetoed file, pretend it doesn't even exist */
1222 if (use_veto
&& IS_VETO_PATH(conn
, name
)) {
1223 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name
));
1227 if (hide_unreadable
|| hide_unwriteable
|| hide_special
) {
1228 entry
= talloc_asprintf(talloc_tos(), "%s/%s", dir_path
, name
);
1234 /* Create an smb_filename with stream_name == NULL. */
1235 status
= create_synthetic_smb_fname(talloc_tos(), entry
, NULL
,
1236 pst
, &smb_fname_base
);
1237 if (!NT_STATUS_IS_OK(status
)) {
1242 /* If the file name does not exist, there's no point checking
1243 * the configuration options. We succeed, on the basis that the
1244 * checks *might* have passed if the file was present.
1246 if (!VALID_STAT(*pst
)) {
1247 if (SMB_VFS_STAT(conn
, smb_fname_base
) != 0) {
1251 *pst
= smb_fname_base
->st
;
1255 /* Honour _hide unreadable_ option */
1256 if (hide_unreadable
&&
1257 !user_can_read_file(conn
, smb_fname_base
)) {
1258 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1263 /* Honour _hide unwriteable_ option */
1264 if (hide_unwriteable
&& !user_can_write_file(conn
,
1266 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1271 /* Honour _hide_special_ option */
1272 if (hide_special
&& file_is_special(conn
, smb_fname_base
)) {
1273 DEBUG(10,("is_visible_file: file %s is special.\n",
1282 TALLOC_FREE(smb_fname_base
);
1287 static int smb_Dir_destructor(struct smb_Dir
*dirp
)
1290 SMB_VFS_CLOSEDIR(dirp
->conn
,dirp
->dir
);
1292 if (dirp
->conn
->sconn
) {
1293 dirp
->conn
->sconn
->smb1
.searches
.dirhandles_open
--;
1298 /*******************************************************************
1300 ********************************************************************/
1302 struct smb_Dir
*OpenDir(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
,
1303 const char *name
, const char *mask
, uint32 attr
)
1305 struct smb_Dir
*dirp
= TALLOC_ZERO_P(mem_ctx
, struct smb_Dir
);
1306 struct smbd_server_connection
*sconn
= conn
->sconn
;
1313 dirp
->name_cache_size
= lp_directory_name_cache_size(SNUM(conn
));
1315 dirp
->dir_path
= talloc_strdup(dirp
, name
);
1316 if (!dirp
->dir_path
) {
1322 sconn
->smb1
.searches
.dirhandles_open
++;
1324 talloc_set_destructor(dirp
, smb_Dir_destructor
);
1326 dirp
->dir
= SMB_VFS_OPENDIR(conn
, dirp
->dir_path
, mask
, attr
);
1328 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp
->dir_path
,
1340 /*******************************************************************
1341 Read from a directory.
1342 Return directory entry, current offset, and optional stat information.
1343 Don't check for veto or invisible files.
1344 ********************************************************************/
1346 const char *ReadDirName(struct smb_Dir
*dirp
, long *poffset
,
1347 SMB_STRUCT_STAT
*sbuf
, char **ptalloced
)
1350 char *talloced
= NULL
;
1351 connection_struct
*conn
= dirp
->conn
;
1353 /* Cheat to allow . and .. to be the first entries returned. */
1354 if (((*poffset
== START_OF_DIRECTORY_OFFSET
) ||
1355 (*poffset
== DOT_DOT_DIRECTORY_OFFSET
)) && (dirp
->file_number
< 2))
1357 if (dirp
->file_number
== 0) {
1359 *poffset
= dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1362 *poffset
= dirp
->offset
= DOT_DOT_DIRECTORY_OFFSET
;
1364 dirp
->file_number
++;
1367 } else if (*poffset
== END_OF_DIRECTORY_OFFSET
) {
1368 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1371 /* A real offset, seek to it. */
1372 SeekDir(dirp
, *poffset
);
1375 while ((n
= vfs_readdirname(conn
, dirp
->dir
, sbuf
, &talloced
))) {
1376 /* Ignore . and .. - we've already returned them. */
1378 if ((n
[1] == '\0') || (n
[1] == '.' && n
[2] == '\0')) {
1379 TALLOC_FREE(talloced
);
1383 *poffset
= dirp
->offset
= SMB_VFS_TELLDIR(conn
, dirp
->dir
);
1384 *ptalloced
= talloced
;
1385 dirp
->file_number
++;
1388 *poffset
= dirp
->offset
= END_OF_DIRECTORY_OFFSET
;
1393 /*******************************************************************
1394 Rewind to the start.
1395 ********************************************************************/
1397 void RewindDir(struct smb_Dir
*dirp
, long *poffset
)
1399 SMB_VFS_REWINDDIR(dirp
->conn
, dirp
->dir
);
1400 dirp
->file_number
= 0;
1401 dirp
->offset
= START_OF_DIRECTORY_OFFSET
;
1402 *poffset
= START_OF_DIRECTORY_OFFSET
;
1405 /*******************************************************************
1407 ********************************************************************/
1409 void SeekDir(struct smb_Dir
*dirp
, long offset
)
1411 if (offset
!= dirp
->offset
) {
1412 if (offset
== START_OF_DIRECTORY_OFFSET
) {
1413 RewindDir(dirp
, &offset
);
1415 * Ok we should really set the file number here
1416 * to 1 to enable ".." to be returned next. Trouble
1417 * is I'm worried about callers using SeekDir(dirp,0)
1418 * as equivalent to RewindDir(). So leave this alone
1421 } else if (offset
== DOT_DOT_DIRECTORY_OFFSET
) {
1422 RewindDir(dirp
, &offset
);
1424 * Set the file number to 2 - we want to get the first
1425 * real file entry (the one we return after "..")
1426 * on the next ReadDir.
1428 dirp
->file_number
= 2;
1429 } else if (offset
== END_OF_DIRECTORY_OFFSET
) {
1430 ; /* Don't seek in this case. */
1432 SMB_VFS_SEEKDIR(dirp
->conn
, dirp
->dir
, offset
);
1434 dirp
->offset
= offset
;
1438 /*******************************************************************
1439 Tell a dir position.
1440 ********************************************************************/
1442 long TellDir(struct smb_Dir
*dirp
)
1444 return(dirp
->offset
);
1447 /*******************************************************************
1448 Add an entry into the dcache.
1449 ********************************************************************/
1451 void DirCacheAdd(struct smb_Dir
*dirp
, const char *name
, long offset
)
1453 struct name_cache_entry
*e
;
1455 if (dirp
->name_cache_size
== 0) {
1459 if (dirp
->name_cache
== NULL
) {
1460 dirp
->name_cache
= TALLOC_ZERO_ARRAY(
1461 dirp
, struct name_cache_entry
, dirp
->name_cache_size
);
1463 if (dirp
->name_cache
== NULL
) {
1468 dirp
->name_cache_index
= (dirp
->name_cache_index
+1) %
1469 dirp
->name_cache_size
;
1470 e
= &dirp
->name_cache
[dirp
->name_cache_index
];
1471 TALLOC_FREE(e
->name
);
1472 e
->name
= talloc_strdup(dirp
, name
);
1476 /*******************************************************************
1477 Find an entry by name. Leave us at the offset after it.
1478 Don't check for veto or invisible files.
1479 ********************************************************************/
1481 bool SearchDir(struct smb_Dir
*dirp
, const char *name
, long *poffset
)
1484 const char *entry
= NULL
;
1485 char *talloced
= NULL
;
1486 connection_struct
*conn
= dirp
->conn
;
1488 /* Search back in the name cache. */
1489 if (dirp
->name_cache_size
&& dirp
->name_cache
) {
1490 for (i
= dirp
->name_cache_index
; i
>= 0; i
--) {
1491 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1492 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1493 *poffset
= e
->offset
;
1494 SeekDir(dirp
, e
->offset
);
1498 for (i
= dirp
->name_cache_size
- 1; i
> dirp
->name_cache_index
; i
--) {
1499 struct name_cache_entry
*e
= &dirp
->name_cache
[i
];
1500 if (e
->name
&& (conn
->case_sensitive
? (strcmp(e
->name
, name
) == 0) : strequal(e
->name
, name
))) {
1501 *poffset
= e
->offset
;
1502 SeekDir(dirp
, e
->offset
);
1508 /* Not found in the name cache. Rewind directory and start from scratch. */
1509 SMB_VFS_REWINDDIR(conn
, dirp
->dir
);
1510 dirp
->file_number
= 0;
1511 *poffset
= START_OF_DIRECTORY_OFFSET
;
1512 while ((entry
= ReadDirName(dirp
, poffset
, NULL
, &talloced
))) {
1513 if (conn
->case_sensitive
? (strcmp(entry
, name
) == 0) : strequal(entry
, name
)) {
1514 TALLOC_FREE(talloced
);
1517 TALLOC_FREE(talloced
);
1522 /*****************************************************************
1523 Is this directory empty ?
1524 *****************************************************************/
1526 NTSTATUS
can_delete_directory(struct connection_struct
*conn
,
1527 const char *dirname
)
1529 NTSTATUS status
= NT_STATUS_OK
;
1531 const char *dname
= NULL
;
1532 char *talloced
= NULL
;
1534 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
, dirname
,
1538 return map_nt_error_from_unix(errno
);
1541 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
, &talloced
))) {
1542 /* Quick check for "." and ".." */
1543 if (dname
[0] == '.') {
1544 if (!dname
[1] || (dname
[1] == '.' && !dname
[2])) {
1545 TALLOC_FREE(talloced
);
1550 if (!is_visible_file(conn
, dirname
, dname
, &st
, True
)) {
1551 TALLOC_FREE(talloced
);
1555 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1557 status
= NT_STATUS_DIRECTORY_NOT_EMPTY
;
1560 TALLOC_FREE(talloced
);
1561 TALLOC_FREE(dir_hnd
);