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 "smbd/globals.h"
25 This module implements directory related functions for Samba.
28 /* "Special" directory offsets. */
29 #define END_OF_DIRECTORY_OFFSET ((long)-1)
30 #define START_OF_DIRECTORY_OFFSET ((long)0)
31 #define DOT_DOT_DIRECTORY_OFFSET ((long)0x80000000)
33 /* Make directory handle internals available. */
35 struct name_cache_entry {
41 connection_struct *conn;
45 size_t name_cache_size;
46 struct name_cache_entry *name_cache;
47 unsigned int name_cache_index;
48 unsigned int file_number;
52 struct dptr_struct *next, *prev;
55 struct connection_struct *conn;
56 struct smb_Dir *dir_hnd;
61 bool has_wild; /* Set to true if the wcard entry has MS wildcard characters in it. */
62 bool did_stat; /* Optimisation for non-wcard searches. */
66 #define INVALID_DPTR_KEY (-3)
68 /****************************************************************************
70 ****************************************************************************/
72 bool make_dir_struct(TALLOC_CTX *ctx,
82 char *mask2 = talloc_strdup(ctx, mask);
88 if ((mode & aDIR) != 0) {
93 if ((p = strchr_m(mask2,'.')) != NULL) {
95 push_ascii(buf+1,mask2,8, 0);
96 push_ascii(buf+9,p+1,3, 0);
99 push_ascii(buf+1,mask2,11, 0);
102 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
104 srv_put_dos_date(buf,22,date);
105 SSVAL(buf,26,size & 0xFFFF);
106 SSVAL(buf,28,(size >> 16)&0xFFFF);
107 /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
108 Strange, but verified on W2K3. Needed for OS/2. JRA. */
109 push_ascii(buf+30,fname,12, uc ? STR_UPPER : 0);
110 DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
114 /****************************************************************************
115 Initialise the dir bitmap.
116 ****************************************************************************/
118 void init_dptrs(void)
123 dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
126 exit_server("out of memory in init_dptrs");
129 /****************************************************************************
130 Idle a dptr - the directory is closed but the control info is kept.
131 ****************************************************************************/
133 static void dptr_idle(struct dptr_struct *dptr)
136 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
137 TALLOC_FREE(dptr->dir_hnd);
141 /****************************************************************************
142 Idle the oldest dptr.
143 ****************************************************************************/
145 static void dptr_idleoldest(void)
147 struct dptr_struct *dptr;
150 * Go to the end of the list.
152 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
156 DEBUG(0,("No dptrs available to idle ?\n"));
161 * Idle the oldest pointer.
164 for(; dptr; dptr = dptr->prev) {
172 /****************************************************************************
173 Get the struct dptr_struct for a dir index.
174 ****************************************************************************/
176 static struct dptr_struct *dptr_get(int key, bool forclose)
178 struct dptr_struct *dptr;
180 for(dptr = dirptrs; dptr; dptr = dptr->next) {
181 if(dptr->dnum == key) {
182 if (!forclose && !dptr->dir_hnd) {
183 if (dirhandles_open >= MAX_OPEN_DIRECTORIES)
185 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
186 if (!(dptr->dir_hnd = OpenDir(
187 NULL, dptr->conn, dptr->path,
188 dptr->wcard, dptr->attr))) {
189 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
194 DLIST_PROMOTE(dirptrs,dptr);
201 /****************************************************************************
202 Get the dir path for a dir index.
203 ****************************************************************************/
205 char *dptr_path(int key)
207 struct dptr_struct *dptr = dptr_get(key, False);
213 /****************************************************************************
214 Get the dir wcard for a dir index.
215 ****************************************************************************/
217 char *dptr_wcard(int key)
219 struct dptr_struct *dptr = dptr_get(key, False);
225 /****************************************************************************
226 Get the dir attrib for a dir index.
227 ****************************************************************************/
229 uint16 dptr_attr(int key)
231 struct dptr_struct *dptr = dptr_get(key, False);
237 /****************************************************************************
238 Close a dptr (internal func).
239 ****************************************************************************/
241 static void dptr_close_internal(struct dptr_struct *dptr)
243 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
245 DLIST_REMOVE(dirptrs, dptr);
248 * Free the dnum in the bitmap. Remember the dnum value is always
249 * biased by one with respect to the bitmap.
252 if(bitmap_query( dptr_bmap, dptr->dnum - 1) != True) {
253 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
257 bitmap_clear(dptr_bmap, dptr->dnum - 1);
259 TALLOC_FREE(dptr->dir_hnd);
261 /* Lanman 2 specific code */
262 SAFE_FREE(dptr->wcard);
263 string_set(&dptr->path,"");
267 /****************************************************************************
268 Close a dptr given a key.
269 ****************************************************************************/
271 void dptr_close(int *key)
273 struct dptr_struct *dptr;
275 if(*key == INVALID_DPTR_KEY)
278 /* OS/2 seems to use -1 to indicate "close all directories" */
280 struct dptr_struct *next;
281 for(dptr = dirptrs; dptr; dptr = next) {
283 dptr_close_internal(dptr);
285 *key = INVALID_DPTR_KEY;
289 dptr = dptr_get(*key, True);
292 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
296 dptr_close_internal(dptr);
298 *key = INVALID_DPTR_KEY;
301 /****************************************************************************
302 Close all dptrs for a cnum.
303 ****************************************************************************/
305 void dptr_closecnum(connection_struct *conn)
307 struct dptr_struct *dptr, *next;
308 for(dptr = dirptrs; dptr; dptr = next) {
310 if (dptr->conn == conn)
311 dptr_close_internal(dptr);
315 /****************************************************************************
316 Idle all dptrs for a cnum.
317 ****************************************************************************/
319 void dptr_idlecnum(connection_struct *conn)
321 struct dptr_struct *dptr;
322 for(dptr = dirptrs; dptr; dptr = dptr->next) {
323 if (dptr->conn == conn && dptr->dir_hnd)
328 /****************************************************************************
329 Close a dptr that matches a given path, only if it matches the spid also.
330 ****************************************************************************/
332 void dptr_closepath(char *path,uint16 spid)
334 struct dptr_struct *dptr, *next;
335 for(dptr = dirptrs; dptr; dptr = next) {
337 if (spid == dptr->spid && strequal(dptr->path,path))
338 dptr_close_internal(dptr);
342 /****************************************************************************
343 Try and close the oldest handle not marked for
344 expect close in the hope that the client has
345 finished with that one.
346 ****************************************************************************/
348 static void dptr_close_oldest(bool old)
350 struct dptr_struct *dptr;
353 * Go to the end of the list.
355 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
359 DEBUG(0,("No old dptrs available to close oldest ?\n"));
364 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
365 * does not have expect_close set. If 'old' is false, close
366 * one of the new dnum handles.
369 for(; dptr; dptr = dptr->prev) {
370 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
371 (!old && (dptr->dnum > 255))) {
372 dptr_close_internal(dptr);
378 /****************************************************************************
379 Create a new dir ptr. If the flag old_handle is true then we must allocate
380 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
381 one byte long. If old_handle is false we allocate from the range
382 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
383 a directory handle is never zero.
384 wcard must not be zero.
385 ****************************************************************************/
387 NTSTATUS dptr_create(connection_struct *conn, const char *path, bool old_handle, bool expect_close,uint16 spid,
388 const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
390 struct dptr_struct *dptr = NULL;
391 struct smb_Dir *dir_hnd;
394 DEBUG(5,("dptr_create dir=%s\n", path));
397 return NT_STATUS_INVALID_PARAMETER;
400 status = check_name(conn,path);
401 if (!NT_STATUS_IS_OK(status)) {
405 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
407 return map_nt_error_from_unix(errno);
410 string_set(&conn->dirpath,path);
412 if (dirhandles_open >= MAX_OPEN_DIRECTORIES) {
416 dptr = SMB_MALLOC_P(struct dptr_struct);
418 DEBUG(0,("malloc fail in dptr_create.\n"));
419 TALLOC_FREE(dir_hnd);
420 return NT_STATUS_NO_MEMORY;
428 * This is an old-style SMBsearch request. Ensure the
429 * value we return will fit in the range 1-255.
432 dptr->dnum = bitmap_find(dptr_bmap, 0);
434 if(dptr->dnum == -1 || dptr->dnum > 254) {
437 * Try and close the oldest handle not marked for
438 * expect close in the hope that the client has
439 * finished with that one.
442 dptr_close_oldest(True);
444 /* Now try again... */
445 dptr->dnum = bitmap_find(dptr_bmap, 0);
446 if(dptr->dnum == -1 || dptr->dnum > 254) {
447 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
449 TALLOC_FREE(dir_hnd);
450 return NT_STATUS_TOO_MANY_OPENED_FILES;
456 * This is a new-style trans2 request. Allocate from
457 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
460 dptr->dnum = bitmap_find(dptr_bmap, 255);
462 if(dptr->dnum == -1 || dptr->dnum < 255) {
465 * Try and close the oldest handle close in the hope that
466 * the client has finished with that one. This will only
467 * happen in the case of the Win98 client bug where it leaks
471 dptr_close_oldest(False);
473 /* Now try again... */
474 dptr->dnum = bitmap_find(dptr_bmap, 255);
476 if(dptr->dnum == -1 || dptr->dnum < 255) {
477 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
479 TALLOC_FREE(dir_hnd);
480 return NT_STATUS_TOO_MANY_OPENED_FILES;
485 bitmap_set(dptr_bmap, dptr->dnum);
487 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
489 string_set(&dptr->path,path);
491 dptr->dir_hnd = dir_hnd;
493 dptr->expect_close = expect_close;
494 dptr->wcard = SMB_STRDUP(wcard);
496 bitmap_clear(dptr_bmap, dptr->dnum - 1);
498 TALLOC_FREE(dir_hnd);
499 return NT_STATUS_NO_MEMORY;
501 if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
502 dptr->has_wild = True;
504 dptr->has_wild = wcard_has_wild;
509 DLIST_ADD(dirptrs, dptr);
511 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
512 dptr->dnum,path,expect_close));
520 /****************************************************************************
521 Wrapper functions to access the lower level directory handles.
522 ****************************************************************************/
524 int dptr_CloseDir(struct dptr_struct *dptr)
526 DLIST_REMOVE(dirptrs, dptr);
527 TALLOC_FREE(dptr->dir_hnd);
531 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
533 SeekDir(dptr->dir_hnd, offset);
536 long dptr_TellDir(struct dptr_struct *dptr)
538 return TellDir(dptr->dir_hnd);
541 bool dptr_has_wild(struct dptr_struct *dptr)
543 return dptr->has_wild;
546 int dptr_dnum(struct dptr_struct *dptr)
551 /****************************************************************************
552 Return the next visible file name, skipping veto'd and invisible files.
553 ****************************************************************************/
555 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
556 long *poffset, SMB_STRUCT_STAT *pst)
558 /* Normal search for the next file. */
560 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst)) != NULL) {
561 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
568 /****************************************************************************
569 Return the next visible file name, skipping veto'd and invisible files.
570 ****************************************************************************/
572 char *dptr_ReadDirName(TALLOC_CTX *ctx,
573 struct dptr_struct *dptr,
575 SMB_STRUCT_STAT *pst)
577 struct smb_filename *smb_fname_base = NULL;
579 char *pathreal = NULL;
580 char *found_name = NULL;
582 const char *name_temp = NULL;
585 SET_STAT_INVALID(*pst);
587 if (dptr->has_wild || dptr->did_stat) {
588 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst);
589 name = talloc_strdup(ctx, name_temp);
593 /* If poffset is -1 then we know we returned this name before and we
594 * have no wildcards. We're at the end of the directory. */
595 if (*poffset == END_OF_DIRECTORY_OFFSET) {
599 /* We know the stored wcard contains no wildcard characters.
600 * See if we can match with a stat call. If we can't, then set
601 * did_stat to true to ensure we only do this once and keep
604 dptr->did_stat = true;
606 /* First check if it should be visible. */
607 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
610 /* This only returns false if the file was found, but
611 is explicitly not visible. Set us to end of
612 directory, but return NULL as we know we can't ever
617 if (VALID_STAT(*pst)) {
618 name = talloc_strdup(ctx, dptr->wcard);
622 pathreal = talloc_asprintf(ctx,
629 /* Create an smb_filename with stream_name == NULL. */
630 status = create_synthetic_smb_fname(ctx, pathreal, NULL, NULL,
632 if (!NT_STATUS_IS_OK(status)) {
636 if (SMB_VFS_STAT(dptr->conn, smb_fname_base) == 0) {
637 *pst = smb_fname_base->st;
638 TALLOC_FREE(smb_fname_base);
639 name = talloc_strdup(ctx, dptr->wcard);
642 TALLOC_FREE(smb_fname_base);
643 /* If we get any other error than ENOENT or ENOTDIR
644 then the file exists we just can't stat it. */
645 if (errno != ENOENT && errno != ENOTDIR) {
646 name = talloc_strdup(ctx, dptr->wcard);
651 /* Stat failed. We know this is authoratiative if we are
652 * providing case sensitive semantics or the underlying
653 * filesystem is case sensitive.
655 if (dptr->conn->case_sensitive ||
656 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
662 * Try case-insensitive stat if the fs has the ability. This avoids
663 * scanning the whole directory.
665 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
670 } else if (errno == ENOENT) {
671 /* The case-insensitive lookup was authoritative. */
675 TALLOC_FREE(pathreal);
677 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst);
678 name = talloc_strdup(ctx, name_temp);
682 TALLOC_FREE(pathreal);
684 /* We need to set the underlying dir_hnd offset to -1
685 * also as this function is usually called with the
686 * output from TellDir. */
687 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
691 /****************************************************************************
692 Search for a file by name, skipping veto'ed and not visible files.
693 ****************************************************************************/
695 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
697 SET_STAT_INVALID(*pst);
699 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
700 /* This is a singleton directory and we're already at the end. */
701 *poffset = END_OF_DIRECTORY_OFFSET;
705 return SearchDir(dptr->dir_hnd, name, poffset);
708 /****************************************************************************
709 Add the name we're returning into the underlying cache.
710 ****************************************************************************/
712 void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
714 DirCacheAdd(dptr->dir_hnd, name, offset);
717 /****************************************************************************
718 Initialize variables & state data at the beginning of all search SMB requests.
719 ****************************************************************************/
720 void dptr_init_search_op(struct dptr_struct *dptr)
722 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
725 /****************************************************************************
726 Fill the 5 byte server reserved dptr field.
727 ****************************************************************************/
729 bool dptr_fill(char *buf1,unsigned int key)
731 unsigned char *buf = (unsigned char *)buf1;
732 struct dptr_struct *dptr = dptr_get(key, False);
735 DEBUG(1,("filling null dirptr %d\n",key));
738 offset = (uint32)TellDir(dptr->dir_hnd);
739 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
740 (long)dptr->dir_hnd,(int)offset));
746 /****************************************************************************
747 Fetch the dir ptr and seek it given the 5 byte server field.
748 ****************************************************************************/
750 struct dptr_struct *dptr_fetch(char *buf,int *num)
752 unsigned int key = *(unsigned char *)buf;
753 struct dptr_struct *dptr = dptr_get(key, False);
758 DEBUG(3,("fetched null dirptr %d\n",key));
762 offset = IVAL(buf,1);
763 if (offset == (uint32)-1) {
764 seekoff = END_OF_DIRECTORY_OFFSET;
766 seekoff = (long)offset;
768 SeekDir(dptr->dir_hnd,seekoff);
769 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
770 key,dptr_path(key),(int)seekoff));
774 /****************************************************************************
776 ****************************************************************************/
778 struct dptr_struct *dptr_fetch_lanman2(int dptr_num)
780 struct dptr_struct *dptr = dptr_get(dptr_num, False);
783 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
786 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
790 /****************************************************************************
791 Check that a file matches a particular file type.
792 ****************************************************************************/
794 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
798 /* Check the "may have" search bits. */
799 if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
802 /* Check the "must have" bits, which are the may have bits shifted eight */
803 /* If must have bit is set, the file/dir can not be returned in search unless the matching
804 file attribute is set */
805 mask = ((dirtype >> 8) & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
807 if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask) /* check if matching attribute present */
816 static bool mangle_mask_match(connection_struct *conn,
817 const char *filename,
822 if (!name_to_8_3(filename,mname,False,conn->params)) {
825 return mask_match_search(mname,mask,False);
828 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
829 struct dptr_struct *dirptr,
834 bool (*match_fn)(TALLOC_CTX *ctx,
839 bool (*mode_fn)(TALLOC_CTX *ctx,
841 struct smb_filename *smb_fname,
845 struct smb_filename **_smb_fname,
849 connection_struct *conn = dirptr->conn;
855 needslash = ( dirptr->path[strlen(dirptr->path) -1] != '/');
860 SMB_STRUCT_STAT sbuf;
864 char *pathreal = NULL;
865 struct smb_filename *smb_fname = NULL;
870 cur_offset = dptr_TellDir(dirptr);
871 prev_offset = cur_offset;
872 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
874 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
875 (long)dirptr, cur_offset));
881 isdots = (ISDOT(dname) || ISDOTDOT(dname));
882 if (dont_descend && !isdots) {
888 * fname may get mangled, dname is never mangled.
889 * Whenever we're accessing the filesystem we use
890 * pathreal which is composed from dname.
893 ok = match_fn(ctx, private_data, dname, mask, &fname);
899 pathreal = talloc_asprintf(ctx, "%s%s%s",
909 /* Create smb_fname with NULL stream_name. */
910 status = create_synthetic_smb_fname(ctx, pathreal,
913 TALLOC_FREE(pathreal);
914 if (!NT_STATUS_IS_OK(status)) {
920 ok = mode_fn(ctx, private_data, smb_fname, &mode);
924 TALLOC_FREE(smb_fname);
928 if (!dir_check_ftype(conn, mode, dirtype)) {
929 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
930 fname, (unsigned int)mode, (unsigned int)dirtype));
933 TALLOC_FREE(smb_fname);
938 struct timespec write_time_ts;
939 struct file_id fileid;
941 fileid = vfs_file_id_from_sbuf(conn,
943 get_file_infos(fileid, NULL, &write_time_ts);
944 if (!null_timespec(write_time_ts)) {
945 update_stat_ex_mtime(&smb_fname->st,
950 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
952 mask, smb_fname_str_dbg(smb_fname),
955 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
960 *_smb_fname = smb_fname;
962 *_prev_offset = prev_offset;
970 /****************************************************************************
971 Get an 8.3 directory entry.
972 ****************************************************************************/
974 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
980 connection_struct *conn = (connection_struct *)private_data;
982 if ((strcmp(mask,"*.*") == 0) ||
983 mask_match_search(dname, mask, false) ||
984 mangle_mask_match(conn, dname, mask)) {
988 if (!mangle_is_8_3(dname, false, conn->params)) {
989 bool ok = name_to_8_3(dname, mname, false,
999 *_fname = talloc_strdup(ctx, fname);
1000 if (*_fname == NULL) {
1010 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1012 struct smb_filename *smb_fname,
1015 connection_struct *conn = (connection_struct *)private_data;
1017 if (!VALID_STAT(smb_fname->st)) {
1018 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1019 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1020 "Couldn't stat [%s]. Error "
1022 smb_fname_str_dbg(smb_fname),
1028 *_mode = dos_mode(conn, smb_fname);
1032 bool get_dir_entry(TALLOC_CTX *ctx,
1033 connection_struct *conn,
1039 struct timespec *_date,
1044 struct smb_filename *smb_fname = NULL;
1049 if (!conn->dirptr) {
1053 ok = smbd_dirptr_get_entry(ctx,
1059 smbd_dirptr_8_3_match_fn,
1060 smbd_dirptr_8_3_mode_fn,
1070 *_fname = talloc_move(ctx, &fname);
1071 *_size = smb_fname->st.st_ex_size;
1073 *_date = smb_fname->st.st_ex_mtime;
1074 TALLOC_FREE(smb_fname);
1078 /*******************************************************************
1079 Check to see if a user can read a file. This is only approximate,
1080 it is used as part of the "hide unreadable" option. Don't
1081 use it for anything security sensitive.
1082 ********************************************************************/
1084 static bool user_can_read_file(connection_struct *conn,
1085 struct smb_filename *smb_fname)
1088 * If user is a member of the Admin group
1089 * we never hide files from them.
1092 if (conn->admin_user) {
1096 return can_access_file_acl(conn, smb_fname, FILE_READ_DATA);
1099 /*******************************************************************
1100 Check to see if a user can write a file (and only files, we do not
1101 check dirs on this one). This is only approximate,
1102 it is used as part of the "hide unwriteable" option. Don't
1103 use it for anything security sensitive.
1104 ********************************************************************/
1106 static bool user_can_write_file(connection_struct *conn,
1107 const struct smb_filename *smb_fname)
1110 * If user is a member of the Admin group
1111 * we never hide files from them.
1114 if (conn->admin_user) {
1118 SMB_ASSERT(VALID_STAT(smb_fname->st));
1120 /* Pseudo-open the file */
1122 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1126 return can_write_to_file(conn, smb_fname);
1129 /*******************************************************************
1130 Is a file a "special" type ?
1131 ********************************************************************/
1133 static bool file_is_special(connection_struct *conn,
1134 const struct smb_filename *smb_fname)
1137 * If user is a member of the Admin group
1138 * we never hide files from them.
1141 if (conn->admin_user)
1144 SMB_ASSERT(VALID_STAT(smb_fname->st));
1146 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1147 S_ISDIR(smb_fname->st.st_ex_mode) ||
1148 S_ISLNK(smb_fname->st.st_ex_mode))
1154 /*******************************************************************
1155 Should the file be seen by the client?
1156 NOTE: A successful return is no guarantee of the file's existence.
1157 ********************************************************************/
1159 bool is_visible_file(connection_struct *conn, const char *dir_path,
1160 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1162 bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1163 bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1164 bool hide_special = lp_hide_special_files(SNUM(conn));
1166 struct smb_filename *smb_fname_base = NULL;
1170 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1171 return True; /* . and .. are always visible. */
1174 /* If it's a vetoed file, pretend it doesn't even exist */
1175 if (use_veto && IS_VETO_PATH(conn, name)) {
1176 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1180 if (hide_unreadable || hide_unwriteable || hide_special) {
1181 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1187 /* If it's a dfs symlink, ignore _hide xxxx_ options */
1188 if (lp_host_msdfs() &&
1189 lp_msdfs_root(SNUM(conn)) &&
1190 is_msdfs_link(conn, entry, NULL)) {
1195 /* Create an smb_filename with stream_name == NULL. */
1196 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1197 pst, &smb_fname_base);
1198 if (!NT_STATUS_IS_OK(status)) {
1203 /* If the file name does not exist, there's no point checking
1204 * the configuration options. We succeed, on the basis that the
1205 * checks *might* have passed if the file was present.
1207 if (!VALID_STAT(*pst)) {
1208 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1212 *pst = smb_fname_base->st;
1216 /* Honour _hide unreadable_ option */
1217 if (hide_unreadable &&
1218 !user_can_read_file(conn, smb_fname_base)) {
1219 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1224 /* Honour _hide unwriteable_ option */
1225 if (hide_unwriteable && !user_can_write_file(conn,
1227 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1232 /* Honour _hide_special_ option */
1233 if (hide_special && file_is_special(conn, smb_fname_base)) {
1234 DEBUG(10,("is_visible_file: file %s is special.\n",
1243 TALLOC_FREE(smb_fname_base);
1248 static int smb_Dir_destructor(struct smb_Dir *dirp)
1251 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1257 /*******************************************************************
1259 ********************************************************************/
1261 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1262 const char *name, const char *mask, uint32 attr)
1264 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1271 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1273 dirp->dir_path = talloc_strdup(dirp, name);
1274 if (!dirp->dir_path) {
1280 talloc_set_destructor(dirp, smb_Dir_destructor);
1282 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1284 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1296 /*******************************************************************
1297 Read from a directory.
1298 Return directory entry, current offset, and optional stat information.
1299 Don't check for veto or invisible files.
1300 ********************************************************************/
1302 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1303 SMB_STRUCT_STAT *sbuf)
1306 connection_struct *conn = dirp->conn;
1308 /* Cheat to allow . and .. to be the first entries returned. */
1309 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1310 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1312 if (dirp->file_number == 0) {
1314 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1316 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1319 dirp->file_number++;
1321 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1322 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1325 /* A real offset, seek to it. */
1326 SeekDir(dirp, *poffset);
1329 while ((n = vfs_readdirname(conn, dirp->dir, sbuf))) {
1330 /* Ignore . and .. - we've already returned them. */
1332 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1336 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1337 dirp->file_number++;
1340 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1344 /*******************************************************************
1345 Rewind to the start.
1346 ********************************************************************/
1348 void RewindDir(struct smb_Dir *dirp, long *poffset)
1350 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1351 dirp->file_number = 0;
1352 dirp->offset = START_OF_DIRECTORY_OFFSET;
1353 *poffset = START_OF_DIRECTORY_OFFSET;
1356 /*******************************************************************
1358 ********************************************************************/
1360 void SeekDir(struct smb_Dir *dirp, long offset)
1362 if (offset != dirp->offset) {
1363 if (offset == START_OF_DIRECTORY_OFFSET) {
1364 RewindDir(dirp, &offset);
1366 * Ok we should really set the file number here
1367 * to 1 to enable ".." to be returned next. Trouble
1368 * is I'm worried about callers using SeekDir(dirp,0)
1369 * as equivalent to RewindDir(). So leave this alone
1372 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1373 RewindDir(dirp, &offset);
1375 * Set the file number to 2 - we want to get the first
1376 * real file entry (the one we return after "..")
1377 * on the next ReadDir.
1379 dirp->file_number = 2;
1380 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1381 ; /* Don't seek in this case. */
1383 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1385 dirp->offset = offset;
1389 /*******************************************************************
1390 Tell a dir position.
1391 ********************************************************************/
1393 long TellDir(struct smb_Dir *dirp)
1395 return(dirp->offset);
1398 /*******************************************************************
1399 Add an entry into the dcache.
1400 ********************************************************************/
1402 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1404 struct name_cache_entry *e;
1406 if (dirp->name_cache_size == 0) {
1410 if (dirp->name_cache == NULL) {
1411 dirp->name_cache = TALLOC_ZERO_ARRAY(
1412 dirp, struct name_cache_entry, dirp->name_cache_size);
1414 if (dirp->name_cache == NULL) {
1419 dirp->name_cache_index = (dirp->name_cache_index+1) %
1420 dirp->name_cache_size;
1421 e = &dirp->name_cache[dirp->name_cache_index];
1422 TALLOC_FREE(e->name);
1423 e->name = talloc_strdup(dirp, name);
1427 /*******************************************************************
1428 Find an entry by name. Leave us at the offset after it.
1429 Don't check for veto or invisible files.
1430 ********************************************************************/
1432 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1436 connection_struct *conn = dirp->conn;
1438 /* Search back in the name cache. */
1439 if (dirp->name_cache_size && dirp->name_cache) {
1440 for (i = dirp->name_cache_index; i >= 0; i--) {
1441 struct name_cache_entry *e = &dirp->name_cache[i];
1442 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1443 *poffset = e->offset;
1444 SeekDir(dirp, e->offset);
1448 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1449 struct name_cache_entry *e = &dirp->name_cache[i];
1450 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1451 *poffset = e->offset;
1452 SeekDir(dirp, e->offset);
1458 /* Not found in the name cache. Rewind directory and start from scratch. */
1459 SMB_VFS_REWINDDIR(conn, dirp->dir);
1460 dirp->file_number = 0;
1461 *poffset = START_OF_DIRECTORY_OFFSET;
1462 while ((entry = ReadDirName(dirp, poffset, NULL))) {
1463 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1470 /*****************************************************************
1471 Is this directory empty ?
1472 *****************************************************************/
1474 NTSTATUS can_delete_directory(struct connection_struct *conn,
1475 const char *dirname)
1477 NTSTATUS status = NT_STATUS_OK;
1481 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, dirname,
1485 return map_nt_error_from_unix(errno);
1488 while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
1489 /* Quick check for "." and ".." */
1490 if (dname[0] == '.') {
1491 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1496 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1500 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1502 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1505 TALLOC_FREE(dir_hnd);