1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00
samba-mirror/source3/utils/nmblookup.c

243 lines
6.0 KiB
C
Raw Normal View History

/*
Unix SMB/Netbios implementation.
Version 1.9.
NBT client - used to lookup netbios names
'The mother of all checkins' :-). Jeremy Allison (jallison@whistle.com) Wed May 7 1997: Update for 1.9.17alpha1 release - 'browsefix release' designed to make browsing across subnets work. byteorder.h: Updated copyright to 1997. charcnv.c: Updated copyright to 1997. charset.c Updated copyright to 1997. charset.h Updated copyright to 1997. client.c Updated copyright to 1997. clientutil.c Updated copyright to 1997. dir.c Updated copyright to 1997. fault.c Updated copyright to 1997. includes.h Updated copyright to 1997. interface.c Updated copyright to 1997. ipc.c Updated copyright to 1997. kanji.c Updated copyright to 1997. kanji.h Updated copyright to 1997. loadparm.c Updated copyright to 1997. locking.c Updated copyright to 1997. mangle.c Updated copyright to 1997. message.c Updated copyright to 1997. nameannounce.c Made use of WINS subnet explicit. Added reset_announce_timer() so announcement can be made immediately when we become a master. Expanded code to do sync with dmb. namebrowse.c Removed redundent checks for AM_MASTER in sync code. Made use of WINS subnet explicit. namedbname.c Made use of WINS subnet explicit. namedbresp.c Made use of WINS subnet explicit. namedbserver.c Made use of WINS subnet explicit. namedbsubnet.c Explicitly add workgroup to WINS subnet when we become a dmb. Made use of WINS subnet explicit. namedbwork.c Made use of WINS subnet explicit. Removed redundent check_work_servertype() function. nameelect.c Explicitly add workgroup to WINS subnet when we become a master browser. Made use of WINS subnet explicit. namelogon.c Updated copyright to 1997. namepacket.c Updated copyright to 1997. namequery.c Updated copyright to 1997. nameresp.c Made use of WINS subnet explicit. Made nmbd fail if configured as master browser and one exists already. nameserv.c Made use of WINS subnet explicit. Remove redundent logon server and domain master code. nameserv.h Add emumerate subnet macros. nameservreply.c Made use of WINS subnet explicit. nameservresp.c Updated copyright to 1997. namework.c Made use of WINS subnet explicit. Updated code to add sync browser entries to add subnet parameter. nmbd.c Added sanity check for misconfigured nmbd. nmblib.c Updated copyright to 1997. nmblookup.c Updated copyright to 1997. nmbsync.c Removed redundent AM_ANY_MASTER check. params.c Updated copyright to 1997. password.c Updated copyright to 1997. pipes.c Updated copyright to 1997. predict.c Updated copyright to 1997. printing.c Updated copyright to 1997. proto.h Changed protos for new nmbd code. quotas.c Updated copyright to 1997. replace.c Updated copyright to 1997. reply.c Updated copyright to 1997. server.c Updated copyright to 1997. shmem.c Updated copyright to 1997. smb.h Updated copyright to 1997. smbencrypt.c Updated copyright to 1997. smbpasswd.c Updated copyright to 1997. smbrun.c Updated copyright to 1997. status.c Updated copyright to 1997. system.c Updated copyright to 1997. testparm.c Updated copyright to 1997. testprns.c Updated copyright to 1997. time.c Updated copyright to 1997. trans2.c Updated copyright to 1997. trans2.h Updated copyright to 1997. uid.c Updated copyright to 1997. username.c Updated copyright to 1997. util.c Updated copyright to 1997. version.h Changed to 1.9.17alpha1. (This used to be commit cf23a155a1315f50d488794a2caf88402bf3e3e6)
1997-05-08 05:14:17 +04:00
Copyright (C) Andrew Tridgell 1994-1997
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 02139, USA.
*/
#ifdef SYSLOG
#undef SYSLOG
#endif
#include "includes.h"
extern int DEBUGLEVEL;
extern pstring scope;
extern pstring myhostname;
extern struct in_addr ipzero;
int ServerFD= -1;
int RootPort = 0;
/****************************************************************************
open the socket communication
**************************************************************************/
static BOOL open_sockets(void)
{
ServerFD = open_socket_in( SOCK_DGRAM,
(RootPort ? 137 :0),
3,
interpret_addr(lp_socket_address()) );
if (ServerFD == -1)
return(False);
set_socket_options(ServerFD,"SO_BROADCAST");
DEBUG(3, ("Socket opened.\n"));
return True;
}
/****************************************************************************
initialise connect, service and file structs
****************************************************************************/
static BOOL init_structs(void )
{
if (!get_myname(myhostname,NULL))
return(False);
return True;
}
/****************************************************************************
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");
printf("\t-U unicast address the address to use for unicast\n");
printf("\t-M searches for a master browser\n");
printf("\t-R set recursion desired in packet\n");
printf("\t-S lookup node status as well\n");
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");
printf("\n");
}
/****************************************************************************
main program
****************************************************************************/
int main(int argc,char *argv[])
{
int opt;
luke's first attempt at using cvs accidentally updated the Makefile updated the name database structure (again!). this time, there is one name database per local interface. there is also a pseudo-interface on ip 255.255.255.255. its purpose is to store WINS name entries. all the local interface name databases store SELF names only. the WINS name database stores non-special browser names. added wins.dat file: records WINS entries in ascii format. this is reloaded when nmbd restarts. added repeating code for response packets. timer is in seconds only at the moment. updated the response queue code to deal with samba registering with a WINS server a bit better (added more cases when a response isn't received). tidied up the response packet processing code and expire_response_queue() code. added cross references between response received and await-response expired code. added over-zealous code that checks all machines that register with samba as a WINS server (every 10 minutes i think): to see whether they are still alive or not (see rfc1001.txt) bug reported by terry@ren.pc.athabascau.ca: DNSFAILed names _stay_ as DNSFAIL, even though the machine may come back up and REGISTER. removed update_from_reg() function. it's not necessary, and it does too much. added code that announces on each local interface samba's ttl as zero and servertype as zero when nmbd is kill -TERMed first attempt at putting the first functionality of samba browsing back in (remote subnets should have samba appear in a workgroup specified through the lmhosts file) lots of other miscellaneous tidying up / chopping about. (This used to be commit 7e8c60cfe54060860e5ce20b1c3b8ec6aa5c54da)
1996-06-29 22:49:20 +04:00
unsigned int lookup_type = 0x0;
pstring lookup;
extern int optind;
extern char *optarg;
BOOL find_master=False;
BOOL find_status=False;
int i;
static pstring servicesf = CONFIGFILE;
struct in_addr bcast_addr;
BOOL use_bcast = True;
BOOL got_bcast = False;
BOOL lookup_by_ip = False;
BOOL recursion_desired = False;
DEBUGLEVEL = 1;
*lookup = 0;
TimeInit();
setup_logging(argv[0],True);
charset_initialise();
while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhAR")) != EOF)
switch (opt)
{
case 'B':
iface_set_default(NULL,optarg,NULL);
bcast_addr = *interpret_addr2(optarg);
got_bcast = True;
use_bcast = True;
break;
case 'U':
iface_set_default(NULL,optarg,NULL);
bcast_addr = *interpret_addr2(optarg);
got_bcast = True;
use_bcast = False;
break;
case 'i':
fstrcpy(scope,optarg);
strupper(scope);
break;
case 'M':
find_master = True;
break;
case 'S':
find_status = True;
break;
case 'R':
recursion_desired = True;
break;
case 'd':
DEBUGLEVEL = atoi(optarg);
break;
case 's':
pstrcpy(servicesf, optarg);
break;
case 'r':
RootPort = -1;
break;
case 'h':
usage();
exit(0);
break;
case 'A':
lookup_by_ip = True;
break;
default:
usage();
exit(1);
}
if (argc < 2) {
usage();
exit(1);
}
if (!lp_load(servicesf,True)) {
fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
}
load_interfaces();
init_structs();
if (!open_sockets()) return(1);
if (!got_bcast)
bcast_addr = *iface_bcast(ipzero);
DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
for (i=optind;i<argc;i++)
{
int j, count, retries = 2;
char *p;
struct in_addr ip;
struct in_addr *ip_list;
fstrcpy(lookup,argv[i]);
if(lookup_by_ip)
{
strcpy(lookup,"*");
ip = *interpret_addr2(argv[i]);
printf("Looking up status of %s\n",inet_ntoa(ip));
name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
printf("\n");
continue;
}
if (find_master) {
if (*lookup == '-') {
strcpy(lookup,"\01\02__MSBROWSE__\02");
lookup_type = 1;
} else {
lookup_type = 0x1d;
}
}
p = strchr(lookup,'#');
if (p) {
*p = 0;
sscanf(p+1,"%x",&lookup_type);
retries = 1;
}
if ((ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,recursion_desired,
bcast_addr,&count,NULL))) {
for (j=0;j<count;j++)
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]));
name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
printf("\n");
}
} else {
printf("name_query failed to find name %s\n", lookup);
}
}
return(0);
}