1996-05-04 07:50:46 +00:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
NBT client - used to lookup netbios names
1998-01-22 13:27:43 +00:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
1996-05-04 07:50:46 +00: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 .
*/
1998-07-29 03:08:05 +00:00
# define NO_SYSLOG
1996-05-04 07:50:46 +00:00
# include "includes.h"
extern int DEBUGLEVEL ;
1996-06-06 11:43:09 +00:00
extern struct in_addr ipzero ;
1996-05-04 07:50:46 +00:00
1999-12-13 13:27:58 +00:00
static BOOL use_bcast = True ;
static BOOL got_bcast = False ;
static struct in_addr bcast_addr ;
static BOOL recursion_desired = False ;
static BOOL translate_addresses = False ;
static int ServerFD = - 1 ;
static int RootPort = 0 ;
static BOOL find_status = False ;
1997-10-04 01:07:47 +00:00
1996-05-04 07:50:46 +00:00
/****************************************************************************
open the socket communication
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL open_sockets ( void )
{
1999-12-13 13:27:58 +00:00
ServerFD = open_socket_in ( SOCK_DGRAM ,
( RootPort ? 137 : 0 ) ,
3 ,
interpret_addr ( lp_socket_address ( ) ) , True ) ;
1999-12-04 19:14:37 +00:00
1999-12-13 13:27:58 +00:00
if ( ServerFD = = - 1 )
1996-05-04 07:50:46 +00:00
return ( False ) ;
1999-12-13 13:27:58 +00:00
set_socket_options ( ServerFD , " SO_BROADCAST " ) ;
DEBUG ( 3 , ( " Socket opened. \n " ) ) ;
1996-05-04 07:50:46 +00:00
return True ;
}
1999-12-13 13:27:58 +00:00
1996-05-04 07:50:46 +00:00
/****************************************************************************
usage on the program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void usage ( void )
{
printf ( " Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name \n " ) ;
printf ( " Version %s \n " , VERSION ) ;
printf ( " \t -d debuglevel set the debuglevel \n " ) ;
printf ( " \t -B broadcast address the address to use for broadcasts \n " ) ;
1997-11-19 23:50:02 +00:00
printf ( " \t -U unicast address the address to use for unicast \n " ) ;
1996-05-04 07:50:46 +00:00
printf ( " \t -M searches for a master browser \n " ) ;
1997-11-19 23:50:02 +00:00
printf ( " \t -R set recursion desired in packet \n " ) ;
1996-05-04 07:50:46 +00:00
printf ( " \t -S lookup node status as well \n " ) ;
1999-12-13 13:27:58 +00:00
printf ( " \t -T translate IP addresses into names \n " ) ;
1997-10-10 01:32:26 +00:00
printf ( " \t -r Use root port 137 (Win95 only replies to this) \n " ) ;
printf ( " \t -A Do a node status on <name> as an IP Address \n " ) ;
1998-11-03 02:25:28 +00:00
printf ( " \t -i NetBIOS scope Use the given NetBIOS scope for name queries \n " ) ;
printf ( " \t -s smb.conf file Use the given path to the smb.conf file \n " ) ;
printf ( " \t -h Print this help message. \n " ) ;
1999-12-13 13:27:58 +00:00
printf ( " \n If you specify -M and name is \" - \" , nmblookup looks up __MSBROWSE__<01> \n " ) ;
1996-05-04 07:50:46 +00:00
printf ( " \n " ) ;
}
1999-12-13 13:27:58 +00:00
/****************************************************************************
send out one query
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL query_one ( char * lookup , unsigned int lookup_type )
{
int j , count ;
struct in_addr * ip_list = NULL ;
if ( got_bcast ) {
printf ( " querying %s on %s \n " , lookup , inet_ntoa ( bcast_addr ) ) ;
ip_list = name_query ( ServerFD , lookup , lookup_type , use_bcast ,
use_bcast ? True : recursion_desired ,
2000-01-03 03:17:16 +00:00
bcast_addr , & count ) ;
1999-12-13 13:27:58 +00:00
} else {
struct in_addr * bcast ;
for ( j = iface_count ( ) - 1 ;
! ip_list & & j > = 0 ;
j - - ) {
bcast = iface_n_bcast ( j ) ;
printf ( " querying %s on %s \n " ,
lookup , inet_ntoa ( * bcast ) ) ;
ip_list = name_query ( ServerFD , lookup , lookup_type ,
use_bcast ,
use_bcast ? True : recursion_desired ,
2000-01-03 03:17:16 +00:00
* bcast , & count ) ;
1999-12-13 13:27:58 +00:00
}
}
if ( ! ip_list ) return False ;
for ( j = 0 ; j < count ; j + + ) {
if ( translate_addresses ) {
struct hostent * host = gethostbyaddr ( ( char * ) & ip_list [ j ] , sizeof ( ip_list [ j ] ) , AF_INET ) ;
if ( host ) {
printf ( " %s, " , host - > h_name ) ;
}
}
printf ( " %s %s<%02x> \n " , inet_ntoa ( ip_list [ j ] ) , lookup , lookup_type ) ;
}
/* We can only do find_status if the ip address returned
was valid - ie . name_query returned true .
*/
if ( find_status ) {
printf ( " Looking up status of %s \n " , inet_ntoa ( ip_list [ 0 ] ) ) ;
2000-01-03 03:17:16 +00:00
name_status ( ServerFD , lookup , lookup_type , True , ip_list [ 0 ] , NULL , NULL ) ;
1999-12-13 13:27:58 +00:00
printf ( " \n " ) ;
}
2000-04-25 00:46:46 +00:00
safe_free ( ip_list ) ;
1999-12-13 13:27:58 +00:00
return ( ip_list ! = NULL ) ;
}
1996-05-04 07:50:46 +00:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * argv [ ] )
{
int opt ;
1996-06-29 18:49:20 +00:00
unsigned int lookup_type = 0x0 ;
1996-05-04 07:50:46 +00:00
pstring lookup ;
extern int optind ;
extern char * optarg ;
BOOL find_master = False ;
int i ;
1996-10-02 14:09:22 +00:00
static pstring servicesf = CONFIGFILE ;
1997-10-10 01:32:26 +00:00
BOOL lookup_by_ip = False ;
1999-12-13 13:27:58 +00:00
int commandline_debuglevel = - 2 ;
1997-11-19 23:50:02 +00:00
1996-05-04 07:50:46 +00:00
DEBUGLEVEL = 1 ;
* lookup = 0 ;
TimeInit ( ) ;
setup_logging ( argv [ 0 ] , True ) ;
1997-07-18 20:21:32 +00:00
charset_initialise ( ) ;
1996-05-04 07:50:46 +00:00
1999-12-13 13:27:58 +00:00
while ( ( opt = getopt ( argc , argv , " d:B:U:i:s:SMrhART " ) ) ! = EOF )
1996-05-04 07:50:46 +00:00
switch ( opt )
{
case ' B ' :
1996-10-07 11:06:34 +00:00
bcast_addr = * interpret_addr2 ( optarg ) ;
got_bcast = True ;
1997-10-26 07:50:28 +00:00
use_bcast = True ;
break ;
case ' U ' :
bcast_addr = * interpret_addr2 ( optarg ) ;
got_bcast = True ;
use_bcast = False ;
1996-05-04 07:50:46 +00:00
break ;
1999-12-13 13:27:58 +00:00
case ' T ' :
translate_addresses = ! translate_addresses ;
break ;
1996-05-04 07:50:46 +00:00
case ' i ' :
2000-01-07 06:55:36 +00:00
{
extern pstring global_scope ;
pstrcpy ( global_scope , optarg ) ;
strupper ( global_scope ) ;
}
break ;
1996-05-04 07:50:46 +00:00
case ' M ' :
find_master = True ;
break ;
case ' S ' :
find_status = True ;
break ;
1997-11-19 23:50:02 +00:00
case ' R ' :
recursion_desired = True ;
break ;
1996-05-04 07:50:46 +00:00
case ' d ' :
1999-12-13 13:27:58 +00:00
commandline_debuglevel = DEBUGLEVEL = atoi ( optarg ) ;
1996-05-04 07:50:46 +00:00
break ;
1996-10-02 14:09:22 +00:00
case ' s ' :
1997-09-26 18:55:29 +00:00
pstrcpy ( servicesf , optarg ) ;
1996-10-02 14:09:22 +00:00
break ;
1997-10-04 01:07:47 +00:00
case ' r ' :
1999-12-13 13:27:58 +00:00
RootPort = - 1 ;
1997-10-04 01:07:47 +00:00
break ;
1996-05-04 07:50:46 +00:00
case ' h ' :
usage ( ) ;
exit ( 0 ) ;
break ;
1997-10-10 01:32:26 +00:00
case ' A ' :
lookup_by_ip = True ;
break ;
1996-05-04 07:50:46 +00:00
default :
usage ( ) ;
exit ( 1 ) ;
}
if ( argc < 2 ) {
usage ( ) ;
exit ( 1 ) ;
}
1998-03-14 08:27:41 +00:00
if ( ! lp_load ( servicesf , True , False , False ) ) {
1996-10-02 14:09:22 +00:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " , servicesf ) ;
}
1999-12-13 13:27:58 +00:00
/*
* Ensure we reset DEBUGLEVEL if someone specified it
* on the command line .
*/
1996-10-07 11:06:34 +00:00
1999-12-13 13:27:58 +00:00
if ( commandline_debuglevel ! = - 2 )
DEBUGLEVEL = commandline_debuglevel ;
1996-05-04 07:50:46 +00:00
1999-12-13 13:27:58 +00:00
load_interfaces ( ) ;
if ( ! open_sockets ( ) ) return ( 1 ) ;
1996-05-04 07:50:46 +00:00
for ( i = optind ; i < argc ; i + + )
1996-08-01 17:49:40 +00:00
{
1996-05-04 07:50:46 +00:00
char * p ;
struct in_addr ip ;
1997-09-26 18:55:29 +00:00
fstrcpy ( lookup , argv [ i ] ) ;
1996-05-04 07:50:46 +00:00
1997-10-10 01:32:26 +00:00
if ( lookup_by_ip )
{
1998-05-12 00:55:32 +00:00
fstrcpy ( lookup , " * " ) ;
1997-10-10 01:32:26 +00:00
ip = * interpret_addr2 ( argv [ i ] ) ;
printf ( " Looking up status of %s \n " , inet_ntoa ( ip ) ) ;
2000-01-03 03:17:16 +00:00
name_status ( ServerFD , lookup , lookup_type , True , ip , NULL , NULL ) ;
1997-10-10 01:32:26 +00:00
printf ( " \n " ) ;
continue ;
}
1996-05-04 07:50:46 +00:00
if ( find_master ) {
if ( * lookup = = ' - ' ) {
1998-05-12 00:55:32 +00:00
fstrcpy ( lookup , " \01 \02 __MSBROWSE__ \02 " ) ;
1996-05-04 07:50:46 +00:00
lookup_type = 1 ;
} else {
1996-11-06 20:14:24 +00:00
lookup_type = 0x1d ;
1996-05-04 07:50:46 +00:00
}
}
p = strchr ( lookup , ' # ' ) ;
if ( p ) {
2000-06-13 20:41:15 +00:00
* p = ' \0 ' ;
sscanf ( + + p , " %x " , & lookup_type ) ;
1996-05-04 07:50:46 +00:00
}
1999-12-13 13:27:58 +00:00
if ( ! query_one ( lookup , lookup_type ) ) {
2000-06-13 20:41:15 +00:00
printf ( " name_query failed to find name %s " , lookup ) ;
if ( 0 ! = lookup_type )
printf ( " #%02x " , lookup_type ) ;
printf ( " \n " ) ;
1997-09-11 20:17:32 +00:00
}
1996-08-01 17:49:40 +00:00
}
1997-12-13 14:16:07 +00:00
1996-05-04 07:50:46 +00:00
return ( 0 ) ;
}