2005-11-06 02:46:57 +03:00
/*
Unix SMB / CIFS implementation .
Get a struct wb_dom_info for a trusted domain , relying on " our " DC .
Copyright ( C ) Volker Lendecke 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 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2005-11-06 02:46:57 +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/>.
2005-11-06 02:46:57 +03:00
*/
# include "includes.h"
# include "libcli/composite/composite.h"
2006-03-07 14:07:23 +03:00
# include "libcli/resolve/resolve.h"
2006-04-02 16:02:01 +04:00
# include "libcli/security/security.h"
2005-11-06 02:46:57 +03:00
# include "winbind/wb_server.h"
# include "smbd/service_task.h"
2006-03-15 02:35:30 +03:00
# include "librpc/gen_ndr/ndr_netlogon_c.h"
2007-05-26 04:25:22 +04:00
# include "libcli/libcli.h"
2005-11-06 02:46:57 +03:00
struct trusted_dom_info_state {
struct composite_context * ctx ;
struct wbsrv_service * service ;
struct wbsrv_domain * my_domain ;
struct netr_DsRGetDCName d ;
struct netr_GetAnyDCName g ;
struct wb_dom_info * info ;
} ;
static void trusted_dom_info_recv_domain ( struct composite_context * ctx ) ;
2010-03-10 12:03:26 +03:00
static void trusted_dom_info_recv_dsr ( struct tevent_req * subreq ) ;
static void trusted_dom_info_recv_dcname ( struct tevent_req * subreq ) ;
2005-11-06 02:46:57 +03:00
static void trusted_dom_info_recv_dcaddr ( struct composite_context * ctx ) ;
struct composite_context * wb_trusted_dom_info_send ( TALLOC_CTX * mem_ctx ,
struct wbsrv_service * service ,
const char * domain_name ,
const struct dom_sid * sid )
{
struct composite_context * result , * ctx ;
struct trusted_dom_info_state * state ;
2007-04-30 20:52:30 +04:00
result = composite_create ( mem_ctx , service - > task - > event_ctx ) ;
2005-11-06 02:46:57 +03:00
if ( result = = NULL ) goto failed ;
state = talloc ( result , struct trusted_dom_info_state ) ;
if ( state = = NULL ) goto failed ;
state - > ctx = result ;
result - > private_data = state ;
state - > info = talloc_zero ( state , struct wb_dom_info ) ;
if ( state - > info = = NULL ) goto failed ;
state - > service = service ;
state - > info - > sid = dom_sid_dup ( state - > info , sid ) ;
if ( state - > info - > sid = = NULL ) goto failed ;
state - > info - > name = talloc_strdup ( state - > info , domain_name ) ;
if ( state - > info - > name = = NULL ) goto failed ;
ctx = wb_sid2domain_send ( state , service , service - > primary_sid ) ;
if ( ctx = = NULL ) goto failed ;
ctx - > async . fn = trusted_dom_info_recv_domain ;
ctx - > async . private_data = state ;
return result ;
failed :
talloc_free ( result ) ;
return NULL ;
}
static void trusted_dom_info_recv_domain ( struct composite_context * ctx )
{
struct trusted_dom_info_state * state =
talloc_get_type ( ctx - > async . private_data ,
struct trusted_dom_info_state ) ;
2010-03-10 12:03:26 +03:00
struct tevent_req * subreq ;
2005-11-06 02:46:57 +03:00
state - > ctx - > status = wb_sid2domain_recv ( ctx , & state - > my_domain ) ;
if ( ! composite_is_ok ( state - > ctx ) ) return ;
state - > d . in . server_unc =
talloc_asprintf ( state , " \\ \\ %s " ,
2007-05-26 04:25:22 +04:00
dcerpc_server_name ( state - > my_domain - > netlogon_pipe ) ) ;
2005-11-06 02:46:57 +03:00
if ( composite_nomem ( state - > d . in . server_unc ,
state - > ctx ) ) return ;
state - > d . in . domain_name = state - > info - > name ;
state - > d . in . domain_guid = NULL ;
state - > d . in . site_guid = NULL ;
2007-05-26 04:25:22 +04:00
state - > d . in . flags = DS_RETURN_DNS_NAME ;
2008-10-28 03:43:06 +03:00
state - > d . out . info = talloc ( state , struct netr_DsRGetDCNameInfo * ) ;
if ( composite_nomem ( state - > d . out . info , state - > ctx ) ) return ;
2005-11-06 02:46:57 +03:00
2010-03-10 12:03:26 +03:00
subreq = dcerpc_netr_DsRGetDCName_r_send ( state ,
state - > ctx - > event_ctx ,
state - > my_domain - > netlogon_pipe - > binding_handle ,
& state - > d ) ;
if ( composite_nomem ( subreq , state - > ctx ) ) return ;
tevent_req_set_callback ( subreq , trusted_dom_info_recv_dsr , state ) ;
2005-11-06 02:46:57 +03:00
}
/*
* dcerpc_netr_DsRGetDCName has replied
*/
2010-03-10 12:03:26 +03:00
static void trusted_dom_info_recv_dsr ( struct tevent_req * subreq )
2005-11-06 02:46:57 +03:00
{
struct trusted_dom_info_state * state =
2010-03-10 12:03:26 +03:00
tevent_req_callback_data ( subreq ,
struct trusted_dom_info_state ) ;
2005-11-06 02:46:57 +03:00
2010-03-10 12:03:26 +03:00
state - > ctx - > status = dcerpc_netr_DsRGetDCName_r_recv ( subreq , state ) ;
TALLOC_FREE ( subreq ) ;
2005-11-06 02:46:57 +03:00
if ( ! NT_STATUS_IS_OK ( state - > ctx - > status ) ) {
2010-02-27 12:59:14 +03:00
DEBUG ( 9 , ( " dcerpc_netr_DsRGetDCName_recv returned %s \n " ,
2005-11-06 02:46:57 +03:00
nt_errstr ( state - > ctx - > status ) ) ) ;
goto fallback ;
}
state - > ctx - > status =
werror_to_ntstatus ( state - > d . out . result ) ;
if ( ! NT_STATUS_IS_OK ( state - > ctx - > status ) ) {
DEBUG ( 9 , ( " dsrgetdcname returned %s \n " ,
nt_errstr ( state - > ctx - > status ) ) ) ;
goto fallback ;
}
/* Hey, that was easy! */
2010-09-14 11:36:23 +04:00
state - > info - > dc = talloc ( state - > info , struct nbt_dc_name ) ;
state - > info - > dc - > name = talloc_steal ( state - > info ,
2008-10-28 03:43:06 +03:00
( * state - > d . out . info ) - > dc_unc ) ;
2010-09-14 11:36:23 +04:00
if ( * state - > info - > dc - > name = = ' \\ ' ) state - > info - > dc - > name + + ;
if ( * state - > info - > dc - > name = = ' \\ ' ) state - > info - > dc - > name + + ;
2005-11-06 02:46:57 +03:00
2010-09-14 11:36:23 +04:00
state - > info - > dc - > address = talloc_steal ( state - > info ,
2008-10-28 03:43:06 +03:00
( * state - > d . out . info ) - > dc_address ) ;
2010-09-14 11:36:23 +04:00
if ( * state - > info - > dc - > address = = ' \\ ' ) state - > info - > dc - > address + + ;
if ( * state - > info - > dc - > address = = ' \\ ' ) state - > info - > dc - > address + + ;
2005-11-06 02:46:57 +03:00
state - > info - > dns_name = talloc_steal ( state - > info ,
2008-10-28 03:43:06 +03:00
( * state - > d . out . info ) - > domain_name ) ;
2005-11-06 02:46:57 +03:00
composite_done ( state - > ctx ) ;
return ;
fallback :
state - > g . in . logon_server = talloc_asprintf (
state , " \\ \\ %s " ,
dcerpc_server_name ( state - > my_domain - > netlogon_pipe ) ) ;
state - > g . in . domainname = state - > info - > name ;
2008-10-28 03:23:04 +03:00
state - > g . out . dcname = talloc ( state , const char * ) ;
2005-11-06 02:46:57 +03:00
2010-03-10 12:03:26 +03:00
subreq = dcerpc_netr_GetAnyDCName_r_send ( state ,
state - > ctx - > event_ctx ,
state - > my_domain - > netlogon_pipe - > binding_handle ,
& state - > g ) ;
if ( composite_nomem ( subreq , state - > ctx ) ) return ;
2005-11-06 02:46:57 +03:00
2010-03-10 12:03:26 +03:00
tevent_req_set_callback ( subreq , trusted_dom_info_recv_dcname , state ) ;
2005-11-06 02:46:57 +03:00
}
2010-03-10 12:03:26 +03:00
static void trusted_dom_info_recv_dcname ( struct tevent_req * subreq )
2005-11-06 02:46:57 +03:00
{
struct trusted_dom_info_state * state =
2010-03-10 12:03:26 +03:00
tevent_req_callback_data ( subreq ,
struct trusted_dom_info_state ) ;
2005-11-06 02:46:57 +03:00
struct composite_context * ctx ;
struct nbt_name name ;
2010-03-10 12:03:26 +03:00
state - > ctx - > status = dcerpc_netr_GetAnyDCName_r_recv ( subreq , state ) ;
TALLOC_FREE ( subreq ) ;
2005-11-06 02:46:57 +03:00
if ( ! composite_is_ok ( state - > ctx ) ) return ;
state - > ctx - > status = werror_to_ntstatus ( state - > g . out . result ) ;
if ( ! composite_is_ok ( state - > ctx ) ) return ;
2007-05-26 04:25:22 +04:00
/* Hey, that was easy! */
2010-09-14 11:36:23 +04:00
state - > info - > dc = talloc ( state - > info , struct nbt_dc_name ) ;
state - > info - > dc - > name = talloc_steal ( state - > info ,
2008-10-28 03:23:04 +03:00
* ( state - > g . out . dcname ) ) ;
2010-09-14 11:36:23 +04:00
if ( * state - > info - > dc - > name = = ' \\ ' ) state - > info - > dc - > name + + ;
if ( * state - > info - > dc - > name = = ' \\ ' ) state - > info - > dc - > name + + ;
2005-11-06 02:46:57 +03:00
2010-09-14 11:36:23 +04:00
make_nbt_name ( & name , state - > info - > dc - > name , 0x20 ) ;
2010-07-16 08:32:42 +04:00
ctx = resolve_name_send ( lpcfg_resolve_context ( state - > service - > task - > lp_ctx ) , state ,
2007-12-10 20:41:19 +03:00
& name , state - > service - > task - > event_ctx ) ;
2005-11-06 02:46:57 +03:00
composite_continue ( state - > ctx , ctx , trusted_dom_info_recv_dcaddr ,
state ) ;
}
static void trusted_dom_info_recv_dcaddr ( struct composite_context * ctx )
{
struct trusted_dom_info_state * state =
talloc_get_type ( ctx - > async . private_data ,
struct trusted_dom_info_state ) ;
state - > ctx - > status = resolve_name_recv ( ctx , state - > info ,
2010-09-14 11:36:23 +04:00
& state - > info - > dc - > address ) ;
2005-11-06 02:46:57 +03:00
if ( ! composite_is_ok ( state - > ctx ) ) return ;
composite_done ( state - > ctx ) ;
}
NTSTATUS wb_trusted_dom_info_recv ( struct composite_context * ctx ,
TALLOC_CTX * mem_ctx ,
struct wb_dom_info * * result )
{
NTSTATUS status = composite_wait ( ctx ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
struct trusted_dom_info_state * state =
talloc_get_type ( ctx - > private_data ,
struct trusted_dom_info_state ) ;
* result = talloc_steal ( mem_ctx , state - > info ) ;
}
talloc_free ( ctx ) ;
return status ;
}
NTSTATUS wb_trusted_dom_info ( TALLOC_CTX * mem_ctx ,
struct wbsrv_service * service ,
const char * domain_name ,
const struct dom_sid * sid ,
struct wb_dom_info * * result )
{
struct composite_context * ctx =
wb_trusted_dom_info_send ( mem_ctx , service , domain_name , sid ) ;
return wb_trusted_dom_info_recv ( ctx , mem_ctx , result ) ;
}