2005-11-19 06:39:12 +00:00
/*
Unix SMB / CIFS implementation .
SMB2 client find calls
Copyright ( C ) Andrew Tridgell 2005
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
2007-07-10 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2005-11-19 06:39:12 +00:00
( 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
2007-07-10 02:07:03 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2005-11-19 06:39:12 +00:00
*/
# include "includes.h"
# include "libcli/raw/libcliraw.h"
2008-04-02 04:53:27 +02:00
# include "libcli/raw/raw_proto.h"
2005-11-19 06:39:12 +00:00
# include "libcli/smb2/smb2.h"
# include "libcli/smb2/smb2_calls.h"
/*
send a find request
*/
struct smb2_request * smb2_find_send ( struct smb2_tree * tree , struct smb2_find * io )
{
struct smb2_request * req ;
NTSTATUS status ;
2015-03-24 07:16:26 -07:00
req = smb2_request_init_tree ( tree , SMB2_OP_QUERY_DIRECTORY , 0x20 , true , 0 ) ;
2005-11-19 06:39:12 +00:00
if ( req = = NULL ) return NULL ;
2018-03-22 10:07:49 +01:00
req - > credit_charge = ( MAX ( io - > in . max_response_size , 1 ) - 1 ) / 65536 + 1 ;
2005-11-19 06:39:12 +00:00
SCVAL ( req - > out . body , 0x02 , io - > in . level ) ;
SCVAL ( req - > out . body , 0x03 , io - > in . continue_flags ) ;
2008-05-15 20:46:10 +10:00
SIVAL ( req - > out . body , 0x04 , io - > in . file_index ) ;
2006-05-20 10:46:38 +00:00
smb2_push_handle ( req - > out . body + 0x08 , & io - > in . file . handle ) ;
2005-11-19 06:39:12 +00:00
status = smb2_push_o16s16_string ( & req - > out , 0x18 , io - > in . pattern ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( req ) ;
return NULL ;
}
2005-11-25 08:24:36 +00:00
SIVAL ( req - > out . body , 0x1C , io - > in . max_response_size ) ;
2005-11-19 06:39:12 +00:00
smb2_transport_send ( req ) ;
return req ;
}
/*
recv a find reply
*/
NTSTATUS smb2_find_recv ( struct smb2_request * req , TALLOC_CTX * mem_ctx ,
struct smb2_find * io )
{
NTSTATUS status ;
if ( ! smb2_request_receive ( req ) | |
smb2_request_is_error ( req ) ) {
return smb2_request_destroy ( req ) ;
}
2007-10-06 22:28:14 +00:00
SMB2_CHECK_PACKET_RECV ( req , 0x08 , true ) ;
2005-11-19 06:39:12 +00:00
status = smb2_pull_o16s32_blob ( & req - > in , mem_ctx ,
req - > in . body + 0x02 , & io - > out . blob ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
return smb2_request_destroy ( req ) ;
}
/*
sync find request
*/
NTSTATUS smb2_find ( struct smb2_tree * tree , TALLOC_CTX * mem_ctx ,
2005-11-22 13:13:17 +00:00
struct smb2_find * io )
2005-11-19 06:39:12 +00:00
{
struct smb2_request * req = smb2_find_send ( tree , io ) ;
return smb2_find_recv ( req , mem_ctx , io ) ;
}
2005-11-22 13:13:17 +00:00
/*
2023-08-03 14:34:51 +02:00
a variant of smb2_find_recv that parses the resulting blob into
2005-11-22 13:13:17 +00:00
smb_search_data structures
*/
NTSTATUS smb2_find_level_recv ( struct smb2_request * req , TALLOC_CTX * mem_ctx ,
2010-01-05 09:42:54 -08:00
uint8_t level , unsigned int * count ,
2005-11-22 13:13:17 +00:00
union smb_search_data * * io )
{
struct smb2_find f ;
NTSTATUS status ;
DATA_BLOB b ;
2006-07-06 08:00:24 +00:00
enum smb_search_data_level smb_level ;
2010-01-05 09:42:54 -08:00
unsigned int next_ofs = 0 ;
2005-11-22 13:13:17 +00:00
switch ( level ) {
case SMB2_FIND_DIRECTORY_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_DIRECTORY_INFO ;
2005-11-22 13:13:17 +00:00
break ;
case SMB2_FIND_FULL_DIRECTORY_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO ;
2005-11-22 13:13:17 +00:00
break ;
case SMB2_FIND_BOTH_DIRECTORY_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO ;
2005-11-22 13:13:17 +00:00
break ;
case SMB2_FIND_NAME_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_NAME_INFO ;
2005-11-22 13:13:17 +00:00
break ;
case SMB2_FIND_ID_FULL_DIRECTORY_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO ;
2005-11-22 13:13:17 +00:00
break ;
case SMB2_FIND_ID_BOTH_DIRECTORY_INFO :
2006-07-06 08:00:24 +00:00
smb_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO ;
2005-11-22 13:13:17 +00:00
break ;
default :
return NT_STATUS_INVALID_INFO_CLASS ;
}
status = smb2_find_recv ( req , mem_ctx , & f ) ;
NT_STATUS_NOT_OK_RETURN ( status ) ;
b = f . out . blob ;
* io = NULL ;
* count = 0 ;
do {
union smb_search_data * io2 ;
io2 = talloc_realloc ( mem_ctx , * io , union smb_search_data , ( * count ) + 1 ) ;
if ( io2 = = NULL ) {
data_blob_free ( & f . out . blob ) ;
talloc_free ( * io ) ;
return NT_STATUS_NO_MEMORY ;
}
* io = io2 ;
status = smb_raw_search_common ( * io , smb_level , & b , ( * io ) + ( * count ) ,
& next_ofs , STR_UNICODE ) ;
if ( NT_STATUS_IS_OK ( status ) & &
next_ofs > = b . length ) {
data_blob_free ( & f . out . blob ) ;
talloc_free ( * io ) ;
return NT_STATUS_INFO_LENGTH_MISMATCH ;
}
( * count ) + + ;
b = data_blob_const ( b . data + next_ofs , b . length - next_ofs ) ;
} while ( NT_STATUS_IS_OK ( status ) & & next_ofs ! = 0 ) ;
data_blob_free ( & f . out . blob ) ;
return NT_STATUS_OK ;
}
/*
2023-08-03 14:34:51 +02:00
a variant of smb2_find that parses the resulting blob into
2005-11-22 13:13:17 +00:00
smb_search_data structures
*/
NTSTATUS smb2_find_level ( struct smb2_tree * tree , TALLOC_CTX * mem_ctx ,
struct smb2_find * f ,
2010-01-05 09:42:54 -08:00
unsigned int * count , union smb_search_data * * io )
2005-11-22 13:13:17 +00:00
{
struct smb2_request * req ;
req = smb2_find_send ( tree , f ) ;
return smb2_find_level_recv ( req , mem_ctx , f - > in . level , count , io ) ;
}