VERSION: Re-enable git snapshots.
[Samba.git] / source3 / smbd / dir.c
blobc39c62462abcb7ab6e8c02ea1f6563a6ec22cffa
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"
26 #include "lib/util/bitmap.h"
27 #include "memcache.h"
30 This module implements directory related functions for Samba.
33 /* "Special" directory offsets. */
34 #define END_OF_DIRECTORY_OFFSET ((long)-1)
35 #define START_OF_DIRECTORY_OFFSET ((long)0)
36 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
38 /* "Special" directory offsets in 32-bit wire format. */
39 #define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
40 #define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
41 #define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
43 /* Make directory handle internals available. */
45 struct name_cache_entry {
46 char *name;
47 long offset;
50 struct smb_Dir {
51 connection_struct *conn;
52 DIR *dir;
53 long offset;
54 char *dir_path;
55 size_t name_cache_size;
56 struct name_cache_entry *name_cache;
57 unsigned int name_cache_index;
58 unsigned int file_number;
59 files_struct *fsp; /* Back pointer to containing fsp, only
60 set from OpenDir_fsp(). */
63 struct dptr_struct {
64 struct dptr_struct *next, *prev;
65 int dnum;
66 uint16 spid;
67 struct connection_struct *conn;
68 struct smb_Dir *dir_hnd;
69 bool expect_close;
70 char *wcard;
71 uint32 attr;
72 char *path;
73 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
74 bool did_stat; /* Optimisation for non-wcard searches. */
75 bool priv; /* Directory handle opened with privilege. */
76 uint32_t counter;
77 struct memcache *dptr_cache;
80 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
81 files_struct *fsp,
82 const char *mask,
83 uint32 attr);
85 #define INVALID_DPTR_KEY (-3)
87 /****************************************************************************
88 Make a dir struct.
89 ****************************************************************************/
91 bool make_dir_struct(TALLOC_CTX *ctx,
92 char *buf,
93 const char *mask,
94 const char *fname,
95 off_t size,
96 uint32 mode,
97 time_t date,
98 bool uc)
100 char *p;
101 char *mask2 = talloc_strdup(ctx, mask);
103 if (!mask2) {
104 return False;
107 if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
108 size = 0;
111 memset(buf+1,' ',11);
112 if ((p = strchr_m(mask2,'.')) != NULL) {
113 *p = 0;
114 push_ascii(buf+1,mask2,8, 0);
115 push_ascii(buf+9,p+1,3, 0);
116 *p = '.';
117 } else {
118 push_ascii(buf+1,mask2,11, 0);
121 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
122 SCVAL(buf,21,mode);
123 srv_put_dos_date(buf,22,date);
124 SSVAL(buf,26,size & 0xFFFF);
125 SSVAL(buf,28,(size >> 16)&0xFFFF);
126 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
127 Strange, but verified on W2K3. Needed for OS/2. JRA. */
128 push_ascii(buf+30,fname,12, uc ? STR_UPPER : 0);
129 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
130 return True;
133 /****************************************************************************
134 Initialise the dir bitmap.
135 ****************************************************************************/
137 bool init_dptrs(struct smbd_server_connection *sconn)
139 if (sconn->searches.dptr_bmap) {
140 return true;
143 sconn->searches.dptr_bmap = bitmap_talloc(
144 sconn, MAX_DIRECTORY_HANDLES);
146 if (sconn->searches.dptr_bmap == NULL) {
147 return false;
150 return true;
153 /****************************************************************************
154 Idle a dptr - the directory is closed but the control info is kept.
155 ****************************************************************************/
157 static void dptr_idle(struct dptr_struct *dptr)
159 if (dptr->dir_hnd) {
160 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
161 TALLOC_FREE(dptr->dir_hnd);
162 TALLOC_FREE(dptr->dptr_cache);
163 dptr->counter = 0;
167 /****************************************************************************
168 Idle the oldest dptr.
169 ****************************************************************************/
171 static void dptr_idleoldest(struct smbd_server_connection *sconn)
173 struct dptr_struct *dptr;
176 * Go to the end of the list.
178 dptr = DLIST_TAIL(sconn->searches.dirptrs);
180 if(!dptr) {
181 DEBUG(0,("No dptrs available to idle ?\n"));
182 return;
186 * Idle the oldest pointer.
189 for(; dptr; dptr = DLIST_PREV(dptr)) {
190 if (dptr->dir_hnd) {
191 dptr_idle(dptr);
192 return;
197 /****************************************************************************
198 Get the struct dptr_struct for a dir index.
199 ****************************************************************************/
201 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
202 int key, bool forclose)
204 struct dptr_struct *dptr;
206 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
207 if(dptr->dnum == key) {
208 if (!forclose && !dptr->dir_hnd) {
209 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
210 dptr_idleoldest(sconn);
211 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
212 if (!(dptr->dir_hnd = OpenDir(
213 NULL, dptr->conn, dptr->path,
214 dptr->wcard, dptr->attr))) {
215 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
216 strerror(errno)));
217 return NULL;
220 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
221 return dptr;
224 return(NULL);
227 /****************************************************************************
228 Get the dir path for a dir index.
229 ****************************************************************************/
231 const char *dptr_path(struct smbd_server_connection *sconn, int key)
233 struct dptr_struct *dptr = dptr_get(sconn, key, false);
234 if (dptr)
235 return(dptr->path);
236 return(NULL);
239 /****************************************************************************
240 Get the dir wcard for a dir index.
241 ****************************************************************************/
243 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
245 struct dptr_struct *dptr = dptr_get(sconn, key, false);
246 if (dptr)
247 return(dptr->wcard);
248 return(NULL);
251 /****************************************************************************
252 Get the dir attrib for a dir index.
253 ****************************************************************************/
255 uint16 dptr_attr(struct smbd_server_connection *sconn, int key)
257 struct dptr_struct *dptr = dptr_get(sconn, key, false);
258 if (dptr)
259 return(dptr->attr);
260 return(0);
263 /****************************************************************************
264 Close a dptr (internal func).
265 ****************************************************************************/
267 static void dptr_close_internal(struct dptr_struct *dptr)
269 struct smbd_server_connection *sconn = dptr->conn->sconn;
271 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
273 if (sconn == NULL) {
274 goto done;
277 if (sconn->using_smb2) {
278 goto done;
281 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
284 * Free the dnum in the bitmap. Remember the dnum value is always
285 * biased by one with respect to the bitmap.
288 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
289 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
290 dptr->dnum ));
293 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
295 done:
296 TALLOC_FREE(dptr->dir_hnd);
297 TALLOC_FREE(dptr);
300 /****************************************************************************
301 Close a dptr given a key.
302 ****************************************************************************/
304 void dptr_close(struct smbd_server_connection *sconn, int *key)
306 struct dptr_struct *dptr;
308 if(*key == INVALID_DPTR_KEY)
309 return;
311 /* OS/2 seems to use -1 to indicate "close all directories" */
312 if (*key == -1) {
313 struct dptr_struct *next;
314 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
315 next = dptr->next;
316 dptr_close_internal(dptr);
318 *key = INVALID_DPTR_KEY;
319 return;
322 dptr = dptr_get(sconn, *key, true);
324 if (!dptr) {
325 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
326 return;
329 dptr_close_internal(dptr);
331 *key = INVALID_DPTR_KEY;
334 /****************************************************************************
335 Close all dptrs for a cnum.
336 ****************************************************************************/
338 void dptr_closecnum(connection_struct *conn)
340 struct dptr_struct *dptr, *next;
341 struct smbd_server_connection *sconn = conn->sconn;
343 if (sconn == NULL) {
344 return;
347 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
348 next = dptr->next;
349 if (dptr->conn == conn) {
350 dptr_close_internal(dptr);
355 /****************************************************************************
356 Idle all dptrs for a cnum.
357 ****************************************************************************/
359 void dptr_idlecnum(connection_struct *conn)
361 struct dptr_struct *dptr;
362 struct smbd_server_connection *sconn = conn->sconn;
364 if (sconn == NULL) {
365 return;
368 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
369 if (dptr->conn == conn && dptr->dir_hnd) {
370 dptr_idle(dptr);
375 /****************************************************************************
376 Close a dptr that matches a given path, only if it matches the spid also.
377 ****************************************************************************/
379 void dptr_closepath(struct smbd_server_connection *sconn,
380 char *path,uint16 spid)
382 struct dptr_struct *dptr, *next;
383 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
384 next = dptr->next;
385 if (spid == dptr->spid && strequal(dptr->path,path))
386 dptr_close_internal(dptr);
390 /****************************************************************************
391 Try and close the oldest handle not marked for
392 expect close in the hope that the client has
393 finished with that one.
394 ****************************************************************************/
396 static void dptr_close_oldest(struct smbd_server_connection *sconn,
397 bool old)
399 struct dptr_struct *dptr;
402 * Go to the end of the list.
404 for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
407 if(!dptr) {
408 DEBUG(0,("No old dptrs available to close oldest ?\n"));
409 return;
413 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
414 * does not have expect_close set. If 'old' is false, close
415 * one of the new dnum handles.
418 for(; dptr; dptr = DLIST_PREV(dptr)) {
419 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
420 (!old && (dptr->dnum > 255))) {
421 dptr_close_internal(dptr);
422 return;
427 /****************************************************************************
428 Safely do an OpenDir as root, ensuring we're in the right place.
429 ****************************************************************************/
431 static struct smb_Dir *open_dir_with_privilege(connection_struct *conn,
432 struct smb_request *req,
433 const char *path,
434 const char *wcard,
435 uint32_t attr)
437 NTSTATUS status;
438 struct smb_Dir *dir_hnd = NULL;
439 struct smb_filename *smb_fname_cwd = NULL;
440 char *saved_dir = vfs_GetWd(talloc_tos(), conn);
441 struct privilege_paths *priv_paths = req->priv_paths;
442 int ret;
444 if (saved_dir == NULL) {
445 return NULL;
448 if (vfs_ChDir(conn, path) == -1) {
449 return NULL;
452 /* Now check the stat value is the same. */
453 status = create_synthetic_smb_fname(talloc_tos(), ".",
454 NULL, NULL,
455 &smb_fname_cwd);
457 if (!NT_STATUS_IS_OK(status)) {
458 goto out;
460 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
461 if (ret != 0) {
462 goto out;
465 if (!check_same_stat(&smb_fname_cwd->st, &priv_paths->parent_name.st)) {
466 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
467 "and %s\n",
468 path,
469 smb_fname_str_dbg(&priv_paths->parent_name)));
470 goto out;
473 dir_hnd = OpenDir(NULL, conn, ".", wcard, attr);
475 out:
477 vfs_ChDir(conn, saved_dir);
478 return dir_hnd;
481 /****************************************************************************
482 Create a new dir ptr. If the flag old_handle is true then we must allocate
483 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
484 one byte long. If old_handle is false we allocate from the range
485 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
486 a directory handle is never zero.
487 wcard must not be zero.
488 ****************************************************************************/
490 NTSTATUS dptr_create(connection_struct *conn,
491 struct smb_request *req,
492 files_struct *fsp,
493 const char *path, bool old_handle, bool expect_close,uint16 spid,
494 const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
496 struct smbd_server_connection *sconn = conn->sconn;
497 struct dptr_struct *dptr = NULL;
498 struct smb_Dir *dir_hnd;
500 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
501 path = fsp->fsp_name->base_name;
504 DEBUG(5,("dptr_create dir=%s\n", path));
506 if (sconn == NULL) {
507 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
508 return NT_STATUS_INTERNAL_ERROR;
511 if (!wcard) {
512 return NT_STATUS_INVALID_PARAMETER;
515 if (fsp) {
516 if (!(fsp->access_mask & SEC_DIR_LIST)) {
517 DEBUG(5,("dptr_create: directory %s "
518 "not open for LIST access\n",
519 path));
520 return NT_STATUS_ACCESS_DENIED;
522 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
523 } else {
524 int ret;
525 struct smb_filename *smb_dname = NULL;
526 NTSTATUS status = create_synthetic_smb_fname(talloc_tos(),
527 path,
528 NULL,
529 NULL,
530 &smb_dname);
531 if (!NT_STATUS_IS_OK(status)) {
532 return status;
534 if (lp_posix_pathnames()) {
535 ret = SMB_VFS_LSTAT(conn, smb_dname);
536 } else {
537 ret = SMB_VFS_STAT(conn, smb_dname);
539 if (ret == -1) {
540 return map_nt_error_from_unix(errno);
542 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
543 return NT_STATUS_NOT_A_DIRECTORY;
545 status = smbd_check_access_rights(conn,
546 smb_dname,
547 SEC_DIR_LIST);
548 if (!NT_STATUS_IS_OK(status)) {
549 return status;
551 if (req && req->priv_paths) {
552 dir_hnd = open_dir_with_privilege(conn,
553 req,
554 path,
555 wcard,
556 attr);
557 } else {
558 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
562 if (!dir_hnd) {
563 return map_nt_error_from_unix(errno);
566 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
567 dptr_idleoldest(sconn);
570 dptr = talloc(NULL, struct dptr_struct);
571 if(!dptr) {
572 DEBUG(0,("talloc fail in dptr_create.\n"));
573 TALLOC_FREE(dir_hnd);
574 return NT_STATUS_NO_MEMORY;
577 ZERO_STRUCTP(dptr);
579 dptr->path = talloc_strdup(dptr, path);
580 if (!dptr->path) {
581 TALLOC_FREE(dptr);
582 TALLOC_FREE(dir_hnd);
583 return NT_STATUS_NO_MEMORY;
585 dptr->conn = conn;
586 dptr->dir_hnd = dir_hnd;
587 dptr->spid = spid;
588 dptr->expect_close = expect_close;
589 dptr->wcard = talloc_strdup(dptr, wcard);
590 if (!dptr->wcard) {
591 TALLOC_FREE(dptr);
592 TALLOC_FREE(dir_hnd);
593 return NT_STATUS_NO_MEMORY;
595 if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
596 dptr->has_wild = True;
597 } else {
598 dptr->has_wild = wcard_has_wild;
601 dptr->attr = attr;
603 if (sconn->using_smb2) {
604 goto done;
607 if(old_handle) {
610 * This is an old-style SMBsearch request. Ensure the
611 * value we return will fit in the range 1-255.
614 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
616 if(dptr->dnum == -1 || dptr->dnum > 254) {
619 * Try and close the oldest handle not marked for
620 * expect close in the hope that the client has
621 * finished with that one.
624 dptr_close_oldest(sconn, true);
626 /* Now try again... */
627 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
628 if(dptr->dnum == -1 || dptr->dnum > 254) {
629 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
630 TALLOC_FREE(dptr);
631 TALLOC_FREE(dir_hnd);
632 return NT_STATUS_TOO_MANY_OPENED_FILES;
635 } else {
638 * This is a new-style trans2 request. Allocate from
639 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
642 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
644 if(dptr->dnum == -1 || dptr->dnum < 255) {
647 * Try and close the oldest handle close in the hope that
648 * the client has finished with that one. This will only
649 * happen in the case of the Win98 client bug where it leaks
650 * directory handles.
653 dptr_close_oldest(sconn, false);
655 /* Now try again... */
656 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
658 if(dptr->dnum == -1 || dptr->dnum < 255) {
659 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
660 TALLOC_FREE(dptr);
661 TALLOC_FREE(dir_hnd);
662 return NT_STATUS_TOO_MANY_OPENED_FILES;
667 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
669 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
671 DLIST_ADD(sconn->searches.dirptrs, dptr);
673 done:
674 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
675 dptr->dnum,path,expect_close));
677 *dptr_ret = dptr;
679 return NT_STATUS_OK;
683 /****************************************************************************
684 Wrapper functions to access the lower level directory handles.
685 ****************************************************************************/
687 void dptr_CloseDir(files_struct *fsp)
689 if (fsp->dptr) {
691 * The destructor for the struct smb_Dir
692 * (fsp->dptr->dir_hnd) now handles
693 * all resource deallocation.
695 dptr_close_internal(fsp->dptr);
696 fsp->dptr = NULL;
700 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
702 SeekDir(dptr->dir_hnd, offset);
705 long dptr_TellDir(struct dptr_struct *dptr)
707 return TellDir(dptr->dir_hnd);
710 bool dptr_has_wild(struct dptr_struct *dptr)
712 return dptr->has_wild;
715 int dptr_dnum(struct dptr_struct *dptr)
717 return dptr->dnum;
720 bool dptr_get_priv(struct dptr_struct *dptr)
722 return dptr->priv;
725 void dptr_set_priv(struct dptr_struct *dptr)
727 dptr->priv = true;
730 /****************************************************************************
731 Return the next visible file name, skipping veto'd and invisible files.
732 ****************************************************************************/
734 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
735 long *poffset, SMB_STRUCT_STAT *pst,
736 char **ptalloced)
738 /* Normal search for the next file. */
739 const char *name;
740 char *talloced = NULL;
742 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
743 != NULL) {
744 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
745 *ptalloced = talloced;
746 return name;
748 TALLOC_FREE(talloced);
750 return NULL;
753 /****************************************************************************
754 Return the next visible file name, skipping veto'd and invisible files.
755 ****************************************************************************/
757 char *dptr_ReadDirName(TALLOC_CTX *ctx,
758 struct dptr_struct *dptr,
759 long *poffset,
760 SMB_STRUCT_STAT *pst)
762 struct smb_filename smb_fname_base;
763 char *name = NULL;
764 const char *name_temp = NULL;
765 char *talloced = NULL;
766 char *pathreal = NULL;
767 char *found_name = NULL;
768 int ret;
770 SET_STAT_INVALID(*pst);
772 if (dptr->has_wild || dptr->did_stat) {
773 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
774 &talloced);
775 if (name_temp == NULL) {
776 return NULL;
778 if (talloced != NULL) {
779 return talloc_move(ctx, &talloced);
781 return talloc_strdup(ctx, name_temp);
784 /* If poffset is -1 then we know we returned this name before and we
785 * have no wildcards. We're at the end of the directory. */
786 if (*poffset == END_OF_DIRECTORY_OFFSET) {
787 return NULL;
790 /* We know the stored wcard contains no wildcard characters.
791 * See if we can match with a stat call. If we can't, then set
792 * did_stat to true to ensure we only do this once and keep
793 * searching. */
795 dptr->did_stat = true;
797 /* First check if it should be visible. */
798 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
799 pst, true))
801 /* This only returns false if the file was found, but
802 is explicitly not visible. Set us to end of
803 directory, but return NULL as we know we can't ever
804 find it. */
805 goto ret;
808 if (VALID_STAT(*pst)) {
809 name = talloc_strdup(ctx, dptr->wcard);
810 goto ret;
813 pathreal = talloc_asprintf(ctx,
814 "%s/%s",
815 dptr->path,
816 dptr->wcard);
817 if (!pathreal)
818 return NULL;
820 /* Create an smb_filename with stream_name == NULL. */
821 ZERO_STRUCT(smb_fname_base);
822 smb_fname_base.base_name = pathreal;
824 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
825 *pst = smb_fname_base.st;
826 name = talloc_strdup(ctx, dptr->wcard);
827 goto clean;
828 } else {
829 /* If we get any other error than ENOENT or ENOTDIR
830 then the file exists we just can't stat it. */
831 if (errno != ENOENT && errno != ENOTDIR) {
832 name = talloc_strdup(ctx, dptr->wcard);
833 goto clean;
837 /* Stat failed. We know this is authoratiative if we are
838 * providing case sensitive semantics or the underlying
839 * filesystem is case sensitive.
841 if (dptr->conn->case_sensitive ||
842 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
844 goto clean;
848 * Try case-insensitive stat if the fs has the ability. This avoids
849 * scanning the whole directory.
851 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
852 ctx, &found_name);
853 if (ret == 0) {
854 name = found_name;
855 goto clean;
856 } else if (errno == ENOENT) {
857 /* The case-insensitive lookup was authoritative. */
858 goto clean;
861 TALLOC_FREE(pathreal);
863 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
864 if (name_temp == NULL) {
865 return NULL;
867 if (talloced != NULL) {
868 return talloc_move(ctx, &talloced);
870 return talloc_strdup(ctx, name_temp);
872 clean:
873 TALLOC_FREE(pathreal);
874 ret:
875 /* We need to set the underlying dir_hnd offset to -1
876 * also as this function is usually called with the
877 * output from TellDir. */
878 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
879 return name;
882 /****************************************************************************
883 Search for a file by name, skipping veto'ed and not visible files.
884 ****************************************************************************/
886 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
888 SET_STAT_INVALID(*pst);
890 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
891 /* This is a singleton directory and we're already at the end. */
892 *poffset = END_OF_DIRECTORY_OFFSET;
893 return False;
896 return SearchDir(dptr->dir_hnd, name, poffset);
899 /****************************************************************************
900 Add the name we're returning into the underlying cache.
901 ****************************************************************************/
903 void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
905 DirCacheAdd(dptr->dir_hnd, name, offset);
908 /****************************************************************************
909 Initialize variables & state data at the beginning of all search SMB requests.
910 ****************************************************************************/
911 void dptr_init_search_op(struct dptr_struct *dptr)
913 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
916 /****************************************************************************
917 Map a native directory offset to a 32-bit cookie.
918 ****************************************************************************/
920 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
922 DATA_BLOB key;
923 DATA_BLOB val;
925 if (offset == END_OF_DIRECTORY_OFFSET) {
926 return WIRE_END_OF_DIRECTORY_OFFSET;
927 } else if(offset == START_OF_DIRECTORY_OFFSET) {
928 return WIRE_START_OF_DIRECTORY_OFFSET;
929 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
930 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
932 if (sizeof(long) == 4) {
933 /* 32-bit machine. We can cheat... */
934 return (uint32_t)offset;
936 if (dptr->dptr_cache == NULL) {
937 /* Lazy initialize cache. */
938 dptr->dptr_cache = memcache_init(dptr, 0);
939 if (dptr->dptr_cache == NULL) {
940 return WIRE_END_OF_DIRECTORY_OFFSET;
942 } else {
943 /* Have we seen this offset before ? */
944 key.data = (void *)&offset;
945 key.length = sizeof(offset);
946 if (memcache_lookup(dptr->dptr_cache,
947 SMB1_SEARCH_OFFSET_MAP,
948 key,
949 &val)) {
950 uint32_t wire_offset;
951 SMB_ASSERT(val.length == sizeof(wire_offset));
952 memcpy(&wire_offset, val.data, sizeof(wire_offset));
953 DEBUG(10,("found wire %u <-> offset %ld\n",
954 (unsigned int)wire_offset,
955 (long)offset));
956 return wire_offset;
959 /* Allocate a new wire cookie. */
960 do {
961 dptr->counter++;
962 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
963 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
964 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
965 /* Store it in the cache. */
966 key.data = (void *)&offset;
967 key.length = sizeof(offset);
968 val.data = (void *)&dptr->counter;
969 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
970 memcache_add(dptr->dptr_cache,
971 SMB1_SEARCH_OFFSET_MAP,
972 key,
973 val);
974 /* And the reverse mapping for lookup from
975 map_wire_to_dir_offset(). */
976 memcache_add(dptr->dptr_cache,
977 SMB1_SEARCH_OFFSET_MAP,
978 val,
979 key);
980 DEBUG(10,("stored wire %u <-> offset %ld\n",
981 (unsigned int)dptr->counter,
982 (long)offset));
983 return dptr->counter;
986 /****************************************************************************
987 Fill the 5 byte server reserved dptr field.
988 ****************************************************************************/
990 bool dptr_fill(struct smbd_server_connection *sconn,
991 char *buf1,unsigned int key)
993 unsigned char *buf = (unsigned char *)buf1;
994 struct dptr_struct *dptr = dptr_get(sconn, key, false);
995 uint32_t wire_offset;
996 if (!dptr) {
997 DEBUG(1,("filling null dirptr %d\n",key));
998 return(False);
1000 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
1001 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
1002 (long)dptr->dir_hnd,(int)wire_offset));
1003 buf[0] = key;
1004 SIVAL(buf,1,wire_offset);
1005 return(True);
1008 /****************************************************************************
1009 Map a 32-bit wire cookie to a native directory offset.
1010 ****************************************************************************/
1012 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
1014 DATA_BLOB key;
1015 DATA_BLOB val;
1017 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
1018 return END_OF_DIRECTORY_OFFSET;
1019 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
1020 return START_OF_DIRECTORY_OFFSET;
1021 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
1022 return DOT_DOT_DIRECTORY_OFFSET;
1024 if (sizeof(long) == 4) {
1025 /* 32-bit machine. We can cheat... */
1026 return (long)wire_offset;
1028 if (dptr->dptr_cache == NULL) {
1029 /* Logic error, cache should be initialized. */
1030 return END_OF_DIRECTORY_OFFSET;
1032 key.data = (void *)&wire_offset;
1033 key.length = sizeof(wire_offset);
1034 if (memcache_lookup(dptr->dptr_cache,
1035 SMB1_SEARCH_OFFSET_MAP,
1036 key,
1037 &val)) {
1038 /* Found mapping. */
1039 long offset;
1040 SMB_ASSERT(val.length == sizeof(offset));
1041 memcpy(&offset, val.data, sizeof(offset));
1042 DEBUG(10,("lookup wire %u <-> offset %ld\n",
1043 (unsigned int)wire_offset,
1044 (long)offset));
1045 return offset;
1047 return END_OF_DIRECTORY_OFFSET;
1050 /****************************************************************************
1051 Fetch the dir ptr and seek it given the 5 byte server field.
1052 ****************************************************************************/
1054 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
1055 char *buf, int *num)
1057 unsigned int key = *(unsigned char *)buf;
1058 struct dptr_struct *dptr = dptr_get(sconn, key, false);
1059 uint32_t wire_offset;
1060 long seekoff;
1062 if (!dptr) {
1063 DEBUG(3,("fetched null dirptr %d\n",key));
1064 return(NULL);
1066 *num = key;
1067 wire_offset = IVAL(buf,1);
1068 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
1069 SeekDir(dptr->dir_hnd,seekoff);
1070 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1071 key, dptr->path, (int)seekoff));
1072 return(dptr);
1075 /****************************************************************************
1076 Fetch the dir ptr.
1077 ****************************************************************************/
1079 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
1080 int dptr_num)
1082 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
1084 if (!dptr) {
1085 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
1086 return(NULL);
1088 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
1089 return(dptr);
1092 /****************************************************************************
1093 Check that a file matches a particular file type.
1094 ****************************************************************************/
1096 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
1098 uint32 mask;
1100 /* Check the "may have" search bits. */
1101 if (((mode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)) != 0)
1102 return False;
1104 /* Check the "must have" bits, which are the may have bits shifted eight */
1105 /* If must have bit is set, the file/dir can not be returned in search unless the matching
1106 file attribute is set */
1107 mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
1108 if(mask) {
1109 if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))) == mask) /* check if matching attribute present */
1110 return True;
1111 else
1112 return False;
1115 return True;
1118 static bool mangle_mask_match(connection_struct *conn,
1119 const char *filename,
1120 const char *mask)
1122 char mname[13];
1124 if (!name_to_8_3(filename,mname,False,conn->params)) {
1125 return False;
1127 return mask_match_search(mname,mask,False);
1130 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
1131 struct dptr_struct *dirptr,
1132 const char *mask,
1133 uint32_t dirtype,
1134 bool dont_descend,
1135 bool ask_sharemode,
1136 bool (*match_fn)(TALLOC_CTX *ctx,
1137 void *private_data,
1138 const char *dname,
1139 const char *mask,
1140 char **_fname),
1141 bool (*mode_fn)(TALLOC_CTX *ctx,
1142 void *private_data,
1143 struct smb_filename *smb_fname,
1144 uint32_t *_mode),
1145 void *private_data,
1146 char **_fname,
1147 struct smb_filename **_smb_fname,
1148 uint32_t *_mode,
1149 long *_prev_offset)
1151 connection_struct *conn = dirptr->conn;
1152 size_t slashlen;
1153 size_t pathlen;
1155 *_smb_fname = NULL;
1156 *_mode = 0;
1158 pathlen = strlen(dirptr->path);
1159 slashlen = ( dirptr->path[pathlen-1] != '/') ? 1 : 0;
1161 while (true) {
1162 long cur_offset;
1163 long prev_offset;
1164 SMB_STRUCT_STAT sbuf;
1165 char *dname = NULL;
1166 bool isdots;
1167 char *fname = NULL;
1168 char *pathreal = NULL;
1169 struct smb_filename smb_fname;
1170 uint32_t mode = 0;
1171 bool ok;
1172 NTSTATUS status;
1174 cur_offset = dptr_TellDir(dirptr);
1175 prev_offset = cur_offset;
1176 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
1178 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1179 (long)dirptr, cur_offset));
1181 if (dname == NULL) {
1182 return false;
1185 isdots = (ISDOT(dname) || ISDOTDOT(dname));
1186 if (dont_descend && !isdots) {
1187 TALLOC_FREE(dname);
1188 continue;
1192 * fname may get mangled, dname is never mangled.
1193 * Whenever we're accessing the filesystem we use
1194 * pathreal which is composed from dname.
1197 ok = match_fn(ctx, private_data, dname, mask, &fname);
1198 if (!ok) {
1199 TALLOC_FREE(dname);
1200 continue;
1204 * This used to be
1205 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1206 * needslash?"/":"", dname);
1207 * but this was measurably slower than doing the memcpy.
1210 pathreal = talloc_array(
1211 ctx, char,
1212 pathlen + slashlen + talloc_get_size(dname));
1213 if (!pathreal) {
1214 TALLOC_FREE(dname);
1215 TALLOC_FREE(fname);
1216 return false;
1219 memcpy(pathreal, dirptr->path, pathlen);
1220 pathreal[pathlen] = '/';
1221 memcpy(pathreal + slashlen + pathlen, dname,
1222 talloc_get_size(dname));
1224 /* Create smb_fname with NULL stream_name. */
1225 ZERO_STRUCT(smb_fname);
1226 smb_fname.base_name = pathreal;
1227 smb_fname.st = sbuf;
1229 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
1230 if (!ok) {
1231 TALLOC_FREE(dname);
1232 TALLOC_FREE(fname);
1233 TALLOC_FREE(pathreal);
1234 continue;
1237 if (!dir_check_ftype(conn, mode, dirtype)) {
1238 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1239 fname, (unsigned int)mode, (unsigned int)dirtype));
1240 TALLOC_FREE(dname);
1241 TALLOC_FREE(fname);
1242 TALLOC_FREE(pathreal);
1243 continue;
1246 if (ask_sharemode) {
1247 struct timespec write_time_ts;
1248 struct file_id fileid;
1250 fileid = vfs_file_id_from_sbuf(conn,
1251 &smb_fname.st);
1252 get_file_infos(fileid, 0, NULL, &write_time_ts);
1253 if (!null_timespec(write_time_ts)) {
1254 update_stat_ex_mtime(&smb_fname.st,
1255 write_time_ts);
1259 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1260 "fname=%s (%s)\n",
1261 mask, smb_fname_str_dbg(&smb_fname),
1262 dname, fname));
1264 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1266 TALLOC_FREE(dname);
1268 status = copy_smb_filename(ctx, &smb_fname, _smb_fname);
1269 TALLOC_FREE(pathreal);
1270 if (!NT_STATUS_IS_OK(status)) {
1271 return false;
1273 *_fname = fname;
1274 *_mode = mode;
1275 *_prev_offset = prev_offset;
1277 return true;
1280 return false;
1283 /****************************************************************************
1284 Get an 8.3 directory entry.
1285 ****************************************************************************/
1287 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1288 void *private_data,
1289 const char *dname,
1290 const char *mask,
1291 char **_fname)
1293 connection_struct *conn = (connection_struct *)private_data;
1295 if ((strcmp(mask,"*.*") == 0) ||
1296 mask_match_search(dname, mask, false) ||
1297 mangle_mask_match(conn, dname, mask)) {
1298 char mname[13];
1299 const char *fname;
1301 if (!mangle_is_8_3(dname, false, conn->params)) {
1302 bool ok = name_to_8_3(dname, mname, false,
1303 conn->params);
1304 if (!ok) {
1305 return false;
1307 fname = mname;
1308 } else {
1309 fname = dname;
1312 *_fname = talloc_strdup(ctx, fname);
1313 if (*_fname == NULL) {
1314 return false;
1317 return true;
1320 return false;
1323 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1324 void *private_data,
1325 struct smb_filename *smb_fname,
1326 uint32_t *_mode)
1328 connection_struct *conn = (connection_struct *)private_data;
1330 if (!VALID_STAT(smb_fname->st)) {
1331 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1332 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1333 "Couldn't stat [%s]. Error "
1334 "= %s\n",
1335 smb_fname_str_dbg(smb_fname),
1336 strerror(errno)));
1337 return false;
1341 *_mode = dos_mode(conn, smb_fname);
1342 return true;
1345 bool get_dir_entry(TALLOC_CTX *ctx,
1346 struct dptr_struct *dirptr,
1347 const char *mask,
1348 uint32_t dirtype,
1349 char **_fname,
1350 off_t *_size,
1351 uint32_t *_mode,
1352 struct timespec *_date,
1353 bool check_descend,
1354 bool ask_sharemode)
1356 connection_struct *conn = dirptr->conn;
1357 char *fname = NULL;
1358 struct smb_filename *smb_fname = NULL;
1359 uint32_t mode = 0;
1360 long prev_offset;
1361 bool ok;
1363 ok = smbd_dirptr_get_entry(ctx,
1364 dirptr,
1365 mask,
1366 dirtype,
1367 check_descend,
1368 ask_sharemode,
1369 smbd_dirptr_8_3_match_fn,
1370 smbd_dirptr_8_3_mode_fn,
1371 conn,
1372 &fname,
1373 &smb_fname,
1374 &mode,
1375 &prev_offset);
1376 if (!ok) {
1377 return false;
1380 *_fname = talloc_move(ctx, &fname);
1381 *_size = smb_fname->st.st_ex_size;
1382 *_mode = mode;
1383 *_date = smb_fname->st.st_ex_mtime;
1384 TALLOC_FREE(smb_fname);
1385 return true;
1388 /*******************************************************************
1389 Check to see if a user can read a file. This is only approximate,
1390 it is used as part of the "hide unreadable" option. Don't
1391 use it for anything security sensitive.
1392 ********************************************************************/
1394 static bool user_can_read_file(connection_struct *conn,
1395 struct smb_filename *smb_fname)
1398 * Never hide files from the root user.
1399 * We use (uid_t)0 here not sec_initial_uid()
1400 * as make test uses a single user context.
1403 if (get_current_uid(conn) == (uid_t)0) {
1404 return True;
1407 return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
1408 smb_fname,
1409 FILE_READ_DATA));
1412 /*******************************************************************
1413 Check to see if a user can write a file (and only files, we do not
1414 check dirs on this one). This is only approximate,
1415 it is used as part of the "hide unwriteable" option. Don't
1416 use it for anything security sensitive.
1417 ********************************************************************/
1419 static bool user_can_write_file(connection_struct *conn,
1420 const struct smb_filename *smb_fname)
1423 * Never hide files from the root user.
1424 * We use (uid_t)0 here not sec_initial_uid()
1425 * as make test uses a single user context.
1428 if (get_current_uid(conn) == (uid_t)0) {
1429 return True;
1432 SMB_ASSERT(VALID_STAT(smb_fname->st));
1434 /* Pseudo-open the file */
1436 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1437 return True;
1440 return can_write_to_file(conn, smb_fname);
1443 /*******************************************************************
1444 Is a file a "special" type ?
1445 ********************************************************************/
1447 static bool file_is_special(connection_struct *conn,
1448 const struct smb_filename *smb_fname)
1451 * Never hide files from the root user.
1452 * We use (uid_t)0 here not sec_initial_uid()
1453 * as make test uses a single user context.
1456 if (get_current_uid(conn) == (uid_t)0) {
1457 return False;
1460 SMB_ASSERT(VALID_STAT(smb_fname->st));
1462 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1463 S_ISDIR(smb_fname->st.st_ex_mode) ||
1464 S_ISLNK(smb_fname->st.st_ex_mode))
1465 return False;
1467 return True;
1470 /*******************************************************************
1471 Should the file be seen by the client?
1472 NOTE: A successful return is no guarantee of the file's existence.
1473 ********************************************************************/
1475 bool is_visible_file(connection_struct *conn, const char *dir_path,
1476 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1478 bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1479 bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1480 bool hide_special = lp_hide_special_files(SNUM(conn));
1481 char *entry = NULL;
1482 struct smb_filename *smb_fname_base = NULL;
1483 NTSTATUS status;
1484 bool ret = false;
1486 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1487 return True; /* . and .. are always visible. */
1490 /* If it's a vetoed file, pretend it doesn't even exist */
1491 if (use_veto && IS_VETO_PATH(conn, name)) {
1492 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1493 return False;
1496 if (hide_unreadable || hide_unwriteable || hide_special) {
1497 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1498 if (!entry) {
1499 ret = false;
1500 goto out;
1503 /* Create an smb_filename with stream_name == NULL. */
1504 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1505 pst, &smb_fname_base);
1506 if (!NT_STATUS_IS_OK(status)) {
1507 ret = false;
1508 goto out;
1511 /* If the file name does not exist, there's no point checking
1512 * the configuration options. We succeed, on the basis that the
1513 * checks *might* have passed if the file was present.
1515 if (!VALID_STAT(*pst)) {
1516 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1517 ret = true;
1518 goto out;
1519 } else {
1520 *pst = smb_fname_base->st;
1524 /* Honour _hide unreadable_ option */
1525 if (hide_unreadable &&
1526 !user_can_read_file(conn, smb_fname_base)) {
1527 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1528 entry ));
1529 ret = false;
1530 goto out;
1532 /* Honour _hide unwriteable_ option */
1533 if (hide_unwriteable && !user_can_write_file(conn,
1534 smb_fname_base)) {
1535 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1536 entry ));
1537 ret = false;
1538 goto out;
1540 /* Honour _hide_special_ option */
1541 if (hide_special && file_is_special(conn, smb_fname_base)) {
1542 DEBUG(10,("is_visible_file: file %s is special.\n",
1543 entry ));
1544 ret = false;
1545 goto out;
1549 ret = true;
1550 out:
1551 TALLOC_FREE(smb_fname_base);
1552 TALLOC_FREE(entry);
1553 return ret;
1556 static int smb_Dir_destructor(struct smb_Dir *dirp)
1558 if (dirp->dir != NULL) {
1559 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1560 if (dirp->fsp != NULL) {
1562 * The SMB_VFS_CLOSEDIR above
1563 * closes the underlying fd inside
1564 * dirp->fsp.
1566 dirp->fsp->fh->fd = -1;
1567 if (dirp->fsp->dptr != NULL) {
1568 SMB_ASSERT(dirp->fsp->dptr->dir_hnd == dirp);
1569 dirp->fsp->dptr->dir_hnd = NULL;
1571 dirp->fsp = NULL;
1574 if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
1575 dirp->conn->sconn->searches.dirhandles_open--;
1577 return 0;
1580 /*******************************************************************
1581 Open a directory.
1582 ********************************************************************/
1584 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1585 const char *name,
1586 const char *mask,
1587 uint32 attr)
1589 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1590 struct smbd_server_connection *sconn = conn->sconn;
1592 if (!dirp) {
1593 return NULL;
1596 dirp->conn = conn;
1597 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1599 dirp->dir_path = talloc_strdup(dirp, name);
1600 if (!dirp->dir_path) {
1601 errno = ENOMEM;
1602 goto fail;
1605 if (sconn && !sconn->using_smb2) {
1606 sconn->searches.dirhandles_open++;
1608 talloc_set_destructor(dirp, smb_Dir_destructor);
1610 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1611 if (!dirp->dir) {
1612 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1613 strerror(errno) ));
1614 goto fail;
1617 return dirp;
1619 fail:
1620 TALLOC_FREE(dirp);
1621 return NULL;
1624 /*******************************************************************
1625 Open a directory from an fsp.
1626 ********************************************************************/
1628 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1629 files_struct *fsp,
1630 const char *mask,
1631 uint32 attr)
1633 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1634 struct smbd_server_connection *sconn = conn->sconn;
1636 if (!dirp) {
1637 return NULL;
1640 dirp->conn = conn;
1641 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1643 dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
1644 if (!dirp->dir_path) {
1645 errno = ENOMEM;
1646 goto fail;
1649 if (sconn && !sconn->using_smb2) {
1650 sconn->searches.dirhandles_open++;
1652 talloc_set_destructor(dirp, smb_Dir_destructor);
1654 if (fsp->is_directory && fsp->fh->fd != -1) {
1655 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1656 if (dirp->dir != NULL) {
1657 dirp->fsp = fsp;
1658 } else {
1659 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1660 "NULL (%s)\n",
1661 dirp->dir_path,
1662 strerror(errno)));
1663 if (errno != ENOSYS) {
1664 return NULL;
1669 if (dirp->dir == NULL) {
1670 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1671 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1674 if (!dirp->dir) {
1675 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1676 strerror(errno) ));
1677 goto fail;
1680 return dirp;
1682 fail:
1683 TALLOC_FREE(dirp);
1684 return NULL;
1688 /*******************************************************************
1689 Read from a directory.
1690 Return directory entry, current offset, and optional stat information.
1691 Don't check for veto or invisible files.
1692 ********************************************************************/
1694 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1695 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1697 const char *n;
1698 char *talloced = NULL;
1699 connection_struct *conn = dirp->conn;
1701 /* Cheat to allow . and .. to be the first entries returned. */
1702 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1703 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1705 if (dirp->file_number == 0) {
1706 n = ".";
1707 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1708 } else {
1709 n = "..";
1710 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1712 dirp->file_number++;
1713 *ptalloced = NULL;
1714 return n;
1715 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1716 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1717 return NULL;
1718 } else {
1719 /* A real offset, seek to it. */
1720 SeekDir(dirp, *poffset);
1723 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1724 /* Ignore . and .. - we've already returned them. */
1725 if (*n == '.') {
1726 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1727 TALLOC_FREE(talloced);
1728 continue;
1731 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1732 *ptalloced = talloced;
1733 dirp->file_number++;
1734 return n;
1736 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1737 *ptalloced = NULL;
1738 return NULL;
1741 /*******************************************************************
1742 Rewind to the start.
1743 ********************************************************************/
1745 void RewindDir(struct smb_Dir *dirp, long *poffset)
1747 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1748 dirp->file_number = 0;
1749 dirp->offset = START_OF_DIRECTORY_OFFSET;
1750 *poffset = START_OF_DIRECTORY_OFFSET;
1753 /*******************************************************************
1754 Seek a dir.
1755 ********************************************************************/
1757 void SeekDir(struct smb_Dir *dirp, long offset)
1759 if (offset != dirp->offset) {
1760 if (offset == START_OF_DIRECTORY_OFFSET) {
1761 RewindDir(dirp, &offset);
1763 * Ok we should really set the file number here
1764 * to 1 to enable ".." to be returned next. Trouble
1765 * is I'm worried about callers using SeekDir(dirp,0)
1766 * as equivalent to RewindDir(). So leave this alone
1767 * for now.
1769 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1770 RewindDir(dirp, &offset);
1772 * Set the file number to 2 - we want to get the first
1773 * real file entry (the one we return after "..")
1774 * on the next ReadDir.
1776 dirp->file_number = 2;
1777 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1778 ; /* Don't seek in this case. */
1779 } else {
1780 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1782 dirp->offset = offset;
1786 /*******************************************************************
1787 Tell a dir position.
1788 ********************************************************************/
1790 long TellDir(struct smb_Dir *dirp)
1792 return(dirp->offset);
1795 /*******************************************************************
1796 Add an entry into the dcache.
1797 ********************************************************************/
1799 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1801 struct name_cache_entry *e;
1803 if (dirp->name_cache_size == 0) {
1804 return;
1807 if (dirp->name_cache == NULL) {
1808 dirp->name_cache = talloc_zero_array(
1809 dirp, struct name_cache_entry, dirp->name_cache_size);
1811 if (dirp->name_cache == NULL) {
1812 return;
1816 dirp->name_cache_index = (dirp->name_cache_index+1) %
1817 dirp->name_cache_size;
1818 e = &dirp->name_cache[dirp->name_cache_index];
1819 TALLOC_FREE(e->name);
1820 e->name = talloc_strdup(dirp, name);
1821 e->offset = offset;
1824 /*******************************************************************
1825 Find an entry by name. Leave us at the offset after it.
1826 Don't check for veto or invisible files.
1827 ********************************************************************/
1829 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1831 int i;
1832 const char *entry = NULL;
1833 char *talloced = NULL;
1834 connection_struct *conn = dirp->conn;
1836 /* Search back in the name cache. */
1837 if (dirp->name_cache_size && dirp->name_cache) {
1838 for (i = dirp->name_cache_index; i >= 0; i--) {
1839 struct name_cache_entry *e = &dirp->name_cache[i];
1840 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1841 *poffset = e->offset;
1842 SeekDir(dirp, e->offset);
1843 return True;
1846 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1847 struct name_cache_entry *e = &dirp->name_cache[i];
1848 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1849 *poffset = e->offset;
1850 SeekDir(dirp, e->offset);
1851 return True;
1856 /* Not found in the name cache. Rewind directory and start from scratch. */
1857 SMB_VFS_REWINDDIR(conn, dirp->dir);
1858 dirp->file_number = 0;
1859 *poffset = START_OF_DIRECTORY_OFFSET;
1860 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
1861 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1862 TALLOC_FREE(talloced);
1863 return True;
1865 TALLOC_FREE(talloced);
1867 return False;
1870 /*****************************************************************
1871 Is this directory empty ?
1872 *****************************************************************/
1874 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1876 NTSTATUS status = NT_STATUS_OK;
1877 long dirpos = 0;
1878 const char *dname = NULL;
1879 const char *dirname = fsp->fsp_name->base_name;
1880 char *talloced = NULL;
1881 SMB_STRUCT_STAT st;
1882 struct connection_struct *conn = fsp->conn;
1883 struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
1884 conn,
1885 fsp,
1886 NULL,
1889 if (!dir_hnd) {
1890 return map_nt_error_from_unix(errno);
1893 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
1894 /* Quick check for "." and ".." */
1895 if (dname[0] == '.') {
1896 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1897 TALLOC_FREE(talloced);
1898 continue;
1902 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1903 TALLOC_FREE(talloced);
1904 continue;
1907 DEBUG(10,("got name %s - can't delete\n",
1908 dname ));
1909 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1910 break;
1912 TALLOC_FREE(talloced);
1913 TALLOC_FREE(dir_hnd);
1915 return status;