2004-04-22 19:30:42 +04:00
/*
* implementation of an Shadow Copy module
*
* Copyright ( C ) Stefan Metzmacher 2003 - 2004
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
* the Free Software Foundation ; either version 3 of the License , or
2004-04-22 19:30:42 +04:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2007-07-10 09:23:25 +04:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2004-04-22 19:30:42 +04:00
*/
# include "includes.h"
2011-03-23 00:34:22 +03:00
# include "smbd/smbd.h"
2011-03-25 15:42:42 +03:00
# include "ntioctl.h"
2004-04-22 19:30:42 +04:00
/*
Please read the VFS module Samba - HowTo - Collection .
there ' s a chapter about this module
For this share
Z : \
the ShadowCopies are in this directories
Z : \ @ GMT - 2003.08 .05 - 12.00 .00 \
Z : \ @ GMT - 2003.08 .05 - 12.01 .00 \
Z : \ @ GMT - 2003.08 .05 - 12.02 .00 \
e . g .
Z : \ testfile . txt
Z : \ @ GMT - 2003.08 .05 - 12.02 .00 \ testfile . txt
or :
Z : \ testdir \ testfile . txt
Z : \ @ GMT - 2003.08 .05 - 12.02 .00 \ testdir \ testfile . txt
Note : Files must differ to be displayed via Windows Explorer !
Directories are always displayed . . .
*/
static int vfs_shadow_copy_debug_level = DBGC_VFS ;
# undef DBGC_CLASS
# define DBGC_CLASS vfs_shadow_copy_debug_level
# define SHADOW_COPY_PREFIX "@GMT-"
# define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
typedef struct {
int pos ;
int num ;
2012-03-28 06:18:14 +04:00
struct dirent * dirs ;
2004-04-22 19:30:42 +04:00
} shadow_copy_Dir ;
2007-10-19 04:40:25 +04:00
static bool shadow_copy_match_name ( const char * name )
2004-04-22 19:30:42 +04:00
{
if ( strncmp ( SHADOW_COPY_PREFIX , name , sizeof ( SHADOW_COPY_PREFIX ) - 1 ) = = 0 & &
( strlen ( SHADOW_COPY_SAMPLE ) = = strlen ( name ) ) ) {
return True ;
}
return False ;
}
2012-03-28 06:22:03 +04:00
static DIR * shadow_copy_opendir ( vfs_handle_struct * handle , const char * fname , const char * mask , uint32 attr )
2004-04-22 19:30:42 +04:00
{
shadow_copy_Dir * dirp ;
2012-03-28 06:22:03 +04:00
DIR * p = SMB_VFS_NEXT_OPENDIR ( handle , fname , mask , attr ) ;
2004-04-22 19:30:42 +04:00
if ( ! p ) {
DEBUG ( 0 , ( " shadow_copy_opendir: SMB_VFS_NEXT_OPENDIR() failed for [%s] \n " , fname ) ) ;
return NULL ;
}
2004-12-07 21:25:53 +03:00
dirp = SMB_MALLOC_P ( shadow_copy_Dir ) ;
2004-04-22 19:30:42 +04:00
if ( ! dirp ) {
DEBUG ( 0 , ( " shadow_copy_opendir: Out of memory \n " ) ) ;
2006-07-11 22:01:26 +04:00
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
2004-04-22 19:30:42 +04:00
return NULL ;
}
ZERO_STRUCTP ( dirp ) ;
while ( True ) {
2012-03-28 06:18:14 +04:00
struct dirent * d ;
2004-04-22 19:30:42 +04:00
2009-01-23 07:14:38 +03:00
d = SMB_VFS_NEXT_READDIR ( handle , p , NULL ) ;
2004-04-22 19:30:42 +04:00
if ( d = = NULL ) {
break ;
}
if ( shadow_copy_match_name ( d - > d_name ) ) {
DEBUG ( 8 , ( " shadow_copy_opendir: hide [%s] \n " , d - > d_name ) ) ;
continue ;
}
DEBUG ( 10 , ( " shadow_copy_opendir: not hide [%s] \n " , d - > d_name ) ) ;
2012-03-28 06:18:14 +04:00
dirp - > dirs = SMB_REALLOC_ARRAY ( dirp - > dirs , struct dirent , dirp - > num + 1 ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! dirp - > dirs ) {
2004-04-22 19:30:42 +04:00
DEBUG ( 0 , ( " shadow_copy_opendir: Out of memory \n " ) ) ;
break ;
}
dirp - > dirs [ dirp - > num + + ] = * d ;
}
2006-07-11 22:01:26 +04:00
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
2012-03-28 06:22:03 +04:00
return ( ( DIR * ) dirp ) ;
2004-04-22 19:30:42 +04:00
}
2012-03-28 06:22:03 +04:00
static DIR * shadow_copy_fdopendir ( vfs_handle_struct * handle , files_struct * fsp , const char * mask , uint32 attr )
2011-02-09 02:07:48 +03:00
{
shadow_copy_Dir * dirp ;
2012-03-28 06:22:03 +04:00
DIR * p = SMB_VFS_NEXT_FDOPENDIR ( handle , fsp , mask , attr ) ;
2011-02-09 02:07:48 +03:00
if ( ! p ) {
DEBUG ( 10 , ( " shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s] \n " ,
smb_fname_str_dbg ( fsp - > fsp_name ) ) ) ;
return NULL ;
}
dirp = SMB_MALLOC_P ( shadow_copy_Dir ) ;
if ( ! dirp ) {
DEBUG ( 0 , ( " shadow_copy_fdopendir: Out of memory \n " ) ) ;
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
/* We have now closed the fd in fsp. */
fsp - > fh - > fd = - 1 ;
return NULL ;
}
ZERO_STRUCTP ( dirp ) ;
while ( True ) {
2012-03-28 06:18:14 +04:00
struct dirent * d ;
2011-02-09 02:07:48 +03:00
d = SMB_VFS_NEXT_READDIR ( handle , p , NULL ) ;
if ( d = = NULL ) {
break ;
}
if ( shadow_copy_match_name ( d - > d_name ) ) {
DEBUG ( 8 , ( " shadow_copy_fdopendir: hide [%s] \n " , d - > d_name ) ) ;
continue ;
}
DEBUG ( 10 , ( " shadow_copy_fdopendir: not hide [%s] \n " , d - > d_name ) ) ;
2012-03-28 06:18:14 +04:00
dirp - > dirs = SMB_REALLOC_ARRAY ( dirp - > dirs , struct dirent , dirp - > num + 1 ) ;
2011-02-09 02:07:48 +03:00
if ( ! dirp - > dirs ) {
DEBUG ( 0 , ( " shadow_copy_fdopendir: Out of memory \n " ) ) ;
break ;
}
dirp - > dirs [ dirp - > num + + ] = * d ;
}
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
/* We have now closed the fd in fsp. */
fsp - > fh - > fd = - 1 ;
2012-03-28 06:22:03 +04:00
return ( ( DIR * ) dirp ) ;
2011-02-09 02:07:48 +03:00
}
2012-03-28 06:18:14 +04:00
static struct dirent * shadow_copy_readdir ( vfs_handle_struct * handle ,
2012-03-28 06:22:03 +04:00
DIR * _dirp ,
2009-07-24 04:28:58 +04:00
SMB_STRUCT_STAT * sbuf )
2004-04-22 19:30:42 +04:00
{
shadow_copy_Dir * dirp = ( shadow_copy_Dir * ) _dirp ;
if ( dirp - > pos < dirp - > num ) {
return & ( dirp - > dirs [ dirp - > pos + + ] ) ;
}
return NULL ;
}
2012-03-28 06:22:03 +04:00
static void shadow_copy_seekdir ( struct vfs_handle_struct * handle , DIR * _dirp , long offset )
2005-06-13 20:49:57 +04:00
{
shadow_copy_Dir * dirp = ( shadow_copy_Dir * ) _dirp ;
if ( offset < dirp - > num ) {
dirp - > pos = offset ;
}
}
2012-03-28 06:22:03 +04:00
static long shadow_copy_telldir ( struct vfs_handle_struct * handle , DIR * _dirp )
2005-06-13 20:49:57 +04:00
{
shadow_copy_Dir * dirp = ( shadow_copy_Dir * ) _dirp ;
return ( dirp - > pos ) ;
}
2012-03-28 06:22:03 +04:00
static void shadow_copy_rewinddir ( struct vfs_handle_struct * handle , DIR * _dirp )
2005-06-13 20:49:57 +04:00
{
shadow_copy_Dir * dirp = ( shadow_copy_Dir * ) _dirp ;
dirp - > pos = 0 ;
}
2012-03-28 06:22:03 +04:00
static int shadow_copy_closedir ( vfs_handle_struct * handle , DIR * _dirp )
2004-04-22 19:30:42 +04:00
{
shadow_copy_Dir * dirp = ( shadow_copy_Dir * ) _dirp ;
2005-01-14 23:23:22 +03:00
SAFE_FREE ( dirp - > dirs ) ;
2004-04-22 19:30:42 +04:00
SAFE_FREE ( dirp ) ;
return 0 ;
}
2011-05-30 14:06:31 +04:00
static int shadow_copy_get_shadow_copy_data ( vfs_handle_struct * handle ,
files_struct * fsp ,
struct shadow_copy_data * shadow_copy_data ,
bool labels )
2004-04-22 19:30:42 +04:00
{
2012-03-28 06:22:03 +04:00
DIR * p = SMB_VFS_NEXT_OPENDIR ( handle , fsp - > conn - > connectpath , NULL , 0 ) ;
2004-04-22 19:30:42 +04:00
shadow_copy_data - > num_volumes = 0 ;
shadow_copy_data - > labels = NULL ;
if ( ! p ) {
DEBUG ( 0 , ( " shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s] \n " , fsp - > conn - > connectpath ) ) ;
return - 1 ;
}
while ( True ) {
SHADOW_COPY_LABEL * tlabels ;
2012-03-28 06:18:14 +04:00
struct dirent * d ;
2004-04-22 19:30:42 +04:00
2009-01-23 07:14:38 +03:00
d = SMB_VFS_NEXT_READDIR ( handle , p , NULL ) ;
2004-04-22 19:30:42 +04:00
if ( d = = NULL ) {
break ;
}
/* */
if ( ! shadow_copy_match_name ( d - > d_name ) ) {
DEBUG ( 10 , ( " shadow_copy_get_shadow_copy_data: ignore [%s] \n " , d - > d_name ) ) ;
continue ;
}
DEBUG ( 7 , ( " shadow_copy_get_shadow_copy_data: not ignore [%s] \n " , d - > d_name ) ) ;
if ( ! labels ) {
shadow_copy_data - > num_volumes + + ;
continue ;
}
2011-05-30 14:11:53 +04:00
tlabels = ( SHADOW_COPY_LABEL * ) TALLOC_REALLOC ( shadow_copy_data ,
2004-04-22 19:30:42 +04:00
shadow_copy_data - > labels ,
( shadow_copy_data - > num_volumes + 1 ) * sizeof ( SHADOW_COPY_LABEL ) ) ;
if ( tlabels = = NULL ) {
DEBUG ( 0 , ( " shadow_copy_get_shadow_copy_data: Out of memory \n " ) ) ;
2006-07-11 22:01:26 +04:00
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
2004-04-22 19:30:42 +04:00
return - 1 ;
}
snprintf ( tlabels [ shadow_copy_data - > num_volumes + + ] , sizeof ( * tlabels ) , " %s " , d - > d_name ) ;
shadow_copy_data - > labels = tlabels ;
}
2006-07-11 22:01:26 +04:00
SMB_VFS_NEXT_CLOSEDIR ( handle , p ) ;
2004-04-22 19:30:42 +04:00
return 0 ;
}
2009-07-24 04:28:58 +04:00
static struct vfs_fn_pointers vfs_shadow_copy_fns = {
2011-12-04 08:45:04 +04:00
. opendir_fn = shadow_copy_opendir ,
. fdopendir_fn = shadow_copy_fdopendir ,
. readdir_fn = shadow_copy_readdir ,
. seekdir_fn = shadow_copy_seekdir ,
. telldir_fn = shadow_copy_telldir ,
. rewind_dir_fn = shadow_copy_rewinddir ,
. closedir_fn = shadow_copy_closedir ,
. get_shadow_copy_data_fn = shadow_copy_get_shadow_copy_data ,
2004-04-22 19:30:42 +04:00
} ;
2006-12-19 23:16:52 +03:00
NTSTATUS vfs_shadow_copy_init ( void ) ;
2004-04-22 19:30:42 +04:00
NTSTATUS vfs_shadow_copy_init ( void )
{
2009-07-24 04:28:58 +04:00
NTSTATUS ret = smb_register_vfs ( SMB_VFS_INTERFACE_VERSION ,
" shadow_copy " , & vfs_shadow_copy_fns ) ;
2004-04-22 19:30:42 +04:00
if ( ! NT_STATUS_IS_OK ( ret ) )
return ret ;
vfs_shadow_copy_debug_level = debug_add_class ( " shadow_copy " ) ;
if ( vfs_shadow_copy_debug_level = = - 1 ) {
vfs_shadow_copy_debug_level = DBGC_VFS ;
DEBUG ( 0 , ( " %s: Couldn't register custom debugging class! \n " ,
" vfs_shadow_copy_init " ) ) ;
} else {
DEBUG ( 10 , ( " %s: Debug class number of '%s': %d \n " ,
" vfs_shadow_copy_init " , " shadow_copy " , vfs_shadow_copy_debug_level ) ) ;
}
return ret ;
}