2009-07-02 14:28:58 +04:00
/*
Unix SMB / CIFS implementation .
Core SMB2 server
Copyright ( C ) Stefan Metzmacher 2009
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-22 18:57:01 +03:00
# include "smbd/smbd.h"
2009-07-02 14:28:58 +04:00
# include "smbd/globals.h"
2009-08-12 19:52:55 +04:00
# include "../libcli/smb/smb_common.h"
2011-02-25 02:03:01 +03:00
# include "trans2.h"
2011-04-28 19:38:09 +04:00
# include "../lib/util/tevent_ntstatus.h"
2017-03-19 17:58:17 +03:00
# include "system/filesys.h"
2009-07-02 14:28:58 +04:00
2018-03-21 22:01:05 +03:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_SMB2
2015-03-24 17:16:26 +03:00
static struct tevent_req * smbd_smb2_query_directory_send ( TALLOC_CTX * mem_ctx ,
2009-07-02 14:28:58 +04:00
struct tevent_context * ev ,
struct smbd_smb2_request * smb2req ,
2012-06-08 12:42:43 +04:00
struct files_struct * in_fsp ,
2009-07-02 14:28:58 +04:00
uint8_t in_file_info_class ,
uint8_t in_flags ,
uint32_t in_file_index ,
uint32_t in_output_buffer_length ,
const char * in_file_name ) ;
2015-03-24 17:16:26 +03:00
static NTSTATUS smbd_smb2_query_directory_recv ( struct tevent_req * req ,
2009-07-02 14:28:58 +04:00
TALLOC_CTX * mem_ctx ,
DATA_BLOB * out_output_buffer ) ;
static void smbd_smb2_request_find_done ( struct tevent_req * subreq ) ;
2015-03-24 17:16:26 +03:00
NTSTATUS smbd_smb2_request_process_query_directory ( struct smbd_smb2_request * req )
2009-07-02 14:28:58 +04:00
{
2011-09-06 16:01:43 +04:00
NTSTATUS status ;
2009-07-02 14:28:58 +04:00
const uint8_t * inbody ;
uint8_t in_file_info_class ;
uint8_t in_flags ;
uint32_t in_file_index ;
uint64_t in_file_id_persistent ;
uint64_t in_file_id_volatile ;
2012-06-08 12:42:43 +04:00
struct files_struct * in_fsp ;
2009-07-02 14:28:58 +04:00
uint16_t in_file_name_offset ;
uint16_t in_file_name_length ;
DATA_BLOB in_file_name_buffer ;
char * in_file_name_string ;
size_t in_file_name_string_size ;
uint32_t in_output_buffer_length ;
struct tevent_req * subreq ;
bool ok ;
2011-09-06 16:01:43 +04:00
status = smbd_smb2_request_verify_sizes ( req , 0x21 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return smbd_smb2_request_error ( req , status ) ;
2009-07-02 14:28:58 +04:00
}
2012-08-05 17:00:23 +04:00
inbody = SMBD_SMB2_IN_BODY_PTR ( req ) ;
2009-07-02 14:28:58 +04:00
in_file_info_class = CVAL ( inbody , 0x02 ) ;
in_flags = CVAL ( inbody , 0x03 ) ;
in_file_index = IVAL ( inbody , 0x04 ) ;
in_file_id_persistent = BVAL ( inbody , 0x08 ) ;
in_file_id_volatile = BVAL ( inbody , 0x10 ) ;
in_file_name_offset = SVAL ( inbody , 0x18 ) ;
in_file_name_length = SVAL ( inbody , 0x1A ) ;
in_output_buffer_length = IVAL ( inbody , 0x1C ) ;
if ( in_file_name_offset = = 0 & & in_file_name_length = = 0 ) {
/* This is ok */
} else if ( in_file_name_offset ! =
2012-08-05 17:00:23 +04:00
( SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN ( req ) ) ) {
2009-07-02 14:28:58 +04:00
return smbd_smb2_request_error ( req , NT_STATUS_INVALID_PARAMETER ) ;
}
2012-08-05 17:00:23 +04:00
if ( in_file_name_length > SMBD_SMB2_IN_DYN_LEN ( req ) ) {
2009-07-02 14:28:58 +04:00
return smbd_smb2_request_error ( req , NT_STATUS_INVALID_PARAMETER ) ;
}
2010-04-30 00:40:25 +04:00
/* The output header is 8 bytes. */
if ( in_output_buffer_length < = 8 ) {
return smbd_smb2_request_error ( req , NT_STATUS_INVALID_PARAMETER ) ;
}
DEBUG ( 10 , ( " smbd_smb2_request_find_done: in_output_buffer_length = %u \n " ,
( unsigned int ) in_output_buffer_length ) ) ;
/* Take into account the output header. */
in_output_buffer_length - = 8 ;
2012-08-05 17:00:23 +04:00
in_file_name_buffer . data = SMBD_SMB2_IN_DYN_PTR ( req ) ;
2009-07-02 14:28:58 +04:00
in_file_name_buffer . length = in_file_name_length ;
ok = convert_string_talloc ( req , CH_UTF16 , CH_UNIX ,
in_file_name_buffer . data ,
in_file_name_buffer . length ,
& in_file_name_string ,
2011-03-24 02:59:41 +03:00
& in_file_name_string_size ) ;
2009-07-02 14:28:58 +04:00
if ( ! ok ) {
return smbd_smb2_request_error ( req , NT_STATUS_ILLEGAL_CHARACTER ) ;
}
2011-09-06 16:14:52 +04:00
if ( in_file_name_buffer . length = = 0 ) {
in_file_name_string_size = 0 ;
}
if ( strlen ( in_file_name_string ) ! = in_file_name_string_size ) {
return smbd_smb2_request_error ( req , NT_STATUS_OBJECT_NAME_INVALID ) ;
}
2012-06-08 12:42:43 +04:00
in_fsp = file_fsp_smb2 ( req , in_file_id_persistent , in_file_id_volatile ) ;
if ( in_fsp = = NULL ) {
2009-07-02 14:28:58 +04:00
return smbd_smb2_request_error ( req , NT_STATUS_FILE_CLOSED ) ;
}
2018-03-22 12:54:41 +03:00
subreq = smbd_smb2_query_directory_send ( req , req - > ev_ctx ,
2012-06-08 12:42:43 +04:00
req , in_fsp ,
2009-07-02 14:28:58 +04:00
in_file_info_class ,
in_flags ,
in_file_index ,
in_output_buffer_length ,
in_file_name_string ) ;
if ( subreq = = NULL ) {
return smbd_smb2_request_error ( req , NT_STATUS_NO_MEMORY ) ;
}
tevent_req_set_callback ( subreq , smbd_smb2_request_find_done , req ) ;
2011-11-14 18:42:55 +04:00
return smbd_smb2_request_pending_queue ( req , subreq , 500 ) ;
2009-07-02 14:28:58 +04:00
}
static void smbd_smb2_request_find_done ( struct tevent_req * subreq )
{
struct smbd_smb2_request * req = tevent_req_callback_data ( subreq ,
struct smbd_smb2_request ) ;
DATA_BLOB outbody ;
DATA_BLOB outdyn ;
uint16_t out_output_buffer_offset ;
2009-07-24 18:21:07 +04:00
DATA_BLOB out_output_buffer = data_blob_null ;
2009-07-02 14:28:58 +04:00
NTSTATUS status ;
NTSTATUS error ; /* transport error */
2015-03-24 17:16:26 +03:00
status = smbd_smb2_query_directory_recv ( subreq ,
2009-07-02 14:28:58 +04:00
req ,
& out_output_buffer ) ;
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
error = smbd_smb2_request_error ( req , status ) ;
if ( ! NT_STATUS_IS_OK ( error ) ) {
2014-06-11 14:15:48 +04:00
smbd_server_connection_terminate ( req - > xconn ,
2009-07-02 14:28:58 +04:00
nt_errstr ( error ) ) ;
return ;
}
return ;
}
out_output_buffer_offset = SMB2_HDR_BODY + 0x08 ;
2013-12-04 17:59:07 +04:00
outbody = smbd_smb2_generate_outbody ( req , 0x08 ) ;
2009-07-02 14:28:58 +04:00
if ( outbody . data = = NULL ) {
error = smbd_smb2_request_error ( req , NT_STATUS_NO_MEMORY ) ;
if ( ! NT_STATUS_IS_OK ( error ) ) {
2014-06-11 14:15:48 +04:00
smbd_server_connection_terminate ( req - > xconn ,
2009-07-02 14:28:58 +04:00
nt_errstr ( error ) ) ;
return ;
}
return ;
}
SSVAL ( outbody . data , 0x00 , 0x08 + 1 ) ; /* struct size */
SSVAL ( outbody . data , 0x02 ,
out_output_buffer_offset ) ; /* output buffer offset */
SIVAL ( outbody . data , 0x04 ,
out_output_buffer . length ) ; /* output buffer length */
2010-04-30 00:40:25 +04:00
DEBUG ( 10 , ( " smbd_smb2_request_find_done: out_output_buffer.length = %u \n " ,
( unsigned int ) out_output_buffer . length ) ) ;
2009-07-02 14:28:58 +04:00
outdyn = out_output_buffer ;
error = smbd_smb2_request_done ( req , outbody , & outdyn ) ;
if ( ! NT_STATUS_IS_OK ( error ) ) {
2014-06-11 14:15:48 +04:00
smbd_server_connection_terminate ( req - > xconn ,
2009-07-02 14:28:58 +04:00
nt_errstr ( error ) ) ;
return ;
}
}
2016-12-18 10:53:43 +03:00
static struct tevent_req * fetch_write_time_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
connection_struct * conn ,
struct file_id id ,
int info_level ,
char * entry_marshall_buf ,
bool * stop ) ;
static NTSTATUS fetch_write_time_recv ( struct tevent_req * req ) ;
2015-03-24 17:16:26 +03:00
struct smbd_smb2_query_directory_state {
2017-01-11 17:36:38 +03:00
struct tevent_context * ev ;
2009-07-02 14:28:58 +04:00
struct smbd_smb2_request * smb2req ;
2018-07-03 12:25:57 +03:00
uint64_t async_sharemode_count ;
2017-01-11 17:36:38 +03:00
uint32_t find_async_delay_usec ;
2009-07-02 14:28:58 +04:00
DATA_BLOB out_output_buffer ;
2018-07-03 12:25:57 +03:00
struct smb_request * smbreq ;
int in_output_buffer_length ;
struct files_struct * fsp ;
const char * in_file_name ;
NTSTATUS empty_status ;
uint32_t info_level ;
uint32_t max_count ;
char * pdata ;
char * base_data ;
char * end_data ;
uint32_t num ;
uint32_t dirtype ;
bool dont_descend ;
bool ask_sharemode ;
bool async_ask_sharemode ;
int last_entry_off ;
bool done ;
2009-07-02 14:28:58 +04:00
} ;
2018-07-03 12:25:57 +03:00
static bool smb2_query_directory_next_entry ( struct tevent_req * req ) ;
2016-12-18 10:53:43 +03:00
static void smb2_query_directory_fetch_write_time_done ( struct tevent_req * subreq ) ;
2017-01-11 17:36:38 +03:00
static void smb2_query_directory_waited ( struct tevent_req * subreq ) ;
2016-12-18 10:53:43 +03:00
2015-03-24 17:16:26 +03:00
static struct tevent_req * smbd_smb2_query_directory_send ( TALLOC_CTX * mem_ctx ,
2009-07-02 14:28:58 +04:00
struct tevent_context * ev ,
struct smbd_smb2_request * smb2req ,
2012-06-08 12:42:43 +04:00
struct files_struct * fsp ,
2009-07-02 14:28:58 +04:00
uint8_t in_file_info_class ,
uint8_t in_flags ,
uint32_t in_file_index ,
uint32_t in_output_buffer_length ,
const char * in_file_name )
{
2014-06-12 10:38:48 +04:00
struct smbXsrv_connection * xconn = smb2req - > xconn ;
2009-07-02 14:28:58 +04:00
struct tevent_req * req ;
2015-03-24 17:16:26 +03:00
struct smbd_smb2_query_directory_state * state ;
2012-03-27 13:09:05 +04:00
connection_struct * conn = smb2req - > tcon - > compat ;
2009-08-06 14:16:30 +04:00
NTSTATUS status ;
2015-12-15 13:06:35 +03:00
bool wcard_has_wild = false ;
2013-08-29 21:36:00 +04:00
struct tm tm ;
char * p ;
2018-07-03 12:25:57 +03:00
bool stop = false ;
2018-03-21 22:41:44 +03:00
bool ok ;
2009-07-02 14:28:58 +04:00
req = tevent_req_create ( mem_ctx , & state ,
2015-03-24 17:16:26 +03:00
struct smbd_smb2_query_directory_state ) ;
2009-07-02 14:28:58 +04:00
if ( req = = NULL ) {
return NULL ;
}
2017-01-11 17:36:38 +03:00
state - > ev = ev ;
2018-07-03 12:25:57 +03:00
state - > fsp = fsp ;
2009-07-02 14:28:58 +04:00
state - > smb2req = smb2req ;
2018-07-03 12:25:57 +03:00
state - > in_output_buffer_length = in_output_buffer_length ;
state - > in_file_name = in_file_name ;
2009-07-02 14:28:58 +04:00
state - > out_output_buffer = data_blob_null ;
2018-07-03 12:25:57 +03:00
state - > dirtype = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY ;
2009-07-02 14:28:58 +04:00
2015-03-24 17:16:26 +03:00
DEBUG ( 10 , ( " smbd_smb2_query_directory_send: %s - %s \n " ,
2012-06-14 15:13:59 +04:00
fsp_str_dbg ( fsp ) , fsp_fnum_dbg ( fsp ) ) ) ;
2009-07-02 14:28:58 +04:00
2018-07-03 12:25:57 +03:00
state - > smbreq = smbd_smb2_fake_smb_request ( smb2req ) ;
if ( tevent_req_nomem ( state - > smbreq , req ) ) {
2009-07-02 14:28:58 +04:00
return tevent_req_post ( req , ev ) ;
}
2009-08-06 14:16:30 +04:00
if ( ! fsp - > is_directory ) {
tevent_req_nterror ( req , NT_STATUS_NOT_SUPPORTED ) ;
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
if ( strcmp ( state - > in_file_name , " " ) = = 0 ) {
2009-08-06 14:16:30 +04:00
tevent_req_nterror ( req , NT_STATUS_OBJECT_NAME_INVALID ) ;
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
if ( strchr_m ( state - > in_file_name , ' \\ ' ) ! = NULL ) {
2009-08-06 14:16:30 +04:00
tevent_req_nterror ( req , NT_STATUS_OBJECT_NAME_INVALID ) ;
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
if ( strchr_m ( state - > in_file_name , ' / ' ) ! = NULL ) {
2009-08-06 14:16:30 +04:00
tevent_req_nterror ( req , NT_STATUS_OBJECT_NAME_INVALID ) ;
return tevent_req_post ( req , ev ) ;
2013-08-29 21:36:00 +04:00
}
2018-07-03 12:25:57 +03:00
p = strptime ( state - > in_file_name , GMT_FORMAT , & tm ) ;
2013-08-29 21:36:00 +04:00
if ( ( p ! = NULL ) & & ( * p = = ' \0 ' ) ) {
/*
* Bogus find that asks for a shadow copy timestamp as a
* directory . The correct response is that it does not exist as
* a directory .
*/
tevent_req_nterror ( req , NT_STATUS_NO_SUCH_FILE ) ;
return tevent_req_post ( req , ev ) ;
2009-08-06 14:16:30 +04:00
}
2014-05-23 12:22:34 +04:00
if ( in_output_buffer_length > xconn - > smb2 . server . max_trans ) {
2015-03-24 17:16:26 +03:00
DEBUG ( 2 , ( " smbd_smb2_query_directory_send: "
2012-05-11 21:03:50 +04:00
" client ignored max trans:%s: 0x%08X: 0x%08X \n " ,
__location__ , in_output_buffer_length ,
2014-05-23 12:22:34 +04:00
xconn - > smb2 . server . max_trans ) ) ;
2009-08-06 14:16:30 +04:00
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER ) ;
return tevent_req_post ( req , ev ) ;
}
2012-02-28 05:51:40 +04:00
status = smbd_smb2_request_verify_creditcharge ( smb2req ,
in_output_buffer_length ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER ) ;
return tevent_req_post ( req , ev ) ;
}
2009-08-06 14:16:30 +04:00
switch ( in_file_info_class ) {
case SMB2_FIND_DIRECTORY_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_FILE_DIRECTORY_INFO ;
2009-08-06 14:16:30 +04:00
break ;
case SMB2_FIND_FULL_DIRECTORY_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO ;
2009-08-06 14:16:30 +04:00
break ;
case SMB2_FIND_BOTH_DIRECTORY_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO ;
2009-08-06 14:16:30 +04:00
break ;
case SMB2_FIND_NAME_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_FILE_NAMES_INFO ;
2009-08-06 14:16:30 +04:00
break ;
case SMB2_FIND_ID_BOTH_DIRECTORY_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_ID_BOTH_DIRECTORY_INFO ;
2009-08-06 14:16:30 +04:00
break ;
case SMB2_FIND_ID_FULL_DIRECTORY_INFO :
2018-07-03 12:25:57 +03:00
state - > info_level = SMB_FIND_ID_FULL_DIRECTORY_INFO ;
2009-08-06 14:16:30 +04:00
break ;
default :
tevent_req_nterror ( req , NT_STATUS_INVALID_INFO_CLASS ) ;
return tevent_req_post ( req , ev ) ;
}
if ( in_flags & SMB2_CONTINUE_FLAG_REOPEN ) {
2017-03-19 17:58:17 +03:00
int flags ;
2018-02-15 00:23:12 +03:00
status = fd_close ( fsp ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
2017-03-19 17:58:17 +03:00
/*
2018-02-15 00:23:12 +03:00
* fd_close ( ) will close and invalidate the fsp ' s file
* descriptor . So we have to reopen it .
2017-03-19 17:58:17 +03:00
*/
flags = O_RDONLY ;
# ifdef O_DIRECTORY
flags | = O_DIRECTORY ;
# endif
status = fd_open ( conn , fsp , flags , 0 ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
2009-08-06 14:16:30 +04:00
}
2018-07-03 12:25:57 +03:00
if ( ! state - > smbreq - > posix_pathnames ) {
wcard_has_wild = ms_has_wild ( state - > in_file_name ) ;
2015-12-12 01:53:30 +03:00
}
2009-08-06 14:16:30 +04:00
2014-06-11 02:58:15 +04:00
/* Ensure we've canonicalized any search path if not a wildcard. */
if ( ! wcard_has_wild ) {
struct smb_filename * smb_fname = NULL ;
const char * fullpath ;
2014-06-11 13:32:56 +04:00
char tmpbuf [ PATH_MAX ] ;
char * to_free = NULL ;
2015-12-22 22:12:34 +03:00
uint32_t ucf_flags = UCF_SAVE_LCOMP |
2015-12-22 22:27:39 +03:00
UCF_ALWAYS_ALLOW_WCARD_LCOMP |
2018-07-03 12:25:57 +03:00
( state - > smbreq - > posix_pathnames ?
2015-12-22 22:27:39 +03:00
UCF_POSIX_PATHNAMES : 0 ) ;
2009-08-06 14:16:30 +04:00
2014-06-11 02:58:15 +04:00
if ( ISDOT ( fsp - > fsp_name - > base_name ) ) {
2018-07-03 12:25:57 +03:00
fullpath = state - > in_file_name ;
2014-06-11 02:58:15 +04:00
} else {
2014-06-11 13:32:56 +04:00
size_t len ;
char * tmp ;
len = full_path_tos (
2018-07-03 12:25:57 +03:00
fsp - > fsp_name - > base_name , state - > in_file_name ,
2014-06-11 13:32:56 +04:00
tmpbuf , sizeof ( tmpbuf ) , & tmp , & to_free ) ;
if ( len = = - 1 ) {
tevent_req_oom ( req ) ;
return tevent_req_post ( req , ev ) ;
}
fullpath = tmp ;
2014-06-11 02:58:15 +04:00
}
status = filename_convert ( state ,
conn ,
fullpath ,
2015-12-22 22:12:34 +03:00
ucf_flags ,
2014-06-11 02:58:15 +04:00
& wcard_has_wild ,
& smb_fname ) ;
2014-06-11 13:32:56 +04:00
TALLOC_FREE ( to_free ) ;
2014-06-11 13:35:37 +04:00
if ( tevent_req_nterror ( req , status ) ) {
2014-06-11 02:58:15 +04:00
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
state - > in_file_name = smb_fname - > original_lcomp ;
2014-06-11 02:58:15 +04:00
}
if ( fsp - > dptr = = NULL ) {
2009-08-06 14:16:30 +04:00
status = dptr_create ( conn ,
2012-03-01 04:05:50 +04:00
NULL , /* req */
2011-02-10 02:05:58 +03:00
fsp ,
2016-02-27 03:35:17 +03:00
fsp - > fsp_name ,
2009-08-06 14:16:30 +04:00
false , /* old_handle */
false , /* expect_close */
0 , /* spid */
2018-07-03 12:25:57 +03:00
state - > in_file_name , /* wcard */
2009-08-06 14:16:30 +04:00
wcard_has_wild ,
2018-07-03 12:25:57 +03:00
state - > dirtype ,
2009-08-06 14:16:30 +04:00
& fsp - > dptr ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
state - > empty_status = NT_STATUS_NO_SUCH_FILE ;
2009-08-06 14:16:30 +04:00
} else {
2018-07-03 12:25:57 +03:00
state - > empty_status = STATUS_NO_MORE_FILES ;
2009-08-06 14:16:30 +04:00
}
if ( in_flags & SMB2_CONTINUE_FLAG_RESTART ) {
dptr_SeekDir ( fsp - > dptr , 0 ) ;
}
if ( in_flags & SMB2_CONTINUE_FLAG_SINGLE ) {
2018-07-03 12:25:57 +03:00
state - > max_count = 1 ;
2009-08-06 14:16:30 +04:00
} else {
2018-07-03 12:25:57 +03:00
state - > max_count = UINT16_MAX ;
2009-08-06 14:16:30 +04:00
}
# define DIR_ENTRY_SAFETY_MARGIN 4096
state - > out_output_buffer = data_blob_talloc ( state , NULL ,
in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN ) ;
if ( tevent_req_nomem ( state - > out_output_buffer . data , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > out_output_buffer . length = 0 ;
2018-07-03 12:25:57 +03:00
state - > pdata = ( char * ) state - > out_output_buffer . data ;
state - > base_data = state - > pdata ;
2010-05-14 02:59:09 +04:00
/*
* end_data must include the safety margin as it ' s what is
* used to determine if pushed strings have been truncated .
*/
2018-07-03 12:25:57 +03:00
state - > end_data = state - > pdata + in_output_buffer_length + DIR_ENTRY_SAFETY_MARGIN - 1 ;
2009-08-06 14:16:30 +04:00
2015-03-24 17:16:26 +03:00
DEBUG ( 8 , ( " smbd_smb2_query_directory_send: dirpath=<%s> dontdescend=<%s>, "
2010-04-30 00:40:25 +04:00
" in_output_buffer_length = %u \n " ,
2014-02-03 05:56:12 +04:00
fsp - > fsp_name - > base_name , lp_dont_descend ( talloc_tos ( ) , SNUM ( conn ) ) ,
2010-04-30 00:40:25 +04:00
( unsigned int ) in_output_buffer_length ) ) ;
2014-02-03 05:56:12 +04:00
if ( in_list ( fsp - > fsp_name - > base_name , lp_dont_descend ( talloc_tos ( ) , SNUM ( conn ) ) ,
2011-02-10 02:06:40 +03:00
conn - > case_sensitive ) ) {
2018-07-03 12:25:57 +03:00
state - > dont_descend = true ;
2009-08-06 14:16:30 +04:00
}
2016-12-23 21:51:49 +03:00
/*
* SMB_FIND_FILE_NAMES_INFO doesn ' t need stat information
*
* This may change when we try to improve the delete on close
* handling in future .
*/
2018-07-03 12:25:57 +03:00
if ( state - > info_level ! = SMB_FIND_FILE_NAMES_INFO ) {
state - > ask_sharemode = lp_parm_bool (
SNUM ( conn ) , " smbd " , " search ask sharemode " , true ) ;
2016-12-23 21:51:49 +03:00
}
2009-08-06 14:16:30 +04:00
2018-07-03 12:25:57 +03:00
if ( state - > ask_sharemode & & lp_clustering ( ) ) {
state - > ask_sharemode = false ;
state - > async_ask_sharemode = true ;
2016-12-18 10:53:43 +03:00
/*
* Should we only set async_internal
* if we ' re not the last request in
* a compound chain ?
*/
smb2_request_set_async_internal ( smb2req , true ) ;
}
2017-01-11 17:36:38 +03:00
/*
* This gets set in autobuild for some tests
*/
state - > find_async_delay_usec = lp_parm_ulong ( SNUM ( conn ) , " smbd " ,
" find async delay usec " ,
0 ) ;
2018-07-03 12:25:57 +03:00
while ( ! stop ) {
stop = smb2_query_directory_next_entry ( req ) ;
}
2009-08-06 14:16:30 +04:00
2018-07-03 12:25:57 +03:00
if ( ! tevent_req_is_in_progress ( req ) ) {
return tevent_req_post ( req , ev ) ;
}
2009-08-06 14:16:30 +04:00
2018-03-21 22:41:44 +03:00
ok = aio_add_req_to_fsp ( fsp , req ) ;
if ( ! ok ) {
DBG_ERR ( " Could not add req to fsp \n " ) ;
tevent_req_nterror ( req , NT_STATUS_INTERNAL_ERROR ) ;
return tevent_req_post ( req , ev ) ;
}
2018-07-03 12:25:57 +03:00
return req ;
}
2016-12-18 10:53:43 +03:00
2018-07-03 12:25:57 +03:00
static bool smb2_query_directory_next_entry ( struct tevent_req * req )
{
struct smbd_smb2_query_directory_state * state = tevent_req_data (
req , struct smbd_smb2_query_directory_state ) ;
2018-03-15 21:03:59 +03:00
struct smb_filename * smb_fname = NULL ; /* relative to fsp !! */
2018-07-03 12:25:57 +03:00
bool got_exact_match = false ;
int off = state - > out_output_buffer . length ;
int space_remaining = state - > in_output_buffer_length - off ;
struct file_id file_id ;
NTSTATUS status ;
bool stop = false ;
SMB_ASSERT ( space_remaining > = 0 ) ;
status = smbd_dirptr_lanman2_entry ( state ,
state - > fsp - > conn ,
state - > fsp - > dptr ,
state - > smbreq - > flags2 ,
state - > in_file_name ,
state - > dirtype ,
state - > info_level ,
false , /* requires_resume_key */
state - > dont_descend ,
state - > ask_sharemode ,
true ,
8 , /* align to 8 bytes */
false , /* no padding */
& state - > pdata ,
state - > base_data ,
state - > end_data ,
space_remaining ,
2018-03-15 21:03:59 +03:00
& smb_fname ,
2018-07-03 12:25:57 +03:00
& got_exact_match ,
& state - > last_entry_off ,
NULL ,
& file_id ) ;
off = ( int ) PTR_DIFF ( state - > pdata , state - > base_data ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
if ( NT_STATUS_EQUAL ( status , NT_STATUS_ILLEGAL_CHARACTER ) ) {
/*
* Bad character conversion on name . Ignore this
* entry .
*/
return false ;
} else if ( state - > num > 0 ) {
goto last_entry_done ;
} else if ( NT_STATUS_EQUAL ( status , STATUS_MORE_ENTRIES ) ) {
tevent_req_nterror ( req , NT_STATUS_INFO_LENGTH_MISMATCH ) ;
return true ;
} else {
tevent_req_nterror ( req , state - > empty_status ) ;
return true ;
2016-12-18 10:53:43 +03:00
}
2018-07-03 12:25:57 +03:00
}
2016-12-18 10:53:43 +03:00
2018-07-03 12:25:57 +03:00
if ( state - > async_ask_sharemode ) {
struct tevent_req * subreq = NULL ;
2018-06-21 13:44:42 +03:00
char * buf = state - > base_data + state - > last_entry_off ;
2009-08-06 14:16:30 +04:00
2018-07-03 12:25:57 +03:00
subreq = fetch_write_time_send ( state ,
state - > ev ,
state - > fsp - > conn ,
file_id ,
state - > info_level ,
2018-06-21 13:44:42 +03:00
buf ,
2018-07-03 12:25:57 +03:00
& stop ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return true ;
2016-12-18 10:53:43 +03:00
}
2018-07-03 12:25:57 +03:00
tevent_req_set_callback (
subreq ,
smb2_query_directory_fetch_write_time_done ,
req ) ;
state - > async_sharemode_count + + ;
}
2016-12-18 10:53:43 +03:00
2018-03-15 21:03:59 +03:00
TALLOC_FREE ( smb_fname ) ;
2018-07-03 12:25:57 +03:00
state - > num + + ;
state - > out_output_buffer . length = off ;
if ( ! state - > done & & state - > num < state - > max_count ) {
return stop ;
}
2009-08-06 14:16:30 +04:00
2016-12-18 10:53:43 +03:00
last_entry_done :
2018-07-03 12:25:57 +03:00
SIVAL ( state - > out_output_buffer . data , state - > last_entry_off , 0 ) ;
2017-01-11 17:36:38 +03:00
2018-07-03 12:25:57 +03:00
state - > done = true ;
2017-01-11 17:36:38 +03:00
2018-07-03 12:25:57 +03:00
if ( state - > async_sharemode_count > 0 ) {
DBG_DEBUG ( " Stopping after % " PRIu64 " async mtime "
" updates \n " , state - > async_sharemode_count ) ;
return true ;
}
2017-01-11 17:36:38 +03:00
2018-07-03 12:25:57 +03:00
if ( state - > find_async_delay_usec > 0 ) {
struct timeval tv ;
struct tevent_req * subreq = NULL ;
2017-01-11 17:36:38 +03:00
2018-07-03 12:25:57 +03:00
/*
* Should we only set async_internal
* if we ' re not the last request in
* a compound chain ?
*/
smb2_request_set_async_internal ( state - > smb2req , true ) ;
2017-01-11 17:36:38 +03:00
2018-07-03 12:25:57 +03:00
tv = timeval_current_ofs ( 0 , state - > find_async_delay_usec ) ;
subreq = tevent_wakeup_send ( state , state - > ev , tv ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return true ;
}
tevent_req_set_callback ( subreq ,
smb2_query_directory_waited ,
req ) ;
return req ;
2009-08-06 14:16:30 +04:00
}
2018-07-03 12:25:57 +03:00
tevent_req_done ( req ) ;
return true ;
2009-07-02 14:28:58 +04:00
}
2018-07-03 12:25:57 +03:00
static void smb2_query_directory_check_next_entry ( struct tevent_req * req ) ;
2016-12-18 10:53:43 +03:00
static void smb2_query_directory_fetch_write_time_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct smbd_smb2_query_directory_state * state = tevent_req_data (
req , struct smbd_smb2_query_directory_state ) ;
NTSTATUS status ;
2018-07-03 12:25:57 +03:00
state - > async_sharemode_count - - ;
2016-12-18 10:53:43 +03:00
status = fetch_write_time_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2018-07-03 12:25:57 +03:00
smb2_query_directory_check_next_entry ( req ) ;
return ;
}
static void smb2_query_directory_check_next_entry ( struct tevent_req * req )
{
struct smbd_smb2_query_directory_state * state = tevent_req_data (
req , struct smbd_smb2_query_directory_state ) ;
bool stop = false ;
if ( ! state - > done ) {
while ( ! stop ) {
stop = smb2_query_directory_next_entry ( req ) ;
}
return ;
}
if ( state - > async_sharemode_count > 0 ) {
2016-12-18 10:53:43 +03:00
return ;
}
2017-01-11 17:36:38 +03:00
if ( state - > find_async_delay_usec > 0 ) {
struct timeval tv ;
2018-07-03 12:25:57 +03:00
struct tevent_req * subreq = NULL ;
2017-01-11 17:36:38 +03:00
tv = timeval_current_ofs ( 0 , state - > find_async_delay_usec ) ;
subreq = tevent_wakeup_send ( state , state - > ev , tv ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
tevent_req_post ( req , state - > ev ) ;
return ;
}
tevent_req_set_callback ( subreq ,
smb2_query_directory_waited ,
req ) ;
return ;
}
2016-12-18 10:53:43 +03:00
tevent_req_done ( req ) ;
return ;
}
2017-01-11 17:36:38 +03:00
static void smb2_query_directory_waited ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
bool ok ;
ok = tevent_wakeup_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ! ok ) {
tevent_req_oom ( req ) ;
return ;
}
tevent_req_done ( req ) ;
}
2015-03-24 17:16:26 +03:00
static NTSTATUS smbd_smb2_query_directory_recv ( struct tevent_req * req ,
2009-07-02 14:28:58 +04:00
TALLOC_CTX * mem_ctx ,
DATA_BLOB * out_output_buffer )
{
NTSTATUS status ;
2015-03-24 17:16:26 +03:00
struct smbd_smb2_query_directory_state * state = tevent_req_data ( req ,
struct smbd_smb2_query_directory_state ) ;
2009-07-02 14:28:58 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
}
* out_output_buffer = state - > out_output_buffer ;
talloc_steal ( mem_ctx , out_output_buffer - > data ) ;
tevent_req_received ( req ) ;
return NT_STATUS_OK ;
}
2016-12-18 10:53:43 +03:00
struct fetch_write_time_state {
connection_struct * conn ;
struct file_id id ;
int info_level ;
char * entry_marshall_buf ;
} ;
static void fetch_write_time_done ( struct tevent_req * subreq ) ;
static struct tevent_req * fetch_write_time_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
connection_struct * conn ,
struct file_id id ,
int info_level ,
char * entry_marshall_buf ,
bool * stop )
{
struct tevent_req * req = NULL ;
struct fetch_write_time_state * state = NULL ;
struct tevent_req * subreq = NULL ;
bool req_queued ;
* stop = false ;
req = tevent_req_create ( mem_ctx , & state , struct fetch_write_time_state ) ;
if ( req = = NULL ) {
return NULL ;
}
* state = ( struct fetch_write_time_state ) {
. conn = conn ,
. id = id ,
. info_level = info_level ,
. entry_marshall_buf = entry_marshall_buf ,
} ;
subreq = fetch_share_mode_send ( state , ev , id , & req_queued ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , fetch_write_time_done , req ) ;
if ( req_queued ) {
* stop = true ;
}
return req ;
}
static void fetch_write_time_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct fetch_write_time_state * state = tevent_req_data (
req , struct fetch_write_time_state ) ;
struct timespec write_time ;
struct share_mode_lock * lck = NULL ;
NTSTATUS status ;
size_t off ;
status = fetch_share_mode_recv ( subreq , state , & lck ) ;
TALLOC_FREE ( subreq ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_FOUND ) ) {
tevent_req_done ( req ) ;
return ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
write_time = get_share_mode_write_time ( lck ) ;
TALLOC_FREE ( lck ) ;
if ( null_timespec ( write_time ) ) {
tevent_req_done ( req ) ;
return ;
}
switch ( state - > info_level ) {
case SMB_FIND_FILE_DIRECTORY_INFO :
case SMB_FIND_FILE_FULL_DIRECTORY_INFO :
case SMB_FIND_FILE_BOTH_DIRECTORY_INFO :
case SMB_FIND_ID_FULL_DIRECTORY_INFO :
case SMB_FIND_ID_BOTH_DIRECTORY_INFO :
off = 24 ;
break ;
default :
DBG_ERR ( " Unsupported info_level [%d] \n " , state - > info_level ) ;
tevent_req_nterror ( req , NT_STATUS_INVALID_LEVEL ) ;
return ;
}
put_long_date_timespec ( state - > conn - > ts_res ,
state - > entry_marshall_buf + off ,
write_time ) ;
tevent_req_done ( req ) ;
return ;
}
static NTSTATUS fetch_write_time_recv ( struct tevent_req * req )
{
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
}
tevent_req_received ( req ) ;
return NT_STATUS_OK ;
}