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