2001-05-07 09:03:40 +04:00
/*
Unix SMB / Netbios implementation .
Version 2.0
Winbind daemon glue functions to connect new cli interface
to older style lsa_ and samr_ functions
Copyright ( C ) tridge @ samba . org 2001
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 2 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 , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "winbindd.h"
/****************************************************************************
do a LSA Open Policy
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_open_policy ( char * server , BOOL sec_qos , uint32 des_access ,
2001-05-07 09:03:40 +04:00
CLI_POLICY_HND * pol )
{
struct nmb_name calling , called ;
struct ntuser_creds creds ;
struct in_addr dest_ip ;
fstring dest_host ;
2001-09-04 11:13:01 +04:00
NTSTATUS result = NT_STATUS_UNSUCCESSFUL ;
2001-05-07 09:03:40 +04:00
extern pstring global_myname ;
ZERO_STRUCTP ( pol ) ;
pol - > cli = ( struct cli_state * ) malloc ( sizeof ( struct cli_state ) ) ;
pol - > mem_ctx = talloc_init ( ) ;
2001-05-15 05:55:29 +04:00
ZERO_STRUCTP ( pol - > cli ) ;
2001-05-07 09:03:40 +04:00
if ( ! pol - > cli | | ! pol - > mem_ctx )
return False ;
/* Initialise RPC connection */
2001-05-08 07:52:07 +04:00
if ( ! cli_initialise ( pol - > cli ) )
goto done ;
2001-05-07 09:03:40 +04:00
ZERO_STRUCT ( creds ) ;
creds . pwd . null_pwd = 1 ;
cli_init_creds ( pol - > cli , & creds ) ;
/* Establish a SMB connection */
if ( ! resolve_srv_name ( server , dest_host , & dest_ip ) ) {
goto done ;
}
make_nmb_name ( & called , dns_to_netbios_name ( dest_host ) , 0x20 ) ;
make_nmb_name ( & calling , dns_to_netbios_name ( global_myname ) , 0 ) ;
if ( ! cli_establish_connection ( pol - > cli , dest_host , & dest_ip , & calling ,
& called , " IPC$ " , " IPC " , False , True ) ) {
goto done ;
}
if ( ! cli_nt_session_open ( pol - > cli , PIPE_LSARPC ) ) {
goto done ;
}
result = cli_lsa_open_policy ( pol - > cli , pol - > mem_ctx , sec_qos ,
des_access , & pol - > handle ) ;
done :
2001-09-04 11:13:01 +04:00
if ( ! NT_STATUS_IS_OK ( result ) & & pol - > cli ) {
2001-05-07 09:03:40 +04:00
if ( pol - > cli - > initialised )
cli_shutdown ( pol - > cli ) ;
free ( pol - > cli ) ;
}
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( result ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
do a LSA Enumerate Trusted Domain
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_enum_trust_dom ( CLI_POLICY_HND * hnd , uint32 * enum_ctx ,
uint32 * num_doms , char * * * names , DOM_SID * * sids )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_lsa_enum_trust_dom ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ,
enum_ctx , num_doms , names , sids ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
do a LSA Query Info Policy
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_query_info_pol ( CLI_POLICY_HND * hnd , uint16 info_class ,
fstring domain_name , DOM_SID * domain_sid )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_lsa_query_info_policy ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ,
info_class , domain_name , domain_sid ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
do a LSA Lookup Names
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_lookup_names ( CLI_POLICY_HND * hnd , int num_names , char * * names ,
DOM_SID * * sids , uint32 * * types , int * num_sids )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_lsa_lookup_names ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ,
num_names , names , sids , types , num_sids ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
do a LSA Lookup SIDS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_lookup_sids ( CLI_POLICY_HND * hnd , int num_sids , DOM_SID * sids ,
char * * * names , uint32 * * types , int * num_names )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_lsa_lookup_sids ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ,
num_sids , sids , names , types , num_names ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
lsa_close glue
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_lsa_close ( CLI_POLICY_HND * hnd )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_lsa_close ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
samr_close glue
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_samr_close ( CLI_POLICY_HND * hnd )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_samr_close ( hnd - > cli , hnd - > mem_ctx , & hnd - > handle ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
samr_connect glue
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-08 07:52:07 +04:00
BOOL wb_samr_connect ( char * server , uint32 access_mask , CLI_POLICY_HND * pol )
2001-05-07 09:03:40 +04:00
{
2001-05-08 07:52:07 +04:00
struct nmb_name calling , called ;
struct ntuser_creds creds ;
struct in_addr dest_ip ;
fstring dest_host ;
2001-09-04 11:13:01 +04:00
NTSTATUS result = NT_STATUS_UNSUCCESSFUL ;
2001-05-08 07:52:07 +04:00
extern pstring global_myname ;
2001-05-07 09:03:40 +04:00
2001-05-08 07:52:07 +04:00
ZERO_STRUCTP ( pol ) ;
2001-05-07 09:03:40 +04:00
2001-05-08 07:52:07 +04:00
pol - > cli = ( struct cli_state * ) malloc ( sizeof ( struct cli_state ) ) ;
2001-05-15 05:55:29 +04:00
ZERO_STRUCTP ( pol - > cli ) ;
2001-05-08 07:52:07 +04:00
pol - > mem_ctx = talloc_init ( ) ;
if ( ! pol - > cli | | ! pol - > mem_ctx )
return False ;
/* Initialise RPC connection */
if ( ! cli_initialise ( pol - > cli ) )
goto done ;
ZERO_STRUCT ( creds ) ;
creds . pwd . null_pwd = 1 ;
cli_init_creds ( pol - > cli , & creds ) ;
/* Establish a SMB connection */
if ( ! resolve_srv_name ( server , dest_host , & dest_ip ) ) {
goto done ;
}
make_nmb_name ( & called , dns_to_netbios_name ( dest_host ) , 0x20 ) ;
make_nmb_name ( & calling , dns_to_netbios_name ( global_myname ) , 0 ) ;
if ( ! cli_establish_connection ( pol - > cli , dest_host , & dest_ip , & calling ,
& called , " IPC$ " , " IPC " , False , True ) ) {
goto done ;
}
if ( ! cli_nt_session_open ( pol - > cli , PIPE_SAMR ) ) {
goto done ;
}
2001-06-06 11:43:47 +04:00
result = cli_samr_connect ( pol - > cli , pol - > mem_ctx ,
2001-05-08 07:52:07 +04:00
access_mask , & pol - > handle ) ;
done :
2001-09-04 11:13:01 +04:00
if ( ! NT_STATUS_IS_OK ( result ) & & pol - > cli ) {
2001-05-08 07:52:07 +04:00
if ( pol - > cli - > initialised )
cli_shutdown ( pol - > cli ) ;
free ( pol - > cli ) ;
}
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( result ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
samr_open_domain glue
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_samr_open_domain ( CLI_POLICY_HND * connect_pol , uint32 ace_perms ,
DOM_SID * sid , CLI_POLICY_HND * domain_pol )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_samr_open_domain ( connect_pol - > cli ,
connect_pol - > mem_ctx ,
& connect_pol - > handle ,
ace_perms ,
sid ,
& domain_pol - > handle ) ;
2001-09-04 11:13:01 +04:00
if NT_STATUS_IS_OK ( ret ) {
2001-05-07 09:03:40 +04:00
domain_pol - > cli = connect_pol - > cli ;
2001-05-08 07:52:07 +04:00
domain_pol - > mem_ctx = connect_pol - > mem_ctx ;
2001-05-07 09:03:40 +04:00
return True ;
}
return False ;
}
/****************************************************************************
do a SAMR enumerate groups
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-09-04 11:13:01 +04:00
NTSTATUS wb_samr_enum_dom_groups ( CLI_POLICY_HND * pol , uint32 * start_idx ,
2001-05-07 10:04:22 +04:00
uint32 size , struct acct_info * * sam ,
uint32 * num_sam_groups )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
return cli_samr_enum_dom_groups ( pol - > cli , pol - > mem_ctx , & pol - > handle ,
start_idx , size , sam , num_sam_groups ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
2001-05-14 07:58:49 +04:00
do a SAMR query userinfo
2001-05-07 09:03:40 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_get_samr_query_userinfo ( CLI_POLICY_HND * pol , uint32 info_level ,
2001-05-14 07:58:49 +04:00
uint32 user_rid , SAM_USERINFO_CTR * * ctr )
2001-05-07 09:03:40 +04:00
{
POLICY_HND user_pol ;
BOOL got_user_pol = False ;
2001-09-04 11:13:01 +04:00
NTSTATUS result ;
2001-05-07 09:03:40 +04:00
2001-09-04 11:13:01 +04:00
result = cli_samr_open_user ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , MAXIMUM_ALLOWED_ACCESS ,
user_rid , & user_pol ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2001-05-07 09:03:40 +04:00
goto done ;
got_user_pol = True ;
2001-09-04 11:13:01 +04:00
result = cli_samr_query_userinfo ( pol - > cli , pol - > mem_ctx ,
& user_pol , info_level , ctr ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2001-05-07 09:03:40 +04:00
goto done ;
done :
if ( got_user_pol ) cli_samr_close ( pol - > cli , pol - > mem_ctx , & user_pol ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( result ) ;
2001-05-07 09:03:40 +04:00
}
/****************************************************************************
do a SAMR enumerate groups
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 10:04:22 +04:00
BOOL wb_samr_open_user ( CLI_POLICY_HND * pol , uint32 access_mask , uint32 rid ,
POLICY_HND * user_pol )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_samr_open_user ( pol - > cli , pol - > mem_ctx , & pol - > handle ,
access_mask , rid , user_pol ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
2001-05-07 10:04:22 +04:00
BOOL wb_samr_query_usergroups ( CLI_POLICY_HND * pol , uint32 * num_groups ,
DOM_GID * * gid )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_samr_query_usergroups ( pol - > cli , pol - > mem_ctx , & pol - > handle ,
num_groups , gid ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
2001-05-07 10:04:22 +04:00
BOOL wb_get_samr_query_groupinfo ( CLI_POLICY_HND * pol , uint32 info_level ,
2001-05-07 09:03:40 +04:00
uint32 group_rid , GROUP_INFO_CTR * ctr )
{
POLICY_HND group_pol ;
BOOL got_group_pol = False ;
2001-09-04 11:13:01 +04:00
NTSTATUS result ;
2001-05-07 09:03:40 +04:00
2001-09-04 11:13:01 +04:00
result = cli_samr_open_group ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , MAXIMUM_ALLOWED_ACCESS ,
group_rid , & group_pol ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2001-05-07 09:03:40 +04:00
goto done ;
got_group_pol = True ;
2001-09-04 11:13:01 +04:00
result = cli_samr_query_groupinfo ( pol - > cli , pol - > mem_ctx ,
& group_pol , info_level ,
ctr ) ;
2001-05-07 09:03:40 +04:00
done :
if ( got_group_pol ) cli_samr_close ( pol - > cli , pol - > mem_ctx , & group_pol ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( result ) ;
2001-05-07 09:03:40 +04:00
}
2001-05-07 10:04:22 +04:00
BOOL wb_sam_query_groupmem ( CLI_POLICY_HND * pol , uint32 group_rid ,
uint32 * num_names , uint32 * * rid_mem ,
char * * * names , uint32 * * name_types )
2001-05-07 09:03:40 +04:00
{
BOOL got_group_pol = False ;
POLICY_HND group_pol ;
2001-09-04 11:13:01 +04:00
NTSTATUS result ;
uint32 i , total_names = 0 ;
2001-05-07 09:03:40 +04:00
2001-09-04 11:13:01 +04:00
result = cli_samr_open_group ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , MAXIMUM_ALLOWED_ACCESS ,
group_rid , & group_pol ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2001-05-07 09:03:40 +04:00
goto done ;
got_group_pol = True ;
2001-09-04 11:13:01 +04:00
result = cli_samr_query_groupmem ( pol - > cli , pol - > mem_ctx ,
& group_pol , num_names , rid_mem ,
name_types ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2001-05-07 09:03:40 +04:00
goto done ;
2001-07-23 07:06:40 +04:00
/* Call cli_samr_lookup_rids() in bunches of ~1000 rids to avoid
crashing NT4 . */
# define MAX_LOOKUP_RIDS 900
* names = talloc ( pol - > mem_ctx , * num_names * sizeof ( char * ) ) ;
* name_types = talloc ( pol - > mem_ctx , * num_names * sizeof ( uint32 ) ) ;
for ( i = 0 ; i < * num_names ; i + = MAX_LOOKUP_RIDS ) {
int num_lookup_rids = MIN ( * num_names - i , MAX_LOOKUP_RIDS ) ;
uint32 tmp_num_names = 0 ;
char * * tmp_names = NULL ;
uint32 * tmp_types = NULL ;
/* Lookup a chunk of rids */
result = cli_samr_lookup_rids ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , 1000 , /* flags */
num_lookup_rids ,
& ( * rid_mem ) [ i ] ,
& tmp_num_names ,
& tmp_names , & tmp_types ) ;
2001-09-04 11:13:01 +04:00
if ( ! NT_STATUS_IS_OK ( result ) )
2001-07-23 07:06:40 +04:00
goto done ;
/* Copy result into array. The talloc system will take
care of freeing the temporary arrays later on . */
memcpy ( & ( * names ) [ i ] , tmp_names , sizeof ( char * ) *
tmp_num_names ) ;
memcpy ( & ( * name_types ) [ i ] , tmp_types , sizeof ( uint32 ) *
tmp_num_names ) ;
total_names + = tmp_num_names ;
}
* num_names = total_names ;
2001-05-07 09:03:40 +04:00
done :
2001-07-23 07:06:40 +04:00
if ( got_group_pol )
cli_samr_close ( pol - > cli , pol - > mem_ctx , & group_pol ) ;
2001-05-07 09:03:40 +04:00
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( result ) ;
2001-05-07 09:03:40 +04:00
}
2001-05-07 10:04:22 +04:00
BOOL wb_samr_query_dom_info ( CLI_POLICY_HND * pol , uint16 switch_value ,
SAM_UNK_CTR * ctr )
2001-05-07 09:03:40 +04:00
{
2001-09-04 11:13:01 +04:00
NTSTATUS ret ;
2001-05-07 09:03:40 +04:00
ret = cli_samr_query_dom_info ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , switch_value , ctr ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_IS_OK ( ret ) ;
2001-05-07 09:03:40 +04:00
}
2001-07-23 11:21:52 +04:00
/* Unlike all the others, the status code of this function is actually used
by winbindd . */
2001-05-07 09:03:40 +04:00
2001-09-04 11:13:01 +04:00
NTSTATUS wb_samr_query_dispinfo ( CLI_POLICY_HND * pol , uint32 * start_ndx ,
uint16 info_level , uint32 * num_entries ,
SAM_DISPINFO_CTR * ctr )
2001-07-23 11:21:52 +04:00
{
return cli_samr_query_dispinfo ( pol - > cli , pol - > mem_ctx ,
& pol - > handle , start_ndx ,
info_level , num_entries ,
0xffff , ctr ) ;
2001-05-07 09:03:40 +04:00
}