2007-10-25 01:16:54 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
NBT client - used to lookup netbios names
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
2003-04-14 07:59:04 +04:00
Copyright ( C ) Jelmer Vernooij 2003 ( Conversion to popt )
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +04: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
1996-05-04 11:50:46 +04: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/>.
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2021-01-13 11:28:34 +03:00
# include "lib/cmdline/cmdline.h"
2011-03-23 16:18:59 +03:00
# include "libsmb/nmblib.h"
2018-03-10 17:31:11 +03:00
# include "libsmb/namequery.h"
2020-08-07 21:17:34 +03:00
# include "lib/util/string_wrappers.h"
1996-05-04 11:50:46 +04:00
2007-10-25 01:16:54 +04:00
static bool give_flags = false ;
static bool use_bcast = true ;
static bool got_bcast = false ;
static struct sockaddr_storage bcast_addr ;
static bool recursion_desired = false ;
static bool translate_addresses = false ;
1999-12-13 16:27:58 +03:00
static int ServerFD = - 1 ;
2007-10-25 01:16:54 +04:00
static bool RootPort = false ;
static bool find_status = false ;
1997-10-04 05:07:47 +04:00
1996-05-04 11:50:46 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Open the socket communication .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool open_sockets ( void )
1996-05-04 11:50:46 +04:00
{
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2012-07-27 06:56:22 +04:00
const char * sock_addr = lp_nbt_client_socket_address ( ) ;
2007-10-25 01:16:54 +04:00
if ( ! interpret_string_addr ( & ss , sock_addr ,
AI_NUMERICHOST | AI_PASSIVE ) ) {
DEBUG ( 0 , ( " open_sockets: unable to get socket address "
" from string %s " , sock_addr ) ) ;
return false ;
}
2021-02-12 23:27:19 +03:00
ServerFD = open_socket_in (
SOCK_DGRAM , & ss , ( RootPort ? 137 : 0 ) , true ) ;
if ( ServerFD < 0 ) {
if ( RootPort ) {
DBG_ERR ( " open_socket_in failed: %s \n " ,
strerror ( - ServerFD ) ) ;
} else {
DBG_NOTICE ( " open_socket_in failed: %s \n " ,
strerror ( - ServerFD ) ) ;
}
2007-10-25 01:16:54 +04:00
return false ;
}
1996-05-04 11:50:46 +04:00
2007-10-25 01:16:54 +04:00
set_socket_options ( ServerFD , " SO_BROADCAST " ) ;
1999-12-13 16:27:58 +03:00
2007-10-25 01:16:54 +04:00
DEBUG ( 3 , ( " Socket opened. \n " ) ) ;
return true ;
1996-05-04 11:50:46 +04:00
}
2000-12-20 06:22:51 +03:00
/****************************************************************************
turn a node status flags field into a string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * node_status_flags ( unsigned char flags )
{
static fstring ret ;
fstrcpy ( ret , " " ) ;
2007-10-25 01:16:54 +04:00
2000-12-20 06:22:51 +03:00
fstrcat ( ret , ( flags & 0x80 ) ? " <GROUP> " : " " ) ;
if ( ( flags & 0x60 ) = = 0x00 ) fstrcat ( ret , " B " ) ;
if ( ( flags & 0x60 ) = = 0x20 ) fstrcat ( ret , " P " ) ;
if ( ( flags & 0x60 ) = = 0x40 ) fstrcat ( ret , " M " ) ;
if ( ( flags & 0x60 ) = = 0x60 ) fstrcat ( ret , " H " ) ;
if ( flags & 0x10 ) fstrcat ( ret , " <DEREGISTERING> " ) ;
if ( flags & 0x08 ) fstrcat ( ret , " <CONFLICT> " ) ;
if ( flags & 0x04 ) fstrcat ( ret , " <ACTIVE> " ) ;
if ( flags & 0x02 ) fstrcat ( ret , " <PERMANENT> " ) ;
2007-10-25 01:16:54 +04:00
2000-12-20 06:22:51 +03:00
return ret ;
}
2002-07-15 14:35:28 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Turn the NMB Query flags into a string .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2002-07-15 14:35:28 +04:00
static char * query_flags ( int flags )
{
static fstring ret1 ;
fstrcpy ( ret1 , " " ) ;
if ( flags & NM_FLAGS_RS ) fstrcat ( ret1 , " Response " ) ;
if ( flags & NM_FLAGS_AA ) fstrcat ( ret1 , " Authoritative " ) ;
if ( flags & NM_FLAGS_TC ) fstrcat ( ret1 , " Truncated " ) ;
if ( flags & NM_FLAGS_RD ) fstrcat ( ret1 , " Recursion_Desired " ) ;
if ( flags & NM_FLAGS_RA ) fstrcat ( ret1 , " Recursion_Available " ) ;
if ( flags & NM_FLAGS_B ) fstrcat ( ret1 , " Broadcast " ) ;
return ret1 ;
}
2000-12-20 06:22:51 +03:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Do a node status query .
2000-12-20 06:22:51 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2014-09-09 11:45:51 +04:00
static bool do_node_status ( const char * name ,
2007-10-25 01:16:54 +04:00
int type ,
struct sockaddr_storage * pss )
2000-12-20 06:22:51 +03:00
{
struct nmb_name nname ;
2020-09-08 23:07:07 +03:00
size_t count = 0 ;
size_t i , j ;
2010-12-28 14:53:12 +03:00
struct node_status * addrs ;
2004-04-16 07:57:30 +04:00
struct node_status_extra extra ;
2000-12-20 06:22:51 +03:00
fstring cleanname ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2010-12-28 14:53:12 +03:00
NTSTATUS status ;
2000-12-20 06:22:51 +03:00
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , pss ) ;
d_printf ( " Looking up status of %s \n " , addr ) ;
2000-12-20 06:22:51 +03:00
make_nmb_name ( & nname , name , type ) ;
2011-01-04 20:34:38 +03:00
status = node_status_query ( talloc_tos ( ) , & nname , pss ,
2010-12-28 14:53:12 +03:00
& addrs , & count , & extra ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
2000-12-20 06:22:51 +03:00
for ( i = 0 ; i < count ; i + + ) {
2010-12-28 14:53:12 +03:00
pull_ascii_fstring ( cleanname , addrs [ i ] . name ) ;
2000-12-20 06:22:51 +03:00
for ( j = 0 ; cleanname [ j ] ; j + + ) {
2007-10-25 01:16:54 +04:00
if ( ! isprint ( ( int ) cleanname [ j ] ) ) {
cleanname [ j ] = ' . ' ;
}
2000-12-20 06:22:51 +03:00
}
2001-09-08 06:59:23 +04:00
d_printf ( " \t %-15s <%02x> - %s \n " ,
2010-12-28 14:53:12 +03:00
cleanname , addrs [ i ] . type ,
node_status_flags ( addrs [ i ] . flags ) ) ;
2000-12-20 06:22:51 +03:00
}
2004-09-28 06:13:53 +04:00
d_printf ( " \n \t MAC Address = %02X-%02X-%02X-%02X-%02X-%02X \n " ,
extra . mac_addr [ 0 ] , extra . mac_addr [ 1 ] ,
extra . mac_addr [ 2 ] , extra . mac_addr [ 3 ] ,
extra . mac_addr [ 4 ] , extra . mac_addr [ 5 ] ) ;
d_printf ( " \n " ) ;
2010-12-28 14:53:12 +03:00
TALLOC_FREE ( addrs ) ;
2014-09-09 11:45:51 +04:00
return true ;
2004-09-28 06:13:53 +04:00
} else {
2007-10-25 01:16:54 +04:00
d_printf ( " No reply from %s \n \n " , addr ) ;
2014-09-09 11:45:51 +04:00
return false ;
2000-12-20 06:22:51 +03:00
}
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Send out one query .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2007-10-19 04:40:25 +04:00
static bool query_one ( const char * lookup , unsigned int lookup_type )
1999-12-13 16:27:58 +03:00
{
2020-09-08 23:37:59 +03:00
size_t j , count = 0 ;
2011-01-04 20:48:47 +03:00
uint8_t flags ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * ip_list = NULL ;
2010-12-30 14:42:50 +03:00
NTSTATUS status = NT_STATUS_NOT_FOUND ;
1999-12-13 16:27:58 +03:00
if ( got_bcast ) {
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
print_sockaddr ( addr , sizeof ( addr ) , & bcast_addr ) ;
d_printf ( " querying %s on %s \n " , lookup , addr ) ;
2011-01-04 20:48:47 +03:00
status = name_query ( lookup , lookup_type , use_bcast ,
2010-12-28 15:47:35 +03:00
use_bcast ? true : recursion_desired ,
& bcast_addr , talloc_tos ( ) ,
2011-01-04 20:48:47 +03:00
& ip_list , & count , & flags ) ;
1999-12-13 16:27:58 +03:00
} else {
2020-07-21 03:56:48 +03:00
status = name_resolve_bcast ( talloc_tos ( ) ,
lookup ,
lookup_type ,
& ip_list ,
& count ) ;
1999-12-13 16:27:58 +03:00
}
2010-12-28 15:47:35 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2007-10-25 01:16:54 +04:00
return false ;
}
1999-12-13 16:27:58 +03:00
2007-10-25 01:16:54 +04:00
if ( give_flags ) {
d_printf ( " Flags: %s \n " , query_flags ( flags ) ) ;
}
2002-07-15 14:35:28 +04:00
1999-12-13 16:27:58 +03:00
for ( j = 0 ; j < count ; j + + ) {
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
1999-12-13 16:27:58 +03:00
if ( translate_addresses ) {
2007-11-16 05:27:26 +03:00
char h_name [ MAX_DNS_NAME_LENGTH ] ;
2007-10-25 01:16:54 +04:00
h_name [ 0 ] = ' \0 ' ;
2007-11-02 22:05:17 +03:00
if ( sys_getnameinfo ( ( const struct sockaddr * ) & ip_list [ j ] ,
2007-10-25 01:16:54 +04:00
sizeof ( struct sockaddr_storage ) ,
h_name , sizeof ( h_name ) ,
NULL , 0 ,
NI_NAMEREQD ) ) {
continue ;
1999-12-13 16:27:58 +03:00
}
2007-10-25 01:16:54 +04:00
d_printf ( " %s, " , h_name ) ;
1999-12-13 16:27:58 +03:00
}
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ip_list [ j ] ) ;
d_printf ( " %s %s<%02x> \n " , addr , lookup , lookup_type ) ;
2006-02-16 23:50:41 +03:00
/* We can only do find_status if the ip address returned
was valid - ie . name_query returned true .
*/
if ( find_status ) {
2014-09-09 11:45:51 +04:00
if ( ! do_node_status ( lookup , lookup_type , & ip_list [ j ] ) ) {
status = NT_STATUS_UNSUCCESSFUL ;
}
2006-02-16 23:50:41 +03:00
}
1999-12-13 16:27:58 +03:00
}
2010-12-28 15:47:35 +03:00
TALLOC_FREE ( ip_list ) ;
2000-04-25 04:46:46 +04:00
2010-12-28 15:47:35 +03:00
return NT_STATUS_IS_OK ( status ) ;
1999-12-13 16:27:58 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2021-01-13 11:27:07 +03:00
enum nmblookup_cmdline_options {
CMDLINE_RECURSIVE = 1 ,
} ;
2014-02-26 23:16:26 +04:00
int main ( int argc , const char * argv [ ] )
1996-05-04 11:50:46 +04:00
{
2007-10-25 01:16:54 +04:00
int opt ;
unsigned int lookup_type = 0x0 ;
fstring lookup ;
static bool find_master = False ;
static bool lookup_by_ip = False ;
2014-09-09 11:45:51 +04:00
poptContext pc = NULL ;
2007-10-25 01:16:54 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2014-09-09 11:45:51 +04:00
int rc = 0 ;
2021-01-13 11:28:34 +03:00
bool ok ;
2007-10-25 01:16:54 +04:00
struct poptOption long_options [ ] = {
POPT_AUTOHELP
2019-01-11 18:25:30 +03:00
{
. longName = " broadcast " ,
. shortName = ' B ' ,
. argInfo = POPT_ARG_STRING ,
. arg = NULL ,
. val = ' B ' ,
. descrip = " Specify address to use for broadcasts " ,
. argDescrip = " BROADCAST-ADDRESS " ,
} ,
{
. longName = " flags " ,
. shortName = ' f ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' f ' ,
. descrip = " List the NMB flags returned " ,
} ,
{
. longName = " unicast " ,
. shortName = ' U ' ,
. argInfo = POPT_ARG_STRING ,
. arg = NULL ,
. val = ' U ' ,
. descrip = " Specify address to use for unicast " ,
} ,
{
. longName = " master-browser " ,
. shortName = ' M ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' M ' ,
. descrip = " Search for a master browser " ,
} ,
{
. longName = " recursion " ,
2021-01-13 11:27:07 +03:00
. shortName = 0 ,
2019-01-11 18:25:30 +03:00
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
2021-01-13 11:27:07 +03:00
. val = CMDLINE_RECURSIVE ,
2019-01-11 18:25:30 +03:00
. descrip = " Set recursion desired in package " ,
} ,
{
. longName = " status " ,
. shortName = ' S ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' S ' ,
. descrip = " Lookup node status as well " ,
} ,
{
. longName = " translate " ,
. shortName = ' T ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' T ' ,
. descrip = " Translate IP addresses into names " ,
} ,
{
. longName = " root-port " ,
. shortName = ' r ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' r ' ,
. descrip = " Use root port 137 (Win95 only replies to this) " ,
} ,
{
. longName = " lookup-by-ip " ,
. shortName = ' A ' ,
. argInfo = POPT_ARG_NONE ,
. arg = NULL ,
. val = ' A ' ,
. descrip = " Do a node status on <name> as an IP Address " ,
} ,
2007-10-25 01:16:54 +04:00
POPT_COMMON_SAMBA
POPT_COMMON_CONNECTION
2021-01-13 11:28:34 +03:00
POPT_COMMON_VERSION
2019-01-11 18:25:30 +03:00
POPT_TABLEEND
2007-10-25 01:16:54 +04:00
} ;
* lookup = 0 ;
2015-03-21 22:00:06 +03:00
smb_init_locale ( ) ;
2007-10-25 01:16:54 +04:00
2021-01-13 11:28:34 +03:00
ok = samba_cmdline_init ( frame ,
SAMBA_CMDLINE_CONFIG_CLIENT ,
false /* require_smbconf */ ) ;
if ( ! ok ) {
DBG_ERR ( " Failed to init cmdline parser! \n " ) ;
TALLOC_FREE ( frame ) ;
exit ( 1 ) ;
}
2007-10-25 01:16:54 +04:00
2021-01-13 11:28:34 +03:00
pc = samba_popt_get_context ( getprogname ( ) ,
argc ,
argv ,
long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
if ( pc = = NULL ) {
DBG_ERR ( " Failed to setup popt context! \n " ) ;
TALLOC_FREE ( frame ) ;
exit ( 1 ) ;
}
2007-10-25 01:16:54 +04:00
poptSetOtherOptionHelp ( pc , " <NODE> ... " ) ;
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
case ' f ' :
give_flags = true ;
break ;
case ' M ' :
find_master = true ;
break ;
2021-01-13 11:27:07 +03:00
case CMDLINE_RECURSIVE :
2007-10-25 01:16:54 +04:00
recursion_desired = true ;
break ;
case ' S ' :
find_status = true ;
break ;
case ' r ' :
RootPort = true ;
break ;
case ' A ' :
lookup_by_ip = true ;
break ;
case ' B ' :
if ( interpret_string_addr ( & bcast_addr ,
poptGetOptArg ( pc ) ,
NI_NUMERICHOST ) ) {
got_bcast = True ;
use_bcast = True ;
}
break ;
case ' U ' :
if ( interpret_string_addr ( & bcast_addr ,
poptGetOptArg ( pc ) ,
0 ) ) {
got_bcast = True ;
use_bcast = False ;
}
break ;
case ' T ' :
translate_addresses = ! translate_addresses ;
break ;
}
}
poptGetArg ( pc ) ; /* Remove argv[0] */
if ( ! poptPeekArg ( pc ) ) {
poptPrintUsage ( pc , stderr , 0 ) ;
2014-09-09 11:45:51 +04:00
rc = 1 ;
goto out ;
2007-10-25 01:16:54 +04:00
}
if ( ! open_sockets ( ) ) {
2014-09-09 11:45:51 +04:00
rc = 1 ;
goto out ;
2007-10-25 01:16:54 +04:00
}
while ( poptPeekArg ( pc ) ) {
char * p ;
struct in_addr ip ;
2015-01-30 16:37:06 +03:00
size_t nbt_len ;
2007-10-25 01:16:54 +04:00
fstrcpy ( lookup , poptGetArg ( pc ) ) ;
if ( lookup_by_ip ) {
struct sockaddr_storage ss ;
2008-10-23 22:41:15 +04:00
ip = interpret_addr2 ( lookup ) ;
2007-10-25 01:16:54 +04:00
in_addr_to_sockaddr_storage ( & ss , ip ) ;
fstrcpy ( lookup , " * " ) ;
2014-09-09 11:45:51 +04:00
if ( ! do_node_status ( lookup , lookup_type , & ss ) ) {
rc = 1 ;
}
2007-10-25 01:16:54 +04:00
continue ;
}
if ( find_master ) {
if ( * lookup = = ' - ' ) {
fstrcpy ( lookup , " \01 \02 __MSBROWSE__ \02 " ) ;
lookup_type = 1 ;
} else {
lookup_type = 0x1d ;
}
}
p = strchr_m ( lookup , ' # ' ) ;
if ( p ) {
* p = ' \0 ' ;
sscanf ( + + p , " %x " , & lookup_type ) ;
}
2015-01-30 16:37:06 +03:00
nbt_len = strlen ( lookup ) ;
if ( nbt_len > MAX_NETBIOSNAME_LEN - 1 ) {
d_printf ( " The specified netbios name [%s] is too long! \n " ,
lookup ) ;
continue ;
}
2007-10-25 01:16:54 +04:00
if ( ! query_one ( lookup , lookup_type ) ) {
2014-09-09 11:45:51 +04:00
rc = 1 ;
2007-10-25 01:16:54 +04:00
d_printf ( " name_query failed to find name %s " , lookup ) ;
if ( 0 ! = lookup_type ) {
d_printf ( " #%02x " , lookup_type ) ;
}
d_printf ( " \n " ) ;
}
}
2014-09-09 11:45:51 +04:00
out :
2007-10-25 01:16:54 +04:00
poptFreeContext ( pc ) ;
TALLOC_FREE ( frame ) ;
2014-09-09 11:45:51 +04:00
return rc ;
1996-05-04 11:50:46 +04:00
}