f7ec40d1371cdd5083226d92fb03164109ad90bf
[kai/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
30 /* command line options */
31 static struct {
32         const char *broadcast_address;
33         const char *unicast_address;
34         BOOL find_master;
35         BOOL wins_lookup;
36         BOOL node_status;
37         BOOL root_port;
38         BOOL lookup_by_ip;
39         BOOL case_sensitive;
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((unsigned char)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 BOOL 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 = NBT_NAME_CLIENT;
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                 return True;
134         }
135
136         return False;
137 }
138
139 /* do a single node query */
140 static NTSTATUS do_node_query(struct nbt_name_socket *nbtsock,
141                               const char *addr, 
142                               const char *node_name, 
143                               enum nbt_name_type node_type,
144                               BOOL broadcast)
145 {
146         struct nbt_name_query io;
147         NTSTATUS status;
148         int i;
149
150         io.in.name.name = node_name;
151         io.in.name.type = node_type;
152         io.in.name.scope = NULL;
153         io.in.dest_addr = addr;
154         io.in.broadcast = broadcast;
155         io.in.wins_lookup = options.wins_lookup;
156         io.in.timeout = 1;
157         io.in.retries = 2;
158
159         status = nbt_name_query(nbtsock, nbtsock, &io);
160         NT_STATUS_NOT_OK_RETURN(status);
161
162         for (i=0;i<io.out.num_addrs;i++) {
163                 printf("%s %s<%02x>\n",
164                        io.out.reply_addrs[i],
165                        io.out.name.name,
166                        io.out.name.type);
167         }
168         if (options.node_status && io.out.num_addrs > 0) {
169                 do_node_status(nbtsock, io.out.reply_addrs[0]);
170         }
171
172         return status;
173 }
174
175
176 static BOOL process_one(const char *name)
177 {
178         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
179         enum nbt_name_type node_type = NBT_NAME_CLIENT;
180         char *node_name, *p;
181         struct socket_address *all_zero_addr;
182         struct nbt_name_socket *nbtsock;
183         NTSTATUS status = NT_STATUS_OK;
184         BOOL ret = True;
185
186         if (!options.case_sensitive) {
187                 name = strupper_talloc(tmp_ctx, name);
188         }
189         
190         if (options.find_master) {
191                 node_type = NBT_NAME_MASTER;
192                 if (*name == '-' || *name == '_') {
193                         name = "\01\02__MSBROWSE__\02";
194                         node_type = NBT_NAME_MS;
195                 }
196         }
197
198         p = strchr(name, '#');
199         if (p) {
200                 node_name = talloc_strndup(tmp_ctx, name, PTR_DIFF(p,name));
201                 node_type = (enum nbt_name_type)strtol(p+1, NULL, 16);
202         } else {
203                 node_name = talloc_strdup(tmp_ctx, name);
204         }
205
206         nbtsock = nbt_name_socket_init(tmp_ctx, NULL);
207         
208         if (options.root_port) {
209                 all_zero_addr = socket_address_from_strings(tmp_ctx, nbtsock->sock->backend_name, 
210                                                             "0.0.0.0", NBT_NAME_SERVICE_PORT);
211                 
212                 if (!all_zero_addr) {
213                         talloc_free(tmp_ctx);
214                         return False;
215                 }
216
217                 status = socket_listen(nbtsock->sock, all_zero_addr, 0, 0);
218                 if (!NT_STATUS_IS_OK(status)) {
219                         printf("Failed to bind to local port 137 - %s\n", nt_errstr(status));
220                         talloc_free(tmp_ctx);
221                         return False;
222                 }
223         }
224
225         if (options.lookup_by_ip) {
226                 ret = do_node_status(nbtsock, name);
227                 talloc_free(tmp_ctx);
228                 return ret;
229         }
230
231         if (options.broadcast_address) {
232                 status = do_node_query(nbtsock, options.broadcast_address, node_name, node_type, True);
233         } else if (options.unicast_address) {
234                 status = do_node_query(nbtsock, options.unicast_address, node_name, node_type, False);
235         } else {
236                 int i, num_interfaces = iface_count();
237                 for (i=0;i<num_interfaces;i++) {
238                         const char *bcast = iface_n_bcast(i);
239                         if (bcast == NULL) continue;
240                         status = do_node_query(nbtsock, bcast, node_name, node_type, True);
241                         if (NT_STATUS_IS_OK(status)) break;
242                 }
243         }
244
245         if (!NT_STATUS_IS_OK(status)) {
246                 printf("Lookup failed - %s\n", nt_errstr(status));
247                 ret = False;
248         }
249
250         talloc_free(tmp_ctx);
251         return ret;
252 }
253
254 /*
255   main program
256 */
257 int main(int argc,char *argv[])
258 {
259         BOOL ret = True;
260         poptContext pc;
261         struct poptOption long_options[] = {
262                 POPT_AUTOHELP
263                 { "broadcast", 'B', POPT_ARG_STRING, &options.broadcast_address, 
264                   'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
265
266                 { "unicast", 'U', POPT_ARG_STRING, &options.unicast_address, 
267                   'U', "Specify address to use for unicast" },
268
269                 { "master-browser", 'M', POPT_ARG_VAL, &options.find_master, 
270                   True, "Search for a master browser" },
271
272                 { "wins", 'W', POPT_ARG_VAL, &options.wins_lookup, True, "Do a WINS lookup" },
273
274                 { "status", 'S', POPT_ARG_VAL, &options.node_status, 
275                   True, "Lookup node status as well" },
276
277                 { "root-port", 'r', POPT_ARG_VAL, &options.root_port, 
278                   True, "Use root port 137 (Win95 only replies to this)" },
279
280                 { "lookup-by-ip", 'A', POPT_ARG_VAL, &options.lookup_by_ip, 
281                   True, "Do a node status on <name> as an IP Address" },
282
283                 { "case-sensitive", 0, POPT_ARG_VAL, &options.case_sensitive, 
284                   True, "Don't uppercase the name before sending" },
285
286                 POPT_COMMON_SAMBA
287                 { 0, 0, 0, 0 }
288         };
289         
290         pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
291                             POPT_CONTEXT_KEEP_FIRST);
292         
293         poptSetOtherOptionHelp(pc, "<NODE> ...");
294
295         while ((poptGetNextOpt(pc) != -1)) /* noop */ ;
296
297         /* swallow argv[0] */
298         poptGetArg(pc);
299
300         if(!poptPeekArg(pc)) { 
301                 poptPrintUsage(pc, stderr, 0);
302                 exit(1);
303         }
304         
305         while (poptPeekArg(pc)) {
306                 const char *name = poptGetArg(pc);
307
308                 ret &= process_one(name);
309         }
310
311         poptFreeContext(pc);
312
313         if (!ret) {
314                 return 1;
315         }
316
317         return 0;
318 }