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