2007-10-25 01:16:54 +04:00
/*
Samba Unix / Linux SMB client library
2001-12-20 10:42:00 +03:00
net lookup command
Copyright ( C ) 2001 Andrew Tridgell ( tridge @ samba . org )
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-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2001-12-20 10:42:00 +03:00
( at your option ) any later version .
2007-10-25 01:16:54 +04:00
2001-12-20 10:42:00 +03:00
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 .
2007-10-25 01:16:54 +04:00
2001-12-20 10:42:00 +03:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>. */
2001-12-20 10:42:00 +03:00
# include "includes.h"
2004-10-07 08:01:18 +04:00
# include "utils/net.h"
2018-03-10 17:31:11 +03:00
# include "libsmb/namequery.h"
2010-02-23 19:11:37 +03:00
# include "libads/sitename_cache.h"
2020-11-29 21:17:30 +03:00
# include "lib/addns/dnsquery_srv.h"
2010-05-05 03:39:16 +04:00
# include "../librpc/gen_ndr/ndr_netlogon.h"
2010-07-02 02:14:04 +04:00
# include "smb_krb5.h"
2010-10-12 08:27:50 +04:00
# include "../libcli/security/security.h"
2011-03-22 18:50:02 +03:00
# include "passdb/lookup_sid.h"
2018-01-05 16:21:05 +03:00
# include "libsmb/dsgetdcname.h"
2001-12-20 10:42:00 +03:00
2008-05-10 01:22:12 +04:00
int net_lookup_usage ( struct net_context * c , int argc , const char * * argv )
2001-12-20 10:42:00 +03:00
{
2009-07-30 14:10:56 +04:00
d_printf ( _ (
2003-09-05 08:40:10 +04:00
" net lookup [host] HOSTNAME[#<type>] \n \t gives IP for a hostname \n \n "
2020-12-02 23:20:32 +03:00
" net lookup ldap [domain] [sitename] \n \t gives IP of domain's ldap server \n \n "
2002-07-15 14:35:28 +04:00
" net lookup kdc [realm] \n \t gives IP of realm's kerberos KDC \n \n "
2007-08-28 19:01:23 +04:00
" net lookup pdc [domain|realm] \n \t gives IP of realm's kerberos KDC \n \n "
2002-07-15 14:35:28 +04:00
" net lookup dc [domain] \n \t gives IP of domains Domain Controllers \n \n "
" net lookup master [domain|wg] \n \t give IP of master browser \n \n "
2006-02-04 01:19:41 +03:00
" net lookup name [name] \n \t Lookup name's sid and type \n \n "
" net lookup sid [sid] \n \t Give sid's name and type \n \n "
2007-08-28 19:01:23 +04:00
" net lookup dsgetdcname [name] [flags] [sitename] \n \n "
2009-07-30 14:10:56 +04:00
) ) ;
2001-12-20 10:42:00 +03:00
return - 1 ;
}
/* lookup a hostname giving an IP */
2008-05-10 01:22:12 +04:00
static int net_lookup_host ( struct net_context * c , int argc , const char * * argv )
2001-12-20 10:42:00 +03:00
{
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2001-12-20 10:42:00 +03:00
int name_type = 0x20 ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2003-09-05 08:40:10 +04:00
const char * name = argv [ 0 ] ;
char * p ;
2001-12-20 10:42:00 +03:00
2007-10-25 01:16:54 +04:00
if ( argc = = 0 )
2008-05-10 01:22:12 +04:00
return net_lookup_usage ( c , argc , argv ) ;
2001-12-20 10:42:00 +03:00
2003-09-05 08:40:10 +04:00
p = strchr_m ( name , ' # ' ) ;
if ( p ) {
* p = ' \0 ' ;
sscanf ( + + p , " %x " , & name_type ) ;
}
2007-10-25 01:16:54 +04:00
2009-07-28 22:51:58 +04:00
if ( ! resolve_name ( name , & ss , name_type , false ) ) {
2007-10-25 01:16:54 +04:00
/* we deliberately use DEBUG() here to send it to stderr
2001-12-20 10:42:00 +03:00
so scripts aren ' t mucked up */
2003-09-05 08:40:10 +04:00
DEBUG ( 0 , ( " Didn't find %s#%02x \n " , name , name_type ) ) ;
2001-12-20 10:42:00 +03:00
return - 1 ;
}
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ss ) ;
d_printf ( " %s \n " , addr ) ;
2001-12-20 10:42:00 +03:00
return 0 ;
}
2006-05-05 23:24:48 +04:00
# ifdef HAVE_ADS
2020-12-02 23:18:04 +03:00
static void print_ldap_srvlist ( struct dns_rr_srv * dclist , size_t numdcs )
2002-07-15 14:35:28 +04:00
{
2020-12-02 23:18:04 +03:00
size_t i ;
2006-05-05 23:24:48 +04:00
for ( i = 0 ; i < numdcs ; i + + ) {
2020-12-02 23:18:04 +03:00
struct dns_rr_srv * dc = & dclist [ i ] ;
size_t j ;
for ( j = 0 ; j < dc - > num_ips ; j + + ) {
struct sockaddr_storage * ss = & dc - > ss_s [ j ] ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2020-12-02 23:18:04 +03:00
print_sockaddr ( addr , sizeof ( addr ) , ss ) ;
2007-10-25 01:16:54 +04:00
# ifdef HAVE_IPV6
2020-12-02 23:18:04 +03:00
if ( ss - > ss_family = = AF_INET6 ) {
d_printf ( " [%s]:% " PRIu16 " \n " , addr , dc - > port ) ;
2007-10-25 01:16:54 +04:00
}
# endif
2020-12-02 23:18:04 +03:00
if ( ss - > ss_family = = AF_INET ) {
d_printf ( " %s:% " PRIu16 " \n " , addr , dc - > port ) ;
2007-10-25 01:16:54 +04:00
}
2006-05-05 23:24:48 +04:00
}
}
2002-07-15 14:35:28 +04:00
}
2005-10-20 21:33:17 +04:00
# endif
2002-07-15 14:35:28 +04:00
2008-05-10 01:22:12 +04:00
static int net_lookup_ldap ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2005-11-22 22:48:33 +03:00
# ifdef HAVE_ADS
2002-08-17 18:45:04 +04:00
const char * domain ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2006-05-05 23:24:48 +04:00
struct dns_rr_srv * dcs = NULL ;
2020-09-10 18:19:37 +03:00
size_t numdcs = 0 ;
2020-12-02 23:20:32 +03:00
const char * sitename = NULL ;
2006-05-05 23:24:48 +04:00
TALLOC_CTX * ctx ;
NTSTATUS status ;
2007-10-25 01:16:54 +04:00
int ret ;
2007-11-16 05:27:26 +03:00
char h_name [ MAX_DNS_NAME_LENGTH ] ;
2020-11-29 21:17:30 +03:00
char * query = NULL ;
2002-07-15 14:35:28 +04:00
if ( argc > 0 )
domain = argv [ 0 ] ;
else
2008-05-10 01:22:12 +04:00
domain = c - > opt_target_workgroup ;
2002-07-15 14:35:28 +04:00
2020-12-02 23:20:32 +03:00
if ( argc > 1 ) {
sitename = argv [ 1 ] ;
}
2006-05-05 23:24:48 +04:00
if ( ( ctx = talloc_init ( " net_lookup_ldap " ) ) = = NULL ) {
2010-01-19 13:43:54 +03:00
d_fprintf ( stderr , " net_lookup_ldap: talloc_init() %s! \n " ,
_ ( " failed " ) ) ;
2006-05-05 23:24:48 +04:00
return - 1 ;
}
2020-12-02 23:20:32 +03:00
if ( sitename = = NULL ) {
sitename = sitename_fetch ( ctx , domain ) ;
}
2020-11-29 21:17:30 +03:00
query = ads_dns_query_string_dcs ( ctx , domain ) ;
2013-09-05 00:58:18 +04:00
2002-07-15 14:35:28 +04:00
DEBUG ( 9 , ( " Lookup up ldap for domain %s \n " , domain ) ) ;
2006-05-05 23:24:48 +04:00
2020-11-29 21:17:30 +03:00
status = ads_dns_query_srv (
ctx ,
lp_get_async_dns_timeout ( ) ,
sitename ,
query ,
& dcs ,
& numdcs ) ;
2006-05-05 23:24:48 +04:00
if ( NT_STATUS_IS_OK ( status ) & & numdcs ) {
print_ldap_srvlist ( dcs , numdcs ) ;
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return 0 ;
}
2007-08-28 19:01:23 +04:00
DEBUG ( 9 , ( " Looking up PDC for domain %s \n " , domain ) ) ;
2007-10-25 01:16:54 +04:00
if ( ! get_pdc_ip ( domain , & ss ) ) {
2006-05-05 23:24:48 +04:00
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
2006-05-05 23:24:48 +04:00
}
2002-07-15 14:35:28 +04:00
2007-11-02 20:25:34 +03:00
ret = sys_getnameinfo ( ( struct sockaddr * ) & ss ,
2007-10-25 01:16:54 +04:00
sizeof ( struct sockaddr_storage ) ,
h_name , sizeof ( h_name ) ,
NULL , 0 ,
NI_NAMEREQD ) ;
if ( ret ) {
2006-05-05 23:24:48 +04:00
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
2006-05-05 23:24:48 +04:00
}
2002-07-15 14:35:28 +04:00
2007-10-25 01:16:54 +04:00
DEBUG ( 9 , ( " Found PDC with DNS name %s \n " , h_name ) ) ;
domain = strchr ( h_name , ' . ' ) ;
2006-05-05 23:24:48 +04:00
if ( ! domain ) {
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
2006-05-05 23:24:48 +04:00
}
2002-07-15 14:35:28 +04:00
domain + + ;
DEBUG ( 9 , ( " Looking up ldap for domain %s \n " , domain ) ) ;
2006-05-05 23:24:48 +04:00
2020-11-29 21:17:30 +03:00
status = ads_dns_query_srv (
ctx ,
lp_get_async_dns_timeout ( ) ,
sitename ,
query ,
& dcs ,
& numdcs ) ;
2006-05-05 23:24:48 +04:00
if ( NT_STATUS_IS_OK ( status ) & & numdcs ) {
print_ldap_srvlist ( dcs , numdcs ) ;
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return 0 ;
}
2006-05-05 23:24:48 +04:00
TALLOC_FREE ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
# endif
2005-11-22 22:48:33 +03:00
DEBUG ( 1 , ( " No ADS support \n " ) ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
}
2008-05-10 01:22:12 +04:00
static int net_lookup_dc ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2020-09-09 02:31:18 +03:00
struct samba_sockaddr * sa_list = NULL ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2002-07-15 14:35:28 +04:00
char * pdc_str = NULL ;
2007-08-28 19:01:23 +04:00
const char * domain = NULL ;
2007-01-17 21:25:35 +03:00
char * sitename = NULL ;
2020-09-02 20:05:48 +03:00
size_t count = 0 ;
size_t i ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2007-10-19 04:40:25 +04:00
bool sec_ads = ( lp_security ( ) = = SEC_ADS ) ;
2020-08-26 21:56:00 +03:00
NTSTATUS status ;
2007-08-28 19:01:23 +04:00
if ( sec_ads ) {
domain = lp_realm ( ) ;
} else {
2008-05-10 01:22:12 +04:00
domain = c - > opt_target_workgroup ;
2007-08-28 19:01:23 +04:00
}
2002-07-15 14:35:28 +04:00
if ( argc > 0 )
domain = argv [ 0 ] ;
/* first get PDC */
2007-10-25 01:16:54 +04:00
if ( ! get_pdc_ip ( domain , & ss ) )
2002-07-15 14:35:28 +04:00
return - 1 ;
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ss ) ;
2009-01-01 05:06:57 +03:00
if ( asprintf ( & pdc_str , " %s " , addr ) = = - 1 ) {
return - 1 ;
}
2002-07-15 14:35:28 +04:00
d_printf ( " %s \n " , pdc_str ) ;
2001-12-20 10:42:00 +03:00
2013-09-05 00:58:18 +04:00
sitename = sitename_fetch ( talloc_tos ( ) , domain ) ;
2020-09-09 04:07:28 +03:00
status = get_sorted_dc_list ( talloc_tos ( ) ,
2020-08-26 21:56:00 +03:00
domain ,
sitename ,
2020-09-09 02:31:18 +03:00
& sa_list ,
2020-08-26 21:56:00 +03:00
& count ,
sec_ads ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2002-07-15 14:35:28 +04:00
SAFE_FREE ( pdc_str ) ;
2013-09-05 00:58:18 +04:00
TALLOC_FREE ( sitename ) ;
2002-07-15 14:35:28 +04:00
return 0 ;
}
2013-09-05 00:58:18 +04:00
TALLOC_FREE ( sitename ) ;
2002-07-15 14:35:28 +04:00
for ( i = 0 ; i < count ; i + + ) {
2020-09-09 02:31:18 +03:00
print_sockaddr ( addr , sizeof ( addr ) , & sa_list [ i ] . u . ss ) ;
2007-10-25 01:16:54 +04:00
if ( ! strequal ( pdc_str , addr ) )
d_printf ( " %s \n " , addr ) ;
2002-07-15 14:35:28 +04:00
}
2020-09-09 02:31:18 +03:00
TALLOC_FREE ( sa_list ) ;
2002-07-15 14:35:28 +04:00
SAFE_FREE ( pdc_str ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_lookup_pdc ( struct net_context * c , int argc , const char * * argv )
2007-08-28 19:01:23 +04:00
{
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2007-08-28 19:01:23 +04:00
char * pdc_str = NULL ;
const char * domain ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2007-08-28 19:01:23 +04:00
if ( lp_security ( ) = = SEC_ADS ) {
domain = lp_realm ( ) ;
} else {
2008-05-10 01:22:12 +04:00
domain = c - > opt_target_workgroup ;
2007-08-28 19:01:23 +04:00
}
if ( argc > 0 )
domain = argv [ 0 ] ;
/* first get PDC */
2007-10-25 01:16:54 +04:00
if ( ! get_pdc_ip ( domain , & ss ) )
2007-08-28 19:01:23 +04:00
return - 1 ;
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ss ) ;
2009-01-01 05:06:57 +03:00
if ( asprintf ( & pdc_str , " %s " , addr ) = = - 1 ) {
return - 1 ;
}
2007-08-28 19:01:23 +04:00
d_printf ( " %s \n " , pdc_str ) ;
SAFE_FREE ( pdc_str ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_lookup_master ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2007-10-25 01:16:54 +04:00
struct sockaddr_storage master_ss ;
2008-05-10 01:22:12 +04:00
const char * domain = c - > opt_target_workgroup ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2002-07-15 14:35:28 +04:00
if ( argc > 0 )
domain = argv [ 0 ] ;
2007-10-25 01:16:54 +04:00
if ( ! find_master_ip ( domain , & master_ss ) )
2002-07-15 14:35:28 +04:00
return - 1 ;
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & master_ss ) ;
d_printf ( " %s \n " , addr ) ;
2002-07-15 14:35:28 +04:00
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_lookup_kdc ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
# ifdef HAVE_KRB5
krb5_error_code rc ;
krb5_context ctx ;
2020-09-09 02:11:47 +03:00
struct samba_sockaddr * kdcs = NULL ;
2011-11-30 20:39:22 +04:00
const char * realm ;
2018-11-21 15:30:08 +03:00
char * * get_host_realms = NULL ;
2020-08-26 21:38:31 +03:00
size_t num_kdcs = 0 ;
size_t i ;
2011-11-30 20:39:22 +04:00
NTSTATUS status ;
2002-07-15 14:35:28 +04:00
2018-12-05 13:44:24 +03:00
rc = smb_krb5_init_context_common ( & ctx ) ;
2002-07-15 14:35:28 +04:00
if ( rc ) {
2018-12-05 13:44:24 +03:00
DBG_ERR ( " kerberos init context failed (%s) \n " ,
error_message ( rc ) ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
}
2011-11-30 20:39:22 +04:00
if ( argc > 0 ) {
realm = argv [ 0 ] ;
2002-07-15 14:35:28 +04:00
} else if ( lp_realm ( ) & & * lp_realm ( ) ) {
2011-11-30 20:39:22 +04:00
realm = lp_realm ( ) ;
2002-07-15 14:35:28 +04:00
} else {
2018-11-21 15:30:08 +03:00
rc = krb5_get_host_realm ( ctx , NULL , & get_host_realms ) ;
2002-07-15 14:35:28 +04:00
if ( rc ) {
DEBUG ( 1 , ( " krb5_gethost_realm failed (%s) \n " ,
error_message ( rc ) ) ) ;
2018-11-21 15:17:38 +03:00
krb5_free_context ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
}
2018-11-21 15:30:08 +03:00
realm = ( const char * ) * get_host_realms ;
2002-07-15 14:35:28 +04:00
}
2020-09-09 02:24:25 +03:00
status = get_kdc_list ( talloc_tos ( ) ,
2020-08-26 21:38:31 +03:00
realm ,
NULL ,
& kdcs ,
& num_kdcs ) ;
2011-11-30 20:39:22 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2020-09-09 02:24:25 +03:00
DBG_WARNING ( " get_kdc_list failed (%s) \n " ,
2020-08-26 21:38:31 +03:00
nt_errstr ( status ) ) ;
2018-11-21 15:30:08 +03:00
krb5_free_host_realm ( ctx , get_host_realms ) ;
2018-11-21 15:17:38 +03:00
krb5_free_context ( ctx ) ;
2002-07-15 14:35:28 +04:00
return - 1 ;
}
2011-11-30 20:39:22 +04:00
for ( i = 0 ; i < num_kdcs ; i + + ) {
char addr [ INET6_ADDRSTRLEN ] ;
2020-09-09 02:11:47 +03:00
print_sockaddr ( addr , sizeof ( addr ) , & kdcs [ i ] . u . ss ) ;
2011-11-30 20:39:22 +04:00
2020-09-09 02:11:47 +03:00
d_printf ( " %s:88 \n " , addr ) ;
2011-11-30 20:39:22 +04:00
}
2018-11-21 15:30:08 +03:00
krb5_free_host_realm ( ctx , get_host_realms ) ;
2018-11-21 15:17:38 +03:00
krb5_free_context ( ctx ) ;
2020-08-26 21:38:31 +03:00
TALLOC_FREE ( kdcs ) ;
2011-11-30 20:39:22 +04:00
return 0 ;
2010-01-19 13:43:54 +03:00
# endif
2002-07-15 14:35:28 +04:00
DEBUG ( 1 , ( " No kerberos support \n " ) ) ;
return - 1 ;
}
2002-08-17 18:45:04 +04:00
2008-05-10 01:22:12 +04:00
static int net_lookup_name ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
const char * dom , * name ;
2010-05-21 05:25:01 +04:00
struct dom_sid sid ;
2018-12-08 17:05:53 +03:00
struct dom_sid_buf buf ;
2006-09-08 18:28:06 +04:00
enum lsa_SidType type ;
2006-02-04 01:19:41 +03:00
if ( argc ! = 1 ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net lookup name <name> \n " ) ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
2007-08-30 23:48:31 +04:00
if ( ! lookup_name ( talloc_tos ( ) , argv [ 0 ] , LOOKUP_NAME_ALL ,
2006-02-04 01:19:41 +03:00
& dom , & name , & sid , & type ) ) {
2009-07-30 14:10:56 +04:00
d_printf ( _ ( " Could not lookup name %s \n " ) , argv [ 0 ] ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
2018-12-08 17:05:53 +03:00
d_printf ( " %s %d (%s) %s \\ %s \n " , dom_sid_str_buf ( & sid , & buf ) ,
2006-02-04 01:19:41 +03:00
type , sid_type_lookup ( type ) , dom , name ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_lookup_sid ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
const char * dom , * name ;
2010-05-21 05:25:01 +04:00
struct dom_sid sid ;
2018-12-08 17:05:53 +03:00
struct dom_sid_buf buf ;
2006-09-08 18:28:06 +04:00
enum lsa_SidType type ;
2006-02-04 01:19:41 +03:00
if ( argc ! = 1 ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net lookup sid <sid> \n " ) ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
if ( ! string_to_sid ( & sid , argv [ 0 ] ) ) {
2009-07-30 14:10:56 +04:00
d_printf ( _ ( " Could not convert %s to SID \n " ) , argv [ 0 ] ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
2007-08-30 23:48:31 +04:00
if ( ! lookup_sid ( talloc_tos ( ) , & sid ,
2006-02-04 01:19:41 +03:00
& dom , & name , & type ) ) {
2009-07-30 14:10:56 +04:00
d_printf ( _ ( " Could not lookup name %s \n " ) , argv [ 0 ] ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
2018-12-08 17:05:53 +03:00
d_printf ( " %s %d (%s) %s \\ %s \n " , dom_sid_str_buf ( & sid , & buf ) ,
2006-02-04 01:19:41 +03:00
type , sid_type_lookup ( type ) , dom , name ) ;
return 0 ;
}
2002-08-17 18:45:04 +04:00
2008-05-10 01:22:12 +04:00
static int net_lookup_dsgetdcname ( struct net_context * c , int argc , const char * * argv )
2007-08-28 19:01:23 +04:00
{
NTSTATUS status ;
const char * domain_name = NULL ;
2008-05-15 19:00:31 +04:00
const char * site_name = NULL ;
2007-08-28 19:01:23 +04:00
uint32_t flags = 0 ;
2008-02-28 14:30:18 +03:00
struct netr_DsRGetDCNameInfo * info = NULL ;
2007-08-28 19:01:23 +04:00
TALLOC_CTX * mem_ctx ;
2008-02-28 14:30:18 +03:00
char * s = NULL ;
2007-08-28 19:01:23 +04:00
if ( argc < 1 | | argc > 3 ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net lookup dsgetdcname "
2009-07-30 14:10:56 +04:00
" <name> <flags> <sitename> \n " ) ) ;
2007-08-28 19:01:23 +04:00
return - 1 ;
}
mem_ctx = talloc_init ( " net_lookup_dsgetdcname " ) ;
if ( ! mem_ctx ) {
return - 1 ;
}
domain_name = argv [ 0 ] ;
2018-02-04 18:45:57 +03:00
if ( argc > = 2 ) {
2007-08-28 19:01:23 +04:00
sscanf ( argv [ 1 ] , " %x " , & flags ) ;
2018-02-04 18:45:57 +03:00
}
2007-08-28 19:01:23 +04:00
2018-02-04 18:48:23 +03:00
if ( flags = = 0 ) {
flags = DS_DIRECTORY_SERVICE_REQUIRED ;
2007-08-28 19:01:23 +04:00
}
if ( argc = = 3 ) {
2008-05-15 19:00:31 +04:00
site_name = argv [ 2 ] ;
2007-08-28 19:01:23 +04:00
}
2010-12-15 21:57:23 +03:00
if ( ! c - > msg_ctx ) {
d_fprintf ( stderr , _ ( " Could not initialise message context. "
" Try running as root \n " ) ) ;
return - 1 ;
}
2010-09-22 07:56:23 +04:00
status = dsgetdcname ( mem_ctx , c - > msg_ctx , domain_name , NULL , site_name ,
2007-08-28 19:01:23 +04:00
flags , & info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-07-30 14:10:56 +04:00
d_printf ( _ ( " failed with: %s \n " ) , nt_errstr ( status ) ) ;
2007-08-28 19:01:23 +04:00
TALLOC_FREE ( mem_ctx ) ;
return - 1 ;
}
2008-02-28 14:30:18 +03:00
s = NDR_PRINT_STRUCT_STRING ( mem_ctx , netr_DsRGetDCNameInfo , info ) ;
printf ( " %s \n " , s ) ;
TALLOC_FREE ( s ) ;
2007-08-28 19:01:23 +04:00
TALLOC_FREE ( mem_ctx ) ;
return 0 ;
}
2001-12-20 10:42:00 +03:00
/* lookup hosts or IP addresses using internal samba lookup fns */
2008-05-10 01:22:12 +04:00
int net_lookup ( struct net_context * c , int argc , const char * * argv )
2001-12-20 10:42:00 +03:00
{
2003-09-05 08:40:10 +04:00
int i ;
struct functable table [ ] = {
2019-01-14 12:50:33 +03:00
{
. funcname = " HOST " ,
. fn = net_lookup_host ,
} ,
{
. funcname = " LDAP " ,
. fn = net_lookup_ldap ,
} ,
{
. funcname = " DC " ,
. fn = net_lookup_dc ,
} ,
{
. funcname = " PDC " ,
. fn = net_lookup_pdc ,
} ,
{
. funcname = " MASTER " ,
. fn = net_lookup_master ,
} ,
{
. funcname = " KDC " ,
. fn = net_lookup_kdc ,
} ,
{
. funcname = " NAME " ,
. fn = net_lookup_name ,
} ,
{
. funcname = " SID " ,
. fn = net_lookup_sid ,
} ,
{
. funcname = " DSGETDCNAME " ,
. fn = net_lookup_dsgetdcname ,
} ,
{
. funcname = NULL ,
} ,
2001-12-20 10:42:00 +03:00
} ;
2003-09-05 08:40:10 +04:00
if ( argc < 1 ) {
2009-07-30 14:10:56 +04:00
d_printf ( _ ( " \n Usage: \n " ) ) ;
2008-05-10 01:22:12 +04:00
return net_lookup_usage ( c , argc , argv ) ;
2003-09-05 08:40:10 +04:00
}
for ( i = 0 ; table [ i ] . funcname ; i + + ) {
2011-05-13 22:21:30 +04:00
if ( strcasecmp_m ( argv [ 0 ] , table [ i ] . funcname ) = = 0 )
2008-05-10 01:22:12 +04:00
return table [ i ] . fn ( c , argc - 1 , argv + 1 ) ;
2003-09-05 08:40:10 +04:00
}
2008-05-08 13:23:38 +04:00
/* Default to lookup a hostname so 'net lookup foo#1b' can be
2003-09-05 08:40:10 +04:00
used instead of ' net lookup host foo # 1 b ' . The host syntax
2008-05-08 13:23:38 +04:00
is a bit confusing as non # 00 names can ' t really be
2003-09-05 08:40:10 +04:00
considered hosts as such . */
2008-05-10 01:22:12 +04:00
return net_lookup_host ( c , argc , argv ) ;
2001-12-20 10:42:00 +03:00
}