2020-06-11 16:15:02 +03:00
/*
* Unix SMB / CIFS implementation .
* Utility functions for reparse points .
*
* Copyright ( C ) Jeremy Allison 2018
*
* 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"
# include "util_reparse.h"
2024-05-02 17:34:43 +03:00
# include "libcli/smb/reparse.h"
2022-12-02 13:56:08 +03:00
# include "source3/smbd/proto.h"
2020-06-11 16:15:02 +03:00
2024-05-05 12:14:45 +03:00
static NTSTATUS fsctl_get_reparse_point_reg ( struct files_struct * fsp ,
TALLOC_CTX * ctx ,
uint8_t * * _out_data ,
uint32_t max_out_len ,
uint32_t * _out_len )
{
uint8_t * val = NULL ;
ssize_t sizeret ;
NTSTATUS status ;
/*
* 64 k + 8 bytes is the maximum reparse point length
* possible
*/
val = talloc_array ( ctx , uint8_t , MIN ( max_out_len , 65536 + 8 ) ) ;
if ( val = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
sizeret = SMB_VFS_FGETXATTR ( fsp ,
SAMBA_XATTR_REPARSE_ATTRIB ,
val ,
talloc_get_size ( val ) ) ;
if ( ( sizeret = = - 1 ) & & ( errno = = ERANGE ) ) {
status = NT_STATUS_BUFFER_TOO_SMALL ;
goto fail ;
}
if ( ( sizeret = = - 1 ) & & ( errno = = ENOATTR ) ) {
DBG_DEBUG ( SAMBA_XATTR_REPARSE_ATTRIB " does not exist \n " ) ;
status = NT_STATUS_NOT_A_REPARSE_POINT ;
goto fail ;
}
if ( sizeret = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
DBG_DEBUG ( " SMB_VFS_FGETXATTR failed: %s \n " , strerror ( errno ) ) ;
goto fail ;
}
* _out_data = val ;
* _out_len = sizeret ;
return NT_STATUS_OK ;
fail :
TALLOC_FREE ( val ) ;
return status ;
}
2024-07-07 21:37:49 +03:00
static NTSTATUS fsctl_get_reparse_point_int (
struct files_struct * fsp ,
const struct reparse_data_buffer * reparse_data ,
TALLOC_CTX * ctx ,
uint8_t * * _out_data ,
uint32_t max_out_len ,
uint32_t * _out_len )
2024-07-05 18:58:55 +03:00
{
uint8_t * out_data = NULL ;
ssize_t out_len ;
2024-07-07 21:37:49 +03:00
out_len = reparse_data_buffer_marshall ( reparse_data , NULL , 0 ) ;
2024-07-05 18:58:55 +03:00
if ( out_len = = - 1 ) {
return NT_STATUS_INSUFFICIENT_RESOURCES ;
}
if ( max_out_len < out_len ) {
return NT_STATUS_BUFFER_TOO_SMALL ;
}
out_data = talloc_array ( ctx , uint8_t , out_len ) ;
if ( out_data = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2024-07-07 21:37:49 +03:00
reparse_data_buffer_marshall ( reparse_data , out_data , out_len ) ;
2024-07-05 18:58:55 +03:00
* _out_data = out_data ;
* _out_len = out_len ;
return NT_STATUS_OK ;
}
2024-07-07 21:37:49 +03:00
static NTSTATUS fsctl_get_reparse_point_fifo ( struct files_struct * fsp ,
TALLOC_CTX * ctx ,
uint8_t * * _out_data ,
uint32_t max_out_len ,
uint32_t * _out_len )
{
struct reparse_data_buffer reparse_data = {
. tag = IO_REPARSE_TAG_NFS ,
. parsed . nfs . type = NFS_SPECFILE_FIFO ,
} ;
return fsctl_get_reparse_point_int (
fsp , & reparse_data , ctx , _out_data , max_out_len , _out_len ) ;
}
2024-07-07 21:39:30 +03:00
static NTSTATUS fsctl_get_reparse_point_sock ( struct files_struct * fsp ,
TALLOC_CTX * ctx ,
uint8_t * * _out_data ,
uint32_t max_out_len ,
uint32_t * _out_len )
{
struct reparse_data_buffer reparse_data = {
. tag = IO_REPARSE_TAG_NFS ,
. parsed . nfs . type = NFS_SPECFILE_SOCK ,
} ;
return fsctl_get_reparse_point_int (
fsp , & reparse_data , ctx , _out_data , max_out_len , _out_len ) ;
}
2024-07-08 12:23:14 +03:00
static NTSTATUS fsctl_get_reparse_point_dev ( struct files_struct * fsp ,
uint64_t nfs_type ,
dev_t rdev ,
TALLOC_CTX * ctx ,
uint8_t * * _out_data ,
uint32_t max_out_len ,
uint32_t * _out_len )
{
struct reparse_data_buffer reparse_data = {
. tag = IO_REPARSE_TAG_NFS ,
. parsed . nfs . type = nfs_type ,
. parsed . nfs . data . dev . major = unix_dev_major ( rdev ) ,
. parsed . nfs . data . dev . minor = unix_dev_minor ( rdev ) ,
} ;
return fsctl_get_reparse_point_int (
fsp , & reparse_data , ctx , _out_data , max_out_len , _out_len ) ;
}
2020-06-11 16:15:02 +03:00
NTSTATUS fsctl_get_reparse_point ( struct files_struct * fsp ,
2022-12-21 16:39:00 +03:00
TALLOC_CTX * mem_ctx ,
2024-05-04 12:19:14 +03:00
uint32_t * _reparse_tag ,
2024-05-04 20:32:03 +03:00
uint8_t * * _out_data ,
2020-06-11 16:15:02 +03:00
uint32_t max_out_len ,
2024-05-04 20:32:03 +03:00
uint32_t * _out_len )
2020-06-11 16:15:02 +03:00
{
2024-05-05 12:14:45 +03:00
uint32_t dos_mode ;
uint8_t * out_data = NULL ;
uint32_t out_len = 0 ;
uint32_t reparse_tag = 0 ;
const uint8_t * reparse_data = NULL ;
size_t reparse_data_length ;
NTSTATUS status = NT_STATUS_NOT_A_REPARSE_POINT ;
dos_mode = fdos_mode ( fsp ) ;
if ( ( dos_mode & FILE_ATTRIBUTE_REPARSE_POINT ) = = 0 ) {
return NT_STATUS_NOT_A_REPARSE_POINT ;
}
2024-07-05 18:31:19 +03:00
switch ( fsp - > fsp_name - > st . st_ex_mode & S_IFMT ) {
case S_IFREG :
2024-05-05 12:14:45 +03:00
DBG_DEBUG ( " %s is a regular file \n " , fsp_str_dbg ( fsp ) ) ;
status = fsctl_get_reparse_point_reg (
fsp , mem_ctx , & out_data , max_out_len , & out_len ) ;
2024-07-05 18:31:19 +03:00
break ;
2024-07-05 18:58:55 +03:00
case S_IFIFO :
DBG_DEBUG ( " %s is a fifo \n " , fsp_str_dbg ( fsp ) ) ;
status = fsctl_get_reparse_point_fifo (
fsp , mem_ctx , & out_data , max_out_len , & out_len ) ;
break ;
2024-07-07 21:39:30 +03:00
case S_IFSOCK :
DBG_DEBUG ( " %s is a socket \n " , fsp_str_dbg ( fsp ) ) ;
status = fsctl_get_reparse_point_sock (
fsp , mem_ctx , & out_data , max_out_len , & out_len ) ;
break ;
2024-07-08 12:23:14 +03:00
case S_IFBLK :
DBG_DEBUG ( " %s is a block device \n " , fsp_str_dbg ( fsp ) ) ;
status = fsctl_get_reparse_point_dev (
fsp ,
NFS_SPECFILE_BLK ,
fsp - > fsp_name - > st . st_ex_rdev ,
mem_ctx ,
& out_data ,
max_out_len ,
& out_len ) ;
break ;
case S_IFCHR :
DBG_DEBUG ( " %s is a character device \n " , fsp_str_dbg ( fsp ) ) ;
status = fsctl_get_reparse_point_dev (
fsp ,
NFS_SPECFILE_CHR ,
fsp - > fsp_name - > st . st_ex_rdev ,
mem_ctx ,
& out_data ,
max_out_len ,
& out_len ) ;
break ;
2024-07-05 18:31:19 +03:00
default :
break ;
2024-05-05 12:14:45 +03:00
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
2022-12-22 13:36:21 +03:00
DBG_DEBUG ( " failed: %s \n " , nt_errstr ( status ) ) ;
2024-05-05 12:14:45 +03:00
return status ;
}
status = reparse_buffer_check ( out_data ,
out_len ,
& reparse_tag ,
& reparse_data ,
& reparse_data_length ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " Invalid reparse data: %s \n " , nt_errstr ( status ) ) ;
TALLOC_FREE ( out_data ) ;
return status ;
}
* _reparse_tag = reparse_tag ;
* _out_data = out_data ;
* _out_len = out_len ;
return NT_STATUS_OK ;
2020-06-11 16:15:02 +03:00
}
2024-05-13 15:26:22 +03:00
NTSTATUS fsctl_get_reparse_tag ( struct files_struct * fsp ,
uint32_t * _reparse_tag )
{
uint8_t * out_data = NULL ;
uint32_t out_len ;
NTSTATUS status ;
status = fsctl_get_reparse_point ( fsp ,
talloc_tos ( ) ,
_reparse_tag ,
& out_data ,
UINT32_MAX ,
& out_len ) ;
TALLOC_FREE ( out_data ) ;
return status ;
}
2020-06-11 16:15:02 +03:00
NTSTATUS fsctl_set_reparse_point ( struct files_struct * fsp ,
2022-12-21 16:39:00 +03:00
TALLOC_CTX * mem_ctx ,
2020-06-11 16:15:02 +03:00
const uint8_t * in_data ,
uint32_t in_len )
{
2024-05-02 17:34:43 +03:00
uint32_t reparse_tag ;
const uint8_t * reparse_data = NULL ;
size_t reparse_data_length ;
2022-12-02 13:56:08 +03:00
uint32_t existing_tag ;
2022-12-02 13:06:38 +03:00
NTSTATUS status ;
2022-12-02 13:56:08 +03:00
uint32_t dos_mode ;
int ret ;
2022-12-02 13:06:38 +03:00
2022-12-02 13:55:31 +03:00
DBG_DEBUG ( " Called on %s \n " , fsp_str_dbg ( fsp ) ) ;
2022-12-02 13:06:38 +03:00
2022-12-02 13:56:08 +03:00
if ( ! S_ISREG ( fsp - > fsp_name - > st . st_ex_mode ) ) {
DBG_DEBUG ( " Can only set reparse point for regular files \n " ) ;
return NT_STATUS_ACCESS_DENIED ;
}
2024-05-02 17:34:43 +03:00
status = reparse_buffer_check ( in_data ,
in_len ,
& reparse_tag ,
& reparse_data ,
& reparse_data_length ) ;
2022-12-02 13:06:38 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2024-05-02 17:34:43 +03:00
DBG_DEBUG ( " check_reparse_data_buffer failed: %s \n " ,
nt_errstr ( status ) ) ;
2022-12-02 13:06:38 +03:00
return status ;
}
2024-05-02 17:34:43 +03:00
DBG_DEBUG ( " reparse tag=% " PRIX32 " , length=%zu \n " ,
reparse_tag ,
reparse_data_length ) ;
2024-05-13 15:28:55 +03:00
status = fsctl_get_reparse_tag ( fsp , & existing_tag ) ;
if ( NT_STATUS_IS_OK ( status ) & & ( existing_tag ! = reparse_tag ) ) {
DBG_DEBUG ( " Can't overwrite tag % " PRIX32 " with tag % " PRIX32
" \n " ,
existing_tag ,
reparse_tag ) ;
return NT_STATUS_IO_REPARSE_TAG_MISMATCH ;
2022-12-02 13:56:08 +03:00
}
/* Store the data */
ret = SMB_VFS_FSETXATTR (
fsp , SAMBA_XATTR_REPARSE_ATTRIB , in_data , in_len , 0 ) ;
if ( ret = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
DBG_DEBUG ( " setxattr fail on %s - %s \n " ,
fsp_str_dbg ( fsp ) ,
strerror ( errno ) ) ;
return status ;
}
/*
* Files with reparse points don ' t have the ATTR_NORMAL bit
* set
*/
dos_mode = fdos_mode ( fsp ) ;
dos_mode & = ~ FILE_ATTRIBUTE_NORMAL ;
dos_mode | = FILE_ATTRIBUTE_REPARSE_POINT ;
status = SMB_VFS_FSET_DOS_ATTRIBUTES ( fsp - > conn , fsp , dos_mode ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_ERR ( " set reparse attr fail on %s - %s \n " ,
fsp_str_dbg ( fsp ) ,
nt_errstr ( status ) ) ;
return status ;
}
2024-06-24 17:50:57 +03:00
fsp - > fsp_name - > st . cached_dos_attributes = dos_mode ;
2022-12-02 13:56:08 +03:00
return NT_STATUS_OK ;
2020-06-11 16:15:02 +03:00
}
NTSTATUS fsctl_del_reparse_point ( struct files_struct * fsp ,
2022-12-21 16:39:00 +03:00
TALLOC_CTX * mem_ctx ,
2020-06-11 16:15:02 +03:00
const uint8_t * in_data ,
uint32_t in_len )
{
2024-05-06 17:59:44 +03:00
uint32_t existing_tag ;
uint32_t reparse_tag ;
const uint8_t * reparse_data = NULL ;
size_t reparse_data_length ;
NTSTATUS status ;
uint32_t dos_mode ;
int ret ;
2024-05-13 15:30:30 +03:00
status = fsctl_get_reparse_tag ( fsp , & existing_tag ) ;
2024-05-06 17:59:44 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
status = reparse_buffer_check ( in_data ,
in_len ,
& reparse_tag ,
& reparse_data ,
& reparse_data_length ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
if ( reparse_data_length ! = 0 ) {
return NT_STATUS_IO_REPARSE_DATA_INVALID ;
}
if ( existing_tag ! = reparse_tag ) {
DBG_DEBUG ( " Expect correct tag % " PRIX32 " , got tag % " PRIX32
" \n " ,
existing_tag ,
reparse_tag ) ;
return NT_STATUS_IO_REPARSE_TAG_MISMATCH ;
}
ret = SMB_VFS_FREMOVEXATTR ( fsp , SAMBA_XATTR_REPARSE_ATTRIB ) ;
if ( ret = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
DBG_DEBUG ( " removexattr fail on %s - %s \n " ,
fsp_str_dbg ( fsp ) ,
strerror ( errno ) ) ;
return status ;
}
/*
* Files with reparse points don ' t have the ATTR_NORMAL bit
* set
*/
dos_mode = fdos_mode ( fsp ) ;
dos_mode & = ~ FILE_ATTRIBUTE_REPARSE_POINT ;
status = SMB_VFS_FSET_DOS_ATTRIBUTES ( fsp - > conn , fsp , dos_mode ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_ERR ( " set reparse attr fail on %s - %s \n " ,
fsp_str_dbg ( fsp ) ,
nt_errstr ( status ) ) ;
return status ;
}
2024-06-24 17:50:57 +03:00
fsp - > fsp_name - > st . cached_dos_attributes = dos_mode ;
2024-05-06 17:59:44 +03:00
return NT_STATUS_OK ;
2020-06-11 16:15:02 +03:00
}