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"
29 This module implements directory related functions for Samba.
32 /* "Special" directory offsets. */
33 #define END_OF_DIRECTORY_OFFSET ((long)-1)
34 #define START_OF_DIRECTORY_OFFSET ((long)0)
35 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
37 /* Make directory handle internals available. */
39 struct name_cache_entry {
45 connection_struct *conn;
49 size_t name_cache_size;
50 struct name_cache_entry *name_cache;
51 unsigned int name_cache_index;
52 unsigned int file_number;
56 struct dptr_struct *next, *prev;
59 struct connection_struct *conn;
60 struct smb_Dir *dir_hnd;
65 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
66 bool did_stat; /* Optimisation for non-wcard searches. */
69 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
74 #define INVALID_DPTR_KEY (-3)
76 /****************************************************************************
78 ****************************************************************************/
80 bool make_dir_struct(TALLOC_CTX *ctx,
90 char *mask2 = talloc_strdup(ctx, mask);
96 if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
100 memset(buf+1,' ',11);
101 if ((p = strchr_m(mask2,'.')) != NULL) {
103 push_ascii(buf+1,mask2,8, 0);
104 push_ascii(buf+9,p+1,3, 0);
107 push_ascii(buf+1,mask2,11, 0);
110 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
112 srv_put_dos_date(buf,22,date);
113 SSVAL(buf,26,size & 0xFFFF);
114 SSVAL(buf,28,(size >> 16)&0xFFFF);
115 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
116 Strange, but verified on W2K3. Needed for OS/2. JRA. */
117 push_ascii(buf+30,fname,12, uc ? STR_UPPER : 0);
118 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
122 /****************************************************************************
123 Initialise the dir bitmap.
124 ****************************************************************************/
126 bool init_dptrs(struct smbd_server_connection *sconn)
128 if (sconn->searches.dptr_bmap) {
132 sconn->searches.dptr_bmap = bitmap_talloc(
133 sconn, MAX_DIRECTORY_HANDLES);
135 if (sconn->searches.dptr_bmap == NULL) {
142 /****************************************************************************
143 Idle a dptr - the directory is closed but the control info is kept.
144 ****************************************************************************/
146 static void dptr_idle(struct dptr_struct *dptr)
149 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
150 TALLOC_FREE(dptr->dir_hnd);
154 /****************************************************************************
155 Idle the oldest dptr.
156 ****************************************************************************/
158 static void dptr_idleoldest(struct smbd_server_connection *sconn)
160 struct dptr_struct *dptr;
163 * Go to the end of the list.
165 dptr = DLIST_TAIL(sconn->searches.dirptrs);
168 DEBUG(0,("No dptrs available to idle ?\n"));
173 * Idle the oldest pointer.
176 for(; dptr; dptr = DLIST_PREV(dptr)) {
184 /****************************************************************************
185 Get the struct dptr_struct for a dir index.
186 ****************************************************************************/
188 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
189 int key, bool forclose)
191 struct dptr_struct *dptr;
193 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
194 if(dptr->dnum == key) {
195 if (!forclose && !dptr->dir_hnd) {
196 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
197 dptr_idleoldest(sconn);
198 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
199 if (!(dptr->dir_hnd = OpenDir(
200 NULL, dptr->conn, dptr->path,
201 dptr->wcard, dptr->attr))) {
202 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
207 DLIST_PROMOTE(sconn->searches.dirptrs,dptr);
214 /****************************************************************************
215 Get the dir path for a dir index.
216 ****************************************************************************/
218 char *dptr_path(struct smbd_server_connection *sconn, int key)
220 struct dptr_struct *dptr = dptr_get(sconn, key, false);
226 /****************************************************************************
227 Get the dir wcard for a dir index.
228 ****************************************************************************/
230 char *dptr_wcard(struct smbd_server_connection *sconn, int key)
232 struct dptr_struct *dptr = dptr_get(sconn, key, false);
238 /****************************************************************************
239 Get the dir attrib for a dir index.
240 ****************************************************************************/
242 uint16 dptr_attr(struct smbd_server_connection *sconn, int key)
244 struct dptr_struct *dptr = dptr_get(sconn, key, false);
250 /****************************************************************************
251 Close a dptr (internal func).
252 ****************************************************************************/
254 static void dptr_close_internal(struct dptr_struct *dptr)
256 struct smbd_server_connection *sconn = dptr->conn->sconn;
258 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
264 DLIST_REMOVE(sconn->searches.dirptrs, dptr);
267 * Free the dnum in the bitmap. Remember the dnum value is always
268 * biased by one with respect to the bitmap.
271 if (!bitmap_query(sconn->searches.dptr_bmap, dptr->dnum - 1)) {
272 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
276 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
279 TALLOC_FREE(dptr->dir_hnd);
281 /* Lanman 2 specific code */
282 SAFE_FREE(dptr->wcard);
283 string_set(&dptr->path,"");
287 /****************************************************************************
288 Close a dptr given a key.
289 ****************************************************************************/
291 void dptr_close(struct smbd_server_connection *sconn, int *key)
293 struct dptr_struct *dptr;
295 if(*key == INVALID_DPTR_KEY)
298 /* OS/2 seems to use -1 to indicate "close all directories" */
300 struct dptr_struct *next;
301 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
303 dptr_close_internal(dptr);
305 *key = INVALID_DPTR_KEY;
309 dptr = dptr_get(sconn, *key, true);
312 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
316 dptr_close_internal(dptr);
318 *key = INVALID_DPTR_KEY;
321 /****************************************************************************
322 Close all dptrs for a cnum.
323 ****************************************************************************/
325 void dptr_closecnum(connection_struct *conn)
327 struct dptr_struct *dptr, *next;
328 struct smbd_server_connection *sconn = conn->sconn;
334 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
336 if (dptr->conn == conn) {
337 dptr_close_internal(dptr);
342 /****************************************************************************
343 Idle all dptrs for a cnum.
344 ****************************************************************************/
346 void dptr_idlecnum(connection_struct *conn)
348 struct dptr_struct *dptr;
349 struct smbd_server_connection *sconn = conn->sconn;
355 for(dptr = sconn->searches.dirptrs; dptr; dptr = dptr->next) {
356 if (dptr->conn == conn && dptr->dir_hnd) {
362 /****************************************************************************
363 Close a dptr that matches a given path, only if it matches the spid also.
364 ****************************************************************************/
366 void dptr_closepath(struct smbd_server_connection *sconn,
367 char *path,uint16 spid)
369 struct dptr_struct *dptr, *next;
370 for(dptr = sconn->searches.dirptrs; dptr; dptr = next) {
372 if (spid == dptr->spid && strequal(dptr->path,path))
373 dptr_close_internal(dptr);
377 /****************************************************************************
378 Try and close the oldest handle not marked for
379 expect close in the hope that the client has
380 finished with that one.
381 ****************************************************************************/
383 static void dptr_close_oldest(struct smbd_server_connection *sconn,
386 struct dptr_struct *dptr;
389 * Go to the end of the list.
391 for(dptr = sconn->searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
395 DEBUG(0,("No old dptrs available to close oldest ?\n"));
400 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
401 * does not have expect_close set. If 'old' is false, close
402 * one of the new dnum handles.
405 for(; dptr; dptr = DLIST_PREV(dptr)) {
406 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
407 (!old && (dptr->dnum > 255))) {
408 dptr_close_internal(dptr);
414 /****************************************************************************
415 Create a new dir ptr. If the flag old_handle is true then we must allocate
416 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
417 one byte long. If old_handle is false we allocate from the range
418 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
419 a directory handle is never zero.
420 wcard must not be zero.
421 ****************************************************************************/
423 NTSTATUS dptr_create(connection_struct *conn, files_struct *fsp,
424 const char *path, bool old_handle, bool expect_close,uint16 spid,
425 const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
427 struct smbd_server_connection *sconn = conn->sconn;
428 struct dptr_struct *dptr = NULL;
429 struct smb_Dir *dir_hnd;
431 if (fsp && fsp->is_directory && fsp->fh->fd != -1) {
432 path = fsp->fsp_name->base_name;
435 DEBUG(5,("dptr_create dir=%s\n", path));
438 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
439 return NT_STATUS_INTERNAL_ERROR;
443 return NT_STATUS_INVALID_PARAMETER;
447 if (!(fsp->access_mask & SEC_DIR_LIST)) {
448 DEBUG(5,("dptr_create: directory %s "
449 "not open for LIST access\n",
451 return NT_STATUS_ACCESS_DENIED;
453 dir_hnd = OpenDir_fsp(NULL, conn, fsp, wcard, attr);
455 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
459 return map_nt_error_from_unix(errno);
462 if (sconn->searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
463 dptr_idleoldest(sconn);
466 dptr = SMB_MALLOC_P(struct dptr_struct);
468 DEBUG(0,("malloc fail in dptr_create.\n"));
469 TALLOC_FREE(dir_hnd);
470 return NT_STATUS_NO_MEMORY;
478 * This is an old-style SMBsearch request. Ensure the
479 * value we return will fit in the range 1-255.
482 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
484 if(dptr->dnum == -1 || dptr->dnum > 254) {
487 * Try and close the oldest handle not marked for
488 * expect close in the hope that the client has
489 * finished with that one.
492 dptr_close_oldest(sconn, true);
494 /* Now try again... */
495 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 0);
496 if(dptr->dnum == -1 || dptr->dnum > 254) {
497 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
499 TALLOC_FREE(dir_hnd);
500 return NT_STATUS_TOO_MANY_OPENED_FILES;
506 * This is a new-style trans2 request. Allocate from
507 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
510 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
512 if(dptr->dnum == -1 || dptr->dnum < 255) {
515 * Try and close the oldest handle close in the hope that
516 * the client has finished with that one. This will only
517 * happen in the case of the Win98 client bug where it leaks
521 dptr_close_oldest(sconn, false);
523 /* Now try again... */
524 dptr->dnum = bitmap_find(sconn->searches.dptr_bmap, 255);
526 if(dptr->dnum == -1 || dptr->dnum < 255) {
527 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
529 TALLOC_FREE(dir_hnd);
530 return NT_STATUS_TOO_MANY_OPENED_FILES;
535 bitmap_set(sconn->searches.dptr_bmap, dptr->dnum);
537 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
539 string_set(&dptr->path,path);
541 dptr->dir_hnd = dir_hnd;
543 dptr->expect_close = expect_close;
544 dptr->wcard = SMB_STRDUP(wcard);
546 bitmap_clear(sconn->searches.dptr_bmap, dptr->dnum - 1);
548 TALLOC_FREE(dir_hnd);
549 return NT_STATUS_NO_MEMORY;
551 if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
552 dptr->has_wild = True;
554 dptr->has_wild = wcard_has_wild;
559 DLIST_ADD(sconn->searches.dirptrs, dptr);
561 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
562 dptr->dnum,path,expect_close));
570 /****************************************************************************
571 Wrapper functions to access the lower level directory handles.
572 ****************************************************************************/
574 void dptr_CloseDir(files_struct *fsp)
578 * Ugly hack. We have defined fdopendir to return ENOSYS if dirfd also isn't
579 * present. I hate Solaris. JRA.
582 if (fsp->fh->fd != -1 &&
583 fsp->dptr->dir_hnd &&
584 dirfd(fsp->dptr->dir_hnd->dir)) {
585 /* The call below closes the underlying fd. */
589 dptr_close_internal(fsp->dptr);
594 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
596 SeekDir(dptr->dir_hnd, offset);
599 long dptr_TellDir(struct dptr_struct *dptr)
601 return TellDir(dptr->dir_hnd);
604 bool dptr_has_wild(struct dptr_struct *dptr)
606 return dptr->has_wild;
609 int dptr_dnum(struct dptr_struct *dptr)
614 /****************************************************************************
615 Return the next visible file name, skipping veto'd and invisible files.
616 ****************************************************************************/
618 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
619 long *poffset, SMB_STRUCT_STAT *pst,
622 /* Normal search for the next file. */
624 char *talloced = NULL;
626 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst, &talloced))
628 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
629 *ptalloced = talloced;
632 TALLOC_FREE(talloced);
637 /****************************************************************************
638 Return the next visible file name, skipping veto'd and invisible files.
639 ****************************************************************************/
641 char *dptr_ReadDirName(TALLOC_CTX *ctx,
642 struct dptr_struct *dptr,
644 SMB_STRUCT_STAT *pst)
646 struct smb_filename smb_fname_base;
648 const char *name_temp = NULL;
649 char *talloced = NULL;
650 char *pathreal = NULL;
651 char *found_name = NULL;
654 SET_STAT_INVALID(*pst);
656 if (dptr->has_wild || dptr->did_stat) {
657 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst,
659 if (name_temp == NULL) {
662 if (talloced != NULL) {
663 return talloc_move(ctx, &talloced);
665 return talloc_strdup(ctx, name_temp);
668 /* If poffset is -1 then we know we returned this name before and we
669 * have no wildcards. We're at the end of the directory. */
670 if (*poffset == END_OF_DIRECTORY_OFFSET) {
674 /* We know the stored wcard contains no wildcard characters.
675 * See if we can match with a stat call. If we can't, then set
676 * did_stat to true to ensure we only do this once and keep
679 dptr->did_stat = true;
681 /* First check if it should be visible. */
682 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
685 /* This only returns false if the file was found, but
686 is explicitly not visible. Set us to end of
687 directory, but return NULL as we know we can't ever
692 if (VALID_STAT(*pst)) {
693 name = talloc_strdup(ctx, dptr->wcard);
697 pathreal = talloc_asprintf(ctx,
704 /* Create an smb_filename with stream_name == NULL. */
705 ZERO_STRUCT(smb_fname_base);
706 smb_fname_base.base_name = pathreal;
708 if (SMB_VFS_STAT(dptr->conn, &smb_fname_base) == 0) {
709 *pst = smb_fname_base.st;
710 name = talloc_strdup(ctx, dptr->wcard);
713 /* If we get any other error than ENOENT or ENOTDIR
714 then the file exists we just can't stat it. */
715 if (errno != ENOENT && errno != ENOTDIR) {
716 name = talloc_strdup(ctx, dptr->wcard);
721 /* Stat failed. We know this is authoratiative if we are
722 * providing case sensitive semantics or the underlying
723 * filesystem is case sensitive.
725 if (dptr->conn->case_sensitive ||
726 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
732 * Try case-insensitive stat if the fs has the ability. This avoids
733 * scanning the whole directory.
735 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
740 } else if (errno == ENOENT) {
741 /* The case-insensitive lookup was authoritative. */
745 TALLOC_FREE(pathreal);
747 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst, &talloced);
748 if (name_temp == NULL) {
751 if (talloced != NULL) {
752 return talloc_move(ctx, &talloced);
754 return talloc_strdup(ctx, name_temp);
757 TALLOC_FREE(pathreal);
759 /* We need to set the underlying dir_hnd offset to -1
760 * also as this function is usually called with the
761 * output from TellDir. */
762 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
766 /****************************************************************************
767 Search for a file by name, skipping veto'ed and not visible files.
768 ****************************************************************************/
770 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
772 SET_STAT_INVALID(*pst);
774 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
775 /* This is a singleton directory and we're already at the end. */
776 *poffset = END_OF_DIRECTORY_OFFSET;
780 return SearchDir(dptr->dir_hnd, name, poffset);
783 /****************************************************************************
784 Add the name we're returning into the underlying cache.
785 ****************************************************************************/
787 void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
789 DirCacheAdd(dptr->dir_hnd, name, offset);
792 /****************************************************************************
793 Initialize variables & state data at the beginning of all search SMB requests.
794 ****************************************************************************/
795 void dptr_init_search_op(struct dptr_struct *dptr)
797 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
800 /****************************************************************************
801 Fill the 5 byte server reserved dptr field.
802 ****************************************************************************/
804 bool dptr_fill(struct smbd_server_connection *sconn,
805 char *buf1,unsigned int key)
807 unsigned char *buf = (unsigned char *)buf1;
808 struct dptr_struct *dptr = dptr_get(sconn, key, false);
811 DEBUG(1,("filling null dirptr %d\n",key));
814 offset = (uint32)TellDir(dptr->dir_hnd);
815 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
816 (long)dptr->dir_hnd,(int)offset));
822 /****************************************************************************
823 Fetch the dir ptr and seek it given the 5 byte server field.
824 ****************************************************************************/
826 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
829 unsigned int key = *(unsigned char *)buf;
830 struct dptr_struct *dptr = dptr_get(sconn, key, false);
835 DEBUG(3,("fetched null dirptr %d\n",key));
839 offset = IVAL(buf,1);
840 if (offset == (uint32)-1) {
841 seekoff = END_OF_DIRECTORY_OFFSET;
843 seekoff = (long)offset;
845 SeekDir(dptr->dir_hnd,seekoff);
846 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
847 key, dptr->path, (int)seekoff));
851 /****************************************************************************
853 ****************************************************************************/
855 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
858 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
861 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
864 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
868 /****************************************************************************
869 Check that a file matches a particular file type.
870 ****************************************************************************/
872 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
876 /* Check the "may have" search bits. */
877 if (((mode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)) != 0)
880 /* Check the "must have" bits, which are the may have bits shifted eight */
881 /* If must have bit is set, the file/dir can not be returned in search unless the matching
882 file attribute is set */
883 mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
885 if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))) == mask) /* check if matching attribute present */
894 static bool mangle_mask_match(connection_struct *conn,
895 const char *filename,
900 if (!name_to_8_3(filename,mname,False,conn->params)) {
903 return mask_match_search(mname,mask,False);
906 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
907 struct dptr_struct *dirptr,
912 bool (*match_fn)(TALLOC_CTX *ctx,
917 bool (*mode_fn)(TALLOC_CTX *ctx,
919 struct smb_filename *smb_fname,
923 struct smb_filename **_smb_fname,
927 connection_struct *conn = dirptr->conn;
933 needslash = ( dirptr->path[strlen(dirptr->path) -1] != '/');
938 SMB_STRUCT_STAT sbuf;
942 char *pathreal = NULL;
943 struct smb_filename smb_fname;
948 cur_offset = dptr_TellDir(dirptr);
949 prev_offset = cur_offset;
950 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
952 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
953 (long)dirptr, cur_offset));
959 isdots = (ISDOT(dname) || ISDOTDOT(dname));
960 if (dont_descend && !isdots) {
966 * fname may get mangled, dname is never mangled.
967 * Whenever we're accessing the filesystem we use
968 * pathreal which is composed from dname.
971 ok = match_fn(ctx, private_data, dname, mask, &fname);
977 pathreal = talloc_asprintf(ctx, "%s%s%s",
987 /* Create smb_fname with NULL stream_name. */
988 ZERO_STRUCT(smb_fname);
989 smb_fname.base_name = pathreal;
992 ok = mode_fn(ctx, private_data, &smb_fname, &mode);
996 TALLOC_FREE(pathreal);
1000 if (!dir_check_ftype(conn, mode, dirtype)) {
1001 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
1002 fname, (unsigned int)mode, (unsigned int)dirtype));
1005 TALLOC_FREE(pathreal);
1009 if (ask_sharemode) {
1010 struct timespec write_time_ts;
1011 struct file_id fileid;
1013 fileid = vfs_file_id_from_sbuf(conn,
1015 get_file_infos(fileid, 0, NULL, &write_time_ts);
1016 if (!null_timespec(write_time_ts)) {
1017 update_stat_ex_mtime(&smb_fname.st,
1022 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
1024 mask, smb_fname_str_dbg(&smb_fname),
1027 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
1031 status = copy_smb_filename(ctx, &smb_fname, _smb_fname);
1032 TALLOC_FREE(pathreal);
1033 if (!NT_STATUS_IS_OK(status)) {
1038 *_prev_offset = prev_offset;
1046 /****************************************************************************
1047 Get an 8.3 directory entry.
1048 ****************************************************************************/
1050 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1056 connection_struct *conn = (connection_struct *)private_data;
1058 if ((strcmp(mask,"*.*") == 0) ||
1059 mask_match_search(dname, mask, false) ||
1060 mangle_mask_match(conn, dname, mask)) {
1064 if (!mangle_is_8_3(dname, false, conn->params)) {
1065 bool ok = name_to_8_3(dname, mname, false,
1075 *_fname = talloc_strdup(ctx, fname);
1076 if (*_fname == NULL) {
1086 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1088 struct smb_filename *smb_fname,
1091 connection_struct *conn = (connection_struct *)private_data;
1093 if (!VALID_STAT(smb_fname->st)) {
1094 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1095 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1096 "Couldn't stat [%s]. Error "
1098 smb_fname_str_dbg(smb_fname),
1104 *_mode = dos_mode(conn, smb_fname);
1108 bool get_dir_entry(TALLOC_CTX *ctx,
1109 struct dptr_struct *dirptr,
1115 struct timespec *_date,
1119 connection_struct *conn = dirptr->conn;
1121 struct smb_filename *smb_fname = NULL;
1126 ok = smbd_dirptr_get_entry(ctx,
1132 smbd_dirptr_8_3_match_fn,
1133 smbd_dirptr_8_3_mode_fn,
1143 *_fname = talloc_move(ctx, &fname);
1144 *_size = smb_fname->st.st_ex_size;
1146 *_date = smb_fname->st.st_ex_mtime;
1147 TALLOC_FREE(smb_fname);
1151 /*******************************************************************
1152 Check to see if a user can read a file. This is only approximate,
1153 it is used as part of the "hide unreadable" option. Don't
1154 use it for anything security sensitive.
1155 ********************************************************************/
1157 static bool user_can_read_file(connection_struct *conn,
1158 struct smb_filename *smb_fname)
1161 * Never hide files from the root user.
1162 * We use (uid_t)0 here not sec_initial_uid()
1163 * as make test uses a single user context.
1166 if (get_current_uid(conn) == (uid_t)0) {
1170 return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
1175 /*******************************************************************
1176 Check to see if a user can write a file (and only files, we do not
1177 check dirs on this one). This is only approximate,
1178 it is used as part of the "hide unwriteable" option. Don't
1179 use it for anything security sensitive.
1180 ********************************************************************/
1182 static bool user_can_write_file(connection_struct *conn,
1183 const struct smb_filename *smb_fname)
1186 * Never hide files from the root user.
1187 * We use (uid_t)0 here not sec_initial_uid()
1188 * as make test uses a single user context.
1191 if (get_current_uid(conn) == (uid_t)0) {
1195 SMB_ASSERT(VALID_STAT(smb_fname->st));
1197 /* Pseudo-open the file */
1199 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1203 return can_write_to_file(conn, smb_fname);
1206 /*******************************************************************
1207 Is a file a "special" type ?
1208 ********************************************************************/
1210 static bool file_is_special(connection_struct *conn,
1211 const struct smb_filename *smb_fname)
1214 * Never hide files from the root user.
1215 * We use (uid_t)0 here not sec_initial_uid()
1216 * as make test uses a single user context.
1219 if (get_current_uid(conn) == (uid_t)0) {
1223 SMB_ASSERT(VALID_STAT(smb_fname->st));
1225 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1226 S_ISDIR(smb_fname->st.st_ex_mode) ||
1227 S_ISLNK(smb_fname->st.st_ex_mode))
1233 /*******************************************************************
1234 Should the file be seen by the client?
1235 NOTE: A successful return is no guarantee of the file's existence.
1236 ********************************************************************/
1238 bool is_visible_file(connection_struct *conn, const char *dir_path,
1239 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1241 bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1242 bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1243 bool hide_special = lp_hide_special_files(SNUM(conn));
1245 struct smb_filename *smb_fname_base = NULL;
1249 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1250 return True; /* . and .. are always visible. */
1253 /* If it's a vetoed file, pretend it doesn't even exist */
1254 if (use_veto && IS_VETO_PATH(conn, name)) {
1255 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1259 if (hide_unreadable || hide_unwriteable || hide_special) {
1260 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1266 /* Create an smb_filename with stream_name == NULL. */
1267 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1268 pst, &smb_fname_base);
1269 if (!NT_STATUS_IS_OK(status)) {
1274 /* If the file name does not exist, there's no point checking
1275 * the configuration options. We succeed, on the basis that the
1276 * checks *might* have passed if the file was present.
1278 if (!VALID_STAT(*pst)) {
1279 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1283 *pst = smb_fname_base->st;
1287 /* Honour _hide unreadable_ option */
1288 if (hide_unreadable &&
1289 !user_can_read_file(conn, smb_fname_base)) {
1290 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1295 /* Honour _hide unwriteable_ option */
1296 if (hide_unwriteable && !user_can_write_file(conn,
1298 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1303 /* Honour _hide_special_ option */
1304 if (hide_special && file_is_special(conn, smb_fname_base)) {
1305 DEBUG(10,("is_visible_file: file %s is special.\n",
1314 TALLOC_FREE(smb_fname_base);
1319 static int smb_Dir_destructor(struct smb_Dir *dirp)
1323 if (dirp->conn->sconn) {
1324 files_struct *fsp = file_find_fd(dirp->conn->sconn,
1327 /* The call below closes the underlying fd. */
1332 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1334 if (dirp->conn->sconn) {
1335 dirp->conn->sconn->searches.dirhandles_open--;
1340 /*******************************************************************
1342 ********************************************************************/
1344 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1349 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1350 struct smbd_server_connection *sconn = conn->sconn;
1357 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1359 dirp->dir_path = talloc_strdup(dirp, name);
1360 if (!dirp->dir_path) {
1366 sconn->searches.dirhandles_open++;
1368 talloc_set_destructor(dirp, smb_Dir_destructor);
1370 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1372 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1384 /*******************************************************************
1385 Open a directory from an fsp.
1386 ********************************************************************/
1388 static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, connection_struct *conn,
1393 struct smb_Dir *dirp = talloc_zero(mem_ctx, struct smb_Dir);
1394 struct smbd_server_connection *sconn = conn->sconn;
1401 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1403 dirp->dir_path = talloc_strdup(dirp, fsp->fsp_name->base_name);
1404 if (!dirp->dir_path) {
1410 sconn->searches.dirhandles_open++;
1412 talloc_set_destructor(dirp, smb_Dir_destructor);
1414 if (fsp->is_directory && fsp->fh->fd != -1) {
1415 dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
1416 if (dirp->dir == NULL) {
1417 DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
1421 if (errno != ENOSYS) {
1427 if (dirp->dir == NULL) {
1428 /* FDOPENDIR didn't work. Use OPENDIR instead. */
1429 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1433 DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n", dirp->dir_path,
1446 /*******************************************************************
1447 Read from a directory.
1448 Return directory entry, current offset, and optional stat information.
1449 Don't check for veto or invisible files.
1450 ********************************************************************/
1452 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1453 SMB_STRUCT_STAT *sbuf, char **ptalloced)
1456 char *talloced = NULL;
1457 connection_struct *conn = dirp->conn;
1459 /* Cheat to allow . and .. to be the first entries returned. */
1460 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1461 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1463 if (dirp->file_number == 0) {
1465 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1468 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1470 dirp->file_number++;
1473 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1474 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1477 /* A real offset, seek to it. */
1478 SeekDir(dirp, *poffset);
1481 while ((n = vfs_readdirname(conn, dirp->dir, sbuf, &talloced))) {
1482 /* Ignore . and .. - we've already returned them. */
1484 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1485 TALLOC_FREE(talloced);
1489 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1490 *ptalloced = talloced;
1491 dirp->file_number++;
1494 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1499 /*******************************************************************
1500 Rewind to the start.
1501 ********************************************************************/
1503 void RewindDir(struct smb_Dir *dirp, long *poffset)
1505 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1506 dirp->file_number = 0;
1507 dirp->offset = START_OF_DIRECTORY_OFFSET;
1508 *poffset = START_OF_DIRECTORY_OFFSET;
1511 /*******************************************************************
1513 ********************************************************************/
1515 void SeekDir(struct smb_Dir *dirp, long offset)
1517 if (offset != dirp->offset) {
1518 if (offset == START_OF_DIRECTORY_OFFSET) {
1519 RewindDir(dirp, &offset);
1521 * Ok we should really set the file number here
1522 * to 1 to enable ".." to be returned next. Trouble
1523 * is I'm worried about callers using SeekDir(dirp,0)
1524 * as equivalent to RewindDir(). So leave this alone
1527 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1528 RewindDir(dirp, &offset);
1530 * Set the file number to 2 - we want to get the first
1531 * real file entry (the one we return after "..")
1532 * on the next ReadDir.
1534 dirp->file_number = 2;
1535 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1536 ; /* Don't seek in this case. */
1538 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1540 dirp->offset = offset;
1544 /*******************************************************************
1545 Tell a dir position.
1546 ********************************************************************/
1548 long TellDir(struct smb_Dir *dirp)
1550 return(dirp->offset);
1553 /*******************************************************************
1554 Add an entry into the dcache.
1555 ********************************************************************/
1557 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1559 struct name_cache_entry *e;
1561 if (dirp->name_cache_size == 0) {
1565 if (dirp->name_cache == NULL) {
1566 dirp->name_cache = talloc_zero_array(
1567 dirp, struct name_cache_entry, dirp->name_cache_size);
1569 if (dirp->name_cache == NULL) {
1574 dirp->name_cache_index = (dirp->name_cache_index+1) %
1575 dirp->name_cache_size;
1576 e = &dirp->name_cache[dirp->name_cache_index];
1577 TALLOC_FREE(e->name);
1578 e->name = talloc_strdup(dirp, name);
1582 /*******************************************************************
1583 Find an entry by name. Leave us at the offset after it.
1584 Don't check for veto or invisible files.
1585 ********************************************************************/
1587 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1590 const char *entry = NULL;
1591 char *talloced = NULL;
1592 connection_struct *conn = dirp->conn;
1594 /* Search back in the name cache. */
1595 if (dirp->name_cache_size && dirp->name_cache) {
1596 for (i = dirp->name_cache_index; i >= 0; i--) {
1597 struct name_cache_entry *e = &dirp->name_cache[i];
1598 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1599 *poffset = e->offset;
1600 SeekDir(dirp, e->offset);
1604 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1605 struct name_cache_entry *e = &dirp->name_cache[i];
1606 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1607 *poffset = e->offset;
1608 SeekDir(dirp, e->offset);
1614 /* Not found in the name cache. Rewind directory and start from scratch. */
1615 SMB_VFS_REWINDDIR(conn, dirp->dir);
1616 dirp->file_number = 0;
1617 *poffset = START_OF_DIRECTORY_OFFSET;
1618 while ((entry = ReadDirName(dirp, poffset, NULL, &talloced))) {
1619 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1620 TALLOC_FREE(talloced);
1623 TALLOC_FREE(talloced);
1628 /*****************************************************************
1629 Is this directory empty ?
1630 *****************************************************************/
1632 NTSTATUS smbd_can_delete_directory(struct connection_struct *conn,
1633 const char *dirname)
1635 NTSTATUS status = NT_STATUS_OK;
1637 const char *dname = NULL;
1638 char *talloced = NULL;
1640 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
1644 return map_nt_error_from_unix(errno);
1647 while ((dname = ReadDirName(dir_hnd, &dirpos, &st, &talloced))) {
1648 /* Quick check for "." and ".." */
1649 if (dname[0] == '.') {
1650 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1651 TALLOC_FREE(talloced);
1656 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1657 TALLOC_FREE(talloced);
1661 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1663 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1666 TALLOC_FREE(talloced);
1667 TALLOC_FREE(dir_hnd);