2 Unix SMB/CIFS implementation.
3 Directory handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 This module implements directory related functions for Samba.
27 typedef struct _dptr_struct {
28 struct _dptr_struct *next, *prev;
31 connection_struct *conn;
34 char *wcard; /* Field only used for trans2_ searches */
35 uint16 attr; /* Field only used for trans2_ searches */
39 static struct bitmap *dptr_bmap;
40 static dptr_struct *dirptrs;
42 static int dptrs_open = 0;
44 #define INVALID_DPTR_KEY (-3)
46 /****************************************************************************
47 Initialise the dir bitmap.
48 ****************************************************************************/
52 static BOOL dptrs_init=False;
57 dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
60 exit_server("out of memory in init_dptrs");
65 /****************************************************************************
66 Idle a dptr - the directory is closed but the control info is kept.
67 ****************************************************************************/
69 static void dptr_idle(dptr_struct *dptr)
72 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
79 /****************************************************************************
81 ****************************************************************************/
83 static void dptr_idleoldest(void)
88 * Go to the end of the list.
90 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
94 DEBUG(0,("No dptrs available to idle ?\n"));
99 * Idle the oldest pointer.
102 for(; dptr; dptr = dptr->prev) {
110 /****************************************************************************
111 Get the dptr_struct for a dir index.
112 ****************************************************************************/
114 static dptr_struct *dptr_get(int key, BOOL forclose)
118 for(dptr = dirptrs; dptr; dptr = dptr->next) {
119 if(dptr->dnum == key) {
120 if (!forclose && !dptr->ptr) {
121 if (dptrs_open >= MAX_OPEN_DIRECTORIES)
123 DEBUG(4,("Reopening dptr key %d\n",key));
124 if ((dptr->ptr = OpenDir(dptr->conn, dptr->path, True)))
127 DLIST_PROMOTE(dirptrs,dptr);
134 /****************************************************************************
135 Get the dptr ptr for a dir index.
136 ****************************************************************************/
138 static void *dptr_ptr(int key)
140 dptr_struct *dptr = dptr_get(key, False);
147 /****************************************************************************
148 Get the dir path for a dir index.
149 ****************************************************************************/
151 char *dptr_path(int key)
153 dptr_struct *dptr = dptr_get(key, False);
160 /****************************************************************************
161 Get the dir wcard for a dir index (lanman2 specific).
162 ****************************************************************************/
164 char *dptr_wcard(int key)
166 dptr_struct *dptr = dptr_get(key, False);
173 /****************************************************************************
174 Set the dir wcard for a dir index (lanman2 specific).
175 Returns 0 on ok, 1 on fail.
176 ****************************************************************************/
178 BOOL dptr_set_wcard(int key, char *wcard)
180 dptr_struct *dptr = dptr_get(key, False);
189 /****************************************************************************
190 Set the dir attrib for a dir index (lanman2 specific).
191 Returns 0 on ok, 1 on fail.
192 ****************************************************************************/
194 BOOL dptr_set_attr(int key, uint16 attr)
196 dptr_struct *dptr = dptr_get(key, False);
205 /****************************************************************************
206 Get the dir attrib for a dir index (lanman2 specific)
207 ****************************************************************************/
209 uint16 dptr_attr(int key)
211 dptr_struct *dptr = dptr_get(key, False);
218 /****************************************************************************
219 Close a dptr (internal func).
220 ****************************************************************************/
222 static void dptr_close_internal(dptr_struct *dptr)
224 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
226 DLIST_REMOVE(dirptrs, dptr);
229 * Free the dnum in the bitmap. Remember the dnum value is always
230 * biased by one with respect to the bitmap.
233 if(bitmap_query( dptr_bmap, dptr->dnum - 1) != True) {
234 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
238 bitmap_clear(dptr_bmap, dptr->dnum - 1);
245 /* Lanman 2 specific code */
246 SAFE_FREE(dptr->wcard);
247 string_set(&dptr->path,"");
251 /****************************************************************************
252 Close a dptr given a key.
253 ****************************************************************************/
255 void dptr_close(int *key)
259 if(*key == INVALID_DPTR_KEY)
262 /* OS/2 seems to use -1 to indicate "close all directories" */
265 for(dptr = dirptrs; dptr; dptr = next) {
267 dptr_close_internal(dptr);
269 *key = INVALID_DPTR_KEY;
273 dptr = dptr_get(*key, True);
276 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
280 dptr_close_internal(dptr);
282 *key = INVALID_DPTR_KEY;
285 /****************************************************************************
286 Close all dptrs for a cnum.
287 ****************************************************************************/
289 void dptr_closecnum(connection_struct *conn)
291 dptr_struct *dptr, *next;
292 for(dptr = dirptrs; dptr; dptr = next) {
294 if (dptr->conn == conn)
295 dptr_close_internal(dptr);
299 /****************************************************************************
300 Idle all dptrs for a cnum.
301 ****************************************************************************/
303 void dptr_idlecnum(connection_struct *conn)
306 for(dptr = dirptrs; dptr; dptr = dptr->next) {
307 if (dptr->conn == conn && dptr->ptr)
312 /****************************************************************************
313 Close a dptr that matches a given path, only if it matches the spid also.
314 ****************************************************************************/
316 void dptr_closepath(char *path,uint16 spid)
318 dptr_struct *dptr, *next;
319 for(dptr = dirptrs; dptr; dptr = next) {
321 if (spid == dptr->spid && strequal(dptr->path,path))
322 dptr_close_internal(dptr);
326 /****************************************************************************
327 Start a directory listing.
328 ****************************************************************************/
330 static BOOL start_dir(connection_struct *conn,char *directory)
332 DEBUG(5,("start_dir dir=%s\n",directory));
334 if (!check_name(directory,conn))
340 conn->dirptr = OpenDir(conn, directory, True);
343 string_set(&conn->dirpath,directory);
350 /****************************************************************************
351 Try and close the oldest handle not marked for
352 expect close in the hope that the client has
353 finished with that one.
354 ****************************************************************************/
356 static void dptr_close_oldest(BOOL old)
361 * Go to the end of the list.
363 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
367 DEBUG(0,("No old dptrs available to close oldest ?\n"));
372 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
373 * does not have expect_close set. If 'old' is false, close
374 * one of the new dnum handles.
377 for(; dptr; dptr = dptr->prev) {
378 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
379 (!old && (dptr->dnum > 255))) {
380 dptr_close_internal(dptr);
386 /****************************************************************************
387 Create a new dir ptr. If the flag old_handle is true then we must allocate
388 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
389 one byte long. If old_handle is false we allocate from the range
390 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
391 a directory handle is never zero. All the above is folklore taught to
392 me at Andrew's knee.... :-) :-). JRA.
393 ****************************************************************************/
395 int dptr_create(connection_struct *conn,char *path, BOOL old_handle, BOOL expect_close,uint16 spid)
399 if (!start_dir(conn,path))
400 return(-2); /* Code to say use a unix error return code. */
402 if (dptrs_open >= MAX_OPEN_DIRECTORIES)
405 dptr = (dptr_struct *)malloc(sizeof(dptr_struct));
407 DEBUG(0,("malloc fail in dptr_create.\n"));
416 * This is an old-style SMBsearch request. Ensure the
417 * value we return will fit in the range 1-255.
420 dptr->dnum = bitmap_find(dptr_bmap, 0);
422 if(dptr->dnum == -1 || dptr->dnum > 254) {
425 * Try and close the oldest handle not marked for
426 * expect close in the hope that the client has
427 * finished with that one.
430 dptr_close_oldest(True);
432 /* Now try again... */
433 dptr->dnum = bitmap_find(dptr_bmap, 0);
435 if(dptr->dnum == -1 || dptr->dnum > 254) {
436 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
444 * This is a new-style trans2 request. Allocate from
445 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
448 dptr->dnum = bitmap_find(dptr_bmap, 255);
450 if(dptr->dnum == -1 || dptr->dnum < 255) {
453 * Try and close the oldest handle close in the hope that
454 * the client has finished with that one. This will only
455 * happen in the case of the Win98 client bug where it leaks
459 dptr_close_oldest(False);
461 /* Now try again... */
462 dptr->dnum = bitmap_find(dptr_bmap, 255);
464 if(dptr->dnum == -1 || dptr->dnum < 255) {
465 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
472 bitmap_set(dptr_bmap, dptr->dnum);
474 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
476 dptr->ptr = conn->dirptr;
477 string_set(&dptr->path,path);
480 dptr->expect_close = expect_close;
481 dptr->wcard = NULL; /* Only used in lanman2 searches */
482 dptr->attr = 0; /* Only used in lanman2 searches */
484 DLIST_ADD(dirptrs, dptr);
486 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
487 dptr->dnum,path,expect_close));
492 /****************************************************************************
493 Fill the 5 byte server reserved dptr field.
494 ****************************************************************************/
496 BOOL dptr_fill(char *buf1,unsigned int key)
498 unsigned char *buf = (unsigned char *)buf1;
499 void *p = dptr_ptr(key);
502 DEBUG(1,("filling null dirptr %d\n",key));
506 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
507 (long)p,(int)offset));
509 SIVAL(buf,1,offset | DPTR_MASK);
513 /****************************************************************************
514 Fetch the dir ptr and seek it given the 5 byte server field.
515 ****************************************************************************/
517 void *dptr_fetch(char *buf,int *num)
519 unsigned int key = *(unsigned char *)buf;
520 void *p = dptr_ptr(key);
523 DEBUG(3,("fetched null dirptr %d\n",key));
527 offset = IVAL(buf,1)&~DPTR_MASK;
529 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
530 key,dptr_path(key),offset));
534 /****************************************************************************
536 ****************************************************************************/
538 void *dptr_fetch_lanman2(int dptr_num)
540 void *p = dptr_ptr(dptr_num);
543 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
546 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
550 /****************************************************************************
551 Check a filetype for being valid.
552 ****************************************************************************/
554 BOOL dir_check_ftype(connection_struct *conn,int mode,SMB_STRUCT_STAT *st,int dirtype)
556 if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
561 /****************************************************************************
562 Get an 8.3 directory entry.
563 ****************************************************************************/
565 BOOL get_dir_entry(connection_struct *conn,char *mask,int dirtype,char *fname,
566 SMB_OFF_T *size,int *mode,time_t *date,BOOL check_descend)
570 SMB_STRUCT_STAT sbuf;
577 *path = *pathreal = *filename = 0;
579 isrootdir = (strequal(conn->dirpath,"./") ||
580 strequal(conn->dirpath,".") ||
581 strequal(conn->dirpath,"/"));
583 needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
590 dname = ReadDirName(conn->dirptr);
592 DEBUG(6,("readdir on dirptr 0x%lx now at offset %d\n",
593 (long)conn->dirptr,TellDir(conn->dirptr)));
598 pstrcpy(filename,dname);
600 /* notice the special *.* handling. This appears to be the only difference
601 between the wildcard handling in this routine and in the trans2 routines.
602 see masktest for a demo
604 if ((strcmp(mask,"*.*") == 0) ||
605 mask_match(filename,mask,False) ||
606 (name_map_mangle(filename,True,False,SNUM(conn)) &&
607 mask_match(filename,mask,False)))
609 if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
612 if (!is_8_3(filename, False)) {
613 name_map_mangle(filename,True,False,SNUM(conn));
616 pstrcpy(fname,filename);
618 pstrcpy(path,conn->dirpath);
621 pstrcpy(pathreal,path);
623 pstrcat(pathreal,dname);
624 if (conn->vfs_ops.stat(conn, pathreal, &sbuf) != 0)
626 DEBUG(5,("Couldn't stat 1 [%s]. Error = %s\n",path, strerror(errno) ));
630 *mode = dos_mode(conn,pathreal,&sbuf);
632 if (!dir_check_ftype(conn,*mode,&sbuf,dirtype))
634 DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
638 *size = sbuf.st_size;
639 *date = sbuf.st_mtime;
641 DEBUG(3,("get_dir_entry mask=[%s] found %s fname=%s\n",mask, pathreal,fname));
663 /*******************************************************************
664 check to see if a user can read a file. This is only approximate,
665 it is used as part of the "hide unreadable" option. Don't
666 use it for anything security sensitive
667 ********************************************************************/
669 static BOOL user_can_read_file(connection_struct *conn, char *name)
671 extern struct current_user current_user;
673 SEC_DESC *psd = NULL;
678 uint32 access_granted;
682 /* if we can't stat it does not show it */
683 if (vfs_stat(conn, name, &ste) != 0)
686 /* Pseudo-open the file (note - no fd's created). */
688 if(S_ISDIR(ste.st_mode))
689 fsp = open_directory(conn, name, &ste, SET_DENY_MODE(DENY_NONE), FILE_OPEN,
690 unix_mode(conn,aRONLY|aDIR, name), &smb_action);
692 fsp = open_file_stat(conn,name,&ste,DOS_OPEN_RDONLY,&smb_action);
696 /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
697 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
698 close_file(fsp, True);
700 /* No access if SD get failed. */
704 return se_access_check(psd, current_user.nt_user_token, FILE_READ_DATA,
705 &access_granted, &status);
708 /* Old - crappy check :-). JRA */
710 if (ste.st_uid == conn->uid) {
711 return (ste.st_mode & S_IRUSR) == S_IRUSR;
714 if (ste.st_gid == conn->gid) {
715 return (ste.st_mode & S_IRGRP) == S_IRGRP;
717 for (i=0; i<conn->ngroups; i++) {
718 if (conn->groups[i] == ste.st_gid) {
719 return (ste.st_mode & S_IRGRP) == S_IRGRP;
724 return (ste.st_mode & S_IROTH) == S_IROTH;
728 /*******************************************************************
730 ********************************************************************/
732 void *OpenDir(connection_struct *conn, char *name, BOOL use_veto)
736 DIR *p = conn->vfs_ops.opendir(conn,name);
739 if (!p) return(NULL);
740 dirp = (Dir *)malloc(sizeof(Dir));
742 DEBUG(0,("Out of memory in OpenDir\n"));
743 conn->vfs_ops.closedir(conn,p);
746 dirp->pos = dirp->numentries = dirp->mallocsize = 0;
747 dirp->data = dirp->current = NULL;
749 while ((n = vfs_readdirname(conn, p)))
755 /* If it's a vetoed file, pretend it doesn't even exist */
756 if (use_veto && conn && IS_VETO_PATH(conn, n)) continue;
758 /* Honour _hide unreadable_ option */
759 if (conn && lp_hideunreadable(SNUM(conn))) {
763 if (asprintf(&entry, "%s/%s/%s", conn->origpath, name, n) > 0) {
764 ret = user_can_read_file(conn, entry);
770 if (used + l > dirp->mallocsize) {
771 int s = MAX(used+l,used+2000);
773 r = (char *)Realloc(dirp->data,s);
775 DEBUG(0,("Out of memory in OpenDir\n"));
779 dirp->mallocsize = s;
780 dirp->current = dirp->data;
782 pstrcpy(dirp->data+used,n);
787 conn->vfs_ops.closedir(conn,p);
788 return((void *)dirp);
792 /*******************************************************************
794 ********************************************************************/
796 void CloseDir(void *p)
799 SAFE_FREE(((Dir *)p)->data);
803 /*******************************************************************
804 Read from a directory.
805 ********************************************************************/
807 char *ReadDirName(void *p)
810 Dir *dirp = (Dir *)p;
812 if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);
815 dirp->current = skip_string(dirp->current,1);
822 /*******************************************************************
824 ********************************************************************/
826 BOOL SeekDir(void *p,int pos)
828 Dir *dirp = (Dir *)p;
830 if (!dirp) return(False);
832 if (pos < dirp->pos) {
833 dirp->current = dirp->data;
837 while (dirp->pos < pos && ReadDirName(p)) ;
839 return(dirp->pos == pos);
842 /*******************************************************************
844 ********************************************************************/
848 Dir *dirp = (Dir *)p;
850 if (!dirp) return(-1);
855 /*******************************************************************************
856 This section manages a global directory cache.
857 (It should probably be split into a separate module. crh)
858 ********************************************************************************/
868 static ubi_dlNewList( dir_cache );
870 /*****************************************************************************
871 Add an entry to the directory cache.
877 *****************************************************************************/
879 void DirCacheAdd( char *path, char *name, char *dname, int snum )
883 dir_cache_entry *entry;
885 /* Allocate the structure & string space in one go so that it can be freed
886 * in one call to free().
888 pathlen = strlen( path ) +1; /* Bytes required to store path (with nul). */
889 namelen = strlen( name ) +1; /* Bytes required to store name (with nul). */
890 entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
893 + strlen( dname ) +1 );
894 if( NULL == entry ) /* Not adding to the cache is not fatal, */
895 return; /* so just return as if nothing happened. */
897 /* Set pointers correctly and load values. */
898 entry->path = pstrcpy( (char *)&entry[1], path);
899 entry->name = pstrcpy( &(entry->path[pathlen]), name);
900 entry->dname = pstrcpy( &(entry->name[namelen]), dname);
903 /* Add the new entry to the linked list. */
904 (void)ubi_dlAddHead( dir_cache, entry );
905 DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );
907 /* Free excess cache entries. */
908 while( DIRCACHESIZE < dir_cache->count )
909 safe_free( ubi_dlRemTail( dir_cache ) );
913 /*****************************************************************************
914 Search for an entry to the directory cache.
918 Output: The dname string of the located entry, or NULL if the entry was
921 Notes: This uses a linear search, which is is okay because of
922 the small size of the cache. Use a splay tree or hash
924 *****************************************************************************/
926 char *DirCacheCheck( char *path, char *name, int snum )
928 dir_cache_entry *entry;
930 for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
932 entry = (dir_cache_entry *)ubi_dlNext( entry ) )
934 if( entry->snum == snum
935 && 0 == strcmp( name, entry->name )
936 && 0 == strcmp( path, entry->path ) )
938 DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
939 return( entry->dname );
946 /*****************************************************************************
947 Remove all cache entries which have an snum that matches the input.
950 *****************************************************************************/
952 void DirCacheFlush(int snum)
954 dir_cache_entry *entry;
957 for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
959 next = ubi_dlNext( entry );
960 if( entry->snum == snum )
961 safe_free( ubi_dlRemThis( dir_cache, entry ) );
962 entry = (dir_cache_entry *)next;