2005-10-19 13:45:44 +00:00
/*
Unix SMB / CIFS implementation .
Find and init a domain struct for a SID
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 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2005-10-19 13:45:44 +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-10-19 13:45:44 +00:00
*/
# include "includes.h"
2012-08-23 13:14:17 +02:00
# include <tevent.h>
# include "../lib/util/tevent_ntstatus.h"
2005-10-19 13:45:44 +00:00
# include "libcli/composite/composite.h"
# include "winbind/wb_server.h"
# include "smbd/service_task.h"
2006-04-02 12:02:01 +00:00
# include "libcli/security/security.h"
2008-10-11 21:31:42 +02:00
# include "../lib/util/dlinklist.h"
2007-09-08 12:42:09 +00:00
# include "param/param.h"
2005-10-19 13:45:44 +00:00
static struct wbsrv_domain * find_domain_from_sid ( struct wbsrv_service * service ,
const struct dom_sid * sid )
{
struct wbsrv_domain * domain ;
for ( domain = service - > domains ; domain ! = NULL ; domain = domain - > next ) {
2005-11-05 23:46:57 +00:00
if ( dom_sid_equal ( domain - > info - > sid , sid ) ) {
2005-10-19 13:45:44 +00:00
break ;
}
2005-11-05 23:46:57 +00:00
if ( dom_sid_in_domain ( domain - > info - > sid , sid ) ) {
2005-10-19 13:45:44 +00:00
break ;
}
}
return domain ;
}
2012-08-23 13:14:17 +02:00
struct wb_sid2domain_state {
2005-10-19 13:45:44 +00:00
struct wbsrv_service * service ;
2012-08-23 13:14:17 +02:00
struct dom_sid sid ;
2005-10-19 13:45:44 +00:00
2005-11-05 23:46:57 +00:00
struct wbsrv_domain * domain ;
2005-10-19 13:45:44 +00:00
} ;
2012-08-23 13:14:17 +02:00
static void wb_sid2domain_recv_dom_info ( struct composite_context * ctx ) ;
static void wb_sid2domain_recv_name ( struct composite_context * ctx ) ;
static void wb_sid2domain_recv_trusted_dom_info ( struct composite_context * ctx ) ;
static void wb_sid2domain_recv_init ( struct composite_context * ctx ) ;
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
static struct tevent_req * _wb_sid2domain_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct wbsrv_service * service ,
const struct dom_sid * sid )
2005-10-19 13:45:44 +00:00
{
2012-08-23 13:14:17 +02:00
struct tevent_req * req ;
struct wb_sid2domain_state * state ;
struct composite_context * ctx ;
2011-01-11 22:38:27 +03:00
DEBUG ( 5 , ( " wb_sid2domain_send called \n " ) ) ;
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
req = tevent_req_create ( mem_ctx , & state ,
struct wb_sid2domain_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2005-10-19 13:45:44 +00:00
state - > service = service ;
2012-08-23 13:14:17 +02:00
state - > sid = * sid ;
2005-10-19 13:45:44 +00:00
2005-11-05 23:46:57 +00:00
state - > domain = find_domain_from_sid ( service , sid ) ;
if ( state - > domain ! = NULL ) {
2012-08-23 13:14:17 +02:00
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
2005-10-19 13:45:44 +00:00
}
2005-11-05 23:46:57 +00:00
if ( dom_sid_equal ( service - > primary_sid , sid ) | |
dom_sid_in_domain ( service - > primary_sid , sid ) ) {
2010-09-14 17:36:23 +10:00
ctx = wb_get_dom_info_send ( state , service ,
lpcfg_workgroup ( service - > task - > lp_ctx ) ,
lpcfg_realm ( service - > task - > lp_ctx ) ,
2005-11-05 23:46:57 +00:00
service - > primary_sid ) ;
2012-08-23 13:14:17 +02:00
if ( tevent_req_nomem ( ctx , req ) ) {
return tevent_req_post ( req , ev ) ;
}
ctx - > async . fn = wb_sid2domain_recv_dom_info ;
ctx - > async . private_data = req ;
2007-08-01 00:38:53 +00:00
2012-08-23 13:14:17 +02:00
return req ;
}
2005-10-19 13:45:44 +00:00
2013-06-15 23:01:44 +10:00
if ( dom_sid_equal ( & global_sid_Builtin , sid ) | |
dom_sid_in_domain ( & global_sid_Builtin , sid ) ) {
ctx = wb_get_dom_info_send ( state , service ,
" BUILTIN " , NULL ,
& global_sid_Builtin ) ;
if ( tevent_req_nomem ( ctx , req ) ) {
return tevent_req_post ( req , ev ) ;
}
ctx - > async . fn = wb_sid2domain_recv_dom_info ;
ctx - > async . private_data = req ;
return req ;
}
2012-08-23 13:14:17 +02:00
ctx = wb_cmd_lookupsid_send ( state , service , & state - > sid ) ;
if ( tevent_req_nomem ( ctx , req ) ) {
return tevent_req_post ( req , ev ) ;
}
ctx - > async . fn = wb_sid2domain_recv_name ;
ctx - > async . private_data = req ;
2005-11-05 23:46:57 +00:00
2012-08-23 13:14:17 +02:00
return req ;
2005-10-19 13:45:44 +00:00
}
2012-08-23 13:14:17 +02:00
static void wb_sid2domain_recv_dom_info ( struct composite_context * ctx )
2005-10-19 13:45:44 +00:00
{
2012-08-23 13:14:17 +02:00
struct tevent_req * req =
talloc_get_type_abort ( ctx - > async . private_data ,
struct tevent_req ) ;
struct wb_sid2domain_state * state =
tevent_req_data ( req ,
struct wb_sid2domain_state ) ;
2005-11-05 23:46:57 +00:00
struct wb_dom_info * info ;
2012-08-23 13:14:17 +02:00
NTSTATUS status ;
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
status = wb_get_dom_info_recv ( ctx , state , & info ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2005-10-19 13:45:44 +00:00
2005-11-05 23:46:57 +00:00
ctx = wb_init_domain_send ( state , state - > service , info ) ;
2012-08-23 13:14:17 +02:00
if ( tevent_req_nomem ( ctx , req ) ) {
return ;
}
ctx - > async . fn = wb_sid2domain_recv_init ;
ctx - > async . private_data = req ;
2005-11-05 09:34:07 +00:00
}
2012-08-23 13:14:17 +02:00
static void wb_sid2domain_recv_name ( struct composite_context * ctx )
2005-11-05 09:34:07 +00:00
{
2012-08-23 13:14:17 +02:00
struct tevent_req * req =
talloc_get_type_abort ( ctx - > async . private_data ,
struct tevent_req ) ;
struct wb_sid2domain_state * state =
tevent_req_data ( req ,
struct wb_sid2domain_state ) ;
2005-11-05 23:46:57 +00:00
struct wb_sid_object * name ;
2012-08-23 13:14:17 +02:00
NTSTATUS status ;
2005-11-05 09:34:07 +00:00
2012-08-23 13:14:17 +02:00
status = wb_cmd_lookupsid_recv ( ctx , state , & name ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2005-11-05 09:34:07 +00:00
2005-11-05 23:46:57 +00:00
if ( name - > type = = SID_NAME_UNKNOWN ) {
2012-08-23 13:14:17 +02:00
tevent_req_nterror ( req , NT_STATUS_NO_SUCH_DOMAIN ) ;
2005-11-05 23:46:57 +00:00
return ;
2005-11-05 09:34:07 +00:00
}
2005-11-05 23:46:57 +00:00
if ( name - > type ! = SID_NAME_DOMAIN ) {
2012-08-23 13:14:17 +02:00
state - > sid . num_auths - = 1 ;
2005-11-05 23:46:57 +00:00
}
2005-11-05 09:34:07 +00:00
2005-11-05 23:46:57 +00:00
ctx = wb_trusted_dom_info_send ( state , state - > service , name - > domain ,
2012-08-23 13:14:17 +02:00
& state - > sid ) ;
if ( tevent_req_nomem ( ctx , req ) ) {
return ;
}
ctx - > async . fn = wb_sid2domain_recv_trusted_dom_info ;
ctx - > async . private_data = req ;
2005-10-19 13:45:44 +00:00
}
2012-08-23 13:14:17 +02:00
static void wb_sid2domain_recv_trusted_dom_info ( struct composite_context * ctx )
2005-10-19 13:45:44 +00:00
{
2012-08-23 13:14:17 +02:00
struct tevent_req * req =
talloc_get_type_abort ( ctx - > async . private_data ,
struct tevent_req ) ;
struct wb_sid2domain_state * state =
tevent_req_data ( req ,
struct wb_sid2domain_state ) ;
2005-11-05 23:46:57 +00:00
struct wb_dom_info * info ;
2012-08-23 13:14:17 +02:00
NTSTATUS status ;
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
status = wb_trusted_dom_info_recv ( ctx , state , & info ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2005-10-19 13:45:44 +00:00
2005-11-05 23:46:57 +00:00
ctx = wb_init_domain_send ( state , state - > service , info ) ;
2012-08-23 13:14:17 +02:00
if ( tevent_req_nomem ( ctx , req ) ) {
return ;
}
ctx - > async . fn = wb_sid2domain_recv_init ;
ctx - > async . private_data = req ;
2005-10-19 13:45:44 +00:00
}
2012-08-23 13:14:17 +02:00
static void wb_sid2domain_recv_init ( struct composite_context * ctx )
2005-10-19 13:45:44 +00:00
{
2012-08-23 13:14:17 +02:00
struct tevent_req * req =
talloc_get_type_abort ( ctx - > async . private_data ,
struct tevent_req ) ;
struct wb_sid2domain_state * state =
tevent_req_data ( req ,
struct wb_sid2domain_state ) ;
2005-11-05 23:46:57 +00:00
struct wbsrv_domain * existing ;
2012-08-23 13:14:17 +02:00
NTSTATUS status ;
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
status = wb_init_domain_recv ( ctx , state , & state - > domain ) ;
if ( tevent_req_nterror ( req , status ) ) {
2005-11-05 09:34:07 +00:00
DEBUG ( 10 , ( " Could not init domain \n " ) ) ;
return ;
}
2005-10-19 13:45:44 +00:00
2012-08-23 13:14:17 +02:00
existing = find_domain_from_sid ( state - > service , & state - > sid ) ;
2005-11-05 23:46:57 +00:00
if ( existing ! = NULL ) {
DEBUG ( 5 , ( " Initialized domain twice, dropping second one \n " ) ) ;
talloc_free ( state - > domain ) ;
state - > domain = existing ;
2012-08-23 13:14:17 +02:00
} else {
talloc_steal ( state - > service , state - > domain ) ;
DLIST_ADD ( state - > service - > domains , state - > domain ) ;
}
tevent_req_done ( req ) ;
}
static NTSTATUS _wb_sid2domain_recv ( struct tevent_req * req ,
struct wbsrv_domain * * result )
{
struct wb_sid2domain_state * state =
tevent_req_data ( req ,
struct wb_sid2domain_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
2005-11-05 23:46:57 +00:00
}
2012-08-23 13:14:17 +02:00
* result = state - > domain ;
tevent_req_received ( req ) ;
return NT_STATUS_OK ;
}
struct sid2domain_state {
struct composite_context * ctx ;
struct wbsrv_domain * domain ;
} ;
static void sid2domain_recv_domain ( struct tevent_req * subreq ) ;
struct composite_context * wb_sid2domain_send ( TALLOC_CTX * mem_ctx ,
struct wbsrv_service * service ,
const struct dom_sid * sid )
{
struct composite_context * result ;
struct sid2domain_state * state ;
struct tevent_req * subreq ;
DEBUG ( 5 , ( " wb_sid2domain_send called \n " ) ) ;
result = composite_create ( mem_ctx , service - > task - > event_ctx ) ;
if ( result = = NULL ) goto failed ;
state = talloc ( result , struct sid2domain_state ) ;
if ( state = = NULL ) goto failed ;
state - > ctx = result ;
result - > private_data = state ;
subreq = _wb_sid2domain_send ( state ,
result - > event_ctx ,
service , sid ) ;
if ( subreq = = NULL ) goto failed ;
tevent_req_set_callback ( subreq , sid2domain_recv_domain , state ) ;
return result ;
failed :
talloc_free ( result ) ;
return NULL ;
}
static void sid2domain_recv_domain ( struct tevent_req * subreq )
{
struct sid2domain_state * state =
tevent_req_callback_data ( subreq ,
struct sid2domain_state ) ;
state - > ctx - > status = _wb_sid2domain_recv ( subreq , & state - > domain ) ;
TALLOC_FREE ( subreq ) ;
if ( ! composite_is_ok ( state - > ctx ) ) return ;
2005-11-05 23:46:57 +00:00
2005-10-19 13:45:44 +00:00
composite_done ( state - > ctx ) ;
}
NTSTATUS wb_sid2domain_recv ( struct composite_context * ctx ,
struct wbsrv_domain * * result )
{
NTSTATUS status = composite_wait ( ctx ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
struct sid2domain_state * state =
talloc_get_type ( ctx - > private_data ,
struct sid2domain_state ) ;
2005-11-05 23:46:57 +00:00
* result = state - > domain ;
2005-10-19 13:45:44 +00:00
}
talloc_free ( ctx ) ;
return status ;
}
2005-11-05 09:34:07 +00:00
NTSTATUS wb_sid2domain ( TALLOC_CTX * mem_ctx , struct wbsrv_service * service ,
2005-10-19 13:45:44 +00:00
const struct dom_sid * sid ,
struct wbsrv_domain * * result )
{
2005-11-05 09:34:07 +00:00
struct composite_context * c = wb_sid2domain_send ( mem_ctx , service ,
sid ) ;
2005-10-19 13:45:44 +00:00
return wb_sid2domain_recv ( c , result ) ;
}