Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[gd/samba/.git] / source4 / utils / nmblookup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT client - used to lookup netbios names
5
6    Copyright (C) Andrew Tridgell 1994-2005
7    Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
8    
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.
13    
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.
18    
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/>.
21    
22 */
23
24 #include "includes.h"
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"
33
34 /* command line options */
35 static struct {
36         const char *broadcast_address;
37         const char *unicast_address;
38         bool find_master;
39         bool wins_lookup;
40         bool node_status;
41         bool root_port;
42         bool lookup_by_ip;
43         bool case_sensitive;
44 } options;
45
46 /*
47   clean any binary from a node name
48 */
49 static const char *clean_name(TALLOC_CTX *mem_ctx, const char *name)
50 {
51         char *ret = talloc_strdup(mem_ctx, name);
52         int i;
53         for (i=0;ret[i];i++) {
54                 if (!isprint((unsigned char)ret[i])) ret[i] = '.';
55         }
56         return ret;
57 }
58
59 /*
60   turn a node status flags field into a string
61 */
62 static char *node_status_flags(TALLOC_CTX *mem_ctx, uint16_t flags)
63 {
64         char *ret;
65         const char *group = "       ";
66         const char *type = "B";
67
68         if (flags & NBT_NM_GROUP) {
69                 group = "<GROUP>";
70         }
71
72         switch (flags & NBT_NM_OWNER_TYPE) {
73         case NBT_NODE_B: 
74                 type = "B";
75                 break;
76         case NBT_NODE_P: 
77                 type = "P";
78                 break;
79         case NBT_NODE_M: 
80                 type = "M";
81                 break;
82         case NBT_NODE_H: 
83                 type = "H";
84                 break;
85         }
86
87         ret = talloc_asprintf(mem_ctx, "%s %s", group, type);
88
89         if (flags & NBT_NM_DEREGISTER) {
90                 ret = talloc_asprintf_append_buffer(ret, " <DEREGISTERING>");
91         }
92         if (flags & NBT_NM_CONFLICT) {
93                 ret = talloc_asprintf_append_buffer(ret, " <CONFLICT>");
94         }
95         if (flags & NBT_NM_ACTIVE) {
96                 ret = talloc_asprintf_append_buffer(ret, " <ACTIVE>");
97         }
98         if (flags & NBT_NM_PERMANENT) {
99                 ret = talloc_asprintf_append_buffer(ret, " <PERMANENT>");
100         }
101         
102         return ret;
103 }
104
105 /* do a single node status */
106 static bool do_node_status(struct nbt_name_socket *nbtsock,
107                            const char *addr, uint16_t port)
108 {
109         struct nbt_name_status io;
110         NTSTATUS status;
111
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;
116         io.in.dest_port = port;
117         io.in.timeout = 1;
118         io.in.retries = 2;
119
120         status = nbt_name_status(nbtsock, nbtsock, &io);
121         if (NT_STATUS_IS_OK(status)) {
122                 int i;
123                 printf("Node status reply from %s\n",
124                        io.out.reply_from);
125                 for (i=0;i<io.out.status.num_names;i++) {
126                         d_printf("\t%-16s <%02x>  %s\n", 
127                                  clean_name(nbtsock, io.out.status.names[i].name),
128                                  io.out.status.names[i].type,
129                                  node_status_flags(nbtsock, io.out.status.names[i].nb_flags));
130                 }
131                 printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
132                        io.out.status.statistics.unit_id[0],
133                        io.out.status.statistics.unit_id[1],
134                        io.out.status.statistics.unit_id[2],
135                        io.out.status.statistics.unit_id[3],
136                        io.out.status.statistics.unit_id[4],
137                        io.out.status.statistics.unit_id[5]);
138                 return true;
139         }
140
141         return false;
142 }
143
144 /* do a single node query */
145 static NTSTATUS do_node_query(struct nbt_name_socket *nbtsock,
146                               const char *addr, 
147                               uint16_t port,
148                               const char *node_name, 
149                               enum nbt_name_type node_type,
150                               bool broadcast)
151 {
152         struct nbt_name_query io;
153         NTSTATUS status;
154         int i;
155
156         io.in.name.name = node_name;
157         io.in.name.type = node_type;
158         io.in.name.scope = NULL;
159         io.in.dest_addr = addr;
160         io.in.dest_port = port;
161         io.in.broadcast = broadcast;
162         io.in.wins_lookup = options.wins_lookup;
163         io.in.timeout = 1;
164         io.in.retries = 2;
165
166         status = nbt_name_query(nbtsock, nbtsock, &io);
167         NT_STATUS_NOT_OK_RETURN(status);
168
169         for (i=0;i<io.out.num_addrs;i++) {
170                 printf("%s %s<%02x>\n",
171                        io.out.reply_addrs[i],
172                        io.out.name.name,
173                        io.out.name.type);
174         }
175         if (options.node_status && io.out.num_addrs > 0) {
176                 do_node_status(nbtsock, io.out.reply_addrs[0], port);
177         }
178
179         return status;
180 }
181
182
183 static bool process_one(struct loadparm_context *lp_ctx, 
184                         struct interface *ifaces, const char *name, int nbt_port)
185 {
186         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
187         enum nbt_name_type node_type = NBT_NAME_CLIENT;
188         char *node_name, *p;
189         struct socket_address *all_zero_addr;
190         struct nbt_name_socket *nbtsock;
191         NTSTATUS status = NT_STATUS_OK;
192         bool ret = true;
193
194         if (!options.case_sensitive) {
195                 name = strupper_talloc(tmp_ctx, name);
196         }
197         
198         if (options.find_master) {
199                 node_type = NBT_NAME_MASTER;
200                 if (*name == '-' || *name == '_') {
201                         name = "\01\02__MSBROWSE__\02";
202                         node_type = NBT_NAME_MS;
203                 }
204         }
205
206         p = strchr(name, '#');
207         if (p) {
208                 node_name = talloc_strndup(tmp_ctx, name, PTR_DIFF(p,name));
209                 node_type = (enum nbt_name_type)strtol(p+1, NULL, 16);
210         } else {
211                 node_name = talloc_strdup(tmp_ctx, name);
212         }
213
214         nbtsock = nbt_name_socket_init(tmp_ctx, NULL, lp_iconv_convenience(lp_ctx));
215         
216         if (options.root_port) {
217                 all_zero_addr = socket_address_from_strings(tmp_ctx, nbtsock->sock->backend_name, 
218                                                             "0.0.0.0", NBT_NAME_SERVICE_PORT);
219                 
220                 if (!all_zero_addr) {
221                         talloc_free(tmp_ctx);
222                         return false;
223                 }
224
225                 status = socket_listen(nbtsock->sock, all_zero_addr, 0, 0);
226                 if (!NT_STATUS_IS_OK(status)) {
227                         printf("Failed to bind to local port 137 - %s\n", nt_errstr(status));
228                         talloc_free(tmp_ctx);
229                         return false;
230                 }
231         }
232
233         if (options.lookup_by_ip) {
234                 ret = do_node_status(nbtsock, name, nbt_port);
235                 talloc_free(tmp_ctx);
236                 return ret;
237         }
238
239         if (options.broadcast_address) {
240                 status = do_node_query(nbtsock, options.broadcast_address, nbt_port,
241                                        node_name, node_type, true);
242         } else if (options.unicast_address) {
243                 status = do_node_query(nbtsock, options.unicast_address, 
244                                        nbt_port, node_name, node_type, false);
245         } else {
246                 int i, num_interfaces;
247
248                 num_interfaces = iface_count(ifaces);
249                 for (i=0;i<num_interfaces;i++) {
250                         const char *bcast = iface_n_bcast(ifaces, i);
251                         if (bcast == NULL) continue;
252                         status = do_node_query(nbtsock, bcast, nbt_port, 
253                                                node_name, node_type, true);
254                         if (NT_STATUS_IS_OK(status)) break;
255                 }
256         }
257
258         if (!NT_STATUS_IS_OK(status)) {
259                 printf("Lookup failed - %s\n", nt_errstr(status));
260                 ret = false;
261         }
262
263         talloc_free(tmp_ctx);
264         return ret;
265 }
266
267 /*
268   main program
269 */
270 int main(int argc, const char *argv[])
271 {
272         bool ret = true;
273         struct interface *ifaces;
274         poptContext pc;
275         int opt;
276         enum {
277                 OPT_BROADCAST_ADDRESS   = 1000,
278                 OPT_UNICAST_ADDRESS,
279                 OPT_FIND_MASTER,
280                 OPT_WINS_LOOKUP,
281                 OPT_NODE_STATUS,
282                 OPT_ROOT_PORT,
283                 OPT_LOOKUP_BY_IP,
284                 OPT_CASE_SENSITIVE
285         };
286         struct poptOption long_options[] = {
287                 POPT_AUTOHELP
288                 { "broadcast", 'B', POPT_ARG_STRING, NULL, OPT_BROADCAST_ADDRESS,
289                   "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
290
291                 { "unicast", 'U', POPT_ARG_STRING, NULL, OPT_UNICAST_ADDRESS,
292                   "Specify address to use for unicast", NULL },
293
294                 { "master-browser", 'M', POPT_ARG_NONE, NULL, OPT_FIND_MASTER,
295                   "Search for a master browser", NULL },
296
297                 { "wins", 'W', POPT_ARG_NONE, NULL, OPT_WINS_LOOKUP,
298                   "Do a WINS lookup", NULL },
299
300                 { "status", 'S', POPT_ARG_NONE, NULL, OPT_NODE_STATUS, 
301                   "Lookup node status as well", NULL },
302
303                 { "root-port", 'r', POPT_ARG_NONE, NULL, OPT_ROOT_PORT, 
304                   "Use root port 137 (Win95 only replies to this)", NULL },
305
306                 { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, OPT_LOOKUP_BY_IP, 
307                   "Do a node status on <name> as an IP Address", NULL },
308
309                 { "case-sensitive", 0, POPT_ARG_NONE, NULL, OPT_CASE_SENSITIVE, 
310                   "Don't uppercase the name before sending", NULL },
311
312                 POPT_COMMON_SAMBA
313                 { 0, 0, 0, 0 }
314         };
315         
316         pc = poptGetContext("nmblookup", argc, argv, long_options, 
317                             POPT_CONTEXT_KEEP_FIRST);
318
319         poptSetOtherOptionHelp(pc, "<NODE> ...");
320
321         while ((opt = poptGetNextOpt(pc)) != -1) {
322                 switch(opt) {
323                 case OPT_BROADCAST_ADDRESS:
324                         options.broadcast_address = poptGetOptArg(pc);
325                         break;
326                 case OPT_UNICAST_ADDRESS:
327                         options.unicast_address = poptGetOptArg(pc);
328                         break;
329                 case OPT_FIND_MASTER:
330                         options.find_master = true;
331                         break;
332                 case OPT_WINS_LOOKUP:
333                         options.wins_lookup = true;
334                         break;
335                 case OPT_NODE_STATUS:
336                         options.node_status = true;
337                         break;
338                 case OPT_ROOT_PORT:
339                         options.root_port = true;
340                         break;
341                 case OPT_LOOKUP_BY_IP:
342                         options.lookup_by_ip = true;
343                         break;
344                 case OPT_CASE_SENSITIVE:
345                         options.case_sensitive = true;
346                         break;
347                 }
348         }
349
350         /* swallow argv[0] */
351         poptGetArg(pc);
352
353         if(!poptPeekArg(pc)) { 
354                 poptPrintUsage(pc, stderr, 0);
355                 exit(1);
356         }
357
358         load_interfaces(NULL, lp_interfaces(cmdline_lp_ctx), &ifaces);
359         
360         while (poptPeekArg(pc)) {
361                 const char *name = poptGetArg(pc);
362
363                 ret &= process_one(cmdline_lp_ctx, ifaces, name, lp_nbt_port(cmdline_lp_ctx));
364         }
365
366         talloc_free(ifaces);
367
368         poptFreeContext(pc);
369
370         if (!ret) {
371                 return 1;
372         }
373
374         return 0;
375 }