2f50c5c8f5a8ac28633ba19ab505f6b12ca60075
[samba.git] / source3 / utils / nmblookup.c
1 /*
2    Unix SMB/CIFS implementation.
3    NBT client - used to lookup netbios names
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "libsmb/nmblib.h"
25 #include "libsmb/namequery.h"
26
27 static bool give_flags = false;
28 static bool use_bcast = true;
29 static bool got_bcast = false;
30 static struct sockaddr_storage bcast_addr;
31 static bool recursion_desired = false;
32 static bool translate_addresses = false;
33 static int ServerFD= -1;
34 static bool RootPort = false;
35 static bool find_status = false;
36
37 /****************************************************************************
38  Open the socket communication.
39 **************************************************************************/
40
41 static bool open_sockets(void)
42 {
43         struct sockaddr_storage ss;
44         const char *sock_addr = lp_nbt_client_socket_address();
45
46         if (!interpret_string_addr(&ss, sock_addr,
47                                 AI_NUMERICHOST|AI_PASSIVE)) {
48                 DEBUG(0,("open_sockets: unable to get socket address "
49                                         "from string %s", sock_addr));
50                 return false;
51         }
52         ServerFD = open_socket_in( SOCK_DGRAM,
53                                 (RootPort ? 137 : 0),
54                                 (RootPort ?   0 : 3),
55                                 &ss, true );
56
57         if (ServerFD == -1) {
58                 return false;
59         }
60
61         set_socket_options( ServerFD, "SO_BROADCAST" );
62
63         DEBUG(3, ("Socket opened.\n"));
64         return true;
65 }
66
67 /****************************************************************************
68 turn a node status flags field into a string
69 ****************************************************************************/
70 static char *node_status_flags(unsigned char flags)
71 {
72         static fstring ret;
73         fstrcpy(ret,"");
74
75         fstrcat(ret, (flags & 0x80) ? "<GROUP> " : "        ");
76         if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
77         if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
78         if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
79         if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
80         if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
81         if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
82         if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
83         if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
84
85         return ret;
86 }
87
88 /****************************************************************************
89  Turn the NMB Query flags into a string.
90 ****************************************************************************/
91
92 static char *query_flags(int flags)
93 {
94         static fstring ret1;
95         fstrcpy(ret1, "");
96
97         if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
98         if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
99         if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
100         if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
101         if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
102         if (flags & NM_FLAGS_B)  fstrcat(ret1, "Broadcast ");
103
104         return ret1;
105 }
106
107 /****************************************************************************
108  Do a node status query.
109 ****************************************************************************/
110
111 static bool do_node_status(const char *name,
112                 int type,
113                 struct sockaddr_storage *pss)
114 {
115         struct nmb_name nname;
116         int count, i, j;
117         struct node_status *addrs;
118         struct node_status_extra extra;
119         fstring cleanname;
120         char addr[INET6_ADDRSTRLEN];
121         NTSTATUS status;
122
123         print_sockaddr(addr, sizeof(addr), pss);
124         d_printf("Looking up status of %s\n",addr);
125         make_nmb_name(&nname, name, type);
126         status = node_status_query(talloc_tos(), &nname, pss,
127                                    &addrs, &count, &extra);
128         if (NT_STATUS_IS_OK(status)) {
129                 for (i=0;i<count;i++) {
130                         pull_ascii_fstring(cleanname, addrs[i].name);
131                         for (j=0;cleanname[j];j++) {
132                                 if (!isprint((int)cleanname[j])) {
133                                         cleanname[j] = '.';
134                                 }
135                         }
136                         d_printf("\t%-15s <%02x> - %s\n",
137                                cleanname,addrs[i].type,
138                                node_status_flags(addrs[i].flags));
139                 }
140                 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
141                                 extra.mac_addr[0], extra.mac_addr[1],
142                                 extra.mac_addr[2], extra.mac_addr[3],
143                                 extra.mac_addr[4], extra.mac_addr[5]);
144                 d_printf("\n");
145                 TALLOC_FREE(addrs);
146                 return true;
147         } else {
148                 d_printf("No reply from %s\n\n",addr);
149                 return false;
150         }
151 }
152
153
154 /****************************************************************************
155  Send out one query.
156 ****************************************************************************/
157
158 static bool query_one(const char *lookup, unsigned int lookup_type)
159 {
160         int j, count;
161         uint8_t flags;
162         struct sockaddr_storage *ip_list=NULL;
163         NTSTATUS status = NT_STATUS_NOT_FOUND;
164
165         if (got_bcast) {
166                 char addr[INET6_ADDRSTRLEN];
167                 print_sockaddr(addr, sizeof(addr), &bcast_addr);
168                 d_printf("querying %s on %s\n", lookup, addr);
169                 status = name_query(lookup,lookup_type,use_bcast,
170                                     use_bcast?true:recursion_desired,
171                                     &bcast_addr, talloc_tos(),
172                                     &ip_list, &count, &flags);
173         } else {
174                 status = name_resolve_bcast(talloc_tos(),
175                                             lookup,
176                                             lookup_type,
177                                             &ip_list,
178                                             &count);
179         }
180
181         if (!NT_STATUS_IS_OK(status)) {
182                 return false;
183         }
184
185         if (give_flags) {
186                 d_printf("Flags: %s\n", query_flags(flags));
187         }
188
189         for (j=0;j<count;j++) {
190                 char addr[INET6_ADDRSTRLEN];
191                 if (translate_addresses) {
192                         char h_name[MAX_DNS_NAME_LENGTH];
193                         h_name[0] = '\0';
194                         if (sys_getnameinfo((const struct sockaddr *)&ip_list[j],
195                                         sizeof(struct sockaddr_storage),
196                                         h_name, sizeof(h_name),
197                                         NULL, 0,
198                                         NI_NAMEREQD)) {
199                                 continue;
200                         }
201                         d_printf("%s, ", h_name);
202                 }
203                 print_sockaddr(addr, sizeof(addr), &ip_list[j]);
204                 d_printf("%s %s<%02x>\n", addr,lookup, lookup_type);
205                 /* We can only do find_status if the ip address returned
206                    was valid - ie. name_query returned true.
207                  */
208                 if (find_status) {
209                         if (!do_node_status(lookup, lookup_type, &ip_list[j])) {
210                                 status = NT_STATUS_UNSUCCESSFUL;
211                         }
212                 }
213         }
214
215         TALLOC_FREE(ip_list);
216
217         return NT_STATUS_IS_OK(status);
218 }
219
220
221 /****************************************************************************
222   main program
223 ****************************************************************************/
224 int main(int argc, const char *argv[])
225 {
226         int opt;
227         unsigned int lookup_type = 0x0;
228         fstring lookup;
229         static bool find_master=False;
230         static bool lookup_by_ip = False;
231         poptContext pc = NULL;
232         TALLOC_CTX *frame = talloc_stackframe();
233         int rc = 0;
234
235         struct poptOption long_options[] = {
236                 POPT_AUTOHELP
237                 {
238                         .longName   = "broadcast",
239                         .shortName  = 'B',
240                         .argInfo    = POPT_ARG_STRING,
241                         .arg        = NULL,
242                         .val        = 'B',
243                         .descrip    = "Specify address to use for broadcasts",
244                         .argDescrip = "BROADCAST-ADDRESS",
245                 },
246                 {
247                         .longName   = "flags",
248                         .shortName  = 'f',
249                         .argInfo    = POPT_ARG_NONE,
250                         .arg        = NULL,
251                         .val        = 'f',
252                         .descrip    = "List the NMB flags returned",
253                 },
254                 {
255                         .longName   = "unicast",
256                         .shortName  = 'U',
257                         .argInfo    = POPT_ARG_STRING,
258                         .arg        = NULL,
259                         .val        = 'U',
260                         .descrip    = "Specify address to use for unicast",
261                 },
262                 {
263                         .longName   = "master-browser",
264                         .shortName  = 'M',
265                         .argInfo    = POPT_ARG_NONE,
266                         .arg        = NULL,
267                         .val        = 'M',
268                         .descrip    = "Search for a master browser",
269                 },
270                 {
271                         .longName   = "recursion",
272                         .shortName  = 'R',
273                         .argInfo    = POPT_ARG_NONE,
274                         .arg        = NULL,
275                         .val        = 'R',
276                         .descrip    = "Set recursion desired in package",
277                 },
278                 {
279                         .longName   = "status",
280                         .shortName  = 'S',
281                         .argInfo    = POPT_ARG_NONE,
282                         .arg        = NULL,
283                         .val        = 'S',
284                         .descrip    = "Lookup node status as well",
285                 },
286                 {
287                         .longName   = "translate",
288                         .shortName  = 'T',
289                         .argInfo    = POPT_ARG_NONE,
290                         .arg        = NULL,
291                         .val        = 'T',
292                         .descrip    = "Translate IP addresses into names",
293                 },
294                 {
295                         .longName   = "root-port",
296                         .shortName  = 'r',
297                         .argInfo    = POPT_ARG_NONE,
298                         .arg        = NULL,
299                         .val        = 'r',
300                         .descrip    = "Use root port 137 (Win95 only replies to this)",
301                 },
302                 {
303                         .longName   = "lookup-by-ip",
304                         .shortName  = 'A',
305                         .argInfo    = POPT_ARG_NONE,
306                         .arg        = NULL,
307                         .val        = 'A',
308                         .descrip    = "Do a node status on <name> as an IP Address",
309                 },
310                 POPT_COMMON_SAMBA
311                 POPT_COMMON_CONNECTION
312                 POPT_TABLEEND
313         };
314
315         *lookup = 0;
316
317         smb_init_locale();
318
319         setup_logging(argv[0], DEBUG_STDOUT);
320
321         pc = poptGetContext("nmblookup", argc, argv,
322                         long_options, POPT_CONTEXT_KEEP_FIRST);
323
324         poptSetOtherOptionHelp(pc, "<NODE> ...");
325
326         while ((opt = poptGetNextOpt(pc)) != -1) {
327                 switch (opt) {
328                 case 'f':
329                         give_flags = true;
330                         break;
331                 case 'M':
332                         find_master = true;
333                         break;
334                 case 'R':
335                         recursion_desired = true;
336                         break;
337                 case 'S':
338                         find_status = true;
339                         break;
340                 case 'r':
341                         RootPort = true;
342                         break;
343                 case 'A':
344                         lookup_by_ip = true;
345                         break;
346                 case 'B':
347                         if (interpret_string_addr(&bcast_addr,
348                                         poptGetOptArg(pc),
349                                         NI_NUMERICHOST)) {
350                                 got_bcast = True;
351                                 use_bcast = True;
352                         }
353                         break;
354                 case 'U':
355                         if (interpret_string_addr(&bcast_addr,
356                                         poptGetOptArg(pc),
357                                         0)) {
358                                 got_bcast = True;
359                                 use_bcast = False;
360                         }
361                         break;
362                 case 'T':
363                         translate_addresses = !translate_addresses;
364                         break;
365                 }
366         }
367
368         poptGetArg(pc); /* Remove argv[0] */
369
370         if(!poptPeekArg(pc)) {
371                 poptPrintUsage(pc, stderr, 0);
372                 rc = 1;
373                 goto out;
374         }
375
376         if (!lp_load_global(get_dyn_CONFIGFILE())) {
377                 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
378                                 get_dyn_CONFIGFILE());
379         }
380
381         load_interfaces();
382         if (!open_sockets()) {
383                 rc = 1;
384                 goto out;
385         }
386
387         while(poptPeekArg(pc)) {
388                 char *p;
389                 struct in_addr ip;
390                 size_t nbt_len;
391
392                 fstrcpy(lookup,poptGetArg(pc));
393
394                 if(lookup_by_ip) {
395                         struct sockaddr_storage ss;
396                         ip = interpret_addr2(lookup);
397                         in_addr_to_sockaddr_storage(&ss, ip);
398                         fstrcpy(lookup,"*");
399                         if (!do_node_status(lookup, lookup_type, &ss)) {
400                                 rc = 1;
401                         }
402                         continue;
403                 }
404
405                 if (find_master) {
406                         if (*lookup == '-') {
407                                 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
408                                 lookup_type = 1;
409                         } else {
410                                 lookup_type = 0x1d;
411                         }
412                 }
413
414                 p = strchr_m(lookup,'#');
415                 if (p) {
416                         *p = '\0';
417                         sscanf(++p,"%x",&lookup_type);
418                 }
419
420                 nbt_len = strlen(lookup);
421                 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
422                         d_printf("The specified netbios name [%s] is too long!\n",
423                                  lookup);
424                         continue;
425                 }
426
427
428                 if (!query_one(lookup, lookup_type)) {
429                         rc = 1;
430                         d_printf( "name_query failed to find name %s", lookup );
431                         if( 0 != lookup_type ) {
432                                 d_printf( "#%02x", lookup_type );
433                         }
434                         d_printf( "\n" );
435                 }
436         }
437
438 out:
439         poptFreeContext(pc);
440         TALLOC_FREE(frame);
441         return rc;
442 }