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)
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/cmdline/cmdline.h"
24 #include "libsmb/nmblib.h"
25 #include "libsmb/namequery.h"
26 #include "lib/util/string_wrappers.h"
28 static bool give_flags
= false;
29 static bool use_bcast
= true;
30 static bool got_bcast
= false;
31 static struct sockaddr_storage bcast_addr
;
32 static bool recursion_desired
= false;
33 static bool translate_addresses
= false;
34 static int ServerFD
= -1;
35 static bool RootPort
= false;
36 static bool find_status
= false;
38 /****************************************************************************
39 Open the socket communication.
40 **************************************************************************/
42 static bool open_sockets(void)
44 struct sockaddr_storage ss
;
45 const char *sock_addr
= lp_nbt_client_socket_address();
47 if (!interpret_string_addr(&ss
, sock_addr
,
48 AI_NUMERICHOST
|AI_PASSIVE
)) {
49 DEBUG(0,("open_sockets: unable to get socket address "
50 "from string %s", sock_addr
));
53 ServerFD
= open_socket_in(
54 SOCK_DGRAM
, &ss
, (RootPort
? 137 : 0), true);
57 DBG_ERR("open_socket_in failed: %s\n",
60 DBG_NOTICE("open_socket_in failed: %s\n",
66 set_socket_options( ServerFD
, "SO_BROADCAST" );
68 DEBUG(3, ("Socket opened.\n"));
72 /****************************************************************************
73 turn a node status flags field into a string
74 ****************************************************************************/
75 static char *node_status_flags(unsigned char flags
)
80 fstrcat(ret
, (flags
& 0x80) ? "<GROUP> " : " ");
81 if ((flags
& 0x60) == 0x00) fstrcat(ret
,"B ");
82 if ((flags
& 0x60) == 0x20) fstrcat(ret
,"P ");
83 if ((flags
& 0x60) == 0x40) fstrcat(ret
,"M ");
84 if ((flags
& 0x60) == 0x60) fstrcat(ret
,"H ");
85 if (flags
& 0x10) fstrcat(ret
,"<DEREGISTERING> ");
86 if (flags
& 0x08) fstrcat(ret
,"<CONFLICT> ");
87 if (flags
& 0x04) fstrcat(ret
,"<ACTIVE> ");
88 if (flags
& 0x02) fstrcat(ret
,"<PERMANENT> ");
93 /****************************************************************************
94 Turn the NMB Query flags into a string.
95 ****************************************************************************/
97 static char *query_flags(int flags
)
102 if (flags
& NM_FLAGS_RS
) fstrcat(ret1
, "Response ");
103 if (flags
& NM_FLAGS_AA
) fstrcat(ret1
, "Authoritative ");
104 if (flags
& NM_FLAGS_TC
) fstrcat(ret1
, "Truncated ");
105 if (flags
& NM_FLAGS_RD
) fstrcat(ret1
, "Recursion_Desired ");
106 if (flags
& NM_FLAGS_RA
) fstrcat(ret1
, "Recursion_Available ");
107 if (flags
& NM_FLAGS_B
) fstrcat(ret1
, "Broadcast ");
112 /****************************************************************************
113 Do a node status query.
114 ****************************************************************************/
116 static bool do_node_status(const char *name
,
118 struct sockaddr_storage
*pss
)
120 struct nmb_name nname
;
123 struct node_status
*addrs
;
124 struct node_status_extra extra
;
126 char addr
[INET6_ADDRSTRLEN
];
129 print_sockaddr(addr
, sizeof(addr
), pss
);
130 d_printf("Looking up status of %s\n",addr
);
131 make_nmb_name(&nname
, name
, type
);
132 status
= node_status_query(talloc_tos(), &nname
, pss
,
133 &addrs
, &count
, &extra
);
134 if (NT_STATUS_IS_OK(status
)) {
135 for (i
=0;i
<count
;i
++) {
136 pull_ascii_fstring(cleanname
, addrs
[i
].name
);
137 for (j
=0;cleanname
[j
];j
++) {
138 if (!isprint((int)cleanname
[j
])) {
142 d_printf("\t%-15s <%02x> - %s\n",
143 cleanname
,addrs
[i
].type
,
144 node_status_flags(addrs
[i
].flags
));
146 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
147 extra
.mac_addr
[0], extra
.mac_addr
[1],
148 extra
.mac_addr
[2], extra
.mac_addr
[3],
149 extra
.mac_addr
[4], extra
.mac_addr
[5]);
154 d_printf("No reply from %s\n\n",addr
);
160 /****************************************************************************
162 ****************************************************************************/
164 static bool query_one(const char *lookup
, unsigned int lookup_type
)
168 struct sockaddr_storage
*ip_list
=NULL
;
169 NTSTATUS status
= NT_STATUS_NOT_FOUND
;
172 char addr
[INET6_ADDRSTRLEN
];
173 print_sockaddr(addr
, sizeof(addr
), &bcast_addr
);
174 d_printf("querying %s on %s\n", lookup
, addr
);
175 status
= name_query(lookup
,lookup_type
,use_bcast
,
176 use_bcast
?true:recursion_desired
,
177 &bcast_addr
, talloc_tos(),
178 &ip_list
, &count
, &flags
);
180 status
= name_resolve_bcast(talloc_tos(),
187 if (!NT_STATUS_IS_OK(status
)) {
192 d_printf("Flags: %s\n", query_flags(flags
));
195 for (j
=0;j
<count
;j
++) {
196 char addr
[INET6_ADDRSTRLEN
];
197 if (translate_addresses
) {
198 char h_name
[MAX_DNS_NAME_LENGTH
];
200 if (sys_getnameinfo((const struct sockaddr
*)&ip_list
[j
],
201 sizeof(struct sockaddr_storage
),
202 h_name
, sizeof(h_name
),
207 d_printf("%s, ", h_name
);
209 print_sockaddr(addr
, sizeof(addr
), &ip_list
[j
]);
210 d_printf("%s %s<%02x>\n", addr
,lookup
, lookup_type
);
211 /* We can only do find_status if the ip address returned
212 was valid - ie. name_query returned true.
215 if (!do_node_status(lookup
, lookup_type
, &ip_list
[j
])) {
216 status
= NT_STATUS_UNSUCCESSFUL
;
221 TALLOC_FREE(ip_list
);
223 return NT_STATUS_IS_OK(status
);
227 /****************************************************************************
229 ****************************************************************************/
230 enum nmblookup_cmdline_options
{
231 CMDLINE_RECURSIVE
= 1,
234 int main(int argc
, const char *argv
[])
237 unsigned int lookup_type
= 0x0;
239 static bool find_master
=False
;
240 static bool lookup_by_ip
= False
;
241 poptContext pc
= NULL
;
242 TALLOC_CTX
*frame
= talloc_stackframe();
246 struct poptOption long_options
[] = {
249 .longName
= "broadcast",
251 .argInfo
= POPT_ARG_STRING
,
254 .descrip
= "Specify address to use for broadcasts",
255 .argDescrip
= "BROADCAST-ADDRESS",
260 .argInfo
= POPT_ARG_NONE
,
263 .descrip
= "List the NMB flags returned",
266 .longName
= "unicast",
268 .argInfo
= POPT_ARG_STRING
,
271 .descrip
= "Specify address to use for unicast",
274 .longName
= "master-browser",
276 .argInfo
= POPT_ARG_NONE
,
279 .descrip
= "Search for a master browser",
282 .longName
= "recursion",
284 .argInfo
= POPT_ARG_NONE
,
286 .val
= CMDLINE_RECURSIVE
,
287 .descrip
= "Set recursion desired in package",
290 .longName
= "status",
292 .argInfo
= POPT_ARG_NONE
,
295 .descrip
= "Lookup node status as well",
298 .longName
= "translate",
300 .argInfo
= POPT_ARG_NONE
,
303 .descrip
= "Translate IP addresses into names",
306 .longName
= "root-port",
308 .argInfo
= POPT_ARG_NONE
,
311 .descrip
= "Use root port 137 (Win95 only replies to this)",
314 .longName
= "lookup-by-ip",
316 .argInfo
= POPT_ARG_NONE
,
319 .descrip
= "Do a node status on <name> as an IP Address",
322 POPT_COMMON_CONNECTION
331 ok
= samba_cmdline_init(frame
,
332 SAMBA_CMDLINE_CONFIG_CLIENT
,
333 false /* require_smbconf */);
335 DBG_ERR("Failed to init cmdline parser!\n");
340 pc
= samba_popt_get_context(getprogname(),
344 POPT_CONTEXT_KEEP_FIRST
);
346 DBG_ERR("Failed to setup popt context!\n");
351 poptSetOtherOptionHelp(pc
, "<NODE> ...");
353 while ((opt
= poptGetNextOpt(pc
)) != -1) {
361 case CMDLINE_RECURSIVE
:
362 recursion_desired
= true;
374 if (interpret_string_addr(&bcast_addr
,
382 if (interpret_string_addr(&bcast_addr
,
390 translate_addresses
= !translate_addresses
;
392 case POPT_ERROR_BADOPT
:
393 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
394 poptBadOption(pc
, 0), poptStrerror(opt
));
395 poptPrintUsage(pc
, stderr
, 0);
400 poptGetArg(pc
); /* Remove argv[0] */
402 if(!poptPeekArg(pc
)) {
403 poptPrintUsage(pc
, stderr
, 0);
408 if (!open_sockets()) {
413 while(poptPeekArg(pc
)) {
418 fstrcpy(lookup
,poptGetArg(pc
));
421 struct sockaddr_storage ss
;
422 ip
= interpret_addr2(lookup
);
423 in_addr_to_sockaddr_storage(&ss
, ip
);
425 if (!do_node_status(lookup
, lookup_type
, &ss
)) {
432 if (*lookup
== '-') {
433 fstrcpy(lookup
,"\01\02__MSBROWSE__\02");
440 p
= strchr_m(lookup
,'#');
443 sscanf(++p
,"%x",&lookup_type
);
446 nbt_len
= strlen(lookup
);
447 if (nbt_len
> MAX_NETBIOSNAME_LEN
- 1) {
448 d_printf("The specified netbios name [%s] is too long!\n",
454 if (!query_one(lookup
, lookup_type
)) {
456 d_printf( "name_query failed to find name %s", lookup
);
457 if( 0 != lookup_type
) {
458 d_printf( "#%02x", lookup_type
);