r5260: - show an error message on nmblookup failure
[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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26 #include "dynconfig.h"
27 #include "libcli/nbt/libnbt.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "system/iconv.h"
30
31 /* command line options */
32 static struct {
33         const char *broadcast_address;
34         const char *unicast_address;
35         BOOL find_master;
36         BOOL wins_lookup;
37         BOOL node_status;
38         BOOL root_port;
39         BOOL lookup_by_ip;
40 } options;
41
42 /*
43   clean any binary from a node name
44 */
45 static const char *clean_name(TALLOC_CTX *mem_ctx, const char *name)
46 {
47         char *ret = talloc_strdup(mem_ctx, name);
48         int i;
49         for (i=0;ret[i];i++) {
50                 if (!isprint(ret[i])) ret[i] = '.';
51         }
52         return ret;
53 }
54
55 /*
56   turn a node status flags field into a string
57 */
58 static char *node_status_flags(TALLOC_CTX *mem_ctx, uint16_t flags)
59 {
60         char *ret;
61         const char *group = "       ";
62         const char *type = "B";
63
64         if (flags & NBT_NM_GROUP) {
65                 group = "<GROUP>";
66         }
67
68         switch (flags & NBT_NM_OWNER_TYPE) {
69         case NBT_NODE_B: 
70                 type = "B";
71                 break;
72         case NBT_NODE_P: 
73                 type = "P";
74                 break;
75         case NBT_NODE_M: 
76                 type = "M";
77                 break;
78         case NBT_NODE_H: 
79                 type = "H";
80                 break;
81         }
82
83         ret = talloc_asprintf(mem_ctx, "%s %s", group, type);
84
85         if (flags & NBT_NM_DEREGISTER) {
86                 ret = talloc_asprintf_append(ret, " <DEREGISTERING>");
87         }
88         if (flags & NBT_NM_CONFLICT) {
89                 ret = talloc_asprintf_append(ret, " <CONFLICT>");
90         }
91         if (flags & NBT_NM_ACTIVE) {
92                 ret = talloc_asprintf_append(ret, " <ACTIVE>");
93         }
94         if (flags & NBT_NM_PERMANENT) {
95                 ret = talloc_asprintf_append(ret, " <PERMANENT>");
96         }
97         
98         return ret;
99 }
100
101 /* do a single node status */
102 static void do_node_status(struct nbt_name_socket *nbtsock,
103                            const char *addr)
104 {
105         struct nbt_name_status io;
106         NTSTATUS status;
107
108         io.in.name.name = "*";
109         io.in.name.type = 0;
110         io.in.name.scope = NULL;
111         io.in.dest_addr = addr;
112         io.in.timeout = 1;
113         io.in.retries = 2;
114
115         status = nbt_name_status(nbtsock, nbtsock, &io);
116         if (NT_STATUS_IS_OK(status)) {
117                 int i;
118                 printf("Node status reply from %s\n",
119                        io.out.reply_from);
120                 for (i=0;i<io.out.status.num_names;i++) {
121                         d_printf("\t%-16s <%02x>  %s\n", 
122                                  clean_name(nbtsock, io.out.status.names[i].name),
123                                  io.out.status.names[i].type,
124                                  node_status_flags(nbtsock, io.out.status.names[i].nb_flags));
125                 }
126                 printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
127                        io.out.status.statistics.unit_id[0],
128                        io.out.status.statistics.unit_id[1],
129                        io.out.status.statistics.unit_id[2],
130                        io.out.status.statistics.unit_id[3],
131                        io.out.status.statistics.unit_id[4],
132                        io.out.status.statistics.unit_id[5]);
133         }
134 }
135
136 /* do a single node query */
137 static NTSTATUS do_node_query(struct nbt_name_socket *nbtsock,
138                               const char *addr, 
139                               const char *node_name, 
140                               enum nbt_name_type node_type,
141                               BOOL broadcast)
142 {
143         struct nbt_name_query io;
144         NTSTATUS status;
145         int i;
146
147         io.in.name.name = node_name;
148         io.in.name.type = node_type;
149         io.in.name.scope = NULL;
150         io.in.dest_addr = addr;
151         io.in.broadcast = broadcast;
152         io.in.wins_lookup = options.wins_lookup;
153         io.in.timeout = 1;
154         io.in.retries = 2;
155
156         status = nbt_name_query(nbtsock, nbtsock, &io);
157         NT_STATUS_NOT_OK_RETURN(status);
158                 
159         for (i=0;i<io.out.num_addrs;i++) {
160                 printf("%s %s<%02x>\n",
161                        io.out.reply_addrs[i],
162                        io.out.name.name,
163                        io.out.name.type);
164         }
165         if (options.node_status && io.out.num_addrs > 0) {
166                 do_node_status(nbtsock, io.out.reply_addrs[0]);
167         }
168
169         return status;
170 }
171
172
173 static void process_one(const char *name)
174 {
175         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
176         enum nbt_name_type node_type = NBT_NAME_CLIENT;
177         char *node_name, *p;
178         struct nbt_name_socket *nbtsock;
179         NTSTATUS status;
180         
181         if (options.find_master) {
182                 node_type = NBT_NAME_MASTER;
183                 if (*name == '-') {
184                         name = "\01\02__MSBROWSE__\02";
185                         node_type = NBT_NAME_MS;
186                 }
187         }
188
189         p = strchr(name, '#');
190         if (p) {
191                 node_name = talloc_strndup(tmp_ctx, name, PTR_DIFF(p,name));
192                 node_type = (enum nbt_name_type)strtol(p+1, NULL, 16);
193         } else {
194                 node_name = talloc_strdup(tmp_ctx, name);
195         }
196
197         nbtsock = nbt_name_socket_init(tmp_ctx, NULL);
198
199         if (options.root_port) {
200                 status = socket_listen(nbtsock->sock, "0.0.0.0", NBT_NAME_SERVICE_PORT, 0, 0);
201                 if (!NT_STATUS_IS_OK(status)) {
202                         printf("Failed to bind to local port 137 - %s\n", nt_errstr(status));
203                         return;
204                 }
205         }
206
207         if (options.lookup_by_ip) {
208                 do_node_status(nbtsock, name);
209                 talloc_free(tmp_ctx);
210                 return;
211         }
212
213         if (options.broadcast_address) {
214                 status = do_node_query(nbtsock, options.broadcast_address, node_name, node_type, True);
215         } else if (options.unicast_address) {
216                 status = do_node_query(nbtsock, options.unicast_address, node_name, node_type, False);
217         } else {
218                 int i, num_interfaces = iface_count();
219                 for (i=0;i<num_interfaces;i++) {
220                         const char *bcast = sys_inet_ntoa(*iface_n_bcast(i));
221                         status = do_node_query(nbtsock, bcast, node_name, node_type, True);
222                         if (NT_STATUS_IS_OK(status)) break;
223                 }
224         }
225
226         if (!NT_STATUS_IS_OK(status)) {
227                 printf("Lookup failed - %s\n", nt_errstr(status));
228         }
229
230         talloc_free(tmp_ctx);
231  }
232
233 /*
234   main program
235 */
236 int main(int argc,char *argv[])
237 {
238         poptContext pc;
239         struct poptOption long_options[] = {
240                 POPT_AUTOHELP
241                 { "broadcast", 'B', POPT_ARG_STRING, &options.broadcast_address, 
242                   'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
243
244                 { "unicast", 'U', POPT_ARG_STRING, &options.unicast_address, 
245                   'U', "Specify address to use for unicast" },
246
247                 { "master-browser", 'M', POPT_ARG_VAL, &options.find_master, 
248                   True, "Search for a master browser" },
249
250                 { "wins", 'W', POPT_ARG_VAL, &options.wins_lookup, True, "Do a WINS lookup" },
251
252                 { "status", 'S', POPT_ARG_VAL, &options.node_status, 
253                   True, "Lookup node status as well" },
254
255                 { "root-port", 'r', POPT_ARG_VAL, &options.root_port, 
256                   True, "Use root port 137 (Win95 only replies to this)" },
257
258                 { "lookup-by-ip", 'A', POPT_ARG_VAL, &options.lookup_by_ip, 
259                   True, "Do a node status on <name> as an IP Address" },
260
261                 POPT_COMMON_SAMBA
262                 { 0, 0, 0, 0 }
263         };
264         
265         setup_logging(argv[0], True);
266
267         pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
268                             POPT_CONTEXT_KEEP_FIRST);
269         
270         poptSetOtherOptionHelp(pc, "<NODE> ...");
271
272         while ((poptGetNextOpt(pc) != -1)) /* noop */ ;
273
274         /* swallow argv[0] */
275         poptGetArg(pc);
276
277         if(!poptPeekArg(pc)) { 
278                 poptPrintUsage(pc, stderr, 0);
279                 exit(1);
280         }
281         
282         lp_load(dyn_CONFIGFILE,True,False,False);
283         load_interfaces();
284         
285         while (poptPeekArg(pc)) {
286                 const char *name = poptGetArg(pc);
287
288                 process_one(name);
289         }
290
291         poptFreeContext(pc);
292
293         return 0;
294 }