Merge branch 'master' of /home/tridge/samba/git/combined
[Samba/aatanasov.git] / source3 / smbd / dir.c
blob60fa0306b9265df60b298cae35e61bce21f83dd2
1 /*
2 Unix SMB/CIFS implementation.
3 Directory handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "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 {
36 char *name;
37 long offset;
40 struct smb_Dir {
41 connection_struct *conn;
42 SMB_STRUCT_DIR *dir;
43 long offset;
44 char *dir_path;
45 size_t name_cache_size;
46 struct name_cache_entry *name_cache;
47 unsigned int name_cache_index;
48 unsigned int file_number;
51 struct dptr_struct {
52 struct dptr_struct *next, *prev;
53 int dnum;
54 uint16 spid;
55 struct connection_struct *conn;
56 struct smb_Dir *dir_hnd;
57 bool expect_close;
58 char *wcard;
59 uint32 attr;
60 char *path;
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 /****************************************************************************
69 Make a dir struct.
70 ****************************************************************************/
72 bool make_dir_struct(TALLOC_CTX *ctx,
73 char *buf,
74 const char *mask,
75 const char *fname,
76 SMB_OFF_T size,
77 uint32 mode,
78 time_t date,
79 bool uc)
81 char *p;
82 char *mask2 = talloc_strdup(ctx, mask);
84 if (!mask2) {
85 return False;
88 if ((mode & aDIR) != 0) {
89 size = 0;
92 memset(buf+1,' ',11);
93 if ((p = strchr_m(mask2,'.')) != NULL) {
94 *p = 0;
95 push_ascii(buf+1,mask2,8, 0);
96 push_ascii(buf+9,p+1,3, 0);
97 *p = '.';
98 } else {
99 push_ascii(buf+1,mask2,11, 0);
102 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
103 SCVAL(buf,21,mode);
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));
111 return True;
114 /****************************************************************************
115 Initialise the dir bitmap.
116 ****************************************************************************/
118 bool init_dptrs(struct smbd_server_connection *sconn)
120 if (sconn->smb1.searches.dptr_bmap) {
121 return true;
124 sconn->smb1.searches.dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
126 if (sconn->smb1.searches.dptr_bmap == NULL) {
127 return false;
130 return true;
133 /****************************************************************************
134 Idle a dptr - the directory is closed but the control info is kept.
135 ****************************************************************************/
137 static void dptr_idle(struct dptr_struct *dptr)
139 if (dptr->dir_hnd) {
140 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
141 TALLOC_FREE(dptr->dir_hnd);
145 /****************************************************************************
146 Idle the oldest dptr.
147 ****************************************************************************/
149 static void dptr_idleoldest(struct smbd_server_connection *sconn)
151 struct dptr_struct *dptr;
154 * Go to the end of the list.
156 for(dptr = sconn->smb1.searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
159 if(!dptr) {
160 DEBUG(0,("No dptrs available to idle ?\n"));
161 return;
165 * Idle the oldest pointer.
168 for(; dptr; dptr = dptr->prev) {
169 if (dptr->dir_hnd) {
170 dptr_idle(dptr);
171 return;
176 /****************************************************************************
177 Get the struct dptr_struct for a dir index.
178 ****************************************************************************/
180 static struct dptr_struct *dptr_get(struct smbd_server_connection *sconn,
181 int key, bool forclose)
183 struct dptr_struct *dptr;
185 for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = dptr->next) {
186 if(dptr->dnum == key) {
187 if (!forclose && !dptr->dir_hnd) {
188 if (sconn->smb1.searches.dirhandles_open >= MAX_OPEN_DIRECTORIES)
189 dptr_idleoldest(sconn);
190 DEBUG(4,("dptr_get: Reopening dptr key %d\n",key));
191 if (!(dptr->dir_hnd = OpenDir(
192 NULL, dptr->conn, dptr->path,
193 dptr->wcard, dptr->attr))) {
194 DEBUG(4,("dptr_get: Failed to open %s (%s)\n",dptr->path,
195 strerror(errno)));
196 return False;
199 DLIST_PROMOTE(sconn->smb1.searches.dirptrs,dptr);
200 return dptr;
203 return(NULL);
206 /****************************************************************************
207 Get the dir path for a dir index.
208 ****************************************************************************/
210 char *dptr_path(struct smbd_server_connection *sconn, int key)
212 struct dptr_struct *dptr = dptr_get(sconn, key, false);
213 if (dptr)
214 return(dptr->path);
215 return(NULL);
218 /****************************************************************************
219 Get the dir wcard for a dir index.
220 ****************************************************************************/
222 char *dptr_wcard(struct smbd_server_connection *sconn, int key)
224 struct dptr_struct *dptr = dptr_get(sconn, key, false);
225 if (dptr)
226 return(dptr->wcard);
227 return(NULL);
230 /****************************************************************************
231 Get the dir attrib for a dir index.
232 ****************************************************************************/
234 uint16 dptr_attr(struct smbd_server_connection *sconn, int key)
236 struct dptr_struct *dptr = dptr_get(sconn, key, false);
237 if (dptr)
238 return(dptr->attr);
239 return(0);
242 /****************************************************************************
243 Close a dptr (internal func).
244 ****************************************************************************/
246 static void dptr_close_internal(struct dptr_struct *dptr)
248 struct smbd_server_connection *sconn = dptr->conn->sconn;
250 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
252 if (sconn == NULL) {
253 goto done;
256 DLIST_REMOVE(sconn->smb1.searches.dirptrs, dptr);
259 * Free the dnum in the bitmap. Remember the dnum value is always
260 * biased by one with respect to the bitmap.
263 if(bitmap_query(sconn->smb1.searches.dptr_bmap, dptr->dnum - 1) != true) {
264 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
265 dptr->dnum ));
268 bitmap_clear(sconn->smb1.searches.dptr_bmap, dptr->dnum - 1);
270 done:
271 TALLOC_FREE(dptr->dir_hnd);
273 /* Lanman 2 specific code */
274 SAFE_FREE(dptr->wcard);
275 string_set(&dptr->path,"");
276 SAFE_FREE(dptr);
279 /****************************************************************************
280 Close a dptr given a key.
281 ****************************************************************************/
283 void dptr_close(struct smbd_server_connection *sconn, int *key)
285 struct dptr_struct *dptr;
287 if(*key == INVALID_DPTR_KEY)
288 return;
290 /* OS/2 seems to use -1 to indicate "close all directories" */
291 if (*key == -1) {
292 struct dptr_struct *next;
293 for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
294 next = dptr->next;
295 dptr_close_internal(dptr);
297 *key = INVALID_DPTR_KEY;
298 return;
301 dptr = dptr_get(sconn, *key, true);
303 if (!dptr) {
304 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
305 return;
308 dptr_close_internal(dptr);
310 *key = INVALID_DPTR_KEY;
313 /****************************************************************************
314 Close all dptrs for a cnum.
315 ****************************************************************************/
317 void dptr_closecnum(connection_struct *conn)
319 struct dptr_struct *dptr, *next;
320 struct smbd_server_connection *sconn = conn->sconn;
322 if (sconn == NULL) {
323 return;
326 for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
327 next = dptr->next;
328 if (dptr->conn == conn) {
329 dptr_close_internal(dptr);
334 /****************************************************************************
335 Idle all dptrs for a cnum.
336 ****************************************************************************/
338 void dptr_idlecnum(connection_struct *conn)
340 struct dptr_struct *dptr;
341 struct smbd_server_connection *sconn = conn->sconn;
343 if (sconn == NULL) {
344 return;
347 for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = dptr->next) {
348 if (dptr->conn == conn && dptr->dir_hnd) {
349 dptr_idle(dptr);
354 /****************************************************************************
355 Close a dptr that matches a given path, only if it matches the spid also.
356 ****************************************************************************/
358 void dptr_closepath(struct smbd_server_connection *sconn,
359 char *path,uint16 spid)
361 struct dptr_struct *dptr, *next;
362 for(dptr = sconn->smb1.searches.dirptrs; dptr; dptr = next) {
363 next = dptr->next;
364 if (spid == dptr->spid && strequal(dptr->path,path))
365 dptr_close_internal(dptr);
369 /****************************************************************************
370 Try and close the oldest handle not marked for
371 expect close in the hope that the client has
372 finished with that one.
373 ****************************************************************************/
375 static void dptr_close_oldest(struct smbd_server_connection *sconn,
376 bool old)
378 struct dptr_struct *dptr;
381 * Go to the end of the list.
383 for(dptr = sconn->smb1.searches.dirptrs; dptr && dptr->next; dptr = dptr->next)
386 if(!dptr) {
387 DEBUG(0,("No old dptrs available to close oldest ?\n"));
388 return;
392 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
393 * does not have expect_close set. If 'old' is false, close
394 * one of the new dnum handles.
397 for(; dptr; dptr = dptr->prev) {
398 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
399 (!old && (dptr->dnum > 255))) {
400 dptr_close_internal(dptr);
401 return;
406 /****************************************************************************
407 Create a new dir ptr. If the flag old_handle is true then we must allocate
408 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
409 one byte long. If old_handle is false we allocate from the range
410 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
411 a directory handle is never zero.
412 wcard must not be zero.
413 ****************************************************************************/
415 NTSTATUS dptr_create(connection_struct *conn, const char *path, bool old_handle, bool expect_close,uint16 spid,
416 const char *wcard, bool wcard_has_wild, uint32 attr, struct dptr_struct **dptr_ret)
418 struct smbd_server_connection *sconn = conn->sconn;
419 struct dptr_struct *dptr = NULL;
420 struct smb_Dir *dir_hnd;
421 NTSTATUS status;
423 DEBUG(5,("dptr_create dir=%s\n", path));
425 if (sconn == NULL) {
426 DEBUG(0,("dptr_create: called with fake connection_struct\n"));
427 return NT_STATUS_INTERNAL_ERROR;
430 if (!wcard) {
431 return NT_STATUS_INVALID_PARAMETER;
434 status = check_name(conn,path);
435 if (!NT_STATUS_IS_OK(status)) {
436 return status;
439 dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
440 if (!dir_hnd) {
441 return map_nt_error_from_unix(errno);
444 if (sconn->smb1.searches.dirhandles_open >= MAX_OPEN_DIRECTORIES) {
445 dptr_idleoldest(sconn);
448 dptr = SMB_MALLOC_P(struct dptr_struct);
449 if(!dptr) {
450 DEBUG(0,("malloc fail in dptr_create.\n"));
451 TALLOC_FREE(dir_hnd);
452 return NT_STATUS_NO_MEMORY;
455 ZERO_STRUCTP(dptr);
457 if(old_handle) {
460 * This is an old-style SMBsearch request. Ensure the
461 * value we return will fit in the range 1-255.
464 dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 0);
466 if(dptr->dnum == -1 || dptr->dnum > 254) {
469 * Try and close the oldest handle not marked for
470 * expect close in the hope that the client has
471 * finished with that one.
474 dptr_close_oldest(sconn, true);
476 /* Now try again... */
477 dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 0);
478 if(dptr->dnum == -1 || dptr->dnum > 254) {
479 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
480 SAFE_FREE(dptr);
481 TALLOC_FREE(dir_hnd);
482 return NT_STATUS_TOO_MANY_OPENED_FILES;
485 } else {
488 * This is a new-style trans2 request. Allocate from
489 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
492 dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 255);
494 if(dptr->dnum == -1 || dptr->dnum < 255) {
497 * Try and close the oldest handle close in the hope that
498 * the client has finished with that one. This will only
499 * happen in the case of the Win98 client bug where it leaks
500 * directory handles.
503 dptr_close_oldest(sconn, false);
505 /* Now try again... */
506 dptr->dnum = bitmap_find(sconn->smb1.searches.dptr_bmap, 255);
508 if(dptr->dnum == -1 || dptr->dnum < 255) {
509 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
510 SAFE_FREE(dptr);
511 TALLOC_FREE(dir_hnd);
512 return NT_STATUS_TOO_MANY_OPENED_FILES;
517 bitmap_set(sconn->smb1.searches.dptr_bmap, dptr->dnum);
519 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
521 string_set(&dptr->path,path);
522 dptr->conn = conn;
523 dptr->dir_hnd = dir_hnd;
524 dptr->spid = spid;
525 dptr->expect_close = expect_close;
526 dptr->wcard = SMB_STRDUP(wcard);
527 if (!dptr->wcard) {
528 bitmap_clear(sconn->smb1.searches.dptr_bmap, dptr->dnum - 1);
529 SAFE_FREE(dptr);
530 TALLOC_FREE(dir_hnd);
531 return NT_STATUS_NO_MEMORY;
533 if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
534 dptr->has_wild = True;
535 } else {
536 dptr->has_wild = wcard_has_wild;
539 dptr->attr = attr;
541 DLIST_ADD(sconn->smb1.searches.dirptrs, dptr);
543 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
544 dptr->dnum,path,expect_close));
546 *dptr_ret = dptr;
548 return NT_STATUS_OK;
552 /****************************************************************************
553 Wrapper functions to access the lower level directory handles.
554 ****************************************************************************/
556 int dptr_CloseDir(struct dptr_struct *dptr)
558 struct smbd_server_connection *sconn = dptr->conn->sconn;
559 DLIST_REMOVE(sconn->smb1.searches.dirptrs, dptr);
560 TALLOC_FREE(dptr->dir_hnd);
561 return 0;
564 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
566 SeekDir(dptr->dir_hnd, offset);
569 long dptr_TellDir(struct dptr_struct *dptr)
571 return TellDir(dptr->dir_hnd);
574 bool dptr_has_wild(struct dptr_struct *dptr)
576 return dptr->has_wild;
579 int dptr_dnum(struct dptr_struct *dptr)
581 return dptr->dnum;
584 /****************************************************************************
585 Return the next visible file name, skipping veto'd and invisible files.
586 ****************************************************************************/
588 static char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
589 long *poffset, SMB_STRUCT_STAT *pst)
591 /* Normal search for the next file. */
592 char *name;
593 while ((name = ReadDirName(dptr->dir_hnd, poffset, pst)) != NULL) {
594 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
595 return name;
597 TALLOC_FREE(name);
599 return NULL;
602 /****************************************************************************
603 Return the next visible file name, skipping veto'd and invisible files.
604 ****************************************************************************/
606 char *dptr_ReadDirName(TALLOC_CTX *ctx,
607 struct dptr_struct *dptr,
608 long *poffset,
609 SMB_STRUCT_STAT *pst)
611 struct smb_filename *smb_fname_base = NULL;
612 char *name = NULL;
613 char *pathreal = NULL;
614 char *found_name = NULL;
615 int ret;
616 NTSTATUS status;
618 SET_STAT_INVALID(*pst);
620 if (dptr->has_wild || dptr->did_stat) {
621 name = dptr_normal_ReadDirName(dptr, poffset, pst);
622 return name;
625 /* If poffset is -1 then we know we returned this name before and we
626 * have no wildcards. We're at the end of the directory. */
627 if (*poffset == END_OF_DIRECTORY_OFFSET) {
628 return NULL;
631 /* We know the stored wcard contains no wildcard characters.
632 * See if we can match with a stat call. If we can't, then set
633 * did_stat to true to ensure we only do this once and keep
634 * searching. */
636 dptr->did_stat = true;
638 /* First check if it should be visible. */
639 if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
640 pst, true))
642 /* This only returns false if the file was found, but
643 is explicitly not visible. Set us to end of
644 directory, but return NULL as we know we can't ever
645 find it. */
646 goto ret;
649 if (VALID_STAT(*pst)) {
650 name = talloc_strdup(ctx, dptr->wcard);
651 goto ret;
654 pathreal = talloc_asprintf(ctx,
655 "%s/%s",
656 dptr->path,
657 dptr->wcard);
658 if (!pathreal)
659 return NULL;
661 /* Create an smb_filename with stream_name == NULL. */
662 status = create_synthetic_smb_fname(ctx, pathreal, NULL, NULL,
663 &smb_fname_base);
664 if (!NT_STATUS_IS_OK(status)) {
665 return NULL;
668 if (SMB_VFS_STAT(dptr->conn, smb_fname_base) == 0) {
669 *pst = smb_fname_base->st;
670 TALLOC_FREE(smb_fname_base);
671 name = talloc_strdup(ctx, dptr->wcard);
672 goto clean;
673 } else {
674 TALLOC_FREE(smb_fname_base);
675 /* If we get any other error than ENOENT or ENOTDIR
676 then the file exists we just can't stat it. */
677 if (errno != ENOENT && errno != ENOTDIR) {
678 name = talloc_strdup(ctx, dptr->wcard);
679 goto clean;
683 /* Stat failed. We know this is authoratiative if we are
684 * providing case sensitive semantics or the underlying
685 * filesystem is case sensitive.
687 if (dptr->conn->case_sensitive ||
688 !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
690 goto clean;
694 * Try case-insensitive stat if the fs has the ability. This avoids
695 * scanning the whole directory.
697 ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
698 ctx, &found_name);
699 if (ret == 0) {
700 name = found_name;
701 goto clean;
702 } else if (errno == ENOENT) {
703 /* The case-insensitive lookup was authoritative. */
704 goto clean;
707 TALLOC_FREE(pathreal);
709 name = dptr_normal_ReadDirName(dptr, poffset, pst);
711 return name;
713 clean:
714 TALLOC_FREE(pathreal);
715 ret:
716 /* We need to set the underlying dir_hnd offset to -1
717 * also as this function is usually called with the
718 * output from TellDir. */
719 dptr->dir_hnd->offset = *poffset = END_OF_DIRECTORY_OFFSET;
720 return name;
723 /****************************************************************************
724 Search for a file by name, skipping veto'ed and not visible files.
725 ****************************************************************************/
727 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
729 SET_STAT_INVALID(*pst);
731 if (!dptr->has_wild && (dptr->dir_hnd->offset == END_OF_DIRECTORY_OFFSET)) {
732 /* This is a singleton directory and we're already at the end. */
733 *poffset = END_OF_DIRECTORY_OFFSET;
734 return False;
737 return SearchDir(dptr->dir_hnd, name, poffset);
740 /****************************************************************************
741 Add the name we're returning into the underlying cache.
742 ****************************************************************************/
744 void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
746 DirCacheAdd(dptr->dir_hnd, name, offset);
749 /****************************************************************************
750 Initialize variables & state data at the beginning of all search SMB requests.
751 ****************************************************************************/
752 void dptr_init_search_op(struct dptr_struct *dptr)
754 SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
757 /****************************************************************************
758 Fill the 5 byte server reserved dptr field.
759 ****************************************************************************/
761 bool dptr_fill(struct smbd_server_connection *sconn,
762 char *buf1,unsigned int key)
764 unsigned char *buf = (unsigned char *)buf1;
765 struct dptr_struct *dptr = dptr_get(sconn, key, false);
766 uint32 offset;
767 if (!dptr) {
768 DEBUG(1,("filling null dirptr %d\n",key));
769 return(False);
771 offset = (uint32)TellDir(dptr->dir_hnd);
772 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
773 (long)dptr->dir_hnd,(int)offset));
774 buf[0] = key;
775 SIVAL(buf,1,offset);
776 return(True);
779 /****************************************************************************
780 Fetch the dir ptr and seek it given the 5 byte server field.
781 ****************************************************************************/
783 struct dptr_struct *dptr_fetch(struct smbd_server_connection *sconn,
784 char *buf, int *num)
786 unsigned int key = *(unsigned char *)buf;
787 struct dptr_struct *dptr = dptr_get(sconn, key, false);
788 uint32 offset;
789 long seekoff;
791 if (!dptr) {
792 DEBUG(3,("fetched null dirptr %d\n",key));
793 return(NULL);
795 *num = key;
796 offset = IVAL(buf,1);
797 if (offset == (uint32)-1) {
798 seekoff = END_OF_DIRECTORY_OFFSET;
799 } else {
800 seekoff = (long)offset;
802 SeekDir(dptr->dir_hnd,seekoff);
803 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
804 key, dptr->path, (int)seekoff));
805 return(dptr);
808 /****************************************************************************
809 Fetch the dir ptr.
810 ****************************************************************************/
812 struct dptr_struct *dptr_fetch_lanman2(struct smbd_server_connection *sconn,
813 int dptr_num)
815 struct dptr_struct *dptr = dptr_get(sconn, dptr_num, false);
817 if (!dptr) {
818 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
819 return(NULL);
821 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr->path));
822 return(dptr);
825 /****************************************************************************
826 Check that a file matches a particular file type.
827 ****************************************************************************/
829 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
831 uint32 mask;
833 /* Check the "may have" search bits. */
834 if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
835 return False;
837 /* Check the "must have" bits, which are the may have bits shifted eight */
838 /* If must have bit is set, the file/dir can not be returned in search unless the matching
839 file attribute is set */
840 mask = ((dirtype >> 8) & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
841 if(mask) {
842 if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask) /* check if matching attribute present */
843 return True;
844 else
845 return False;
848 return True;
851 static bool mangle_mask_match(connection_struct *conn,
852 const char *filename,
853 const char *mask)
855 char mname[13];
857 if (!name_to_8_3(filename,mname,False,conn->params)) {
858 return False;
860 return mask_match_search(mname,mask,False);
863 bool smbd_dirptr_get_entry(TALLOC_CTX *ctx,
864 struct dptr_struct *dirptr,
865 const char *mask,
866 uint32_t dirtype,
867 bool dont_descend,
868 bool ask_sharemode,
869 bool (*match_fn)(TALLOC_CTX *ctx,
870 void *private_data,
871 const char *dname,
872 const char *mask,
873 char **_fname),
874 bool (*mode_fn)(TALLOC_CTX *ctx,
875 void *private_data,
876 struct smb_filename *smb_fname,
877 uint32_t *_mode),
878 void *private_data,
879 char **_fname,
880 struct smb_filename **_smb_fname,
881 uint32_t *_mode,
882 long *_prev_offset)
884 connection_struct *conn = dirptr->conn;
885 bool needslash;
887 *_smb_fname = NULL;
888 *_mode = 0;
890 needslash = ( dirptr->path[strlen(dirptr->path) -1] != '/');
892 while (true) {
893 long cur_offset;
894 long prev_offset;
895 SMB_STRUCT_STAT sbuf;
896 char *dname = NULL;
897 bool isdots;
898 char *fname = NULL;
899 char *pathreal = NULL;
900 struct smb_filename *smb_fname = NULL;
901 uint32_t mode = 0;
902 bool ok;
903 NTSTATUS status;
905 cur_offset = dptr_TellDir(dirptr);
906 prev_offset = cur_offset;
907 dname = dptr_ReadDirName(ctx, dirptr, &cur_offset, &sbuf);
909 DEBUG(6,("smbd_dirptr_get_entry: dirptr 0x%lx now at offset %ld\n",
910 (long)dirptr, cur_offset));
912 if (dname == NULL) {
913 return false;
916 isdots = (ISDOT(dname) || ISDOTDOT(dname));
917 if (dont_descend && !isdots) {
918 TALLOC_FREE(dname);
919 continue;
923 * fname may get mangled, dname is never mangled.
924 * Whenever we're accessing the filesystem we use
925 * pathreal which is composed from dname.
928 ok = match_fn(ctx, private_data, dname, mask, &fname);
929 if (!ok) {
930 TALLOC_FREE(dname);
931 continue;
934 pathreal = talloc_asprintf(ctx, "%s%s%s",
935 dirptr->path,
936 needslash?"/":"",
937 dname);
938 if (!pathreal) {
939 TALLOC_FREE(dname);
940 TALLOC_FREE(fname);
941 return false;
944 /* Create smb_fname with NULL stream_name. */
945 status = create_synthetic_smb_fname(ctx, pathreal,
946 NULL, &sbuf,
947 &smb_fname);
948 TALLOC_FREE(pathreal);
949 if (!NT_STATUS_IS_OK(status)) {
950 TALLOC_FREE(dname);
951 TALLOC_FREE(fname);
952 return false;
955 ok = mode_fn(ctx, private_data, smb_fname, &mode);
956 if (!ok) {
957 TALLOC_FREE(dname);
958 TALLOC_FREE(fname);
959 TALLOC_FREE(smb_fname);
960 continue;
963 if (!dir_check_ftype(conn, mode, dirtype)) {
964 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",
965 fname, (unsigned int)mode, (unsigned int)dirtype));
966 TALLOC_FREE(dname);
967 TALLOC_FREE(fname);
968 TALLOC_FREE(smb_fname);
969 continue;
972 if (ask_sharemode) {
973 struct timespec write_time_ts;
974 struct file_id fileid;
976 fileid = vfs_file_id_from_sbuf(conn,
977 &smb_fname->st);
978 get_file_infos(fileid, NULL, &write_time_ts);
979 if (!null_timespec(write_time_ts)) {
980 update_stat_ex_mtime(&smb_fname->st,
981 write_time_ts);
985 DEBUG(3,("smbd_dirptr_get_entry mask=[%s] found %s "
986 "fname=%s (%s)\n",
987 mask, smb_fname_str_dbg(smb_fname),
988 dname, fname));
990 DirCacheAdd(dirptr->dir_hnd, dname, cur_offset);
992 TALLOC_FREE(dname);
994 *_fname = fname;
995 *_smb_fname = smb_fname;
996 *_mode = mode;
997 *_prev_offset = prev_offset;
999 return true;
1002 return false;
1005 /****************************************************************************
1006 Get an 8.3 directory entry.
1007 ****************************************************************************/
1009 static bool smbd_dirptr_8_3_match_fn(TALLOC_CTX *ctx,
1010 void *private_data,
1011 const char *dname,
1012 const char *mask,
1013 char **_fname)
1015 connection_struct *conn = (connection_struct *)private_data;
1017 if ((strcmp(mask,"*.*") == 0) ||
1018 mask_match_search(dname, mask, false) ||
1019 mangle_mask_match(conn, dname, mask)) {
1020 char mname[13];
1021 const char *fname;
1023 if (!mangle_is_8_3(dname, false, conn->params)) {
1024 bool ok = name_to_8_3(dname, mname, false,
1025 conn->params);
1026 if (!ok) {
1027 return false;
1029 fname = mname;
1030 } else {
1031 fname = dname;
1034 *_fname = talloc_strdup(ctx, fname);
1035 if (*_fname == NULL) {
1036 return false;
1039 return true;
1042 return false;
1045 static bool smbd_dirptr_8_3_mode_fn(TALLOC_CTX *ctx,
1046 void *private_data,
1047 struct smb_filename *smb_fname,
1048 uint32_t *_mode)
1050 connection_struct *conn = (connection_struct *)private_data;
1052 if (!VALID_STAT(smb_fname->st)) {
1053 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
1054 DEBUG(5,("smbd_dirptr_8_3_mode_fn: "
1055 "Couldn't stat [%s]. Error "
1056 "= %s\n",
1057 smb_fname_str_dbg(smb_fname),
1058 strerror(errno)));
1059 return false;
1063 *_mode = dos_mode(conn, smb_fname);
1064 return true;
1067 bool get_dir_entry(TALLOC_CTX *ctx,
1068 struct dptr_struct *dirptr,
1069 const char *mask,
1070 uint32_t dirtype,
1071 char **_fname,
1072 SMB_OFF_T *_size,
1073 uint32_t *_mode,
1074 struct timespec *_date,
1075 bool check_descend,
1076 bool ask_sharemode)
1078 connection_struct *conn = dirptr->conn;
1079 char *fname = NULL;
1080 struct smb_filename *smb_fname = NULL;
1081 uint32_t mode = 0;
1082 long prev_offset;
1083 bool ok;
1085 ok = smbd_dirptr_get_entry(ctx,
1086 dirptr,
1087 mask,
1088 dirtype,
1089 check_descend,
1090 ask_sharemode,
1091 smbd_dirptr_8_3_match_fn,
1092 smbd_dirptr_8_3_mode_fn,
1093 conn,
1094 &fname,
1095 &smb_fname,
1096 &mode,
1097 &prev_offset);
1098 if (!ok) {
1099 return false;
1102 *_fname = talloc_move(ctx, &fname);
1103 *_size = smb_fname->st.st_ex_size;
1104 *_mode = mode;
1105 *_date = smb_fname->st.st_ex_mtime;
1106 TALLOC_FREE(smb_fname);
1107 return true;
1110 /*******************************************************************
1111 Check to see if a user can read a file. This is only approximate,
1112 it is used as part of the "hide unreadable" option. Don't
1113 use it for anything security sensitive.
1114 ********************************************************************/
1116 static bool user_can_read_file(connection_struct *conn,
1117 struct smb_filename *smb_fname)
1120 * If user is a member of the Admin group
1121 * we never hide files from them.
1124 if (conn->admin_user) {
1125 return True;
1128 return can_access_file_acl(conn, smb_fname, FILE_READ_DATA);
1131 /*******************************************************************
1132 Check to see if a user can write a file (and only files, we do not
1133 check dirs on this one). This is only approximate,
1134 it is used as part of the "hide unwriteable" option. Don't
1135 use it for anything security sensitive.
1136 ********************************************************************/
1138 static bool user_can_write_file(connection_struct *conn,
1139 const struct smb_filename *smb_fname)
1142 * If user is a member of the Admin group
1143 * we never hide files from them.
1146 if (conn->admin_user) {
1147 return True;
1150 SMB_ASSERT(VALID_STAT(smb_fname->st));
1152 /* Pseudo-open the file */
1154 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1155 return True;
1158 return can_write_to_file(conn, smb_fname);
1161 /*******************************************************************
1162 Is a file a "special" type ?
1163 ********************************************************************/
1165 static bool file_is_special(connection_struct *conn,
1166 const struct smb_filename *smb_fname)
1169 * If user is a member of the Admin group
1170 * we never hide files from them.
1173 if (conn->admin_user)
1174 return False;
1176 SMB_ASSERT(VALID_STAT(smb_fname->st));
1178 if (S_ISREG(smb_fname->st.st_ex_mode) ||
1179 S_ISDIR(smb_fname->st.st_ex_mode) ||
1180 S_ISLNK(smb_fname->st.st_ex_mode))
1181 return False;
1183 return True;
1186 /*******************************************************************
1187 Should the file be seen by the client?
1188 NOTE: A successful return is no guarantee of the file's existence.
1189 ********************************************************************/
1191 bool is_visible_file(connection_struct *conn, const char *dir_path,
1192 const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1194 bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1195 bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1196 bool hide_special = lp_hide_special_files(SNUM(conn));
1197 char *entry = NULL;
1198 struct smb_filename *smb_fname_base = NULL;
1199 NTSTATUS status;
1200 bool ret = false;
1202 if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1203 return True; /* . and .. are always visible. */
1206 /* If it's a vetoed file, pretend it doesn't even exist */
1207 if (use_veto && IS_VETO_PATH(conn, name)) {
1208 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1209 return False;
1212 if (hide_unreadable || hide_unwriteable || hide_special) {
1213 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1214 if (!entry) {
1215 ret = false;
1216 goto out;
1219 /* If it's a dfs symlink, ignore _hide xxxx_ options */
1220 if (lp_host_msdfs() &&
1221 lp_msdfs_root(SNUM(conn)) &&
1222 is_msdfs_link(conn, entry, NULL)) {
1223 ret = true;
1224 goto out;
1227 /* Create an smb_filename with stream_name == NULL. */
1228 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1229 pst, &smb_fname_base);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 ret = false;
1232 goto out;
1235 /* If the file name does not exist, there's no point checking
1236 * the configuration options. We succeed, on the basis that the
1237 * checks *might* have passed if the file was present.
1239 if (!VALID_STAT(*pst)) {
1240 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
1241 ret = true;
1242 goto out;
1243 } else {
1244 *pst = smb_fname_base->st;
1248 /* Honour _hide unreadable_ option */
1249 if (hide_unreadable &&
1250 !user_can_read_file(conn, smb_fname_base)) {
1251 DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1252 entry ));
1253 ret = false;
1254 goto out;
1256 /* Honour _hide unwriteable_ option */
1257 if (hide_unwriteable && !user_can_write_file(conn,
1258 smb_fname_base)) {
1259 DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1260 entry ));
1261 ret = false;
1262 goto out;
1264 /* Honour _hide_special_ option */
1265 if (hide_special && file_is_special(conn, smb_fname_base)) {
1266 DEBUG(10,("is_visible_file: file %s is special.\n",
1267 entry ));
1268 ret = false;
1269 goto out;
1273 ret = true;
1274 out:
1275 TALLOC_FREE(smb_fname_base);
1276 TALLOC_FREE(entry);
1277 return ret;
1280 static int smb_Dir_destructor(struct smb_Dir *dirp)
1282 if (dirp->dir) {
1283 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1285 if (dirp->conn->sconn) {
1286 dirp->conn->sconn->smb1.searches.dirhandles_open--;
1288 return 0;
1291 /*******************************************************************
1292 Open a directory.
1293 ********************************************************************/
1295 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1296 const char *name, const char *mask, uint32 attr)
1298 struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1299 struct smbd_server_connection *sconn = conn->sconn;
1301 if (!dirp) {
1302 return NULL;
1305 dirp->conn = conn;
1306 dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1308 dirp->dir_path = talloc_strdup(dirp, name);
1309 if (!dirp->dir_path) {
1310 errno = ENOMEM;
1311 goto fail;
1314 if (sconn) {
1315 sconn->smb1.searches.dirhandles_open++;
1317 talloc_set_destructor(dirp, smb_Dir_destructor);
1319 dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1320 if (!dirp->dir) {
1321 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1322 strerror(errno) ));
1323 goto fail;
1326 return dirp;
1328 fail:
1329 TALLOC_FREE(dirp);
1330 return NULL;
1333 /*******************************************************************
1334 Read from a directory.
1335 Return directory entry, current offset, and optional stat information.
1336 Don't check for veto or invisible files.
1337 ********************************************************************/
1339 char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1340 SMB_STRUCT_STAT *sbuf)
1342 char *n;
1343 connection_struct *conn = dirp->conn;
1345 /* Cheat to allow . and .. to be the first entries returned. */
1346 if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1347 (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1349 if (dirp->file_number == 0) {
1350 n = talloc_strdup(talloc_tos(), ".");
1351 if (n == NULL)
1352 return NULL;
1353 *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1354 } else {
1355 *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1356 n = talloc_strdup(talloc_tos(), "..");
1357 if (n == NULL)
1358 return NULL;
1360 dirp->file_number++;
1361 return n;
1362 } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1363 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1364 return NULL;
1365 } else {
1366 /* A real offset, seek to it. */
1367 SeekDir(dirp, *poffset);
1370 while ((n = vfs_readdirname(conn, dirp->dir, sbuf))) {
1371 /* Ignore . and .. - we've already returned them. */
1372 if (*n == '.') {
1373 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1374 TALLOC_FREE(n);
1375 continue;
1378 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1379 dirp->file_number++;
1380 return n;
1382 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1383 return NULL;
1386 /*******************************************************************
1387 Rewind to the start.
1388 ********************************************************************/
1390 void RewindDir(struct smb_Dir *dirp, long *poffset)
1392 SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1393 dirp->file_number = 0;
1394 dirp->offset = START_OF_DIRECTORY_OFFSET;
1395 *poffset = START_OF_DIRECTORY_OFFSET;
1398 /*******************************************************************
1399 Seek a dir.
1400 ********************************************************************/
1402 void SeekDir(struct smb_Dir *dirp, long offset)
1404 if (offset != dirp->offset) {
1405 if (offset == START_OF_DIRECTORY_OFFSET) {
1406 RewindDir(dirp, &offset);
1408 * Ok we should really set the file number here
1409 * to 1 to enable ".." to be returned next. Trouble
1410 * is I'm worried about callers using SeekDir(dirp,0)
1411 * as equivalent to RewindDir(). So leave this alone
1412 * for now.
1414 } else if (offset == DOT_DOT_DIRECTORY_OFFSET) {
1415 RewindDir(dirp, &offset);
1417 * Set the file number to 2 - we want to get the first
1418 * real file entry (the one we return after "..")
1419 * on the next ReadDir.
1421 dirp->file_number = 2;
1422 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1423 ; /* Don't seek in this case. */
1424 } else {
1425 SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1427 dirp->offset = offset;
1431 /*******************************************************************
1432 Tell a dir position.
1433 ********************************************************************/
1435 long TellDir(struct smb_Dir *dirp)
1437 return(dirp->offset);
1440 /*******************************************************************
1441 Add an entry into the dcache.
1442 ********************************************************************/
1444 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1446 struct name_cache_entry *e;
1448 if (dirp->name_cache_size == 0) {
1449 return;
1452 if (dirp->name_cache == NULL) {
1453 dirp->name_cache = TALLOC_ZERO_ARRAY(
1454 dirp, struct name_cache_entry, dirp->name_cache_size);
1456 if (dirp->name_cache == NULL) {
1457 return;
1461 dirp->name_cache_index = (dirp->name_cache_index+1) %
1462 dirp->name_cache_size;
1463 e = &dirp->name_cache[dirp->name_cache_index];
1464 TALLOC_FREE(e->name);
1465 e->name = talloc_strdup(dirp, name);
1466 e->offset = offset;
1469 /*******************************************************************
1470 Find an entry by name. Leave us at the offset after it.
1471 Don't check for veto or invisible files.
1472 ********************************************************************/
1474 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1476 int i;
1477 char *entry = NULL;
1478 connection_struct *conn = dirp->conn;
1480 /* Search back in the name cache. */
1481 if (dirp->name_cache_size && dirp->name_cache) {
1482 for (i = dirp->name_cache_index; i >= 0; i--) {
1483 struct name_cache_entry *e = &dirp->name_cache[i];
1484 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1485 *poffset = e->offset;
1486 SeekDir(dirp, e->offset);
1487 return True;
1490 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1491 struct name_cache_entry *e = &dirp->name_cache[i];
1492 if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1493 *poffset = e->offset;
1494 SeekDir(dirp, e->offset);
1495 return True;
1500 /* Not found in the name cache. Rewind directory and start from scratch. */
1501 SMB_VFS_REWINDDIR(conn, dirp->dir);
1502 dirp->file_number = 0;
1503 *poffset = START_OF_DIRECTORY_OFFSET;
1504 while ((entry = ReadDirName(dirp, poffset, NULL))) {
1505 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1506 TALLOC_FREE(entry);
1507 return True;
1509 TALLOC_FREE(entry);
1511 return False;
1514 /*****************************************************************
1515 Is this directory empty ?
1516 *****************************************************************/
1518 NTSTATUS can_delete_directory(struct connection_struct *conn,
1519 const char *dirname)
1521 NTSTATUS status = NT_STATUS_OK;
1522 long dirpos = 0;
1523 char *dname = NULL;
1524 SMB_STRUCT_STAT st;
1525 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, dirname,
1526 NULL, 0);
1528 if (!dir_hnd) {
1529 return map_nt_error_from_unix(errno);
1532 while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
1533 /* Quick check for "." and ".." */
1534 if (dname[0] == '.') {
1535 if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1536 TALLOC_FREE(dname);
1537 continue;
1541 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1542 TALLOC_FREE(dname);
1543 continue;
1546 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1547 dname ));
1548 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1549 break;
1551 TALLOC_FREE(dname);
1552 TALLOC_FREE(dir_hnd);
1554 return status;