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 ;
}
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 ;
}
if ( S_ISREG ( fsp - > fsp_name - > st . st_ex_mode ) ) {
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 ) ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
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
}
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 ;
uint8_t * existing_data = NULL ;
uint32_t existing_len ;
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 ) ;
2022-12-02 13:56:08 +03:00
status = fsctl_get_reparse_point ( fsp ,
talloc_tos ( ) ,
& existing_tag ,
& existing_data ,
UINT32_MAX ,
& existing_len ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( existing_data ) ;
if ( 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 ;
}
}
/* 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 ;
}
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 ;
uint8_t * existing_data = NULL ;
uint32_t existing_len ;
uint32_t reparse_tag ;
const uint8_t * reparse_data = NULL ;
size_t reparse_data_length ;
NTSTATUS status ;
uint32_t dos_mode ;
int ret ;
status = fsctl_get_reparse_point ( fsp ,
talloc_tos ( ) ,
& existing_tag ,
& existing_data ,
UINT32_MAX ,
& existing_len ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
TALLOC_FREE ( existing_data ) ;
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 ;
}
return NT_STATUS_OK ;
2020-06-11 16:15:02 +03:00
}