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