2007-09-13 03:50:21 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-08-17 17:11:34 +04:00
file closing
Copyright ( C ) Andrew Tridgell 1992 - 1998
2007-02-07 00:05:34 +03:00
Copyright ( C ) Jeremy Allison 1992 - 2007.
2005-07-08 08:51:27 +04:00
Copyright ( C ) Volker Lendecke 2005
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +04:00
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
1998-08-17 17:11:34 +04:00
( at your option ) any later version .
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +04:00
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 .
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1998-08-17 17:11:34 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2010-05-04 12:28:48 +04:00
# include "printing.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2010-07-04 20:25:02 +04:00
# include "smbd/globals.h"
2010-10-03 12:46:22 +04:00
# include "fake_file.h"
2011-02-26 02:56:46 +03:00
# include "transfer_file.h"
2011-03-24 15:46:20 +03:00
# include "auth.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-11-24 17:11:28 +04:00
# include "../librpc/gen_ndr/open_files.h"
1998-08-17 17:11:34 +04:00
/****************************************************************************
2003-01-03 21:50:13 +03:00
Run a file if it is a magic script .
1998-08-17 17:11:34 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 21:50:13 +03:00
2009-06-07 01:27:31 +04:00
static NTSTATUS check_magic ( struct files_struct * fsp )
1998-08-17 17:11:34 +04:00
{
2007-09-13 03:50:21 +04:00
int ret ;
const char * magic_output = NULL ;
SMB_STRUCT_STAT st ;
int tmp_fd , outfd ;
TALLOC_CTX * ctx = NULL ;
const char * p ;
2007-12-22 04:13:02 +03:00
struct connection_struct * conn = fsp - > conn ;
2009-07-11 01:50:37 +04:00
char * fname = NULL ;
NTSTATUS status ;
2007-09-13 03:50:21 +04:00
2012-07-18 09:37:23 +04:00
if ( ! * lp_magicscript ( talloc_tos ( ) , SNUM ( conn ) ) ) {
2009-06-07 01:27:31 +04:00
return NT_STATUS_OK ;
2007-09-13 03:50:21 +04:00
}
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
DEBUG ( 5 , ( " checking magic for %s \n " , fsp_str_dbg ( fsp ) ) ) ;
ctx = talloc_stackframe ( ) ;
2009-07-21 22:35:17 +04:00
fname = fsp - > fsp_name - > base_name ;
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
if ( ! ( p = strrchr_m ( fname , ' / ' ) ) ) {
p = fname ;
2007-09-13 03:50:21 +04:00
} else {
p + + ;
}
2003-01-03 21:50:13 +03:00
2012-07-18 09:37:23 +04:00
if ( ! strequal ( lp_magicscript ( talloc_tos ( ) , SNUM ( conn ) ) , p ) ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_OK ;
goto out ;
2001-04-13 04:37:00 +04:00
}
2012-07-18 09:37:23 +04:00
if ( * lp_magicoutput ( talloc_tos ( ) , SNUM ( conn ) ) ) {
magic_output = lp_magicoutput ( talloc_tos ( ) , SNUM ( conn ) ) ;
2007-09-13 03:50:21 +04:00
} else {
magic_output = talloc_asprintf ( ctx ,
" %s.out " ,
2009-07-11 01:50:37 +04:00
fname ) ;
2007-09-13 03:50:21 +04:00
}
if ( ! magic_output ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_NO_MEMORY ;
goto out ;
2007-09-13 03:50:21 +04:00
}
2009-01-07 03:05:28 +03:00
/* Ensure we don't depend on user's PATH. */
2009-07-11 01:50:37 +04:00
p = talloc_asprintf ( ctx , " ./%s " , fname ) ;
2009-01-07 03:05:28 +03:00
if ( ! p ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_NO_MEMORY ;
goto out ;
2009-01-07 03:05:28 +03:00
}
2009-07-11 01:50:37 +04:00
if ( chmod ( fname , 0755 ) = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
goto out ;
2009-01-07 03:05:28 +03:00
}
ret = smbrun ( p , & tmp_fd ) ;
2007-09-13 03:50:21 +04:00
DEBUG ( 3 , ( " Invoking magic command %s gave %d \n " ,
2009-01-07 03:05:28 +03:00
p , ret ) ) ;
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
unlink ( fname ) ;
2007-09-13 03:50:21 +04:00
if ( ret ! = 0 | | tmp_fd = = - 1 ) {
if ( tmp_fd ! = - 1 ) {
2003-01-03 21:50:13 +03:00
close ( tmp_fd ) ;
}
2009-07-11 01:50:37 +04:00
status = NT_STATUS_UNSUCCESSFUL ;
goto out ;
2007-09-13 03:50:21 +04:00
}
outfd = open ( magic_output , O_CREAT | O_EXCL | O_RDWR , 0600 ) ;
if ( outfd = = - 1 ) {
2009-06-07 01:27:31 +04:00
int err = errno ;
2007-09-13 03:50:21 +04:00
close ( tmp_fd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2007-09-13 03:50:21 +04:00
}
2003-01-03 21:50:13 +03:00
2009-11-27 14:58:26 +03:00
if ( sys_fstat ( tmp_fd , & st , false ) = = - 1 ) {
2009-06-07 01:27:31 +04:00
int err = errno ;
2001-04-13 04:37:00 +04:00
close ( tmp_fd ) ;
close ( outfd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2001-04-13 04:37:00 +04:00
}
2007-09-13 03:50:21 +04:00
2012-04-05 08:53:08 +04:00
if ( transfer_file ( tmp_fd , outfd , ( off_t ) st . st_ex_size ) = = ( off_t ) - 1 ) {
2009-06-13 00:55:18 +04:00
int err = errno ;
close ( tmp_fd ) ;
close ( outfd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2009-06-13 00:55:18 +04:00
}
2007-09-13 03:50:21 +04:00
close ( tmp_fd ) ;
2009-06-07 01:27:31 +04:00
if ( close ( outfd ) = = - 1 ) {
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( errno ) ;
goto out ;
2009-06-07 01:27:31 +04:00
}
2009-07-11 01:50:37 +04:00
status = NT_STATUS_OK ;
out :
2007-09-13 03:50:21 +04:00
TALLOC_FREE ( ctx ) ;
2009-07-11 01:50:37 +04:00
return status ;
1998-08-17 17:11:34 +04:00
}
/****************************************************************************
Common code to close a file or a directory .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-22 04:33:16 +04:00
2007-02-07 00:05:34 +03:00
static NTSTATUS close_filestruct ( files_struct * fsp )
2007-09-13 03:50:21 +04:00
{
2007-02-07 00:05:34 +03:00
NTSTATUS status = NT_STATUS_OK ;
2008-11-21 23:14:53 +03:00
2005-07-08 08:51:27 +04:00
if ( fsp - > fh - > fd ! = - 1 ) {
2007-02-07 00:05:34 +03:00
if ( flush_write_cache ( fsp , CLOSE_FLUSH ) = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
}
2002-03-20 03:46:53 +03:00
delete_write_cache ( fsp ) ;
}
1999-12-13 16:27:58 +03:00
2007-02-07 00:05:34 +03:00
return status ;
2008-11-21 23:14:53 +03:00
}
1998-08-17 17:11:34 +04:00
2011-12-14 19:54:07 +04:00
static int compare_share_mode_times ( const void * p1 , const void * p2 )
{
2012-02-24 02:49:02 +04:00
const struct share_mode_entry * s1 = ( const struct share_mode_entry * ) p1 ;
const struct share_mode_entry * s2 = ( const struct share_mode_entry * ) p2 ;
2011-12-14 19:54:07 +04:00
return timeval_compare ( & s1 - > time , & s2 - > time ) ;
}
2004-06-08 20:14:31 +04:00
/****************************************************************************
If any deferred opens are waiting on this close , notify them .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-12-12 19:07:19 +04:00
static void notify_deferred_opens ( struct smbd_server_connection * sconn ,
2010-07-04 20:25:02 +04:00
struct share_mode_lock * lck )
2004-06-08 20:14:31 +04:00
{
2012-06-02 15:42:09 +04:00
struct server_id self = messaging_server_id ( sconn - > msg_ctx ) ;
2011-12-14 19:54:07 +04:00
uint32_t i , num_deferred ;
struct share_mode_entry * deferred ;
2009-02-04 02:40:23 +03:00
2012-05-25 01:32:04 +04:00
if ( ! should_notify_deferred_opens ( sconn ) ) {
2009-02-04 02:40:23 +03:00
return ;
}
2011-08-02 18:44:53 +04:00
2011-12-14 19:54:07 +04:00
num_deferred = 0 ;
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < lck - > data - > num_share_modes ; i + + ) {
2012-05-07 14:22:50 +04:00
struct share_mode_entry * e = & lck - > data - > share_modes [ i ] ;
if ( ! is_deferred_open_entry ( e ) ) {
continue ;
}
if ( share_mode_stale_pid ( lck - > data , i ) ) {
continue ;
2011-12-14 19:54:07 +04:00
}
2012-05-07 14:22:50 +04:00
num_deferred + = 1 ;
2011-12-14 19:54:07 +04:00
}
if ( num_deferred = = 0 ) {
return ;
}
deferred = talloc_array ( talloc_tos ( ) , struct share_mode_entry ,
num_deferred ) ;
if ( deferred = = NULL ) {
return ;
}
num_deferred = 0 ;
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < lck - > data - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & lck - > data - > share_modes [ i ] ;
2011-12-14 19:54:07 +04:00
if ( is_deferred_open_entry ( e ) ) {
deferred [ num_deferred ] = * e ;
num_deferred + = 1 ;
}
}
2011-08-02 18:44:53 +04:00
2011-12-14 19:54:07 +04:00
/*
* We need to sort the notifications by initial request time . Imagine
* two opens come in asyncronously , both conflicting with the open we
* just close here . If we don ' t sort the notifications , the one that
* came in last might get the response before the one that came in
* first . This is demonstrated with the smbtorture4 raw . mux test .
*
* As long as we had the UNUSED_SHARE_MODE_ENTRY , we happened to
* survive this particular test . Without UNUSED_SHARE_MODE_ENTRY , we
* shuffle the share mode entries around a bit , so that we do not
* survive raw . mux anymore .
*
* We could have kept the ordering in del_share_mode , but as the
* ordering was never formalized I think it is better to do it here
* where it is necessary .
*/
qsort ( deferred , num_deferred , sizeof ( struct share_mode_entry ) ,
compare_share_mode_times ) ;
for ( i = 0 ; i < num_deferred ; i + + ) {
struct share_mode_entry * e = & deferred [ i ] ;
2011-08-02 18:44:53 +04:00
2012-06-16 02:26:26 +04:00
if ( serverid_equal ( & self , & e - > pid ) ) {
2005-09-30 21:13:37 +04:00
/*
* We need to notify ourself to retry the open . Do
* this by finding the queued SMB record , moving it to
* the head of the queue and changing the wait time to
* zero .
*/
2011-12-12 19:07:19 +04:00
schedule_deferred_open_message_smb ( sconn , e - > op_mid ) ;
2005-09-30 21:13:37 +04:00
} else {
2006-01-13 01:17:54 +03:00
char msg [ MSG_SMB_SHARE_MODE_ENTRY_SIZE ] ;
share_mode_entry_to_message ( msg , e ) ;
2011-12-12 19:07:19 +04:00
messaging_send_buf ( sconn - > msg_ctx , e - > pid ,
MSG_SMB_OPEN_RETRY ,
2007-05-15 00:31:28 +04:00
( uint8 * ) msg ,
MSG_SMB_SHARE_MODE_ENTRY_SIZE ) ;
2005-09-30 21:13:37 +04:00
}
}
2011-12-14 19:54:07 +04:00
TALLOC_FREE ( deferred ) ;
2004-06-08 20:14:31 +04:00
}
2008-01-20 01:25:36 +03:00
/****************************************************************************
Delete all streams
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-12-22 02:49:46 +03:00
NTSTATUS delete_all_streams ( connection_struct * conn , const char * fname )
2008-01-20 01:25:36 +03:00
{
2011-10-14 02:41:53 +04:00
struct stream_struct * stream_info = NULL ;
2008-01-20 01:25:36 +03:00
int i ;
2011-10-14 02:41:53 +04:00
unsigned int num_streams = 0 ;
2008-01-20 01:25:36 +03:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
NTSTATUS status ;
2011-10-17 22:10:29 +04:00
status = vfs_streaminfo ( conn , NULL , fname , talloc_tos ( ) ,
& num_streams , & stream_info ) ;
2008-01-20 01:25:36 +03:00
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_IMPLEMENTED ) ) {
DEBUG ( 10 , ( " no streams around \n " ) ) ;
TALLOC_FREE ( frame ) ;
return NT_STATUS_OK ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
2011-10-17 22:10:29 +04:00
DEBUG ( 10 , ( " vfs_streaminfo failed: %s \n " ,
2008-01-20 01:25:36 +03:00
nt_errstr ( status ) ) ) ;
goto fail ;
}
DEBUG ( 10 , ( " delete_all_streams found %d streams \n " ,
num_streams ) ) ;
if ( num_streams = = 0 ) {
TALLOC_FREE ( frame ) ;
return NT_STATUS_OK ;
}
for ( i = 0 ; i < num_streams ; i + + ) {
int res ;
2009-07-02 20:27:44 +04:00
struct smb_filename * smb_fname_stream = NULL ;
2008-01-20 01:25:36 +03:00
if ( strequal ( stream_info [ i ] . name , " ::$DATA " ) ) {
continue ;
}
2009-07-02 20:27:44 +04:00
status = create_synthetic_smb_fname ( talloc_tos ( ) , fname ,
stream_info [ i ] . name , NULL ,
& smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
2009-07-02 20:27:44 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-01-20 01:25:36 +03:00
DEBUG ( 0 , ( " talloc_aprintf failed \n " ) ) ;
goto fail ;
}
2009-07-02 20:27:44 +04:00
res = SMB_VFS_UNLINK ( conn , smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
if ( res = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
DEBUG ( 10 , ( " Could not delete stream %s: %s \n " ,
2009-07-02 20:27:44 +04:00
smb_fname_str_dbg ( smb_fname_stream ) ,
strerror ( errno ) ) ) ;
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
break ;
}
2009-07-02 20:27:44 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
}
fail :
TALLOC_FREE ( frame ) ;
return status ;
}
1998-08-17 17:11:34 +04:00
/****************************************************************************
2006-05-18 03:15:53 +04:00
Deal with removing a share mode on last close .
1998-08-17 17:11:34 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-12-13 16:27:58 +03:00
2007-01-05 14:31:28 +03:00
static NTSTATUS close_remove_share_mode ( files_struct * fsp ,
enum file_close_type close_type )
1998-08-17 17:11:34 +04:00
{
connection_struct * conn = fsp - > conn ;
2012-06-01 18:29:51 +04:00
struct server_id self = messaging_server_id ( conn - > sconn - > msg_ctx ) ;
2008-01-11 03:35:54 +03:00
bool delete_file = false ;
bool changed_user = false ;
2009-07-02 20:27:44 +04:00
struct share_mode_lock * lck = NULL ;
2007-01-05 14:31:28 +03:00
NTSTATUS status = NT_STATUS_OK ;
2009-10-03 00:45:38 +04:00
NTSTATUS tmp_status ;
2007-05-29 13:30:34 +04:00
struct file_id id ;
2011-02-11 10:49:15 +03:00
const struct security_unix_token * del_token = NULL ;
2012-04-05 01:54:02 +04:00
const struct security_token * del_nt_token = NULL ;
bool got_tokens = false ;
2012-06-01 18:13:22 +04:00
bool normal_close ;
1998-08-17 17:11:34 +04:00
2009-11-06 09:58:12 +03:00
/* Ensure any pending write time updates are done. */
if ( fsp - > update_write_time_event ) {
2011-12-12 16:39:56 +04:00
update_write_time_handler ( fsp - > conn - > sconn - > ev_ctx ,
2009-11-06 09:58:12 +03:00
fsp - > update_write_time_event ,
timeval_current ( ) ,
( void * ) fsp ) ;
}
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
/*
* Lock the share entries , and determine if we should delete
* on close . If so delete whilst the lock is still in effect .
* This prevents race conditions with the file being created . JRA .
*/
1998-08-17 17:11:34 +04:00
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fsp - > file_id ) ;
2005-09-30 21:13:37 +04:00
if ( lck = = NULL ) {
2007-01-04 18:12:22 +03:00
DEBUG ( 0 , ( " close_remove_share_mode: Could not get share mode "
2009-07-11 01:50:37 +04:00
" lock for file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2012-06-01 18:42:29 +04:00
return NT_STATUS_INVALID_PARAMETER ;
2005-09-30 21:13:37 +04:00
}
2001-08-22 04:29:40 +04:00
2008-03-12 17:39:38 +03:00
if ( fsp - > write_time_forced ) {
2009-11-06 03:20:11 +03:00
DEBUG ( 10 , ( " close_remove_share_mode: write time forced "
" for file %s \n " ,
fsp_str_dbg ( fsp ) ) ) ;
2012-01-10 16:56:37 +04:00
set_close_write_time ( fsp , lck - > data - > changed_write_time ) ;
2009-11-06 08:27:25 +03:00
} else if ( fsp - > update_write_time_on_close ) {
2009-11-06 09:58:12 +03:00
/* Someone had a pending write. */
2009-11-06 08:27:25 +03:00
if ( null_timespec ( fsp - > close_write_time ) ) {
2009-11-06 09:58:12 +03:00
DEBUG ( 10 , ( " close_remove_share_mode: update to current time "
" for file %s \n " ,
fsp_str_dbg ( fsp ) ) ) ;
/* Update to current time due to "normal" write. */
set_close_write_time ( fsp , timespec_current ( ) ) ;
2009-11-06 08:27:25 +03:00
} else {
2009-11-06 09:58:12 +03:00
DEBUG ( 10 , ( " close_remove_share_mode: write time pending "
" for file %s \n " ,
fsp_str_dbg ( fsp ) ) ) ;
/* Update to time set on close call. */
set_close_write_time ( fsp , fsp - > close_write_time ) ;
2009-11-06 08:27:25 +03:00
}
2008-03-12 17:39:38 +03:00
}
2011-01-26 01:23:19 +03:00
if ( fsp - > initial_delete_on_close & &
! is_delete_on_close_set ( lck , fsp - > name_hash ) ) {
2007-10-19 04:40:25 +04:00
bool became_user = False ;
2007-01-19 00:51:52 +03:00
/* Initial delete on close was set and no one else
* wrote a real delete on close . */
2010-03-15 22:13:30 +03:00
if ( get_current_vuid ( conn ) ! = fsp - > vuid ) {
2007-01-19 00:51:52 +03:00
become_user ( conn , fsp - > vuid ) ;
became_user = True ;
}
2010-01-13 03:04:44 +03:00
fsp - > delete_on_close = true ;
2012-04-05 01:54:02 +04:00
set_delete_on_close_lck ( fsp , lck , True ,
get_current_nttok ( conn ) ,
get_current_utok ( conn ) ) ;
2007-01-19 00:51:52 +03:00
if ( became_user ) {
unbecome_user ( ) ;
}
}
2011-01-26 01:23:19 +03:00
delete_file = is_delete_on_close_set ( lck , fsp - > name_hash ) ;
2005-09-30 21:13:37 +04:00
if ( delete_file ) {
int i ;
2011-01-26 01:23:19 +03:00
/* See if others still have the file open via this pathname.
If this is the case , then don ' t delete . If all opens are
POSIX delete now . */
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < lck - > data - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & lck - > data - > share_modes [ i ] ;
2012-06-01 17:21:12 +04:00
if ( ! is_valid_share_mode_entry ( e ) ) {
continue ;
}
if ( e - > name_hash ! = fsp - > name_hash ) {
continue ;
}
if ( fsp - > posix_open
& & ( e - > flags & SHARE_MODE_FLAG_POSIX_OPEN ) ) {
continue ;
2005-09-30 21:13:37 +04:00
}
2012-06-16 02:26:26 +04:00
if ( serverid_equal ( & self , & e - > pid ) & &
2012-06-01 18:29:51 +04:00
( e - > share_file_id = = fsp - > fh - > gen_id ) ) {
continue ;
}
2012-06-01 17:21:12 +04:00
if ( share_mode_stale_pid ( lck - > data , i ) ) {
continue ;
}
delete_file = False ;
break ;
2005-09-30 21:13:37 +04:00
}
}
1998-08-17 17:11:34 +04:00
2004-06-08 20:14:31 +04:00
/* Notify any deferred opens waiting on this close. */
2011-12-12 19:07:19 +04:00
notify_deferred_opens ( conn - > sconn , lck ) ;
2005-09-30 21:13:37 +04:00
reply_to_oplock_break_requests ( fsp ) ;
2004-06-08 20:14:31 +04:00
1998-09-11 23:14:27 +04:00
/*
* NT can set delete_on_close of the last open
* reference to a file .
*/
2012-06-01 18:13:22 +04:00
normal_close = ( close_type = = NORMAL_CLOSE | | close_type = = SHUTDOWN_CLOSE ) ;
if ( ! normal_close | | ! delete_file ) {
2012-06-01 18:29:51 +04:00
if ( ! del_share_mode ( lck , fsp ) ) {
DEBUG ( 0 , ( " close_remove_share_mode: Could not delete "
" share entry for file %s \n " ,
fsp_str_dbg ( fsp ) ) ) ;
}
2007-01-04 18:12:22 +03:00
TALLOC_FREE ( lck ) ;
2007-01-05 14:31:28 +03:00
return NT_STATUS_OK ;
2007-01-04 18:12:22 +03:00
}
2005-12-13 21:11:50 +03:00
2007-01-04 18:12:22 +03:00
/*
* Ok , we have to delete the file
*/
2005-12-13 21:11:50 +03:00
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close was set "
2009-07-11 01:50:37 +04:00
" - deleting file. \n " , fsp_str_dbg ( fsp ) ) ) ;
2006-02-02 23:44:50 +03:00
2008-03-12 17:39:38 +03:00
/*
* Don ' t try to update the write time when we delete the file
*/
fsp - > update_write_time_on_close = false ;
2012-04-05 01:54:02 +04:00
got_tokens = get_delete_on_close_token ( lck , fsp - > name_hash ,
& del_nt_token , & del_token ) ;
SMB_ASSERT ( got_tokens ) ;
2011-01-26 01:23:19 +03:00
if ( ! unix_token_equal ( del_token , get_current_utok ( conn ) ) ) {
2008-01-11 03:35:54 +03:00
/* Become the user who requested the delete. */
2006-02-02 23:44:50 +03:00
2008-01-11 03:35:54 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. "
" Change user to uid %u \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ,
2011-01-26 01:23:19 +03:00
( unsigned int ) del_token - > uid ) ) ;
2008-01-11 03:35:54 +03:00
if ( ! push_sec_ctx ( ) ) {
smb_panic ( " close_remove_share_mode: file %s. failed to push "
" sec_ctx. \n " ) ;
}
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
set_sec_ctx ( del_token - > uid ,
del_token - > gid ,
del_token - > ngroups ,
del_token - > groups ,
2012-04-05 01:54:02 +04:00
del_nt_token ) ;
2008-01-11 03:35:54 +03:00
changed_user = true ;
}
2007-01-04 18:12:22 +03:00
/* We can only delete the file if the name we have is still valid and
hasn ' t been renamed . */
2007-07-17 04:09:56 +04:00
2009-10-03 00:45:38 +04:00
tmp_status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( tmp_status ) ) {
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and stat failed with error %s \n " ,
2009-10-03 00:45:38 +04:00
fsp_str_dbg ( fsp ) , nt_errstr ( tmp_status ) ) ) ;
2007-01-05 14:31:28 +03:00
/*
* Don ' t save the errno here , we ignore this error
*/
2007-01-04 18:12:22 +03:00
goto done ;
}
2009-07-11 01:50:37 +04:00
id = vfs_file_id_from_sbuf ( conn , & fsp - > fsp_name - > st ) ;
2007-05-29 13:30:34 +04:00
if ( ! file_id_equal ( & fsp - > file_id , & id ) ) {
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and dev and/or inode does not match \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2007-05-29 13:30:34 +04:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. stored file_id %s, "
" stat file_id %s \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ,
2007-09-10 14:56:07 +04:00
file_id_string_tos ( & fsp - > file_id ) ,
file_id_string_tos ( & id ) ) ) ;
2007-01-05 14:31:28 +03:00
/*
* Don ' t save the errno here , we ignore this error
*/
2007-01-04 18:12:22 +03:00
goto done ;
2001-11-13 04:35:20 +03:00
}
1998-09-11 23:14:27 +04:00
2008-01-20 01:25:36 +03:00
if ( ( conn - > fs_capabilities & FILE_NAMED_STREAMS )
2009-07-11 01:50:37 +04:00
& & ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) {
2008-01-20 01:25:36 +03:00
2009-07-11 01:50:37 +04:00
status = delete_all_streams ( conn , fsp - > fsp_name - > base_name ) ;
2008-01-20 01:25:36 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 5 , ( " delete_all_streams failed: %s \n " ,
nt_errstr ( status ) ) ) ;
goto done ;
}
}
2009-07-11 01:50:37 +04:00
if ( SMB_VFS_UNLINK ( conn , fsp - > fsp_name ) ! = 0 ) {
2007-01-04 18:12:22 +03:00
/*
* This call can potentially fail as another smbd may
* have had the file open with delete on close set and
* deleted it when its last reference to this file
* went away . Hence we log this but not at debug level
* zero .
*/
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and unlink failed with error %s \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) , strerror ( errno ) ) ) ;
2007-01-05 14:31:28 +03:00
status = map_nt_error_from_unix ( errno ) ;
2007-01-04 18:12:22 +03:00
}
2007-02-11 07:37:56 +03:00
/* As we now have POSIX opens which can unlink
* with other open files we may have taken
* this code path with more than one share mode
* entry - ensure we only delete once by resetting
* the delete on close flag . JRA .
*/
2010-01-13 03:04:44 +03:00
fsp - > delete_on_close = false ;
2012-04-05 01:54:02 +04:00
set_delete_on_close_lck ( fsp , lck , false , NULL , NULL ) ;
2007-02-11 07:37:56 +03:00
2007-01-04 18:12:22 +03:00
done :
2007-02-07 00:05:34 +03:00
2008-01-11 03:35:54 +03:00
if ( changed_user ) {
/* unbecome user. */
pop_sec_ctx ( ) ;
}
2012-06-01 18:29:51 +04:00
if ( ! del_share_mode ( lck , fsp ) ) {
DEBUG ( 0 , ( " close_remove_share_mode: Could not delete share "
" entry for file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
}
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( lck ) ;
2011-01-21 12:30:13 +03:00
if ( delete_file ) {
/*
* Do the notification after we released the share
* mode lock . Inside notify_fname we take out another
* tdb lock . With ctdb also accessing our databases ,
* this can lead to deadlocks . Putting this notify
* after the TALLOC_FREE ( lck ) above we avoid locking
* two records simultaneously . Notifies are async and
* informational only , so calling the notify_fname
* without holding the share mode lock should not do
* any harm .
*/
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_FILE_NAME ,
fsp - > fsp_name - > base_name ) ;
}
2007-01-05 14:31:28 +03:00
return status ;
2006-05-18 03:15:53 +04:00
}
2009-11-06 09:58:12 +03:00
void set_close_write_time ( struct files_struct * fsp , struct timespec ts )
2008-03-12 17:39:38 +03:00
{
DEBUG ( 6 , ( " close_write_time: %s " , time_to_asc ( convert_timespec_to_time_t ( ts ) ) ) ) ;
if ( null_timespec ( ts ) ) {
return ;
}
2009-11-06 08:27:25 +03:00
fsp - > write_time_forced = false ;
2008-03-12 17:39:38 +03:00
fsp - > update_write_time_on_close = true ;
fsp - > close_write_time = ts ;
}
static NTSTATUS update_write_time_on_close ( struct files_struct * fsp )
{
2009-01-24 01:40:19 +03:00
struct smb_file_time ft ;
2008-03-12 17:39:38 +03:00
NTSTATUS status ;
2009-11-06 09:58:12 +03:00
struct share_mode_lock * lck = NULL ;
2008-03-12 17:39:38 +03:00
2009-01-24 01:40:19 +03:00
ZERO_STRUCT ( ft ) ;
2008-03-12 17:39:38 +03:00
if ( ! fsp - > update_write_time_on_close ) {
return NT_STATUS_OK ;
}
if ( null_timespec ( fsp - > close_write_time ) ) {
fsp - > close_write_time = timespec_current ( ) ;
}
/* Ensure we have a valid stat struct for the source. */
2009-10-03 00:45:38 +04:00
status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
2009-02-26 01:57:16 +03:00
}
2009-07-11 01:50:37 +04:00
if ( ! VALID_STAT ( fsp - > fsp_name - > st ) ) {
2008-03-12 17:39:38 +03:00
/* if it doesn't seem to be a real file */
2009-07-11 01:50:37 +04:00
return NT_STATUS_OK ;
2008-03-12 17:39:38 +03:00
}
2012-05-19 06:29:36 +04:00
/*
* get_existing_share_mode_lock ( ) isn ' t really the right
* call here , as we ' re being called after
* close_remove_share_mode ( ) inside close_normal_file ( )
* so it ' s quite normal to not have an existing share
* mode here . However , get_share_mode_lock ( ) doesn ' t
* work because that will create a new share mode if
* one doesn ' t exist - so stick with this call ( just
* ignore any error we get if the share mode doesn ' t
* exist .
*/
2009-11-06 09:58:12 +03:00
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fsp - > file_id ) ;
2009-11-06 09:58:12 +03:00
if ( lck ) {
2012-05-19 06:29:36 +04:00
/* On close if we're changing the real file time we
* must update it in the open file db too . */
( void ) set_write_time ( fsp - > file_id , fsp - > close_write_time ) ;
2009-11-06 09:58:12 +03:00
/* Close write times overwrite sticky write times
so we must replace any sticky write time here . */
2012-01-10 16:56:37 +04:00
if ( ! null_timespec ( lck - > data - > changed_write_time ) ) {
2009-11-06 09:58:12 +03:00
( void ) set_sticky_write_time ( fsp - > file_id , fsp - > close_write_time ) ;
}
TALLOC_FREE ( lck ) ;
}
2009-01-24 01:40:19 +03:00
ft . mtime = fsp - > close_write_time ;
2011-11-16 05:41:48 +04:00
/* As this is a close based update, we are not directly changing the
2011-02-23 03:38:42 +03:00
file attributes from a client call , but indirectly from a write . */
2011-11-16 05:41:48 +04:00
status = smb_set_file_time ( fsp - > conn , fsp , fsp - > fsp_name , & ft , false ) ;
2008-03-12 17:39:38 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2011-02-23 03:38:42 +03:00
DEBUG ( 10 , ( " update_write_time_on_close: smb_set_file_time "
" on file %s returned %s \n " ,
fsp_str_dbg ( fsp ) ,
nt_errstr ( status ) ) ) ;
2009-07-11 01:50:37 +04:00
return status ;
2008-03-12 17:39:38 +03:00
}
2009-07-03 00:39:20 +04:00
return status ;
2008-03-12 17:39:38 +03:00
}
2009-06-07 01:20:44 +04:00
static NTSTATUS ntstatus_keeperror ( NTSTATUS s1 , NTSTATUS s2 )
{
if ( ! NT_STATUS_IS_OK ( s1 ) ) {
return s1 ;
}
return s2 ;
}
2006-05-18 03:15:53 +04:00
/****************************************************************************
Close a file .
close_type can be NORMAL_CLOSE = 0 , SHUTDOWN_CLOSE , ERROR_CLOSE .
printing and magic scripts are only run on normal close .
delete on close is done on normal and shutdown close .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-09 18:27:49 +04:00
static NTSTATUS close_normal_file ( struct smb_request * req , files_struct * fsp ,
enum file_close_type close_type )
2006-05-18 03:15:53 +04:00
{
2007-02-07 00:05:34 +03:00
NTSTATUS status = NT_STATUS_OK ;
2009-06-07 01:20:44 +04:00
NTSTATUS tmp ;
2006-05-18 03:15:53 +04:00
connection_struct * conn = fsp - > conn ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2012-07-18 00:43:06 +04:00
if ( fsp - > num_aio_requests ! = 0 ) {
/*
* reply_close and the smb2 close must have taken care of
* this . No other callers of close_file should ever have
* created async I / O .
*
* We need to panic here because if we close ( ) the fd while we
* have outstanding async I / O requests , in the worst case we
* could end up writing to the wrong file .
*/
DEBUG ( 0 , ( " fsp->num_aio_requests=%u \n " ,
fsp - > num_aio_requests ) ) ;
smb_panic ( " can not close with outstanding aio requests " ) ;
}
2010-06-05 08:00:24 +04:00
2006-05-18 03:15:53 +04:00
/*
* If we ' re flushing on a close we can get a write
* error here , we must remember this .
*/
2009-06-07 01:20:44 +04:00
tmp = close_filestruct ( fsp ) ;
status = ntstatus_keeperror ( status , tmp ) ;
2006-05-18 03:15:53 +04:00
if ( fsp - > print_file ) {
2010-05-04 12:28:48 +04:00
/* FIXME: return spool errors */
print_spool_end ( fsp , close_type ) ;
2008-10-09 18:27:49 +04:00
file_free ( req , fsp ) ;
2007-02-07 00:05:34 +03:00
return NT_STATUS_OK ;
2006-05-18 03:15:53 +04:00
}
2009-02-04 10:17:48 +03:00
/* Remove the oplock before potentially deleting the file. */
if ( fsp - > oplock_type ) {
release_file_oplock ( fsp ) ;
}
2006-05-18 03:15:53 +04:00
/* If this is an old DOS or FCB open and we have multiple opens on
the same handle we only have one share mode . Ensure we only remove
the share mode on the last close . */
if ( fsp - > fh - > ref_count = = 1 ) {
/* Should we return on error here... ? */
2009-06-07 01:20:44 +04:00
tmp = close_remove_share_mode ( fsp , close_type ) ;
status = ntstatus_keeperror ( status , tmp ) ;
2006-05-18 03:15:53 +04:00
}
2010-07-04 20:26:14 +04:00
locking_close_file ( conn - > sconn - > msg_ctx , fsp , close_type ) ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2009-06-07 01:20:44 +04:00
tmp = fd_close ( fsp ) ;
status = ntstatus_keeperror ( status , tmp ) ;
2004-10-21 23:51:33 +04:00
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
/* check for magic scripts */
2006-02-02 23:44:50 +03:00
if ( close_type = = NORMAL_CLOSE ) {
2009-06-07 01:27:31 +04:00
tmp = check_magic ( fsp ) ;
status = ntstatus_keeperror ( status , tmp ) ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
}
2001-12-17 00:04:19 +03:00
/*
* Ensure pending modtime is set after close .
*/
2009-06-07 01:20:44 +04:00
tmp = update_write_time_on_close ( fsp ) ;
if ( NT_STATUS_EQUAL ( tmp , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
2009-02-26 01:57:16 +03:00
/* Someone renamed the file or a parent directory containing
* this file . We can ' t do anything about this , we don ' t have
* an " update timestamp by fd " call in POSIX . Eat the error . */
2009-06-07 01:20:44 +04:00
tmp = NT_STATUS_OK ;
2009-02-26 01:57:16 +03:00
}
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2009-06-07 01:20:44 +04:00
status = ntstatus_keeperror ( status , tmp ) ;
2007-02-07 00:05:34 +03:00
1999-12-13 16:27:58 +03:00
DEBUG ( 2 , ( " %s closed file %s (numopen=%d) %s \n " ,
2011-07-15 09:55:31 +04:00
conn - > session_info - > unix_info - > unix_name , fsp_str_dbg ( fsp ) ,
2008-11-21 23:14:53 +03:00
conn - > num_files_open - 1 ,
2007-02-07 00:05:34 +03:00
nt_errstr ( status ) ) ) ;
1998-08-17 17:11:34 +04:00
2008-10-09 18:27:49 +04:00
file_free ( req , fsp ) ;
2007-02-07 00:05:34 +03:00
return status ;
1998-08-17 17:11:34 +04:00
}
2009-12-04 00:01:10 +03:00
/****************************************************************************
Static function used by reply_rmdir to delete an entire directory
tree recursively . Return True on ok , False on fail .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static bool recursive_rmdir ( TALLOC_CTX * ctx ,
connection_struct * conn ,
struct smb_filename * smb_dname )
{
const char * dname = NULL ;
char * talloced = NULL ;
bool ret = True ;
long offset = 0 ;
SMB_STRUCT_STAT st ;
struct smb_Dir * dir_hnd ;
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( smb_dname ) ) ;
2011-02-10 03:31:06 +03:00
dir_hnd = OpenDir ( talloc_tos ( ) , conn , smb_dname - > base_name , NULL , 0 ) ;
2009-12-04 00:01:10 +03:00
if ( dir_hnd = = NULL )
return False ;
while ( ( dname = ReadDirName ( dir_hnd , & offset , & st , & talloced ) ) ) {
struct smb_filename * smb_dname_full = NULL ;
char * fullname = NULL ;
bool do_break = true ;
NTSTATUS status ;
if ( ISDOT ( dname ) | | ISDOTDOT ( dname ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
if ( ! is_visible_file ( conn , smb_dname - > base_name , dname , & st ,
false ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
/* Construct the full name. */
fullname = talloc_asprintf ( ctx ,
" %s/%s " ,
smb_dname - > base_name ,
dname ) ;
if ( ! fullname ) {
errno = ENOMEM ;
goto err_break ;
}
status = create_synthetic_smb_fname ( talloc_tos ( ) , fullname ,
NULL , NULL ,
& smb_dname_full ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto err_break ;
}
if ( SMB_VFS_LSTAT ( conn , smb_dname_full ) ! = 0 ) {
goto err_break ;
}
if ( smb_dname_full - > st . st_ex_mode & S_IFDIR ) {
if ( ! recursive_rmdir ( ctx , conn , smb_dname_full ) ) {
goto err_break ;
}
if ( SMB_VFS_RMDIR ( conn ,
smb_dname_full - > base_name ) ! = 0 ) {
goto err_break ;
}
} else if ( SMB_VFS_UNLINK ( conn , smb_dname_full ) ! = 0 ) {
goto err_break ;
}
/* Successful iteration. */
do_break = false ;
err_break :
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( talloced ) ;
if ( do_break ) {
ret = false ;
break ;
}
}
TALLOC_FREE ( dir_hnd ) ;
return ret ;
}
/****************************************************************************
The internals of the rmdir code - called elsewhere .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static NTSTATUS rmdir_internals ( TALLOC_CTX * ctx , files_struct * fsp )
{
connection_struct * conn = fsp - > conn ;
struct smb_filename * smb_dname = fsp - > fsp_name ;
int ret ;
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( smb_dname ) ) ;
/* Might be a symlink. */
if ( SMB_VFS_LSTAT ( conn , smb_dname ) ! = 0 ) {
return map_nt_error_from_unix ( errno ) ;
}
if ( S_ISLNK ( smb_dname - > st . st_ex_mode ) ) {
/* Is what it points to a directory ? */
if ( SMB_VFS_STAT ( conn , smb_dname ) ! = 0 ) {
return map_nt_error_from_unix ( errno ) ;
}
if ( ! ( S_ISDIR ( smb_dname - > st . st_ex_mode ) ) ) {
return NT_STATUS_NOT_A_DIRECTORY ;
}
ret = SMB_VFS_UNLINK ( conn , smb_dname ) ;
} else {
ret = SMB_VFS_RMDIR ( conn , smb_dname - > base_name ) ;
}
if ( ret = = 0 ) {
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_DIR_NAME ,
smb_dname - > base_name ) ;
return NT_STATUS_OK ;
}
2012-07-18 09:37:23 +04:00
if ( ( ( errno = = ENOTEMPTY ) | | ( errno = = EEXIST ) ) & & * lp_veto_files ( talloc_tos ( ) , SNUM ( conn ) ) ) {
2009-12-04 00:01:10 +03:00
/*
* Check to see if the only thing in this directory are
* vetoed files / directories . If so then delete them and
* retry . If we fail to delete any of them ( and we * don ' t *
* do a recursive delete ) then fail the rmdir .
*/
SMB_STRUCT_STAT st ;
const char * dname = NULL ;
char * talloced = NULL ;
long dirpos = 0 ;
struct smb_Dir * dir_hnd = OpenDir ( talloc_tos ( ) , conn ,
smb_dname - > base_name , NULL ,
0 ) ;
if ( dir_hnd = = NULL ) {
errno = ENOTEMPTY ;
goto err ;
}
while ( ( dname = ReadDirName ( dir_hnd , & dirpos , & st ,
& talloced ) ) ! = NULL ) {
if ( ( strcmp ( dname , " . " ) = = 0 ) | | ( strcmp ( dname , " .. " ) = = 0 ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
if ( ! is_visible_file ( conn , smb_dname - > base_name , dname ,
& st , false ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
if ( ! IS_VETO_PATH ( conn , dname ) ) {
TALLOC_FREE ( dir_hnd ) ;
TALLOC_FREE ( talloced ) ;
errno = ENOTEMPTY ;
goto err ;
}
TALLOC_FREE ( talloced ) ;
}
/* We only have veto files/directories.
* Are we allowed to delete them ? */
if ( ! lp_recursive_veto_delete ( SNUM ( conn ) ) ) {
TALLOC_FREE ( dir_hnd ) ;
errno = ENOTEMPTY ;
goto err ;
}
/* Do a recursive delete. */
RewindDir ( dir_hnd , & dirpos ) ;
while ( ( dname = ReadDirName ( dir_hnd , & dirpos , & st ,
& talloced ) ) ! = NULL ) {
struct smb_filename * smb_dname_full = NULL ;
char * fullname = NULL ;
bool do_break = true ;
NTSTATUS status ;
if ( ISDOT ( dname ) | | ISDOTDOT ( dname ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
if ( ! is_visible_file ( conn , smb_dname - > base_name , dname ,
& st , false ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
fullname = talloc_asprintf ( ctx ,
" %s/%s " ,
smb_dname - > base_name ,
dname ) ;
if ( ! fullname ) {
errno = ENOMEM ;
goto err_break ;
}
status = create_synthetic_smb_fname ( talloc_tos ( ) ,
fullname , NULL ,
NULL ,
& smb_dname_full ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto err_break ;
}
if ( SMB_VFS_LSTAT ( conn , smb_dname_full ) ! = 0 ) {
goto err_break ;
}
if ( smb_dname_full - > st . st_ex_mode & S_IFDIR ) {
if ( ! recursive_rmdir ( ctx , conn ,
smb_dname_full ) ) {
goto err_break ;
}
if ( SMB_VFS_RMDIR ( conn ,
smb_dname_full - > base_name ) ! = 0 ) {
goto err_break ;
}
} else if ( SMB_VFS_UNLINK ( conn , smb_dname_full ) ! = 0 ) {
goto err_break ;
}
/* Successful iteration. */
do_break = false ;
err_break :
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( talloced ) ;
if ( do_break )
break ;
}
TALLOC_FREE ( dir_hnd ) ;
/* Retry the rmdir */
ret = SMB_VFS_RMDIR ( conn , smb_dname - > base_name ) ;
}
err :
if ( ret ! = 0 ) {
DEBUG ( 3 , ( " rmdir_internals: couldn't remove directory %s : "
" %s \n " , smb_fname_str_dbg ( smb_dname ) ,
strerror ( errno ) ) ) ;
return map_nt_error_from_unix ( errno ) ;
}
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_DIR_NAME ,
smb_dname - > base_name ) ;
return NT_STATUS_OK ;
}
1998-08-17 17:11:34 +04:00
/****************************************************************************
Close a directory opened by an NT SMB call .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-09 18:27:49 +04:00
static NTSTATUS close_directory ( struct smb_request * req , files_struct * fsp ,
enum file_close_type close_type )
1998-08-17 17:11:34 +04:00
{
2012-06-02 16:06:29 +04:00
struct server_id self = messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ;
2009-07-02 20:27:44 +04:00
struct share_mode_lock * lck = NULL ;
2007-10-19 04:40:25 +04:00
bool delete_dir = False ;
2007-02-07 00:05:34 +03:00
NTSTATUS status = NT_STATUS_OK ;
2010-01-13 03:55:31 +03:00
NTSTATUS status1 = NT_STATUS_OK ;
2012-04-05 01:54:02 +04:00
const struct security_token * del_nt_token = NULL ;
2011-02-11 10:49:15 +03:00
const struct security_unix_token * del_token = NULL ;
2005-10-28 02:35:08 +04:00
1999-12-13 16:27:58 +03:00
/*
* NT can set delete_on_close of the last open
* reference to a directory also .
*/
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fsp - > file_id ) ;
2005-10-28 02:35:08 +04:00
if ( lck = = NULL ) {
2009-07-02 20:27:44 +04:00
DEBUG ( 0 , ( " close_directory: Could not get share mode lock for "
2009-07-11 01:50:37 +04:00
" %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2012-06-02 16:04:52 +04:00
return NT_STATUS_INVALID_PARAMETER ;
2005-10-28 02:35:08 +04:00
}
2007-01-19 00:51:52 +03:00
if ( fsp - > initial_delete_on_close ) {
2007-10-19 04:40:25 +04:00
bool became_user = False ;
2007-01-19 00:51:52 +03:00
/* Initial delete on close was set - for
* directories we don ' t care if anyone else
* wrote a real delete on close . */
2010-03-15 22:13:30 +03:00
if ( get_current_vuid ( fsp - > conn ) ! = fsp - > vuid ) {
2007-01-19 00:51:52 +03:00
become_user ( fsp - > conn , fsp - > vuid ) ;
became_user = True ;
}
2010-07-07 00:43:14 +04:00
send_stat_cache_delete_message ( fsp - > conn - > sconn - > msg_ctx ,
fsp - > fsp_name - > base_name ) ;
2011-01-26 01:23:19 +03:00
set_delete_on_close_lck ( fsp , lck , true ,
2012-04-05 01:54:02 +04:00
get_current_nttok ( fsp - > conn ) ,
2011-01-26 01:23:19 +03:00
get_current_utok ( fsp - > conn ) ) ;
2010-01-13 03:04:44 +03:00
fsp - > delete_on_close = true ;
2007-01-19 00:51:52 +03:00
if ( became_user ) {
unbecome_user ( ) ;
}
}
2012-04-05 01:54:02 +04:00
delete_dir = get_delete_on_close_token ( lck , fsp - > name_hash ,
& del_nt_token , & del_token ) ;
2005-10-28 02:35:08 +04:00
2007-01-04 19:26:15 +03:00
if ( delete_dir ) {
int i ;
/* See if others still have the dir open. If this is the
2007-02-07 00:05:34 +03:00
* case , then don ' t delete . If all opens are POSIX delete now . */
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < lck - > data - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & lck - > data - > share_modes [ i ] ;
2011-01-26 01:23:19 +03:00
if ( is_valid_share_mode_entry ( e ) & &
e - > name_hash = = fsp - > name_hash ) {
2007-02-07 00:05:34 +03:00
if ( fsp - > posix_open & & ( e - > flags & SHARE_MODE_FLAG_POSIX_OPEN ) ) {
continue ;
}
2012-06-16 02:26:26 +04:00
if ( serverid_equal ( & self , & e - > pid ) & &
2012-06-02 16:06:29 +04:00
( e - > share_file_id = = fsp - > fh - > gen_id ) ) {
continue ;
}
2012-05-07 17:23:29 +04:00
if ( share_mode_stale_pid ( lck - > data , i ) ) {
continue ;
}
2007-01-04 19:26:15 +03:00
delete_dir = False ;
break ;
}
}
}
2006-02-02 23:44:50 +03:00
if ( ( close_type = = NORMAL_CLOSE | | close_type = = SHUTDOWN_CLOSE ) & &
2011-01-26 01:23:19 +03:00
delete_dir ) {
2006-02-02 23:44:50 +03:00
/* Become the user who requested the delete. */
if ( ! push_sec_ctx ( ) ) {
smb_panic ( " close_directory: failed to push sec_ctx. \n " ) ;
}
2011-01-26 01:23:19 +03:00
set_sec_ctx ( del_token - > uid ,
del_token - > gid ,
del_token - > ngroups ,
del_token - > groups ,
2012-04-05 01:54:02 +04:00
del_nt_token ) ;
2006-02-02 23:44:50 +03:00
2012-06-02 16:06:29 +04:00
if ( ! del_share_mode ( lck , fsp ) ) {
DEBUG ( 0 , ( " close_directory: Could not delete share entry for "
" %s \n " , fsp_str_dbg ( fsp ) ) ) ;
}
2006-02-28 09:41:07 +03:00
TALLOC_FREE ( lck ) ;
2012-02-01 01:26:23 +04:00
if ( ( fsp - > conn - > fs_capabilities & FILE_NAMED_STREAMS )
& & ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) {
status = delete_all_streams ( fsp - > conn , fsp - > fsp_name - > base_name ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 5 , ( " delete_all_streams failed: %s \n " ,
nt_errstr ( status ) ) ) ;
2012-06-03 17:17:29 +04:00
return status ;
2012-02-01 01:26:23 +04:00
}
}
2009-12-04 00:01:10 +03:00
status = rmdir_internals ( talloc_tos ( ) , fsp ) ;
2006-02-02 23:44:50 +03:00
2007-02-11 17:55:21 +03:00
DEBUG ( 5 , ( " close_directory: %s. Delete on close was set - "
" deleting directory returned %s. \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) , nt_errstr ( status ) ) ) ;
1999-12-13 16:27:58 +03:00
2006-02-02 23:44:50 +03:00
/* unbecome user. */
pop_sec_ctx ( ) ;
1999-12-13 16:27:58 +03:00
/*
* Ensure we remove any change notify requests that would
* now fail as the directory has been deleted .
*/
2007-02-07 00:05:34 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
2005-10-28 03:30:25 +04:00
remove_pending_change_notify_requests_by_fid ( fsp , NT_STATUS_DELETE_PENDING ) ;
2005-07-08 08:51:27 +04:00
}
2005-10-28 03:30:25 +04:00
} else {
2012-06-02 16:06:29 +04:00
if ( ! del_share_mode ( lck , fsp ) ) {
DEBUG ( 0 , ( " close_directory: Could not delete share entry for "
" %s \n " , fsp_str_dbg ( fsp ) ) ) ;
}
2006-02-28 09:41:07 +03:00
TALLOC_FREE ( lck ) ;
2006-12-31 20:52:24 +03:00
remove_pending_change_notify_requests_by_fid (
fsp , NT_STATUS_OK ) ;
2001-11-13 04:35:20 +03:00
}
1999-12-13 16:27:58 +03:00
2010-01-13 03:55:31 +03:00
status1 = fd_close ( fsp ) ;
2008-11-27 03:47:14 +03:00
2010-01-13 03:55:31 +03:00
if ( ! NT_STATUS_IS_OK ( status1 ) ) {
2008-11-27 03:47:14 +03:00
DEBUG ( 0 , ( " Could not close dir! fname=%s, fd=%d, err=%d=%s \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) , fsp - > fh - > fd , errno ,
2009-07-02 20:27:44 +04:00
strerror ( errno ) ) ) ;
2008-11-27 03:47:14 +03:00
}
1998-08-17 17:11:34 +04:00
/*
* Do the code common to files and directories .
*/
close_filestruct ( fsp ) ;
2008-10-09 18:27:49 +04:00
file_free ( req , fsp ) ;
2009-07-02 20:27:44 +04:00
2010-01-13 03:55:31 +03:00
if ( NT_STATUS_IS_OK ( status ) & & ! NT_STATUS_IS_OK ( status1 ) ) {
status = status1 ;
}
2007-02-07 00:05:34 +03:00
return status ;
2003-01-03 21:50:13 +03:00
}
1999-12-13 16:27:58 +03:00
/****************************************************************************
2003-01-03 21:50:13 +03:00
Close a files_struct .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-09 18:27:49 +04:00
NTSTATUS close_file ( struct smb_request * req , files_struct * fsp ,
enum file_close_type close_type )
1999-12-13 16:27:58 +03:00
{
2008-01-20 01:25:36 +03:00
NTSTATUS status ;
struct files_struct * base_fsp = fsp - > base_fsp ;
2007-02-07 00:05:34 +03:00
if ( fsp - > is_directory ) {
2008-10-09 18:27:49 +04:00
status = close_directory ( req , fsp , close_type ) ;
2007-02-07 00:05:34 +03:00
} else if ( fsp - > fake_file_handle ! = NULL ) {
2008-10-09 18:27:49 +04:00
status = close_fake_file ( req , fsp ) ;
2008-01-20 01:25:36 +03:00
} else {
2008-10-09 18:27:49 +04:00
status = close_normal_file ( req , fsp , close_type ) ;
2008-01-20 01:25:36 +03:00
}
if ( ( base_fsp ! = NULL ) & & ( close_type ! = SHUTDOWN_CLOSE ) ) {
/*
* fsp was a stream , the base fsp can ' t be a stream as well
*
* For SHUTDOWN_CLOSE this is not possible here , because
* SHUTDOWN_CLOSE only happens from files . c which walks the
* complete list of files . If we mess with more than one fsp
* those loops will become confused .
*/
SMB_ASSERT ( base_fsp - > base_fsp = = NULL ) ;
2008-10-09 18:27:49 +04:00
close_file ( req , base_fsp , close_type ) ;
2008-01-20 01:25:36 +03:00
}
return status ;
1999-12-13 16:27:58 +03:00
}
2008-05-27 22:23:59 +04:00
/****************************************************************************
Deal with an ( authorized ) message to close a file given the share mode
entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void msg_close_file ( struct messaging_context * msg_ctx ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
{
files_struct * fsp = NULL ;
struct share_mode_entry e ;
2011-12-12 18:45:07 +04:00
struct smbd_server_connection * sconn =
2011-12-14 12:50:24 +04:00
talloc_get_type_abort ( private_data ,
2011-12-12 18:45:07 +04:00
struct smbd_server_connection ) ;
2008-05-27 22:23:59 +04:00
message_to_share_mode_entry ( & e , ( char * ) data - > data ) ;
if ( DEBUGLVL ( 10 ) ) {
char * sm_str = share_mode_str ( NULL , 0 , & e ) ;
if ( ! sm_str ) {
smb_panic ( " talloc failed " ) ;
}
DEBUG ( 10 , ( " msg_close_file: got request to close share mode "
" entry %s \n " , sm_str ) ) ;
TALLOC_FREE ( sm_str ) ;
}
2010-10-03 19:29:56 +04:00
fsp = file_find_dif ( sconn , e . id , e . share_file_id ) ;
2008-05-27 22:23:59 +04:00
if ( ! fsp ) {
DEBUG ( 10 , ( " msg_close_file: failed to find file. \n " ) ) ;
return ;
}
2008-10-09 18:27:49 +04:00
close_file ( NULL , fsp , NORMAL_CLOSE ) ;
2008-05-27 22:23:59 +04:00
}