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