d7e0cb23ced1957d740e93cca5d486d0d486cbcf
[sfrench/samba-autobuild/.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
25 extern bool AllowDebugChange;
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_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 void do_node_status(int fd,
112                 const char *name,
113                 int type,
114                 struct sockaddr_storage *pss)
115 {
116         struct nmb_name nname;
117         int count, i, j;
118         NODE_STATUS_STRUCT *status;
119         struct node_status_extra extra;
120         fstring cleanname;
121         char addr[INET6_ADDRSTRLEN];
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(fd, &nname, pss, &count, &extra);
127         if (status) {
128                 for (i=0;i<count;i++) {
129                         pull_ascii_fstring(cleanname, status[i].name);
130                         for (j=0;cleanname[j];j++) {
131                                 if (!isprint((int)cleanname[j])) {
132                                         cleanname[j] = '.';
133                                 }
134                         }
135                         d_printf("\t%-15s <%02x> - %s\n",
136                                cleanname,status[i].type,
137                                node_status_flags(status[i].flags));
138                 }
139                 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
140                                 extra.mac_addr[0], extra.mac_addr[1],
141                                 extra.mac_addr[2], extra.mac_addr[3],
142                                 extra.mac_addr[4], extra.mac_addr[5]);
143                 d_printf("\n");
144                 SAFE_FREE(status);
145         } else {
146                 d_printf("No reply from %s\n\n",addr);
147         }
148 }
149
150
151 /****************************************************************************
152  Send out one query.
153 ****************************************************************************/
154
155 static bool query_one(const char *lookup, unsigned int lookup_type)
156 {
157         int j, count, flags = 0;
158         struct sockaddr_storage *ip_list=NULL;
159
160         if (got_bcast) {
161                 char addr[INET6_ADDRSTRLEN];
162                 print_sockaddr(addr, sizeof(addr), &bcast_addr);
163                 d_printf("querying %s on %s\n", lookup, addr);
164                 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
165                                      use_bcast?true:recursion_desired,
166                                      &bcast_addr, &count, &flags, NULL);
167         } else {
168                 const struct in_addr *bcast;
169                 for (j=iface_count() - 1;
170                      !ip_list && j >= 0;
171                      j--) {
172                         char addr[INET6_ADDRSTRLEN];
173                         struct sockaddr_storage bcast_ss;
174
175                         bcast = iface_n_bcast_v4(j);
176                         if (!bcast) {
177                                 continue;
178                         }
179                         in_addr_to_sockaddr_storage(&bcast_ss, *bcast);
180                         print_sockaddr(addr, sizeof(addr), &bcast_ss);
181                         d_printf("querying %s on %s\n",
182                                lookup, addr);
183                         ip_list = name_query(ServerFD,lookup,lookup_type,
184                                              use_bcast,
185                                              use_bcast?True:recursion_desired,
186                                              &bcast_ss,&count, &flags, NULL);
187                 }
188         }
189
190         if (!ip_list) {
191                 return false;
192         }
193
194         if (give_flags) {
195                 d_printf("Flags: %s\n", query_flags(flags));
196         }
197
198         for (j=0;j<count;j++) {
199                 char addr[INET6_ADDRSTRLEN];
200                 if (translate_addresses) {
201                         char h_name[MAX_DNS_NAME_LENGTH];
202                         h_name[0] = '\0';
203                         if (sys_getnameinfo((const struct sockaddr *)&ip_list[j],
204                                         sizeof(struct sockaddr_storage),
205                                         h_name, sizeof(h_name),
206                                         NULL, 0,
207                                         NI_NAMEREQD)) {
208                                 continue;
209                         }
210                         d_printf("%s, ", h_name);
211                 }
212                 print_sockaddr(addr, sizeof(addr), &ip_list[j]);
213                 d_printf("%s %s<%02x>\n", addr,lookup, lookup_type);
214                 /* We can only do find_status if the ip address returned
215                    was valid - ie. name_query returned true.
216                  */
217                 if (find_status) {
218                         do_node_status(ServerFD, lookup,
219                                         lookup_type, &ip_list[j]);
220                 }
221         }
222
223         free(ip_list);
224
225         return (ip_list != NULL);
226 }
227
228
229 /****************************************************************************
230   main program
231 ****************************************************************************/
232 int main(int argc,char *argv[])
233 {
234         int opt;
235         unsigned int lookup_type = 0x0;
236         fstring lookup;
237         static bool find_master=False;
238         static bool lookup_by_ip = False;
239         poptContext pc;
240         TALLOC_CTX *frame = talloc_stackframe();
241
242         struct poptOption long_options[] = {
243                 POPT_AUTOHELP
244                 { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
245                 { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" },
246                 { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
247                 { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" },
248                 { "recursion", 'R', POPT_ARG_NONE, NULL, 'R', "Set recursion desired in package" },
249                 { "status", 'S', POPT_ARG_NONE, NULL, 'S', "Lookup node status as well" },
250                 { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
251                 { "root-port", 'r', POPT_ARG_NONE, NULL, 'r', "Use root port 137 (Win95 only replies to this)" },
252                 { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, 'A', "Do a node status on <name> as an IP Address" },
253                 POPT_COMMON_SAMBA
254                 POPT_COMMON_CONNECTION
255                 { 0, 0, 0, 0 }
256         };
257
258         *lookup = 0;
259
260         load_case_tables();
261
262         setup_logging(argv[0], DEBUG_STDOUT);
263
264         pc = poptGetContext("nmblookup", argc, (const char **)argv,
265                         long_options, POPT_CONTEXT_KEEP_FIRST);
266
267         poptSetOtherOptionHelp(pc, "<NODE> ...");
268
269         while ((opt = poptGetNextOpt(pc)) != -1) {
270                 switch (opt) {
271                 case 'f':
272                         give_flags = true;
273                         break;
274                 case 'M':
275                         find_master = true;
276                         break;
277                 case 'R':
278                         recursion_desired = true;
279                         break;
280                 case 'S':
281                         find_status = true;
282                         break;
283                 case 'r':
284                         RootPort = true;
285                         break;
286                 case 'A':
287                         lookup_by_ip = true;
288                         break;
289                 case 'B':
290                         if (interpret_string_addr(&bcast_addr,
291                                         poptGetOptArg(pc),
292                                         NI_NUMERICHOST)) {
293                                 got_bcast = True;
294                                 use_bcast = True;
295                         }
296                         break;
297                 case 'U':
298                         if (interpret_string_addr(&bcast_addr,
299                                         poptGetOptArg(pc),
300                                         0)) {
301                                 got_bcast = True;
302                                 use_bcast = False;
303                         }
304                         break;
305                 case 'T':
306                         translate_addresses = !translate_addresses;
307                         break;
308                 }
309         }
310
311         poptGetArg(pc); /* Remove argv[0] */
312
313         if(!poptPeekArg(pc)) {
314                 poptPrintUsage(pc, stderr, 0);
315                 exit(1);
316         }
317
318         if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True)) {
319                 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
320                                 get_dyn_CONFIGFILE());
321         }
322
323         load_interfaces();
324         if (!open_sockets()) {
325                 return(1);
326         }
327
328         while(poptPeekArg(pc)) {
329                 char *p;
330                 struct in_addr ip;
331
332                 fstrcpy(lookup,poptGetArg(pc));
333
334                 if(lookup_by_ip) {
335                         struct sockaddr_storage ss;
336                         ip = interpret_addr2(lookup);
337                         in_addr_to_sockaddr_storage(&ss, ip);
338                         fstrcpy(lookup,"*");
339                         do_node_status(ServerFD, lookup, lookup_type, &ss);
340                         continue;
341                 }
342
343                 if (find_master) {
344                         if (*lookup == '-') {
345                                 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
346                                 lookup_type = 1;
347                         } else {
348                                 lookup_type = 0x1d;
349                         }
350                 }
351
352                 p = strchr_m(lookup,'#');
353                 if (p) {
354                         *p = '\0';
355                         sscanf(++p,"%x",&lookup_type);
356                 }
357
358                 if (!query_one(lookup, lookup_type)) {
359                         d_printf( "name_query failed to find name %s", lookup );
360                         if( 0 != lookup_type ) {
361                                 d_printf( "#%02x", lookup_type );
362                         }
363                         d_printf( "\n" );
364                 }
365         }
366
367         poptFreeContext(pc);
368         TALLOC_FREE(frame);
369         return(0);
370 }