2008-11-11 04:57:22 +03:00
/*
2008-11-19 23:24:53 +03:00
* Store Windows ACLs in a tdb .
2008-11-11 04:57:22 +03:00
*
* Copyright ( C ) Volker Lendecke , 2008
* Copyright ( C ) Jeremy Allison , 2008
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
*/
/* NOTE: This is an experimental module, not yet finished. JRA. */
# include "includes.h"
# include "librpc/gen_ndr/xattr.h"
# include "librpc/gen_ndr/ndr_xattr.h"
2009-07-25 01:47:52 +04:00
# include "../lib/crypto/crypto.h"
2008-11-11 04:57:22 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_VFS
2009-07-27 23:09:40 +04:00
# include "modules/vfs_acl_common.c"
2008-11-11 04:57:22 +03:00
static unsigned int ref_count ;
static struct db_context * acl_db ;
/*******************************************************************
Open acl_db if not already open , increment ref count .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static bool acl_tdb_init ( struct db_context * * pp_db )
{
2009-01-16 08:59:20 +03:00
char * dbname ;
2008-11-11 04:57:22 +03:00
if ( acl_db ) {
* pp_db = acl_db ;
ref_count + + ;
return true ;
}
2009-01-12 09:56:48 +03:00
dbname = state_path ( " file_ntacls.tdb " ) ;
2008-11-11 04:57:22 +03:00
if ( dbname = = NULL ) {
errno = ENOSYS ;
return false ;
}
become_root ( ) ;
* pp_db = db_open ( NULL , dbname , 0 , TDB_DEFAULT , O_RDWR | O_CREAT , 0600 ) ;
unbecome_root ( ) ;
if ( * pp_db = = NULL ) {
# if defined(ENOTSUP)
errno = ENOTSUP ;
# else
errno = ENOSYS ;
# endif
2009-01-16 01:31:17 +03:00
TALLOC_FREE ( dbname ) ;
2008-11-11 04:57:22 +03:00
return false ;
}
ref_count + + ;
2009-01-16 01:31:17 +03:00
TALLOC_FREE ( dbname ) ;
2008-11-11 04:57:22 +03:00
return true ;
}
/*******************************************************************
Lower ref count and close acl_db if zero .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-19 23:24:53 +03:00
static void free_acl_tdb_data ( void * * pptr )
2008-11-11 04:57:22 +03:00
{
struct db_context * * pp_db = ( struct db_context * * ) pptr ;
ref_count - - ;
if ( ref_count = = 0 ) {
TALLOC_FREE ( * pp_db ) ;
acl_db = NULL ;
}
}
/*******************************************************************
Fetch_lock the tdb acl record for a file
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-19 23:24:53 +03:00
static struct db_record * acl_tdb_lock ( TALLOC_CTX * mem_ctx ,
2008-11-11 04:57:22 +03:00
struct db_context * db ,
const struct file_id * id )
{
uint8 id_buf [ 16 ] ;
2009-02-16 10:45:28 +03:00
/* For backwards compatibility only store the dev/inode. */
2008-11-11 04:57:22 +03:00
push_file_id_16 ( ( char * ) id_buf , id ) ;
return db - > fetch_locked ( db ,
mem_ctx ,
make_tdb_data ( id_buf ,
sizeof ( id_buf ) ) ) ;
}
2008-11-19 23:24:53 +03:00
/*******************************************************************
Delete the tdb acl record for a file
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static NTSTATUS acl_tdb_delete ( vfs_handle_struct * handle ,
struct db_context * db ,
SMB_STRUCT_STAT * psbuf )
{
NTSTATUS status ;
struct file_id id = vfs_file_id_from_sbuf ( handle - > conn , psbuf ) ;
struct db_record * rec = acl_tdb_lock ( talloc_tos ( ) , db , & id ) ;
/*
* If rec = = NULL there ' s not much we can do about it
*/
if ( rec = = NULL ) {
DEBUG ( 10 , ( " acl_tdb_delete: rec == NULL \n " ) ) ;
TALLOC_FREE ( rec ) ;
return NT_STATUS_OK ;
}
status = rec - > delete_rec ( rec ) ;
TALLOC_FREE ( rec ) ;
return status ;
}
2008-11-11 04:57:22 +03:00
/*******************************************************************
Pull a security descriptor into a DATA_BLOB from a tdb store .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-27 23:09:40 +04:00
static NTSTATUS get_acl_blob ( TALLOC_CTX * ctx ,
2008-11-11 04:57:22 +03:00
vfs_handle_struct * handle ,
files_struct * fsp ,
const char * name ,
DATA_BLOB * pblob )
{
uint8 id_buf [ 16 ] ;
TDB_DATA data ;
struct file_id id ;
struct db_context * db ;
2010-02-18 04:13:07 +03:00
NTSTATUS status = NT_STATUS_OK ;
2008-11-11 04:57:22 +03:00
SMB_STRUCT_STAT sbuf ;
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context ,
return NT_STATUS_INTERNAL_DB_CORRUPTION ) ;
2009-10-03 00:45:38 +04:00
ZERO_STRUCT ( sbuf ) ;
if ( fsp ) {
status = vfs_stat_fsp ( fsp ) ;
sbuf = fsp - > fsp_name - > st ;
2008-11-11 04:57:22 +03:00
} else {
2009-10-03 00:45:38 +04:00
int ret = vfs_stat_smb_fname ( handle - > conn , name , & sbuf ) ;
if ( ret = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
2008-11-11 04:57:22 +03:00
}
}
2009-02-26 01:12:51 +03:00
2009-10-03 00:45:38 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
2009-02-26 01:12:51 +03:00
}
2008-11-11 04:57:22 +03:00
id = vfs_file_id_from_sbuf ( handle - > conn , & sbuf ) ;
2009-02-16 10:45:28 +03:00
/* For backwards compatibility only store the dev/inode. */
2008-11-11 04:57:22 +03:00
push_file_id_16 ( ( char * ) id_buf , & id ) ;
if ( db - > fetch ( db ,
ctx ,
make_tdb_data ( id_buf , sizeof ( id_buf ) ) ,
& data ) = = - 1 ) {
return NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
pblob - > data = data . dptr ;
pblob - > length = data . dsize ;
DEBUG ( 10 , ( " get_acl_blob: returned %u bytes from file %s \n " ,
( unsigned int ) data . dsize , name ) ) ;
if ( pblob - > length = = 0 | | pblob - > data = = NULL ) {
2009-12-02 23:09:48 +03:00
return NT_STATUS_NOT_FOUND ;
2008-11-11 04:57:22 +03:00
}
return NT_STATUS_OK ;
}
/*******************************************************************
Store a DATA_BLOB into a tdb record given an fsp pointer .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-27 23:09:40 +04:00
static NTSTATUS store_acl_blob_fsp ( vfs_handle_struct * handle ,
2008-11-11 04:57:22 +03:00
files_struct * fsp ,
DATA_BLOB * pblob )
{
uint8 id_buf [ 16 ] ;
struct file_id id ;
TDB_DATA data ;
struct db_context * db ;
struct db_record * rec ;
2009-10-03 00:45:38 +04:00
NTSTATUS status ;
2008-11-11 04:57:22 +03:00
DEBUG ( 10 , ( " store_acl_blob_fsp: storing blob length %u on file %s \n " ,
2009-07-11 05:11:32 +04:00
( unsigned int ) pblob - > length , fsp_str_dbg ( fsp ) ) ) ;
2008-11-11 04:57:22 +03:00
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context ,
return NT_STATUS_INTERNAL_DB_CORRUPTION ) ;
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:12:51 +03:00
}
2009-07-11 05:11:32 +04:00
id = vfs_file_id_from_sbuf ( handle - > conn , & fsp - > fsp_name - > st ) ;
2008-11-11 04:57:22 +03:00
2009-02-16 10:45:28 +03:00
/* For backwards compatibility only store the dev/inode. */
2008-11-11 04:57:22 +03:00
push_file_id_16 ( ( char * ) id_buf , & id ) ;
rec = db - > fetch_locked ( db , talloc_tos ( ) ,
make_tdb_data ( id_buf ,
sizeof ( id_buf ) ) ) ;
if ( rec = = NULL ) {
DEBUG ( 0 , ( " store_acl_blob_fsp_tdb: fetch_lock failed \n " ) ) ;
return NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
data . dptr = pblob - > data ;
data . dsize = pblob - > length ;
return rec - > store ( rec , data , 0 ) ;
}
/*********************************************************************
On unlink we need to delete the tdb record ( if using tdb ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-02 20:27:44 +04:00
static int unlink_acl_tdb ( vfs_handle_struct * handle ,
const struct smb_filename * smb_fname )
2008-11-11 04:57:22 +03:00
{
2009-07-02 20:27:44 +04:00
struct smb_filename * smb_fname_tmp = NULL ;
2008-11-11 04:57:22 +03:00
struct db_context * db ;
2009-07-02 20:27:44 +04:00
NTSTATUS status ;
2009-02-26 01:12:51 +03:00
int ret = - 1 ;
2008-11-11 04:57:22 +03:00
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context , return - 1 ) ;
2009-07-02 20:27:44 +04:00
status = copy_smb_filename ( talloc_tos ( ) , smb_fname , & smb_fname_tmp ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
errno = map_errno_from_nt_status ( status ) ;
goto out ;
}
2009-02-26 01:12:51 +03:00
if ( lp_posix_pathnames ( ) ) {
2009-07-02 20:27:44 +04:00
ret = SMB_VFS_LSTAT ( handle - > conn , smb_fname_tmp ) ;
2009-02-26 01:12:51 +03:00
} else {
2009-07-02 20:27:44 +04:00
ret = SMB_VFS_STAT ( handle - > conn , smb_fname_tmp ) ;
2009-02-26 01:12:51 +03:00
}
if ( ret = = - 1 ) {
2009-07-02 20:27:44 +04:00
goto out ;
2008-11-11 04:57:22 +03:00
}
2010-01-13 03:04:44 +03:00
ret = unlink_acl_common ( handle , smb_fname_tmp ) ;
2008-11-11 04:57:22 +03:00
if ( ret = = - 1 ) {
2009-07-02 20:27:44 +04:00
goto out ;
2008-11-11 04:57:22 +03:00
}
2009-07-02 20:27:44 +04:00
acl_tdb_delete ( handle , db , & smb_fname_tmp - > st ) ;
out :
return ret ;
2008-11-11 04:57:22 +03:00
}
/*********************************************************************
On rmdir we need to delete the tdb record ( if using tdb ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-19 23:24:53 +03:00
static int rmdir_acl_tdb ( vfs_handle_struct * handle , const char * path )
2008-11-11 04:57:22 +03:00
{
2008-11-19 23:24:53 +03:00
2008-11-11 04:57:22 +03:00
SMB_STRUCT_STAT sbuf ;
struct db_context * db ;
2009-02-26 01:12:51 +03:00
int ret = - 1 ;
2008-11-11 04:57:22 +03:00
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context , return - 1 ) ;
2009-02-26 01:12:51 +03:00
if ( lp_posix_pathnames ( ) ) {
2009-06-23 02:26:56 +04:00
ret = vfs_lstat_smb_fname ( handle - > conn , path , & sbuf ) ;
2009-02-26 01:12:51 +03:00
} else {
2009-06-23 02:26:56 +04:00
ret = vfs_stat_smb_fname ( handle - > conn , path , & sbuf ) ;
2009-02-26 01:12:51 +03:00
}
if ( ret = = - 1 ) {
2008-11-11 04:57:22 +03:00
return - 1 ;
}
2010-02-08 22:04:38 +03:00
ret = rmdir_acl_common ( handle , path ) ;
2008-11-11 04:57:22 +03:00
if ( ret = = - 1 ) {
return - 1 ;
}
2008-11-19 23:24:53 +03:00
acl_tdb_delete ( handle , db , & sbuf ) ;
2008-11-11 04:57:22 +03:00
return 0 ;
}
/*******************************************************************
Handle opening the storage tdb if so configured .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-19 23:24:53 +03:00
static int connect_acl_tdb ( struct vfs_handle_struct * handle ,
2008-11-11 04:57:22 +03:00
const char * service ,
const char * user )
{
struct db_context * db ;
2009-12-01 02:53:04 +03:00
int ret = SMB_VFS_NEXT_CONNECT ( handle , service , user ) ;
2008-11-11 04:57:22 +03:00
2009-12-01 02:53:04 +03:00
if ( ret < 0 ) {
return ret ;
}
2008-11-11 04:57:22 +03:00
if ( ! acl_tdb_init ( & db ) ) {
SMB_VFS_NEXT_DISCONNECT ( handle ) ;
return - 1 ;
}
2008-11-19 23:24:53 +03:00
SMB_VFS_HANDLE_SET_DATA ( handle , db , free_acl_tdb_data ,
2008-11-11 04:57:22 +03:00
struct db_context , return - 1 ) ;
2009-11-07 08:53:07 +03:00
/* Ensure we have "inherit acls = yes" if we're
* using this module . */
2009-11-12 05:35:18 +03:00
DEBUG ( 2 , ( " connect_acl_tdb: setting 'inherit acls = true' "
" and 'dos filemode = true' for service %s \n " ,
2009-11-07 08:53:07 +03:00
service ) ) ;
2009-11-12 05:35:18 +03:00
lp_do_parameter ( SNUM ( handle - > conn ) , " inherit acls " , " true " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " dos filemode " , " true " ) ;
2009-11-07 08:53:07 +03:00
2008-11-11 04:57:22 +03:00
return 0 ;
}
2008-11-19 23:24:53 +03:00
/*********************************************************************
Remove a Windows ACL - we ' re setting the underlying POSIX ACL .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int sys_acl_set_file_tdb ( vfs_handle_struct * handle ,
const char * path ,
SMB_ACL_TYPE_T type ,
SMB_ACL_T theacl )
{
SMB_STRUCT_STAT sbuf ;
struct db_context * db ;
2009-02-26 01:12:51 +03:00
int ret = - 1 ;
2008-11-19 23:24:53 +03:00
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context , return - 1 ) ;
2009-02-26 01:12:51 +03:00
if ( lp_posix_pathnames ( ) ) {
2009-06-23 02:26:56 +04:00
ret = vfs_lstat_smb_fname ( handle - > conn , path , & sbuf ) ;
2009-02-26 01:12:51 +03:00
} else {
2009-06-23 02:26:56 +04:00
ret = vfs_stat_smb_fname ( handle - > conn , path , & sbuf ) ;
2009-02-26 01:12:51 +03:00
}
if ( ret = = - 1 ) {
2008-11-19 23:24:53 +03:00
return - 1 ;
}
ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE ( handle ,
path ,
type ,
theacl ) ;
if ( ret = = - 1 ) {
return - 1 ;
}
acl_tdb_delete ( handle , db , & sbuf ) ;
return 0 ;
}
/*********************************************************************
Remove a Windows ACL - we ' re setting the underlying POSIX ACL .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int sys_acl_set_fd_tdb ( vfs_handle_struct * handle ,
files_struct * fsp ,
SMB_ACL_T theacl )
{
struct db_context * db ;
2009-10-03 00:45:38 +04:00
NTSTATUS status ;
2008-11-19 23:24:53 +03:00
int ret ;
SMB_VFS_HANDLE_GET_DATA ( handle , db , struct db_context , return - 1 ) ;
2009-10-03 00:45:38 +04:00
status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-11-19 23:24:53 +03:00
return - 1 ;
}
ret = SMB_VFS_NEXT_SYS_ACL_SET_FD ( handle ,
fsp ,
theacl ) ;
if ( ret = = - 1 ) {
return - 1 ;
}
2009-07-11 05:11:32 +04:00
acl_tdb_delete ( handle , db , & fsp - > fsp_name - > st ) ;
2008-11-19 23:24:53 +03:00
return 0 ;
}
2009-07-24 04:28:58 +04:00
static struct vfs_fn_pointers vfs_acl_tdb_fns = {
. connect_fn = connect_acl_tdb ,
2009-12-01 03:50:34 +03:00
. opendir = opendir_acl_common ,
2009-07-25 04:06:41 +04:00
. mkdir = mkdir_acl_common ,
. open = open_acl_common ,
2009-12-08 01:36:12 +03:00
. create_file = create_file_acl_common ,
2009-07-24 04:28:58 +04:00
. unlink = unlink_acl_tdb ,
. rmdir = rmdir_acl_tdb ,
2009-07-25 04:06:41 +04:00
. fget_nt_acl = fget_nt_acl_common ,
. get_nt_acl = get_nt_acl_common ,
. fset_nt_acl = fset_nt_acl_common ,
2009-07-24 04:28:58 +04:00
. sys_acl_set_file = sys_acl_set_file_tdb ,
. sys_acl_set_fd = sys_acl_set_fd_tdb
2008-11-11 04:57:22 +03:00
} ;
2008-11-19 23:24:53 +03:00
NTSTATUS vfs_acl_tdb_init ( void )
2008-11-11 04:57:22 +03:00
{
2009-07-24 04:28:58 +04:00
return smb_register_vfs ( SMB_VFS_INTERFACE_VERSION , " acl_tdb " ,
& vfs_acl_tdb_fns ) ;
2008-11-11 04:57:22 +03:00
}