smbd: Tune "dir" a bit.
[Samba.git] / source3 / smbd / dir.c
blobf7bc325d9fe4f57c701510e25f094875ddeaf14e
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 size_t slashlen;
945 size_t pathlen;
947 *_smb_fname = NULL;
948 *_mode = 0;
950 pathlen = strlen(dirptr->path);
951 slashlen = ( dirptr->path[pathlen-1] != '/') ? 1 : 0;
953 while (true) {
954 long cur_offset;
955 long prev_offset;
956 SMB_STRUCT_STAT sbuf;
957 char *dname = NULL;
958 bool isdots;
959 char *fname = NULL;
960 char *pathreal = NULL;
961 struct smb_filename smb_fname;
962 uint32_t mode = 0;
963 bool ok;
964 NTSTATUS status;
966 cur_offset = dptr_TellDir(dirptr);
967 prev_offset = cur_offset;
968 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
970 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
971 (long)dirptr, cur_offset));
973 if (dname == NULL) {
974 return false;
977 isdots = (ISDOT(dname) || ISDOTDOT(dname));
978 if (dont_descend && !isdots) {
979 TALLOC_FREE(dname);
980 continue;
984 * fname may get mangled, dname is never mangled.
985 * Whenever we're accessing the filesystem we use
986 * pathreal which is composed from dname.
989 ok = match_fn(ctx, private_data, dname, mask, &fname);
990 if (!ok) {
991 TALLOC_FREE(dname);
992 continue;
996 * This used to be
997 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
998 * needslash?"/":"", dname);
999 * but this was measurably slower than doing the memcpy.
1002 pathreal = talloc_array(
1003 ctx, char,
1004 pathlen + slashlen + talloc_get_size(dname));
1005 if (!pathreal) {
1006 TALLOC_FREE(dname);
1007 TALLOC_FREE(fname);
1008 return false;
1011 memcpy(pathreal, dirptr->path, pathlen);
1012 pathreal[pathlen] = '/';
1013 memcpy(pathreal + slashlen + pathlen, dname,
1014 talloc_get_size(dname));
1016 /* Create smb_fname with NULL stream_name. */
1017 ZERO_STRUCT(smb_fname);
1018 smb_fname.base_name = pathreal;
1019 smb_fname.st = sbuf;
1021 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
1022 if (!ok) {
1023 TALLOC_FREE(dname);
1024 TALLOC_FREE(fname);
1025 TALLOC_FREE(pathreal);
1026 continue;
1029 if (!dir_check_ftype(conn, mode, dirtype)) {
1030 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1031 fname, (unsigned int)mode, (unsigned int)dirtype));
1032 TALLOC_FREE(dname);
1033 TALLOC_FREE(fname);
1034 TALLOC_FREE(pathreal);
1035 continue;
1038 if (ask_sharemode) {
1039 struct timespec write_time_ts;
1040 struct file_id fileid;
1042 fileid = vfs_file_id_from_sbuf(conn,
1043 &smb_fname.st);
1044 get_file_infos(fileid, 0, NULL, &write_time_ts);
1045 if (!null_timespec(write_time_ts)) {
1046 update_stat_ex_mtime(&smb_fname.st,
1047 write_time_ts);
1051 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1052 "fname=%s (%s)\n",
1053 mask, smb_fname_str_dbg(&smb_fname),
1054 dname, fname));
1056 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1058 TALLOC_FREE(dname);
1060 status = copy_smb_filename(ctx, &smb_fname, _smb_fname);
1061 TALLOC_FREE(pathreal);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 return false;
1065 *_fname = fname;
1066 *_mode = mode;
1067 *_prev_offset = prev_offset;
1069 return true;
1072 return false;
1075 /****************************************************************************
1076 Get an 8.3 directory entry.
1077 ****************************************************************************/
1079 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1080 void *private_data,
1081 const char *dname,
1082 const char *mask,
1083 char **_fname)
1085 connection_struct *conn = (connection_struct *)private_data;
1087 if ((strcmp(mask,"*.*") == 0) ||
1088 mask_match_search(dname, mask, false) ||
1089 mangle_mask_match(conn, dname, mask)) {
1090 char mname[13];
1091 const char *fname;
1093 if (!mangle_is_8_3(dname, false, conn->params)) {
1094 bool ok = name_to_8_3(dname, mname, false,
1095 conn->params);
1096 if (!ok) {
1097 return false;
1099 fname = mname;
1100 } else {
1101 fname = dname;
1104 *_fname = talloc_strdup(ctx, fname);
1105 if (*_fname == NULL) {
1106 return false;
1109 return true;
1112 return false;
1115 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1116 void *private_data,
1117 struct smb_filename *smb_fname,
1118 uint32_t *_mode)
1120 connection_struct *conn = (connection_struct *)private_data;
1122 if (!VALID_STAT(smb_fname->st)) {
1123 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1124 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1125 "Couldn't stat [%s]. Error "
1126 "= %s\n",
1127 smb_fname_str_dbg(smb_fname),
1128 strerror(errno)));
1129 return false;
1133 *_mode = dos_mode(conn, smb_fname);
1134 return true;
1137 bool get_dir_entry(TALLOC_CTX *ctx,
1138 struct dptr_struct *dirptr,
1139 const char *mask,
1140 uint32_t dirtype,
1141 char **_fname,
1142 SMB_OFF_T *_size,
1143 uint32_t *_mode,
1144 struct timespec *_date,
1145 bool check_descend,
1146 bool ask_sharemode)
1148 connection_struct *conn = dirptr->conn;
1149 char *fname = NULL;
1150 struct smb_filename *smb_fname = NULL;
1151 uint32_t mode = 0;
1152 long prev_offset;
1153 bool ok;
1155 ok = smbd_dirptr_get_entry(ctx,
1156 dirptr,
1157 mask,
1158 dirtype,
1159 check_descend,
1160 ask_sharemode,
1161 smbd_dirptr_8_3_match_fn,
1162 smbd_dirptr_8_3_mode_fn,
1163 conn,
1164 &fname,
1165 &smb_fname,
1166 &mode,
1167 &prev_offset);
1168 if (!ok) {
1169 return false;
1172 *_fname = talloc_move(ctx, &fname);
1173 *_size = smb_fname->st.st_ex_size;
1174 *_mode = mode;
1175 *_date = smb_fname->st.st_ex_mtime;
1176 TALLOC_FREE(smb_fname);
1177 return true;
1180 /*******************************************************************
1181 Check to see if a user can read a file. This is only approximate,
1182 it is used as part of the "hide unreadable" option. Don't
1183 use it for anything security sensitive.
1184 ********************************************************************/
1186 static bool user_can_read_file(connection_struct *conn,
1187 struct smb_filename *smb_fname)
1190 * Never hide files from the root user.
1191 * We use (uid_t)0 here not sec_initial_uid()
1192 * as make test uses a single user context.
1195 if (get_current_uid(conn) == (uid_t)0) {
1196 return True;
1199 return can_access_file_acl(conn, smb_fname, FILE_READ_DATA);
1202 /*******************************************************************
1203 Check to see if a user can write a file (and only files, we do not
1204 check dirs on this one). This is only approximate,
1205 it is used as part of the "hide unwriteable" option. Don't
1206 use it for anything security sensitive.
1207 ********************************************************************/
1209 static bool user_can_write_file(connection_struct *conn,
1210 const struct smb_filename *smb_fname)
1213 * Never hide files from the root user.
1214 * We use (uid_t)0 here not sec_initial_uid()
1215 * as make test uses a single user context.
1218 if (get_current_uid(conn) == (uid_t)0) {
1219 return True;
1222 SMB_ASSERT(VALID_STAT(smb_fname->st));
1224 /* Pseudo-open the file */
1226 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1227 return True;
1230 return can_write_to_file(conn, smb_fname);
1233 /*******************************************************************
1234 Is a file a "special" type ?
1235 ********************************************************************/
1237 static bool file_is_special(connection_struct *conn,
1238 const struct smb_filename *smb_fname)
1241 * Never hide files from the root user.
1242 * We use (uid_t)0 here not sec_initial_uid()
1243 * as make test uses a single user context.
1246 if (get_current_uid(conn) == (uid_t)0) {
1247 return False;
1250 SMB_ASSERT(VALID_STAT(smb_fname->st));
1252 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1253 S_ISDIR(smb_fname->st.st_ex_mode) ||
1254 S_ISLNK(smb_fname->st.st_ex_mode))
1255 return False;
1257 return True;
1260 /*******************************************************************
1261 Should the file be seen by the client?
1262 NOTE: A successful return is no guarantee of the file's existence.
1263 ********************************************************************/
1265 bool is_visible_file(connection_struct *conn, const char *dir_path,
1266 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1268 bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1269 bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1270 bool hide_special = lp_hide_special_files(SNUM(conn));
1271 char *entry = NULL;
1272 struct smb_filename *smb_fname_base = NULL;
1273 NTSTATUS status;
1274 bool ret = false;
1276 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1277 return True; /* . and .. are always visible. */
1280 /* If it's a vetoed file, pretend it doesn't even exist */
1281 if (use_veto && IS_VETO_PATH(conn, name)) {
1282 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1283 return False;
1286 if (hide_unreadable || hide_unwriteable || hide_special) {
1287 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1288 if (!entry) {
1289 ret = false;
1290 goto out;
1293 /* Create an smb_filename with stream_name == NULL. */
1294 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1295 pst, &smb_fname_base);
1296 if (!NT_STATUS_IS_OK(status)) {
1297 ret = false;
1298 goto out;
1301 /* If the file name does not exist, there's no point checking
1302 * the configuration options. We succeed, on the basis that the
1303 * checks *might* have passed if the file was present.
1305 if (!VALID_STAT(*pst)) {
1306 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1307 ret = true;
1308 goto out;
1309 } else {
1310 *pst = smb_fname_base->st;
1314 /* Honour _hide unreadable_ option */
1315 if (hide_unreadable &&
1316 !user_can_read_file(conn, smb_fname_base)) {
1317 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1318 entry ));
1319 ret = false;
1320 goto out;
1322 /* Honour _hide unwriteable_ option */
1323 if (hide_unwriteable && !user_can_write_file(conn,
1324 smb_fname_base)) {
1325 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1326 entry ));
1327 ret = false;
1328 goto out;
1330 /* Honour _hide_special_ option */
1331 if (hide_special && file_is_special(conn, smb_fname_base)) {
1332 DEBUG(10,("is_visible_file: file %s is special.\n",
1333 entry ));
1334 ret = false;
1335 goto out;
1339 ret = true;
1340 out:
1341 TALLOC_FREE(smb_fname_base);
1342 TALLOC_FREE(entry);
1343 return ret;
1346 static int smb_Dir_destructor(struct smb_Dir *dirp)
1348 if (dirp->dir) {
1349 #ifdef HAVE_DIRFD
1350 if (dirp->conn->sconn) {
1351 files_struct *fsp = file_find_fd(dirp->conn->sconn,
1352 dirfd(dirp->dir));
1353 if (fsp) {
1354 /* The call below closes the underlying fd. */
1355 fsp->fh->fd = -1;
1358 #endif
1359 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1361 if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
1362 dirp->conn->sconn->searches.dirhandles_open--;
1364 return 0;
1367 /*******************************************************************
1368 Open a directory.
1369 ********************************************************************/
1371 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1372 const char *name,
1373 const char *mask,
1374 uint32 attr)
1376 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1377 struct smbd_server_connection *sconn = conn->sconn;
1379 if (!dirp) {
1380 return NULL;
1383 dirp->conn = conn;
1384 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1386 dirp->dir_path = talloc_strdup(dirp, name);
1387 if (!dirp->dir_path) {
1388 errno = ENOMEM;
1389 goto fail;
1392 if (sconn && !sconn->using_smb2) {
1393 sconn->searches.dirhandles_open++;
1395 talloc_set_destructor(dirp, smb_Dir_destructor);
1397 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1398 if (!dirp->dir) {
1399 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1400 strerror(errno) ));
1401 goto fail;
1404 return dirp;
1406 fail:
1407 TALLOC_FREE(dirp);
1408 return NULL;
1411 /*******************************************************************
1412 Open a directory from an fsp.
1413 ********************************************************************/
1415 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1416 files_struct *fsp,
1417 const char *mask,
1418 uint32 attr)
1420 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1421 struct smbd_server_connection *sconn = conn->sconn;
1423 if (!dirp) {
1424 return NULL;
1427 dirp->conn = conn;
1428 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1430 dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
1431 if (!dirp->dir_path) {
1432 errno = ENOMEM;
1433 goto fail;
1436 if (sconn && !sconn->using_smb2) {
1437 sconn->searches.dirhandles_open++;
1439 talloc_set_destructor(dirp, smb_Dir_destructor);
1441 if (fsp->is_directory && fsp->fh->fd != -1) {
1442 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1443 if (dirp->dir == NULL) {
1444 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1445 "NULL (%s)\n",
1446 dirp->dir_path,
1447 strerror(errno)));
1448 if (errno != ENOSYS) {
1449 return NULL;
1454 if (dirp->dir == NULL) {
1455 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1456 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1459 if (!dirp->dir) {
1460 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1461 strerror(errno) ));
1462 goto fail;
1465 return dirp;
1467 fail:
1468 TALLOC_FREE(dirp);
1469 return NULL;
1473 /*******************************************************************
1474 Read from a directory.
1475 Return directory entry, current offset, and optional stat information.
1476 Don't check for veto or invisible files.
1477 ********************************************************************/
1479 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1480 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1482 const char *n;
1483 char *talloced = NULL;
1484 connection_struct *conn = dirp->conn;
1486 /* Cheat to allow . and .. to be the first entries returned. */
1487 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1488 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1490 if (dirp->file_number == 0) {
1491 n = ".";
1492 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1493 } else {
1494 n = "..";
1495 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1497 dirp->file_number++;
1498 *ptalloced = NULL;
1499 return n;
1500 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1501 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1502 return NULL;
1503 } else {
1504 /* A real offset, seek to it. */
1505 SeekDir(dirp, *poffset);
1508 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1509 /* Ignore . and .. - we've already returned them. */
1510 if (*n == '.') {
1511 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1512 TALLOC_FREE(talloced);
1513 continue;
1516 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1517 *ptalloced = talloced;
1518 dirp->file_number++;
1519 return n;
1521 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1522 *ptalloced = NULL;
1523 return NULL;
1526 /*******************************************************************
1527 Rewind to the start.
1528 ********************************************************************/
1530 void RewindDir(struct smb_Dir *dirp, long *poffset)
1532 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1533 dirp->file_number = 0;
1534 dirp->offset = START_OF_DIRECTORY_OFFSET;
1535 *poffset = START_OF_DIRECTORY_OFFSET;
1538 /*******************************************************************
1539 Seek a dir.
1540 ********************************************************************/
1542 void SeekDir(struct smb_Dir *dirp, long offset)
1544 if (offset != dirp->offset) {
1545 if (offset == START_OF_DIRECTORY_OFFSET) {
1546 RewindDir(dirp, &offset);
1548 * Ok we should really set the file number here
1549 * to 1 to enable ".." to be returned next. Trouble
1550 * is I'm worried about callers using SeekDir(dirp,0)
1551 * as equivalent to RewindDir(). So leave this alone
1552 * for now.
1554 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1555 RewindDir(dirp, &offset);
1557 * Set the file number to 2 - we want to get the first
1558 * real file entry (the one we return after "..")
1559 * on the next ReadDir.
1561 dirp->file_number = 2;
1562 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1563 ; /* Don't seek in this case. */
1564 } else {
1565 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1567 dirp->offset = offset;
1571 /*******************************************************************
1572 Tell a dir position.
1573 ********************************************************************/
1575 long TellDir(struct smb_Dir *dirp)
1577 return(dirp->offset);
1580 /*******************************************************************
1581 Add an entry into the dcache.
1582 ********************************************************************/
1584 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1586 struct name_cache_entry *e;
1588 if (dirp->name_cache_size == 0) {
1589 return;
1592 if (dirp->name_cache == NULL) {
1593 dirp->name_cache = TALLOC_ZERO_ARRAY(
1594 dirp, struct name_cache_entry, dirp->name_cache_size);
1596 if (dirp->name_cache == NULL) {
1597 return;
1601 dirp->name_cache_index = (dirp->name_cache_index+1) %
1602 dirp->name_cache_size;
1603 e = &dirp->name_cache[dirp->name_cache_index];
1604 TALLOC_FREE(e->name);
1605 e->name = talloc_strdup(dirp, name);
1606 e->offset = offset;
1609 /*******************************************************************
1610 Find an entry by name. Leave us at the offset after it.
1611 Don't check for veto or invisible files.
1612 ********************************************************************/
1614 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1616 int i;
1617 const char *entry = NULL;
1618 char *talloced = NULL;
1619 connection_struct *conn = dirp->conn;
1621 /* Search back in the name cache. */
1622 if (dirp->name_cache_size && dirp->name_cache) {
1623 for (i = dirp->name_cache_index; i >= 0; i--) {
1624 struct name_cache_entry *e = &dirp->name_cache[i];
1625 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1626 *poffset = e->offset;
1627 SeekDir(dirp, e->offset);
1628 return True;
1631 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1632 struct name_cache_entry *e = &dirp->name_cache[i];
1633 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1634 *poffset = e->offset;
1635 SeekDir(dirp, e->offset);
1636 return True;
1641 /* Not found in the name cache. Rewind directory and start from scratch. */
1642 SMB_VFS_REWINDDIR(conn, dirp->dir);
1643 dirp->file_number = 0;
1644 *poffset = START_OF_DIRECTORY_OFFSET;
1645 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
1646 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1647 TALLOC_FREE(talloced);
1648 return True;
1650 TALLOC_FREE(talloced);
1652 return False;
1655 /*****************************************************************
1656 Is this directory empty ?
1657 *****************************************************************/
1659 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1661 NTSTATUS status = NT_STATUS_OK;
1662 long dirpos = 0;
1663 const char *dname = NULL;
1664 char *talloced = NULL;
1665 SMB_STRUCT_STAT st;
1666 struct connection_struct *conn = fsp->conn;
1667 struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
1668 conn,
1669 fsp,
1670 NULL,
1673 if (!dir_hnd) {
1674 return map_nt_error_from_unix(errno);
1677 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
1678 /* Quick check for "." and ".." */
1679 if (dname[0] == '.') {
1680 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1681 TALLOC_FREE(talloced);
1682 continue;
1686 if (!is_visible_file(conn, fsp->fsp_name->base_name, dname, &st, True)) {
1687 TALLOC_FREE(talloced);
1688 continue;
1691 DEBUG(10,("can_delete_directory_fsp: got name %s - can't delete\n",
1692 dname ));
1693 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1694 break;
1696 TALLOC_FREE(talloced);
1697 TALLOC_FREE(dir_hnd);
1699 return status;