1998-09-26 01:01:52 +04:00
/*
Unix SMB / Netbios implementation .
2001-01-12 01:49:30 +03:00
Version 2.2
RPC pipe client
Copyright ( C ) Tim Potter 2000
1998-09-26 01:01:52 +04:00
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 "includes.h"
2001-01-12 01:49:30 +03:00
extern int DEBUGLEVEL ;
extern pstring server ;
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
/* Look up domain related information on a remote host */
2001-03-14 23:22:57 +03:00
static uint32 cmd_lsa_query_info_policy ( struct cli_state * cli , int argc , char * * argv )
2001-01-12 01:49:30 +03:00
{
POLICY_HND pol ;
uint32 result = NT_STATUS_UNSUCCESSFUL ;
BOOL got_policy_hnd = False ;
DOM_SID dom_sid ;
fstring sid_str , domain_name ;
uint32 info_class = 3 ;
2001-04-28 04:32:56 +04:00
TALLOC_CTX * mem_ctx ;
2001-01-12 01:49:30 +03:00
if ( argc > 2 ) {
printf ( " Usage: %s [info_class] \n " , argv [ 0 ] ) ;
return 0 ;
2000-12-08 06:34:00 +03:00
}
1999-11-02 01:25:38 +03:00
2001-05-11 11:04:47 +04:00
if ( ! ( mem_ctx = talloc_init ( ) ) ) {
DEBUG ( 0 , ( " cmd_lsa_query_info_poicy: talloc_init failed \n " ) ) ;
2001-04-28 04:32:56 +04:00
return NT_STATUS_UNSUCCESSFUL ;
}
2001-01-12 01:49:30 +03:00
if ( argc = = 2 ) {
info_class = atoi ( argv [ 1 ] ) ;
}
2001-03-14 23:22:57 +03:00
/* Initialise RPC connection */
if ( ! cli_nt_session_open ( cli , PIPE_LSARPC ) ) {
2001-05-11 11:04:47 +04:00
DEBUG ( 0 , ( " Could not initialize samr pipe! \n " ) ) ;
2001-08-10 13:52:10 +04:00
talloc_destroy ( mem_ctx ) ;
2001-03-14 23:22:57 +03:00
return NT_STATUS_UNSUCCESSFUL ;
2001-01-12 01:49:30 +03:00
}
1998-09-26 01:01:52 +04:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_open_policy ( cli , mem_ctx , True ,
2001-01-12 01:49:30 +03:00
SEC_RIGHTS_MAXIMUM_ALLOWED ,
2001-08-27 23:46:22 +04:00
& pol ) ) ! = NT_STATUS_OK ) {
2001-01-12 01:49:30 +03:00
goto done ;
2000-12-08 06:34:00 +03:00
}
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
got_policy_hnd = True ;
2001-01-16 02:35:59 +03:00
/* Lookup info policy */
2000-12-08 06:34:00 +03:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_query_info_policy ( cli , mem_ctx , & pol , info_class ,
2001-01-12 01:49:30 +03:00
domain_name , & dom_sid ) )
2001-08-27 23:46:22 +04:00
! = NT_STATUS_OK ) {
2001-01-12 01:49:30 +03:00
goto done ;
2000-12-08 06:34:00 +03:00
}
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
sid_to_string ( sid_str , & dom_sid ) ;
1998-09-26 01:01:52 +04:00
2001-05-11 11:04:47 +04:00
if ( domain_name [ 0 ] ) {
printf ( " domain %s has sid %s \n " , domain_name , sid_str ) ;
} else {
printf ( " could not query info for level %d \n " , info_class ) ;
}
1998-09-26 01:01:52 +04:00
2001-03-14 23:22:57 +03:00
done :
1999-12-13 16:27:58 +03:00
2001-01-12 01:49:30 +03:00
if ( got_policy_hnd ) {
2001-04-28 04:32:56 +04:00
cli_lsa_close ( cli , mem_ctx , & pol ) ;
2000-12-08 06:34:00 +03:00
}
1999-12-13 16:27:58 +03:00
2001-03-14 23:22:57 +03:00
cli_nt_session_close ( cli ) ;
2001-04-28 04:32:56 +04:00
talloc_destroy ( mem_ctx ) ;
2001-01-12 20:53:47 +03:00
2001-01-12 01:49:30 +03:00
return result ;
}
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
/* Resolve a list of names to a list of sids */
2001-03-14 23:22:57 +03:00
static uint32 cmd_lsa_lookup_names ( struct cli_state * cli , int argc , char * * argv )
2001-01-12 01:49:30 +03:00
{
POLICY_HND pol ;
uint32 result = NT_STATUS_UNSUCCESSFUL ;
BOOL got_policy_hnd = False ;
DOM_SID * sids ;
uint32 * types ;
int num_names , i ;
2001-04-28 04:32:56 +04:00
TALLOC_CTX * mem_ctx ;
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
if ( argc = = 1 ) {
printf ( " Usage: %s [name1 [name2 [...]]] \n " , argv [ 0 ] ) ;
return 0 ;
2000-12-08 06:34:00 +03:00
}
1998-09-26 01:01:52 +04:00
2001-05-11 11:04:47 +04:00
if ( ! ( mem_ctx = talloc_init ( ) ) ) {
DEBUG ( 0 , ( " cmd_lsa_lookup_names: talloc_init failed \n " ) ) ;
2001-04-28 04:32:56 +04:00
return NT_STATUS_UNSUCCESSFUL ;
}
2001-03-14 23:22:57 +03:00
/* Initialise RPC connection */
if ( ! cli_nt_session_open ( cli , PIPE_LSARPC ) ) {
2001-05-11 11:04:47 +04:00
DEBUG ( 0 , ( " Could not initialize samr pipe! \n " ) ) ;
2001-08-10 13:52:10 +04:00
talloc_destroy ( mem_ctx ) ;
2001-03-14 23:22:57 +03:00
return NT_STATUS_UNSUCCESSFUL ;
2000-12-08 06:34:00 +03:00
}
1998-09-26 01:01:52 +04:00
2001-03-14 23:22:57 +03:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_open_policy ( cli , mem_ctx , True ,
2001-01-12 01:49:30 +03:00
SEC_RIGHTS_MAXIMUM_ALLOWED ,
2001-08-27 23:46:22 +04:00
& pol ) ) ! = NT_STATUS_OK ) {
2001-01-12 01:49:30 +03:00
goto done ;
}
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
got_policy_hnd = True ;
1998-09-26 01:01:52 +04:00
2001-01-12 01:49:30 +03:00
/* Lookup the names */
2000-12-08 06:34:00 +03:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_lookup_names ( cli , mem_ctx , & pol , argc - 1 ,
& argv [ 1 ] , & sids , & types , & num_names ) ! =
2001-08-27 23:46:22 +04:00
NT_STATUS_OK ) ) {
2001-01-12 01:49:30 +03:00
goto done ;
}
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
/* Print results */
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
for ( i = 0 ; i < num_names ; i + + ) {
fstring sid_str ;
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
sid_to_string ( sid_str , & sids [ i ] ) ;
2001-05-11 11:04:47 +04:00
printf ( " %s %s (%d) \n " , argv [ i + 1 ] , sid_str ,
2001-01-12 01:49:30 +03:00
types [ i ] ) ;
1998-09-26 01:01:52 +04:00
}
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
done :
if ( got_policy_hnd ) {
2001-04-28 04:32:56 +04:00
cli_lsa_close ( cli , mem_ctx , & pol ) ;
1998-09-30 23:09:57 +04:00
}
2000-12-08 06:34:00 +03:00
2001-03-14 23:22:57 +03:00
cli_nt_session_close ( cli ) ;
2001-04-28 04:32:56 +04:00
talloc_destroy ( mem_ctx ) ;
2001-01-12 20:53:47 +03:00
2000-12-08 06:34:00 +03:00
return result ;
1998-09-30 23:09:57 +04:00
}
2001-01-12 01:49:30 +03:00
/* Resolve a list of SIDs to a list of names */
2000-12-08 06:34:00 +03:00
2001-03-14 23:22:57 +03:00
static uint32 cmd_lsa_lookup_sids ( struct cli_state * cli , int argc , char * * argv )
1998-11-25 22:57:04 +03:00
{
2001-01-12 01:49:30 +03:00
POLICY_HND pol ;
uint32 result = NT_STATUS_UNSUCCESSFUL ;
BOOL got_policy_hnd = False ;
2000-12-08 06:34:00 +03:00
DOM_SID * sids ;
char * * names ;
2001-01-12 01:49:30 +03:00
uint32 * types ;
int num_names , i ;
2001-04-28 04:32:56 +04:00
TALLOC_CTX * mem_ctx ;
1998-09-30 23:09:57 +04:00
2000-12-08 06:34:00 +03:00
if ( argc = = 1 ) {
2001-01-12 01:49:30 +03:00
printf ( " Usage: %s [sid1 [sid2 [...]]] \n " , argv [ 0 ] ) ;
return 0 ;
2000-12-08 06:34:00 +03:00
}
1998-09-30 23:09:57 +04:00
2001-05-11 11:04:47 +04:00
if ( ! ( mem_ctx = talloc_init ( ) ) ) {
DEBUG ( 0 , ( " cmd_lsa_lookup_sids: talloc_init failed \n " ) ) ;
2001-04-28 04:32:56 +04:00
return NT_STATUS_UNSUCCESSFUL ;
}
2001-03-14 23:22:57 +03:00
/* Initialise RPC connection */
if ( ! cli_nt_session_open ( cli , PIPE_LSARPC ) ) {
2001-05-11 11:04:47 +04:00
DEBUG ( 0 , ( " Could not initialize samr pipe! \n " ) ) ;
2001-08-10 13:52:10 +04:00
talloc_destroy ( mem_ctx ) ;
2001-03-14 23:22:57 +03:00
return NT_STATUS_UNSUCCESSFUL ;
2001-01-12 01:49:30 +03:00
}
1998-09-30 23:09:57 +04:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_open_policy ( cli , mem_ctx , True ,
2001-01-12 01:49:30 +03:00
SEC_RIGHTS_MAXIMUM_ALLOWED ,
2001-08-27 23:46:22 +04:00
& pol ) ) ! = NT_STATUS_OK ) {
2001-01-12 01:49:30 +03:00
goto done ;
1998-09-30 23:09:57 +04:00
}
1998-11-11 17:23:55 +03:00
2001-01-12 01:49:30 +03:00
got_policy_hnd = True ;
1998-09-30 23:09:57 +04:00
2001-01-12 01:49:30 +03:00
/* Convert arguments to sids */
1999-12-13 16:27:58 +03:00
2001-04-28 04:32:56 +04:00
sids = ( DOM_SID * ) talloc ( mem_ctx , sizeof ( DOM_SID ) * ( argc - 1 ) ) ;
1998-09-30 23:09:57 +04:00
2001-01-12 01:49:30 +03:00
if ( ! sids ) {
printf ( " out of memory \n " ) ;
goto done ;
2000-12-08 06:34:00 +03:00
}
1998-09-30 23:09:57 +04:00
2001-01-12 01:49:30 +03:00
for ( i = 0 ; i < argc - 1 ; i + + ) {
string_to_sid ( & sids [ i ] , argv [ i + 1 ] ) ;
}
1999-12-13 16:27:58 +03:00
2001-01-12 01:49:30 +03:00
/* Lookup the SIDs */
1998-09-30 23:09:57 +04:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_lookup_sids ( cli , mem_ctx , & pol , argc - 1 , sids ,
2001-01-12 01:49:30 +03:00
& names , & types , & num_names ) ! =
2001-08-27 23:46:22 +04:00
NT_STATUS_OK ) ) {
2001-01-12 01:49:30 +03:00
goto done ;
1998-09-30 23:09:57 +04:00
}
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
/* Print results */
for ( i = 0 ; i < num_names ; i + + ) {
fstring sid_str ;
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
sid_to_string ( sid_str , & sids [ i ] ) ;
2001-05-11 11:04:47 +04:00
printf ( " %s %s (%d) \n " , sid_str , names [ i ] ? names [ i ] :
2001-01-12 01:49:30 +03:00
" *unknown* " , types [ i ] ) ;
1998-09-26 01:01:52 +04:00
}
2000-12-08 06:34:00 +03:00
2001-04-28 04:32:56 +04:00
#if 0 /* JERRY */
2001-01-12 01:49:30 +03:00
safe_free ( sids ) ;
safe_free ( types ) ;
for ( i = 0 ; i < num_names ; i + + ) {
safe_free ( names [ i ] ) ;
}
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
safe_free ( names ) ;
2001-04-28 04:32:56 +04:00
# endif
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
done :
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
if ( got_policy_hnd ) {
2001-04-28 04:32:56 +04:00
cli_lsa_close ( cli , mem_ctx , & pol ) ;
1998-11-11 17:23:55 +03:00
}
2000-12-08 06:34:00 +03:00
2001-03-14 23:22:57 +03:00
cli_nt_session_close ( cli ) ;
2001-04-28 04:32:56 +04:00
talloc_destroy ( mem_ctx ) ;
2001-01-12 20:53:47 +03:00
2000-12-08 06:34:00 +03:00
return result ;
1999-03-18 08:16:59 +03:00
}
2001-01-12 01:49:30 +03:00
/* Enumerate list of trusted domains */
2000-12-08 06:34:00 +03:00
2001-03-14 23:22:57 +03:00
static uint32 cmd_lsa_enum_trust_dom ( struct cli_state * cli , int argc , char * * argv )
2001-01-12 01:49:30 +03:00
{
POLICY_HND pol ;
uint32 result = NT_STATUS_UNSUCCESSFUL ;
BOOL got_policy_hnd = False ;
DOM_SID * domain_sids ;
char * * domain_names ;
2001-03-23 23:41:22 +03:00
uint32 enum_ctx = 0 ;
uint32 num_domains ;
int i ;
2001-04-28 04:32:56 +04:00
TALLOC_CTX * mem_ctx ;
2001-01-12 01:49:30 +03:00
if ( argc ! = 1 ) {
printf ( " Usage: %s \n " , argv [ 0 ] ) ;
return 0 ;
}
2000-12-08 06:34:00 +03:00
2001-05-11 11:04:47 +04:00
if ( ! ( mem_ctx = talloc_init ( ) ) ) {
DEBUG ( 0 , ( " cmd_lsa_enum_trust_dom: talloc_init failed \n " ) ) ;
2001-04-28 04:32:56 +04:00
return NT_STATUS_UNSUCCESSFUL ;
}
2001-03-14 23:22:57 +03:00
/* Initialise RPC connection */
if ( ! cli_nt_session_open ( cli , PIPE_LSARPC ) ) {
2001-05-11 11:04:47 +04:00
DEBUG ( 0 , ( " Could not initialize samr pipe! \n " ) ) ;
2001-08-10 13:52:10 +04:00
talloc_destroy ( mem_ctx ) ;
2001-03-14 23:22:57 +03:00
return NT_STATUS_UNSUCCESSFUL ;
2001-01-12 01:49:30 +03:00
}
2000-12-08 06:34:00 +03:00
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_open_policy ( cli , mem_ctx , True ,
2001-01-12 01:49:30 +03:00
SEC_RIGHTS_MAXIMUM_ALLOWED ,
2001-08-27 23:46:22 +04:00
& pol ) ) ! = NT_STATUS_OK ) {
2001-01-12 01:49:30 +03:00
goto done ;
}
2000-12-08 06:34:00 +03:00
2001-01-12 01:49:30 +03:00
got_policy_hnd = True ;
/* Lookup list of trusted domains */
2001-04-28 04:32:56 +04:00
if ( ( result = cli_lsa_enum_trust_dom ( cli , mem_ctx , & pol , & enum_ctx ,
2001-01-12 01:49:30 +03:00
& num_domains , & domain_names ,
& domain_sids )
2001-08-27 23:46:22 +04:00
! = NT_STATUS_OK ) ) {
2001-01-12 01:49:30 +03:00
goto done ;
}
/* Print results */
for ( i = 0 ; i < num_domains ; i + + ) {
fstring sid_str ;
sid_to_string ( sid_str , & domain_sids [ i ] ) ;
2001-05-11 11:04:47 +04:00
printf ( " %s %s \n " , domain_names [ i ] ? domain_names [ i ] :
2001-01-12 01:49:30 +03:00
" *unknown* " , sid_str ) ;
}
done :
if ( got_policy_hnd ) {
2001-04-28 04:32:56 +04:00
cli_lsa_close ( cli , mem_ctx , & pol ) ;
2001-01-12 01:49:30 +03:00
}
2001-03-14 23:22:57 +03:00
cli_nt_session_close ( cli ) ;
2001-04-28 04:32:56 +04:00
talloc_destroy ( mem_ctx ) ;
2001-01-12 20:53:47 +03:00
2001-01-12 01:49:30 +03:00
return result ;
2000-12-08 06:34:00 +03:00
}
2001-01-12 01:49:30 +03:00
/* List of commands exported by this module */
struct cmd_set lsarpc_commands [ ] = {
2001-07-20 08:38:58 +04:00
{ " LSARPC " } ,
{ " lsaquery " , cmd_lsa_query_info_policy , " Query info policy " , " " } ,
{ " lookupsids " , cmd_lsa_lookup_sids , " Convert SIDs to names " , " " } ,
{ " lookupnames " , cmd_lsa_lookup_names , " Convert names to SIDs " , " " } ,
{ " enumtrust " , cmd_lsa_enum_trust_dom , " Enumerate trusted domains " , " " } ,
{ NULL }
2001-01-12 01:49:30 +03:00
} ;