1/*
2   Unix SMB/CIFS implementation.
3   NBT client - used to lookup netbios names
4   Copyright (C) Andrew Tridgell 1994-1998
5   Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21*/
22
23#define NO_SYSLOG
24
25#include "includes.h"
26
27extern BOOL AllowDebugChange;
28
29static BOOL give_flags = False;
30static BOOL use_bcast = True;
31static BOOL got_bcast = False;
32static struct in_addr bcast_addr;
33static BOOL recursion_desired = False;
34static BOOL translate_addresses = False;
35static int ServerFD= -1;
36static int RootPort = False;
37static BOOL find_status=False;
38
39/****************************************************************************
40  open the socket communication
41  **************************************************************************/
42static BOOL open_sockets(void)
43{
44  ServerFD = open_socket_in( SOCK_DGRAM,
45                             (RootPort ? 137 : 0),
46                             (RootPort ?   0 : 3),
47                             interpret_addr(lp_socket_address()), True );
48
49  if (ServerFD == -1)
50    return(False);
51
52  set_socket_options( ServerFD, "SO_BROADCAST" );
53
54  DEBUG(3, ("Socket opened.\n"));
55  return True;
56}
57
58/****************************************************************************
59turn a node status flags field into a string
60****************************************************************************/
61static char *node_status_flags(unsigned char flags)
62{
63	static fstring ret;
64	fstrcpy(ret,"");
65
66	fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
67	if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
68	if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
69	if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
70	if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
71	if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
72	if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
73	if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
74	if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
75
76	return ret;
77}
78
79/****************************************************************************
80turn the NMB Query flags into a string
81****************************************************************************/
82static char *query_flags(int flags)
83{
84	static fstring ret1;
85	fstrcpy(ret1, "");
86
87	if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
88	if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
89	if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
90	if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
91	if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
92	if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
93
94	return ret1;
95}
96
97/****************************************************************************
98do a node status query
99****************************************************************************/
100static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
101{
102	struct nmb_name nname;
103	int count, i, j;
104	struct node_status *status;
105	struct node_status_extra extra;
106	fstring cleanname;
107
108	d_printf("Looking up status of %s\n",inet_ntoa(ip));
109	make_nmb_name(&nname, name, type);
110	status = node_status_query(fd,&nname,ip, &count, &extra);
111	if (status) {
112		for (i=0;i<count;i++) {
113			pull_ascii_fstring(cleanname, status[i].name);
114			for (j=0;cleanname[j];j++) {
115				if (!isprint((int)cleanname[j])) cleanname[j] = '.';
116			}
117			d_printf("\t%-15s <%02x> - %s\n",
118			       cleanname,status[i].type,
119			       node_status_flags(status[i].flags));
120		}
121		d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
122				extra.mac_addr[0], extra.mac_addr[1],
123				extra.mac_addr[2], extra.mac_addr[3],
124				extra.mac_addr[4], extra.mac_addr[5]);
125		d_printf("\n");
126		SAFE_FREE(status);
127	} else {
128		d_printf("No reply from %s\n\n",inet_ntoa(ip));
129	}
130}
131
132
133/****************************************************************************
134send out one query
135****************************************************************************/
136static BOOL query_one(const char *lookup, unsigned int lookup_type)
137{
138	int j, count, flags = 0;
139	struct in_addr *ip_list=NULL;
140
141	if (got_bcast) {
142		d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
143		ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
144				     use_bcast?True:recursion_desired,
145				     bcast_addr,&count, &flags, NULL);
146	} else {
147		struct in_addr *bcast;
148		for (j=iface_count() - 1;
149		     !ip_list && j >= 0;
150		     j--) {
151			bcast = iface_n_bcast(j);
152			d_printf("querying %s on %s\n",
153			       lookup, inet_ntoa(*bcast));
154			ip_list = name_query(ServerFD,lookup,lookup_type,
155					     use_bcast,
156					     use_bcast?True:recursion_desired,
157					     *bcast,&count, &flags, NULL);
158		}
159	}
160
161	if (!ip_list) return False;
162
163	if (give_flags)
164	  d_printf("Flags: %s\n", query_flags(flags));
165
166	for (j=0;j<count;j++) {
167		if (translate_addresses) {
168			struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
169			if (host) {
170				d_printf("%s, ", host -> h_name);
171			}
172		}
173		d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
174	}
175
176	/* We can only do find_status if the ip address returned
177	   was valid - ie. name_query returned true.
178	*/
179	if (find_status) {
180		do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
181	}
182
183	safe_free(ip_list);
184
185	return (ip_list != NULL);
186}
187
188
189/****************************************************************************
190  main program
191****************************************************************************/
192int main(int argc,char *argv[])
193{
194  int opt;
195  unsigned int lookup_type = 0x0;
196  fstring lookup;
197  static BOOL find_master=False;
198  static BOOL lookup_by_ip = False;
199  poptContext pc;
200
201  struct poptOption long_options[] = {
202	  POPT_AUTOHELP
203	  { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
204	  { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
205	  { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
206	  { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
207	  { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
208	  { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
209	  { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
210	  { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
211	  { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
212	  POPT_COMMON_SAMBA
213	  POPT_COMMON_CONNECTION
214	  { 0, 0, 0, 0 }
215  };
216
217  *lookup = 0;
218
219  setup_logging(argv[0],True);
220
221  pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options,
222					  POPT_CONTEXT_KEEP_FIRST);
223
224  poptSetOtherOptionHelp(pc, "<NODE> ...");
225
226  while ((opt = poptGetNextOpt(pc)) != -1) {
227	  switch (opt) {
228	  case 'B':
229		  bcast_addr = *interpret_addr2(poptGetOptArg(pc));
230		  got_bcast = True;
231		  use_bcast = True;
232		  break;
233	  case 'U':
234		  bcast_addr = *interpret_addr2(poptGetOptArg(pc));
235		  got_bcast = True;
236		  use_bcast = False;
237		  break;
238	  case 'T':
239		  translate_addresses = !translate_addresses;
240		  break;
241	  }
242  }
243
244  poptGetArg(pc); /* Remove argv[0] */
245
246  if(!poptPeekArg(pc)) {
247	  poptPrintUsage(pc, stderr, 0);
248	  exit(1);
249  }
250
251  if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
252	  fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
253  }
254
255  load_interfaces();
256  if (!open_sockets()) return(1);
257
258  while(poptPeekArg(pc))
259  {
260	  char *p;
261	  struct in_addr ip;
262
263	  fstrcpy(lookup,poptGetArg(pc));
264
265	  if(lookup_by_ip)
266	  {
267		  ip = *interpret_addr2(lookup);
268		  fstrcpy(lookup,"*");
269		  do_node_status(ServerFD, lookup, lookup_type, ip);
270		  continue;
271	  }
272
273	  if (find_master) {
274		  if (*lookup == '-') {
275			  fstrcpy(lookup,"\01\02__MSBROWSE__\02");
276			  lookup_type = 1;
277		  } else {
278			  lookup_type = 0x1d;
279		  }
280	  }
281
282	  p = strchr_m(lookup,'#');
283	  if (p) {
284		  *p = '\0';
285		  sscanf(++p,"%x",&lookup_type);
286	  }
287
288	  if (!query_one(lookup, lookup_type)) {
289		  d_printf( "name_query failed to find name %s", lookup );
290		  if( 0 != lookup_type )
291			  d_printf( "#%02x", lookup_type );
292		  d_printf( "\n" );
293	  }
294  }
295
296  poptFreeContext(pc);
297
298  return(0);
299}
300