2003-10-28 13:24:13 +03:00
/*
Unix SMB / CIFS implementation .
2003-11-24 15:40:47 +03:00
dcerpc over SMB transport
2003-10-28 13:24:13 +03:00
2003-11-03 09:22:45 +03:00
Copyright ( C ) Tim Potter 2003
Copyright ( C ) Andrew Tridgell 2003
2003-10-28 13:24:13 +03:00
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 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-10-28 13:24:13 +03: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 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-10-28 13:24:13 +03:00
*/
# include "includes.h"
2013-09-16 13:51:44 +04:00
# include "system/filesys.h"
# include <tevent.h>
# include "lib/tsocket/tsocket.h"
# include "libcli/smb/smb_constants.h"
# include "libcli/smb/smbXcli_base.h"
# include "libcli/smb/tstream_smbXcli_np.h"
2004-11-01 04:03:22 +03:00
# include "libcli/raw/libcliraw.h"
2013-09-19 20:50:27 +04:00
# include "libcli/smb2/smb2.h"
2006-03-18 18:42:57 +03:00
# include "librpc/rpc/dcerpc.h"
2008-04-02 06:53:27 +04:00
# include "librpc/rpc/dcerpc_proto.h"
2013-09-16 13:51:44 +04:00
# include "libcli/composite/composite.h"
2003-10-28 13:24:13 +03:00
2020-08-07 23:27:39 +03:00
# undef strncasecmp
2003-11-24 14:45:33 +03:00
/* transport private information used by SMB pipe transport */
struct smb_private {
2012-08-01 11:09:34 +04:00
DATA_BLOB session_key ;
2013-09-16 13:51:44 +04:00
2013-09-19 20:16:16 +04:00
/*
* these are needed to open a secondary connection
*/
struct smbXcli_conn * conn ;
struct smbXcli_session * session ;
struct smbXcli_tcon * tcon ;
uint32_t timeout_msec ;
2003-11-24 14:45:33 +03:00
} ;
2003-11-04 12:10:31 +03:00
2004-09-09 18:31:27 +04:00
/*
fetch the user session key
*/
2011-03-11 10:27:00 +03:00
static NTSTATUS smb_session_key ( struct dcecli_connection * c , DATA_BLOB * session_key )
2004-09-09 18:31:27 +04:00
{
2013-09-16 13:51:44 +04:00
struct smb_private * smb = talloc_get_type_abort (
c - > transport . private_data , struct smb_private ) ;
2004-09-09 18:31:27 +04:00
2009-09-17 20:09:48 +04:00
if ( smb = = NULL ) return NT_STATUS_CONNECTION_DISCONNECTED ;
2012-08-01 11:09:34 +04:00
if ( smb - > session_key . length = = 0 ) {
return NT_STATUS_NO_USER_SESSION_KEY ;
2004-09-09 18:31:27 +04:00
}
2012-08-01 11:09:34 +04:00
* session_key = smb - > session_key ;
return NT_STATUS_OK ;
2004-09-09 18:31:27 +04:00
}
2013-09-16 13:51:44 +04:00
struct dcerpc_pipe_open_smb_state {
2011-03-11 10:27:00 +03:00
struct dcecli_connection * c ;
2005-10-03 13:00:36 +04:00
struct composite_context * ctx ;
2013-09-16 13:51:44 +04:00
const char * fname ;
struct smb_private * smb ;
2005-10-03 13:00:36 +04:00
} ;
2013-09-16 13:51:44 +04:00
static void dcerpc_pipe_open_smb_done ( struct tevent_req * subreq ) ;
2005-10-03 13:00:36 +04:00
2013-09-19 20:16:16 +04:00
struct composite_context * dcerpc_pipe_open_smb_send ( struct dcecli_connection * c ,
struct smbXcli_conn * conn ,
struct smbXcli_session * session ,
struct smbXcli_tcon * tcon ,
uint32_t timeout_msec ,
const char * pipe_name )
2003-11-24 14:45:33 +03:00
{
2005-10-03 13:00:36 +04:00
struct composite_context * ctx ;
2013-09-16 13:51:44 +04:00
struct dcerpc_pipe_open_smb_state * state ;
2013-09-19 20:16:16 +04:00
uint16_t pid = 0 ;
2013-09-16 13:51:44 +04:00
struct tevent_req * subreq ;
2007-02-26 08:37:19 +03:00
2006-07-30 21:45:11 +04:00
ctx = composite_create ( c , c - > event_ctx ) ;
if ( ctx = = NULL ) return NULL ;
r5902: A rather large change...
I wanted to add a simple 'workstation' argument to the DCERPC
authenticated binding calls, but this patch kind of grew from there.
With SCHANNEL, the 'workstation' name (the netbios name of the client)
matters, as this is what ties the session between the NETLOGON ops and
the SCHANNEL bind. This changes a lot of files, and these will again
be changed when jelmer does the credentials work.
I also correct some schannel IDL to distinguish between workstation
names and account names. The distinction matters for domain trust
accounts.
Issues in handling this (issues with lifetime of talloc pointers)
caused me to change the 'creds_CredentialsState' and 'struct
dcerpc_binding' pointers to always be talloc()ed pointers.
In the schannel DB, we now store both the domain and computername, and
query on both. This should ensure we fault correctly when the domain
is specified incorrectly in the SCHANNEL bind.
In the RPC-SCHANNEL test, I finally fixed a bug that vl pointed out,
where the comment claimed we re-used a connection, but in fact we made
a new connection.
This was achived by breaking apart some of the
dcerpc_secondary_connection() logic.
The addition of workstation handling was also propogated to NTLMSSP
and GENSEC, for completeness.
The RPC-SAMSYNC test has been cleaned up a little, using a loop over
usernames/passwords rather than manually expanded tests. This will be
expanded further (the code in #if 0 in this patch) to use a newly
created user account for testing.
In making this test pass test_rpc.sh, I found a bug in the RPC-ECHO
server, caused by the removal of [ref] and the assoicated pointer from
the IDL. This has been re-added, until the underlying pidl issues are
solved.
(This used to be commit 824289dcc20908ddec957a4a892a103eec2da9b9)
2005-03-19 11:34:43 +03:00
2013-09-16 13:51:44 +04:00
state = talloc ( ctx , struct dcerpc_pipe_open_smb_state ) ;
2006-07-30 21:45:11 +04:00
if ( composite_nomem ( state , ctx ) ) return ctx ;
ctx - > private_data = state ;
2005-10-03 13:00:36 +04:00
state - > c = c ;
state - > ctx = ctx ;
if ( ( strncasecmp ( pipe_name , " /pipe/ " , 6 ) = = 0 ) | |
( strncasecmp ( pipe_name , " \\ pipe \\ " , 6 ) = = 0 ) ) {
pipe_name + = 6 ;
}
2013-09-16 13:51:44 +04:00
if ( ( strncasecmp ( pipe_name , " / " , 1 ) = = 0 ) | |
( strncasecmp ( pipe_name , " \\ " , 1 ) = = 0 ) ) {
pipe_name + = 1 ;
}
state - > fname = talloc_strdup ( state , pipe_name ) ;
if ( composite_nomem ( state - > fname , ctx ) ) return ctx ;
state - > smb = talloc_zero ( state , struct smb_private ) ;
if ( composite_nomem ( state - > smb , ctx ) ) return ctx ;
2013-09-19 20:16:16 +04:00
state - > smb - > conn = conn ;
state - > smb - > session = session ;
state - > smb - > tcon = tcon ;
state - > smb - > timeout_msec = timeout_msec ;
2013-09-16 13:51:44 +04:00
2013-09-23 15:54:38 +04:00
state - > c - > server_name = strupper_talloc ( state - > c ,
2013-09-16 13:51:44 +04:00
smbXcli_conn_remote_name ( conn ) ) ;
2013-09-23 15:54:38 +04:00
if ( composite_nomem ( state - > c - > server_name , ctx ) ) return ctx ;
2013-09-16 13:51:44 +04:00
ctx - > status = smbXcli_session_application_key ( session ,
state - > smb ,
& state - > smb - > session_key ) ;
if ( NT_STATUS_EQUAL ( ctx - > status , NT_STATUS_NO_USER_SESSION_KEY ) ) {
state - > smb - > session_key = data_blob_null ;
ctx - > status = NT_STATUS_OK ;
}
if ( ! composite_is_ok ( ctx ) ) return ctx ;
subreq = tstream_smbXcli_np_open_send ( state , c - > event_ctx ,
conn , session , tcon , pid ,
timeout_msec , state - > fname ) ;
if ( composite_nomem ( subreq , ctx ) ) return ctx ;
tevent_req_set_callback ( subreq , dcerpc_pipe_open_smb_done , state ) ;
2005-10-03 13:00:36 +04:00
return ctx ;
}
2013-09-16 13:51:44 +04:00
static void dcerpc_pipe_open_smb_done ( struct tevent_req * subreq )
2005-10-03 13:00:36 +04:00
{
2013-09-16 13:51:44 +04:00
struct dcerpc_pipe_open_smb_state * state =
tevent_req_callback_data ( subreq ,
struct dcerpc_pipe_open_smb_state ) ;
2005-10-03 13:00:36 +04:00
struct composite_context * ctx = state - > ctx ;
2011-03-11 10:27:00 +03:00
struct dcecli_connection * c = state - > c ;
2020-08-20 13:18:21 +03:00
uint16_t enc_cipher ;
2013-09-16 13:51:44 +04:00
ctx - > status = tstream_smbXcli_np_open_recv ( subreq ,
state - > smb ,
2013-09-24 14:32:50 +04:00
& state - > c - > transport . stream ) ;
2013-09-16 13:51:44 +04:00
TALLOC_FREE ( subreq ) ;
2006-07-30 21:45:11 +04:00
if ( ! composite_is_ok ( ctx ) ) return ;
2003-11-24 14:45:33 +03:00
2013-09-24 14:32:50 +04:00
state - > c - > transport . write_queue =
tevent_queue_create ( state - > c , " dcerpc_smb write queue " ) ;
if ( composite_nomem ( state - > c - > transport . write_queue , ctx ) ) return ;
2013-09-16 13:51:44 +04:00
2003-11-24 14:45:33 +03:00
/*
fill in the transport methods
*/
2006-03-25 14:39:09 +03:00
c - > transport . transport = NCACN_NP ;
2007-05-16 18:52:54 +04:00
c - > transport . private_data = NULL ;
2006-03-25 14:39:09 +03:00
2013-09-16 13:51:44 +04:00
/*
* Windows uses 4280 for ncacn_np ,
* so we also use it , this is what our
* tstream_smbXcli_np code relies on .
*/
c - > srv_max_xmit_frag = 4280 ;
c - > srv_max_recv_frag = 4280 ;
2007-04-27 09:45:53 +04:00
2013-09-16 13:51:44 +04:00
/* Over-ride the default session key with the SMB session key */
c - > security_state . session_key = smb_session_key ;
2012-08-01 11:09:34 +04:00
2020-08-20 13:18:21 +03:00
enc_cipher = smb2cli_session_get_encryption_cipher ( state - > smb - > session ) ;
switch ( enc_cipher ) {
case SMB2_ENCRYPTION_AES128_CCM :
case SMB2_ENCRYPTION_AES128_GCM :
c - > transport . encrypted = true ;
break ;
default :
c - > transport . encrypted = false ;
}
2013-09-16 13:51:44 +04:00
c - > transport . private_data = talloc_move ( c , & state - > smb ) ;
2003-11-24 14:45:33 +03:00
2006-07-30 21:45:11 +04:00
composite_done ( ctx ) ;
2005-10-03 13:00:36 +04:00
}
NTSTATUS dcerpc_pipe_open_smb_recv ( struct composite_context * c )
{
NTSTATUS status = composite_wait ( c ) ;
talloc_free ( c ) ;
return status ;
}
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS dcerpc_pipe_open_smb ( struct dcerpc_pipe * p ,
2013-09-19 20:16:16 +04:00
struct smbcli_tree * t ,
2005-10-03 13:00:36 +04:00
const char * pipe_name )
{
2013-09-19 20:16:16 +04:00
struct smbXcli_conn * conn ;
struct smbXcli_session * session ;
struct smbXcli_tcon * tcon ;
struct composite_context * ctx ;
conn = t - > session - > transport - > conn ;
session = t - > session - > smbXcli ;
tcon = t - > smbXcli ;
smb1cli_tcon_set_id ( tcon , t - > tid ) ;
/* if we don't have a binding on this pipe yet, then create one */
if ( p - > binding = = NULL ) {
2014-02-12 15:24:29 +04:00
struct dcerpc_binding * b ;
2013-09-19 20:16:16 +04:00
NTSTATUS status ;
const char * r = smbXcli_conn_remote_name ( conn ) ;
char * str ;
SMB_ASSERT ( r ! = NULL ) ;
str = talloc_asprintf ( p , " ncacn_np:%s " , r ) ;
2013-09-19 20:50:27 +04:00
if ( str = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2014-02-12 15:24:29 +04:00
status = dcerpc_parse_binding ( p , str , & b ) ;
2013-09-19 20:50:27 +04:00
talloc_free ( str ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
2014-02-12 15:24:29 +04:00
p - > binding = b ;
2013-09-19 20:50:27 +04:00
}
ctx = dcerpc_pipe_open_smb_send ( p - > conn ,
2014-01-15 16:17:42 +04:00
conn , session , tcon ,
DCERPC_REQUEST_TIMEOUT * 1000 ,
2013-09-19 20:50:27 +04:00
pipe_name ) ;
if ( ctx = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
return dcerpc_pipe_open_smb_recv ( ctx ) ;
}
_PUBLIC_ NTSTATUS dcerpc_pipe_open_smb2 ( struct dcerpc_pipe * p ,
struct smb2_tree * t ,
const char * pipe_name )
{
struct smbXcli_conn * conn ;
struct smbXcli_session * session ;
struct smbXcli_tcon * tcon ;
struct composite_context * ctx ;
conn = t - > session - > transport - > conn ;
session = t - > session - > smbXcli ;
tcon = t - > smbXcli ;
/* if we don't have a binding on this pipe yet, then create one */
if ( p - > binding = = NULL ) {
2014-02-12 15:24:29 +04:00
struct dcerpc_binding * b ;
2013-09-19 20:50:27 +04:00
NTSTATUS status ;
const char * r = smbXcli_conn_remote_name ( conn ) ;
char * str ;
SMB_ASSERT ( r ! = NULL ) ;
str = talloc_asprintf ( p , " ncacn_np:%s " , r ) ;
2013-09-19 20:16:16 +04:00
if ( str = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2014-02-12 15:24:29 +04:00
status = dcerpc_parse_binding ( p , str , & b ) ;
2013-09-19 20:16:16 +04:00
talloc_free ( str ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
2014-02-12 15:24:29 +04:00
p - > binding = b ;
2013-09-19 20:16:16 +04:00
}
ctx = dcerpc_pipe_open_smb_send ( p - > conn ,
2014-01-15 16:17:42 +04:00
conn , session , tcon ,
DCERPC_REQUEST_TIMEOUT * 1000 ,
2013-09-19 20:16:16 +04:00
pipe_name ) ;
if ( ctx = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2005-10-03 13:00:36 +04:00
return dcerpc_pipe_open_smb_recv ( ctx ) ;
2003-11-24 14:45:33 +03:00
}
2004-01-20 09:07:09 +03:00
2013-09-17 09:16:29 +04:00
struct composite_context * dcerpc_secondary_smb_send ( struct dcecli_connection * c1 ,
2013-09-19 20:36:19 +04:00
struct dcecli_connection * c2 ,
2013-09-17 09:16:29 +04:00
const char * pipe_name )
{
struct smb_private * smb ;
if ( c1 - > transport . transport ! = NCACN_NP ) return NULL ;
smb = talloc_get_type ( c1 - > transport . private_data , struct smb_private ) ;
if ( ! smb ) return NULL ;
2013-09-19 20:36:19 +04:00
return dcerpc_pipe_open_smb_send ( c2 ,
2013-09-19 20:16:16 +04:00
smb - > conn ,
smb - > session ,
smb - > tcon ,
smb - > timeout_msec ,
pipe_name ) ;
2013-09-17 09:16:29 +04:00
}
NTSTATUS dcerpc_secondary_smb_recv ( struct composite_context * c )
{
return dcerpc_pipe_open_smb_recv ( c ) ;
}