2009-08-29 18:05:02 +04:00
/*
Unix SMB / CIFS implementation .
async implementation of WINBINDD_LIST_USERS
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"
# include "winbindd.h"
2014-05-08 04:17:32 +04:00
# include "librpc/gen_ndr/ndr_winbind_c.h"
2017-01-02 18:44:04 +03:00
# include "lib/util/strv.h"
2009-08-29 18:05:02 +04:00
struct winbindd_list_users_domstate {
struct tevent_req * subreq ;
struct winbindd_domain * domain ;
2017-01-02 18:44:04 +03:00
char * users ;
2009-08-29 18:05:02 +04:00
} ;
struct winbindd_list_users_state {
2017-01-02 18:44:04 +03:00
size_t num_received ;
2009-08-29 18:05:02 +04:00
/* All domains */
2017-01-02 18:44:04 +03:00
size_t num_domains ;
2009-08-29 18:05:02 +04:00
struct winbindd_list_users_domstate * domains ;
} ;
static void winbindd_list_users_done ( struct tevent_req * subreq ) ;
struct tevent_req * winbindd_list_users_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_users_state * state ;
struct winbindd_domain * domain ;
2017-01-02 18:44:04 +03:00
size_t i ;
2009-08-29 18:05:02 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct winbindd_list_users_state ) ;
if ( req = = NULL ) {
return NULL ;
}
/* Ensure null termination */
request - > domain_name [ sizeof ( request - > domain_name ) - 1 ] = ' \0 ' ;
DEBUG ( 3 , ( " list_users %s \n " , request - > domain_name ) ) ;
if ( request - > domain_name [ 0 ] ! = ' \0 ' ) {
state - > num_domains = 1 ;
} else {
state - > num_domains = 0 ;
for ( domain = domain_list ( ) ; domain ; domain = domain - > next ) {
state - > num_domains + = 1 ;
}
}
state - > domains = talloc_array ( state ,
struct winbindd_list_users_domstate ,
state - > num_domains ) ;
if ( tevent_req_nomem ( state - > domains , req ) ) {
return tevent_req_post ( req , ev ) ;
}
if ( request - > domain_name [ 0 ] ! = ' \0 ' ) {
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 ) {
state - > domains [ i + + ] . domain = domain ;
}
}
for ( i = 0 ; i < state - > num_domains ; i + + ) {
struct winbindd_list_users_domstate * d = & state - > domains [ i ] ;
2017-01-02 18:44:04 +03:00
d - > subreq = wb_query_user_list_send (
state - > domains , ev , d - > domain ) ;
2009-08-29 18:05:02 +04:00
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_users_done ,
req ) ;
}
state - > num_received = 0 ;
return req ;
}
static void winbindd_list_users_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct winbindd_list_users_state * state = tevent_req_data (
req , struct winbindd_list_users_state ) ;
2017-01-02 18:44:04 +03:00
struct winbindd_list_users_domstate * d ;
NTSTATUS status ;
size_t i ;
2009-08-29 18:05:02 +04:00
for ( i = 0 ; i < state - > num_domains ; i + + ) {
if ( subreq = = state - > domains [ i ] . subreq ) {
break ;
}
}
2017-01-02 18:44:04 +03:00
if ( i = = state - > num_domains ) {
tevent_req_nterror ( req , NT_STATUS_INTERNAL_ERROR ) ;
return ;
2009-08-29 18:05:02 +04:00
}
2017-01-02 18:44:04 +03:00
d = & state - > domains [ i ] ;
status = wb_query_user_list_recv ( subreq , state - > domains ,
& d - > users ) ;
2009-08-29 18:05:02 +04:00
TALLOC_FREE ( subreq ) ;
2017-01-02 18:44:04 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
/*
* Just skip this domain
*/
d - > users = NULL ;
}
2009-08-29 18:05:02 +04:00
state - > num_received + = 1 ;
if ( state - > num_received > = state - > num_domains ) {
tevent_req_done ( req ) ;
}
}
NTSTATUS winbindd_list_users_recv ( struct tevent_req * req ,
struct winbindd_response * response )
{
struct winbindd_list_users_state * state = tevent_req_data (
req , struct winbindd_list_users_state ) ;
NTSTATUS status ;
char * result ;
2017-01-02 18:44:04 +03:00
size_t i , len ;
2009-08-29 18:05:02 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
2017-01-02 18:44:04 +03:00
result = NULL ;
2009-08-29 18:05:02 +04:00
for ( i = 0 ; i < state - > num_domains ; i + + ) {
struct winbindd_list_users_domstate * d = & state - > domains [ i ] ;
2017-01-02 18:44:04 +03:00
int ret ;
2009-08-29 18:05:02 +04:00
2017-01-02 18:44:04 +03:00
if ( d - > users = = NULL ) {
continue ;
2009-08-29 18:05:02 +04:00
}
2017-01-02 18:44:04 +03:00
ret = strv_append ( state , & result , d - > users ) ;
if ( ret ! = 0 ) {
return map_nt_error_from_unix ( ret ) ;
2009-08-29 18:05:02 +04:00
}
}
2017-01-02 18:44:04 +03:00
len = talloc_get_size ( result ) ;
2009-08-29 18:05:02 +04:00
2017-01-10 11:48:33 +03:00
response - > extra_data . data = talloc_steal ( response , result ) ;
2009-08-29 18:05:02 +04:00
response - > length + = len ;
2017-01-02 18:44:04 +03:00
response - > data . num_entries = 0 ;
2017-02-23 20:41:03 +03:00
if ( result ! = NULL & & len > = 1 ) {
2017-01-02 18:44:04 +03:00
len - = 1 ;
response - > data . num_entries = 1 ;
for ( i = 0 ; i < len ; i + + ) {
if ( result [ i ] = = ' \0 ' ) {
result [ i ] = ' , ' ;
response - > data . num_entries + = 1 ;
}
}
}
2009-08-29 18:05:02 +04:00
return NT_STATUS_OK ;
}