aff30a6967cee7b4a694514bd26b26d091fb4cf8
[ira/wip.git] / source3 / smbd / dir.c
1 /*
2    Unix SMB/CIFS implementation.
3    Directory handling routines
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2007
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23
24 /*
25    This module implements directory related functions for Samba.
26 */
27
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)
32
33 /* Make directory handle internals available. */
34
35 struct name_cache_entry {
36         char *name;
37         long offset;
38 };
39
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;
49 };
50
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. */
63 };
64
65
66 #define INVALID_DPTR_KEY (-3)
67
68 /****************************************************************************
69  Make a dir struct.
70 ****************************************************************************/
71
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)
80 {
81         char *p;
82         char *mask2 = talloc_strdup(ctx, mask);
83
84         if (!mask2) {
85                 return False;
86         }
87
88         if ((mode & aDIR) != 0) {
89                 size = 0;
90         }
91
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);
100         }
101
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;
112 }
113
114 /****************************************************************************
115  Initialise the dir bitmap.
116 ****************************************************************************/
117
118 void init_dptrs(void)
119 {
120         if (dptr_bmap)
121                 return;
122
123         dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
124
125         if (!dptr_bmap)
126                 exit_server("out of memory in init_dptrs");
127 }
128
129 /****************************************************************************
130  Idle a dptr - the directory is closed but the control info is kept.
131 ****************************************************************************/
132
133 static void dptr_idle(struct dptr_struct *dptr)
134 {
135         if (dptr->dir_hnd) {
136                 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
137                 TALLOC_FREE(dptr->dir_hnd);
138         }
139 }
140
141 /****************************************************************************
142  Idle the oldest dptr.
143 ****************************************************************************/
144
145 static void dptr_idleoldest(void)
146 {
147         struct dptr_struct *dptr;
148
149         /*
150          * Go to the end of the list.
151          */
152         for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
153                 ;
154
155         if(!dptr) {
156                 DEBUG(0,("No dptrs available to idle ?\n"));
157                 return;
158         }
159
160         /*
161          * Idle the oldest pointer.
162          */
163
164         for(; dptr; dptr = dptr->prev) {
165                 if (dptr->dir_hnd) {
166                         dptr_idle(dptr);
167                         return;
168                 }
169         }
170 }
171
172 /****************************************************************************
173  Get the struct dptr_struct for a dir index.
174 ****************************************************************************/
175
176 static struct dptr_struct *dptr_get(int key, bool forclose)
177 {
178         struct dptr_struct *dptr;
179
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)
184                                         dptr_idleoldest();
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,
190                                                 strerror(errno)));
191                                         return False;
192                                 }
193                         }
194                         DLIST_PROMOTE(dirptrs,dptr);
195                         return dptr;
196                 }
197         }
198         return(NULL);
199 }
200
201 /****************************************************************************
202  Get the dir path for a dir index.
203 ****************************************************************************/
204
205 char *dptr_path(int key)
206 {
207         struct dptr_struct *dptr = dptr_get(key, False);
208         if (dptr)
209                 return(dptr->path);
210         return(NULL);
211 }
212
213 /****************************************************************************
214  Get the dir wcard for a dir index.
215 ****************************************************************************/
216
217 char *dptr_wcard(int key)
218 {
219         struct dptr_struct *dptr = dptr_get(key, False);
220         if (dptr)
221                 return(dptr->wcard);
222         return(NULL);
223 }
224
225 /****************************************************************************
226  Get the dir attrib for a dir index.
227 ****************************************************************************/
228
229 uint16 dptr_attr(int key)
230 {
231         struct dptr_struct *dptr = dptr_get(key, False);
232         if (dptr)
233                 return(dptr->attr);
234         return(0);
235 }
236
237 /****************************************************************************
238  Close a dptr (internal func).
239 ****************************************************************************/
240
241 static void dptr_close_internal(struct dptr_struct *dptr)
242 {
243         DEBUG(4,("closing dptr key %d\n",dptr->dnum));
244
245         DLIST_REMOVE(dirptrs, dptr);
246
247         /*
248          * Free the dnum in the bitmap. Remember the dnum value is always 
249          * biased by one with respect to the bitmap.
250          */
251
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",
254                         dptr->dnum ));
255         }
256
257         bitmap_clear(dptr_bmap, dptr->dnum - 1);
258
259         TALLOC_FREE(dptr->dir_hnd);
260
261         /* Lanman 2 specific code */
262         SAFE_FREE(dptr->wcard);
263         string_set(&dptr->path,"");
264         SAFE_FREE(dptr);
265 }
266
267 /****************************************************************************
268  Close a dptr given a key.
269 ****************************************************************************/
270
271 void dptr_close(int *key)
272 {
273         struct dptr_struct *dptr;
274
275         if(*key == INVALID_DPTR_KEY)
276                 return;
277
278         /* OS/2 seems to use -1 to indicate "close all directories" */
279         if (*key == -1) {
280                 struct dptr_struct *next;
281                 for(dptr = dirptrs; dptr; dptr = next) {
282                         next = dptr->next;
283                         dptr_close_internal(dptr);
284                 }
285                 *key = INVALID_DPTR_KEY;
286                 return;
287         }
288
289         dptr = dptr_get(*key, True);
290
291         if (!dptr) {
292                 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
293                 return;
294         }
295
296         dptr_close_internal(dptr);
297
298         *key = INVALID_DPTR_KEY;
299 }
300
301 /****************************************************************************
302  Close all dptrs for a cnum.
303 ****************************************************************************/
304
305 void dptr_closecnum(connection_struct *conn)
306 {
307         struct dptr_struct *dptr, *next;
308         for(dptr = dirptrs; dptr; dptr = next) {
309                 next = dptr->next;
310                 if (dptr->conn == conn)
311                         dptr_close_internal(dptr);
312         }
313 }
314
315 /****************************************************************************
316  Idle all dptrs for a cnum.
317 ****************************************************************************/
318
319 void dptr_idlecnum(connection_struct *conn)
320 {
321         struct dptr_struct *dptr;
322         for(dptr = dirptrs; dptr; dptr = dptr->next) {
323                 if (dptr->conn == conn && dptr->dir_hnd)
324                         dptr_idle(dptr);
325         }
326 }
327
328 /****************************************************************************
329  Close a dptr that matches a given path, only if it matches the spid also.
330 ****************************************************************************/
331
332 void dptr_closepath(char *path,uint16 spid)
333 {
334         struct dptr_struct *dptr, *next;
335         for(dptr = dirptrs; dptr; dptr = next) {
336                 next = dptr->next;
337                 if (spid == dptr->spid && strequal(dptr->path,path))
338                         dptr_close_internal(dptr);
339         }
340 }
341
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 ****************************************************************************/
347
348 static void dptr_close_oldest(bool old)
349 {
350         struct dptr_struct *dptr;
351
352         /*
353          * Go to the end of the list.
354          */
355         for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
356                 ;
357
358         if(!dptr) {
359                 DEBUG(0,("No old dptrs available to close oldest ?\n"));
360                 return;
361         }
362
363         /*
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.
367          */
368
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);
373                                 return;
374                 }
375         }
376 }
377
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 ****************************************************************************/
386
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)
389 {
390         struct dptr_struct *dptr = NULL;
391         struct smb_Dir *dir_hnd;
392         NTSTATUS status;
393
394         DEBUG(5,("dptr_create dir=%s\n", path));
395
396         if (!wcard) {
397                 return NT_STATUS_INVALID_PARAMETER;
398         }
399
400         status = check_name(conn,path);
401         if (!NT_STATUS_IS_OK(status)) {
402                 return status;
403         }
404
405         dir_hnd = OpenDir(NULL, conn, path, wcard, attr);
406         if (!dir_hnd) {
407                 return map_nt_error_from_unix(errno);
408         }
409
410         string_set(&conn->dirpath,path);
411
412         if (dirhandles_open >= MAX_OPEN_DIRECTORIES) {
413                 dptr_idleoldest();
414         }
415
416         dptr = SMB_MALLOC_P(struct dptr_struct);
417         if(!dptr) {
418                 DEBUG(0,("malloc fail in dptr_create.\n"));
419                 TALLOC_FREE(dir_hnd);
420                 return NT_STATUS_NO_MEMORY;
421         }
422
423         ZERO_STRUCTP(dptr);
424
425         if(old_handle) {
426
427                 /*
428                  * This is an old-style SMBsearch request. Ensure the
429                  * value we return will fit in the range 1-255.
430                  */
431
432                 dptr->dnum = bitmap_find(dptr_bmap, 0);
433
434                 if(dptr->dnum == -1 || dptr->dnum > 254) {
435
436                         /*
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.
440                          */
441
442                         dptr_close_oldest(True);
443
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));
448                                 SAFE_FREE(dptr);
449                                 TALLOC_FREE(dir_hnd);
450                                 return NT_STATUS_TOO_MANY_OPENED_FILES;
451                         }
452                 }
453         } else {
454
455                 /*
456                  * This is a new-style trans2 request. Allocate from
457                  * a range that will return 256 - MAX_DIRECTORY_HANDLES.
458                  */
459
460                 dptr->dnum = bitmap_find(dptr_bmap, 255);
461
462                 if(dptr->dnum == -1 || dptr->dnum < 255) {
463
464                         /*
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
468                          * directory handles.
469                          */
470
471                         dptr_close_oldest(False);
472
473                         /* Now try again... */
474                         dptr->dnum = bitmap_find(dptr_bmap, 255);
475
476                         if(dptr->dnum == -1 || dptr->dnum < 255) {
477                                 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
478                                 SAFE_FREE(dptr);
479                                 TALLOC_FREE(dir_hnd);
480                                 return NT_STATUS_TOO_MANY_OPENED_FILES;
481                         }
482                 }
483         }
484
485         bitmap_set(dptr_bmap, dptr->dnum);
486
487         dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
488
489         string_set(&dptr->path,path);
490         dptr->conn = conn;
491         dptr->dir_hnd = dir_hnd;
492         dptr->spid = spid;
493         dptr->expect_close = expect_close;
494         dptr->wcard = SMB_STRDUP(wcard);
495         if (!dptr->wcard) {
496                 bitmap_clear(dptr_bmap, dptr->dnum - 1);
497                 SAFE_FREE(dptr);
498                 TALLOC_FREE(dir_hnd);
499                 return NT_STATUS_NO_MEMORY;
500         }
501         if (lp_posix_pathnames() || (wcard[0] == '.' && wcard[1] == 0)) {
502                 dptr->has_wild = True;
503         } else {
504                 dptr->has_wild = wcard_has_wild;
505         }
506
507         dptr->attr = attr;
508
509         DLIST_ADD(dirptrs, dptr);
510
511         DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
512                 dptr->dnum,path,expect_close));  
513
514         *dptr_ret = dptr;
515
516         return NT_STATUS_OK;
517 }
518
519
520 /****************************************************************************
521  Wrapper functions to access the lower level directory handles.
522 ****************************************************************************/
523
524 int dptr_CloseDir(struct dptr_struct *dptr)
525 {
526         DLIST_REMOVE(dirptrs, dptr);
527         TALLOC_FREE(dptr->dir_hnd);
528         return 0;
529 }
530
531 void dptr_SeekDir(struct dptr_struct *dptr, long offset)
532 {
533         SeekDir(dptr->dir_hnd, offset);
534 }
535
536 long dptr_TellDir(struct dptr_struct *dptr)
537 {
538         return TellDir(dptr->dir_hnd);
539 }
540
541 bool dptr_has_wild(struct dptr_struct *dptr)
542 {
543         return dptr->has_wild;
544 }
545
546 int dptr_dnum(struct dptr_struct *dptr)
547 {
548         return dptr->dnum;
549 }
550
551 /****************************************************************************
552  Return the next visible file name, skipping veto'd and invisible files.
553 ****************************************************************************/
554
555 static const char *dptr_normal_ReadDirName(struct dptr_struct *dptr,
556                                            long *poffset, SMB_STRUCT_STAT *pst)
557 {
558         /* Normal search for the next file. */
559         const char *name;
560         while ((name = ReadDirName(dptr->dir_hnd, poffset, pst)) != NULL) {
561                 if (is_visible_file(dptr->conn, dptr->path, name, pst, True)) {
562                         return name;
563                 }
564         }
565         return NULL;
566 }
567
568 /****************************************************************************
569  Return the next visible file name, skipping veto'd and invisible files.
570 ****************************************************************************/
571
572 char *dptr_ReadDirName(TALLOC_CTX *ctx,
573                         struct dptr_struct *dptr,
574                         long *poffset,
575                         SMB_STRUCT_STAT *pst)
576 {
577         struct smb_filename *smb_fname_base = NULL;
578         char *name = NULL;
579         char *pathreal = NULL;
580         char *found_name = NULL;
581         int ret;
582         const char *name_temp = NULL;
583         NTSTATUS status;
584
585         SET_STAT_INVALID(*pst);
586
587         if (dptr->has_wild || dptr->did_stat) {
588                 name_temp = dptr_normal_ReadDirName(dptr, poffset, pst);
589                 name = talloc_strdup(ctx, name_temp);
590                 return name;
591         }
592
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) {
596                 return NULL;
597         }
598
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
602          * searching. */
603
604         dptr->did_stat = true;
605
606         /* First check if it should be visible. */
607         if (!is_visible_file(dptr->conn, dptr->path, dptr->wcard,
608             pst, true))
609         {
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
613                    find it. */
614                 goto ret;
615         }
616
617         if (VALID_STAT(*pst)) {
618                 name = talloc_strdup(ctx, dptr->wcard);
619                 goto ret;
620         }
621
622         pathreal = talloc_asprintf(ctx,
623                                 "%s/%s",
624                                 dptr->path,
625                                 dptr->wcard);
626         if (!pathreal)
627                 return NULL;
628
629         /* Create an smb_filename with stream_name == NULL. */
630         status = create_synthetic_smb_fname(ctx, pathreal, NULL, NULL,
631                                             &smb_fname_base);
632         if (!NT_STATUS_IS_OK(status)) {
633                 return NULL;
634         }
635
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);
640                 goto clean;
641         } else {
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);
647                         goto clean;
648                 }
649         }
650
651         /* Stat failed. We know this is authoratiative if we are
652          * providing case sensitive semantics or the underlying
653          * filesystem is case sensitive.
654          */
655         if (dptr->conn->case_sensitive ||
656             !(dptr->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
657         {
658                 goto clean;
659         }
660
661         /*
662          * Try case-insensitive stat if the fs has the ability. This avoids
663          * scanning the whole directory.
664          */
665         ret = SMB_VFS_GET_REAL_FILENAME(dptr->conn, dptr->path, dptr->wcard,
666                                         ctx, &found_name);
667         if (ret == 0) {
668                 name = found_name;
669                 goto clean;
670         } else if (errno == ENOENT) {
671                 /* The case-insensitive lookup was authoritative. */
672                 goto clean;
673         }
674
675         TALLOC_FREE(pathreal);
676
677         name_temp = dptr_normal_ReadDirName(dptr, poffset, pst);
678         name = talloc_strdup(ctx, name_temp);
679         return name;
680
681 clean:
682         TALLOC_FREE(pathreal);
683 ret:
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;
688         return name;
689 }
690
691 /****************************************************************************
692  Search for a file by name, skipping veto'ed and not visible files.
693 ****************************************************************************/
694
695 bool dptr_SearchDir(struct dptr_struct *dptr, const char *name, long *poffset, SMB_STRUCT_STAT *pst)
696 {
697         SET_STAT_INVALID(*pst);
698
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;
702                 return False;
703         }
704
705         return SearchDir(dptr->dir_hnd, name, poffset);
706 }
707
708 /****************************************************************************
709  Add the name we're returning into the underlying cache.
710 ****************************************************************************/
711
712 void dptr_DirCacheAdd(struct dptr_struct *dptr, const char *name, long offset)
713 {
714         DirCacheAdd(dptr->dir_hnd, name, offset);
715 }
716
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)
721 {
722         SMB_VFS_INIT_SEARCH_OP(dptr->conn, dptr->dir_hnd->dir);
723 }
724
725 /****************************************************************************
726  Fill the 5 byte server reserved dptr field.
727 ****************************************************************************/
728
729 bool dptr_fill(char *buf1,unsigned int key)
730 {
731         unsigned char *buf = (unsigned char *)buf1;
732         struct dptr_struct *dptr = dptr_get(key, False);
733         uint32 offset;
734         if (!dptr) {
735                 DEBUG(1,("filling null dirptr %d\n",key));
736                 return(False);
737         }
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));
741         buf[0] = key;
742         SIVAL(buf,1,offset);
743         return(True);
744 }
745
746 /****************************************************************************
747  Fetch the dir ptr and seek it given the 5 byte server field.
748 ****************************************************************************/
749
750 struct dptr_struct *dptr_fetch(char *buf,int *num)
751 {
752         unsigned int key = *(unsigned char *)buf;
753         struct dptr_struct *dptr = dptr_get(key, False);
754         uint32 offset;
755         long seekoff;
756
757         if (!dptr) {
758                 DEBUG(3,("fetched null dirptr %d\n",key));
759                 return(NULL);
760         }
761         *num = key;
762         offset = IVAL(buf,1);
763         if (offset == (uint32)-1) {
764                 seekoff = END_OF_DIRECTORY_OFFSET;
765         } else {
766                 seekoff = (long)offset;
767         }
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));
771         return(dptr);
772 }
773
774 /****************************************************************************
775  Fetch the dir ptr.
776 ****************************************************************************/
777
778 struct dptr_struct *dptr_fetch_lanman2(int dptr_num)
779 {
780         struct dptr_struct *dptr  = dptr_get(dptr_num, False);
781
782         if (!dptr) {
783                 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
784                 return(NULL);
785         }
786         DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
787         return(dptr);
788 }
789
790 /****************************************************************************
791  Check that a file matches a particular file type.
792 ****************************************************************************/
793
794 bool dir_check_ftype(connection_struct *conn, uint32 mode, uint32 dirtype)
795 {
796         uint32 mask;
797
798         /* Check the "may have" search bits. */
799         if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
800                 return False;
801
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 */
806         if(mask) {
807                 if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask)   /* check if matching attribute present */
808                         return True;
809                 else
810                         return False;
811         }
812
813         return True;
814 }
815
816 static bool mangle_mask_match(connection_struct *conn,
817                 const char *filename,
818                 const char *mask)
819 {
820         char mname[13];
821
822         if (!name_to_8_3(filename,mname,False,conn->params)) {
823                 return False;
824         }
825         return mask_match_search(mname,mask,False);
826 }
827
828 /****************************************************************************
829  Get an 8.3 directory entry.
830 ****************************************************************************/
831
832 bool get_dir_entry(TALLOC_CTX *ctx,
833                 connection_struct *conn,
834                 const char *mask,
835                 uint32 dirtype,
836                 char **pp_fname_out,
837                 SMB_OFF_T *size,
838                 uint32 *mode,
839                 struct timespec *date,
840                 bool check_descend,
841                 bool ask_sharemode)
842 {
843         char *dname = NULL;
844         bool found = False;
845         SMB_STRUCT_STAT sbuf;
846         char *pathreal = NULL;
847         char *filename = NULL;
848         bool needslash;
849
850         *pp_fname_out = NULL;
851
852         needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
853
854         if (!conn->dirptr) {
855                 return(False);
856         }
857
858         while (!found) {
859                 long curoff = dptr_TellDir(conn->dirptr);
860                 dname = dptr_ReadDirName(ctx, conn->dirptr, &curoff, &sbuf);
861
862                 DEBUG(6,("readdir on dirptr 0x%lx now at offset %ld\n",
863                         (long)conn->dirptr,TellDir(conn->dirptr->dir_hnd)));
864
865                 if (dname == NULL) {
866                         return(False);
867                 }
868
869                 filename = dname;
870
871                 /* notice the special *.* handling. This appears to be the only difference
872                         between the wildcard handling in this routine and in the trans2 routines.
873                         see masktest for a demo
874                 */
875                 if ((strcmp(mask,"*.*") == 0) ||
876                     mask_match_search(filename,mask,False) ||
877                     mangle_mask_match(conn,filename,mask)) {
878                         char mname[13];
879
880                         if (!mangle_is_8_3(filename, False, conn->params)) {
881                                 if (!name_to_8_3(filename,mname,False,
882                                            conn->params)) {
883                                         TALLOC_FREE(filename);
884                                         continue;
885                                 }
886                                 filename = talloc_strdup(ctx, mname);
887                                 if (!filename) {
888                                         return False;
889                                 }
890                         }
891
892                         if (needslash) {
893                                 pathreal = talloc_asprintf(ctx,
894                                                 "%s/%s",
895                                                 conn->dirpath,
896                                                 dname);
897                         } else {
898                                 pathreal = talloc_asprintf(ctx,
899                                                 "%s%s",
900                                                 conn->dirpath,
901                                                 dname);
902                         }
903                         if (!pathreal) {
904                                 TALLOC_FREE(filename);
905                                 return False;
906                         }
907
908                         if (!VALID_STAT(sbuf)) {
909                                 struct smb_filename *smb_fname = NULL;
910                                 NTSTATUS status;
911
912                                 /* Create smb_fname with NULL stream_name. */
913                                 status =
914                                     create_synthetic_smb_fname(ctx, pathreal,
915                                                                NULL, NULL,
916                                                                &smb_fname);
917                                 if (!NT_STATUS_IS_OK(status)) {
918                                         TALLOC_FREE(pathreal);
919                                         TALLOC_FREE(filename);
920                                         return NULL;
921                                 }
922
923                                 if ((SMB_VFS_STAT(conn, smb_fname)) != 0) {
924                                         DEBUG(5,("Couldn't stat 1 [%s]. Error "
925                                                  "= %s\n", pathreal,
926                                                  strerror(errno)));
927                                         TALLOC_FREE(smb_fname);
928                                         TALLOC_FREE(pathreal);
929                                         TALLOC_FREE(filename);
930                                         continue;
931                                 }
932                                 sbuf = smb_fname->st;
933                                 TALLOC_FREE(smb_fname);
934                         }
935
936                         *mode = dos_mode(conn,pathreal,&sbuf);
937
938                         if (!dir_check_ftype(conn,*mode,dirtype)) {
939                                 DEBUG(5,("[%s] attribs 0x%x didn't match 0x%x\n",filename,(unsigned int)*mode,(unsigned int)dirtype));
940                                 TALLOC_FREE(pathreal);
941                                 TALLOC_FREE(filename);
942                                 continue;
943                         }
944
945                         *size = sbuf.st_ex_size;
946                         *date = sbuf.st_ex_mtime;
947
948                         if (ask_sharemode) {
949                                 struct timespec write_time_ts;
950                                 struct file_id fileid;
951
952                                 fileid = vfs_file_id_from_sbuf(conn, &sbuf);
953                                 get_file_infos(fileid, NULL, &write_time_ts);
954                                 if (!null_timespec(write_time_ts)) {
955                                         *date = write_time_ts;
956                                 }
957                         }
958
959                         DEBUG(3,("get_dir_entry mask=[%s] found %s "
960                                 "fname=%s (%s)\n",
961                                 mask,
962                                 pathreal,
963                                 dname,
964                                 filename));
965
966                         found = True;
967
968                         SMB_ASSERT(filename != NULL);
969                         *pp_fname_out = filename;
970
971                         DirCacheAdd(conn->dirptr->dir_hnd, dname, curoff);
972                         TALLOC_FREE(pathreal);
973                 }
974
975                 if (!found)
976                         TALLOC_FREE(filename);
977         }
978
979         return(found);
980 }
981
982 /*******************************************************************
983  Check to see if a user can read a file. This is only approximate,
984  it is used as part of the "hide unreadable" option. Don't
985  use it for anything security sensitive.
986 ********************************************************************/
987
988 static bool user_can_read_file(connection_struct *conn,
989                                struct smb_filename *smb_fname)
990 {
991         /*
992          * If user is a member of the Admin group
993          * we never hide files from them.
994          */
995
996         if (conn->admin_user) {
997                 return True;
998         }
999
1000         return can_access_file_acl(conn, smb_fname, FILE_READ_DATA);
1001 }
1002
1003 /*******************************************************************
1004  Check to see if a user can write a file (and only files, we do not
1005  check dirs on this one). This is only approximate,
1006  it is used as part of the "hide unwriteable" option. Don't
1007  use it for anything security sensitive.
1008 ********************************************************************/
1009
1010 static bool user_can_write_file(connection_struct *conn, char *name, SMB_STRUCT_STAT *pst)
1011 {
1012         /*
1013          * If user is a member of the Admin group
1014          * we never hide files from them.
1015          */
1016
1017         if (conn->admin_user) {
1018                 return True;
1019         }
1020
1021         SMB_ASSERT(VALID_STAT(*pst));
1022
1023         /* Pseudo-open the file */
1024
1025         if(S_ISDIR(pst->st_ex_mode)) {
1026                 return True;
1027         }
1028
1029         return can_write_to_file(conn, name, pst);
1030 }
1031
1032 /*******************************************************************
1033   Is a file a "special" type ?
1034 ********************************************************************/
1035
1036 static bool file_is_special(connection_struct *conn, char *name, SMB_STRUCT_STAT *pst)
1037 {
1038         /*
1039          * If user is a member of the Admin group
1040          * we never hide files from them.
1041          */
1042
1043         if (conn->admin_user)
1044                 return False;
1045
1046         SMB_ASSERT(VALID_STAT(*pst));
1047
1048         if (S_ISREG(pst->st_ex_mode) || S_ISDIR(pst->st_ex_mode) || S_ISLNK(pst->st_ex_mode))
1049                 return False;
1050
1051         return True;
1052 }
1053
1054 /*******************************************************************
1055  Should the file be seen by the client?
1056  NOTE: A successful return is no guarantee of the file's existence.
1057 ********************************************************************/
1058
1059 bool is_visible_file(connection_struct *conn, const char *dir_path,
1060                      const char *name, SMB_STRUCT_STAT *pst, bool use_veto)
1061 {
1062         bool hide_unreadable = lp_hideunreadable(SNUM(conn));
1063         bool hide_unwriteable = lp_hideunwriteable_files(SNUM(conn));
1064         bool hide_special = lp_hide_special_files(SNUM(conn));
1065         char *entry = NULL;
1066         struct smb_filename *smb_fname_base = NULL;
1067         NTSTATUS status;
1068         bool ret = false;
1069
1070         if ((strcmp(".",name) == 0) || (strcmp("..",name) == 0)) {
1071                 return True; /* . and .. are always visible. */
1072         }
1073
1074         /* If it's a vetoed file, pretend it doesn't even exist */
1075         if (use_veto && IS_VETO_PATH(conn, name)) {
1076                 DEBUG(10,("is_visible_file: file %s is vetoed.\n", name ));
1077                 return False;
1078         }
1079
1080         if (hide_unreadable || hide_unwriteable || hide_special) {
1081                 entry = talloc_asprintf(talloc_tos(), "%s/%s", dir_path, name);
1082                 if (!entry) {
1083                         ret = false;
1084                         goto out;
1085                 }
1086
1087                 /* If it's a dfs symlink, ignore _hide xxxx_ options */
1088                 if (lp_host_msdfs() &&
1089                                 lp_msdfs_root(SNUM(conn)) &&
1090                                 is_msdfs_link(conn, entry, NULL)) {
1091                         ret = true;
1092                         goto out;
1093                 }
1094
1095                 /* Create an smb_filename with stream_name == NULL. */
1096                 status = create_synthetic_smb_fname(talloc_tos(), entry, NULL,
1097                                                     NULL, &smb_fname_base);
1098                 if (!NT_STATUS_IS_OK(status)) {
1099                         ret = false;
1100                         goto out;
1101                 }
1102
1103                 /* If the file name does not exist, there's no point checking
1104                  * the configuration options. We succeed, on the basis that the
1105                  * checks *might* have passed if the file was present.
1106                  */
1107                 if (!VALID_STAT(*pst) &&
1108                     (SMB_VFS_STAT(conn, smb_fname_base) != 0))
1109                 {
1110                         ret = true;
1111                         goto out;
1112                 }
1113
1114                 *pst = smb_fname_base->st;
1115
1116                 /* Honour _hide unreadable_ option */
1117                 if (hide_unreadable &&
1118                     !user_can_read_file(conn, smb_fname_base)) {
1119                         DEBUG(10,("is_visible_file: file %s is unreadable.\n",
1120                                  entry ));
1121                         ret = false;
1122                         goto out;
1123                 }
1124                 /* Honour _hide unwriteable_ option */
1125                 if (hide_unwriteable && !user_can_write_file(conn, entry, pst)) {
1126                         DEBUG(10,("is_visible_file: file %s is unwritable.\n",
1127                                  entry ));
1128                         ret = false;
1129                         goto out;
1130                 }
1131                 /* Honour _hide_special_ option */
1132                 if (hide_special && file_is_special(conn, entry, pst)) {
1133                         DEBUG(10,("is_visible_file: file %s is special.\n",
1134                                  entry ));
1135                         ret = false;
1136                         goto out;
1137                 }
1138         }
1139
1140         ret = true;
1141  out:
1142         TALLOC_FREE(smb_fname_base);
1143         TALLOC_FREE(entry);
1144         return ret;
1145 }
1146
1147 static int smb_Dir_destructor(struct smb_Dir *dirp)
1148 {
1149         if (dirp->dir) {
1150                 SMB_VFS_CLOSEDIR(dirp->conn,dirp->dir);
1151         }
1152         dirhandles_open--;
1153         return 0;
1154 }
1155
1156 /*******************************************************************
1157  Open a directory.
1158 ********************************************************************/
1159
1160 struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
1161                         const char *name, const char *mask, uint32 attr)
1162 {
1163         struct smb_Dir *dirp = TALLOC_ZERO_P(mem_ctx, struct smb_Dir);
1164
1165         if (!dirp) {
1166                 return NULL;
1167         }
1168
1169         dirp->conn = conn;
1170         dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
1171
1172         dirp->dir_path = talloc_strdup(dirp, name);
1173         if (!dirp->dir_path) {
1174                 errno = ENOMEM;
1175                 goto fail;
1176         }
1177
1178         dirhandles_open++;
1179         talloc_set_destructor(dirp, smb_Dir_destructor);
1180
1181         dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_path, mask, attr);
1182         if (!dirp->dir) {
1183                 DEBUG(5,("OpenDir: Can't open %s. %s\n", dirp->dir_path,
1184                          strerror(errno) ));
1185                 goto fail;
1186         }
1187
1188         return dirp;
1189
1190   fail:
1191         TALLOC_FREE(dirp);
1192         return NULL;
1193 }
1194
1195 /*******************************************************************
1196  Read from a directory.
1197  Return directory entry, current offset, and optional stat information.
1198  Don't check for veto or invisible files.
1199 ********************************************************************/
1200
1201 const char *ReadDirName(struct smb_Dir *dirp, long *poffset,
1202                         SMB_STRUCT_STAT *sbuf)
1203 {
1204         const char *n;
1205         connection_struct *conn = dirp->conn;
1206
1207         /* Cheat to allow . and .. to be the first entries returned. */
1208         if (((*poffset == START_OF_DIRECTORY_OFFSET) ||
1209              (*poffset == DOT_DOT_DIRECTORY_OFFSET)) && (dirp->file_number < 2))
1210         {
1211                 if (dirp->file_number == 0) {
1212                         n = ".";
1213                         *poffset = dirp->offset = START_OF_DIRECTORY_OFFSET;
1214                 } else {
1215                         *poffset = dirp->offset = DOT_DOT_DIRECTORY_OFFSET;
1216                         n = "..";
1217                 }
1218                 dirp->file_number++;
1219                 return n;
1220         } else if (*poffset == END_OF_DIRECTORY_OFFSET) {
1221                 *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1222                 return NULL;
1223         } else {
1224                 /* A real offset, seek to it. */
1225                 SeekDir(dirp, *poffset);
1226         }
1227
1228         while ((n = vfs_readdirname(conn, dirp->dir, sbuf))) {
1229                 /* Ignore . and .. - we've already returned them. */
1230                 if (*n == '.') {
1231                         if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
1232                                 continue;
1233                         }
1234                 }
1235                 *poffset = dirp->offset = SMB_VFS_TELLDIR(conn, dirp->dir);
1236                 dirp->file_number++;
1237                 return n;
1238         }
1239         *poffset = dirp->offset = END_OF_DIRECTORY_OFFSET;
1240         return NULL;
1241 }
1242
1243 /*******************************************************************
1244  Rewind to the start.
1245 ********************************************************************/
1246
1247 void RewindDir(struct smb_Dir *dirp, long *poffset)
1248 {
1249         SMB_VFS_REWINDDIR(dirp->conn, dirp->dir);
1250         dirp->file_number = 0;
1251         dirp->offset = START_OF_DIRECTORY_OFFSET;
1252         *poffset = START_OF_DIRECTORY_OFFSET;
1253 }
1254
1255 /*******************************************************************
1256  Seek a dir.
1257 ********************************************************************/
1258
1259 void SeekDir(struct smb_Dir *dirp, long offset)
1260 {
1261         if (offset != dirp->offset) {
1262                 if (offset == START_OF_DIRECTORY_OFFSET) {
1263                         RewindDir(dirp, &offset);
1264                         /*
1265                          * Ok we should really set the file number here
1266                          * to 1 to enable ".." to be returned next. Trouble
1267                          * is I'm worried about callers using SeekDir(dirp,0)
1268                          * as equivalent to RewindDir(). So leave this alone
1269                          * for now.
1270                          */
1271                 } else if  (offset == DOT_DOT_DIRECTORY_OFFSET) {
1272                         RewindDir(dirp, &offset);
1273                         /*
1274                          * Set the file number to 2 - we want to get the first
1275                          * real file entry (the one we return after "..")
1276                          * on the next ReadDir.
1277                          */
1278                         dirp->file_number = 2;
1279                 } else if (offset == END_OF_DIRECTORY_OFFSET) {
1280                         ; /* Don't seek in this case. */
1281                 } else {
1282                         SMB_VFS_SEEKDIR(dirp->conn, dirp->dir, offset);
1283                 }
1284                 dirp->offset = offset;
1285         }
1286 }
1287
1288 /*******************************************************************
1289  Tell a dir position.
1290 ********************************************************************/
1291
1292 long TellDir(struct smb_Dir *dirp)
1293 {
1294         return(dirp->offset);
1295 }
1296
1297 /*******************************************************************
1298  Add an entry into the dcache.
1299 ********************************************************************/
1300
1301 void DirCacheAdd(struct smb_Dir *dirp, const char *name, long offset)
1302 {
1303         struct name_cache_entry *e;
1304
1305         if (dirp->name_cache_size == 0) {
1306                 return;
1307         }
1308
1309         if (dirp->name_cache == NULL) {
1310                 dirp->name_cache = TALLOC_ZERO_ARRAY(
1311                         dirp, struct name_cache_entry, dirp->name_cache_size);
1312
1313                 if (dirp->name_cache == NULL) {
1314                         return;
1315                 }
1316         }
1317
1318         dirp->name_cache_index = (dirp->name_cache_index+1) %
1319                                         dirp->name_cache_size;
1320         e = &dirp->name_cache[dirp->name_cache_index];
1321         TALLOC_FREE(e->name);
1322         e->name = talloc_strdup(dirp, name);
1323         e->offset = offset;
1324 }
1325
1326 /*******************************************************************
1327  Find an entry by name. Leave us at the offset after it.
1328  Don't check for veto or invisible files.
1329 ********************************************************************/
1330
1331 bool SearchDir(struct smb_Dir *dirp, const char *name, long *poffset)
1332 {
1333         int i;
1334         const char *entry;
1335         connection_struct *conn = dirp->conn;
1336
1337         /* Search back in the name cache. */
1338         if (dirp->name_cache_size && dirp->name_cache) {
1339                 for (i = dirp->name_cache_index; i >= 0; i--) {
1340                         struct name_cache_entry *e = &dirp->name_cache[i];
1341                         if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1342                                 *poffset = e->offset;
1343                                 SeekDir(dirp, e->offset);
1344                                 return True;
1345                         }
1346                 }
1347                 for (i = dirp->name_cache_size - 1; i > dirp->name_cache_index; i--) {
1348                         struct name_cache_entry *e = &dirp->name_cache[i];
1349                         if (e->name && (conn->case_sensitive ? (strcmp(e->name, name) == 0) : strequal(e->name, name))) {
1350                                 *poffset = e->offset;
1351                                 SeekDir(dirp, e->offset);
1352                                 return True;
1353                         }
1354                 }
1355         }
1356
1357         /* Not found in the name cache. Rewind directory and start from scratch. */
1358         SMB_VFS_REWINDDIR(conn, dirp->dir);
1359         dirp->file_number = 0;
1360         *poffset = START_OF_DIRECTORY_OFFSET;
1361         while ((entry = ReadDirName(dirp, poffset, NULL))) {
1362                 if (conn->case_sensitive ? (strcmp(entry, name) == 0) : strequal(entry, name)) {
1363                         return True;
1364                 }
1365         }
1366         return False;
1367 }
1368
1369 /*****************************************************************
1370  Is this directory empty ?
1371 *****************************************************************/
1372
1373 NTSTATUS can_delete_directory(struct connection_struct *conn,
1374                                 const char *dirname)
1375 {
1376         NTSTATUS status = NT_STATUS_OK;
1377         long dirpos = 0;
1378         const char *dname;
1379         SMB_STRUCT_STAT st;
1380         struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, dirname,
1381                                           NULL, 0);
1382
1383         if (!dir_hnd) {
1384                 return map_nt_error_from_unix(errno);
1385         }
1386
1387         while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
1388                 /* Quick check for "." and ".." */
1389                 if (dname[0] == '.') {
1390                         if (!dname[1] || (dname[1] == '.' && !dname[2])) {
1391                                 continue;
1392                         }
1393                 }
1394
1395                 if (!is_visible_file(conn, dirname, dname, &st, True)) {
1396                         continue;
1397                 }
1398
1399                 DEBUG(10,("can_delete_directory: got name %s - can't delete\n",
1400                          dname ));
1401                 status = NT_STATUS_DIRECTORY_NOT_EMPTY;
1402                 break;
1403         }
1404         TALLOC_FREE(dir_hnd);
1405
1406         return status;
1407 }