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"
28 #include "../librpc/gen_ndr/open_files.h"
31 This module implements directory related functions for Samba.
34 /* "Special" directory offsets. */
35 #define END_OF_DIRECTORY_OFFSET ((long)-1)
36 #define START_OF_DIRECTORY_OFFSET ((long)0)
37 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
39 /* "Special" directory offsets in 32-bit wire format. */
40 #define WIRE_END_OF_DIRECTORY_OFFSET ((uint32_t)0xFFFFFFFF)
41 #define WIRE_START_OF_DIRECTORY_OFFSET ((uint32_t)0)
42 #define WIRE_DOT_DOT_DIRECTORY_OFFSET ((uint32_t)0x80000000)
44 /* Make directory handle internals available. */
46 struct name_cache_entry {
52 connection_struct *conn;
55 struct smb_filename *dir_smb_fname;
56 size_t name_cache_size;
57 struct name_cache_entry *name_cache;
58 unsigned int name_cache_index;
59 unsigned int file_number;
60 files_struct *fsp; /* Back pointer to containing fsp, only
61 set from OpenDir_fsp(). */
65 struct dptr_struct *next, *prev;
68 struct connection_struct *conn;
69 struct smb_Dir *dir_hnd;
73 struct smb_filename *smb_dname;
74 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
75 bool did_stat; /* Optimisation for non-wcard searches. */
76 bool priv; /* Directory handle opened with privilege. */
78 struct memcache *dptr_cache;
81 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
86 static void DirCacheAdd(struct smb_Dir *dir_hnd, const char *name, long offset);
88 static int smb_Dir_destructor(struct smb_Dir *dir_hnd);
90 #define INVALID_DPTR_KEY (-3)
92 /****************************************************************************
93 Initialise the dir bitmap.
94 ****************************************************************************/
96 bool init_dptrs(struct smbd_server_connection *sconn)
98 if (sconn->searches.dptr_bmap) {
102 sconn->searches.dptr_bmap = bitmap_talloc(
103 sconn, MAX_DIRECTORY_HANDLES);
105 if (sconn->searches.dptr_bmap == NULL) {
112 /****************************************************************************
113 Get the struct dptr_struct for a dir index.
114 ****************************************************************************/
116 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
119 struct dptr_struct *dptr;
121 for (dptr = sconn->searches.dirptrs; dptr != NULL; dptr = dptr->next) {
122 if(dptr->dnum != key) {
125 DLIST_PROMOTE(sconn->searches.dirptrs, dptr);
131 /****************************************************************************
132 Get the dir path for a dir index.
133 ****************************************************************************/
135 const char *dptr_path(struct smbd_server_connection *sconn, int key)
137 struct dptr_struct *dptr = dptr_get(sconn, key);
139 return(dptr->smb_dname->base_name);
143 /****************************************************************************
144 Get the dir wcard for a dir index.
145 ****************************************************************************/
147 const char *dptr_wcard(struct smbd_server_connection *sconn, int key)
149 struct dptr_struct *dptr = dptr_get(sconn, key);
155 /****************************************************************************
156 Get the dir attrib for a dir index.
157 ****************************************************************************/
159 uint16_t dptr_attr(struct smbd_server_connection *sconn, int key)
161 struct dptr_struct *dptr = dptr_get(sconn, key);
167 /****************************************************************************
168 Close all dptrs for a cnum.
169 ****************************************************************************/
171 void dptr_closecnum(connection_struct *conn)
173 struct dptr_struct *dptr, *next;
174 struct smbd_server_connection *sconn = conn->sconn;
180 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
182 if (dptr->conn == conn) {
183 files_struct *fsp = dptr->dir_hnd->fsp;
184 close_file(NULL, fsp, NORMAL_CLOSE);
190 /****************************************************************************
191 Create a new dir ptr. If the flag old_handle is true then we must allocate
192 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
193 one byte long. If old_handle is false we allocate from the range
194 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
195 a directory handle is never zero.
196 wcard must not be zero.
197 ****************************************************************************/
199 NTSTATUS dptr_create(connection_struct *conn,
200 struct smb_request *req,
208 struct dptr_struct **dptr_ret)
210 struct smbd_server_connection *sconn = conn->sconn;
211 struct dptr_struct *dptr = NULL;
212 struct smb_Dir *dir_hnd;
214 DBG_INFO("dir=%s\n", fsp_str_dbg(fsp));
217 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
218 return NT_STATUS_INTERNAL_ERROR;
222 return NT_STATUS_INVALID_PARAMETER;
225 if (!(fsp->access_mask & SEC_DIR_LIST)) {
226 DBG_INFO("dptr_create: directory %s "
227 "not open for LIST access\n",
229 return NT_STATUS_ACCESS_DENIED;
231 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
233 return map_nt_error_from_unix(errno);
236 dptr = talloc_zero(NULL, struct dptr_struct);
238 DEBUG(0,("talloc fail in dptr_create.\n"));
239 TALLOC_FREE(dir_hnd);
240 return NT_STATUS_NO_MEMORY;
243 dptr->smb_dname = cp_smb_filename(dptr, fsp->fsp_name);
244 if (dptr->smb_dname == NULL) {
246 TALLOC_FREE(dir_hnd);
247 return NT_STATUS_NO_MEMORY;
250 dptr->dir_hnd = dir_hnd;
252 dptr->expect_close = expect_close;
253 dptr->wcard = talloc_strdup(dptr, wcard);
256 TALLOC_FREE(dir_hnd);
257 return NT_STATUS_NO_MEMORY;
259 if ((req != NULL && req->posix_pathnames) ||
260 (wcard[0] == '.' && wcard[1] == 0)) {
261 dptr->has_wild = True;
263 dptr->has_wild = wcard_has_wild;
268 if (sconn->using_smb2) {
275 * This is an old-style SMBsearch request. Ensure the
276 * value we return will fit in the range 1-255.
279 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
281 if(dptr->dnum == -1 || dptr->dnum > 254) {
282 DBG_ERR("returned %d: Error - all old "
283 "dirptrs in use ?\n",
286 TALLOC_FREE(dir_hnd);
287 return NT_STATUS_TOO_MANY_OPENED_FILES;
292 * This is a new-style trans2 request. Allocate from
293 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
296 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
298 if(dptr->dnum == -1 || dptr->dnum < 255) {
299 DBG_ERR("returned %d: Error - all new "
300 "dirptrs in use ?\n",
303 TALLOC_FREE(dir_hnd);
304 return NT_STATUS_TOO_MANY_OPENED_FILES;
308 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
310 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
312 DLIST_ADD(sconn->searches.dirptrs, dptr);
315 DBG_INFO("creating new dirptr [%d] for path [%s], expect_close = %d\n",
316 dptr->dnum, fsp_str_dbg(fsp), expect_close);
324 /****************************************************************************
325 Wrapper functions to access the lower level directory handles.
326 ****************************************************************************/
328 void dptr_CloseDir(files_struct *fsp)
330 struct smbd_server_connection *sconn = NULL;
332 if (fsp->dptr == NULL) {
335 sconn = fsp->dptr->conn->sconn;
338 * The destructor for the struct smb_Dir (fsp->dptr->dir_hnd)
339 * now handles all resource deallocation.
342 DBG_INFO("closing dptr key %d\n", fsp->dptr->dnum);
344 if (sconn != NULL && !sconn->using_smb2) {
345 DLIST_REMOVE(sconn->searches.dirptrs, fsp->dptr);
348 * Free the dnum in the bitmap. Remember the dnum value is
349 * always biased by one with respect to the bitmap.
352 if (!bitmap_query(sconn->searches.dptr_bmap,
353 fsp->dptr->dnum - 1))
355 DBG_ERR("closing dnum = %d and bitmap not set !\n",
359 bitmap_clear(sconn->searches.dptr_bmap, fsp->dptr->dnum - 1);
362 TALLOC_FREE(fsp->dptr->dir_hnd);
363 TALLOC_FREE(fsp->dptr);
366 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
368 SeekDir(dptr->dir_hnd, offset);
371 long dptr_TellDir(struct dptr_struct *dptr)
373 return TellDir(dptr->dir_hnd);
376 bool dptr_has_wild(struct dptr_struct *dptr)
378 return dptr->has_wild;
381 int dptr_dnum(struct dptr_struct *dptr)
386 bool dptr_get_priv(struct dptr_struct *dptr)
391 void dptr_set_priv(struct dptr_struct *dptr)
396 /****************************************************************************
397 Return the next visible file name, skipping veto'd and invisible files.
398 ****************************************************************************/
400 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
401 long *poffset, SMB_STRUCT_STAT *pst,
404 /* Normal search for the next file. */
406 char *talloced = NULL;
408 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
410 if (is_visible_file(dptr->conn,
415 *ptalloced = talloced;
418 TALLOC_FREE(talloced);
423 /****************************************************************************
424 Return the next visible file name, skipping veto'd and invisible files.
425 ****************************************************************************/
427 static char *dptr_ReadDirName(TALLOC_CTX *ctx,
428 struct dptr_struct *dptr,
430 SMB_STRUCT_STAT *pst)
432 struct smb_filename smb_fname_base;
434 const char *name_temp = NULL;
435 char *talloced = NULL;
436 char *pathreal = NULL;
437 char *found_name = NULL;
440 SET_STAT_INVALID(*pst);
442 if (dptr->has_wild || dptr->did_stat) {
443 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
445 if (name_temp == NULL) {
448 if (talloced != NULL) {
449 return talloc_move(ctx, &talloced);
451 return talloc_strdup(ctx, name_temp);
454 /* If poffset is -1 then we know we returned this name before and we
455 * have no wildcards. We're at the end of the directory. */
456 if (*poffset == END_OF_DIRECTORY_OFFSET) {
460 /* We know the stored wcard contains no wildcard characters.
461 * See if we can match with a stat call. If we can't, then set
462 * did_stat to true to ensure we only do this once and keep
465 dptr->did_stat = true;
467 /* First check if it should be visible. */
468 if (!is_visible_file(dptr->conn,
473 /* This only returns false if the file was found, but
474 is explicitly not visible. Set us to end of
475 directory, but return NULL as we know we can't ever
480 if (VALID_STAT(*pst)) {
481 name = talloc_strdup(ctx, dptr->wcard);
485 pathreal = talloc_asprintf(ctx,
487 dptr->smb_dname->base_name,
492 /* Create an smb_filename with stream_name == NULL. */
493 smb_fname_base = (struct smb_filename) { .base_name = pathreal };
495 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
496 *pst = smb_fname_base.st;
497 name = talloc_strdup(ctx, dptr->wcard);
500 /* If we get any other error than ENOENT or ENOTDIR
501 then the file exists we just can't stat it. */
502 if (errno != ENOENT && errno != ENOTDIR) {
503 name = talloc_strdup(ctx, dptr->wcard);
508 /* Stat failed. We know this is authoratiative if we are
509 * providing case sensitive semantics or the underlying
510 * filesystem is case sensitive.
512 if (dptr->conn->case_sensitive ||
513 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
519 * Try case-insensitive stat if the fs has the ability. This avoids
520 * scanning the whole directory.
522 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn,
523 dptr->smb_dname->base_name,
530 } else if (errno == ENOENT) {
531 /* The case-insensitive lookup was authoritative. */
535 TALLOC_FREE(pathreal);
537 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
538 if (name_temp == NULL) {
541 if (talloced != NULL) {
542 return talloc_move(ctx, &talloced);
544 return talloc_strdup(ctx, name_temp);
547 TALLOC_FREE(pathreal);
549 /* We need to set the underlying dir_hnd offset to -1
550 * also as this function is usually called with the
551 * output from TellDir. */
552 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
556 /****************************************************************************
557 Search for a file by name, skipping veto'ed and not visible files.
558 ****************************************************************************/
560 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
562 SET_STAT_INVALID(*pst);
564 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
565 /* This is a singleton directory and we're already at the end. */
566 *poffset = END_OF_DIRECTORY_OFFSET;
570 return SearchDir(dptr->dir_hnd, name, poffset);
573 /****************************************************************************
574 Map a native directory offset to a 32-bit cookie.
575 ****************************************************************************/
577 static uint32_t map_dir_offset_to_wire(struct dptr_struct *dptr, long offset)
582 if (offset == END_OF_DIRECTORY_OFFSET) {
583 return WIRE_END_OF_DIRECTORY_OFFSET;
584 } else if(offset == START_OF_DIRECTORY_OFFSET) {
585 return WIRE_START_OF_DIRECTORY_OFFSET;
586 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
587 return WIRE_DOT_DOT_DIRECTORY_OFFSET;
589 if (sizeof(long) == 4) {
590 /* 32-bit machine. We can cheat... */
591 return (uint32_t)offset;
593 if (dptr->dptr_cache == NULL) {
594 /* Lazy initialize cache. */
595 dptr->dptr_cache = memcache_init(dptr, 0);
596 if (dptr->dptr_cache == NULL) {
597 return WIRE_END_OF_DIRECTORY_OFFSET;
600 /* Have we seen this offset before ? */
601 key.data = (void *)&offset;
602 key.length = sizeof(offset);
603 if (memcache_lookup(dptr->dptr_cache,
604 SMB1_SEARCH_OFFSET_MAP,
607 uint32_t wire_offset;
608 SMB_ASSERT(val.length == sizeof(wire_offset));
609 memcpy(&wire_offset, val.data, sizeof(wire_offset));
610 DEBUG(10,("found wire %u <-> offset %ld\n",
611 (unsigned int)wire_offset,
616 /* Allocate a new wire cookie. */
619 } while (dptr->counter == WIRE_START_OF_DIRECTORY_OFFSET ||
620 dptr->counter == WIRE_END_OF_DIRECTORY_OFFSET ||
621 dptr->counter == WIRE_DOT_DOT_DIRECTORY_OFFSET);
622 /* Store it in the cache. */
623 key.data = (void *)&offset;
624 key.length = sizeof(offset);
625 val.data = (void *)&dptr->counter;
626 val.length = sizeof(dptr->counter); /* MUST BE uint32_t ! */
627 memcache_add(dptr->dptr_cache,
628 SMB1_SEARCH_OFFSET_MAP,
631 /* And the reverse mapping for lookup from
632 map_wire_to_dir_offset(). */
633 memcache_add(dptr->dptr_cache,
634 SMB1_SEARCH_OFFSET_MAP,
637 DEBUG(10,("stored wire %u <-> offset %ld\n",
638 (unsigned int)dptr->counter,
640 return dptr->counter;
643 /****************************************************************************
644 Fill the 5 byte server reserved dptr field.
645 ****************************************************************************/
647 bool dptr_fill(struct smbd_server_connection *sconn,
648 char *buf1,unsigned int key)
650 unsigned char *buf = (unsigned char *)buf1;
651 struct dptr_struct *dptr = dptr_get(sconn, key);
652 uint32_t wire_offset;
654 DEBUG(1,("filling null dirptr %d\n",key));
657 wire_offset = map_dir_offset_to_wire(dptr,TellDir(dptr->dir_hnd));
658 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
659 (long)dptr->dir_hnd,(int)wire_offset));
661 SIVAL(buf,1,wire_offset);
665 /****************************************************************************
666 Map a 32-bit wire cookie to a native directory offset.
667 ****************************************************************************/
669 static long map_wire_to_dir_offset(struct dptr_struct *dptr, uint32_t wire_offset)
674 if (wire_offset == WIRE_END_OF_DIRECTORY_OFFSET) {
675 return END_OF_DIRECTORY_OFFSET;
676 } else if(wire_offset == WIRE_START_OF_DIRECTORY_OFFSET) {
677 return START_OF_DIRECTORY_OFFSET;
678 } else if (wire_offset == WIRE_DOT_DOT_DIRECTORY_OFFSET) {
679 return DOT_DOT_DIRECTORY_OFFSET;
681 if (sizeof(long) == 4) {
682 /* 32-bit machine. We can cheat... */
683 return (long)wire_offset;
685 if (dptr->dptr_cache == NULL) {
686 /* Logic error, cache should be initialized. */
687 return END_OF_DIRECTORY_OFFSET;
689 key.data = (void *)&wire_offset;
690 key.length = sizeof(wire_offset);
691 if (memcache_lookup(dptr->dptr_cache,
692 SMB1_SEARCH_OFFSET_MAP,
697 SMB_ASSERT(val.length == sizeof(offset));
698 memcpy(&offset, val.data, sizeof(offset));
699 DEBUG(10,("lookup wire %u <-> offset %ld\n",
700 (unsigned int)wire_offset,
704 return END_OF_DIRECTORY_OFFSET;
707 /****************************************************************************
708 Return the associated fsp and seek the dir_hnd on it it given the 5 byte
710 ****************************************************************************/
712 files_struct *dptr_fetch_fsp(struct smbd_server_connection *sconn,
715 unsigned int key = *(unsigned char *)buf;
716 struct dptr_struct *dptr = dptr_get(sconn, key);
717 uint32_t wire_offset;
721 DEBUG(3,("fetched null dirptr %d\n",key));
725 wire_offset = IVAL(buf,1);
726 seekoff = map_wire_to_dir_offset(dptr, wire_offset);
727 SeekDir(dptr->dir_hnd,seekoff);
728 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
729 key, dptr->smb_dname->base_name, (int)seekoff));
730 return dptr->dir_hnd->fsp;
733 /****************************************************************************
734 Fetch the fsp associated with the dptr_num.
735 ****************************************************************************/
737 files_struct *dptr_fetch_lanman2_fsp(struct smbd_server_connection *sconn,
740 struct dptr_struct *dptr = dptr_get(sconn, dptr_num);
744 DBG_NOTICE("fetching dirptr %d for path %s\n",
746 dptr->smb_dname->base_name);
747 return dptr->dir_hnd->fsp;
750 static bool mangle_mask_match(connection_struct *conn,
751 const char *filename,
756 if (!name_to_8_3(filename,mname,False,conn->params)) {
759 return mask_match_search(mname,mask,False);
762 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
763 struct dptr_struct *dirptr,
769 bool (*match_fn)(TALLOC_CTX *ctx,
774 bool (*mode_fn)(TALLOC_CTX *ctx,
776 struct smb_filename *smb_fname,
781 struct smb_filename **_smb_fname,
785 connection_struct *conn = dirptr->conn;
788 const char *dpath = dirptr->smb_dname->base_name;
789 bool dirptr_path_is_dot = ISDOT(dpath);
794 pathlen = strlen(dpath);
795 slashlen = ( dpath[pathlen-1] != '/') ? 1 : 0;
800 SMB_STRUCT_STAT sbuf = { 0 };
804 char *pathreal = NULL;
805 struct smb_filename smb_fname;
809 cur_offset = dptr_TellDir(dirptr);
810 prev_offset = cur_offset;
811 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
813 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
814 (long)dirptr, cur_offset));
820 isdots = (ISDOT(dname) || ISDOTDOT(dname));
821 if (dont_descend && !isdots) {
827 * fname may get mangled, dname is never mangled.
828 * Whenever we're accessing the filesystem we use
829 * pathreal which is composed from dname.
832 ok = match_fn(ctx, private_data, dname, mask, &fname);
840 * pathreal = talloc_asprintf(ctx, "%s%s%s", dirptr->path,
841 * needslash?"/":"", dname);
842 * but this was measurably slower than doing the memcpy.
845 pathreal = talloc_array(
847 pathlen + slashlen + talloc_get_size(dname));
855 * We don't want to pass ./xxx to modules below us so don't
856 * add the path if it is just . by itself.
858 if (dirptr_path_is_dot) {
859 memcpy(pathreal, dname, talloc_get_size(dname));
861 memcpy(pathreal, dpath, pathlen);
862 pathreal[pathlen] = '/';
863 memcpy(pathreal + slashlen + pathlen, dname,
864 talloc_get_size(dname));
867 /* Create smb_fname with NULL stream_name. */
868 smb_fname = (struct smb_filename) {
869 .base_name = pathreal, .st = sbuf
872 ok = mode_fn(ctx, private_data, &smb_fname, get_dosmode, &mode);
876 TALLOC_FREE(pathreal);
880 if (!dir_check_ftype(mode, dirtype)) {
881 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
882 fname, (unsigned int)mode, (unsigned int)dirtype));
885 TALLOC_FREE(pathreal);
889 if (ask_sharemode && !S_ISDIR(smb_fname.st.st_ex_mode)) {
890 struct timespec write_time_ts;
891 struct file_id fileid;
893 fileid = vfs_file_id_from_sbuf(conn,
895 get_file_infos(fileid, 0, NULL, &write_time_ts);
896 if (!is_omit_timespec(&write_time_ts)) {
897 update_stat_ex_mtime(&smb_fname.st,
902 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
904 mask, smb_fname_str_dbg(&smb_fname),
907 if (!conn->sconn->using_smb2) {
909 * The dircache is only needed for SMB1 because SMB1
910 * uses a name for the resume wheras SMB2 always
911 * continues from the next position (unless it's told to
912 * restart or close-and-reopen the listing).
914 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
919 *_smb_fname = cp_smb_filename(ctx, &smb_fname);
920 TALLOC_FREE(pathreal);
921 if (*_smb_fname == NULL) {
926 *_prev_offset = prev_offset;
934 /****************************************************************************
935 Get an 8.3 directory entry.
936 ****************************************************************************/
938 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
944 connection_struct *conn = (connection_struct *)private_data;
946 if ((strcmp(mask,"*.*") == 0) ||
947 mask_match_search(dname, mask, false) ||
948 mangle_mask_match(conn, dname, mask)) {
952 * Ensure we can push the original name as UCS2. If
953 * not, then just don't return this name.
957 size_t len = (strlen(dname) + 2) * 4; /* Allow enough space. */
958 uint8_t *tmp = talloc_array(talloc_tos(),
962 status = srvstr_push(NULL,
963 FLAGS2_UNICODE_STRINGS,
972 if (!NT_STATUS_IS_OK(status)) {
976 if (!mangle_is_8_3(dname, false, conn->params)) {
977 bool ok = name_to_8_3(dname, mname, false,
987 *_fname = talloc_strdup(ctx, fname);
988 if (*_fname == NULL) {
998 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1000 struct smb_filename *smb_fname,
1004 connection_struct *conn = (connection_struct *)private_data;
1006 if (!VALID_STAT(smb_fname->st)) {
1007 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1008 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1009 "Couldn't stat [%s]. Error "
1011 smb_fname_str_dbg(smb_fname),
1017 *_mode = dos_mode(conn, smb_fname);
1021 bool get_dir_entry(TALLOC_CTX *ctx,
1022 struct dptr_struct *dirptr,
1028 struct timespec *_date,
1032 connection_struct *conn = dirptr->conn;
1034 struct smb_filename *smb_fname = NULL;
1039 ok = smbd_dirptr_get_entry(ctx,
1046 smbd_dirptr_8_3_match_fn,
1047 smbd_dirptr_8_3_mode_fn,
1057 *_fname = talloc_move(ctx, &fname);
1058 *_size = smb_fname->st.st_ex_size;
1060 *_date = smb_fname->st.st_ex_mtime;
1061 TALLOC_FREE(smb_fname);
1065 /*******************************************************************
1066 Check to see if a user can read a file. This is only approximate,
1067 it is used as part of the "hide unreadable" option. Don't
1068 use it for anything security sensitive.
1069 ********************************************************************/
1071 static bool user_can_read_file(connection_struct *conn,
1072 struct smb_filename *smb_fname)
1075 uint32_t rejected_share_access = 0;
1076 uint32_t rejected_mask = 0;
1077 struct security_descriptor *sd = NULL;
1078 uint32_t access_mask = FILE_READ_DATA|
1080 FILE_READ_ATTRIBUTES|
1081 SEC_STD_READ_CONTROL;
1084 * Never hide files from the root user.
1085 * We use (uid_t)0 here not sec_initial_uid()
1086 * as make test uses a single user context.
1089 if (get_current_uid(conn) == (uid_t)0) {
1094 * We can't directly use smbd_check_access_rights()
1095 * here, as this implicitly grants FILE_READ_ATTRIBUTES
1096 * which the Windows access-based-enumeration code
1097 * explicitly checks for on the file security descriptor.
1100 * https://bugzilla.samba.org/show_bug.cgi?id=10252
1102 * and the smb2.acl2.ACCESSBASED test for details.
1105 rejected_share_access = access_mask & ~(conn->share_access);
1106 if (rejected_share_access) {
1107 DEBUG(10, ("rejected share access 0x%x "
1109 (unsigned int)access_mask,
1110 smb_fname_str_dbg(smb_fname),
1111 (unsigned int)rejected_share_access ));
1115 status = SMB_VFS_GET_NT_ACL(conn,
1123 if (!NT_STATUS_IS_OK(status)) {
1124 DEBUG(10, ("Could not get acl "
1126 smb_fname_str_dbg(smb_fname),
1127 nt_errstr(status)));
1131 status = se_file_access_check(sd,
1132 get_current_nttok(conn),
1139 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1140 DEBUG(10,("rejected bits 0x%x read access for %s\n",
1141 (unsigned int)rejected_mask,
1142 smb_fname_str_dbg(smb_fname) ));
1148 /*******************************************************************
1149 Check to see if a user can write a file (and only files, we do not
1150 check dirs on this one). This is only approximate,
1151 it is used as part of the "hide unwriteable" option. Don't
1152 use it for anything security sensitive.
1153 ********************************************************************/
1155 static bool user_can_write_file(connection_struct *conn,
1156 const struct smb_filename *smb_fname)
1159 * Never hide files from the root user.
1160 * We use (uid_t)0 here not sec_initial_uid()
1161 * as make test uses a single user context.
1164 if (get_current_uid(conn) == (uid_t)0) {
1168 SMB_ASSERT(VALID_STAT(smb_fname->st));
1170 /* Pseudo-open the file */
1172 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1176 return can_write_to_file(conn, smb_fname);
1179 /*******************************************************************
1180 Is a file a "special" type ?
1181 ********************************************************************/
1183 static bool file_is_special(connection_struct *conn,
1184 const struct smb_filename *smb_fname)
1187 * Never hide files from the root user.
1188 * We use (uid_t)0 here not sec_initial_uid()
1189 * as make test uses a single user context.
1192 if (get_current_uid(conn) == (uid_t)0) {
1196 SMB_ASSERT(VALID_STAT(smb_fname->st));
1198 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1199 S_ISDIR(smb_fname->st.st_ex_mode) ||
1200 S_ISLNK(smb_fname->st.st_ex_mode))
1206 /*******************************************************************
1207 Should the file be seen by the client?
1208 NOTE: A successful return is no guarantee of the file's existence.
1209 ********************************************************************/
1211 bool is_visible_file(connection_struct *conn,
1212 struct smb_Dir *dir_hnd,
1214 SMB_STRUCT_STAT *pst,
1217 bool hide_unreadable = lp_hide_unreadable(SNUM(conn));
1218 bool hide_unwriteable = lp_hide_unwriteable_files(SNUM(conn));
1219 bool hide_special = lp_hide_special_files(SNUM(conn));
1220 int hide_new_files_timeout = lp_hide_new_files_timeout(SNUM(conn));
1222 struct smb_filename *dir_path = dir_hnd->fsp->fsp_name;
1223 struct smb_filename *smb_fname_base = NULL;
1226 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1227 return True; /* . and .. are always visible. */
1230 /* If it's a vetoed file, pretend it doesn't even exist */
1231 if (use_veto && IS_VETO_PATH(conn, name)) {
1232 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1236 if (hide_unreadable ||
1239 (hide_new_files_timeout != 0))
1241 entry = talloc_asprintf(talloc_tos(),
1243 dir_path->base_name,
1250 /* Create an smb_filename with stream_name == NULL. */
1251 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1256 if (smb_fname_base == NULL) {
1261 /* If the file name does not exist, there's no point checking
1262 * the configuration options. We succeed, on the basis that the
1263 * checks *might* have passed if the file was present.
1265 if (!VALID_STAT(*pst)) {
1266 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1270 *pst = smb_fname_base->st;
1273 /* Honour _hide unreadable_ option */
1274 if (hide_unreadable &&
1275 !user_can_read_file(conn, smb_fname_base)) {
1276 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1281 /* Honour _hide unwriteable_ option */
1282 if (hide_unwriteable && !user_can_write_file(conn,
1284 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1289 /* Honour _hide_special_ option */
1290 if (hide_special && file_is_special(conn, smb_fname_base)) {
1291 DEBUG(10,("is_visible_file: file %s is special.\n",
1297 if (hide_new_files_timeout != 0) {
1299 double age = timespec_elapsed(
1300 &smb_fname_base->st.st_ex_mtime);
1302 if (age < (double)hide_new_files_timeout) {
1311 TALLOC_FREE(smb_fname_base);
1316 static int smb_Dir_destructor(struct smb_Dir *dir_hnd)
1318 files_struct *fsp = dir_hnd->fsp;
1320 SMB_VFS_CLOSEDIR(dir_hnd->conn, dir_hnd->dir);
1322 if (fsp->dptr != NULL) {
1323 SMB_ASSERT(fsp->dptr->dir_hnd == dir_hnd);
1324 fsp->dptr->dir_hnd = NULL;
1326 dir_hnd->fsp = NULL;
1330 /*******************************************************************
1332 ********************************************************************/
1334 static int smb_Dir_OpenDir_destructor(struct smb_Dir *dir_hnd)
1336 files_struct *fsp = dir_hnd->fsp;
1338 smb_Dir_destructor(dir_hnd);
1339 file_free(NULL, fsp);
1343 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx,
1344 connection_struct *conn,
1345 const struct smb_filename *smb_dname,
1349 struct files_struct *fsp = NULL;
1350 struct smb_Dir *dir_hnd = NULL;
1353 status = open_internal_dirfsp_at(conn, conn->cwd_fsp, smb_dname, &fsp);
1354 if (!NT_STATUS_IS_OK(status)) {
1358 dir_hnd = OpenDir_fsp(mem_ctx, conn, fsp, mask, attr);
1359 if (dir_hnd == NULL) {
1364 * This overwrites the destructor set by smb_Dir_OpenDir_destructor(),
1365 * but smb_Dir_OpenDir_destructor() calls the OpenDir_fsp() destructor.
1367 talloc_set_destructor(dir_hnd, smb_Dir_OpenDir_destructor);
1371 /*******************************************************************
1372 Open a directory from an fsp.
1373 ********************************************************************/
1375 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1380 struct smb_Dir *dir_hnd = talloc_zero(mem_ctx, struct smb_Dir);
1386 if (!fsp->fsp_flags.is_directory) {
1391 if (fsp->fh->fd == -1) {
1396 dir_hnd->conn = conn;
1398 if (!conn->sconn->using_smb2) {
1400 * The dircache is only needed for SMB1 because SMB1 uses a name
1401 * for the resume wheras SMB2 always continues from the next
1402 * position (unless it's told to restart or close-and-reopen the
1405 dir_hnd->name_cache_size =
1406 lp_directory_name_cache_size(SNUM(conn));
1409 dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, fsp->fsp_name);
1410 if (!dir_hnd->dir_smb_fname) {
1415 dir_hnd->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1416 if (dir_hnd->dir == NULL) {
1421 talloc_set_destructor(dir_hnd, smb_Dir_destructor);
1426 TALLOC_FREE(dir_hnd);
1431 /*******************************************************************
1432 Read from a directory.
1433 Return directory entry, current offset, and optional stat information.
1434 Don't check for veto or invisible files.
1435 ********************************************************************/
1437 const char *ReadDirName(struct smb_Dir *dir_hnd, long *poffset,
1438 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1441 char *talloced = NULL;
1442 connection_struct *conn = dir_hnd->conn;
1444 /* Cheat to allow . and .. to be the first entries returned. */
1445 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1446 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dir_hnd->file_number < 2))
1448 if (dir_hnd->file_number == 0) {
1450 *poffset = dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1453 *poffset = dir_hnd->offset = DOT_DOT_DIRECTORY_OFFSET;
1455 dir_hnd->file_number++;
1460 if (*poffset == END_OF_DIRECTORY_OFFSET) {
1461 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1465 /* A real offset, seek to it. */
1466 SeekDir(dir_hnd, *poffset);
1468 while ((n = vfs_readdirname(conn, dir_hnd->dir, sbuf, &talloced))) {
1469 /* Ignore . and .. - we've already returned them. */
1471 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1472 TALLOC_FREE(talloced);
1476 *poffset = dir_hnd->offset = SMB_VFS_TELLDIR(conn, dir_hnd->dir);
1477 *ptalloced = talloced;
1478 dir_hnd->file_number++;
1481 *poffset = dir_hnd->offset = END_OF_DIRECTORY_OFFSET;
1486 /*******************************************************************
1487 Rewind to the start.
1488 ********************************************************************/
1490 void RewindDir(struct smb_Dir *dir_hnd, long *poffset)
1492 SMB_VFS_REWINDDIR(dir_hnd->conn, dir_hnd->dir);
1493 dir_hnd->file_number = 0;
1494 dir_hnd->offset = START_OF_DIRECTORY_OFFSET;
1495 *poffset = START_OF_DIRECTORY_OFFSET;
1498 /*******************************************************************
1500 ********************************************************************/
1502 void SeekDir(struct smb_Dir *dirp, long offset)
1504 if (offset != dirp->offset) {
1505 if (offset == START_OF_DIRECTORY_OFFSET) {
1506 RewindDir(dirp, &offset);
1508 * Ok we should really set the file number here
1509 * to 1 to enable ".." to be returned next. Trouble
1510 * is I'm worried about callers using SeekDir(dirp,0)
1511 * as equivalent to RewindDir(). So leave this alone
1514 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1515 RewindDir(dirp, &offset);
1517 * Set the file number to 2 - we want to get the first
1518 * real file entry (the one we return after "..")
1519 * on the next ReadDir.
1521 dirp->file_number = 2;
1522 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1523 ; /* Don't seek in this case. */
1525 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1527 dirp->offset = offset;
1531 /*******************************************************************
1532 Tell a dir position.
1533 ********************************************************************/
1535 long TellDir(struct smb_Dir *dir_hnd)
1537 return(dir_hnd->offset);
1540 /*******************************************************************
1541 Add an entry into the dcache.
1542 ********************************************************************/
1544 static void DirCacheAdd(struct smb_Dir *dir_hnd, const char *name, long offset)
1546 struct name_cache_entry *e;
1548 if (dir_hnd->name_cache_size == 0) {
1552 if (dir_hnd->name_cache == NULL) {
1553 dir_hnd->name_cache = talloc_zero_array(dir_hnd,
1554 struct name_cache_entry,
1555 dir_hnd->name_cache_size);
1557 if (dir_hnd->name_cache == NULL) {
1562 dir_hnd->name_cache_index = (dir_hnd->name_cache_index+1) %
1563 dir_hnd->name_cache_size;
1564 e = &dir_hnd->name_cache[dir_hnd->name_cache_index];
1565 TALLOC_FREE(e->name);
1566 e->name = talloc_strdup(dir_hnd, name);
1570 /*******************************************************************
1571 Find an entry by name. Leave us at the offset after it.
1572 Don't check for veto or invisible files.
1573 ********************************************************************/
1575 bool SearchDir(struct smb_Dir *dir_hnd, const char *name, long *poffset)
1578 const char *entry = NULL;
1579 char *talloced = NULL;
1580 connection_struct *conn = dir_hnd->conn;
1582 /* Search back in the name cache. */
1583 if (dir_hnd->name_cache_size && dir_hnd->name_cache) {
1584 for (i = dir_hnd->name_cache_index; i >= 0; i--) {
1585 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1586 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1587 *poffset = e->offset;
1588 SeekDir(dir_hnd, e->offset);
1592 for (i = dir_hnd->name_cache_size - 1;
1593 i > dir_hnd->name_cache_index; i--) {
1594 struct name_cache_entry *e = &dir_hnd->name_cache[i];
1595 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1596 *poffset = e->offset;
1597 SeekDir(dir_hnd, e->offset);
1603 /* Not found in the name cache. Rewind directory and start from scratch. */
1604 SMB_VFS_REWINDDIR(conn, dir_hnd->dir);
1605 dir_hnd->file_number = 0;
1606 *poffset = START_OF_DIRECTORY_OFFSET;
1607 while ((entry = ReadDirName(dir_hnd, poffset, NULL, &talloced))) {
1608 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1609 TALLOC_FREE(talloced);
1612 TALLOC_FREE(talloced);
1617 struct files_below_forall_state {
1620 int (*fn)(struct file_id fid, const struct share_mode_data *data,
1621 void *private_data);
1625 static int files_below_forall_fn(struct file_id fid,
1626 const struct share_mode_data *data,
1629 struct files_below_forall_state *state = private_data;
1630 char tmpbuf[PATH_MAX];
1631 char *fullpath, *to_free;
1634 len = full_path_tos(data->servicepath, data->base_name,
1635 tmpbuf, sizeof(tmpbuf),
1636 &fullpath, &to_free);
1640 if (state->dirpath_len >= len) {
1642 * Filter files above dirpath
1646 if (fullpath[state->dirpath_len] != '/') {
1648 * Filter file that don't have a path separator at the end of
1654 if (memcmp(state->dirpath, fullpath, state->dirpath_len) != 0) {
1661 TALLOC_FREE(to_free);
1662 return state->fn(fid, data, state->private_data);
1665 TALLOC_FREE(to_free);
1669 static int files_below_forall(connection_struct *conn,
1670 const struct smb_filename *dir_name,
1671 int (*fn)(struct file_id fid,
1672 const struct share_mode_data *data,
1673 void *private_data),
1676 struct files_below_forall_state state = {
1678 .private_data = private_data,
1681 char tmpbuf[PATH_MAX];
1684 state.dirpath_len = full_path_tos(conn->connectpath,
1685 dir_name->base_name,
1686 tmpbuf, sizeof(tmpbuf),
1687 &state.dirpath, &to_free);
1688 if (state.dirpath_len == -1) {
1692 ret = share_mode_forall(files_below_forall_fn, &state);
1693 TALLOC_FREE(to_free);
1697 struct have_file_open_below_state {
1701 static int have_file_open_below_fn(struct file_id fid,
1702 const struct share_mode_data *data,
1705 struct have_file_open_below_state *state = private_data;
1706 state->found_one = true;
1710 bool have_file_open_below(connection_struct *conn,
1711 const struct smb_filename *name)
1713 struct have_file_open_below_state state = {
1718 if (!VALID_STAT(name->st)) {
1721 if (!S_ISDIR(name->st.st_ex_mode)) {
1725 ret = files_below_forall(conn, name, have_file_open_below_fn, &state);
1730 return state.found_one;
1733 /*****************************************************************
1734 Is this directory empty ?
1735 *****************************************************************/
1737 NTSTATUS can_delete_directory_fsp(files_struct *fsp)
1739 NTSTATUS status = NT_STATUS_OK;
1741 const char *dname = NULL;
1742 char *talloced = NULL;
1744 struct connection_struct *conn = fsp->conn;
1745 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(),
1752 return map_nt_error_from_unix(errno);
1755 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
1756 /* Quick check for "." and ".." */
1757 if (dname[0] == '.') {
1758 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1759 TALLOC_FREE(talloced);
1764 if (!is_visible_file(conn,
1769 TALLOC_FREE(talloced);
1773 DEBUG(10,("got name %s - can't delete\n",
1775 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1778 TALLOC_FREE(talloced);
1779 TALLOC_FREE(dir_hnd);
1781 if (!NT_STATUS_IS_OK(status)) {
1785 if (!(fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) &&
1786 lp_strict_rename(SNUM(conn)) &&
1787 have_file_open_below(fsp->conn, fsp->fsp_name))
1789 return NT_STATUS_ACCESS_DENIED;
1792 return NT_STATUS_OK;