1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Locking functions
2000-04-28 01:12:33 +04:00
Copyright ( C ) Andrew Tridgell 1992 - 2000
2006-04-10 19:33:04 +04:00
Copyright ( C ) Jeremy Allison 1992 - 2006
2005-07-08 08:51:27 +04:00
Copyright ( C ) Volker Lendecke 2005
1996-05-04 11:50:46 +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
1996-05-04 11:50:46 +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 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1996-08-15 19:11:34 +04:00
Revision History :
12 aug 96 : Erik . Devriendt @ te6 . siemens . be
added support for shared memory implementation of share mode locking
1997-05-20 04:32:51 +04:00
May 1997. Jeremy Allison ( jallison @ whistle . com ) . Modified share mode
locking to deal with multiple share modes per open file .
1997-10-07 22:46:19 +04:00
September 1997. Jeremy Allison ( jallison @ whistle . com ) . Added oplock
support .
1999-12-21 12:25:59 +03:00
rewrtten completely to use new tdb code . Tridge , Dec ' 99
2000-04-28 01:12:33 +04:00
Added POSIX locking support . Jeremy Allison ( jeremy @ valinux . com ) , Apr . 2000.
2006-04-10 19:33:04 +04:00
Added Unix Extensions POSIX locking support . Jeremy Allison Mar 2006.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2010-05-05 03:39:16 +04:00
# include "librpc/gen_ndr/messaging.h"
2010-07-04 20:18:58 +04:00
# include "smbd/globals.h"
2010-08-18 20:59:23 +04:00
# include "dbwrap.h"
2010-10-12 08:27:50 +04:00
# include "../libcli/security/security.h"
2011-02-25 01:05:57 +03:00
# include "serverid.h"
1996-05-04 11:50:46 +04:00
2005-04-27 22:32:37 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_LOCKING
2007-07-17 04:50:48 +04:00
# define NO_LOCKING_COUNT (-1)
1999-12-21 12:25:59 +03:00
/* the locking database handle */
2007-05-27 13:57:05 +04:00
static struct db_context * lock_db ;
1997-10-20 12:46:00 +04:00
2000-04-12 21:59:09 +04:00
/****************************************************************************
2006-04-10 19:33:04 +04:00
Debugging aids : - ) .
2000-04-12 21:59:09 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-04-10 19:33:04 +04:00
const char * lock_type_name ( enum brl_type lock_type )
2000-04-12 21:59:09 +04:00
{
2006-04-10 19:33:04 +04:00
switch ( lock_type ) {
case READ_LOCK :
return " READ " ;
case WRITE_LOCK :
return " WRITE " ;
2006-07-29 23:14:24 +04:00
case PENDING_READ_LOCK :
return " PENDING_READ " ;
case PENDING_WRITE_LOCK :
return " PENDING_WRITE " ;
2006-04-10 19:33:04 +04:00
default :
return " other " ;
}
}
const char * lock_flav_name ( enum brl_flavour lock_flav )
{
return ( lock_flav = = WINDOWS_LOCK ) ? " WINDOWS_LOCK " : " POSIX_LOCK " ;
2000-04-12 21:59:09 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
1998-07-23 04:10:26 +04:00
Utility function called to see if a file region is locked .
2006-04-10 19:33:04 +04:00
Called in the read / write codepath .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2009-03-14 00:15:28 +03:00
void init_strict_lock_struct ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2009-03-14 00:15:28 +03:00
br_off start ,
br_off size ,
enum brl_type lock_type ,
struct lock_struct * plock )
{
SMB_ASSERT ( lock_type = = READ_LOCK | | lock_type = = WRITE_LOCK ) ;
2010-05-07 17:20:50 +04:00
plock - > context . smblctx = smblctx ;
2009-03-14 00:15:28 +03:00
plock - > context . tid = fsp - > conn - > cnum ;
2010-07-04 20:18:58 +04:00
plock - > context . pid = sconn_server_id ( fsp - > conn - > sconn ) ;
2009-03-14 00:15:28 +03:00
plock - > start = start ;
plock - > size = size ;
plock - > fnum = fsp - > fnum ;
plock - > lock_type = lock_type ;
plock - > lock_flav = lp_posix_cifsu_locktype ( fsp ) ;
}
bool strict_lock_default ( files_struct * fsp , struct lock_struct * plock )
1996-05-04 11:50:46 +04:00
{
2006-11-11 20:05:11 +03:00
int strict_locking = lp_strict_locking ( fsp - > conn - > params ) ;
2009-03-14 00:15:28 +03:00
bool ret = False ;
if ( plock - > size = = 0 ) {
return True ;
2006-04-10 19:33:04 +04:00
}
1996-05-04 11:50:46 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) | | ! strict_locking ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
1998-07-28 22:26:47 +04:00
2004-12-11 03:30:28 +03:00
if ( strict_locking = = Auto ) {
2009-03-14 00:15:28 +03:00
if ( EXCLUSIVE_OPLOCK_TYPE ( fsp - > oplock_type ) & & ( plock - > lock_type = = READ_LOCK | | plock - > lock_type = = WRITE_LOCK ) ) {
2009-07-11 01:50:37 +04:00
DEBUG ( 10 , ( " is_locked: optimisation - exclusive oplock on file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2009-03-14 00:15:28 +03:00
ret = True ;
2005-09-30 21:13:37 +04:00
} else if ( ( fsp - > oplock_type = = LEVEL_II_OPLOCK ) & &
2009-03-14 00:15:28 +03:00
( plock - > lock_type = = READ_LOCK ) ) {
2009-07-11 01:50:37 +04:00
DEBUG ( 10 , ( " is_locked: optimisation - level II oplock on file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2009-03-14 00:15:28 +03:00
ret = True ;
2004-12-11 03:30:28 +03:00
} else {
2009-11-16 11:40:47 +03:00
struct byte_range_lock * br_lck ;
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
2009-03-14 00:15:28 +03:00
ret = brl_locktest ( br_lck ,
2010-05-07 17:20:50 +04:00
plock - > context . smblctx ,
2009-03-14 00:15:28 +03:00
plock - > context . pid ,
plock - > start ,
plock - > size ,
plock - > lock_type ,
plock - > lock_flav ) ;
2004-12-11 03:30:28 +03:00
}
2004-12-10 01:10:40 +03:00
} else {
2009-11-16 11:40:47 +03:00
struct byte_range_lock * br_lck ;
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
2009-03-14 00:15:28 +03:00
ret = brl_locktest ( br_lck ,
2010-05-07 17:20:50 +04:00
plock - > context . smblctx ,
2009-03-14 00:15:28 +03:00
plock - > context . pid ,
plock - > start ,
plock - > size ,
plock - > lock_type ,
plock - > lock_flav ) ;
2004-12-10 01:10:40 +03:00
}
2001-06-30 05:59:48 +04:00
2009-03-14 00:15:28 +03:00
DEBUG ( 10 , ( " strict_lock_default: flavour = %s brl start=%.0f "
" len=%.0f %s for fnum %d file %s \n " ,
lock_flav_name ( plock - > lock_flav ) ,
( double ) plock - > start , ( double ) plock - > size ,
ret ? " unlocked " : " locked " ,
2009-07-11 01:50:37 +04:00
plock - > fnum , fsp_str_dbg ( fsp ) ) ) ;
1996-05-04 11:50:46 +04:00
2006-04-10 19:33:04 +04:00
return ret ;
}
2009-03-14 00:15:28 +03:00
void strict_unlock_default ( files_struct * fsp , struct lock_struct * plock )
{
}
2006-04-10 19:33:04 +04:00
/****************************************************************************
Find out if a lock could be granted - return who is blocking us if we can ' t .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2006-04-10 19:33:04 +04:00
NTSTATUS query_lock ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t * psmblctx ,
2008-10-14 03:59:36 +04:00
uint64_t * pcount ,
uint64_t * poffset ,
2006-04-10 19:33:04 +04:00
enum brl_type * plock_type ,
enum brl_flavour lock_flav )
{
struct byte_range_lock * br_lck = NULL ;
2000-04-12 21:59:09 +04:00
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
return fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2001-06-30 05:59:48 +04:00
}
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-04-10 19:33:04 +04:00
return NT_STATUS_OK ;
}
2009-11-16 11:40:47 +03:00
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2009-11-16 11:40:47 +03:00
return brl_lockquery ( br_lck ,
2010-05-07 17:20:50 +04:00
psmblctx ,
2010-07-04 22:01:48 +04:00
sconn_server_id ( fsp - > conn - > sconn ) ,
2006-04-10 19:33:04 +04:00
poffset ,
pcount ,
plock_type ,
lock_flav ) ;
2000-04-12 21:59:09 +04:00
}
1996-05-04 11:50:46 +04:00
2009-02-10 08:51:29 +03:00
static void increment_current_lock_count ( files_struct * fsp ,
enum brl_flavour lock_flav )
{
if ( lock_flav = = WINDOWS_LOCK & &
fsp - > current_lock_count ! = NO_LOCKING_COUNT ) {
/* blocking ie. pending, locks also count here,
* as this is an efficiency counter to avoid checking
* the lock db . on close . JRA . */
fsp - > current_lock_count + + ;
} else {
/* Notice that this has had a POSIX lock request.
* We can ' t count locks after this so forget them .
*/
fsp - > current_lock_count = NO_LOCKING_COUNT ;
}
}
static void decrement_current_lock_count ( files_struct * fsp ,
enum brl_flavour lock_flav )
{
if ( lock_flav = = WINDOWS_LOCK & &
fsp - > current_lock_count ! = NO_LOCKING_COUNT ) {
SMB_ASSERT ( fsp - > current_lock_count > 0 ) ;
fsp - > current_lock_count - - ;
}
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
1998-07-23 04:10:26 +04:00
Utility function called by locking requests .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2007-05-14 17:01:28 +04:00
struct byte_range_lock * do_lock ( struct messaging_context * msg_ctx ,
files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2006-04-10 19:33:04 +04:00
enum brl_type lock_type ,
enum brl_flavour lock_flav ,
2007-10-19 04:40:25 +04:00
bool blocking_lock ,
2007-05-20 00:57:12 +04:00
NTSTATUS * perr ,
2010-05-07 17:20:50 +04:00
uint64_t * psmblctx ,
2009-02-10 08:51:29 +03:00
struct blocking_lock_record * blr )
1996-05-04 11:50:46 +04:00
{
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck = NULL ;
1999-12-21 12:25:59 +03:00
2010-04-29 03:42:04 +04:00
/* silently return ok on print files as we don't do locking there */
if ( fsp - > print_file ) {
* perr = NT_STATUS_OK ;
return NULL ;
}
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
2006-07-18 05:05:51 +04:00
* perr = fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
return NULL ;
2006-04-10 19:33:04 +04:00
}
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 05:05:51 +04:00
* perr = NT_STATUS_OK ;
return NULL ;
2006-04-10 19:33:04 +04:00
}
1999-12-21 12:25:59 +03:00
2001-08-24 08:55:28 +04:00
/* NOTE! 0 byte long ranges ARE allowed and should be stored */
2009-02-10 08:51:29 +03:00
DEBUG ( 10 , ( " do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
" blocking_lock=%s requested for fnum %d file %s \n " ,
2006-04-10 19:33:04 +04:00
lock_flav_name ( lock_flav ) , lock_type_name ( lock_type ) ,
2009-02-10 08:51:29 +03:00
( double ) offset , ( double ) count , blocking_lock ? " true " :
2009-07-11 01:50:37 +04:00
" false " , fsp - > fnum , fsp_str_dbg ( fsp ) ) ) ;
2006-04-10 19:33:04 +04:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2006-07-18 05:05:51 +04:00
* perr = NT_STATUS_NO_MEMORY ;
return NULL ;
2000-01-13 15:09:36 +03:00
}
1999-12-21 12:25:59 +03:00
2007-05-14 17:01:28 +04:00
* perr = brl_lock ( msg_ctx ,
br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2010-07-04 22:02:30 +04:00
sconn_server_id ( fsp - > conn - > sconn ) ,
2006-04-10 19:33:04 +04:00
offset ,
2010-05-07 17:20:50 +04:00
count ,
2006-04-10 19:33:04 +04:00
lock_type ,
lock_flav ,
2007-05-20 00:57:12 +04:00
blocking_lock ,
2010-05-07 17:20:50 +04:00
psmblctx ,
2009-02-10 08:51:29 +03:00
blr ) ;
2006-04-10 19:33:04 +04:00
2009-02-10 08:51:29 +03:00
DEBUG ( 10 , ( " do_lock: returning status=%s \n " , nt_errstr ( * perr ) ) ) ;
2007-03-07 00:59:51 +03:00
2009-02-10 08:51:29 +03:00
increment_current_lock_count ( fsp , lock_flav ) ;
2006-07-18 05:05:51 +04:00
return br_lck ;
1996-05-04 11:50:46 +04:00
}
2002-03-13 23:28:19 +03:00
/****************************************************************************
2006-07-18 01:09:02 +04:00
Utility function called by unlocking requests .
2002-03-13 23:28:19 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-14 17:01:28 +04:00
NTSTATUS do_unlock ( struct messaging_context * msg_ctx ,
files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2006-07-18 01:09:02 +04:00
enum brl_flavour lock_flav )
2002-03-13 23:28:19 +03:00
{
2007-10-19 04:40:25 +04:00
bool ok = False ;
2006-07-18 01:09:02 +04:00
struct byte_range_lock * br_lck = NULL ;
if ( ! fsp - > can_lock ) {
return fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2006-04-10 19:33:04 +04:00
}
2006-07-18 01:09:02 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 01:09:02 +04:00
return NT_STATUS_OK ;
}
DEBUG ( 10 , ( " do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s \n " ,
2009-07-11 01:50:37 +04:00
( double ) offset , ( double ) count , fsp - > fnum ,
fsp_str_dbg ( fsp ) ) ) ;
2002-03-13 23:49:57 +03:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-07-18 01:09:02 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2006-04-10 19:33:04 +04:00
2007-05-14 17:01:28 +04:00
ok = brl_unlock ( msg_ctx ,
br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2010-07-04 22:05:43 +04:00
sconn_server_id ( fsp - > conn - > sconn ) ,
2006-07-18 01:09:02 +04:00
offset ,
count ,
lock_flav ) ;
TALLOC_FREE ( br_lck ) ;
2006-04-10 19:33:04 +04:00
2006-07-18 01:09:02 +04:00
if ( ! ok ) {
DEBUG ( 10 , ( " do_unlock: returning ERRlock. \n " ) ) ;
return NT_STATUS_RANGE_NOT_LOCKED ;
2002-03-13 23:28:19 +03:00
}
2006-07-18 01:09:02 +04:00
2009-02-10 08:51:29 +03:00
decrement_current_lock_count ( fsp , lock_flav ) ;
2006-07-18 01:09:02 +04:00
return NT_STATUS_OK ;
2002-03-13 23:28:19 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2006-07-18 01:09:02 +04:00
Cancel any pending blocked locks .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-13 03:01:11 +04:00
2006-07-18 01:09:02 +04:00
NTSTATUS do_lock_cancel ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64 smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2009-02-10 08:51:29 +03:00
enum brl_flavour lock_flav ,
struct blocking_lock_record * blr )
1996-05-04 11:50:46 +04:00
{
2007-10-19 04:40:25 +04:00
bool ok = False ;
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck = NULL ;
2009-02-10 08:51:29 +03:00
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
2006-07-18 01:09:02 +04:00
return fsp - > is_directory ?
NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2006-04-10 19:33:04 +04:00
}
1999-12-21 12:25:59 +03:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 01:09:02 +04:00
return NT_STATUS_DOS ( ERRDOS , ERRcancelviolation ) ;
2000-04-26 00:30:58 +04:00
}
2006-07-18 01:09:02 +04:00
DEBUG ( 10 , ( " do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s \n " ,
2009-07-11 01:50:37 +04:00
( double ) offset , ( double ) count , fsp - > fnum ,
fsp_str_dbg ( fsp ) ) ) ;
2000-04-12 21:59:09 +04:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2003-04-05 00:38:12 +04:00
2006-07-18 01:09:02 +04:00
ok = brl_lock_cancel ( br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2010-07-04 22:06:07 +04:00
sconn_server_id ( fsp - > conn - > sconn ) ,
2006-04-10 19:33:04 +04:00
offset ,
count ,
2009-02-10 08:51:29 +03:00
lock_flav ,
blr ) ;
2006-07-11 22:01:26 +04:00
TALLOC_FREE ( br_lck ) ;
2006-04-10 19:33:04 +04:00
1999-12-21 12:25:59 +03:00
if ( ! ok ) {
2006-07-18 01:09:02 +04:00
DEBUG ( 10 , ( " do_lock_cancel: returning ERRcancelviolation. \n " ) ) ;
return NT_STATUS_DOS ( ERRDOS , ERRcancelviolation ) ;
1999-12-21 12:25:59 +03:00
}
2006-04-10 19:33:04 +04:00
2009-02-10 08:51:29 +03:00
decrement_current_lock_count ( fsp , lock_flav ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_OK ;
1996-05-04 11:50:46 +04:00
}
2000-04-26 00:30:58 +04:00
/****************************************************************************
Remove any locks on this fd . Called from file_close ( ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-14 17:01:28 +04:00
void locking_close_file ( struct messaging_context * msg_ctx ,
2010-05-07 12:20:26 +04:00
files_struct * fsp ,
enum file_close_type close_type )
2000-04-26 00:30:58 +04:00
{
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck ;
2000-04-26 00:30:58 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2000-04-26 00:30:58 +04:00
return ;
2006-04-10 19:33:04 +04:00
}
2000-04-26 00:30:58 +04:00
2007-03-07 00:59:51 +03:00
/* If we have not outstanding locks or pending
* locks then we don ' t need to look in the lock db .
*/
if ( fsp - > current_lock_count = = 0 ) {
return ;
}
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-07-18 01:09:02 +04:00
2006-07-11 22:01:26 +04:00
if ( br_lck ) {
2010-05-07 12:20:26 +04:00
cancel_pending_lock_requests_by_fid ( fsp , br_lck , close_type ) ;
2007-05-14 17:01:28 +04:00
brl_close_fnum ( msg_ctx , br_lck ) ;
2006-07-11 22:01:26 +04:00
TALLOC_FREE ( br_lck ) ;
2000-04-26 00:30:58 +04:00
}
}
1997-10-20 12:46:00 +04:00
/****************************************************************************
1998-07-23 04:10:26 +04:00
Initialise the locking functions .
1997-10-20 12:46:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-23 00:35:48 +04:00
2007-12-05 22:53:22 +03:00
static bool locking_init_internal ( bool read_only )
1996-08-15 19:11:34 +04:00
{
2000-01-16 14:14:44 +03:00
brl_init ( read_only ) ;
2000-01-13 15:09:36 +03:00
2007-05-27 13:57:05 +04:00
if ( lock_db )
2001-05-23 00:35:48 +04:00
return True ;
1997-10-29 05:18:08 +03:00
2008-03-17 16:40:07 +03:00
lock_db = db_open ( NULL , lock_path ( " locking.tdb " ) ,
lp_open_files_db_hash_size ( ) ,
2010-09-27 16:46:07 +04:00
TDB_DEFAULT | TDB_VOLATILE | TDB_CLEAR_IF_FIRST | TDB_INCOMPATIBLE_HASH ,
2007-05-27 13:57:05 +04:00
read_only ? O_RDONLY : O_RDWR | O_CREAT , 0644 ) ;
1998-07-29 07:08:05 +04:00
2007-05-27 13:57:05 +04:00
if ( ! lock_db ) {
2000-09-11 11:02:43 +04:00
DEBUG ( 0 , ( " ERROR: Failed to initialise locking database \n " ) ) ;
1999-12-13 16:27:58 +03:00
return False ;
}
2005-05-01 11:01:18 +04:00
2000-06-13 19:46:46 +04:00
if ( ! posix_locking_init ( read_only ) )
2000-04-29 00:54:23 +04:00
return False ;
1999-12-13 16:27:58 +03:00
return True ;
1996-05-04 11:50:46 +04:00
}
2007-12-05 22:53:22 +03:00
bool locking_init ( void )
{
return locking_init_internal ( false ) ;
}
bool locking_init_readonly ( void )
{
return locking_init_internal ( true ) ;
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
1998-07-23 04:10:26 +04:00
Deinitialize the share_mode management .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-23 00:35:48 +04:00
2007-10-19 04:40:25 +04:00
bool locking_end ( void )
1996-05-04 11:50:46 +04:00
{
2007-12-05 22:53:22 +03:00
brl_shutdown ( ) ;
TALLOC_FREE ( lock_db ) ;
return true ;
1996-05-04 11:50:46 +04:00
}
1999-12-21 12:25:59 +03:00
/*******************************************************************
2001-08-21 05:25:45 +04:00
Form a static locking key for a dev / inode pair .
1999-12-21 12:25:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-08-21 05:25:45 +04:00
2007-12-05 22:41:24 +03:00
static TDB_DATA locking_key ( const struct file_id * id , struct file_id * tmp )
1999-12-21 12:25:59 +03:00
{
2007-12-05 22:41:24 +03:00
* tmp = * id ;
return make_tdb_data ( ( const uint8_t * ) tmp , sizeof ( * tmp ) ) ;
1999-12-21 12:25:59 +03:00
}
2001-08-21 05:25:45 +04:00
2002-01-08 03:52:36 +03:00
/*******************************************************************
Print out a share mode .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-05-27 22:23:59 +04:00
char * share_mode_str ( TALLOC_CTX * ctx , int num , const struct share_mode_entry * e )
2002-01-08 03:52:36 +03:00
{
2007-11-16 05:27:26 +03:00
return talloc_asprintf ( ctx , " share_mode_entry[%d]: %s "
2005-09-30 21:13:37 +04:00
" pid = %s, share_access = 0x%x, private_options = 0x%x, "
2010-04-13 08:40:28 +04:00
" access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
2011-01-26 01:01:52 +03:00
" uid = %u, flags = %u, file_id %s, name_hash = 0x%x " ,
2006-01-27 22:54:39 +03:00
num ,
e - > op_type = = UNUSED_SHARE_MODE_ENTRY ? " UNUSED " : " " ,
procid_str_static ( & e - > pid ) ,
2005-07-08 08:51:27 +04:00
e - > share_access , e - > private_options ,
2010-04-13 08:40:28 +04:00
e - > access_mask , ( unsigned long long ) e - > op_mid ,
e - > op_type , e - > share_file_id ,
2007-02-07 00:05:34 +03:00
( unsigned int ) e - > uid , ( unsigned int ) e - > flags ,
2011-01-26 01:01:52 +03:00
file_id_string_tos ( & e - > id ) ,
( unsigned int ) e - > name_hash ) ;
2002-01-08 03:52:36 +03:00
}
/*******************************************************************
Print out a share mode table .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void print_share_mode_table ( struct locking_data * data )
{
2005-07-08 08:51:27 +04:00
int num_share_modes = data - > u . s . num_share_mode_entries ;
2005-09-30 21:13:37 +04:00
struct share_mode_entry * shares =
( struct share_mode_entry * ) ( data + 1 ) ;
2002-01-08 03:52:36 +03:00
int i ;
for ( i = 0 ; i < num_share_modes ; i + + ) {
2006-01-27 22:54:39 +03:00
struct share_mode_entry entry ;
2007-11-18 16:24:54 +03:00
char * str ;
2006-01-27 22:54:39 +03:00
2007-11-18 16:45:19 +03:00
/*
* We need to memcpy the entry here due to alignment
* restrictions that are not met when directly accessing
* shares [ i ]
*/
2006-01-27 22:54:39 +03:00
memcpy ( & entry , & shares [ i ] , sizeof ( struct share_mode_entry ) ) ;
2007-11-18 16:45:19 +03:00
str = share_mode_str ( talloc_tos ( ) , i , & entry ) ;
2007-11-18 16:24:54 +03:00
DEBUG ( 10 , ( " print_share_mode_table: %s \n " , str ? str : " " ) ) ;
2007-11-16 05:27:26 +03:00
TALLOC_FREE ( str ) ;
2002-01-08 03:52:36 +03:00
}
}
2011-01-26 01:23:19 +03:00
static int parse_delete_tokens_list ( struct share_mode_lock * lck ,
struct locking_data * pdata ,
const TDB_DATA dbuf )
{
uint8_t * p = dbuf . dptr + sizeof ( struct locking_data ) +
( lck - > num_share_modes *
sizeof ( struct share_mode_entry ) ) ;
uint8_t * end_ptr = dbuf . dptr + ( dbuf . dsize - 2 ) ;
int delete_tokens_size = 0 ;
int i ;
lck - > delete_tokens = NULL ;
for ( i = 0 ; i < pdata - > u . s . num_delete_token_entries ; i + + ) {
uint32_t token_len ;
struct delete_token_list * pdtl ;
if ( end_ptr - p < ( sizeof ( uint32_t ) + sizeof ( uint32_t ) +
sizeof ( uid_t ) + sizeof ( gid_t ) ) ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: "
" corrupt token list (%u) " ,
( unsigned int ) ( end_ptr - p ) ) ) ;
smb_panic ( " corrupt token list " ) ;
return - 1 ;
}
memcpy ( & token_len , p , sizeof ( token_len ) ) ;
delete_tokens_size + = token_len ;
if ( p + token_len > end_ptr | | token_len < sizeof ( token_len ) +
sizeof ( pdtl - > name_hash ) +
sizeof ( uid_t ) +
sizeof ( gid_t ) ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: "
" invalid token length (%u) \n " ,
( unsigned int ) token_len ) ) ;
smb_panic ( " invalid token length " ) ;
return - 1 ;
}
p + = sizeof ( token_len ) ;
pdtl = TALLOC_ZERO_P ( lck , struct delete_token_list ) ;
if ( pdtl = = NULL ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: talloc failed " ) ) ;
return - 1 ;
}
/* Copy out the name_hash. */
memcpy ( & pdtl - > name_hash , p , sizeof ( pdtl - > name_hash ) ) ;
p + = sizeof ( pdtl - > name_hash ) ;
2011-02-11 10:49:15 +03:00
pdtl - > delete_token = TALLOC_ZERO_P ( pdtl , struct security_unix_token ) ;
2011-01-26 01:23:19 +03:00
if ( pdtl - > delete_token = = NULL ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: talloc failed " ) ) ;
return - 1 ;
}
/* Copy out the uid and gid. */
memcpy ( & pdtl - > delete_token - > uid , p , sizeof ( uid_t ) ) ;
p + = sizeof ( uid_t ) ;
memcpy ( & pdtl - > delete_token - > gid , p , sizeof ( gid_t ) ) ;
p + = sizeof ( gid_t ) ;
token_len - = ( sizeof ( token_len ) + sizeof ( pdtl - > name_hash ) +
sizeof ( uid_t ) + sizeof ( gid_t ) ) ;
/* Any supplementary groups ? */
if ( token_len ) {
int j ;
if ( token_len % sizeof ( gid_t ) ! = 0 ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: "
" corrupt group list (%u) " ,
( unsigned int ) ( token_len % sizeof ( gid_t ) ) ) ) ;
smb_panic ( " corrupt group list " ) ;
return - 1 ;
}
pdtl - > delete_token - > ngroups = token_len / sizeof ( gid_t ) ;
pdtl - > delete_token - > groups = TALLOC_ARRAY ( pdtl - > delete_token , gid_t ,
pdtl - > delete_token - > ngroups ) ;
if ( pdtl - > delete_token - > groups = = NULL ) {
DEBUG ( 0 , ( " parse_delete_tokens_list: talloc failed " ) ) ;
return - 1 ;
}
for ( j = 0 ; j < pdtl - > delete_token - > ngroups ; j + + ) {
memcpy ( & pdtl - > delete_token - > groups [ j ] , p , sizeof ( gid_t ) ) ;
p + = sizeof ( gid_t ) ;
}
}
/* Add to the list. */
DLIST_ADD ( lck - > delete_tokens , pdtl ) ;
}
return delete_tokens_size ;
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
1998-07-23 04:10:26 +04:00
Get all share mode entries for a dev / inode pair .
1997-10-20 12:46:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-08-21 05:25:45 +04:00
2009-07-09 01:27:06 +04:00
static bool parse_share_modes ( const TDB_DATA dbuf , struct share_mode_lock * lck )
1997-05-20 04:32:51 +04:00
{
2008-07-04 11:28:04 +04:00
struct locking_data data ;
2011-01-26 01:23:19 +03:00
int delete_tokens_size ;
2005-09-30 21:13:37 +04:00
int i ;
2005-07-08 08:51:27 +04:00
2005-09-30 21:13:37 +04:00
if ( dbuf . dsize < sizeof ( struct locking_data ) ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " parse_share_modes: buffer too short " ) ;
2005-09-30 21:13:37 +04:00
}
2001-11-30 04:59:03 +03:00
2008-07-04 11:28:04 +04:00
memcpy ( & data , dbuf . dptr , sizeof ( data ) ) ;
2004-06-08 20:14:31 +04:00
2008-07-04 11:28:04 +04:00
lck - > old_write_time = data . u . s . old_write_time ;
lck - > changed_write_time = data . u . s . changed_write_time ;
lck - > num_share_modes = data . u . s . num_share_mode_entries ;
2001-11-30 04:59:03 +03:00
2011-01-26 01:23:19 +03:00
DEBUG ( 10 , ( " parse_share_modes: owrt: %s, "
" cwrt: %s, ntok: %u, num_share_modes: %d \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t ( lck - > old_write_time ) ) ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t (
lck - > changed_write_time ) ) ,
2011-01-26 01:23:19 +03:00
( unsigned int ) data . u . s . num_delete_token_entries ,
2008-03-12 17:32:47 +03:00
lck - > num_share_modes ) ) ;
2001-11-30 04:59:03 +03:00
2005-09-30 21:13:37 +04:00
if ( ( lck - > num_share_modes < 0 ) | | ( lck - > num_share_modes > 1000000 ) ) {
DEBUG ( 0 , ( " invalid number of share modes: %d \n " ,
lck - > num_share_modes ) ) ;
2007-06-16 01:58:49 +04:00
smb_panic ( " parse_share_modes: invalid number of share modes " ) ;
2001-11-30 04:59:03 +03:00
}
2005-09-30 21:13:37 +04:00
lck - > share_modes = NULL ;
2006-02-02 23:44:50 +03:00
2005-09-30 21:13:37 +04:00
if ( lck - > num_share_modes ! = 0 ) {
2005-07-08 08:51:27 +04:00
2005-09-30 21:13:37 +04:00
if ( dbuf . dsize < ( sizeof ( struct locking_data ) +
( lck - > num_share_modes *
sizeof ( struct share_mode_entry ) ) ) ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " parse_share_modes: buffer too short " ) ;
2005-09-30 21:13:37 +04:00
}
2006-07-11 22:01:26 +04:00
lck - > share_modes = ( struct share_mode_entry * )
2008-07-04 11:28:04 +04:00
TALLOC_MEMDUP ( lck ,
dbuf . dptr + sizeof ( struct locking_data ) ,
2006-07-11 22:01:26 +04:00
lck - > num_share_modes *
sizeof ( struct share_mode_entry ) ) ;
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
2005-09-30 21:13:37 +04:00
if ( lck - > share_modes = = NULL ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " parse_share_modes: talloc failed " ) ;
2005-09-30 21:13:37 +04:00
}
}
2001-09-18 10:41:29 +04:00
2011-01-26 01:23:19 +03:00
/* Get any delete tokens. */
delete_tokens_size = parse_delete_tokens_list ( lck , & data , dbuf ) ;
if ( delete_tokens_size < 0 ) {
smb_panic ( " parse_share_modes: parse_delete_tokens_list failed " ) ;
2006-02-02 23:44:50 +03:00
}
2005-12-13 01:07:36 +03:00
/* Save off the associated service path and filename. */
2008-07-04 11:28:04 +04:00
lck - > servicepath = ( const char * ) dbuf . dptr + sizeof ( struct locking_data ) +
2008-01-07 14:57:10 +03:00
( lck - > num_share_modes * sizeof ( struct share_mode_entry ) ) +
2011-01-26 01:23:19 +03:00
delete_tokens_size ;
2008-01-07 14:57:10 +03:00
2009-07-09 03:22:44 +04:00
lck - > base_name = ( const char * ) dbuf . dptr + sizeof ( struct locking_data ) +
2008-01-07 14:57:10 +03:00
( lck - > num_share_modes * sizeof ( struct share_mode_entry ) ) +
2011-01-26 01:23:19 +03:00
delete_tokens_size +
2008-01-07 14:57:10 +03:00
strlen ( lck - > servicepath ) + 1 ;
2001-08-21 05:25:45 +04:00
2009-07-09 03:22:44 +04:00
lck - > stream_name = ( const char * ) dbuf . dptr + sizeof ( struct locking_data ) +
( lck - > num_share_modes * sizeof ( struct share_mode_entry ) ) +
2011-01-26 01:23:19 +03:00
delete_tokens_size +
2009-07-09 03:22:44 +04:00
strlen ( lck - > servicepath ) + 1 +
strlen ( lck - > base_name ) + 1 ;
2005-09-30 21:13:37 +04:00
/*
* Ensure that each entry has a real process attached .
*/
2001-08-21 05:25:45 +04:00
2005-09-30 21:13:37 +04:00
for ( i = 0 ; i < lck - > num_share_modes ; i + + ) {
struct share_mode_entry * entry_p = & lck - > share_modes [ i ] ;
2008-01-05 01:27:14 +03:00
char * str = NULL ;
if ( DEBUGLEVEL > = 10 ) {
str = share_mode_str ( NULL , i , entry_p ) ;
}
2005-09-30 21:13:37 +04:00
DEBUG ( 10 , ( " parse_share_modes: %s \n " ,
2007-11-16 05:27:26 +03:00
str ? str : " " ) ) ;
s3: Fix a long-standing problem with recycled PIDs
When a samba server process dies hard, it has no chance to clean up its entries
in locking.tdb, brlock.tdb, connections.tdb and sessionid.tdb.
For locking.tdb and brlock.tdb Samba is robust by checking every time we read
an entry from the database if the corresponding process still exists. If it
does not exist anymore, the entry is deleted. This is not 100% failsafe though:
On systems with a limited PID space there is a non-zero chance that between the
smbd's death and the fresh access, the PID is recycled by another long-running
process. This renders all files that had been locked by the killed smbd
potentially unusable until the new process also dies.
This patch is supposed to fix the problem the following way: Every process ID
in every database is augmented by a random 64-bit number that is stored in a
serverid.tdb. Whenever we need to check if a process still exists we know its
PID and the 64-bit number. We look up the PID in serverid.tdb and compare the
64-bit number. If it's the same, the process still is a valid smbd holding the
lock. If it is different, a new smbd has taken over.
I believe this is safe against an smbd that has died hard and the PID has been
taken over by a non-samba process. This process would not have registered
itself with a fresh 64-bit number in serverid.tdb, so the old one still exists
in serverid.tdb. We protect against this case by the parent smbd taking care of
deregistering PIDs from serverid.tdb and the fact that serverid.tdb is
CLEAR_IF_FIRST.
CLEAR_IF_FIRST does not work in a cluster, so the automatic cleanup does not
work when all smbds are restarted. For this, "net serverid wipe" has to be run
before smbd starts up. As a convenience, "net serverid wipedbs" also cleans up
sessionid.tdb and connections.tdb.
While there, this also cleans up overloading connections.tdb with all the
process entries just for messaging_send_all().
Volker
2010-03-02 19:02:01 +03:00
if ( ! serverid_exists ( & entry_p - > pid ) ) {
2005-09-30 21:13:37 +04:00
DEBUG ( 10 , ( " parse_share_modes: deleted %s \n " ,
2007-11-16 05:27:26 +03:00
str ? str : " " ) ) ;
2005-09-30 21:13:37 +04:00
entry_p - > op_type = UNUSED_SHARE_MODE_ENTRY ;
lck - > modified = True ;
}
2007-11-16 05:27:26 +03:00
TALLOC_FREE ( str ) ;
2001-10-21 01:59:34 +04:00
}
2005-09-30 21:13:37 +04:00
return True ;
2001-08-21 05:25:45 +04:00
}
2009-07-09 01:27:06 +04:00
static TDB_DATA unparse_share_modes ( const struct share_mode_lock * lck )
1999-12-21 12:25:59 +03:00
{
2005-09-30 21:13:37 +04:00
TDB_DATA result ;
int num_valid = 0 ;
int i ;
1999-12-21 12:25:59 +03:00
struct locking_data * data ;
2005-09-30 21:13:37 +04:00
ssize_t offset ;
2009-07-09 03:22:44 +04:00
ssize_t sp_len , bn_len , sn_len ;
2011-01-26 01:23:19 +03:00
uint32_t delete_tokens_size = 0 ;
struct delete_token_list * pdtl = NULL ;
uint32_t num_delete_token_entries = 0 ;
2001-08-22 04:29:40 +04:00
2005-09-30 21:13:37 +04:00
result . dptr = NULL ;
result . dsize = 0 ;
2001-08-22 04:29:40 +04:00
2005-09-30 21:13:37 +04:00
for ( i = 0 ; i < lck - > num_share_modes ; i + + ) {
if ( ! is_unused_share_mode_entry ( & lck - > share_modes [ i ] ) ) {
num_valid + = 1 ;
1999-12-21 12:25:59 +03:00
}
}
2005-09-30 21:13:37 +04:00
if ( num_valid = = 0 ) {
return result ;
2000-04-23 18:25:04 +04:00
}
1997-05-20 04:32:51 +04:00
2005-12-13 01:07:36 +03:00
sp_len = strlen ( lck - > servicepath ) ;
2009-07-09 03:22:44 +04:00
bn_len = strlen ( lck - > base_name ) ;
sn_len = lck - > stream_name ! = NULL ? strlen ( lck - > stream_name ) : 0 ;
2011-01-26 01:23:19 +03:00
for ( pdtl = lck - > delete_tokens ; pdtl ; pdtl = pdtl - > next ) {
num_delete_token_entries + + ;
delete_tokens_size + = ( sizeof ( uint32_t ) +
sizeof ( uint32_t ) +
sizeof ( uid_t ) +
sizeof ( gid_t ) +
pdtl - > delete_token - > ngroups * sizeof ( gid_t ) ) ;
}
2005-12-13 01:07:36 +03:00
2005-09-30 21:13:37 +04:00
result . dsize = sizeof ( * data ) +
lck - > num_share_modes * sizeof ( struct share_mode_entry ) +
2011-01-26 01:23:19 +03:00
delete_tokens_size +
2005-12-13 01:07:36 +03:00
sp_len + 1 +
2009-07-09 03:22:44 +04:00
bn_len + 1 +
sn_len + 1 ;
2007-03-29 13:35:51 +04:00
result . dptr = TALLOC_ARRAY ( lck , uint8 , result . dsize ) ;
2001-08-21 05:25:45 +04:00
2005-09-30 21:13:37 +04:00
if ( result . dptr = = NULL ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " talloc failed " ) ;
2005-09-30 21:13:37 +04:00
}
2001-08-21 05:25:45 +04:00
2005-09-30 21:13:37 +04:00
data = ( struct locking_data * ) result . dptr ;
ZERO_STRUCTP ( data ) ;
data - > u . s . num_share_mode_entries = lck - > num_share_modes ;
2008-03-12 17:32:47 +03:00
data - > u . s . old_write_time = lck - > old_write_time ;
data - > u . s . changed_write_time = lck - > changed_write_time ;
2011-01-26 01:23:19 +03:00
data - > u . s . num_delete_token_entries = num_delete_token_entries ;
2008-03-12 17:32:47 +03:00
2011-01-26 01:23:19 +03:00
DEBUG ( 10 , ( " unparse_share_modes: owrt: %s cwrt: %s, ntok: %u, "
" num: %d \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t ( lck - > old_write_time ) ) ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t (
lck - > changed_write_time ) ) ,
2011-01-26 01:23:19 +03:00
( unsigned int ) data - > u . s . num_delete_token_entries ,
2008-03-12 17:32:47 +03:00
data - > u . s . num_share_mode_entries ) ) ;
2005-09-30 21:13:37 +04:00
memcpy ( result . dptr + sizeof ( * data ) , lck - > share_modes ,
sizeof ( struct share_mode_entry ) * lck - > num_share_modes ) ;
offset = sizeof ( * data ) +
sizeof ( struct share_mode_entry ) * lck - > num_share_modes ;
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
/* Store any delete on close tokens. */
for ( pdtl = lck - > delete_tokens ; pdtl ; pdtl = pdtl - > next ) {
2011-02-11 10:49:15 +03:00
struct security_unix_token * pdt = pdtl - > delete_token ;
2011-01-26 01:23:19 +03:00
uint32_t token_size = sizeof ( uint32_t ) +
sizeof ( uint32_t ) +
sizeof ( uid_t ) +
sizeof ( gid_t ) +
( pdt - > ngroups * sizeof ( gid_t ) ) ;
uint8_t * p = result . dptr + offset ;
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
memcpy ( p , & token_size , sizeof ( uint32_t ) ) ;
p + = sizeof ( uint32_t ) ;
memcpy ( p , & pdtl - > name_hash , sizeof ( uint32_t ) ) ;
p + = sizeof ( uint32_t ) ;
memcpy ( p , & pdt - > uid , sizeof ( uid_t ) ) ;
2006-02-09 21:18:51 +03:00
p + = sizeof ( uid_t ) ;
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
memcpy ( p , & pdt - > gid , sizeof ( gid_t ) ) ;
2006-05-18 04:22:07 +04:00
p + = sizeof ( gid_t ) ;
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
for ( i = 0 ; i < pdt - > ngroups ; i + + ) {
memcpy ( p , & pdt - > groups [ i ] , sizeof ( gid_t ) ) ;
2006-02-09 21:18:51 +03:00
p + = sizeof ( gid_t ) ;
2006-02-02 23:44:50 +03:00
}
2011-01-26 01:23:19 +03:00
offset + = token_size ;
2006-02-02 23:44:50 +03:00
}
2007-03-29 13:35:51 +04:00
safe_strcpy ( ( char * ) result . dptr + offset , lck - > servicepath ,
2005-12-13 01:07:36 +03:00
result . dsize - offset - 1 ) ;
offset + = sp_len + 1 ;
2009-07-09 03:22:44 +04:00
safe_strcpy ( ( char * ) result . dptr + offset , lck - > base_name ,
result . dsize - offset - 1 ) ;
offset + = bn_len + 1 ;
safe_strcpy ( ( char * ) result . dptr + offset , lck - > stream_name ,
2005-09-30 21:13:37 +04:00
result . dsize - offset - 1 ) ;
2006-01-27 22:59:48 +03:00
if ( DEBUGLEVEL > = 10 ) {
print_share_mode_table ( data ) ;
}
2005-09-30 21:13:37 +04:00
return result ;
1997-05-20 04:32:51 +04:00
}
2006-08-29 23:14:25 +04:00
static int share_mode_lock_destructor ( struct share_mode_lock * lck )
1997-05-20 04:32:51 +04:00
{
2007-05-27 13:57:05 +04:00
NTSTATUS status ;
2005-09-30 21:13:37 +04:00
TDB_DATA data ;
2002-01-05 05:37:23 +03:00
2005-09-30 21:13:37 +04:00
if ( ! lck - > modified ) {
2007-05-27 13:57:05 +04:00
return 0 ;
2005-09-30 21:13:37 +04:00
}
2002-01-08 03:52:36 +03:00
2005-09-30 21:13:37 +04:00
data = unparse_share_modes ( lck ) ;
2002-01-08 03:52:36 +03:00
2005-09-30 21:13:37 +04:00
if ( data . dptr = = NULL ) {
if ( ! lck - > fresh ) {
/* There has been an entry before, delete it */
2007-05-27 13:57:05 +04:00
status = lck - > record - > delete_rec ( lck - > record ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-09-05 21:20:16 +04:00
char * errmsg ;
2007-05-27 13:57:05 +04:00
DEBUG ( 0 , ( " delete_rec returned %s \n " ,
nt_errstr ( status ) ) ) ;
2009-09-05 21:20:16 +04:00
if ( asprintf ( & errmsg , " could not delete share "
" entry: %s \n " ,
nt_errstr ( status ) ) = = - 1 ) {
smb_panic ( " could not delete share "
" entry " ) ;
}
smb_panic ( errmsg ) ;
2005-09-30 21:13:37 +04:00
}
}
goto done ;
1999-12-21 12:25:59 +03:00
}
2007-05-27 13:57:05 +04:00
status = lck - > record - > store ( lck - > record , data , TDB_REPLACE ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-09-05 21:20:16 +04:00
char * errmsg ;
2007-05-27 13:57:05 +04:00
DEBUG ( 0 , ( " store returned %s \n " , nt_errstr ( status ) ) ) ;
2009-09-05 21:20:16 +04:00
if ( asprintf ( & errmsg , " could not store share mode entry: %s " ,
nt_errstr ( status ) ) = = - 1 ) {
smb_panic ( " could not store share mode entry " ) ;
}
smb_panic ( errmsg ) ;
2005-09-30 21:13:37 +04:00
}
1999-12-21 12:25:59 +03:00
2005-09-30 21:13:37 +04:00
done :
2002-01-05 05:37:23 +03:00
2005-09-30 21:13:37 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2007-10-19 04:40:25 +04:00
static bool fill_share_mode_lock ( struct share_mode_lock * lck ,
2007-05-29 13:30:34 +04:00
struct file_id id ,
2007-05-27 13:57:05 +04:00
const char * servicepath ,
2009-07-09 03:22:44 +04:00
const struct smb_filename * smb_fname ,
2008-03-12 17:32:47 +03:00
TDB_DATA share_mode_data ,
const struct timespec * old_write_time )
1999-12-21 12:25:59 +03:00
{
2005-12-14 03:42:40 +03:00
/* Ensure we set every field here as the destructor must be
valid even if parse_share_modes fails . */
lck - > servicepath = NULL ;
2009-07-09 03:22:44 +04:00
lck - > base_name = NULL ;
lck - > stream_name = NULL ;
2007-05-29 13:30:34 +04:00
lck - > id = id ;
2005-09-30 21:13:37 +04:00
lck - > num_share_modes = 0 ;
lck - > share_modes = NULL ;
2011-01-26 01:23:19 +03:00
lck - > delete_tokens = NULL ;
2008-03-12 17:32:47 +03:00
ZERO_STRUCT ( lck - > old_write_time ) ;
ZERO_STRUCT ( lck - > changed_write_time ) ;
2005-12-14 03:42:40 +03:00
lck - > fresh = False ;
2005-12-14 20:46:29 +03:00
lck - > modified = False ;
2005-09-30 21:13:37 +04:00
2007-05-27 13:57:05 +04:00
lck - > fresh = ( share_mode_data . dptr = = NULL ) ;
2005-09-30 21:13:37 +04:00
if ( lck - > fresh ) {
2009-07-09 03:22:44 +04:00
bool has_stream ;
if ( smb_fname = = NULL | | servicepath = = NULL
2008-03-12 17:32:47 +03:00
| | old_write_time = = NULL ) {
2007-05-27 13:57:05 +04:00
return False ;
2005-09-30 21:13:37 +04:00
}
2009-07-09 03:22:44 +04:00
has_stream = smb_fname - > stream_name ! = NULL ;
lck - > base_name = talloc_strdup ( lck , smb_fname - > base_name ) ;
lck - > stream_name = talloc_strdup ( lck , smb_fname - > stream_name ) ;
2005-12-13 01:07:36 +03:00
lck - > servicepath = talloc_strdup ( lck , servicepath ) ;
2009-07-09 03:22:44 +04:00
if ( lck - > base_name = = NULL | |
( has_stream & & lck - > stream_name = = NULL ) | |
lck - > servicepath = = NULL ) {
2005-09-30 21:13:37 +04:00
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
2007-05-27 13:57:05 +04:00
return False ;
2005-09-30 21:13:37 +04:00
}
2008-03-12 17:32:47 +03:00
lck - > old_write_time = * old_write_time ;
2005-09-30 21:13:37 +04:00
} else {
2007-05-27 13:57:05 +04:00
if ( ! parse_share_modes ( share_mode_data , lck ) ) {
2005-09-30 21:13:37 +04:00
DEBUG ( 0 , ( " Could not parse share modes \n " ) ) ;
2007-05-27 13:57:05 +04:00
return False ;
2000-04-23 18:25:04 +04:00
}
1999-12-21 12:25:59 +03:00
}
2007-05-27 13:57:05 +04:00
return True ;
}
struct share_mode_lock * get_share_mode_lock ( TALLOC_CTX * mem_ctx ,
2007-12-05 22:41:24 +03:00
const struct file_id id ,
2007-05-27 13:57:05 +04:00
const char * servicepath ,
2009-07-09 03:22:44 +04:00
const struct smb_filename * smb_fname ,
2008-03-12 17:32:47 +03:00
const struct timespec * old_write_time )
2007-05-27 13:57:05 +04:00
{
struct share_mode_lock * lck ;
2007-12-05 22:41:24 +03:00
struct file_id tmp ;
TDB_DATA key = locking_key ( & id , & tmp ) ;
2007-05-27 13:57:05 +04:00
if ( ! ( lck = TALLOC_P ( mem_ctx , struct share_mode_lock ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return NULL ;
}
if ( ! ( lck - > record = lock_db - > fetch_locked ( lock_db , lck , key ) ) ) {
DEBUG ( 3 , ( " Could not lock share entry \n " ) ) ;
TALLOC_FREE ( lck ) ;
return NULL ;
}
2009-07-09 03:22:44 +04:00
if ( ! fill_share_mode_lock ( lck , id , servicepath , smb_fname ,
2008-03-12 17:32:47 +03:00
lck - > record - > value , old_write_time ) ) {
2007-05-27 13:57:05 +04:00
DEBUG ( 3 , ( " fill_share_mode_lock failed \n " ) ) ;
TALLOC_FREE ( lck ) ;
return NULL ;
}
talloc_set_destructor ( lck , share_mode_lock_destructor ) ;
return lck ;
}
struct share_mode_lock * fetch_share_mode_unlocked ( TALLOC_CTX * mem_ctx ,
2009-07-09 03:22:44 +04:00
const struct file_id id )
2007-05-27 13:57:05 +04:00
{
struct share_mode_lock * lck ;
2007-12-05 22:41:24 +03:00
struct file_id tmp ;
TDB_DATA key = locking_key ( & id , & tmp ) ;
2007-05-27 13:57:05 +04:00
TDB_DATA data ;
if ( ! ( lck = TALLOC_P ( mem_ctx , struct share_mode_lock ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return NULL ;
}
if ( lock_db - > fetch ( lock_db , lck , key , & data ) = = - 1 ) {
DEBUG ( 3 , ( " Could not fetch share entry \n " ) ) ;
TALLOC_FREE ( lck ) ;
return NULL ;
}
2009-07-09 03:22:44 +04:00
if ( ! fill_share_mode_lock ( lck , id , NULL , NULL , data , NULL ) ) {
2009-03-26 14:24:50 +03:00
DEBUG ( 10 , ( " fetch_share_mode_unlocked: no share_mode record "
" around (file not open) \n " ) ) ;
2007-05-27 13:57:05 +04:00
TALLOC_FREE ( lck ) ;
return NULL ;
}
2005-09-30 21:13:37 +04:00
return lck ;
}
2001-08-21 05:25:45 +04:00
2005-12-13 01:07:36 +03:00
/*******************************************************************
Sets the service name and filename for rename .
2005-12-13 21:11:50 +03:00
At this point we emit " file renamed " messages to all
process id ' s that have this file open .
Based on an initial code idea from SATOH Fumiyasu < fumiya @ samba . gr . jp >
2005-12-13 01:07:36 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool rename_share_filename ( struct messaging_context * msg_ctx ,
2007-05-14 17:01:28 +04:00
struct share_mode_lock * lck ,
2005-12-13 01:07:36 +03:00
const char * servicepath ,
2011-01-26 01:01:52 +03:00
uint32_t orig_name_hash ,
uint32_t new_name_hash ,
2009-07-09 03:22:44 +04:00
const struct smb_filename * smb_fname_dst )
2005-12-13 01:07:36 +03:00
{
2005-12-13 21:11:50 +03:00
size_t sp_len ;
2009-07-09 03:22:44 +04:00
size_t bn_len ;
size_t sn_len ;
2005-12-13 21:11:50 +03:00
size_t msg_len ;
2006-01-13 01:17:54 +03:00
char * frm = NULL ;
2005-12-13 21:11:50 +03:00
int i ;
2009-07-09 03:22:44 +04:00
bool strip_two_chars = false ;
bool has_stream = smb_fname_dst - > stream_name ! = NULL ;
2005-12-13 21:11:50 +03:00
DEBUG ( 10 , ( " rename_share_filename: servicepath %s newname %s \n " ,
2009-07-09 03:22:44 +04:00
servicepath , smb_fname_dst - > base_name ) ) ;
2005-12-13 21:11:50 +03:00
2005-12-13 01:07:36 +03:00
/*
* rename_internal_fsp ( ) and rename_internals ( ) add ' . / ' to
* head of newname if newname does not contain a ' / ' .
*/
2009-07-09 03:22:44 +04:00
if ( smb_fname_dst - > base_name [ 0 ] & &
smb_fname_dst - > base_name [ 1 ] & &
smb_fname_dst - > base_name [ 0 ] = = ' . ' & &
smb_fname_dst - > base_name [ 1 ] = = ' / ' ) {
strip_two_chars = true ;
2005-12-13 01:07:36 +03:00
}
lck - > servicepath = talloc_strdup ( lck , servicepath ) ;
2009-07-09 03:22:44 +04:00
lck - > base_name = talloc_strdup ( lck , smb_fname_dst - > base_name +
( strip_two_chars ? 2 : 0 ) ) ;
lck - > stream_name = talloc_strdup ( lck , smb_fname_dst - > stream_name ) ;
if ( lck - > base_name = = NULL | |
( has_stream & & lck - > stream_name = = NULL ) | |
lck - > servicepath = = NULL ) {
2005-12-13 01:07:36 +03:00
DEBUG ( 0 , ( " rename_share_filename: talloc failed \n " ) ) ;
return False ;
}
lck - > modified = True ;
2005-12-13 21:11:50 +03:00
sp_len = strlen ( lck - > servicepath ) ;
2009-07-09 03:22:44 +04:00
bn_len = strlen ( lck - > base_name ) ;
sn_len = has_stream ? strlen ( lck - > stream_name ) : 0 ;
2005-12-13 21:11:50 +03:00
2009-07-09 03:22:44 +04:00
msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
sn_len + 1 ;
2005-12-13 21:11:50 +03:00
/* Set up the name changed message. */
2006-07-11 22:01:26 +04:00
frm = TALLOC_ARRAY ( lck , char , msg_len ) ;
2005-12-13 21:11:50 +03:00
if ( ! frm ) {
return False ;
}
2006-01-13 01:17:54 +03:00
2009-02-16 10:45:28 +03:00
push_file_id_24 ( frm , & lck - > id ) ;
2005-12-13 21:11:50 +03:00
2006-06-16 05:35:08 +04:00
DEBUG ( 10 , ( " rename_share_filename: msg_len = %u \n " , ( unsigned int ) msg_len ) ) ;
2005-12-13 21:11:50 +03:00
2009-02-16 10:45:28 +03:00
safe_strcpy ( & frm [ 24 ] , lck - > servicepath , sp_len ) ;
2009-07-09 03:22:44 +04:00
safe_strcpy ( & frm [ 24 + sp_len + 1 ] , lck - > base_name , bn_len ) ;
safe_strcpy ( & frm [ 24 + sp_len + 1 + bn_len + 1 ] , lck - > stream_name ,
sn_len ) ;
2005-12-13 21:11:50 +03:00
/* Send the messages. */
for ( i = 0 ; i < lck - > num_share_modes ; i + + ) {
struct share_mode_entry * se = & lck - > share_modes [ i ] ;
if ( ! is_valid_share_mode_entry ( se ) ) {
continue ;
}
2011-01-26 01:01:52 +03:00
/* If this is a hardlink to the inode
with a different name , skip this . */
if ( se - > name_hash ! = orig_name_hash ) {
continue ;
}
se - > name_hash = new_name_hash ;
2005-12-13 21:11:50 +03:00
/* But not to ourselves... */
if ( procid_is_me ( & se - > pid ) ) {
continue ;
}
2009-07-09 03:22:44 +04:00
DEBUG ( 10 , ( " rename_share_filename: sending rename message to "
" pid %s file_id %s sharepath %s base_name %s "
" stream_name %s \n " ,
2007-05-29 13:30:34 +04:00
procid_str_static ( & se - > pid ) ,
2007-09-10 14:56:07 +04:00
file_id_string_tos ( & lck - > id ) ,
2009-07-09 03:22:44 +04:00
lck - > servicepath , lck - > base_name ,
has_stream ? lck - > stream_name : " " ) ) ;
2005-12-13 21:11:50 +03:00
2007-05-27 13:29:26 +04:00
messaging_send_buf ( msg_ctx , se - > pid , MSG_SMB_FILE_RENAME ,
( uint8 * ) frm , msg_len ) ;
2005-12-13 21:11:50 +03:00
}
2005-12-13 01:07:36 +03:00
return True ;
}
2008-04-01 13:40:23 +04:00
void get_file_infos ( struct file_id id ,
2011-01-26 00:57:38 +03:00
uint32_t name_hash ,
2008-04-01 13:40:23 +04:00
bool * delete_on_close ,
struct timespec * write_time )
2008-03-12 17:32:47 +03:00
{
struct share_mode_lock * lck ;
2008-04-01 13:40:23 +04:00
if ( delete_on_close ) {
* delete_on_close = false ;
}
if ( write_time ) {
ZERO_STRUCTP ( write_time ) ;
}
2008-03-12 17:32:47 +03:00
2009-07-09 03:22:44 +04:00
if ( ! ( lck = fetch_share_mode_unlocked ( talloc_tos ( ) , id ) ) ) {
2008-04-01 13:40:23 +04:00
return ;
2008-03-12 17:32:47 +03:00
}
2008-04-01 13:40:23 +04:00
if ( delete_on_close ) {
2011-01-26 01:23:19 +03:00
* delete_on_close = is_delete_on_close_set ( lck , name_hash ) ;
2008-03-12 17:32:47 +03:00
}
2008-04-01 13:40:23 +04:00
if ( write_time ) {
struct timespec wt ;
2008-03-12 17:32:47 +03:00
2008-04-01 13:40:23 +04:00
wt = lck - > changed_write_time ;
if ( null_timespec ( wt ) ) {
wt = lck - > old_write_time ;
}
* write_time = wt ;
2007-05-27 13:57:05 +04:00
}
2008-04-01 13:40:23 +04:00
2007-05-27 13:57:05 +04:00
TALLOC_FREE ( lck ) ;
1998-10-23 07:34:50 +04:00
}
2007-10-19 04:40:25 +04:00
bool is_valid_share_mode_entry ( const struct share_mode_entry * e )
1998-10-23 07:34:50 +04:00
{
2005-09-30 21:13:37 +04:00
int num_props = 0 ;
2007-07-18 18:03:46 +04:00
if ( e - > op_type = = UNUSED_SHARE_MODE_ENTRY ) {
/* cope with dead entries from the process not
existing . These should not be considered valid ,
otherwise we end up doing zero timeout sharing
violation */
return False ;
}
2005-09-30 21:13:37 +04:00
num_props + = ( ( e - > op_type = = NO_OPLOCK ) ? 1 : 0 ) ;
num_props + = ( EXCLUSIVE_OPLOCK_TYPE ( e - > op_type ) ? 1 : 0 ) ;
num_props + = ( LEVEL_II_OPLOCK_TYPE ( e - > op_type ) ? 1 : 0 ) ;
SMB_ASSERT ( num_props < = 1 ) ;
return ( num_props ! = 0 ) ;
1998-10-23 07:34:50 +04:00
}
2007-10-19 04:40:25 +04:00
bool is_deferred_open_entry ( const struct share_mode_entry * e )
2005-09-30 21:13:37 +04:00
{
return ( e - > op_type = = DEFERRED_OPEN_ENTRY ) ;
}
2001-08-21 05:25:45 +04:00
2007-10-19 04:40:25 +04:00
bool is_unused_share_mode_entry ( const struct share_mode_entry * e )
1999-12-13 16:27:58 +03:00
{
2005-09-30 21:13:37 +04:00
return ( e - > op_type = = UNUSED_SHARE_MODE_ENTRY ) ;
1999-12-13 16:27:58 +03:00
}
/*******************************************************************
2005-09-30 21:13:37 +04:00
Fill a share mode entry .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-08-21 05:25:45 +04:00
2005-09-30 21:13:37 +04:00
static void fill_share_mode_entry ( struct share_mode_entry * e ,
files_struct * fsp ,
2010-04-13 08:40:28 +04:00
uid_t uid , uint64_t mid , uint16 op_type )
1999-12-13 16:27:58 +03:00
{
2005-09-30 21:13:37 +04:00
ZERO_STRUCTP ( e ) ;
2010-07-04 20:20:11 +04:00
e - > pid = sconn_server_id ( fsp - > conn - > sconn ) ;
2005-09-30 21:13:37 +04:00
e - > share_access = fsp - > share_access ;
e - > private_options = fsp - > fh - > private_options ;
e - > access_mask = fsp - > access_mask ;
e - > op_mid = mid ;
e - > op_type = op_type ;
e - > time . tv_sec = fsp - > open_time . tv_sec ;
e - > time . tv_usec = fsp - > open_time . tv_usec ;
2007-05-29 13:30:34 +04:00
e - > id = fsp - > file_id ;
e - > share_file_id = fsp - > fh - > gen_id ;
2006-06-21 06:31:12 +04:00
e - > uid = ( uint32 ) uid ;
2007-02-07 00:05:34 +03:00
e - > flags = fsp - > posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0 ;
2011-01-26 01:01:52 +03:00
e - > name_hash = fsp - > name_hash ;
2001-08-22 04:29:40 +04:00
}
2005-09-30 21:13:37 +04:00
static void fill_deferred_open_entry ( struct share_mode_entry * e ,
const struct timeval request_time ,
2010-07-04 22:14:27 +04:00
struct file_id id ,
struct server_id pid ,
uint64_t mid )
1998-10-23 07:34:50 +04:00
{
2005-09-30 21:13:37 +04:00
ZERO_STRUCTP ( e ) ;
2010-07-04 22:14:27 +04:00
e - > pid = pid ;
2005-09-30 21:13:37 +04:00
e - > op_mid = mid ;
e - > op_type = DEFERRED_OPEN_ENTRY ;
e - > time . tv_sec = request_time . tv_sec ;
e - > time . tv_usec = request_time . tv_usec ;
2007-05-29 13:30:34 +04:00
e - > id = id ;
2006-06-21 06:31:12 +04:00
e - > uid = ( uint32 ) - 1 ;
2007-02-07 00:05:34 +03:00
e - > flags = 0 ;
2005-09-30 21:13:37 +04:00
}
1998-10-23 07:34:50 +04:00
2005-09-30 21:13:37 +04:00
static void add_share_mode_entry ( struct share_mode_lock * lck ,
const struct share_mode_entry * entry )
{
int i ;
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
2005-09-30 21:13:37 +04:00
for ( i = 0 ; i < lck - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & lck - > share_modes [ i ] ;
if ( is_unused_share_mode_entry ( e ) ) {
* e = * entry ;
break ;
}
}
1999-12-13 16:27:58 +03:00
2005-09-30 21:13:37 +04:00
if ( i = = lck - > num_share_modes ) {
/* No unused entry found */
ADD_TO_ARRAY ( lck , struct share_mode_entry , * entry ,
& lck - > share_modes , & lck - > num_share_modes ) ;
}
lck - > modified = True ;
}
1999-12-13 16:27:58 +03:00
2005-09-30 21:13:37 +04:00
void set_share_mode ( struct share_mode_lock * lck , files_struct * fsp ,
2010-04-13 08:40:28 +04:00
uid_t uid , uint64_t mid , uint16 op_type )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry ;
2006-06-21 06:31:12 +04:00
fill_share_mode_entry ( & entry , fsp , uid , mid , op_type ) ;
2005-09-30 21:13:37 +04:00
add_share_mode_entry ( lck , & entry ) ;
}
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
2010-04-13 08:40:28 +04:00
void add_deferred_open ( struct share_mode_lock * lck , uint64_t mid ,
2005-09-30 21:13:37 +04:00
struct timeval request_time ,
2010-07-04 22:14:27 +04:00
struct server_id pid , struct file_id id )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry ;
2010-07-04 22:14:27 +04:00
fill_deferred_open_entry ( & entry , request_time , id , pid , mid ) ;
2005-09-30 21:13:37 +04:00
add_share_mode_entry ( lck , & entry ) ;
1997-10-20 12:46:00 +04:00
}
1997-05-20 04:32:51 +04:00
2004-06-08 20:14:31 +04:00
/*******************************************************************
2005-09-30 21:13:37 +04:00
Check if two share mode entries are identical , ignoring oplock
2006-05-18 03:15:53 +04:00
and mid info and desired_access . ( Removed paranoia test - it ' s
not automatically a logic error if they are identical . JRA . )
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool share_modes_identical ( struct share_mode_entry * e1 ,
2005-09-30 21:13:37 +04:00
struct share_mode_entry * e2 )
2004-06-08 20:14:31 +04:00
{
2006-05-18 03:15:53 +04:00
/* We used to check for e1->share_access == e2->share_access here
as well as the other fields but 2 different DOS or FCB opens
sharing the same share mode entry may validly differ in
fsp - > share_access field . */
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
return ( procid_equal ( & e1 - > pid , & e2 - > pid ) & &
2007-05-29 13:30:34 +04:00
file_id_equal ( & e1 - > id , & e2 - > id ) & &
2005-09-30 21:13:37 +04:00
e1 - > share_file_id = = e2 - > share_file_id ) ;
2004-06-08 20:14:31 +04:00
}
2007-10-19 04:40:25 +04:00
static bool deferred_open_identical ( struct share_mode_entry * e1 ,
2005-09-30 21:13:37 +04:00
struct share_mode_entry * e2 )
{
return ( procid_equal ( & e1 - > pid , & e2 - > pid ) & &
( e1 - > op_mid = = e2 - > op_mid ) & &
2007-05-29 13:30:34 +04:00
file_id_equal ( & e1 - > id , & e2 - > id ) ) ;
2005-09-30 21:13:37 +04:00
}
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
static struct share_mode_entry * find_share_mode_entry ( struct share_mode_lock * lck ,
struct share_mode_entry * entry )
{
int i ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
for ( i = 0 ; i < lck - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & lck - > share_modes [ i ] ;
if ( is_valid_share_mode_entry ( entry ) & &
is_valid_share_mode_entry ( e ) & &
share_modes_identical ( e , entry ) ) {
return e ;
}
if ( is_deferred_open_entry ( entry ) & &
is_deferred_open_entry ( e ) & &
deferred_open_identical ( e , entry ) ) {
return e ;
}
}
return NULL ;
}
2004-06-08 20:14:31 +04:00
/*******************************************************************
2005-09-30 21:13:37 +04:00
Del the share mode of a file for this process . Return the number of
entries left .
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool del_share_mode ( struct share_mode_lock * lck , files_struct * fsp )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_entry entry , * e ;
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
e = find_share_mode_entry ( lck , & entry ) ;
if ( e = = NULL ) {
return False ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
e - > op_type = UNUSED_SHARE_MODE_ENTRY ;
lck - > modified = True ;
return True ;
2004-06-08 20:14:31 +04:00
}
2010-07-04 22:14:27 +04:00
void del_deferred_open_entry ( struct share_mode_lock * lck , uint64_t mid ,
struct server_id pid )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
fill_deferred_open_entry ( & entry , timeval_zero ( ) ,
2010-07-04 22:14:27 +04:00
lck - > id , pid , mid ) ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
e = find_share_mode_entry ( lck , & entry ) ;
if ( e = = NULL ) {
return ;
}
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
e - > op_type = UNUSED_SHARE_MODE_ENTRY ;
lck - > modified = True ;
2004-06-08 20:14:31 +04:00
}
/*******************************************************************
2005-09-30 21:13:37 +04:00
Remove an oplock mid and mode entry from a share mode .
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool remove_share_oplock ( struct share_mode_lock * lck , files_struct * fsp )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
e = find_share_mode_entry ( lck , & entry ) ;
if ( e = = NULL ) {
return False ;
}
2004-06-08 20:14:31 +04:00
2008-12-18 04:23:13 +03:00
if ( EXCLUSIVE_OPLOCK_TYPE ( fsp - > oplock_type ) ) {
/*
* Going from exclusive or batch ,
* we always go through FAKE_LEVEL_II
* first .
*/
e - > op_type = FAKE_LEVEL_II_OPLOCK ;
} else {
e - > op_type = NO_OPLOCK ;
}
2005-09-30 21:13:37 +04:00
lck - > modified = True ;
return True ;
}
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
/*******************************************************************
Downgrade a oplock type from exclusive to level II .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-06-08 20:14:31 +04:00
2007-10-19 04:40:25 +04:00
bool downgrade_share_oplock ( struct share_mode_lock * lck , files_struct * fsp )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
e = find_share_mode_entry ( lck , & entry ) ;
if ( e = = NULL ) {
return False ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
e - > op_type = LEVEL_II_OPLOCK ;
lck - > modified = True ;
return True ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
/****************************************************************************
2009-12-03 05:06:40 +03:00
Check if setting delete on close is allowed on this fsp .
2005-09-30 21:13:37 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-06-08 20:14:31 +04:00
2009-12-03 05:06:40 +03:00
NTSTATUS can_set_delete_on_close ( files_struct * fsp , uint32 dosmode )
2004-06-08 20:14:31 +04:00
{
/*
2005-09-30 21:13:37 +04:00
* Only allow delete on close for writable files .
2004-06-08 20:14:31 +04:00
*/
2005-09-30 21:13:37 +04:00
if ( ( dosmode & aRONLY ) & &
! lp_delete_readonly ( SNUM ( fsp - > conn ) ) ) {
DEBUG ( 10 , ( " can_set_delete_on_close: file %s delete on close "
" flag set but file attribute is readonly. \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2005-09-30 21:13:37 +04:00
return NT_STATUS_CANNOT_DELETE ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
/*
* Only allow delete on close for writable shares .
*/
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
if ( ! CAN_WRITE ( fsp - > conn ) ) {
DEBUG ( 10 , ( " can_set_delete_on_close: file %s delete on "
" close flag set but write access denied on share. \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2005-09-30 21:13:37 +04:00
return NT_STATUS_ACCESS_DENIED ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
/*
* Only allow delete on close for files / directories opened with delete
* intent .
*/
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
if ( ! ( fsp - > access_mask & DELETE_ACCESS ) ) {
DEBUG ( 10 , ( " can_set_delete_on_close: file %s delete on "
" close flag set but delete access denied. \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2005-09-30 21:13:37 +04:00
return NT_STATUS_ACCESS_DENIED ;
}
2004-06-08 20:14:31 +04:00
2007-01-18 09:19:24 +03:00
/* Don't allow delete on close for non-empty directories. */
if ( fsp - > is_directory ) {
2009-07-11 01:50:37 +04:00
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) ;
return can_delete_directory ( fsp - > conn ,
fsp - > fsp_name - > base_name ) ;
2007-01-18 09:19:24 +03:00
}
2005-09-30 21:13:37 +04:00
return NT_STATUS_OK ;
2004-06-08 20:14:31 +04:00
}
2006-02-02 23:44:50 +03:00
/*************************************************************************
2011-02-11 10:49:15 +03:00
Return a talloced copy of a struct security_unix_token . NULL on fail .
2006-02-02 23:44:50 +03:00
( Should this be in locking . c . . . . ? ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-02-11 10:49:15 +03:00
static struct security_unix_token * copy_unix_token ( TALLOC_CTX * ctx , const struct security_unix_token * tok )
2006-02-02 23:44:50 +03:00
{
2011-02-11 10:49:15 +03:00
struct security_unix_token * cpy ;
2006-02-02 23:44:50 +03:00
2011-02-11 10:49:15 +03:00
cpy = TALLOC_P ( ctx , struct security_unix_token ) ;
2006-02-02 23:44:50 +03:00
if ( ! cpy ) {
return NULL ;
}
cpy - > uid = tok - > uid ;
cpy - > gid = tok - > gid ;
cpy - > ngroups = tok - > ngroups ;
if ( tok - > ngroups ) {
/* Make this a talloc child of cpy. */
cpy - > groups = TALLOC_ARRAY ( cpy , gid_t , tok - > ngroups ) ;
if ( ! cpy - > groups ) {
return NULL ;
}
memcpy ( cpy - > groups , tok - > groups , tok - > ngroups * sizeof ( gid_t ) ) ;
}
return cpy ;
}
/****************************************************************************
2011-01-26 01:23:19 +03:00
Adds a delete on close token .
2006-02-02 23:44:50 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-01-26 01:23:19 +03:00
static bool add_delete_on_close_token ( struct share_mode_lock * lck ,
uint32_t name_hash ,
2011-02-11 10:49:15 +03:00
const struct security_unix_token * tok )
2006-02-02 23:44:50 +03:00
{
2011-01-26 01:23:19 +03:00
struct delete_token_list * dtl ;
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
dtl = TALLOC_ZERO_P ( lck , struct delete_token_list ) ;
if ( dtl = = NULL ) {
return false ;
}
dtl - > name_hash = name_hash ;
dtl - > delete_token = copy_unix_token ( lck , tok ) ;
if ( dtl - > delete_token = = NULL ) {
TALLOC_FREE ( dtl ) ;
return false ;
}
DLIST_ADD ( lck - > delete_tokens , dtl ) ;
lck - > modified = true ;
return true ;
2006-02-02 23:44:50 +03:00
}
2005-09-30 21:13:37 +04:00
/****************************************************************************
Sets the delete on close flag over all share modes on this file .
Modify the share mode entry for all files open
on this device and inode to tell other smbds we have
changed the delete on close flag . This will be noticed
in the close code , the last closer will delete the file
if flag is set .
2011-02-11 10:49:15 +03:00
This makes a copy of any struct security_unix_token into the
2007-01-19 00:51:52 +03:00
lck entry . This function is used when the lock is already granted .
2005-09-30 21:13:37 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-06-08 20:14:31 +04:00
2011-01-26 01:23:19 +03:00
void set_delete_on_close_lck ( files_struct * fsp ,
struct share_mode_lock * lck ,
bool delete_on_close ,
2011-02-11 10:49:15 +03:00
const struct security_unix_token * tok )
2007-01-19 00:51:52 +03:00
{
2011-01-26 01:23:19 +03:00
struct delete_token_list * dtl ;
bool ret ;
if ( delete_on_close ) {
SMB_ASSERT ( tok ! = NULL ) ;
} else {
SMB_ASSERT ( tok = = NULL ) ;
}
for ( dtl = lck - > delete_tokens ; dtl ; dtl = dtl - > next ) {
if ( dtl - > name_hash = = fsp - > name_hash ) {
lck - > modified = true ;
if ( delete_on_close = = false ) {
/* Delete this entry. */
DLIST_REMOVE ( lck - > delete_tokens , dtl ) ;
TALLOC_FREE ( dtl ) ;
return ;
}
/* Replace this token with the
given tok . */
TALLOC_FREE ( dtl - > delete_token ) ;
dtl - > delete_token = copy_unix_token ( dtl , tok ) ;
SMB_ASSERT ( dtl - > delete_token ! = NULL ) ;
2007-01-19 00:51:52 +03:00
}
}
2011-01-26 01:23:19 +03:00
if ( ! delete_on_close ) {
/* Nothing to delete - not found. */
return ;
}
ret = add_delete_on_close_token ( lck , fsp - > name_hash , tok ) ;
SMB_ASSERT ( ret ) ;
2007-01-19 00:51:52 +03:00
}
2011-02-11 10:49:15 +03:00
bool set_delete_on_close ( files_struct * fsp , bool delete_on_close , const struct security_unix_token * tok )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_lock * lck ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
DEBUG ( 10 , ( " set_delete_on_close: %s delete on close flag for "
" fnum = %d, file %s \n " ,
delete_on_close ? " Adding " : " Removing " , fsp - > fnum ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2004-06-08 20:14:31 +04:00
2008-03-12 17:32:47 +03:00
lck = get_share_mode_lock ( talloc_tos ( ) , fsp - > file_id , NULL , NULL ,
NULL ) ;
2005-09-30 21:13:37 +04:00
if ( lck = = NULL ) {
2004-06-08 20:14:31 +04:00
return False ;
}
2006-02-02 23:44:50 +03:00
2011-01-26 01:23:19 +03:00
set_delete_on_close_lck ( fsp , lck , delete_on_close ,
delete_on_close ? tok : NULL ) ;
2006-02-01 07:14:07 +03:00
2007-01-20 00:46:12 +03:00
if ( fsp - > is_directory ) {
2009-07-11 01:50:37 +04:00
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) ;
2010-07-07 00:43:14 +04:00
send_stat_cache_delete_message ( fsp - > conn - > sconn - > msg_ctx ,
fsp - > fsp_name - > base_name ) ;
2007-01-20 00:46:12 +03:00
}
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( lck ) ;
2010-01-13 03:04:44 +03:00
fsp - > delete_on_close = delete_on_close ;
2005-09-30 21:13:37 +04:00
return True ;
}
2001-08-21 05:25:45 +04:00
2011-02-11 10:49:15 +03:00
const struct security_unix_token * get_delete_on_close_token ( struct share_mode_lock * lck , uint32_t name_hash )
2011-01-26 01:23:19 +03:00
{
struct delete_token_list * dtl ;
2011-01-26 01:59:44 +03:00
DEBUG ( 10 , ( " get_delete_on_close_token: name_hash = 0x%x \n " ,
2011-01-26 01:23:19 +03:00
( unsigned int ) name_hash ) ) ;
for ( dtl = lck - > delete_tokens ; dtl ; dtl = dtl - > next ) {
2011-01-26 01:59:44 +03:00
DEBUG ( 10 , ( " get_delete_on_close_token: dtl->name_hash = 0x%x \n " ,
2011-01-26 01:23:19 +03:00
( unsigned int ) dtl - > name_hash ) ) ;
if ( dtl - > name_hash = = name_hash ) {
return dtl - > delete_token ;
}
}
return NULL ;
}
bool is_delete_on_close_set ( struct share_mode_lock * lck , uint32_t name_hash )
{
return ( get_delete_on_close_token ( lck , name_hash ) ! = NULL ) ;
}
2009-07-08 04:27:50 +04:00
bool set_sticky_write_time ( struct file_id fileid , struct timespec write_time )
2008-03-12 17:32:47 +03:00
{
struct share_mode_lock * lck ;
2008-09-06 06:00:48 +04:00
DEBUG ( 5 , ( " set_sticky_write_time: %s id=%s \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t ( write_time ) ) ,
2008-09-06 06:00:48 +04:00
file_id_string_tos ( & fileid ) ) ) ;
2008-03-12 17:32:47 +03:00
lck = get_share_mode_lock ( NULL , fileid , NULL , NULL , NULL ) ;
if ( lck = = NULL ) {
return False ;
}
2008-09-06 06:00:48 +04:00
if ( timespec_compare ( & lck - > changed_write_time , & write_time ) ! = 0 ) {
2008-03-12 17:32:47 +03:00
lck - > modified = True ;
lck - > changed_write_time = write_time ;
}
TALLOC_FREE ( lck ) ;
return True ;
}
2008-09-06 06:00:48 +04:00
bool set_write_time ( struct file_id fileid , struct timespec write_time )
{
struct share_mode_lock * lck ;
2008-09-06 07:37:00 +04:00
DEBUG ( 5 , ( " set_write_time: %s id=%s \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-09-06 06:00:48 +04:00
convert_timespec_to_time_t ( write_time ) ) ,
file_id_string_tos ( & fileid ) ) ) ;
lck = get_share_mode_lock ( NULL , fileid , NULL , NULL , NULL ) ;
if ( lck = = NULL ) {
return False ;
}
if ( timespec_compare ( & lck - > old_write_time , & write_time ) ! = 0 ) {
lck - > modified = True ;
lck - > old_write_time = write_time ;
}
TALLOC_FREE ( lck ) ;
return True ;
}
2006-07-21 18:13:30 +04:00
struct forall_state {
void ( * fn ) ( const struct share_mode_entry * entry ,
const char * sharepath ,
const char * fname ,
void * private_data ) ;
void * private_data ;
} ;
2007-05-27 13:57:05 +04:00
static int traverse_fn ( struct db_record * rec , void * _state )
1997-05-20 04:32:51 +04:00
{
2006-07-21 18:13:30 +04:00
struct forall_state * state = ( struct forall_state * ) _state ;
1999-12-21 12:25:59 +03:00
struct locking_data * data ;
2005-09-30 21:13:37 +04:00
struct share_mode_entry * shares ;
2005-12-13 01:07:36 +03:00
const char * sharepath ;
const char * fname ;
2011-01-26 01:23:19 +03:00
const char * del_tokens ;
uint32_t total_del_token_size = 0 ;
1999-12-21 12:25:59 +03:00
int i ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
2004-06-08 20:14:31 +04:00
/* Ensure this is a locking_key record. */
2007-05-29 13:30:34 +04:00
if ( rec - > key . dsize ! = sizeof ( struct file_id ) )
2004-06-08 20:14:31 +04:00
return 0 ;
2007-05-27 13:57:05 +04:00
data = ( struct locking_data * ) rec - > value . dptr ;
shares = ( struct share_mode_entry * ) ( rec - > value . dptr + sizeof ( * data ) ) ;
2011-01-26 01:23:19 +03:00
del_tokens = ( const char * ) rec - > value . dptr + sizeof ( * data ) +
data - > u . s . num_share_mode_entries * sizeof ( * shares ) ;
for ( i = 0 ; i < data - > u . s . num_delete_token_entries ; i + + ) {
uint32_t del_token_size ;
memcpy ( & del_token_size , del_tokens , sizeof ( uint32_t ) ) ;
total_del_token_size + = del_token_size ;
del_tokens + = del_token_size ;
}
2007-05-27 13:57:05 +04:00
sharepath = ( const char * ) rec - > value . dptr + sizeof ( * data ) +
2006-02-02 23:44:50 +03:00
data - > u . s . num_share_mode_entries * sizeof ( * shares ) +
2011-01-26 01:23:19 +03:00
total_del_token_size ;
2007-05-27 13:57:05 +04:00
fname = ( const char * ) rec - > value . dptr + sizeof ( * data ) +
2005-12-13 01:07:36 +03:00
data - > u . s . num_share_mode_entries * sizeof ( * shares ) +
2011-01-26 01:23:19 +03:00
total_del_token_size +
2005-12-13 01:07:36 +03:00
strlen ( sharepath ) + 1 ;
1999-12-21 12:25:59 +03:00
2005-07-08 08:51:27 +04:00
for ( i = 0 ; i < data - > u . s . num_share_mode_entries ; i + + ) {
2006-07-21 18:13:30 +04:00
state - > fn ( & shares [ i ] , sharepath , fname ,
state - > private_data ) ;
1999-12-21 12:25:59 +03:00
}
return 0 ;
1996-05-04 11:50:46 +04:00
}
1997-10-02 03:32:22 +04:00
/*******************************************************************
1999-12-21 12:25:59 +03:00
Call the specified function on each entry under management by the
share mode system .
1997-10-02 03:32:22 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-08-21 05:25:45 +04:00
2006-07-21 18:13:30 +04:00
int share_mode_forall ( void ( * fn ) ( const struct share_mode_entry * , const char * ,
const char * , void * ) ,
void * private_data )
1997-10-02 03:32:22 +04:00
{
2006-07-21 18:13:30 +04:00
struct forall_state state ;
2007-05-27 13:57:05 +04:00
if ( lock_db = = NULL )
2001-08-21 05:25:45 +04:00
return 0 ;
2006-07-21 18:13:30 +04:00
state . fn = fn ;
state . private_data = private_data ;
2007-05-29 22:32:49 +04:00
return lock_db - > traverse_read ( lock_db , traverse_fn , ( void * ) & state ) ;
1997-10-02 03:32:22 +04:00
}