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/>.
*/
# include "includes.h"
2011-03-23 00:34:22 +03:00
# include "smbd/smbd.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2008-11-11 04:57:22 +03:00
# include "librpc/gen_ndr/xattr.h"
2011-07-07 19:42:08 +04:00
# include "dbwrap/dbwrap.h"
2011-07-06 18:40:21 +04:00
# include "dbwrap/dbwrap_open.h"
2011-03-24 16:15:54 +03:00
# include "auth.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2017-08-18 15:41:57 +03:00
# include "vfs_acl_common.h"
2008-11-11 04:57:22 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_VFS
2010-10-16 02:56:09 +04:00
# define ACL_MODULE_NAME "acl_tdb"
2009-07-27 23:09:40 +04:00
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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-03-24 20:31:43 +03:00
static bool acl_tdb_init ( void )
2008-11-11 04:57:22 +03:00
{
2009-01-16 08:59:20 +03:00
char * dbname ;
2008-11-11 04:57:22 +03:00
if ( acl_db ) {
ref_count + + ;
return true ;
}
2018-08-16 11:51:44 +03:00
dbname = state_path ( talloc_tos ( ) , " file_ntacls.tdb " ) ;
2008-11-11 04:57:22 +03:00
if ( dbname = = NULL ) {
errno = ENOSYS ;
return false ;
}
become_root ( ) ;
2012-01-06 20:19:54 +04:00
acl_db = db_open ( NULL , dbname , 0 , TDB_DEFAULT , O_RDWR | O_CREAT , 0600 ,
2014-01-27 17:49:12 +04:00
DBWRAP_LOCK_ORDER_1 , DBWRAP_FLAG_NONE ) ;
2008-11-11 04:57:22 +03:00
unbecome_root ( ) ;
2010-03-24 20:31:43 +03:00
if ( acl_db = = NULL ) {
2008-11-11 04:57:22 +03:00
# 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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-03-24 20:31:43 +03:00
static void disconnect_acl_tdb ( struct vfs_handle_struct * handle )
2008-11-11 04:57:22 +03:00
{
2010-03-24 20:31:43 +03:00
SMB_VFS_NEXT_DISCONNECT ( handle ) ;
2008-11-11 04:57:22 +03:00
ref_count - - ;
if ( ref_count = = 0 ) {
2010-03-24 20:31:43 +03:00
TALLOC_FREE ( acl_db ) ;
2008-11-11 04:57:22 +03:00
}
}
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 ) ;
2019-11-13 18:02:11 +03:00
uint8_t id_buf [ 16 ] ;
2008-11-19 23:24:53 +03:00
2019-11-13 18:02:11 +03:00
/* For backwards compatibility only store the dev/inode. */
push_file_id_16 ( ( char * ) id_buf , & id ) ;
2008-11-19 23:24:53 +03:00
2019-11-13 18:02:11 +03:00
status = dbwrap_delete ( db , make_tdb_data ( id_buf , sizeof ( id_buf ) ) ) ;
2008-11-19 23:24:53 +03:00
return status ;
}
2020-04-13 22:19:03 +03:00
/*******************************************************************
Pull a security descriptor from an fsp into a DATA_BLOB from a tdb store .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static NTSTATUS fget_acl_blob ( TALLOC_CTX * ctx ,
vfs_handle_struct * handle ,
files_struct * fsp ,
DATA_BLOB * pblob )
{
uint8_t id_buf [ 16 ] ;
TDB_DATA data ;
struct file_id id ;
struct db_context * db = acl_db ;
NTSTATUS status = NT_STATUS_OK ;
status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
id = vfs_file_id_from_sbuf ( handle - > conn , & fsp - > fsp_name - > st ) ;
/* For backwards compatibility only store the dev/inode. */
push_file_id_16 ( ( char * ) id_buf , & id ) ;
status = dbwrap_fetch ( db ,
ctx ,
make_tdb_data ( id_buf , sizeof ( id_buf ) ) ,
& data ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
pblob - > data = data . dptr ;
pblob - > length = data . dsize ;
DBG_DEBUG ( " returned %u bytes from file %s \n " ,
( unsigned int ) data . dsize ,
fsp_str_dbg ( fsp ) ) ;
if ( pblob - > length = = 0 | | pblob - > data = = NULL ) {
return NT_STATUS_NOT_FOUND ;
}
return NT_STATUS_OK ;
}
2008-11-11 04:57:22 +03:00
/*******************************************************************
Pull a security descriptor into a DATA_BLOB from a tdb store .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-04-13 23:29:48 +03:00
static NTSTATUS get_acl_blob_at ( TALLOC_CTX * ctx ,
2008-11-11 04:57:22 +03:00
vfs_handle_struct * handle ,
2020-04-13 23:29:48 +03:00
struct files_struct * dirfsp ,
2016-03-12 02:50:57 +03:00
const struct smb_filename * smb_fname ,
2008-11-11 04:57:22 +03:00
DATA_BLOB * pblob )
{
2015-05-03 06:11:02 +03:00
uint8_t id_buf [ 16 ] ;
2008-11-11 04:57:22 +03:00
TDB_DATA data ;
struct file_id id ;
2010-03-24 20:31:43 +03:00
struct db_context * db = acl_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 ;
2020-04-13 22:53:58 +03:00
int ret ;
2008-11-11 04:57:22 +03:00
2009-10-03 00:45:38 +04:00
ZERO_STRUCT ( sbuf ) ;
2020-04-13 22:53:58 +03:00
ret = vfs_stat_smb_basename ( handle - > conn ,
2016-03-19 08:17:30 +03:00
smb_fname ,
2016-03-12 02:50:57 +03:00
& sbuf ) ;
2020-04-13 22:53:58 +03:00
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 ) ;
2011-08-24 17:40:09 +04:00
status = dbwrap_fetch ( db ,
ctx ,
make_tdb_data ( id_buf , sizeof ( id_buf ) ) ,
& data ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-11-11 04:57:22 +03:00
return NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
pblob - > data = data . dptr ;
pblob - > length = data . dsize ;
2020-04-13 22:53:58 +03:00
DBG_DEBUG ( " returned %u bytes from file %s \n " ,
( unsigned int ) data . dsize , smb_fname - > base_name ) ;
2008-11-11 04:57:22 +03:00
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 )
{
2015-05-03 06:11:02 +03:00
uint8_t id_buf [ 16 ] ;
2008-11-11 04:57:22 +03:00
struct file_id id ;
2019-11-13 17:59:54 +03:00
TDB_DATA data = { . dptr = pblob - > data , . dsize = pblob - > length } ;
2010-03-24 20:31:43 +03:00
struct db_context * db = acl_db ;
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
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 ) ;
2019-11-13 18:02:11 +03:00
status = dbwrap_store (
db , make_tdb_data ( id_buf , sizeof ( id_buf ) ) , data , 0 ) ;
return status ;
2008-11-11 04:57:22 +03:00
}
2019-09-12 02:42:01 +03:00
/*********************************************************************
On unlinkat we need to delete the tdb record ( if using tdb ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int unlinkat_acl_tdb ( vfs_handle_struct * handle ,
struct files_struct * dirfsp ,
const struct smb_filename * smb_fname ,
int flags )
{
struct smb_filename * smb_fname_tmp = NULL ;
struct db_context * db = acl_db ;
int ret = - 1 ;
smb_fname_tmp = cp_smb_filename_nostream ( talloc_tos ( ) , smb_fname ) ;
if ( smb_fname_tmp = = NULL ) {
errno = ENOMEM ;
goto out ;
}
2020-11-25 14:29:40 +03:00
ret = vfs_stat ( handle - > conn , smb_fname_tmp ) ;
2019-09-12 02:42:01 +03:00
if ( ret = = - 1 ) {
goto out ;
}
if ( flags & AT_REMOVEDIR ) {
2019-10-05 00:34:41 +03:00
ret = rmdir_acl_common ( handle ,
dirfsp ,
smb_fname_tmp ) ;
2019-09-12 02:42:01 +03:00
} else {
2019-09-17 03:50:17 +03:00
ret = unlink_acl_common ( handle ,
dirfsp ,
smb_fname_tmp ,
flags ) ;
2019-09-12 02:42:01 +03:00
}
if ( ret = = - 1 ) {
goto out ;
}
acl_tdb_delete ( handle , db , & smb_fname_tmp - > st ) ;
out :
return ret ;
}
2008-11-11 04:57:22 +03:00
/*******************************************************************
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 )
{
2009-12-01 02:53:04 +03:00
int ret = SMB_VFS_NEXT_CONNECT ( handle , service , user ) ;
2016-08-24 11:01:17 +03:00
bool ok ;
2016-08-26 11:04:53 +03:00
struct acl_common_config * config = NULL ;
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
2010-03-24 20:31:43 +03:00
if ( ! acl_tdb_init ( ) ) {
2008-11-11 04:57:22 +03:00
SMB_VFS_NEXT_DISCONNECT ( handle ) ;
return - 1 ;
}
2017-08-18 15:41:57 +03:00
ok = init_acl_common_config ( handle , ACL_MODULE_NAME ) ;
2016-08-24 11:01:17 +03:00
if ( ! ok ) {
DBG_ERR ( " init_acl_common_config failed \n " ) ;
return - 1 ;
}
2010-10-16 06:54:51 +04:00
/* Ensure we have the parameters correct if we're
2009-11-07 08:53:07 +03:00
* using this module . */
2009-11-12 05:35:18 +03:00
DEBUG ( 2 , ( " connect_acl_tdb: setting 'inherit acls = true' "
2010-10-16 06:54:51 +04:00
" 'dos filemode = true' and "
" 'force unknown acl user = true' for service %s \n " ,
2009-11-07 08:53:07 +03:00
service ) ) ;
2010-10-16 06:54:51 +04:00
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 " ) ;
2010-10-16 06:54:51 +04:00
lp_do_parameter ( SNUM ( handle - > conn ) , " force unknown acl user " , " true " ) ;
2009-11-07 08:53:07 +03:00
2016-08-26 11:04:53 +03:00
SMB_VFS_HANDLE_GET_DATA ( handle , config ,
struct acl_common_config ,
return - 1 ) ;
if ( config - > ignore_system_acls ) {
2017-02-06 14:47:41 +03:00
mode_t create_mask = lp_create_mask ( SNUM ( handle - > conn ) ) ;
char * create_mask_str = NULL ;
if ( ( create_mask & 0666 ) ! = 0666 ) {
create_mask | = 0666 ;
create_mask_str = talloc_asprintf ( handle , " 0%o " ,
create_mask ) ;
if ( create_mask_str = = NULL ) {
DBG_ERR ( " talloc_asprintf failed \n " ) ;
return - 1 ;
}
DBG_NOTICE ( " setting 'create mask = %s' \n " , create_mask_str ) ;
lp_do_parameter ( SNUM ( handle - > conn ) ,
" create mask " , create_mask_str ) ;
TALLOC_FREE ( create_mask_str ) ;
}
DBG_NOTICE ( " setting 'directory mask = 0777', "
2016-08-26 11:04:53 +03:00
" 'store dos attributes = yes' and all "
" 'map ...' options to 'no' \n " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " directory mask " , " 0777 " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " map archive " , " no " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " map hidden " , " no " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " map readonly " , " no " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " map system " , " no " ) ;
lp_do_parameter ( SNUM ( handle - > conn ) , " store dos attributes " ,
" yes " ) ;
}
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_fd_tdb ( vfs_handle_struct * handle ,
files_struct * fsp ,
2020-12-14 18:28:26 +03:00
SMB_ACL_TYPE_T type ,
2008-11-19 23:24:53 +03:00
SMB_ACL_T theacl )
{
2020-12-16 17:41:07 +03:00
struct acl_common_fsp_ext * ext = ( struct acl_common_fsp_ext * )
VFS_FETCH_FSP_EXTENSION ( handle , fsp ) ;
2010-03-24 20:31:43 +03:00
struct db_context * db = acl_db ;
2009-10-03 00:45:38 +04:00
NTSTATUS status ;
2008-11-19 23:24:53 +03:00
int ret ;
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 ,
2020-12-14 18:28:26 +03:00
fsp ,
type ,
theacl ) ;
2008-11-19 23:24:53 +03:00
if ( ret = = - 1 ) {
return - 1 ;
}
2020-12-16 17:41:07 +03:00
if ( ext ! = NULL & & ext - > setting_nt_acl ) {
return 0 ;
}
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 ;
}
2017-08-18 15:41:57 +03:00
static NTSTATUS acl_tdb_fget_nt_acl ( vfs_handle_struct * handle ,
files_struct * fsp ,
uint32_t security_info ,
TALLOC_CTX * mem_ctx ,
struct security_descriptor * * ppdesc )
{
NTSTATUS status ;
2020-04-13 22:41:31 +03:00
status = fget_nt_acl_common ( fget_acl_blob , handle , fsp ,
2017-08-18 15:41:57 +03:00
security_info , mem_ctx , ppdesc ) ;
return status ;
}
2020-04-13 23:33:44 +03:00
static NTSTATUS acl_tdb_get_nt_acl_at ( vfs_handle_struct * handle ,
struct files_struct * dirfsp ,
const struct smb_filename * smb_fname ,
uint32_t security_info ,
TALLOC_CTX * mem_ctx ,
struct security_descriptor * * ppdesc )
{
NTSTATUS status ;
status = get_nt_acl_common_at ( get_acl_blob_at ,
handle ,
dirfsp ,
smb_fname ,
security_info ,
mem_ctx ,
ppdesc ) ;
return status ;
}
2017-08-18 15:41:57 +03:00
static NTSTATUS acl_tdb_fset_nt_acl ( vfs_handle_struct * handle ,
files_struct * fsp ,
uint32_t security_info_sent ,
const struct security_descriptor * psd )
{
NTSTATUS status ;
2020-04-13 22:29:22 +03:00
status = fset_nt_acl_common ( fget_acl_blob , store_acl_blob_fsp ,
2017-08-18 15:41:57 +03:00
ACL_MODULE_NAME ,
handle , fsp , security_info_sent , psd ) ;
return status ;
}
2009-07-24 04:28:58 +04:00
static struct vfs_fn_pointers vfs_acl_tdb_fns = {
. connect_fn = connect_acl_tdb ,
2011-12-04 08:45:04 +04:00
. disconnect_fn = disconnect_acl_tdb ,
2019-09-12 02:42:01 +03:00
. unlinkat_fn = unlinkat_acl_tdb ,
2011-12-04 08:45:04 +04:00
. chmod_fn = chmod_acl_module_common ,
. fchmod_fn = fchmod_acl_module_common ,
2017-08-18 15:41:57 +03:00
. fget_nt_acl_fn = acl_tdb_fget_nt_acl ,
2020-04-13 23:33:44 +03:00
. get_nt_acl_at_fn = acl_tdb_get_nt_acl_at ,
2017-08-18 15:41:57 +03:00
. fset_nt_acl_fn = acl_tdb_fset_nt_acl ,
2011-12-04 08:45:04 +04:00
. sys_acl_set_fd_fn = sys_acl_set_fd_tdb
2008-11-11 04:57:22 +03:00
} ;
2015-08-13 19:16:20 +03:00
static_decl_vfs ;
2017-04-20 22:24:43 +03:00
NTSTATUS vfs_acl_tdb_init ( TALLOC_CTX * ctx )
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
}