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