2003-11-26 03:36:17 +00:00
/*
Unix SMB / CIFS implementation .
dcerpc utility functions
Copyright ( C ) Andrew Tridgell 2003
2004-10-18 15:18:05 +00:00
Copyright ( C ) Jelmer Vernooij 2004
r6028: A MAJOR update to intergrate the new credentails system fully with
GENSEC, and to pull SCHANNEL into GENSEC, by making it less 'special'.
GENSEC now no longer has it's own handling of 'set username' etc,
instead it uses cli_credentials calls.
In order to link the credentails code right though Samba, a lot of
interfaces have changed to remove 'username, domain, password'
arguments, and these have been replaced with a single 'struct
cli_credentials'.
In the session setup code, a new parameter 'workgroup' contains the
client/server current workgroup, which seems unrelated to the
authentication exchange (it was being filled in from the auth info).
This allows in particular kerberos to only call back for passwords
when it actually needs to perform the kinit.
The kerberos code has been modified not to use the SPNEGO provided
'principal name' (in the mechListMIC), but to instead use the name the
host was connected to as. This better matches Microsoft behaviour,
is more secure and allows better use of standard kerberos functions.
To achieve this, I made changes to our socket code so that the
hostname (before name resolution) is now recorded on the socket.
In schannel, most of the code from librpc/rpc/dcerpc_schannel.c is now
in libcli/auth/schannel.c, and it looks much more like a standard
GENSEC module. The actual sign/seal code moved to
libcli/auth/schannel_sign.c in a previous commit.
The schannel credentails structure is now merged with the rest of the
credentails, as many of the values (username, workstation, domain)
where already present there. This makes handling this in a generic
manner much easier, as there is no longer a custom entry-point.
The auth_domain module continues to be developed, but is now just as
functional as auth_winbind. The changes here are consequential to the
schannel changes.
The only removed function at this point is the RPC-LOGIN test
(simulating the load of a WinXP login), which needs much more work to
clean it up (it contains copies of too much code from all over the
torture suite, and I havn't been able to penetrate its 'structure').
Andrew Bartlett
(This used to be commit 2301a4b38a21aa60917973451687063d83d18d66)
2005-03-24 04:14:06 +00:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2005
2006-03-10 16:52:46 +00:00
Copyright ( C ) Rafal Szczesniak 2006
2003-11-26 03:36:17 +00: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 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2003-11-26 03:36:17 +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/>.
2003-11-26 03:36:17 +00:00
*/
# include "includes.h"
2006-03-11 14:18:00 +00:00
# include "lib/events/events.h"
2006-03-09 23:21:49 +00:00
# include "libcli/composite/composite.h"
2006-03-14 23:35:30 +00:00
# include "librpc/gen_ndr/ndr_epmapper_c.h"
2005-07-28 00:27:28 +00:00
# include "librpc/gen_ndr/ndr_dcerpc.h"
# include "librpc/gen_ndr/ndr_misc.h"
2008-04-02 04:53:27 +02:00
# include "librpc/rpc/dcerpc_proto.h"
2006-03-14 01:29:56 +00:00
# include "auth/credentials/credentials.h"
2007-09-08 12:42:09 +00:00
# include "param/param.h"
2003-12-15 03:29:55 +00:00
2005-07-02 11:12:33 +00:00
/*
find a dcerpc call on an interface by name
*/
2007-08-19 20:46:45 +00:00
const struct ndr_interface_call * dcerpc_iface_find_call ( const struct ndr_interface_table * iface ,
2007-08-18 10:30:40 +00:00
const char * name )
2005-07-02 11:12:33 +00:00
{
int i ;
for ( i = 0 ; i < iface - > num_calls ; i + + ) {
if ( strcmp ( iface - > calls [ i ] . name , name ) = = 0 ) {
return & iface - > calls [ i ] ;
}
}
return NULL ;
}
2003-12-14 01:09:10 +00:00
/*
2005-06-05 23:05:37 +00:00
push a ncacn_packet into a blob , potentially with auth info
2003-12-14 01:09:10 +00:00
*/
2005-06-05 23:39:00 +00:00
NTSTATUS ncacn_push_auth ( DATA_BLOB * blob , TALLOC_CTX * mem_ctx ,
2008-02-21 17:54:24 +01:00
struct smb_iconv_convenience * iconv_convenience ,
2005-06-05 23:05:37 +00:00
struct ncacn_packet * pkt ,
2003-12-17 02:06:44 +00:00
struct dcerpc_auth * auth_info )
2003-12-14 01:09:10 +00:00
{
struct ndr_push * ndr ;
2007-11-09 19:24:51 +01:00
enum ndr_err_code ndr_err ;
2003-12-14 01:09:10 +00:00
2008-02-21 17:54:24 +01:00
ndr = ndr_push_init_ctx ( mem_ctx , iconv_convenience ) ;
2003-12-14 01:09:10 +00:00
if ( ! ndr ) {
return NT_STATUS_NO_MEMORY ;
}
2003-12-17 02:06:44 +00:00
if ( ! ( pkt - > drep [ 0 ] & DCERPC_DREP_LE ) ) {
2003-12-16 09:02:58 +00:00
ndr - > flags | = LIBNDR_FLAG_BIGENDIAN ;
}
2007-02-23 11:00:20 +00:00
if ( pkt - > pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID ) {
2004-11-08 02:12:15 +00:00
ndr - > flags | = LIBNDR_FLAG_OBJECT_PRESENT ;
}
2003-12-14 01:09:10 +00:00
if ( auth_info ) {
pkt - > auth_length = auth_info - > credentials . length ;
} else {
pkt - > auth_length = 0 ;
}
2007-11-09 19:24:51 +01:00
ndr_err = ndr_push_ncacn_packet ( ndr , NDR_SCALARS | NDR_BUFFERS , pkt ) ;
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_err ) ) {
return ndr_map_error2ntstatus ( ndr_err ) ;
2003-12-14 01:09:10 +00:00
}
if ( auth_info ) {
2007-11-09 19:24:51 +01:00
ndr_err = ndr_push_dcerpc_auth ( ndr , NDR_SCALARS | NDR_BUFFERS , auth_info ) ;
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_err ) ) {
return ndr_map_error2ntstatus ( ndr_err ) ;
2007-11-05 06:58:57 +01:00
}
2003-12-14 01:09:10 +00:00
}
* blob = ndr_push_blob ( ndr ) ;
/* fill in the frag length */
2003-12-16 09:02:58 +00:00
dcerpc_set_frag_length ( blob , blob - > length ) ;
2003-12-14 01:09:10 +00:00
return NT_STATUS_OK ;
}
2003-12-15 03:29:55 +00:00
2006-03-09 23:21:49 +00:00
struct epm_map_binding_state {
struct dcerpc_binding * binding ;
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table ;
2006-03-09 23:21:49 +00:00
struct dcerpc_pipe * pipe ;
2006-03-10 10:31:47 +00:00
struct policy_handle handle ;
struct GUID guid ;
2006-03-09 23:21:49 +00:00
struct epm_twr_t twr ;
struct epm_twr_t * twr_r ;
struct epm_Map r ;
} ;
static void continue_epm_recv_binding ( struct composite_context * ctx ) ;
static void continue_epm_map ( struct rpc_request * req ) ;
2006-03-11 19:06:47 +00:00
/*
Stage 2 of epm_map_binding : Receive connected rpc pipe and send endpoint
mapping rpc request
*/
2006-03-09 23:21:49 +00:00
static void continue_epm_recv_binding ( struct composite_context * ctx )
{
struct rpc_request * map_req ;
struct composite_context * c = talloc_get_type ( ctx - > async . private_data ,
struct composite_context ) ;
struct epm_map_binding_state * s = talloc_get_type ( c - > private_data ,
struct epm_map_binding_state ) ;
2006-03-11 19:06:47 +00:00
/* receive result of rpc pipe connect request */
2006-03-09 23:21:49 +00:00
c - > status = dcerpc_pipe_connect_b_recv ( ctx , c , & s - > pipe ) ;
if ( ! composite_is_ok ( c ) ) return ;
2006-11-22 16:55:21 +00:00
s - > pipe - > conn - > flags | = DCERPC_NDR_REF_ALLOC ;
2006-03-11 19:06:47 +00:00
/* prepare requested binding parameters */
2006-03-26 00:59:17 +00:00
s - > binding - > object = s - > table - > syntax_id ;
2006-03-09 23:21:49 +00:00
c - > status = dcerpc_binding_build_tower ( s - > pipe , s - > binding , & s - > twr . tower ) ;
if ( ! composite_is_ok ( c ) ) return ;
/* with some nice pretty paper around it of course */
2006-03-10 10:31:47 +00:00
s - > r . in . object = & s - > guid ;
2006-03-09 23:21:49 +00:00
s - > r . in . map_tower = & s - > twr ;
2006-03-10 10:31:47 +00:00
s - > r . in . entry_handle = & s - > handle ;
2006-03-09 23:21:49 +00:00
s - > r . in . max_towers = 1 ;
2006-03-10 10:31:47 +00:00
s - > r . out . entry_handle = & s - > handle ;
2006-03-09 23:21:49 +00:00
2006-03-11 19:06:47 +00:00
/* send request for an endpoint mapping - a rpc request on connected pipe */
2006-03-09 23:21:49 +00:00
map_req = dcerpc_epm_Map_send ( s - > pipe , c , & s - > r ) ;
if ( composite_nomem ( map_req , c ) ) return ;
composite_continue_rpc ( c , map_req , continue_epm_map , c ) ;
}
2006-03-11 19:06:47 +00:00
/*
Stage 3 of epm_map_binding : Receive endpoint mapping and provide binding details
*/
2006-03-09 23:21:49 +00:00
static void continue_epm_map ( struct rpc_request * req )
{
2007-05-16 14:52:54 +00:00
struct composite_context * c = talloc_get_type ( req - > async . private_data ,
2006-03-09 23:21:49 +00:00
struct composite_context ) ;
struct epm_map_binding_state * s = talloc_get_type ( c - > private_data ,
struct epm_map_binding_state ) ;
2006-03-11 19:06:47 +00:00
/* receive result of a rpc request */
2006-03-09 23:21:49 +00:00
c - > status = dcerpc_ndr_request_recv ( req ) ;
if ( ! composite_is_ok ( c ) ) return ;
2006-03-11 19:06:47 +00:00
/* check the details */
2006-11-22 16:55:21 +00:00
if ( s - > r . out . result ! = 0 | | * s - > r . out . num_towers ! = 1 ) {
2006-03-09 23:21:49 +00:00
composite_error ( c , NT_STATUS_PORT_UNREACHABLE ) ;
return ;
}
s - > twr_r = s - > r . out . towers [ 0 ] . twr ;
if ( s - > twr_r = = NULL ) {
composite_error ( c , NT_STATUS_PORT_UNREACHABLE ) ;
return ;
}
if ( s - > twr_r - > tower . num_floors ! = s - > twr . tower . num_floors | |
s - > twr_r - > tower . floors [ 3 ] . lhs . protocol ! = s - > twr . tower . floors [ 3 ] . lhs . protocol ) {
composite_error ( c , NT_STATUS_PORT_UNREACHABLE ) ;
return ;
}
2006-03-11 19:06:47 +00:00
/* get received endpoint */
2006-03-09 23:21:49 +00:00
s - > binding - > endpoint = talloc_reference ( s - > binding ,
dcerpc_floor_get_rhs_data ( c , & s - > twr_r - > tower . floors [ 3 ] ) ) ;
2006-07-31 08:14:27 +00:00
if ( composite_nomem ( s - > binding - > endpoint , c ) ) return ;
2006-03-09 23:21:49 +00:00
composite_done ( c ) ;
}
2006-03-11 19:06:47 +00:00
/*
Request for endpoint mapping of dcerpc binding - try to request for endpoint
unless there is default one .
*/
2006-03-09 23:21:49 +00:00
struct composite_context * dcerpc_epm_map_binding_send ( TALLOC_CTX * mem_ctx ,
struct dcerpc_binding * binding ,
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table ,
2007-12-07 02:37:04 +01:00
struct event_context * ev ,
struct loadparm_context * lp_ctx )
2006-03-09 23:21:49 +00:00
{
struct composite_context * c ;
struct epm_map_binding_state * s ;
struct composite_context * pipe_connect_req ;
2006-03-09 23:54:50 +00:00
struct cli_credentials * anon_creds ;
2006-03-09 23:21:49 +00:00
NTSTATUS status ;
struct dcerpc_binding * epmapper_binding ;
int i ;
2006-07-31 08:14:27 +00:00
if ( ev = = NULL ) {
2008-04-17 01:09:33 +02:00
return NULL ;
2006-07-31 08:14:27 +00:00
}
2006-03-09 23:21:49 +00:00
/* composite context allocation and setup */
2006-07-31 08:14:27 +00:00
c = composite_create ( mem_ctx , ev ) ;
if ( c = = NULL ) {
return NULL ;
}
2006-03-09 23:21:49 +00:00
s = talloc_zero ( c , struct epm_map_binding_state ) ;
if ( composite_nomem ( s , c ) ) return c ;
c - > private_data = s ;
2006-03-11 14:18:00 +00:00
2006-03-09 23:21:49 +00:00
s - > binding = binding ;
s - > table = table ;
2006-03-11 19:06:47 +00:00
/* anonymous credentials for rpc connection used to get endpoint mapping */
2006-03-09 23:54:50 +00:00
anon_creds = cli_credentials_init ( mem_ctx ) ;
2006-03-09 23:21:49 +00:00
cli_credentials_set_anonymous ( anon_creds ) ;
2006-03-11 19:06:47 +00:00
/*
First , check if there is a default endpoint specified in the IDL
*/
2007-08-29 02:01:56 +00:00
if ( table ! = NULL ) {
2006-03-09 23:21:49 +00:00
struct dcerpc_binding * default_binding ;
/* Find one of the default pipes for this interface */
for ( i = 0 ; i < table - > endpoints - > count ; i + + ) {
status = dcerpc_parse_binding ( mem_ctx , table - > endpoints - > names [ i ] , & default_binding ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
2007-08-29 02:01:56 +00:00
if ( binding - > transport = = NCA_UNKNOWN )
binding - > transport = default_binding - > transport ;
if ( default_binding - > transport = = binding - > transport & &
default_binding - > endpoint ) {
2006-03-09 23:21:49 +00:00
binding - > endpoint = talloc_reference ( binding , default_binding - > endpoint ) ;
talloc_free ( default_binding ) ;
composite_done ( c ) ;
return c ;
} else {
talloc_free ( default_binding ) ;
}
}
}
}
epmapper_binding = talloc_zero ( c , struct dcerpc_binding ) ;
if ( composite_nomem ( epmapper_binding , c ) ) return c ;
2006-03-11 19:06:47 +00:00
/* basic endpoint mapping data */
2006-12-22 19:38:32 +00:00
epmapper_binding - > transport = binding - > transport ;
epmapper_binding - > host = talloc_reference ( epmapper_binding , binding - > host ) ;
epmapper_binding - > target_hostname = epmapper_binding - > host ;
epmapper_binding - > options = NULL ;
epmapper_binding - > flags = 0 ;
2007-02-23 11:00:20 +00:00
epmapper_binding - > assoc_group_id = 0 ;
2006-12-22 19:38:32 +00:00
epmapper_binding - > endpoint = NULL ;
2006-03-09 23:21:49 +00:00
2006-03-11 19:06:47 +00:00
/* initiate rpc pipe connection */
2007-08-29 02:01:56 +00:00
pipe_connect_req = dcerpc_pipe_connect_b_send ( c , epmapper_binding ,
& ndr_table_epmapper ,
2007-12-07 02:37:04 +01:00
anon_creds , c - > event_ctx ,
lp_ctx ) ;
2006-03-09 23:21:49 +00:00
if ( composite_nomem ( pipe_connect_req , c ) ) return c ;
composite_continue ( c , pipe_connect_req , continue_epm_recv_binding , c ) ;
return c ;
}
2006-03-11 19:06:47 +00:00
/*
Receive result of endpoint mapping request
*/
2006-03-09 23:21:49 +00:00
NTSTATUS dcerpc_epm_map_binding_recv ( struct composite_context * c )
{
NTSTATUS status = composite_wait ( c ) ;
talloc_free ( c ) ;
return status ;
}
2006-03-11 19:06:47 +00:00
/*
Get endpoint mapping for rpc connection
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS dcerpc_epm_map_binding ( TALLOC_CTX * mem_ctx , struct dcerpc_binding * binding ,
2007-12-07 02:37:04 +01:00
const struct ndr_interface_table * table , struct event_context * ev ,
struct loadparm_context * lp_ctx )
2004-10-24 22:46:47 +00:00
{
2006-03-11 14:18:00 +00:00
struct composite_context * c ;
2004-10-24 22:46:47 +00:00
2007-12-07 02:37:04 +01:00
c = dcerpc_epm_map_binding_send ( mem_ctx , binding , table , ev , lp_ctx ) ;
2006-03-11 14:18:00 +00:00
return dcerpc_epm_map_binding_recv ( c ) ;
2004-10-24 22:46:47 +00:00
}
2003-12-15 03:29:55 +00:00
2006-03-09 23:21:49 +00:00
struct pipe_auth_state {
struct dcerpc_pipe * pipe ;
struct dcerpc_binding * binding ;
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table ;
2007-12-07 02:37:04 +01:00
struct loadparm_context * lp_ctx ;
2006-03-09 23:21:49 +00:00
struct cli_credentials * credentials ;
} ;
2006-05-07 19:55:14 +00:00
static void continue_auth_schannel ( struct composite_context * ctx ) ;
static void continue_auth ( struct composite_context * ctx ) ;
static void continue_auth_none ( struct composite_context * ctx ) ;
static void continue_ntlmssp_connection ( struct composite_context * ctx ) ;
static void continue_spnego_after_wrong_pass ( struct composite_context * ctx ) ;
2006-03-09 23:21:49 +00:00
2006-03-10 16:52:46 +00:00
/*
Stage 2 of pipe_auth : Receive result of schannel bind request
*/
2006-03-09 23:21:49 +00:00
static void continue_auth_schannel ( struct composite_context * ctx )
{
struct composite_context * c = talloc_get_type ( ctx - > async . private_data ,
struct composite_context ) ;
c - > status = dcerpc_bind_auth_schannel_recv ( ctx ) ;
if ( ! composite_is_ok ( c ) ) return ;
composite_done ( c ) ;
}
2006-05-07 19:55:14 +00:00
/*
Stage 2 of pipe_auth : Receive result of authenticated bind request
*/
static void continue_auth ( struct composite_context * ctx )
{
struct composite_context * c = talloc_get_type ( ctx - > async . private_data ,
struct composite_context ) ;
c - > status = dcerpc_bind_auth_recv ( ctx ) ;
if ( ! composite_is_ok ( c ) ) return ;
composite_done ( c ) ;
}
2006-05-04 10:03:41 +00:00
/*
Stage 2 of pipe_auth : Receive result of authenticated bind request , but handle fallbacks :
SPNEGO - > NTLMSSP
*/
2006-05-07 19:55:14 +00:00
static void continue_auth_auto ( struct composite_context * ctx )
2006-05-04 10:03:41 +00:00
{
struct composite_context * c = talloc_get_type ( ctx - > async . private_data ,
struct composite_context ) ;
2006-07-30 19:22:39 +00:00
struct pipe_auth_state * s = talloc_get_type ( c - > private_data , struct pipe_auth_state ) ;
struct composite_context * sec_conn_req ;
2006-05-04 10:03:41 +00:00
2006-05-07 19:55:14 +00:00
c - > status = dcerpc_bind_auth_recv ( ctx ) ;
if ( NT_STATUS_EQUAL ( c - > status , NT_STATUS_INVALID_PARAMETER ) ) {
2006-07-30 19:22:39 +00:00
/*
* Retry with NTLMSSP auth as fallback
* send a request for secondary rpc connection
*/
2006-05-04 10:03:41 +00:00
sec_conn_req = dcerpc_secondary_connection_send ( s - > pipe ,
s - > binding ) ;
2006-05-07 19:55:14 +00:00
composite_continue ( c , sec_conn_req , continue_ntlmssp_connection , c ) ;
2006-05-04 10:03:41 +00:00
return ;
2006-05-07 19:55:14 +00:00
} else if ( NT_STATUS_EQUAL ( c - > status , NT_STATUS_LOGON_FAILURE ) ) {
2006-05-04 10:03:41 +00:00
if ( cli_credentials_wrong_password ( s - > credentials ) ) {
2006-07-30 19:22:39 +00:00
/*
* Retry SPNEGO with a better password
* send a request for secondary rpc connection
*/
2006-05-04 10:03:41 +00:00
sec_conn_req = dcerpc_secondary_connection_send ( s - > pipe ,
s - > binding ) ;
2006-05-07 19:55:14 +00:00
composite_continue ( c , sec_conn_req , continue_spnego_after_wrong_pass , c ) ;
2006-07-30 19:22:39 +00:00
return ;
2006-05-04 10:03:41 +00:00
}
}
if ( ! composite_is_ok ( c ) ) return ;
2006-07-31 08:14:27 +00:00
2006-05-04 10:03:41 +00:00
composite_done ( c ) ;
}
/*
2006-05-07 19:55:14 +00:00
Stage 3 of pipe_auth ( fallback to NTLMSSP case ) : Receive secondary
rpc connection ( the first one can ' t be used any more , due to the
bind nak ) and perform authenticated bind request
*/
static void continue_ntlmssp_connection ( struct composite_context * ctx )
{
struct composite_context * c ;
struct pipe_auth_state * s ;
struct composite_context * auth_req ;
struct dcerpc_pipe * p2 ;
2006-05-04 10:03:41 +00:00
2006-05-07 19:55:14 +00:00
c = talloc_get_type ( ctx - > async . private_data , struct composite_context ) ;
s = talloc_get_type ( c - > private_data , struct pipe_auth_state ) ;
/* receive secondary rpc connection */
c - > status = dcerpc_secondary_connection_recv ( ctx , & p2 ) ;
2006-07-31 08:14:27 +00:00
if ( ! composite_is_ok ( c ) ) return ;
2006-05-07 19:55:14 +00:00
talloc_steal ( s , p2 ) ;
talloc_steal ( p2 , s - > pipe ) ;
s - > pipe = p2 ;
/* initiate a authenticated bind */
auth_req = dcerpc_bind_auth_send ( c , s - > pipe , s - > table ,
2007-12-07 02:37:04 +01:00
s - > credentials , s - > lp_ctx ,
DCERPC_AUTH_TYPE_NTLMSSP ,
2006-05-07 19:55:14 +00:00
dcerpc_auth_level ( s - > pipe - > conn ) ,
s - > table - > authservices - > names [ 0 ] ) ;
composite_continue ( c , auth_req , continue_auth , c ) ;
}
/*
Stage 3 of pipe_auth ( retry on wrong password ) : Receive secondary
rpc connection ( the first one can ' t be used any more , due to the
bind nak ) and perform authenticated bind request
2006-05-04 10:03:41 +00:00
*/
2006-05-07 19:55:14 +00:00
static void continue_spnego_after_wrong_pass ( struct composite_context * ctx )
2006-05-04 10:03:41 +00:00
{
struct composite_context * c ;
struct pipe_auth_state * s ;
struct composite_context * auth_req ;
struct dcerpc_pipe * p2 ;
c = talloc_get_type ( ctx - > async . private_data , struct composite_context ) ;
s = talloc_get_type ( c - > private_data , struct pipe_auth_state ) ;
/* receive secondary rpc connection */
c - > status = dcerpc_secondary_connection_recv ( ctx , & p2 ) ;
2006-07-31 08:14:27 +00:00
if ( ! composite_is_ok ( c ) ) return ;
2006-05-07 19:55:14 +00:00
talloc_steal ( s , p2 ) ;
talloc_steal ( p2 , s - > pipe ) ;
2006-05-04 10:03:41 +00:00
s - > pipe = p2 ;
/* initiate a authenticated bind */
auth_req = dcerpc_bind_auth_send ( c , s - > pipe , s - > table ,
2007-12-07 02:37:04 +01:00
s - > credentials , s - > lp_ctx , DCERPC_AUTH_TYPE_SPNEGO ,
2006-05-04 10:03:41 +00:00
dcerpc_auth_level ( s - > pipe - > conn ) ,
s - > table - > authservices - > names [ 0 ] ) ;
2006-05-07 19:55:14 +00:00
composite_continue ( c , auth_req , continue_auth , c ) ;
2006-05-04 10:03:41 +00:00
}
2006-03-09 23:21:49 +00:00
2006-03-10 16:52:46 +00:00
/*
Stage 2 of pipe_auth : Receive result of non - authenticated bind request
*/
2006-05-07 19:55:14 +00:00
static void continue_auth_none ( struct composite_context * ctx )
2006-03-09 23:21:49 +00:00
{
struct composite_context * c = talloc_get_type ( ctx - > async . private_data ,
struct composite_context ) ;
c - > status = dcerpc_bind_auth_none_recv ( ctx ) ;
if ( ! composite_is_ok ( c ) ) return ;
composite_done ( c ) ;
}
2006-03-10 16:52:46 +00:00
/*
Request to perform an authenticated bind if required . Authentication
is determined using credentials passed and binding flags .
*/
2006-03-09 23:21:49 +00:00
struct composite_context * dcerpc_pipe_auth_send ( struct dcerpc_pipe * p ,
struct dcerpc_binding * binding ,
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table ,
2007-12-07 02:37:04 +01:00
struct cli_credentials * credentials ,
struct loadparm_context * lp_ctx )
2006-03-09 23:21:49 +00:00
{
struct composite_context * c ;
struct pipe_auth_state * s ;
struct composite_context * auth_schannel_req ;
struct composite_context * auth_req ;
struct composite_context * auth_none_req ;
2006-05-04 10:03:41 +00:00
struct dcerpc_connection * conn ;
2006-07-31 07:29:44 +00:00
uint8_t auth_type ;
2006-03-09 23:21:49 +00:00
/* composite context allocation and setup */
2006-07-31 08:14:27 +00:00
c = composite_create ( p , p - > conn - > event_ctx ) ;
2006-03-09 23:21:49 +00:00
if ( c = = NULL ) return NULL ;
s = talloc_zero ( c , struct pipe_auth_state ) ;
if ( composite_nomem ( s , c ) ) return c ;
c - > private_data = s ;
2006-03-10 16:52:46 +00:00
/* store parameters in state structure */
2006-03-09 23:21:49 +00:00
s - > binding = binding ;
s - > table = table ;
s - > credentials = credentials ;
s - > pipe = p ;
2007-12-07 02:37:04 +01:00
s - > lp_ctx = lp_ctx ;
2006-03-09 23:21:49 +00:00
2006-05-04 10:03:41 +00:00
conn = s - > pipe - > conn ;
conn - > flags = binding - > flags ;
2006-03-09 23:21:49 +00:00
/* remember the binding string for possible secondary connections */
2006-05-04 10:03:41 +00:00
conn - > binding_string = dcerpc_binding_string ( p , binding ) ;
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
if ( cli_credentials_is_anonymous ( s - > credentials ) ) {
auth_none_req = dcerpc_bind_auth_none_send ( c , s - > pipe , s - > table ) ;
composite_continue ( c , auth_none_req , continue_auth_none , c ) ;
return c ;
}
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
if ( ( binding - > flags & DCERPC_SCHANNEL ) & &
! cli_credentials_get_netlogon_creds ( s - > credentials ) ) {
2006-03-09 23:21:49 +00:00
/* If we don't already have netlogon credentials for
* the schannel bind , then we have to get these
* first */
auth_schannel_req = dcerpc_bind_auth_schannel_send ( c , s - > pipe , s - > table ,
2007-12-07 02:37:04 +01:00
s - > credentials , s - > lp_ctx ,
2006-05-04 10:03:41 +00:00
dcerpc_auth_level ( conn ) ) ;
2006-03-09 23:21:49 +00:00
composite_continue ( c , auth_schannel_req , continue_auth_schannel , c ) ;
2006-07-31 07:29:44 +00:00
return c ;
}
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
/*
* we rely on the already authenticated CIFS connection
* if not doing sign or seal
*/
if ( conn - > transport . transport = = NCACN_NP & &
! ( s - > binding - > flags & ( DCERPC_SIGN | DCERPC_SEAL ) ) ) {
auth_none_req = dcerpc_bind_auth_none_send ( c , s - > pipe , s - > table ) ;
composite_continue ( c , auth_none_req , continue_auth_none , c ) ;
return c ;
}
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
/* Perform an authenticated DCE-RPC bind
*/
if ( ! ( conn - > flags & ( DCERPC_SIGN | DCERPC_SEAL ) ) ) {
/*
we are doing an authenticated connection ,
but not using sign or seal . We must force
the CONNECT dcerpc auth type as a NONE auth
type doesn ' t allow authentication
information to be passed .
*/
conn - > flags | = DCERPC_CONNECT ;
}
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
if ( s - > binding - > flags & DCERPC_AUTH_SPNEGO ) {
auth_type = DCERPC_AUTH_TYPE_SPNEGO ;
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
} else if ( s - > binding - > flags & DCERPC_AUTH_KRB5 ) {
auth_type = DCERPC_AUTH_TYPE_KRB5 ;
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
} else if ( s - > binding - > flags & DCERPC_SCHANNEL ) {
auth_type = DCERPC_AUTH_TYPE_SCHANNEL ;
2006-03-09 23:21:49 +00:00
2006-07-31 07:29:44 +00:00
} else if ( s - > binding - > flags & DCERPC_AUTH_NTLM ) {
auth_type = DCERPC_AUTH_TYPE_NTLMSSP ;
} else {
/* try SPNEGO with fallback to NTLMSSP */
2006-03-09 23:21:49 +00:00
auth_req = dcerpc_bind_auth_send ( c , s - > pipe , s - > table ,
2007-12-07 02:37:04 +01:00
s - > credentials , s - > lp_ctx , DCERPC_AUTH_TYPE_SPNEGO ,
2006-05-04 10:03:41 +00:00
dcerpc_auth_level ( conn ) ,
2006-03-09 23:21:49 +00:00
s - > table - > authservices - > names [ 0 ] ) ;
2006-07-31 07:29:44 +00:00
composite_continue ( c , auth_req , continue_auth_auto , c ) ;
return c ;
2006-03-09 23:21:49 +00:00
}
2006-07-31 07:29:44 +00:00
auth_req = dcerpc_bind_auth_send ( c , s - > pipe , s - > table ,
2007-12-07 02:37:04 +01:00
s - > credentials , s - > lp_ctx , auth_type ,
2006-07-31 07:29:44 +00:00
dcerpc_auth_level ( conn ) ,
s - > table - > authservices - > names [ 0 ] ) ;
composite_continue ( c , auth_req , continue_auth , c ) ;
2006-03-09 23:21:49 +00:00
return c ;
}
2006-03-10 16:52:46 +00:00
/*
Receive result of authenticated bind request on dcerpc pipe
2006-05-04 10:03:41 +00:00
This returns * p , which may be different to the one originally
supllied , as it rebinds to a new pipe due to authentication fallback
2006-03-10 16:52:46 +00:00
*/
2006-05-07 19:55:14 +00:00
NTSTATUS dcerpc_pipe_auth_recv ( struct composite_context * c , TALLOC_CTX * mem_ctx ,
2006-05-04 10:03:41 +00:00
struct dcerpc_pipe * * p )
2006-03-09 23:21:49 +00:00
{
NTSTATUS status ;
struct pipe_auth_state * s = talloc_get_type ( c - > private_data ,
struct pipe_auth_state ) ;
status = composite_wait ( c ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2006-03-26 00:59:17 +00:00
char * uuid_str = GUID_string ( s - > pipe , & s - > table - > syntax_id . uuid ) ;
2006-03-09 23:21:49 +00:00
DEBUG ( 0 , ( " Failed to bind to uuid %s - %s \n " , uuid_str , nt_errstr ( status ) ) ) ;
talloc_free ( uuid_str ) ;
2006-05-04 10:03:41 +00:00
} else {
2006-05-07 19:55:14 +00:00
talloc_steal ( mem_ctx , s - > pipe ) ;
2006-05-04 10:03:41 +00:00
* p = s - > pipe ;
2006-03-09 23:21:49 +00:00
}
talloc_free ( c ) ;
return status ;
}
2005-01-10 07:14:12 +00:00
/*
2006-03-10 16:52:46 +00:00
Perform an authenticated bind if needed - sync version
2006-05-04 10:03:41 +00:00
This may change * p , as it rebinds to a new pipe due to authentication fallback
2005-01-10 07:14:12 +00:00
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS dcerpc_pipe_auth ( TALLOC_CTX * mem_ctx ,
2006-05-07 19:55:14 +00:00
struct dcerpc_pipe * * p ,
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 08:34:43 +00:00
struct dcerpc_binding * binding ,
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table ,
2007-12-07 02:37:04 +01:00
struct cli_credentials * credentials ,
struct loadparm_context * lp_ctx )
2005-01-10 07:14:12 +00:00
{
2006-03-10 12:59:05 +00:00
struct composite_context * c ;
2005-01-10 07:14:12 +00:00
2007-12-07 02:37:04 +01:00
c = dcerpc_pipe_auth_send ( * p , binding , table , credentials , lp_ctx ) ;
2006-05-07 19:55:14 +00:00
return dcerpc_pipe_auth_recv ( c , mem_ctx , p ) ;
2005-01-10 07:14:12 +00:00
}
2005-01-09 08:34:05 +00:00
NTSTATUS dcerpc_generic_session_key ( struct dcerpc_connection * c ,
2004-09-25 12:48:56 +00:00
DATA_BLOB * session_key )
2004-09-11 15:11:36 +00:00
{
/* this took quite a few CPU cycles to find ... */
2004-10-21 19:54:38 +00:00
session_key - > data = discard_const_p ( unsigned char , " SystemLibraryDTC " ) ;
2004-09-11 15:11:36 +00:00
session_key - > length = 16 ;
return NT_STATUS_OK ;
}
2004-04-07 07:20:53 +00:00
/*
2004-09-11 15:11:36 +00:00
fetch the user session key - may be default ( above ) or the SMB session key
2008-07-23 16:19:54 +10:00
The key is always truncated to 16 bytes
2004-04-07 07:20:53 +00:00
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS dcerpc_fetch_session_key ( struct dcerpc_pipe * p ,
2008-07-23 16:19:54 +10:00
DATA_BLOB * session_key )
2004-04-07 07:20:53 +00:00
{
2008-07-23 16:19:54 +10:00
NTSTATUS status ;
status = p - > conn - > security_state . session_key ( p - > conn , session_key ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
session_key - > length = MIN ( session_key - > length , 16 ) ;
return NT_STATUS_OK ;
2004-04-07 07:20:53 +00:00
}
2004-06-14 08:12:50 +00:00
/*
log a rpc packet in a format suitable for ndrdump . This is especially useful
for sealed packets , where ethereal cannot easily see the contents
this triggers on a debug level of > = 10
*/
2008-11-02 00:26:04 +01:00
_PUBLIC_ void dcerpc_log_packet ( const char * lockdir ,
const struct ndr_interface_table * ndr ,
2008-01-02 15:00:58 -06:00
uint32_t opnum , uint32_t flags ,
DATA_BLOB * pkt )
2004-06-14 08:12:50 +00:00
{
const int num_examples = 20 ;
int i ;
2008-11-02 00:26:04 +01:00
if ( lockdir = = NULL ) return ;
2004-06-14 08:12:50 +00:00
for ( i = 0 ; i < num_examples ; i + + ) {
char * name = NULL ;
asprintf ( & name , " %s/rpclog/%s-%u.%d.%s " ,
2008-11-02 00:26:04 +01:00
lockdir , ndr - > name , opnum , i ,
2004-06-14 08:12:50 +00:00
( flags & NDR_IN ) ? " in " : " out " ) ;
if ( name = = NULL ) {
return ;
}
2005-02-10 06:36:30 +00:00
if ( ! file_exist ( name ) ) {
2004-06-14 08:12:50 +00:00
if ( file_save ( name , pkt - > data , pkt - > length ) ) {
DEBUG ( 10 , ( " Logged rpc packet to %s \n " , name ) ) ;
}
free ( name ) ;
break ;
}
free ( name ) ;
}
}
2005-01-09 09:38:16 +00:00
/*
create a secondary context from a primary connection
this uses dcerpc_alter_context ( ) to create a new dcerpc context_id
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS dcerpc_secondary_context ( struct dcerpc_pipe * p ,
2005-01-09 11:32:12 +00:00
struct dcerpc_pipe * * pp2 ,
2007-08-19 20:46:45 +00:00
const struct ndr_interface_table * table )
2005-01-09 09:38:16 +00:00
{
NTSTATUS status ;
struct dcerpc_pipe * p2 ;
p2 = talloc_zero ( p , struct dcerpc_pipe ) ;
if ( p2 = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
p2 - > conn = talloc_reference ( p2 , p - > conn ) ;
2005-06-12 02:42:40 +00:00
p2 - > request_timeout = p - > request_timeout ;
2005-01-09 09:38:16 +00:00
2005-01-09 11:32:12 +00:00
p2 - > context_id = + + p - > conn - > next_context_id ;
2005-01-09 09:38:16 +00:00
2006-03-26 00:59:17 +00:00
p2 - > syntax = table - > syntax_id ;
2005-01-09 09:38:16 +00:00
2005-12-27 16:47:09 +00:00
p2 - > transfer_syntax = ndr_transfer_syntax ;
2005-01-09 09:38:16 +00:00
2007-02-23 13:42:00 +00:00
p2 - > binding = talloc_reference ( p2 , p - > binding ) ;
2005-01-09 09:38:16 +00:00
status = dcerpc_alter_context ( p2 , p2 , & p2 - > syntax , & p2 - > transfer_syntax ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( p2 ) ;
return status ;
}
* pp2 = p2 ;
2007-11-04 16:16:18 +01:00
return NT_STATUS_OK ;
2005-01-09 09:38:16 +00:00
}