lib: load_case_tables() -> smb_init_locale()
[kai/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 #include "libsmb/nmblib.h"
25
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;
35
36 /****************************************************************************
37  Open the socket communication.
38 **************************************************************************/
39
40 static bool open_sockets(void)
41 {
42         struct sockaddr_storage ss;
43         const char *sock_addr = lp_nbt_client_socket_address();
44
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;
50         }
51         ServerFD = open_socket_in( SOCK_DGRAM,
52                                 (RootPort ? 137 : 0),
53                                 (RootPort ?   0 : 3),
54                                 &ss, true );
55
56         if (ServerFD == -1) {
57                 return false;
58         }
59
60         set_socket_options( ServerFD, "SO_BROADCAST" );
61
62         DEBUG(3, ("Socket opened.\n"));
63         return true;
64 }
65
66 /****************************************************************************
67 turn a node status flags field into a string
68 ****************************************************************************/
69 static char *node_status_flags(unsigned char flags)
70 {
71         static fstring ret;
72         fstrcpy(ret,"");
73
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> ");
83
84         return ret;
85 }
86
87 /****************************************************************************
88  Turn the NMB Query flags into a string.
89 ****************************************************************************/
90
91 static char *query_flags(int flags)
92 {
93         static fstring ret1;
94         fstrcpy(ret1, "");
95
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 ");
102
103         return ret1;
104 }
105
106 /****************************************************************************
107  Do a node status query.
108 ****************************************************************************/
109
110 static bool do_node_status(const char *name,
111                 int type,
112                 struct sockaddr_storage *pss)
113 {
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;
121
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] = '.';
133                                 }
134                         }
135                         d_printf("\t%-15s <%02x> - %s\n",
136                                cleanname,addrs[i].type,
137                                node_status_flags(addrs[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                 TALLOC_FREE(addrs);
145                 return true;
146         } else {
147                 d_printf("No reply from %s\n\n",addr);
148                 return false;
149         }
150 }
151
152
153 /****************************************************************************
154  Send out one query.
155 ****************************************************************************/
156
157 static bool query_one(const char *lookup, unsigned int lookup_type)
158 {
159         int j, count;
160         uint8_t flags;
161         struct sockaddr_storage *ip_list=NULL;
162         NTSTATUS status = NT_STATUS_NOT_FOUND;
163
164         if (got_bcast) {
165                 char addr[INET6_ADDRSTRLEN];
166                 print_sockaddr(addr, sizeof(addr), &bcast_addr);
167                 d_printf("querying %s on %s\n", lookup, addr);
168                 status = name_query(lookup,lookup_type,use_bcast,
169                                     use_bcast?true:recursion_desired,
170                                     &bcast_addr, talloc_tos(),
171                                     &ip_list, &count, &flags);
172         } else {
173                 status = name_resolve_bcast(
174                         lookup, lookup_type,
175                         talloc_tos(), &ip_list, &count);
176         }
177
178         if (!NT_STATUS_IS_OK(status)) {
179                 return false;
180         }
181
182         if (give_flags) {
183                 d_printf("Flags: %s\n", query_flags(flags));
184         }
185
186         for (j=0;j<count;j++) {
187                 char addr[INET6_ADDRSTRLEN];
188                 if (translate_addresses) {
189                         char h_name[MAX_DNS_NAME_LENGTH];
190                         h_name[0] = '\0';
191                         if (sys_getnameinfo((const struct sockaddr *)&ip_list[j],
192                                         sizeof(struct sockaddr_storage),
193                                         h_name, sizeof(h_name),
194                                         NULL, 0,
195                                         NI_NAMEREQD)) {
196                                 continue;
197                         }
198                         d_printf("%s, ", h_name);
199                 }
200                 print_sockaddr(addr, sizeof(addr), &ip_list[j]);
201                 d_printf("%s %s<%02x>\n", addr,lookup, lookup_type);
202                 /* We can only do find_status if the ip address returned
203                    was valid - ie. name_query returned true.
204                  */
205                 if (find_status) {
206                         if (!do_node_status(lookup, lookup_type, &ip_list[j])) {
207                                 status = NT_STATUS_UNSUCCESSFUL;
208                         }
209                 }
210         }
211
212         TALLOC_FREE(ip_list);
213
214         return NT_STATUS_IS_OK(status);
215 }
216
217
218 /****************************************************************************
219   main program
220 ****************************************************************************/
221 int main(int argc, const char *argv[])
222 {
223         int opt;
224         unsigned int lookup_type = 0x0;
225         fstring lookup;
226         static bool find_master=False;
227         static bool lookup_by_ip = False;
228         poptContext pc = NULL;
229         TALLOC_CTX *frame = talloc_stackframe();
230         int rc = 0;
231
232         struct poptOption long_options[] = {
233                 POPT_AUTOHELP
234                 { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
235                 { "flags", 'f', POPT_ARG_NONE, NULL, 'f', "List the NMB flags returned" },
236                 { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
237                 { "master-browser", 'M', POPT_ARG_NONE, NULL, 'M', "Search for a master browser" },
238                 { "recursion", 'R', POPT_ARG_NONE, NULL, 'R', "Set recursion desired in package" },
239                 { "status", 'S', POPT_ARG_NONE, NULL, 'S', "Lookup node status as well" },
240                 { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
241                 { "root-port", 'r', POPT_ARG_NONE, NULL, 'r', "Use root port 137 (Win95 only replies to this)" },
242                 { "lookup-by-ip", 'A', POPT_ARG_NONE, NULL, 'A', "Do a node status on <name> as an IP Address" },
243                 POPT_COMMON_SAMBA
244                 POPT_COMMON_CONNECTION
245                 { 0, 0, 0, 0 }
246         };
247
248         *lookup = 0;
249
250         smb_init_locale();
251
252         setup_logging(argv[0], DEBUG_STDOUT);
253
254         pc = poptGetContext("nmblookup", argc, argv,
255                         long_options, POPT_CONTEXT_KEEP_FIRST);
256
257         poptSetOtherOptionHelp(pc, "<NODE> ...");
258
259         while ((opt = poptGetNextOpt(pc)) != -1) {
260                 switch (opt) {
261                 case 'f':
262                         give_flags = true;
263                         break;
264                 case 'M':
265                         find_master = true;
266                         break;
267                 case 'R':
268                         recursion_desired = true;
269                         break;
270                 case 'S':
271                         find_status = true;
272                         break;
273                 case 'r':
274                         RootPort = true;
275                         break;
276                 case 'A':
277                         lookup_by_ip = true;
278                         break;
279                 case 'B':
280                         if (interpret_string_addr(&bcast_addr,
281                                         poptGetOptArg(pc),
282                                         NI_NUMERICHOST)) {
283                                 got_bcast = True;
284                                 use_bcast = True;
285                         }
286                         break;
287                 case 'U':
288                         if (interpret_string_addr(&bcast_addr,
289                                         poptGetOptArg(pc),
290                                         0)) {
291                                 got_bcast = True;
292                                 use_bcast = False;
293                         }
294                         break;
295                 case 'T':
296                         translate_addresses = !translate_addresses;
297                         break;
298                 }
299         }
300
301         poptGetArg(pc); /* Remove argv[0] */
302
303         if(!poptPeekArg(pc)) {
304                 poptPrintUsage(pc, stderr, 0);
305                 rc = 1;
306                 goto out;
307         }
308
309         if (!lp_load_global(get_dyn_CONFIGFILE())) {
310                 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
311                                 get_dyn_CONFIGFILE());
312         }
313
314         load_interfaces();
315         if (!open_sockets()) {
316                 rc = 1;
317                 goto out;
318         }
319
320         while(poptPeekArg(pc)) {
321                 char *p;
322                 struct in_addr ip;
323                 size_t nbt_len;
324
325                 fstrcpy(lookup,poptGetArg(pc));
326
327                 if(lookup_by_ip) {
328                         struct sockaddr_storage ss;
329                         ip = interpret_addr2(lookup);
330                         in_addr_to_sockaddr_storage(&ss, ip);
331                         fstrcpy(lookup,"*");
332                         if (!do_node_status(lookup, lookup_type, &ss)) {
333                                 rc = 1;
334                         }
335                         continue;
336                 }
337
338                 if (find_master) {
339                         if (*lookup == '-') {
340                                 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
341                                 lookup_type = 1;
342                         } else {
343                                 lookup_type = 0x1d;
344                         }
345                 }
346
347                 p = strchr_m(lookup,'#');
348                 if (p) {
349                         *p = '\0';
350                         sscanf(++p,"%x",&lookup_type);
351                 }
352
353                 nbt_len = strlen(lookup);
354                 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
355                         d_printf("The specified netbios name [%s] is too long!\n",
356                                  lookup);
357                         continue;
358                 }
359
360
361                 if (!query_one(lookup, lookup_type)) {
362                         rc = 1;
363                         d_printf( "name_query failed to find name %s", lookup );
364                         if( 0 != lookup_type ) {
365                                 d_printf( "#%02x", lookup_type );
366                         }
367                         d_printf( "\n" );
368                 }
369         }
370
371 out:
372         poptFreeContext(pc);
373         TALLOC_FREE(frame);
374         return rc;
375 }