s3:smbd: don't limit the number of open dptrs for smb2 (bug #8592)
[Samba.git] / source3 / smbd / dir.c
blob9108a80c6caa919b694043e27e7e573b847dc3b6
1 /*
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/>.
21 #include "includes.h"
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 {
39 char *name;
40 long offset;
43 struct smb_Dir {
44 connection_struct *conn;
45 SMB_STRUCT_DIR *dir;
46 long offset;
47 char *dir_path;
48 size_t name_cache_size;
49 struct name_cache_entry *name_cache;
50 unsigned int name_cache_index;
51 unsigned int file_number;
54 struct dptr_struct {
55 struct dptr_struct *next, *prev;
56 int dnum;
57 uint16 spid;
58 struct connection_struct *conn;
59 struct smb_Dir *dir_hnd;
60 bool expect_close;
61 char *wcard;
62 uint32 attr;
63 char *path;
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,
69 files_struct *fsp,
70 const char *mask,
71 uint32 attr);
73 #define INVALID_DPTR_KEY (-3)
75 /****************************************************************************
76 Make a dir struct.
77 ****************************************************************************/
79 bool make_dir_struct(TALLOC_CTX *ctx,
80 char *buf,
81 const char *mask,
82 const char *fname,
83 SMB_OFF_T size,
84 uint32 mode,
85 time_t date,
86 bool uc)
88 char *p;
89 char *mask2 = talloc_strdup(ctx, mask);
91 if (!mask2) {
92 return False;
95 if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
96 size = 0;
99 memset(buf+1,' ',11);
100 if ((p = strchr_m(mask2,'.')) != NULL) {
101 *p = 0;
102 push_ascii(buf+1,mask2,8, 0);
103 push_ascii(buf+9,p+1,3, 0);
104 *p = '.';
105 } else {
106 push_ascii(buf+1,mask2,11, 0);
109 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
110 SCVAL(buf,21,mode);
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));
118 return True;
121 /****************************************************************************
122 Initialise the dir bitmap.
123 ****************************************************************************/
125 bool init_dptrs(struct smbd_server_connection *sconn)
127 if (sconn->searches.dptr_bmap) {
128 return true;
131 sconn->searches.dptr_bmap = bitmap_talloc(
132 sconn, MAX_DIRECTORY_HANDLES);
134 if (sconn->searches.dptr_bmap == NULL) {
135 return false;
138 return true;
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)
147 if (dptr->dir_hnd) {
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);
166 if(!dptr) {
167 DEBUG(0,("No dptrs available to idle ?\n"));
168 return;
172 * Idle the oldest pointer.
175 for(; dptr; dptr = DLIST_PREV(dptr)) {
176 if (dptr->dir_hnd) {
177 dptr_idle(dptr);
178 return;
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,
202 strerror(errno)));
203 return False;
206 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
207 return dptr;
210 return(NULL);
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);
220 if (dptr)
221 return(dptr->path);
222 return(NULL);
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);
232 if (dptr)
233 return(dptr->wcard);
234 return(NULL);
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);
244 if (dptr)
245 return(dptr->attr);
246 return(0);
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));
259 if (sconn == NULL) {
260 goto done;
263 if (sconn->using_smb2) {
264 goto done;
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",
276 dptr->dnum ));
279 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
281 done:
282 TALLOC_FREE(dptr->dir_hnd);
284 /* Lanman 2 specific code */
285 SAFE_FREE(dptr->wcard);
286 SAFE_FREE(dptr->path);
287 SAFE_FREE(dptr);
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)
299 return;
301 /* OS/2 seems to use -1 to indicate "close all directories" */
302 if (*key == -1) {
303 struct dptr_struct *next;
304 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
305 next = dptr->next;
306 dptr_close_internal(dptr);
308 *key = INVALID_DPTR_KEY;
309 return;
312 dptr = dptr_get(sconn, *key, true);
314 if (!dptr) {
315 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
316 return;
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;
333 if (sconn == NULL) {
334 return;
337 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
338 next = 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;
354 if (sconn == NULL) {
355 return;
358 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
359 if (dptr->conn == conn && dptr->dir_hnd) {
360 dptr_idle(dptr);
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) {
374 next = 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,
387 bool old)
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)
397 if(!dptr) {
398 DEBUG(0,("No old dptrs available to close oldest ?\n"));
399 return;
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);
412 return;
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;
433 NTSTATUS status;
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));
441 if (sconn == NULL) {
442 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
443 return NT_STATUS_INTERNAL_ERROR;
446 if (!wcard) {
447 return NT_STATUS_INVALID_PARAMETER;
450 if (fsp) {
451 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
452 } else {
453 status = check_name(conn,path);
454 if (!NT_STATUS_IS_OK(status)) {
455 return status;
457 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
460 if (!dir_hnd) {
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);
469 if(!dptr) {
470 DEBUG(0,("malloc fail in dptr_create.\n"));
471 TALLOC_FREE(dir_hnd);
472 return NT_STATUS_NO_MEMORY;
475 ZERO_STRUCTP(dptr);
477 dptr->path = SMB_STRDUP(path);
478 if (!dptr->path) {
479 SAFE_FREE(dptr);
480 TALLOC_FREE(dir_hnd);
481 return NT_STATUS_NO_MEMORY;
483 dptr->conn = conn;
484 dptr->dir_hnd = dir_hnd;
485 dptr->spid = spid;
486 dptr->expect_close = expect_close;
487 dptr->wcard = SMB_STRDUP(wcard);
488 if (!dptr->wcard) {
489 SAFE_FREE(dptr->path);
490 SAFE_FREE(dptr);
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;
496 } else {
497 dptr->has_wild = wcard_has_wild;
500 dptr->attr = attr;
502 if (sconn->using_smb2) {
503 goto done;
506 if(old_handle) {
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);
531 SAFE_FREE(dptr);
532 TALLOC_FREE(dir_hnd);
533 return NT_STATUS_TOO_MANY_OPENED_FILES;
536 } else {
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
551 * directory handles.
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);
563 SAFE_FREE(dptr);
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);
576 done:
577 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
578 dptr->dnum,path,expect_close));
580 *dptr_ret = dptr;
582 return NT_STATUS_OK;
586 /****************************************************************************
587 Wrapper functions to access the lower level directory handles.
588 ****************************************************************************/
590 void dptr_CloseDir(files_struct *fsp)
592 if (fsp->dptr) {
594 * Ugly hack. We have defined fdopendir to return ENOSYS if dirfd also isn't
595 * present. I hate Solaris. JRA.
597 #ifdef HAVE_DIRFD
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. */
602 fsp->fh->fd = -1;
604 #endif
605 dptr_close_internal(fsp->dptr);
606 fsp->dptr = NULL;
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)
627 return dptr->dnum;
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,
636 char **ptalloced)
638 /* Normal search for the next file. */
639 const char *name;
640 char *talloced = NULL;
642 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
643 != NULL) {
644 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
645 *ptalloced = talloced;
646 return name;
648 TALLOC_FREE(talloced);
650 return NULL;
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,
659 long *poffset,
660 SMB_STRUCT_STAT *pst)
662 struct smb_filename smb_fname_base;
663 char *name = NULL;
664 const char *name_temp = NULL;
665 char *talloced = NULL;
666 char *pathreal = NULL;
667 char *found_name = NULL;
668 int ret;
670 SET_STAT_INVALID(*pst);
672 if (dptr->has_wild || dptr->did_stat) {
673 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
674 &talloced);
675 if (name_temp == NULL) {
676 return 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) {
687 return NULL;
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
693 * searching. */
695 dptr->did_stat = true;
697 /* First check if it should be visible. */
698 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
699 pst, true))
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
704 find it. */
705 goto ret;
708 if (VALID_STAT(*pst)) {
709 name = talloc_strdup(ctx, dptr->wcard);
710 goto ret;
713 pathreal = talloc_asprintf(ctx,
714 "%s/%s",
715 dptr->path,
716 dptr->wcard);
717 if (!pathreal)
718 return NULL;
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);
727 goto clean;
728 } else {
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);
733 goto clean;
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))
744 goto clean;
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,
752 ctx, &found_name);
753 if (ret == 0) {
754 name = found_name;
755 goto clean;
756 } else if (errno == ENOENT) {
757 /* The case-insensitive lookup was authoritative. */
758 goto clean;
761 TALLOC_FREE(pathreal);
763 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
764 if (name_temp == NULL) {
765 return NULL;
767 if (talloced != NULL) {
768 return talloc_move(ctx, &talloced);
770 return talloc_strdup(ctx, name_temp);
772 clean:
773 TALLOC_FREE(pathreal);
774 ret:
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;
779 return name;
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;
793 return False;
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);
825 uint32 offset;
826 if (!dptr) {
827 DEBUG(1,("filling null dirptr %d\n",key));
828 return(False);
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));
833 buf[0] = key;
834 SIVAL(buf,1,offset);
835 return(True);
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,
843 char *buf, int *num)
845 unsigned int key = *(unsigned char *)buf;
846 struct dptr_struct *dptr = dptr_get(sconn, key, false);
847 uint32 offset;
848 long seekoff;
850 if (!dptr) {
851 DEBUG(3,("fetched null dirptr %d\n",key));
852 return(NULL);
854 *num = key;
855 offset = IVAL(buf,1);
856 if (offset == (uint32)-1) {
857 seekoff = END_OF_DIRECTORY_OFFSET;
858 } else {
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));
864 return(dptr);
867 /****************************************************************************
868 Fetch the dir ptr.
869 ****************************************************************************/
871 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
872 int dptr_num)
874 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
876 if (!dptr) {
877 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
878 return(NULL);
880 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
881 return(dptr);
884 /****************************************************************************
885 Check that a file matches a particular file type.
886 ****************************************************************************/
888 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
890 uint32 mask;
892 /* Check the "may have" search bits. */
893 if (((mode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)) != 0)
894 return False;
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 */
900 if(mask) {
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 */
902 return True;
903 else
904 return False;
907 return True;
910 static bool mangle_mask_match(connection_struct *conn,
911 const char *filename,
912 const char *mask)
914 char mname[13];
916 if (!name_to_8_3(filename,mname,False,conn->params)) {
917 return False;
919 return mask_match_search(mname,mask,False);
922 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
923 struct dptr_struct *dirptr,
924 const char *mask,
925 uint32_t dirtype,
926 bool dont_descend,
927 bool ask_sharemode,
928 bool (*match_fn)(TALLOC_CTX *ctx,
929 void *private_data,
930 const char *dname,
931 const char *mask,
932 char **_fname),
933 bool (*mode_fn)(TALLOC_CTX *ctx,
934 void *private_data,
935 struct smb_filename *smb_fname,
936 uint32_t *_mode),
937 void *private_data,
938 char **_fname,
939 struct smb_filename **_smb_fname,
940 uint32_t *_mode,
941 long *_prev_offset)
943 connection_struct *conn = dirptr->conn;
944 bool needslash;
946 *_smb_fname = NULL;
947 *_mode = 0;
949 needslash = ( dirptr->path[strlen(dirptr->path) -1] != '/');
951 while (true) {
952 long cur_offset;
953 long prev_offset;
954 SMB_STRUCT_STAT sbuf;
955 char *dname = NULL;
956 bool isdots;
957 char *fname = NULL;
958 char *pathreal = NULL;
959 struct smb_filename smb_fname;
960 uint32_t mode = 0;
961 bool ok;
962 NTSTATUS status;
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));
971 if (dname == NULL) {
972 return false;
975 isdots = (ISDOT(dname) || ISDOTDOT(dname));
976 if (dont_descend && !isdots) {
977 TALLOC_FREE(dname);
978 continue;
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);
988 if (!ok) {
989 TALLOC_FREE(dname);
990 continue;
993 pathreal = talloc_asprintf(ctx, "%s%s%s",
994 dirptr->path,
995 needslash?"/":"",
996 dname);
997 if (!pathreal) {
998 TALLOC_FREE(dname);
999 TALLOC_FREE(fname);
1000 return false;
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);
1009 if (!ok) {
1010 TALLOC_FREE(dname);
1011 TALLOC_FREE(fname);
1012 TALLOC_FREE(pathreal);
1013 continue;
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));
1019 TALLOC_FREE(dname);
1020 TALLOC_FREE(fname);
1021 TALLOC_FREE(pathreal);
1022 continue;
1025 if (ask_sharemode) {
1026 struct timespec write_time_ts;
1027 struct file_id fileid;
1029 fileid = vfs_file_id_from_sbuf(conn,
1030 &smb_fname.st);
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,
1034 write_time_ts);
1038 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1039 "fname=%s (%s)\n",
1040 mask, smb_fname_str_dbg(&smb_fname),
1041 dname, fname));
1043 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1045 TALLOC_FREE(dname);
1047 status = copy_smb_filename(ctx, &smb_fname, _smb_fname);
1048 TALLOC_FREE(pathreal);
1049 if (!NT_STATUS_IS_OK(status)) {
1050 return false;
1052 *_fname = fname;
1053 *_mode = mode;
1054 *_prev_offset = prev_offset;
1056 return true;
1059 return false;
1062 /****************************************************************************
1063 Get an 8.3 directory entry.
1064 ****************************************************************************/
1066 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1067 void *private_data,
1068 const char *dname,
1069 const char *mask,
1070 char **_fname)
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)) {
1077 char mname[13];
1078 const char *fname;
1080 if (!mangle_is_8_3(dname, false, conn->params)) {
1081 bool ok = name_to_8_3(dname, mname, false,
1082 conn->params);
1083 if (!ok) {
1084 return false;
1086 fname = mname;
1087 } else {
1088 fname = dname;
1091 *_fname = talloc_strdup(ctx, fname);
1092 if (*_fname == NULL) {
1093 return false;
1096 return true;
1099 return false;
1102 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1103 void *private_data,
1104 struct smb_filename *smb_fname,
1105 uint32_t *_mode)
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 "
1113 "= %s\n",
1114 smb_fname_str_dbg(smb_fname),
1115 strerror(errno)));
1116 return false;
1120 *_mode = dos_mode(conn, smb_fname);
1121 return true;
1124 bool get_dir_entry(TALLOC_CTX *ctx,
1125 struct dptr_struct *dirptr,
1126 const char *mask,
1127 uint32_t dirtype,
1128 char **_fname,
1129 SMB_OFF_T *_size,
1130 uint32_t *_mode,
1131 struct timespec *_date,
1132 bool check_descend,
1133 bool ask_sharemode)
1135 connection_struct *conn = dirptr->conn;
1136 char *fname = NULL;
1137 struct smb_filename *smb_fname = NULL;
1138 uint32_t mode = 0;
1139 long prev_offset;
1140 bool ok;
1142 ok = smbd_dirptr_get_entry(ctx,
1143 dirptr,
1144 mask,
1145 dirtype,
1146 check_descend,
1147 ask_sharemode,
1148 smbd_dirptr_8_3_match_fn,
1149 smbd_dirptr_8_3_mode_fn,
1150 conn,
1151 &fname,
1152 &smb_fname,
1153 &mode,
1154 &prev_offset);
1155 if (!ok) {
1156 return false;
1159 *_fname = talloc_move(ctx, &fname);
1160 *_size = smb_fname->st.st_ex_size;
1161 *_mode = mode;
1162 *_date = smb_fname->st.st_ex_mtime;
1163 TALLOC_FREE(smb_fname);
1164 return true;
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) {
1183 return True;
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) {
1206 return True;
1209 SMB_ASSERT(VALID_STAT(smb_fname->st));
1211 /* Pseudo-open the file */
1213 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1214 return True;
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) {
1234 return False;
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))
1242 return False;
1244 return True;
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));
1258 char *entry = NULL;
1259 struct smb_filename *smb_fname_base = NULL;
1260 NTSTATUS status;
1261 bool ret = false;
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 ));
1270 return False;
1273 if (hide_unreadable || hide_unwriteable || hide_special) {
1274 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1275 if (!entry) {
1276 ret = false;
1277 goto out;
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)) {
1284 ret = false;
1285 goto out;
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) {
1294 ret = true;
1295 goto out;
1296 } else {
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",
1305 entry ));
1306 ret = false;
1307 goto out;
1309 /* Honour _hide unwriteable_ option */
1310 if (hide_unwriteable && !user_can_write_file(conn,
1311 smb_fname_base)) {
1312 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1313 entry ));
1314 ret = false;
1315 goto out;
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",
1320 entry ));
1321 ret = false;
1322 goto out;
1326 ret = true;
1327 out:
1328 TALLOC_FREE(smb_fname_base);
1329 TALLOC_FREE(entry);
1330 return ret;
1333 static int smb_Dir_destructor(struct smb_Dir *dirp)
1335 if (dirp->dir) {
1336 #ifdef HAVE_DIRFD
1337 if (dirp->conn->sconn) {
1338 files_struct *fsp = file_find_fd(dirp->conn->sconn,
1339 dirfd(dirp->dir));
1340 if (fsp) {
1341 /* The call below closes the underlying fd. */
1342 fsp->fh->fd = -1;
1345 #endif
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--;
1351 return 0;
1354 /*******************************************************************
1355 Open a directory.
1356 ********************************************************************/
1358 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1359 const char *name,
1360 const char *mask,
1361 uint32 attr)
1363 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1364 struct smbd_server_connection *sconn = conn->sconn;
1366 if (!dirp) {
1367 return NULL;
1370 dirp->conn = conn;
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) {
1375 errno = ENOMEM;
1376 goto fail;
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);
1385 if (!dirp->dir) {
1386 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1387 strerror(errno) ));
1388 goto fail;
1391 return dirp;
1393 fail:
1394 TALLOC_FREE(dirp);
1395 return NULL;
1398 /*******************************************************************
1399 Open a directory from an fsp.
1400 ********************************************************************/
1402 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1403 files_struct *fsp,
1404 const char *mask,
1405 uint32 attr)
1407 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1408 struct smbd_server_connection *sconn = conn->sconn;
1410 if (!dirp) {
1411 return NULL;
1414 dirp->conn = conn;
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) {
1419 errno = ENOMEM;
1420 goto fail;
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 "
1432 "NULL (%s)\n",
1433 dirp->dir_path,
1434 strerror(errno)));
1435 if (errno != ENOSYS) {
1436 return NULL;
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);
1446 if (!dirp->dir) {
1447 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1448 strerror(errno) ));
1449 goto fail;
1452 return dirp;
1454 fail:
1455 TALLOC_FREE(dirp);
1456 return NULL;
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)
1469 const char *n;
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) {
1478 n = ".";
1479 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1480 } else {
1481 n = "..";
1482 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1484 dirp->file_number++;
1485 *ptalloced = NULL;
1486 return n;
1487 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1488 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1489 return NULL;
1490 } else {
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. */
1497 if (*n == '.') {
1498 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1499 TALLOC_FREE(talloced);
1500 continue;
1503 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1504 *ptalloced = talloced;
1505 dirp->file_number++;
1506 return n;
1508 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1509 *ptalloced = NULL;
1510 return NULL;
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 /*******************************************************************
1526 Seek a dir.
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
1539 * for now.
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. */
1551 } else {
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) {
1576 return;
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) {
1584 return;
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);
1593 e->offset = offset;
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)
1603 int i;
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);
1615 return True;
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);
1623 return True;
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);
1635 return True;
1637 TALLOC_FREE(talloced);
1639 return False;
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;
1650 long dirpos = 0;
1651 const char *dname = NULL;
1652 char *talloced = NULL;
1653 SMB_STRUCT_STAT st;
1654 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
1655 dirname, NULL, 0);
1657 if (!dir_hnd) {
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);
1666 continue;
1670 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1671 TALLOC_FREE(talloced);
1672 continue;
1675 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1676 dname ));
1677 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1678 break;
1680 TALLOC_FREE(talloced);
1681 TALLOC_FREE(dir_hnd);
1683 return status;