2 Unix SMB/CIFS implementation.
4 NBT client - used to lookup netbios names
6 Copyright (C) Andrew Tridgell 1994-2005
7 Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/cmdline/popt_common.h"
26 #include "lib/socket/socket.h"
27 #include "system/network.h"
28 #include "system/locale.h"
29 #include "lib/socket/netif.h"
30 #include "librpc/gen_ndr/nbt.h"
31 #include "libcli/nbt/libnbt.h"
32 #include "param/param.h"
34 /* command line options */
36 const char *broadcast_address;
37 const char *unicast_address;
47 clean any binary from a node name
49 static const char *clean_name(TALLOC_CTX *mem_ctx, const char *name)
51 char *ret = talloc_strdup(mem_ctx, name);
53 for (i=0;ret[i];i++) {
54 if (!isprint((unsigned char)ret[i])) ret[i] = '.';
60 turn a node status flags field into a string
62 static char *node_status_flags(TALLOC_CTX *mem_ctx, uint16_t flags)
65 const char *group = " ";
66 const char *type = "B";
68 if (flags & NBT_NM_GROUP) {
72 switch (flags & NBT_NM_OWNER_TYPE) {
87 ret = talloc_asprintf(mem_ctx, "%s %s", group, type);
89 if (flags & NBT_NM_DEREGISTER) {
90 ret = talloc_asprintf_append_buffer(ret, " <DEREGISTERING>");
92 if (flags & NBT_NM_CONFLICT) {
93 ret = talloc_asprintf_append_buffer(ret, " <CONFLICT>");
95 if (flags & NBT_NM_ACTIVE) {
96 ret = talloc_asprintf_append_buffer(ret, " <ACTIVE>");
98 if (flags & NBT_NM_PERMANENT) {
99 ret = talloc_asprintf_append_buffer(ret, " <PERMANENT>");
105 /* do a single node status */
106 static bool do_node_status(struct nbt_name_socket *nbtsock,
109 struct nbt_name_status io;
112 io.in.name.name = "*";
113 io.in.name.type = NBT_NAME_CLIENT;
114 io.in.name.scope = NULL;
115 io.in.dest_addr = addr;
119 status = nbt_name_status(nbtsock, nbtsock, &io);
120 if (NT_STATUS_IS_OK(status)) {
122 printf("Node status reply from %s\n",
124 for (i=0;i<io.out.status.num_names;i++) {
125 d_printf("\t%-16s <%02x> %s\n",
126 clean_name(nbtsock, io.out.status.names[i].name),
127 io.out.status.names[i].type,
128 node_status_flags(nbtsock, io.out.status.names[i].nb_flags));
130 printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
131 io.out.status.statistics.unit_id[0],
132 io.out.status.statistics.unit_id[1],
133 io.out.status.statistics.unit_id[2],
134 io.out.status.statistics.unit_id[3],
135 io.out.status.statistics.unit_id[4],
136 io.out.status.statistics.unit_id[5]);
143 /* do a single node query */
144 static NTSTATUS do_node_query(struct nbt_name_socket *nbtsock,
147 const char *node_name,
148 enum nbt_name_type node_type,
151 struct nbt_name_query io;
155 io.in.name.name = node_name;
156 io.in.name.type = node_type;
157 io.in.name.scope = NULL;
158 io.in.dest_addr = addr;
159 io.in.dest_port = port;
160 io.in.broadcast = broadcast;
161 io.in.wins_lookup = options.wins_lookup;
165 status = nbt_name_query(nbtsock, nbtsock, &io);
166 NT_STATUS_NOT_OK_RETURN(status);
168 for (i=0;i<io.out.num_addrs;i++) {
169 printf("%s %s<%02x>\n",
170 io.out.reply_addrs[i],
174 if (options.node_status && io.out.num_addrs > 0) {
175 do_node_status(nbtsock, io.out.reply_addrs[0]);
182 static bool process_one(const char *name, int nbt_port)
184 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
185 enum nbt_name_type node_type = NBT_NAME_CLIENT;
187 struct socket_address *all_zero_addr;
188 struct nbt_name_socket *nbtsock;
189 NTSTATUS status = NT_STATUS_OK;
192 if (!options.case_sensitive) {
193 name = strupper_talloc(tmp_ctx, name);
196 if (options.find_master) {
197 node_type = NBT_NAME_MASTER;
198 if (*name == '-' || *name == '_') {
199 name = "\01\02__MSBROWSE__\02";
200 node_type = NBT_NAME_MS;
204 p = strchr(name, '#');
206 node_name = talloc_strndup(tmp_ctx, name, PTR_DIFF(p,name));
207 node_type = (enum nbt_name_type)strtol(p+1, NULL, 16);
209 node_name = talloc_strdup(tmp_ctx, name);
212 nbtsock = nbt_name_socket_init(tmp_ctx, NULL);
214 if (options.root_port) {
215 all_zero_addr = socket_address_from_strings(tmp_ctx, nbtsock->sock->backend_name,
216 "0.0.0.0", NBT_NAME_SERVICE_PORT);
218 if (!all_zero_addr) {
219 talloc_free(tmp_ctx);
223 status = socket_listen(nbtsock->sock, all_zero_addr, 0, 0);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("Failed to bind to local port 137 - %s\n", nt_errstr(status));
226 talloc_free(tmp_ctx);
231 if (options.lookup_by_ip) {
232 ret = do_node_status(nbtsock, name);
233 talloc_free(tmp_ctx);
237 if (options.broadcast_address) {
238 status = do_node_query(nbtsock, options.broadcast_address, nbt_port,
239 node_name, node_type, true);
240 } else if (options.unicast_address) {
241 status = do_node_query(nbtsock, options.unicast_address,
242 nbt_port, node_name, node_type, false);
244 int i, num_interfaces = iface_count(global_loadparm);
245 for (i=0;i<num_interfaces;i++) {
246 const char *bcast = iface_n_bcast(global_loadparm, i);
247 if (bcast == NULL) continue;
248 status = do_node_query(nbtsock, bcast, nbt_port,
249 node_name, node_type, true);
250 if (NT_STATUS_IS_OK(status)) break;
254 if (!NT_STATUS_IS_OK(status)) {
255 printf("Lookup failed - %s\n", nt_errstr(status));
259 talloc_free(tmp_ctx);
266 int main(int argc, const char *argv[])
272 OPT_BROADCAST_ADDRESS = 1000,
281 struct poptOption long_options[] = {
283 { "broadcast", 'B', POPT_ARG_STRING, NULL, OPT_BROADCAST_ADDRESS,
284 "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
286 { "unicast", 'U', POPT_ARG_STRING, NULL, OPT_UNICAST_ADDRESS,
287 "Specify address to use for unicast", NULL },
289 { "master-browser", 'M', POPT_ARG_NONE, NULL, OPT_FIND_MASTER,
290 "Search for a master browser", NULL },
292 { "wins", 'W', POPT_ARG_NONE, NULL, OPT_WINS_LOOKUP,
293 "Do a WINS lookup", NULL },
295 { "status", 'S', POPT_ARG_NONE, NULL, OPT_NODE_STATUS,
296 "Lookup node status as well", NULL },
298 { "root-port", 'r', POPT_ARG_NONE, NULL, OPT_ROOT_PORT,
299 "Use root port 137 (Win95 only replies to this)", NULL },
301 { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, OPT_LOOKUP_BY_IP,
302 "Do a node status on <name> as an IP Address", NULL },
304 { "case-sensitive", 0, POPT_ARG_NONE, NULL, OPT_CASE_SENSITIVE,
305 "Don't uppercase the name before sending", NULL },
311 pc = poptGetContext("nmblookup", argc, argv, long_options,
312 POPT_CONTEXT_KEEP_FIRST);
314 poptSetOtherOptionHelp(pc, "<NODE> ...");
316 while ((opt = poptGetNextOpt(pc)) != -1) {
318 case OPT_BROADCAST_ADDRESS:
319 options.broadcast_address = poptGetOptArg(pc);
321 case OPT_UNICAST_ADDRESS:
322 options.unicast_address = poptGetOptArg(pc);
324 case OPT_FIND_MASTER:
325 options.find_master = true;
327 case OPT_WINS_LOOKUP:
328 options.wins_lookup = true;
330 case OPT_NODE_STATUS:
331 options.node_status = true;
334 options.root_port = true;
336 case OPT_LOOKUP_BY_IP:
337 options.lookup_by_ip = true;
339 case OPT_CASE_SENSITIVE:
340 options.case_sensitive = true;
345 /* swallow argv[0] */
348 if(!poptPeekArg(pc)) {
349 poptPrintUsage(pc, stderr, 0);
353 while (poptPeekArg(pc)) {
354 const char *name = poptGetArg(pc);
356 ret &= process_one(name, lp_nbt_port(global_loadparm));