printing: explicitly clear PUBLISHED attribute
[Samba.git] / source3 / utils / nmblookup.c
blobf8545401e6a468a335526912cf00e940b6da1b61
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)
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.
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.
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/>.
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "libsmb/nmblib.h"
26 static bool give_flags = false;
27 static bool use_bcast = true;
28 static bool got_bcast = false;
29 static struct sockaddr_storage bcast_addr;
30 static bool recursion_desired = false;
31 static bool translate_addresses = false;
32 static int ServerFD= -1;
33 static bool RootPort = false;
34 static bool find_status = false;
36 /****************************************************************************
37 Open the socket communication.
38 **************************************************************************/
40 static bool open_sockets(void)
42 struct sockaddr_storage ss;
43 const char *sock_addr = lp_socket_address();
45 if (!interpret_string_addr(&ss, sock_addr,
46 AI_NUMERICHOST|AI_PASSIVE)) {
47 DEBUG(0,("open_sockets: unable to get socket address "
48 "from string %s", sock_addr));
49 return false;
51 ServerFD = open_socket_in( SOCK_DGRAM,
52 (RootPort ? 137 : 0),
53 (RootPort ? 0 : 3),
54 &ss, true );
56 if (ServerFD == -1) {
57 return false;
60 set_socket_options( ServerFD, "SO_BROADCAST" );
62 DEBUG(3, ("Socket opened.\n"));
63 return true;
66 /****************************************************************************
67 turn a node status flags field into a string
68 ****************************************************************************/
69 static char *node_status_flags(unsigned char flags)
71 static fstring ret;
72 fstrcpy(ret,"");
74 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
75 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
76 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
77 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
78 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
79 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
80 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
81 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
82 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
84 return ret;
87 /****************************************************************************
88 Turn the NMB Query flags into a string.
89 ****************************************************************************/
91 static char *query_flags(int flags)
93 static fstring ret1;
94 fstrcpy(ret1, "");
96 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
97 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
98 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
99 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
100 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
101 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
103 return ret1;
106 /****************************************************************************
107 Do a node status query.
108 ****************************************************************************/
110 static void do_node_status(const char *name,
111 int type,
112 struct sockaddr_storage *pss)
114 struct nmb_name nname;
115 int count, i, j;
116 struct node_status *addrs;
117 struct node_status_extra extra;
118 fstring cleanname;
119 char addr[INET6_ADDRSTRLEN];
120 NTSTATUS status;
122 print_sockaddr(addr, sizeof(addr), pss);
123 d_printf("Looking up status of %s\n",addr);
124 make_nmb_name(&nname, name, type);
125 status = node_status_query(talloc_tos(), &nname, pss,
126 &addrs, &count, &extra);
127 if (NT_STATUS_IS_OK(status)) {
128 for (i=0;i<count;i++) {
129 pull_ascii_fstring(cleanname, addrs[i].name);
130 for (j=0;cleanname[j];j++) {
131 if (!isprint((int)cleanname[j])) {
132 cleanname[j] = '.';
135 d_printf("\t%-15s <%02x> - %s\n",
136 cleanname,addrs[i].type,
137 node_status_flags(addrs[i].flags));
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 TALLOC_FREE(addrs);
145 } else {
146 d_printf("No reply from %s\n\n",addr);
151 /****************************************************************************
152 Send out one query.
153 ****************************************************************************/
155 static bool query_one(const char *lookup, unsigned int lookup_type)
157 int j, count;
158 uint8_t flags;
159 struct sockaddr_storage *ip_list=NULL;
160 NTSTATUS status = NT_STATUS_NOT_FOUND;
162 if (got_bcast) {
163 char addr[INET6_ADDRSTRLEN];
164 print_sockaddr(addr, sizeof(addr), &bcast_addr);
165 d_printf("querying %s on %s\n", lookup, addr);
166 status = name_query(lookup,lookup_type,use_bcast,
167 use_bcast?true:recursion_desired,
168 &bcast_addr, talloc_tos(),
169 &ip_list, &count, &flags);
170 } else {
171 const struct in_addr *bcast;
172 for (j=iface_count() - 1;
173 !ip_list && j >= 0;
174 j--) {
175 char addr[INET6_ADDRSTRLEN];
176 struct sockaddr_storage bcast_ss;
178 bcast = iface_n_bcast_v4(j);
179 if (!bcast) {
180 continue;
182 in_addr_to_sockaddr_storage(&bcast_ss, *bcast);
183 print_sockaddr(addr, sizeof(addr), &bcast_ss);
184 d_printf("querying %s on %s\n",
185 lookup, addr);
186 status = name_query(lookup,lookup_type,
187 use_bcast,
188 use_bcast?True:recursion_desired,
189 &bcast_ss, talloc_tos(),
190 &ip_list, &count, &flags);
194 if (!NT_STATUS_IS_OK(status)) {
195 return false;
198 if (give_flags) {
199 d_printf("Flags: %s\n", query_flags(flags));
202 for (j=0;j<count;j++) {
203 char addr[INET6_ADDRSTRLEN];
204 if (translate_addresses) {
205 char h_name[MAX_DNS_NAME_LENGTH];
206 h_name[0] = '\0';
207 if (sys_getnameinfo((const struct sockaddr *)&ip_list[j],
208 sizeof(struct sockaddr_storage),
209 h_name, sizeof(h_name),
210 NULL, 0,
211 NI_NAMEREQD)) {
212 continue;
214 d_printf("%s, ", h_name);
216 print_sockaddr(addr, sizeof(addr), &ip_list[j]);
217 d_printf("%s %s<%02x>\n", addr,lookup, lookup_type);
218 /* We can only do find_status if the ip address returned
219 was valid - ie. name_query returned true.
221 if (find_status) {
222 do_node_status(lookup, lookup_type, &ip_list[j]);
226 TALLOC_FREE(ip_list);
228 return NT_STATUS_IS_OK(status);
232 /****************************************************************************
233 main program
234 ****************************************************************************/
235 int main(int argc,char *argv[])
237 int opt;
238 unsigned int lookup_type = 0x0;
239 fstring lookup;
240 static bool find_master=False;
241 static bool lookup_by_ip = False;
242 poptContext pc;
243 TALLOC_CTX *frame = talloc_stackframe();
245 struct poptOption long_options[] = {
246 POPT_AUTOHELP
247 { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
248 { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" },
249 { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
250 { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" },
251 { "recursion", 'R', POPT_ARG_NONE, NULL, 'R', "Set recursion desired in package" },
252 { "status", 'S', POPT_ARG_NONE, NULL, 'S', "Lookup node status as well" },
253 { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
254 { "root-port", 'r', POPT_ARG_NONE, NULL, 'r', "Use root port 137 (Win95 only replies to this)" },
255 { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, 'A', "Do a node status on <name> as an IP Address" },
256 POPT_COMMON_SAMBA
257 POPT_COMMON_CONNECTION
258 { 0, 0, 0, 0 }
261 *lookup = 0;
263 load_case_tables();
265 setup_logging(argv[0], DEBUG_STDOUT);
267 pc = poptGetContext("nmblookup", argc, (const char **)argv,
268 long_options, POPT_CONTEXT_KEEP_FIRST);
270 poptSetOtherOptionHelp(pc, "<NODE> ...");
272 while ((opt = poptGetNextOpt(pc)) != -1) {
273 switch (opt) {
274 case 'f':
275 give_flags = true;
276 break;
277 case 'M':
278 find_master = true;
279 break;
280 case 'R':
281 recursion_desired = true;
282 break;
283 case 'S':
284 find_status = true;
285 break;
286 case 'r':
287 RootPort = true;
288 break;
289 case 'A':
290 lookup_by_ip = true;
291 break;
292 case 'B':
293 if (interpret_string_addr(&bcast_addr,
294 poptGetOptArg(pc),
295 NI_NUMERICHOST)) {
296 got_bcast = True;
297 use_bcast = True;
299 break;
300 case 'U':
301 if (interpret_string_addr(&bcast_addr,
302 poptGetOptArg(pc),
303 0)) {
304 got_bcast = True;
305 use_bcast = False;
307 break;
308 case 'T':
309 translate_addresses = !translate_addresses;
310 break;
314 poptGetArg(pc); /* Remove argv[0] */
316 if(!poptPeekArg(pc)) {
317 poptPrintUsage(pc, stderr, 0);
318 exit(1);
321 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True)) {
322 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
323 get_dyn_CONFIGFILE());
326 load_interfaces();
327 if (!open_sockets()) {
328 return(1);
331 while(poptPeekArg(pc)) {
332 char *p;
333 struct in_addr ip;
335 fstrcpy(lookup,poptGetArg(pc));
337 if(lookup_by_ip) {
338 struct sockaddr_storage ss;
339 ip = interpret_addr2(lookup);
340 in_addr_to_sockaddr_storage(&ss, ip);
341 fstrcpy(lookup,"*");
342 do_node_status(lookup, lookup_type, &ss);
343 continue;
346 if (find_master) {
347 if (*lookup == '-') {
348 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
349 lookup_type = 1;
350 } else {
351 lookup_type = 0x1d;
355 p = strchr_m(lookup,'#');
356 if (p) {
357 *p = '\0';
358 sscanf(++p,"%x",&lookup_type);
361 if (!query_one(lookup, lookup_type)) {
362 d_printf( "name_query failed to find name %s", lookup );
363 if( 0 != lookup_type ) {
364 d_printf( "#%02x", lookup_type );
366 d_printf( "\n" );
370 poptFreeContext(pc);
371 TALLOC_FREE(frame);
372 return(0);