2008-01-20 01:36:34 +03:00
/*
* Store streams in a separate subdirectory
*
* Copyright ( C ) Volker Lendecke , 2007
*
* 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
* the Free Software Foundation ; either version 3 of the License , or
* ( 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
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2011-03-23 00:34:22 +03:00
# include "smbd/smbd.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2008-01-20 01:36:34 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_VFS
/*
* Excerpt from a mail from tridge :
*
* Volker , what I ' m thinking of is this :
* / mount - point / . streams / XX / YY / aaaa . bbbb / namedstream1
* / mount - point / . streams / XX / YY / aaaa . bbbb / namedstream2
*
* where XX / YY is a 2 level hash based on the fsid / inode . " aaaa.bbbb "
* is the fsid / inode . " namedstreamX " is a file named after the stream
* name .
*/
static uint32_t hash_fn ( DATA_BLOB key )
{
uint32_t value ; /* Used to compute the hash value. */
uint32_t i ; /* Used to cycle through random values. */
/* Set the initial value from the key size. */
for ( value = 0x238F13AF * key . length , i = 0 ; i < key . length ; i + + )
value = ( value + ( key . data [ i ] < < ( i * 5 % 24 ) ) ) ;
return ( 1103515243 * value + 12345 ) ;
}
/*
* With the hashing scheme based on the inode we need to protect against
* streams showing up on files with re - used inodes . This can happen if we
* create a stream directory from within Samba , and a local process or NFS
* client deletes the file without deleting the streams directory . When the
* inode is re - used and the stream directory is still around , the streams in
* there would be show up as belonging to the new file .
*
* There are several workarounds for this , probably the easiest one is on
* systems which have a true birthtime stat element : When the file has a later
* birthtime than the streams directory , then we have to recreate the
* directory .
*
* The other workaround is to somehow mark the file as generated by Samba with
* something that a NFS client would not do . The closest one is a special
* xattr value being set . On systems which do not support xattrs , it might be
* an option to put in a special ACL entry for a non - existing group .
*/
2008-12-02 01:20:21 +03:00
static bool file_is_valid ( vfs_handle_struct * handle , const char * path ,
bool check_valid )
2008-01-20 01:36:34 +03:00
{
char buf ;
2008-12-02 01:20:21 +03:00
if ( ! check_valid ) {
return true ;
}
2008-01-20 01:36:34 +03:00
DEBUG ( 10 , ( " file_is_valid (%s) called \n " , path ) ) ;
2009-03-27 01:30:42 +03:00
if ( SMB_VFS_GETXATTR ( handle - > conn , path , SAMBA_XATTR_MARKER ,
2008-01-20 01:36:34 +03:00
& buf , sizeof ( buf ) ) ! = sizeof ( buf ) ) {
DEBUG ( 10 , ( " GETXATTR failed: %s \n " , strerror ( errno ) ) ) ;
return false ;
}
if ( buf ! = ' 1 ' ) {
DEBUG ( 10 , ( " got wrong buffer content: '%c' \n " , buf ) ) ;
return false ;
}
return true ;
}
2008-12-02 01:20:21 +03:00
static bool mark_file_valid ( vfs_handle_struct * handle , const char * path ,
bool check_valid )
2008-01-20 01:36:34 +03:00
{
char buf = ' 1 ' ;
int ret ;
2008-12-02 01:20:21 +03:00
if ( ! check_valid ) {
return true ;
}
2008-01-20 01:36:34 +03:00
DEBUG ( 10 , ( " marking file %s as valid \n " , path ) ) ;
2009-03-27 01:30:42 +03:00
ret = SMB_VFS_SETXATTR ( handle - > conn , path , SAMBA_XATTR_MARKER ,
2008-01-20 01:36:34 +03:00
& buf , sizeof ( buf ) , 0 ) ;
if ( ret = = - 1 ) {
DEBUG ( 10 , ( " SETXATTR failed: %s \n " , strerror ( errno ) ) ) ;
return false ;
}
return true ;
}
2009-06-23 02:26:56 +04:00
/**
* Given an smb_filename , determine the stream directory using the file ' s
* base_name .
*/
static char * stream_dir ( vfs_handle_struct * handle ,
const struct smb_filename * smb_fname ,
2008-01-20 01:36:34 +03:00
const SMB_STRUCT_STAT * base_sbuf , bool create_it )
{
uint32_t hash ;
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_hash = NULL ;
2008-01-20 01:36:34 +03:00
char * result = NULL ;
2009-06-23 02:26:56 +04:00
SMB_STRUCT_STAT base_sbuf_tmp ;
2008-01-20 01:36:34 +03:00
uint8_t first , second ;
char * tmp ;
char * id_hex ;
struct file_id id ;
uint8 id_buf [ 16 ] ;
2008-12-02 01:20:21 +03:00
bool check_valid ;
2008-11-22 09:51:20 +03:00
const char * rootdir ;
2009-06-23 02:26:56 +04:00
NTSTATUS status ;
2008-01-20 01:36:34 +03:00
2008-12-02 01:20:21 +03:00
check_valid = lp_parm_bool ( SNUM ( handle - > conn ) ,
" streams_depot " , " check_valid " , true ) ;
2008-11-16 15:17:16 +03:00
tmp = talloc_asprintf ( talloc_tos ( ) , " %s/.streams " , handle - > conn - > connectpath ) ;
if ( tmp = = NULL ) {
errno = ENOMEM ;
goto fail ;
}
2008-11-22 09:51:20 +03:00
rootdir = lp_parm_const_string (
2008-07-18 13:17:33 +04:00
SNUM ( handle - > conn ) , " streams_depot " , " directory " ,
2008-11-16 15:17:16 +03:00
tmp ) ;
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
/* Stat the base file if it hasn't already been done. */
2008-01-20 01:36:34 +03:00
if ( base_sbuf = = NULL ) {
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_base = NULL ;
status = create_synthetic_smb_fname ( talloc_tos ( ) ,
smb_fname - > base_name ,
NULL , NULL ,
& smb_fname_base ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto fail ;
}
if ( SMB_VFS_NEXT_STAT ( handle , smb_fname_base ) = = - 1 ) {
TALLOC_FREE ( smb_fname_base ) ;
2008-01-20 01:36:34 +03:00
goto fail ;
}
2009-06-23 02:26:56 +04:00
base_sbuf_tmp = smb_fname_base - > st ;
TALLOC_FREE ( smb_fname_base ) ;
} else {
base_sbuf_tmp = * base_sbuf ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
id = SMB_VFS_FILE_ID_CREATE ( handle - > conn , & base_sbuf_tmp ) ;
2008-01-20 01:36:34 +03:00
push_file_id_16 ( ( char * ) id_buf , & id ) ;
hash = hash_fn ( data_blob_const ( id_buf , sizeof ( id_buf ) ) ) ;
first = hash & 0xff ;
second = ( hash > > 8 ) & 0xff ;
2008-11-22 09:51:20 +03:00
id_hex = hex_encode_talloc ( talloc_tos ( ) , id_buf , sizeof ( id_buf ) ) ;
2008-01-20 01:36:34 +03:00
if ( id_hex = = NULL ) {
errno = ENOMEM ;
goto fail ;
}
result = talloc_asprintf ( talloc_tos ( ) , " %s/%2.2X/%2.2X/%s " , rootdir ,
first , second , id_hex ) ;
TALLOC_FREE ( id_hex ) ;
if ( result = = NULL ) {
errno = ENOMEM ;
return NULL ;
}
2009-06-23 02:26:56 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , result , NULL , NULL ,
& smb_fname_hash ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto fail ;
}
if ( SMB_VFS_NEXT_STAT ( handle , smb_fname_hash ) = = 0 ) {
2009-07-01 04:04:38 +04:00
struct smb_filename * smb_fname_new = NULL ;
2008-01-20 01:36:34 +03:00
char * newname ;
2009-06-23 02:26:56 +04:00
if ( ! S_ISDIR ( smb_fname_hash - > st . st_ex_mode ) ) {
2008-01-20 01:36:34 +03:00
errno = EINVAL ;
goto fail ;
}
2009-06-23 02:26:56 +04:00
if ( file_is_valid ( handle , smb_fname - > base_name , check_valid ) ) {
2008-01-20 01:36:34 +03:00
return result ;
}
/*
* Someone has recreated a file under an existing inode
* without deleting the streams directory . For now , just move
* it away .
*/
again :
newname = talloc_asprintf ( talloc_tos ( ) , " lost-%lu " , random ( ) ) ;
if ( newname = = NULL ) {
errno = ENOMEM ;
goto fail ;
}
2009-07-01 04:04:38 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , newname ,
NULL , NULL ,
& smb_fname_new ) ;
TALLOC_FREE ( newname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto fail ;
}
if ( SMB_VFS_NEXT_RENAME ( handle , smb_fname_hash ,
smb_fname_new ) = = - 1 ) {
TALLOC_FREE ( smb_fname_new ) ;
2008-01-20 01:36:34 +03:00
if ( ( errno = = EEXIST ) | | ( errno = = ENOTEMPTY ) ) {
goto again ;
}
goto fail ;
}
2009-07-01 04:04:38 +04:00
TALLOC_FREE ( smb_fname_new ) ;
2008-01-20 01:36:34 +03:00
}
if ( ! create_it ) {
errno = ENOENT ;
goto fail ;
}
if ( ( SMB_VFS_NEXT_MKDIR ( handle , rootdir , 0755 ) ! = 0 )
& & ( errno ! = EEXIST ) ) {
goto fail ;
}
tmp = talloc_asprintf ( result , " %s/%2.2X " , rootdir , first ) ;
if ( tmp = = NULL ) {
errno = ENOMEM ;
goto fail ;
}
if ( ( SMB_VFS_NEXT_MKDIR ( handle , tmp , 0755 ) ! = 0 )
& & ( errno ! = EEXIST ) ) {
goto fail ;
}
TALLOC_FREE ( tmp ) ;
tmp = talloc_asprintf ( result , " %s/%2.2X/%2.2X " , rootdir , first ,
second ) ;
if ( tmp = = NULL ) {
errno = ENOMEM ;
goto fail ;
}
if ( ( SMB_VFS_NEXT_MKDIR ( handle , tmp , 0755 ) ! = 0 )
& & ( errno ! = EEXIST ) ) {
goto fail ;
}
TALLOC_FREE ( tmp ) ;
if ( ( SMB_VFS_NEXT_MKDIR ( handle , result , 0755 ) ! = 0 )
& & ( errno ! = EEXIST ) ) {
goto fail ;
}
2009-06-23 02:26:56 +04:00
if ( ! mark_file_valid ( handle , smb_fname - > base_name , check_valid ) ) {
2008-01-20 01:36:34 +03:00
goto fail ;
}
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_hash ) ;
2008-01-20 01:36:34 +03:00
return result ;
fail :
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_hash ) ;
2008-01-20 01:36:34 +03:00
TALLOC_FREE ( result ) ;
return NULL ;
}
2009-06-16 23:01:13 +04:00
/**
* Given a stream name , populate smb_fname_out with the actual location of the
* stream .
*/
static NTSTATUS stream_smb_fname ( vfs_handle_struct * handle ,
const struct smb_filename * smb_fname ,
struct smb_filename * * smb_fname_out ,
bool create_dir )
{
char * dirname , * stream_fname ;
const char * stype ;
NTSTATUS status ;
* smb_fname_out = NULL ;
2009-08-18 13:34:54 +04:00
stype = strchr_m ( smb_fname - > stream_name + 1 , ' : ' ) ;
if ( stype ) {
if ( StrCaseCmp ( stype , " :$DATA " ) ! = 0 ) {
return NT_STATUS_INVALID_PARAMETER ;
}
}
2009-06-23 02:26:56 +04:00
dirname = stream_dir ( handle , smb_fname , NULL , create_dir ) ;
2009-06-16 23:01:13 +04:00
if ( dirname = = NULL ) {
status = map_nt_error_from_unix ( errno ) ;
goto fail ;
}
stream_fname = talloc_asprintf ( talloc_tos ( ) , " %s/%s " , dirname ,
smb_fname - > stream_name ) ;
if ( stream_fname = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
}
if ( stype = = NULL ) {
/* Append an explicit stream type if one wasn't specified. */
stream_fname = talloc_asprintf ( talloc_tos ( ) , " %s:$DATA " ,
stream_fname ) ;
if ( stream_fname = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
}
} else {
/* Normalize the stream type to upercase. */
strupper_m ( strrchr_m ( stream_fname , ' : ' ) + 1 ) ;
}
DEBUG ( 10 , ( " stream filename = %s \n " , stream_fname ) ) ;
/* Create an smb_filename with stream_name == NULL. */
2009-06-23 02:26:56 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , stream_fname , NULL ,
NULL , smb_fname_out ) ;
2009-06-16 23:01:13 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
return NT_STATUS_OK ;
fail :
DEBUG ( 5 , ( " stream_name failed: %s \n " , strerror ( errno ) ) ) ;
TALLOC_FREE ( * smb_fname_out ) ;
return status ;
}
2008-01-20 01:36:34 +03:00
static NTSTATUS walk_streams ( vfs_handle_struct * handle ,
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_base ,
2008-01-20 01:36:34 +03:00
char * * pdirname ,
bool ( * fn ) ( const char * dirname ,
const char * dirent ,
void * private_data ) ,
void * private_data )
{
char * dirname ;
SMB_STRUCT_DIR * dirhandle = NULL ;
2009-11-16 11:49:23 +03:00
const char * dirent = NULL ;
char * talloced = NULL ;
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
dirname = stream_dir ( handle , smb_fname_base , & smb_fname_base - > st ,
false ) ;
2008-01-20 01:36:34 +03:00
if ( dirname = = NULL ) {
if ( errno = = ENOENT ) {
/*
* no stream around
*/
return NT_STATUS_OK ;
}
return map_nt_error_from_unix ( errno ) ;
}
DEBUG ( 10 , ( " walk_streams: dirname=%s \n " , dirname ) ) ;
dirhandle = SMB_VFS_NEXT_OPENDIR ( handle , dirname , NULL , 0 ) ;
if ( dirhandle = = NULL ) {
TALLOC_FREE ( dirname ) ;
return map_nt_error_from_unix ( errno ) ;
}
2009-11-16 11:49:23 +03:00
while ( ( dirent = vfs_readdirname ( handle - > conn , dirhandle , NULL ,
& talloced ) ) ! = NULL ) {
2008-01-20 01:36:34 +03:00
if ( ISDOT ( dirent ) | | ISDOTDOT ( dirent ) ) {
2009-11-16 11:49:23 +03:00
TALLOC_FREE ( talloced ) ;
2008-01-20 01:36:34 +03:00
continue ;
}
DEBUG ( 10 , ( " walk_streams: dirent=%s \n " , dirent ) ) ;
if ( ! fn ( dirname , dirent , private_data ) ) {
2009-11-16 11:49:23 +03:00
TALLOC_FREE ( talloced ) ;
2008-01-20 01:36:34 +03:00
break ;
}
2009-11-16 11:49:23 +03:00
TALLOC_FREE ( talloced ) ;
2008-01-20 01:36:34 +03:00
}
SMB_VFS_NEXT_CLOSEDIR ( handle , dirhandle ) ;
if ( pdirname ! = NULL ) {
* pdirname = dirname ;
}
else {
TALLOC_FREE ( dirname ) ;
}
return NT_STATUS_OK ;
}
2009-06-23 02:26:56 +04:00
/**
* Helper to stat / lstat the base file of an smb_fname . This will actually
* fills in the stat struct in smb_filename .
*/
static int streams_depot_stat_base ( vfs_handle_struct * handle ,
struct smb_filename * smb_fname ,
bool follow_links )
2008-01-20 01:36:34 +03:00
{
2009-06-23 02:26:56 +04:00
char * tmp_stream_name ;
int result ;
tmp_stream_name = smb_fname - > stream_name ;
smb_fname - > stream_name = NULL ;
if ( follow_links ) {
result = SMB_VFS_NEXT_STAT ( handle , smb_fname ) ;
} else {
result = SMB_VFS_NEXT_LSTAT ( handle , smb_fname ) ;
}
smb_fname - > stream_name = tmp_stream_name ;
return result ;
}
static int streams_depot_stat ( vfs_handle_struct * handle ,
struct smb_filename * smb_fname )
{
struct smb_filename * smb_fname_stream = NULL ;
NTSTATUS status ;
2008-01-20 01:36:34 +03:00
int ret = - 1 ;
2009-06-23 02:26:56 +04:00
DEBUG ( 10 , ( " streams_depot_stat called for [%s] \n " ,
smb_fname_str_dbg ( smb_fname ) ) ) ;
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
if ( ! is_ntfs_stream_smb_fname ( smb_fname ) ) {
return SMB_VFS_NEXT_STAT ( handle , smb_fname ) ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
/* If the default stream is requested, just stat the base file. */
if ( is_ntfs_default_stream_smb_fname ( smb_fname ) ) {
return streams_depot_stat_base ( handle , smb_fname , true ) ;
}
/* Stat the actual stream now. */
status = stream_smb_fname ( handle , smb_fname , & smb_fname_stream ,
false ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
ret = - 1 ;
errno = map_errno_from_nt_status ( status ) ;
2008-01-20 01:36:34 +03:00
goto done ;
}
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_STAT ( handle , smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
/* Update the original smb_fname with the stat info. */
smb_fname - > st = smb_fname_stream - > st ;
2008-01-20 01:36:34 +03:00
done :
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
return ret ;
}
2009-06-23 02:26:56 +04:00
static int streams_depot_lstat ( vfs_handle_struct * handle ,
struct smb_filename * smb_fname )
2008-01-20 01:36:34 +03:00
{
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_stream = NULL ;
NTSTATUS status ;
2008-01-20 01:36:34 +03:00
int ret = - 1 ;
2009-06-23 02:26:56 +04:00
DEBUG ( 10 , ( " streams_depot_lstat called for [%s] \n " ,
smb_fname_str_dbg ( smb_fname ) ) ) ;
if ( ! is_ntfs_stream_smb_fname ( smb_fname ) ) {
return SMB_VFS_NEXT_LSTAT ( handle , smb_fname ) ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
/* If the default stream is requested, just stat the base file. */
if ( is_ntfs_default_stream_smb_fname ( smb_fname ) ) {
return streams_depot_stat_base ( handle , smb_fname , false ) ;
}
/* Stat the actual stream now. */
status = stream_smb_fname ( handle , smb_fname , & smb_fname_stream ,
false ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
ret = - 1 ;
errno = map_errno_from_nt_status ( status ) ;
2008-01-20 01:36:34 +03:00
goto done ;
}
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_LSTAT ( handle , smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
done :
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
return ret ;
}
2009-06-16 23:01:13 +04:00
static int streams_depot_open ( vfs_handle_struct * handle ,
struct smb_filename * smb_fname ,
2008-01-20 01:36:34 +03:00
files_struct * fsp , int flags , mode_t mode )
{
2009-06-16 23:01:13 +04:00
struct smb_filename * smb_fname_stream = NULL ;
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_base = NULL ;
2009-06-16 23:01:13 +04:00
NTSTATUS status ;
2008-01-20 01:36:34 +03:00
int ret = - 1 ;
2009-06-16 23:01:13 +04:00
if ( ! is_ntfs_stream_smb_fname ( smb_fname ) ) {
2009-07-01 04:04:38 +04:00
return SMB_VFS_NEXT_OPEN ( handle , smb_fname , fsp , flags , mode ) ;
2008-01-20 01:36:34 +03:00
}
2009-06-16 23:01:13 +04:00
/* If the default stream is requested, just open the base file. */
if ( is_ntfs_default_stream_smb_fname ( smb_fname ) ) {
char * tmp_stream_name ;
2008-01-20 01:36:34 +03:00
2009-06-16 23:01:13 +04:00
tmp_stream_name = smb_fname - > stream_name ;
smb_fname - > stream_name = NULL ;
ret = SMB_VFS_NEXT_OPEN ( handle , smb_fname , fsp , flags , mode ) ;
smb_fname - > stream_name = tmp_stream_name ;
return ret ;
2008-12-02 00:59:21 +03:00
}
2008-01-20 01:36:34 +03:00
2009-06-16 23:01:13 +04:00
/* Ensure the base file still exists. */
2009-06-23 02:26:56 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) ,
smb_fname - > base_name ,
NULL , NULL ,
& smb_fname_base ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
ret = - 1 ;
errno = map_errno_from_nt_status ( status ) ;
goto done ;
}
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_STAT ( handle , smb_fname_base ) ;
2008-01-20 01:36:34 +03:00
if ( ret = = - 1 ) {
goto done ;
}
2009-06-23 02:26:56 +04:00
/* Determine the stream name, and then open it. */
2009-06-16 23:01:13 +04:00
status = stream_smb_fname ( handle , smb_fname , & smb_fname_stream , true ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
ret = - 1 ;
errno = map_errno_from_nt_status ( status ) ;
2008-01-20 01:36:34 +03:00
goto done ;
}
2009-06-16 23:01:13 +04:00
ret = SMB_VFS_NEXT_OPEN ( handle , smb_fname_stream , fsp , flags , mode ) ;
2008-01-20 01:36:34 +03:00
done :
2009-06-16 23:01:13 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_base ) ;
2008-01-20 01:36:34 +03:00
return ret ;
}
2009-07-02 20:27:44 +04:00
static int streams_depot_unlink ( vfs_handle_struct * handle ,
const struct smb_filename * smb_fname )
2008-01-20 01:36:34 +03:00
{
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_base = NULL ;
NTSTATUS status ;
2008-01-20 01:36:34 +03:00
int ret = - 1 ;
2009-07-02 20:27:44 +04:00
DEBUG ( 10 , ( " streams_depot_unlink called for %s \n " ,
smb_fname_str_dbg ( smb_fname ) ) ) ;
2008-01-20 01:36:34 +03:00
2009-07-02 20:27:44 +04:00
/* If there is a valid stream, just unlink the stream and return. */
if ( is_ntfs_stream_smb_fname ( smb_fname ) & &
! is_ntfs_default_stream_smb_fname ( smb_fname ) ) {
struct smb_filename * smb_fname_stream = NULL ;
2008-01-20 01:36:34 +03:00
2009-07-02 20:27:44 +04:00
status = stream_smb_fname ( handle , smb_fname , & smb_fname_stream ,
false ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
2008-01-20 01:36:34 +03:00
return - 1 ;
}
2009-07-02 20:27:44 +04:00
ret = SMB_VFS_NEXT_UNLINK ( handle , smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
2009-07-02 20:27:44 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:36:34 +03:00
return ret ;
}
/*
* We potentially need to delete the per - inode streams directory
*/
2009-07-02 20:27:44 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , smb_fname - > base_name ,
NULL , NULL , & smb_fname_base ) ;
2009-06-23 02:26:56 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
return - 1 ;
}
2009-02-26 00:46:21 +03:00
if ( lp_posix_pathnames ( ) ) {
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_LSTAT ( handle , smb_fname_base ) ;
2009-02-26 00:46:21 +03:00
} else {
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_STAT ( handle , smb_fname_base ) ;
2009-02-26 00:46:21 +03:00
}
if ( ret = = - 1 ) {
2009-06-23 02:26:56 +04:00
TALLOC_FREE ( smb_fname_base ) ;
2008-01-20 01:36:34 +03:00
return - 1 ;
}
2009-06-23 02:26:56 +04:00
if ( smb_fname_base - > st . st_ex_nlink = = 1 ) {
char * dirname = stream_dir ( handle , smb_fname_base ,
& smb_fname_base - > st , false ) ;
2008-01-20 01:36:34 +03:00
if ( dirname ! = NULL ) {
SMB_VFS_NEXT_RMDIR ( handle , dirname ) ;
}
TALLOC_FREE ( dirname ) ;
}
2009-07-02 20:27:44 +04:00
ret = SMB_VFS_NEXT_UNLINK ( handle , smb_fname ) ;
2009-06-23 02:26:56 +04:00
2009-07-02 20:27:44 +04:00
TALLOC_FREE ( smb_fname_base ) ;
return ret ;
2008-01-20 01:36:34 +03:00
}
2008-12-02 01:10:04 +03:00
static int streams_depot_rename ( vfs_handle_struct * handle ,
2009-07-01 04:04:38 +04:00
const struct smb_filename * smb_fname_src ,
const struct smb_filename * smb_fname_dst )
2008-12-02 01:10:04 +03:00
{
2009-07-01 04:04:38 +04:00
struct smb_filename * smb_fname_src_stream = NULL ;
struct smb_filename * smb_fname_dst_stream = NULL ;
bool src_is_stream , dst_is_stream ;
NTSTATUS status ;
2008-12-02 01:10:04 +03:00
int ret = - 1 ;
DEBUG ( 10 , ( " streams_depot_rename called for %s => %s \n " ,
2009-07-01 04:04:38 +04:00
smb_fname_str_dbg ( smb_fname_src ) ,
smb_fname_str_dbg ( smb_fname_dst ) ) ) ;
2008-12-02 01:10:04 +03:00
2009-07-01 04:04:38 +04:00
src_is_stream = is_ntfs_stream_smb_fname ( smb_fname_src ) ;
dst_is_stream = is_ntfs_stream_smb_fname ( smb_fname_dst ) ;
2008-12-02 01:10:04 +03:00
2009-07-01 04:04:38 +04:00
if ( ! src_is_stream & & ! dst_is_stream ) {
return SMB_VFS_NEXT_RENAME ( handle , smb_fname_src ,
smb_fname_dst ) ;
2008-12-02 01:10:04 +03:00
}
/* for now don't allow renames from or to the default stream */
2009-07-01 04:04:38 +04:00
if ( is_ntfs_default_stream_smb_fname ( smb_fname_src ) | |
is_ntfs_default_stream_smb_fname ( smb_fname_dst ) ) {
2008-12-02 01:10:04 +03:00
errno = ENOSYS ;
goto done ;
}
2009-07-01 04:04:38 +04:00
status = stream_smb_fname ( handle , smb_fname_src , & smb_fname_src_stream ,
false ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto done ;
2008-12-02 01:10:04 +03:00
}
2011-01-21 03:27:56 +03:00
status = stream_smb_fname ( handle , smb_fname_dst ,
2009-07-01 04:04:38 +04:00
& smb_fname_dst_stream , false ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto done ;
2008-12-02 01:10:04 +03:00
}
2009-07-01 04:04:38 +04:00
ret = SMB_VFS_NEXT_RENAME ( handle , smb_fname_src_stream ,
smb_fname_dst_stream ) ;
2008-12-02 01:10:04 +03:00
done :
2009-07-01 04:04:38 +04:00
TALLOC_FREE ( smb_fname_src_stream ) ;
TALLOC_FREE ( smb_fname_dst_stream ) ;
2008-12-02 01:10:04 +03:00
return ret ;
}
2008-01-20 01:36:34 +03:00
static bool add_one_stream ( TALLOC_CTX * mem_ctx , unsigned int * num_streams ,
struct stream_struct * * streams ,
const char * name , SMB_OFF_T size ,
SMB_OFF_T alloc_size )
{
struct stream_struct * tmp ;
tmp = TALLOC_REALLOC_ARRAY ( mem_ctx , * streams , struct stream_struct ,
( * num_streams ) + 1 ) ;
if ( tmp = = NULL ) {
return false ;
}
tmp [ * num_streams ] . name = talloc_strdup ( tmp , name ) ;
if ( tmp [ * num_streams ] . name = = NULL ) {
return false ;
}
tmp [ * num_streams ] . size = size ;
tmp [ * num_streams ] . alloc_size = alloc_size ;
* streams = tmp ;
* num_streams + = 1 ;
return true ;
}
struct streaminfo_state {
TALLOC_CTX * mem_ctx ;
vfs_handle_struct * handle ;
unsigned int num_streams ;
struct stream_struct * streams ;
NTSTATUS status ;
} ;
static bool collect_one_stream ( const char * dirname ,
const char * dirent ,
void * private_data )
{
struct streaminfo_state * state =
( struct streaminfo_state * ) private_data ;
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname = NULL ;
char * sname = NULL ;
NTSTATUS status ;
bool ret ;
2008-01-20 01:36:34 +03:00
2009-06-23 02:26:56 +04:00
sname = talloc_asprintf ( talloc_tos ( ) , " %s/%s " , dirname , dirent ) ;
if ( sname = = NULL ) {
2008-01-20 01:36:34 +03:00
state - > status = NT_STATUS_NO_MEMORY ;
2009-06-23 02:26:56 +04:00
ret = false ;
goto out ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , sname , NULL ,
NULL , & smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
state - > status = status ;
ret = false ;
goto out ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
if ( SMB_VFS_NEXT_STAT ( state - > handle , smb_fname ) = = - 1 ) {
DEBUG ( 10 , ( " Could not stat %s: %s \n " , sname ,
strerror ( errno ) ) ) ;
ret = true ;
goto out ;
}
2008-01-20 01:36:34 +03:00
if ( ! add_one_stream ( state - > mem_ctx ,
& state - > num_streams , & state - > streams ,
2009-06-23 02:26:56 +04:00
dirent , smb_fname - > st . st_ex_size ,
2009-01-27 02:39:40 +03:00
SMB_VFS_GET_ALLOC_SIZE ( state - > handle - > conn , NULL ,
2009-06-23 02:26:56 +04:00
& smb_fname - > st ) ) ) {
2008-01-20 01:36:34 +03:00
state - > status = NT_STATUS_NO_MEMORY ;
2009-06-23 02:26:56 +04:00
ret = false ;
goto out ;
2008-01-20 01:36:34 +03:00
}
2009-06-23 02:26:56 +04:00
ret = true ;
out :
TALLOC_FREE ( sname ) ;
TALLOC_FREE ( smb_fname ) ;
return ret ;
2008-01-20 01:36:34 +03:00
}
static NTSTATUS streams_depot_streaminfo ( vfs_handle_struct * handle ,
struct files_struct * fsp ,
const char * fname ,
TALLOC_CTX * mem_ctx ,
unsigned int * pnum_streams ,
struct stream_struct * * pstreams )
{
2009-06-23 02:26:56 +04:00
struct smb_filename * smb_fname_base = NULL ;
2008-01-20 01:36:34 +03:00
int ret ;
NTSTATUS status ;
struct streaminfo_state state ;
2009-06-23 02:26:56 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , fname , NULL , NULL ,
& smb_fname_base ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
2008-01-20 01:36:34 +03:00
if ( ( fsp ! = NULL ) & & ( fsp - > fh - > fd ! = - 1 ) ) {
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_FSTAT ( handle , fsp , & smb_fname_base - > st ) ;
2008-01-20 01:36:34 +03:00
}
else {
2009-02-26 00:46:21 +03:00
if ( lp_posix_pathnames ( ) ) {
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_LSTAT ( handle , smb_fname_base ) ;
2009-02-26 00:46:21 +03:00
} else {
2009-06-23 02:26:56 +04:00
ret = SMB_VFS_NEXT_STAT ( handle , smb_fname_base ) ;
2009-02-26 00:46:21 +03:00
}
2008-01-20 01:36:34 +03:00
}
if ( ret = = - 1 ) {
2009-06-23 02:26:56 +04:00
status = map_nt_error_from_unix ( errno ) ;
goto out ;
2008-01-20 01:36:34 +03:00
}
state . streams = NULL ;
state . num_streams = 0 ;
2009-06-23 02:26:56 +04:00
if ( ! S_ISDIR ( smb_fname_base - > st . st_ex_mode ) ) {
2008-01-20 01:36:34 +03:00
if ( ! add_one_stream ( mem_ctx ,
& state . num_streams , & state . streams ,
2009-06-23 02:26:56 +04:00
" ::$DATA " , smb_fname_base - > st . st_ex_size ,
2009-01-27 02:39:40 +03:00
SMB_VFS_GET_ALLOC_SIZE ( handle - > conn , fsp ,
2009-06-23 02:26:56 +04:00
& smb_fname_base - > st ) ) ) {
status = NT_STATUS_NO_MEMORY ;
goto out ;
2008-01-20 01:36:34 +03:00
}
}
state . mem_ctx = mem_ctx ;
state . handle = handle ;
state . status = NT_STATUS_OK ;
2009-06-23 02:26:56 +04:00
status = walk_streams ( handle , smb_fname_base , NULL , collect_one_stream ,
2008-01-20 01:36:34 +03:00
& state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( state . streams ) ;
2009-06-23 02:26:56 +04:00
goto out ;
2008-01-20 01:36:34 +03:00
}
if ( ! NT_STATUS_IS_OK ( state . status ) ) {
TALLOC_FREE ( state . streams ) ;
2009-06-23 02:26:56 +04:00
status = state . status ;
goto out ;
2008-01-20 01:36:34 +03:00
}
* pnum_streams = state . num_streams ;
* pstreams = state . streams ;
2009-06-23 02:26:56 +04:00
status = NT_STATUS_OK ;
out :
TALLOC_FREE ( smb_fname_base ) ;
return status ;
2008-01-20 01:36:34 +03:00
}
2009-08-25 07:57:37 +04:00
static uint32_t streams_depot_fs_capabilities ( struct vfs_handle_struct * handle ,
enum timestamp_set_resolution * p_ts_res )
2008-01-20 01:36:34 +03:00
{
2009-08-25 07:57:37 +04:00
return SMB_VFS_NEXT_FS_CAPABILITIES ( handle , p_ts_res ) | FILE_NAMED_STREAMS ;
2008-01-20 01:36:34 +03:00
}
2009-07-24 04:28:58 +04:00
static struct vfs_fn_pointers vfs_streams_depot_fns = {
. fs_capabilities = streams_depot_fs_capabilities ,
. open = streams_depot_open ,
. stat = streams_depot_stat ,
. lstat = streams_depot_lstat ,
. unlink = streams_depot_unlink ,
. rename = streams_depot_rename ,
. streaminfo = streams_depot_streaminfo ,
2008-01-20 01:36:34 +03:00
} ;
NTSTATUS vfs_streams_depot_init ( void ) ;
NTSTATUS vfs_streams_depot_init ( void )
{
return smb_register_vfs ( SMB_VFS_INTERFACE_VERSION , " streams_depot " ,
2009-07-24 04:28:58 +04:00
& vfs_streams_depot_fns ) ;
2008-01-20 01:36:34 +03:00
}