2009-08-29 19:17:47 +04:00
/*
Unix SMB / CIFS implementation .
async implementation of WINBINDD_LIST_GROUPS
Copyright ( C ) Volker Lendecke 2009
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
the Free Software Foundation ; either version 3 of the License , or
( 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
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2022-06-24 13:25:16 +03:00
# include "util/debug.h"
2009-08-29 19:17:47 +04:00
# include "winbindd.h"
2014-05-08 04:17:32 +04:00
# include "librpc/gen_ndr/ndr_winbind_c.h"
2009-08-29 19:17:47 +04:00
struct winbindd_list_groups_domstate {
struct tevent_req * subreq ;
struct winbindd_domain * domain ;
struct wbint_Principals groups ;
} ;
struct winbindd_list_groups_state {
2022-07-14 14:19:44 +03:00
uint32_t num_received ;
2009-08-29 19:17:47 +04:00
/* All domains */
2022-07-14 14:19:44 +03:00
uint32_t num_domains ;
2009-08-29 19:17:47 +04:00
struct winbindd_list_groups_domstate * domains ;
} ;
static void winbindd_list_groups_done ( struct tevent_req * subreq ) ;
struct tevent_req * winbindd_list_groups_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct winbindd_cli_state * cli ,
struct winbindd_request * request )
{
struct tevent_req * req ;
struct winbindd_list_groups_state * state ;
struct winbindd_domain * domain ;
2022-07-14 14:19:44 +03:00
uint32_t i ;
2009-08-29 19:17:47 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct winbindd_list_groups_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2022-06-24 13:25:16 +03:00
D_NOTICE ( " [%s (%u)] Winbind external command LIST_GROUPS start. \n "
" WBFLAG_FROM_NSS is %s, winbind enum groups is %d. \n " ,
cli - > client_name ,
( unsigned int ) cli - > pid ,
request - > wb_flags & WBFLAG_FROM_NSS ? " Set " : " Unset " ,
lp_winbind_enum_groups ( ) ) ;
2018-12-20 19:25:01 +03:00
if ( request - > wb_flags & WBFLAG_FROM_NSS & & ! lp_winbind_enum_groups ( ) ) {
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
}
2009-08-29 19:17:47 +04:00
/* Ensure null termination */
request - > domain_name [ sizeof ( request - > domain_name ) - 1 ] = ' \0 ' ;
if ( request - > domain_name [ 0 ] ! = ' \0 ' ) {
state - > num_domains = 1 ;
2022-06-24 13:25:16 +03:00
D_DEBUG ( " List groups for domain %s. \n " , request - > domain_name ) ;
2009-08-29 19:17:47 +04:00
} else {
state - > num_domains = 0 ;
for ( domain = domain_list ( ) ; domain ; domain = domain - > next ) {
state - > num_domains + = 1 ;
}
2022-07-18 16:28:11 +03:00
D_DEBUG ( " List groups for % " PRIu32 " domain(s). \n " , state - > num_domains ) ;
2009-08-29 19:17:47 +04:00
}
state - > domains = talloc_array ( state ,
struct winbindd_list_groups_domstate ,
state - > num_domains ) ;
if ( tevent_req_nomem ( state - > domains , req ) ) {
return tevent_req_post ( req , ev ) ;
}
if ( request - > domain_name [ 0 ] ! = ' \0 ' ) {
2018-02-09 18:19:53 +03:00
ZERO_STRUCT ( state - > domains [ 0 ] . groups ) ;
2009-08-29 19:17:47 +04:00
state - > domains [ 0 ] . domain = find_domain_from_name_noinit (
request - > domain_name ) ;
if ( state - > domains [ 0 ] . domain = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_NO_SUCH_DOMAIN ) ;
return tevent_req_post ( req , ev ) ;
}
} else {
i = 0 ;
for ( domain = domain_list ( ) ; domain ; domain = domain - > next ) {
2018-02-09 18:19:53 +03:00
ZERO_STRUCT ( state - > domains [ i ] . groups ) ;
state - > domains [ i ] . domain = domain ;
i + + ;
2009-08-29 19:17:47 +04:00
}
}
for ( i = 0 ; i < state - > num_domains ; i + + ) {
struct winbindd_list_groups_domstate * d = & state - > domains [ i ] ;
2010-08-12 17:13:54 +04:00
d - > subreq = dcerpc_wbint_QueryGroupList_send (
2010-04-07 19:45:12 +04:00
state - > domains , ev , dom_child_handle ( d - > domain ) ,
2009-08-29 19:17:47 +04:00
& d - > groups ) ;
if ( tevent_req_nomem ( d - > subreq , req ) ) {
TALLOC_FREE ( state - > domains ) ;
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( d - > subreq , winbindd_list_groups_done ,
req ) ;
}
state - > num_received = 0 ;
return req ;
}
static void winbindd_list_groups_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct winbindd_list_groups_state * state = tevent_req_data (
req , struct winbindd_list_groups_state ) ;
NTSTATUS status , result ;
2022-07-14 14:19:44 +03:00
uint32_t i ;
2009-08-29 19:17:47 +04:00
2010-08-12 17:13:54 +04:00
status = dcerpc_wbint_QueryGroupList_recv ( subreq , state - > domains ,
2009-08-29 19:17:47 +04:00
& result ) ;
for ( i = 0 ; i < state - > num_domains ; i + + ) {
if ( subreq = = state - > domains [ i ] . subreq ) {
break ;
}
}
if ( i < state - > num_domains ) {
struct winbindd_list_groups_domstate * d = & state - > domains [ i ] ;
2022-07-18 16:28:11 +03:00
D_DEBUG ( " Domain %s returned % " PRIu32 " groups \n " , d - > domain - > name ,
2022-06-24 13:25:16 +03:00
d - > groups . num_principals ) ;
2009-08-29 19:17:47 +04:00
d - > subreq = NULL ;
if ( ! NT_STATUS_IS_OK ( status ) | | ! NT_STATUS_IS_OK ( result ) ) {
2022-06-24 13:25:16 +03:00
D_WARNING ( " list_groups for domain %s failed \n " ,
d - > domain - > name ) ;
2009-08-29 19:17:47 +04:00
d - > groups . num_principals = 0 ;
}
}
TALLOC_FREE ( subreq ) ;
state - > num_received + = 1 ;
if ( state - > num_received > = state - > num_domains ) {
tevent_req_done ( req ) ;
}
}
NTSTATUS winbindd_list_groups_recv ( struct tevent_req * req ,
struct winbindd_response * response )
{
struct winbindd_list_groups_state * state = tevent_req_data (
req , struct winbindd_list_groups_state ) ;
NTSTATUS status ;
char * result ;
2022-07-14 14:19:44 +03:00
uint32_t i , j , num_entries = 0 ;
2009-08-29 19:17:47 +04:00
size_t len ;
2022-06-24 13:25:16 +03:00
D_NOTICE ( " Winbind external command LIST_GROUPS end. \n " ) ;
2009-08-29 19:17:47 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
2022-06-24 13:25:16 +03:00
D_WARNING ( " Failed with %s. \n " , nt_errstr ( status ) ) ;
2009-08-29 19:17:47 +04:00
return status ;
}
len = 0 ;
2010-04-03 16:16:19 +04:00
response - > data . num_entries = 0 ;
2009-08-29 19:17:47 +04:00
for ( i = 0 ; i < state - > num_domains ; i + + ) {
struct winbindd_list_groups_domstate * d = & state - > domains [ i ] ;
for ( j = 0 ; j < d - > groups . num_principals ; j + + ) {
2018-05-08 12:18:56 +03:00
const char * name ;
name = fill_domain_username_talloc ( response , d - > domain - > name ,
2009-08-29 19:17:47 +04:00
d - > groups . principals [ j ] . name ,
True ) ;
2018-05-08 12:18:56 +03:00
if ( name = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2009-08-29 19:17:47 +04:00
len + = strlen ( name ) + 1 ;
}
2010-04-03 16:16:19 +04:00
response - > data . num_entries + = d - > groups . num_principals ;
2009-08-29 19:17:47 +04:00
}
result = talloc_array ( response , char , len + 1 ) ;
if ( result = = 0 ) {
return NT_STATUS_NO_MEMORY ;
}
len = 0 ;
for ( i = 0 ; i < state - > num_domains ; i + + ) {
struct winbindd_list_groups_domstate * d = & state - > domains [ i ] ;
for ( j = 0 ; j < d - > groups . num_principals ; j + + ) {
2018-05-08 12:18:56 +03:00
const char * name ;
2009-08-29 19:17:47 +04:00
size_t this_len ;
2018-05-08 12:18:56 +03:00
name = fill_domain_username_talloc ( response , d - > domain - > name ,
2009-08-29 19:17:47 +04:00
d - > groups . principals [ j ] . name ,
True ) ;
2018-05-08 12:18:56 +03:00
if ( name = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2009-08-29 19:17:47 +04:00
this_len = strlen ( name ) ;
memcpy ( result + len , name , this_len ) ;
len + = this_len ;
result [ len ] = ' , ' ;
len + = 1 ;
2010-01-13 17:59:57 +03:00
num_entries + + ;
2009-08-29 19:17:47 +04:00
}
}
result [ len - 1 ] = ' \0 ' ;
2010-01-13 17:59:57 +03:00
response - > data . num_entries = num_entries ;
2009-08-29 19:17:47 +04:00
response - > extra_data . data = result ;
response - > length + = len ;
return NT_STATUS_OK ;
}