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