2 Unix SMB/CIFS implementation.
3 Directory handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "libcli/security/security.h"
26 #include "lib/util/bitmap.h"
27 #include "../lib/util/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 {
51 connection_struct *conn;
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(). */
64 struct dptr_struct *next, *prev;
67 struct connection_struct *conn;
68 struct smb_Dir *dir_hnd;
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. */
77 struct memcache *dptr_cache;
80 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
85 static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset);
87 #define INVALID_DPTR_KEY (-3)
89 /****************************************************************************
90 Initialise the dir bitmap.
91 ****************************************************************************/
93 bool init_dptrs(struct smbd_server_connection *sconn)
95 if (sconn->searches.dptr_bmap) {
99 sconn->searches.dptr_bmap = bitmap_talloc(
100 sconn, MAX_DIRECTORY_HANDLES);
102 if (sconn->searches.dptr_bmap == NULL) {
109 /****************************************************************************
110 Idle a dptr - the directory is closed but the control info is kept.
111 ****************************************************************************/
113 static void dptr_idle(struct dptr_struct *dptr)
116 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
117 TALLOC_FREE(dptr->dir_hnd);
118 TALLOC_FREE(dptr->dptr_cache);
123 /****************************************************************************
124 Idle the oldest dptr.
125 ****************************************************************************/
127 static void dptr_idleoldest(struct smbd_server_connection *sconn)
129 struct dptr_struct *dptr;
132 * Go to the end of the list.
134 dptr = DLIST_TAIL(sconn->searches.dirptrs);
137 DEBUG(0,("No dptrs available to idle ?\n"));
142 * Idle the oldest pointer.
145 for(; dptr; dptr = DLIST_PREV(dptr)) {
153 /****************************************************************************
154 Get the struct dptr_struct for a dir index.
155 ****************************************************************************/
157 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
158 int key, bool forclose)
160 struct dptr_struct *dptr;
162 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
163 if(dptr->dnum == key) {
164 if (!forclose && !dptr->dir_hnd) {
165 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
166 dptr_idleoldest(sconn);
167 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
168 if (!(dptr->dir_hnd = OpenDir(
169 NULL, dptr->conn, dptr->path,
170 dptr->wcard, dptr->attr))) {
171 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
176 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
183 /****************************************************************************
184 Get the dir path for a dir index.
185 ****************************************************************************/
187 const char *dptr_path(struct smbd_server_connection *sconn, int key)
189 struct dptr_struct *dptr = dptr_get(sconn, key, false);
195 /****************************************************************************
196 Get the dir wcard for a dir index.
197 ****************************************************************************/
199 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
201 struct dptr_struct *dptr = dptr_get(sconn, key, false);
207 /****************************************************************************
208 Get the dir attrib for a dir index.
209 ****************************************************************************/
211 uint16 dptr_attr(struct smbd_server_connection *sconn, int key)
213 struct dptr_struct *dptr = dptr_get(sconn, key, false);
219 /****************************************************************************
220 Close a dptr (internal func).
221 ****************************************************************************/
223 static void dptr_close_internal(struct dptr_struct *dptr)
225 struct smbd_server_connection *sconn = dptr->conn->sconn;
227 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
233 if (sconn->using_smb2) {
237 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
240 * Free the dnum in the bitmap. Remember the dnum value is always
241 * biased by one with respect to the bitmap.
244 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
245 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
249 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
252 TALLOC_FREE(dptr->dir_hnd);
256 /****************************************************************************
257 Close a dptr given a key.
258 ****************************************************************************/
260 void dptr_close(struct smbd_server_connection *sconn, int *key)
262 struct dptr_struct *dptr;
264 if(*key == INVALID_DPTR_KEY)
267 /* OS/2 seems to use -1 to indicate "close all directories" */
269 struct dptr_struct *next;
270 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
272 dptr_close_internal(dptr);
274 *key = INVALID_DPTR_KEY;
278 dptr = dptr_get(sconn, *key, true);
281 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
285 dptr_close_internal(dptr);
287 *key = INVALID_DPTR_KEY;
290 /****************************************************************************
291 Close all dptrs for a cnum.
292 ****************************************************************************/
294 void dptr_closecnum(connection_struct *conn)
296 struct dptr_struct *dptr, *next;
297 struct smbd_server_connection *sconn = conn->sconn;
303 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
305 if (dptr->conn == conn) {
306 dptr_close_internal(dptr);
311 /****************************************************************************
312 Idle all dptrs for a cnum.
313 ****************************************************************************/
315 void dptr_idlecnum(connection_struct *conn)
317 struct dptr_struct *dptr;
318 struct smbd_server_connection *sconn = conn->sconn;
324 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
325 if (dptr->conn == conn && dptr->dir_hnd) {
331 /****************************************************************************
332 Close a dptr that matches a given path, only if it matches the spid also.
333 ****************************************************************************/
335 void dptr_closepath(struct smbd_server_connection *sconn,
336 char *path,uint16 spid)
338 struct dptr_struct *dptr, *next;
339 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
341 if (spid == dptr->spid && strequal(dptr->path,path))
342 dptr_close_internal(dptr);
346 /****************************************************************************
347 Try and close the oldest handle not marked for
348 expect close in the hope that the client has
349 finished with that one.
350 ****************************************************************************/
352 static void dptr_close_oldest(struct smbd_server_connection *sconn,
355 struct dptr_struct *dptr;
358 * Go to the end of the list.
360 for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
364 DEBUG(0,("No old dptrs available to close oldest ?\n"));
369 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
370 * does not have expect_close set. If 'old' is false, close
371 * one of the new dnum handles.
374 for(; dptr; dptr = DLIST_PREV(dptr)) {
375 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
376 (!old && (dptr->dnum > 255))) {
377 dptr_close_internal(dptr);
383 /****************************************************************************
384 Safely do an OpenDir as root, ensuring we're in the right place.
385 ****************************************************************************/
387 static struct smb_Dir *open_dir_with_privilege(connection_struct *conn,
388 struct smb_request *req,
393 struct smb_Dir *dir_hnd = NULL;
394 struct smb_filename *smb_fname_cwd;
395 char *saved_dir = vfs_GetWd(talloc_tos(), conn);
396 struct privilege_paths *priv_paths = req->priv_paths;
399 if (saved_dir == NULL) {
403 if (vfs_ChDir(conn, path) == -1) {
407 /* Now check the stat value is the same. */
408 smb_fname_cwd = synthetic_smb_fname(talloc_tos(), ".", NULL, NULL);
410 if (smb_fname_cwd == NULL) {
413 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
418 if (!check_same_stat(&smb_fname_cwd->st, &priv_paths->parent_name.st)) {
419 DEBUG(0,("open_dir_with_privilege: stat mismatch between %s "
422 smb_fname_str_dbg(&priv_paths->parent_name)));
426 dir_hnd = OpenDir(NULL, conn, ".", wcard, attr);
430 vfs_ChDir(conn, saved_dir);
434 /****************************************************************************
435 Create a new dir ptr. If the flag old_handle is true then we must allocate
436 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
437 one byte long. If old_handle is false we allocate from the range
438 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
439 a directory handle is never zero.
440 wcard must not be zero.
441 ****************************************************************************/
443 NTSTATUS dptr_create(connection_struct *conn,
444 struct smb_request *req,
446 const char *path, bool old_handle, bool expect_close,uint16 spid,
447 const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
449 struct smbd_server_connection *sconn = conn->sconn;
450 struct dptr_struct *dptr = NULL;
451 struct smb_Dir *dir_hnd;
453 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
454 path = fsp->fsp_name->base_name;
457 DEBUG(5,("dptr_create dir=%s\n", path));
460 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
461 return NT_STATUS_INTERNAL_ERROR;
465 return NT_STATUS_INVALID_PARAMETER;
469 if (!(fsp->access_mask & SEC_DIR_LIST)) {
470 DEBUG(5,("dptr_create: directory %s "
471 "not open for LIST access\n",
473 return NT_STATUS_ACCESS_DENIED;
475 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
478 bool backup_intent = (req && req->priv_paths);
479 struct smb_filename *smb_dname;
482 smb_dname = synthetic_smb_fname(talloc_tos(), path,
484 if (smb_dname == NULL) {
485 return NT_STATUS_NO_MEMORY;
487 if (lp_posix_pathnames()) {
488 ret = SMB_VFS_LSTAT(conn, smb_dname);
490 ret = SMB_VFS_STAT(conn, smb_dname);
493 return map_nt_error_from_unix(errno);
495 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
496 return NT_STATUS_NOT_A_DIRECTORY;
498 status = smbd_check_access_rights(conn,
502 if (!NT_STATUS_IS_OK(status)) {
506 dir_hnd = open_dir_with_privilege(conn,
512 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
517 return map_nt_error_from_unix(errno);
520 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
521 dptr_idleoldest(sconn);
524 dptr = talloc(NULL, struct dptr_struct);
526 DEBUG(0,("talloc fail in dptr_create.\n"));
527 TALLOC_FREE(dir_hnd);
528 return NT_STATUS_NO_MEMORY;
533 dptr->path = talloc_strdup(dptr, path);
536 TALLOC_FREE(dir_hnd);
537 return NT_STATUS_NO_MEMORY;
540 dptr->dir_hnd = dir_hnd;
542 dptr->expect_close = expect_close;
543 dptr->wcard = talloc_strdup(dptr, wcard);
546 TALLOC_FREE(dir_hnd);
547 return NT_STATUS_NO_MEMORY;
549 if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
550 dptr->has_wild = True;
552 dptr->has_wild = wcard_has_wild;
557 if (sconn->using_smb2) {
564 * This is an old-style SMBsearch request. Ensure the
565 * value we return will fit in the range 1-255.
568 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
570 if(dptr->dnum == -1 || dptr->dnum > 254) {
573 * Try and close the oldest handle not marked for
574 * expect close in the hope that the client has
575 * finished with that one.
578 dptr_close_oldest(sconn, true);
580 /* Now try again... */
581 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
582 if(dptr->dnum == -1 || dptr->dnum > 254) {
583 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
585 TALLOC_FREE(dir_hnd);
586 return NT_STATUS_TOO_MANY_OPENED_FILES;
592 * This is a new-style trans2 request. Allocate from
593 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
596 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
598 if(dptr->dnum == -1 || dptr->dnum < 255) {
601 * Try and close the oldest handle close in the hope that
602 * the client has finished with that one. This will only
603 * happen in the case of the Win98 client bug where it leaks
607 dptr_close_oldest(sconn, false);
609 /* Now try again... */
610 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
612 if(dptr->dnum == -1 || dptr->dnum < 255) {
613 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
615 TALLOC_FREE(dir_hnd);
616 return NT_STATUS_TOO_MANY_OPENED_FILES;
621 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
623 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
625 DLIST_ADD(sconn->searches.dirptrs, dptr);
628 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
629 dptr->dnum,path,expect_close));
637 /****************************************************************************
638 Wrapper functions to access the lower level directory handles.
639 ****************************************************************************/
641 void dptr_CloseDir(files_struct *fsp)
645 * The destructor for the struct smb_Dir
646 * (fsp->dptr->dir_hnd) now handles
647 * all resource deallocation.
649 dptr_close_internal(fsp->dptr);
654 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
656 SeekDir(dptr->dir_hnd, offset);
659 long dptr_TellDir(struct dptr_struct *dptr)
661 return TellDir(dptr->dir_hnd);
664 bool dptr_has_wild(struct dptr_struct *dptr)
666 return dptr->has_wild;
669 int dptr_dnum(struct dptr_struct *dptr)
674 bool dptr_get_priv(struct dptr_struct *dptr)
679 void dptr_set_priv(struct dptr_struct *dptr)
684 /****************************************************************************
685 Return the next visible file name, skipping veto'd and invisible files.
686 ****************************************************************************/
688 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
689 long *poffset, SMB_STRUCT_STAT *pst,
692 /* Normal search for the next file. */
694 char *talloced = NULL;
696 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
698 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
699 *ptalloced = talloced;
702 TALLOC_FREE(talloced);
707 /****************************************************************************
708 Return the next visible file name, skipping veto'd and invisible files.
709 ****************************************************************************/
711 static char *dptr_ReadDirName(TALLOC_CTX *ctx,
712 struct dptr_struct *dptr,
714 SMB_STRUCT_STAT *pst)
716 struct smb_filename smb_fname_base;
718 const char *name_temp = NULL;
719 char *talloced = NULL;
720 char *pathreal = NULL;
721 char *found_name = NULL;
724 SET_STAT_INVALID(*pst);
726 if (dptr->has_wild || dptr->did_stat) {
727 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
729 if (name_temp == NULL) {
732 if (talloced != NULL) {
733 return talloc_move(ctx, &talloced);
735 return talloc_strdup(ctx, name_temp);
738 /* If poffset is -1 then we know we returned this name before and we
739 * have no wildcards. We're at the end of the directory. */
740 if (*poffset == END_OF_DIRECTORY_OFFSET) {
744 /* We know the stored wcard contains no wildcard characters.
745 * See if we can match with a stat call. If we can't, then set
746 * did_stat to true to ensure we only do this once and keep
749 dptr->did_stat = true;
751 /* First check if it should be visible. */
752 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
755 /* This only returns false if the file was found, but
756 is explicitly not visible. Set us to end of
757 directory, but return NULL as we know we can't ever
762 if (VALID_STAT(*pst)) {
763 name = talloc_strdup(ctx, dptr->wcard);
767 pathreal = talloc_asprintf(ctx,
774 /* Create an smb_filename with stream_name == NULL. */
775 ZERO_STRUCT(smb_fname_base);
776 smb_fname_base.base_name = pathreal;
778 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
779 *pst = smb_fname_base.st;
780 name = talloc_strdup(ctx, dptr->wcard);
783 /* If we get any other error than ENOENT or ENOTDIR
784 then the file exists we just can't stat it. */
785 if (errno != ENOENT && errno != ENOTDIR) {
786 name = talloc_strdup(ctx, dptr->wcard);
791 /* Stat failed. We know this is authoratiative if we are
792 * providing case sensitive semantics or the underlying
793 * filesystem is case sensitive.
795 if (dptr->conn->case_sensitive ||
796 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
802 * Try case-insensitive stat if the fs has the ability. This avoids
803 * scanning the whole directory.
805 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
810 } else if (errno == ENOENT) {
811 /* The case-insensitive lookup was authoritative. */
815 TALLOC_FREE(pathreal);
817 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
818 if (name_temp == NULL) {
821 if (talloced != NULL) {
822 return talloc_move(ctx, &talloced);
824 return talloc_strdup(ctx, name_temp);
827 TALLOC_FREE(pathreal);
829 /* We need to set the underlying dir_hnd offset to -1
830 * also as this function is usually called with the
831 * output from TellDir. */
832 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
836 /****************************************************************************
837 Search for a file by name, skipping veto'ed and not visible files.
838 ****************************************************************************/
840 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
842 SET_STAT_INVALID(*pst);
844 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
845 /* This is a singleton directory and we're already at the end. */
846 *poffset = END_OF_DIRECTORY_OFFSET;
850 return SearchDir(dptr->dir_hnd, name, poffset);
853 /****************************************************************************
854 Initialize variables & state data at the beginning of all search SMB requests.
855 ****************************************************************************/
856 void dptr_init_search_op(struct dptr_struct *dptr)
858 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
861 /****************************************************************************
862 Map a native directory offset to a 32-bit cookie.
863 ****************************************************************************/
865 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
870 if (offset == END_OF_DIRECTORY_OFFSET) {
871 return WIRE_END_OF_DIRECTORY_OFFSET;
872 } else if(offset == START_OF_DIRECTORY_OFFSET) {
873 return WIRE_START_OF_DIRECTORY_OFFSET;
874 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
875 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
877 if (sizeof(long) == 4) {
878 /* 32-bit machine. We can cheat... */
879 return (uint32_t)offset;
881 if (dptr->dptr_cache == NULL) {
882 /* Lazy initialize cache. */
883 dptr->dptr_cache = memcache_init(dptr, 0);
884 if (dptr->dptr_cache == NULL) {
885 return WIRE_END_OF_DIRECTORY_OFFSET;
888 /* Have we seen this offset before ? */
889 key.data = (void *)&offset;
890 key.length = sizeof(offset);
891 if (memcache_lookup(dptr->dptr_cache,
892 SMB1_SEARCH_OFFSET_MAP,
895 uint32_t wire_offset;
896 SMB_ASSERT(val.length == sizeof(wire_offset));
897 memcpy(&wire_offset, val.data, sizeof(wire_offset));
898 DEBUG(10,("found wire %u <-> offset %ld\n",
899 (unsigned int)wire_offset,
904 /* Allocate a new wire cookie. */
907 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
908 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
909 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
910 /* Store it in the cache. */
911 key.data = (void *)&offset;
912 key.length = sizeof(offset);
913 val.data = (void *)&dptr->counter;
914 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
915 memcache_add(dptr->dptr_cache,
916 SMB1_SEARCH_OFFSET_MAP,
919 /* And the reverse mapping for lookup from
920 map_wire_to_dir_offset(). */
921 memcache_add(dptr->dptr_cache,
922 SMB1_SEARCH_OFFSET_MAP,
925 DEBUG(10,("stored wire %u <-> offset %ld\n",
926 (unsigned int)dptr->counter,
928 return dptr->counter;
931 /****************************************************************************
932 Fill the 5 byte server reserved dptr field.
933 ****************************************************************************/
935 bool dptr_fill(struct smbd_server_connection *sconn,
936 char *buf1,unsigned int key)
938 unsigned char *buf = (unsigned char *)buf1;
939 struct dptr_struct *dptr = dptr_get(sconn, key, false);
940 uint32_t wire_offset;
942 DEBUG(1,("filling null dirptr %d\n",key));
945 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
946 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
947 (long)dptr->dir_hnd,(int)wire_offset));
949 SIVAL(buf,1,wire_offset);
953 /****************************************************************************
954 Map a 32-bit wire cookie to a native directory offset.
955 ****************************************************************************/
957 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
962 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
963 return END_OF_DIRECTORY_OFFSET;
964 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
965 return START_OF_DIRECTORY_OFFSET;
966 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
967 return DOT_DOT_DIRECTORY_OFFSET;
969 if (sizeof(long) == 4) {
970 /* 32-bit machine. We can cheat... */
971 return (long)wire_offset;
973 if (dptr->dptr_cache == NULL) {
974 /* Logic error, cache should be initialized. */
975 return END_OF_DIRECTORY_OFFSET;
977 key.data = (void *)&wire_offset;
978 key.length = sizeof(wire_offset);
979 if (memcache_lookup(dptr->dptr_cache,
980 SMB1_SEARCH_OFFSET_MAP,
985 SMB_ASSERT(val.length == sizeof(offset));
986 memcpy(&offset, val.data, sizeof(offset));
987 DEBUG(10,("lookup wire %u <-> offset %ld\n",
988 (unsigned int)wire_offset,
992 return END_OF_DIRECTORY_OFFSET;
995 /****************************************************************************
996 Fetch the dir ptr and seek it given the 5 byte server field.
997 ****************************************************************************/
999 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
1000 char *buf, int *num)
1002 unsigned int key = *(unsigned char *)buf;
1003 struct dptr_struct *dptr = dptr_get(sconn, key, false);
1004 uint32_t wire_offset;
1008 DEBUG(3,("fetched null dirptr %d\n",key));
1012 wire_offset = IVAL(buf,1);
1013 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
1014 SeekDir(dptr->dir_hnd,seekoff);
1015 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
1016 key, dptr->path, (int)seekoff));
1020 /****************************************************************************
1022 ****************************************************************************/
1024 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
1027 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
1030 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
1033 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
1037 static bool mangle_mask_match(connection_struct *conn,
1038 const char *filename,
1043 if (!name_to_8_3(filename,mname,False,conn->params)) {
1046 return mask_match_search(mname,mask,False);
1049 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
1050 struct dptr_struct *dirptr,
1055 bool (*match_fn)(TALLOC_CTX *ctx,
1060 bool (*mode_fn)(TALLOC_CTX *ctx,
1062 struct smb_filename *smb_fname,
1066 struct smb_filename **_smb_fname,
1070 connection_struct *conn = dirptr->conn;
1077 pathlen = strlen(dirptr->path);
1078 slashlen = ( dirptr->path[pathlen-1] != '/') ? 1 : 0;
1083 SMB_STRUCT_STAT sbuf = { 0 };
1087 char *pathreal = NULL;
1088 struct smb_filename smb_fname;
1092 cur_offset = dptr_TellDir(dirptr);
1093 prev_offset = cur_offset;
1094 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
1096 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
1097 (long)dirptr, cur_offset));
1099 if (dname == NULL) {
1103 isdots = (ISDOT(dname) || ISDOTDOT(dname));
1104 if (dont_descend && !isdots) {
1110 * fname may get mangled, dname is never mangled.
1111 * Whenever we're accessing the filesystem we use
1112 * pathreal which is composed from dname.
1115 ok = match_fn(ctx, private_data, dname, mask, &fname);
1123 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
1124 * needslash?"/":"", dname);
1125 * but this was measurably slower than doing the memcpy.
1128 pathreal = talloc_array(
1130 pathlen + slashlen + talloc_get_size(dname));
1137 memcpy(pathreal, dirptr->path, pathlen);
1138 pathreal[pathlen] = '/';
1139 memcpy(pathreal + slashlen + pathlen, dname,
1140 talloc_get_size(dname));
1142 /* Create smb_fname with NULL stream_name. */
1143 ZERO_STRUCT(smb_fname);
1144 smb_fname.base_name = pathreal;
1145 smb_fname.st = sbuf;
1147 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
1151 TALLOC_FREE(pathreal);
1155 if (!dir_check_ftype(mode, dirtype)) {
1156 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1157 fname, (unsigned int)mode, (unsigned int)dirtype));
1160 TALLOC_FREE(pathreal);
1164 if (ask_sharemode) {
1165 struct timespec write_time_ts;
1166 struct file_id fileid;
1168 fileid = vfs_file_id_from_sbuf(conn,
1170 get_file_infos(fileid, 0, NULL, &write_time_ts);
1171 if (!null_timespec(write_time_ts)) {
1172 update_stat_ex_mtime(&smb_fname.st,
1177 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1179 mask, smb_fname_str_dbg(&smb_fname),
1182 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1186 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
1187 TALLOC_FREE(pathreal);
1188 if (*_smb_fname == NULL) {
1193 *_prev_offset = prev_offset;
1201 /****************************************************************************
1202 Get an 8.3 directory entry.
1203 ****************************************************************************/
1205 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1211 connection_struct *conn = (connection_struct *)private_data;
1213 if ((strcmp(mask,"*.*") == 0) ||
1214 mask_match_search(dname, mask, false) ||
1215 mangle_mask_match(conn, dname, mask)) {
1219 * Ensure we can push the original name as UCS2. If
1220 * not, then just don't return this name.
1224 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
1225 uint8_t *tmp = talloc_array(talloc_tos(),
1229 status = srvstr_push(NULL,
1230 FLAGS2_UNICODE_STRINGS,
1239 if (!NT_STATUS_IS_OK(status)) {
1243 if (!mangle_is_8_3(dname, false, conn->params)) {
1244 bool ok = name_to_8_3(dname, mname, false,
1254 *_fname = talloc_strdup(ctx, fname);
1255 if (*_fname == NULL) {
1265 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1267 struct smb_filename *smb_fname,
1270 connection_struct *conn = (connection_struct *)private_data;
1272 if (!VALID_STAT(smb_fname->st)) {
1273 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1274 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1275 "Couldn't stat [%s]. Error "
1277 smb_fname_str_dbg(smb_fname),
1283 *_mode = dos_mode(conn, smb_fname);
1287 bool get_dir_entry(TALLOC_CTX *ctx,
1288 struct dptr_struct *dirptr,
1294 struct timespec *_date,
1298 connection_struct *conn = dirptr->conn;
1300 struct smb_filename *smb_fname = NULL;
1305 ok = smbd_dirptr_get_entry(ctx,
1311 smbd_dirptr_8_3_match_fn,
1312 smbd_dirptr_8_3_mode_fn,
1322 *_fname = talloc_move(ctx, &fname);
1323 *_size = smb_fname->st.st_ex_size;
1325 *_date = smb_fname->st.st_ex_mtime;
1326 TALLOC_FREE(smb_fname);
1330 /*******************************************************************
1331 Check to see if a user can read a file. This is only approximate,
1332 it is used as part of the "hide unreadable" option. Don't
1333 use it for anything security sensitive.
1334 ********************************************************************/
1336 static bool user_can_read_file(connection_struct *conn,
1337 struct smb_filename *smb_fname)
1340 * Never hide files from the root user.
1341 * We use (uid_t)0 here not sec_initial_uid()
1342 * as make test uses a single user context.
1345 if (get_current_uid(conn) == (uid_t)0) {
1349 return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
1355 /*******************************************************************
1356 Check to see if a user can write a file (and only files, we do not
1357 check dirs on this one). This is only approximate,
1358 it is used as part of the "hide unwriteable" option. Don't
1359 use it for anything security sensitive.
1360 ********************************************************************/
1362 static bool user_can_write_file(connection_struct *conn,
1363 const struct smb_filename *smb_fname)
1366 * Never hide files from the root user.
1367 * We use (uid_t)0 here not sec_initial_uid()
1368 * as make test uses a single user context.
1371 if (get_current_uid(conn) == (uid_t)0) {
1375 SMB_ASSERT(VALID_STAT(smb_fname->st));
1377 /* Pseudo-open the file */
1379 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1383 return can_write_to_file(conn, smb_fname);
1386 /*******************************************************************
1387 Is a file a "special" type ?
1388 ********************************************************************/
1390 static bool file_is_special(connection_struct *conn,
1391 const struct smb_filename *smb_fname)
1394 * Never hide files from the root user.
1395 * We use (uid_t)0 here not sec_initial_uid()
1396 * as make test uses a single user context.
1399 if (get_current_uid(conn) == (uid_t)0) {
1403 SMB_ASSERT(VALID_STAT(smb_fname->st));
1405 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1406 S_ISDIR(smb_fname->st.st_ex_mode) ||
1407 S_ISLNK(smb_fname->st.st_ex_mode))
1413 /*******************************************************************
1414 Should the file be seen by the client?
1415 NOTE: A successful return is no guarantee of the file's existence.
1416 ********************************************************************/
1418 bool is_visible_file(connection_struct *conn, const char *dir_path,
1419 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1421 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1422 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1423 bool hide_special = lp_hide_special_files(SNUM(conn));
1425 struct smb_filename *smb_fname_base = NULL;
1428 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1429 return True; /* . and .. are always visible. */
1432 /* If it's a vetoed file, pretend it doesn't even exist */
1433 if (use_veto && IS_VETO_PATH(conn, name)) {
1434 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1438 if (hide_unreadable || hide_unwriteable || hide_special) {
1439 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1445 /* Create an smb_filename with stream_name == NULL. */
1446 smb_fname_base = synthetic_smb_fname(talloc_tos(), entry, NULL,
1448 if (smb_fname_base == NULL) {
1453 /* If the file name does not exist, there's no point checking
1454 * the configuration options. We succeed, on the basis that the
1455 * checks *might* have passed if the file was present.
1457 if (!VALID_STAT(*pst)) {
1458 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1462 *pst = smb_fname_base->st;
1466 /* Honour _hide unreadable_ option */
1467 if (hide_unreadable &&
1468 !user_can_read_file(conn, smb_fname_base)) {
1469 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1474 /* Honour _hide unwriteable_ option */
1475 if (hide_unwriteable && !user_can_write_file(conn,
1477 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1482 /* Honour _hide_special_ option */
1483 if (hide_special && file_is_special(conn, smb_fname_base)) {
1484 DEBUG(10,("is_visible_file: file %s is special.\n",
1493 TALLOC_FREE(smb_fname_base);
1498 static int smb_Dir_destructor(struct smb_Dir *dirp)
1500 if (dirp->dir != NULL) {
1501 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1502 if (dirp->fsp != NULL) {
1504 * The SMB_VFS_CLOSEDIR above
1505 * closes the underlying fd inside
1508 dirp->fsp->fh->fd = -1;
1509 if (dirp->fsp->dptr != NULL) {
1510 SMB_ASSERT(dirp->fsp->dptr->dir_hnd == dirp);
1511 dirp->fsp->dptr->dir_hnd = NULL;
1516 if (dirp->conn->sconn && !dirp->conn->sconn->using_smb2) {
1517 dirp->conn->sconn->searches.dirhandles_open--;
1522 /*******************************************************************
1524 ********************************************************************/
1526 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1531 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1532 struct smbd_server_connection *sconn = conn->sconn;
1539 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1541 dirp->dir_path = talloc_strdup(dirp, name);
1542 if (!dirp->dir_path) {
1547 if (sconn && !sconn->using_smb2) {
1548 sconn->searches.dirhandles_open++;
1550 talloc_set_destructor(dirp, smb_Dir_destructor);
1552 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1554 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1566 /*******************************************************************
1567 Open a directory from an fsp.
1568 ********************************************************************/
1570 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1575 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1576 struct smbd_server_connection *sconn = conn->sconn;
1583 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1585 dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
1586 if (!dirp->dir_path) {
1591 if (sconn && !sconn->using_smb2) {
1592 sconn->searches.dirhandles_open++;
1594 talloc_set_destructor(dirp, smb_Dir_destructor);
1596 if (fsp->is_directory && fsp->fh->fd != -1) {
1597 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1598 if (dirp->dir != NULL) {
1601 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1605 if (errno != ENOSYS) {
1611 if (dirp->dir == NULL) {
1612 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1613 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1617 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1630 /*******************************************************************
1631 Read from a directory.
1632 Return directory entry, current offset, and optional stat information.
1633 Don't check for veto or invisible files.
1634 ********************************************************************/
1636 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1637 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1640 char *talloced = NULL;
1641 connection_struct *conn = dirp->conn;
1643 /* Cheat to allow . and .. to be the first entries returned. */
1644 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1645 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1647 if (dirp->file_number == 0) {
1649 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1652 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1654 dirp->file_number++;
1657 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1658 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1661 /* A real offset, seek to it. */
1662 SeekDir(dirp, *poffset);
1665 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1666 /* Ignore . and .. - we've already returned them. */
1668 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1669 TALLOC_FREE(talloced);
1673 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1674 *ptalloced = talloced;
1675 dirp->file_number++;
1678 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1683 /*******************************************************************
1684 Rewind to the start.
1685 ********************************************************************/
1687 void RewindDir(struct smb_Dir *dirp, long *poffset)
1689 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1690 dirp->file_number = 0;
1691 dirp->offset = START_OF_DIRECTORY_OFFSET;
1692 *poffset = START_OF_DIRECTORY_OFFSET;
1695 /*******************************************************************
1697 ********************************************************************/
1699 void SeekDir(struct smb_Dir *dirp, long offset)
1701 if (offset != dirp->offset) {
1702 if (offset == START_OF_DIRECTORY_OFFSET) {
1703 RewindDir(dirp, &offset);
1705 * Ok we should really set the file number here
1706 * to 1 to enable ".." to be returned next. Trouble
1707 * is I'm worried about callers using SeekDir(dirp,0)
1708 * as equivalent to RewindDir(). So leave this alone
1711 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1712 RewindDir(dirp, &offset);
1714 * Set the file number to 2 - we want to get the first
1715 * real file entry (the one we return after "..")
1716 * on the next ReadDir.
1718 dirp->file_number = 2;
1719 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1720 ; /* Don't seek in this case. */
1722 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1724 dirp->offset = offset;
1728 /*******************************************************************
1729 Tell a dir position.
1730 ********************************************************************/
1732 long TellDir(struct smb_Dir *dirp)
1734 return(dirp->offset);
1737 /*******************************************************************
1738 Add an entry into the dcache.
1739 ********************************************************************/
1741 static void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1743 struct name_cache_entry *e;
1745 if (dirp->name_cache_size == 0) {
1749 if (dirp->name_cache == NULL) {
1750 dirp->name_cache = talloc_zero_array(
1751 dirp, struct name_cache_entry, dirp->name_cache_size);
1753 if (dirp->name_cache == NULL) {
1758 dirp->name_cache_index = (dirp->name_cache_index+1) %
1759 dirp->name_cache_size;
1760 e = &dirp->name_cache[dirp->name_cache_index];
1761 TALLOC_FREE(e->name);
1762 e->name = talloc_strdup(dirp, name);
1766 /*******************************************************************
1767 Find an entry by name. Leave us at the offset after it.
1768 Don't check for veto or invisible files.
1769 ********************************************************************/
1771 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1774 const char *entry = NULL;
1775 char *talloced = NULL;
1776 connection_struct *conn = dirp->conn;
1778 /* Search back in the name cache. */
1779 if (dirp->name_cache_size && dirp->name_cache) {
1780 for (i = dirp->name_cache_index; i >= 0; i--) {
1781 struct name_cache_entry *e = &dirp->name_cache[i];
1782 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1783 *poffset = e->offset;
1784 SeekDir(dirp, e->offset);
1788 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1789 struct name_cache_entry *e = &dirp->name_cache[i];
1790 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1791 *poffset = e->offset;
1792 SeekDir(dirp, e->offset);
1798 /* Not found in the name cache. Rewind directory and start from scratch. */
1799 SMB_VFS_REWINDDIR(conn, dirp->dir);
1800 dirp->file_number = 0;
1801 *poffset = START_OF_DIRECTORY_OFFSET;
1802 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
1803 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1804 TALLOC_FREE(talloced);
1807 TALLOC_FREE(talloced);
1812 /*****************************************************************
1813 Is this directory empty ?
1814 *****************************************************************/
1816 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1818 NTSTATUS status = NT_STATUS_OK;
1820 const char *dname = NULL;
1821 const char *dirname = fsp->fsp_name->base_name;
1822 char *talloced = NULL;
1824 struct connection_struct *conn = fsp->conn;
1825 struct smb_Dir *dir_hnd = OpenDir_fsp(talloc_tos(),
1832 return map_nt_error_from_unix(errno);
1835 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
1836 /* Quick check for "." and ".." */
1837 if (dname[0] == '.') {
1838 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1839 TALLOC_FREE(talloced);
1844 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1845 TALLOC_FREE(talloced);
1849 DEBUG(10,("got name %s - can't delete\n",
1851 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1854 TALLOC_FREE(talloced);
1855 TALLOC_FREE(dir_hnd);