blob: a7dd823f076b85f8a1d752d27610ce20de70ac99 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +02002
Denys Vlasenko47367e12016-11-23 09:05:14 +01003//config:config NSLOOKUP
Denys Vlasenkob097a842018-12-28 03:20:17 +01004//config: bool "nslookup (9.7 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +01005//config: default y
6//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +02007//config: nslookup is a tool to query Internet name servers.
Denys Vlasenko0dd3be82018-04-14 14:05:45 +02008//config:
Denys Vlasenkod4461ef2018-04-15 12:01:46 +02009//config:config FEATURE_NSLOOKUP_BIG
Denys Vlasenko0dd3be82018-04-14 14:05:45 +020010//config: bool "Use internal resolver code instead of libc"
11//config: depends on NSLOOKUP
12//config: default y
13//config:
14//config:config FEATURE_NSLOOKUP_LONG_OPTIONS
15//config: bool "Enable long options"
16//config: default y
Denys Vlasenkod4461ef2018-04-15 12:01:46 +020017//config: depends on FEATURE_NSLOOKUP_BIG && LONG_OPTS
Denys Vlasenko47367e12016-11-23 09:05:14 +010018
19//applet:IF_NSLOOKUP(APPLET(nslookup, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_NSLOOKUP) += nslookup.o
John Beppub332e772000-01-29 12:59:01 +000022
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define nslookup_trivial_usage
Denys Vlasenko6cdc3192018-04-15 12:49:11 +020024//usage: IF_FEATURE_NSLOOKUP_BIG("[-type=QUERY_TYPE] [-debug] ") "HOST [DNS_SERVER]"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define nslookup_full_usage "\n\n"
Denys Vlasenkod4461ef2018-04-15 12:01:46 +020026//usage: "Query DNS about HOST"
Denys Vlasenko4b6091f2018-04-15 12:57:32 +020027//usage: IF_FEATURE_NSLOOKUP_BIG("\n")
28//usage: IF_FEATURE_NSLOOKUP_BIG("\nQUERY_TYPE: soa,ns,a,"IF_FEATURE_IPV6("aaaa,")"cname,mx,txt,ptr,any")
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage:#define nslookup_example_usage
30//usage: "$ nslookup localhost\n"
31//usage: "Server: default\n"
32//usage: "Address: default\n"
33//usage: "\n"
34//usage: "Name: debian\n"
35//usage: "Address: 127.0.0.1\n"
36
Eric Andersendab3d462001-06-12 22:21:24 +000037#include <resolv.h>
Denys Vlasenko0dd3be82018-04-14 14:05:45 +020038#include <net/if.h> /* for IFNAMSIZ */
39//#include <arpa/inet.h>
40//#include <netdb.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Denys Vlasenko0dd3be82018-04-14 14:05:45 +020042#include "common_bufsiz.h"
43
44
Denys Vlasenkod4461ef2018-04-15 12:01:46 +020045#if !ENABLE_FEATURE_NSLOOKUP_BIG
Denys Vlasenko0dd3be82018-04-14 14:05:45 +020046
47/*
48 * Mini nslookup implementation for busybox
49 *
50 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
51 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
52 *
53 * Correct default name server display and explicit name server option
54 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
55 *
56 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
57 */
John Beppub332e772000-01-29 12:59:01 +000058
John Beppu50bc1012000-01-30 09:47:16 +000059/*
Denis Vlasenko391ffa12008-11-04 21:44:28 +000060 * I'm only implementing non-interactive mode;
61 * I totally forgot nslookup even had an interactive mode.
62 *
63 * This applet is the only user of res_init(). Without it,
64 * you may avoid pulling in _res global from libc.
John Beppu50bc1012000-01-30 09:47:16 +000065 */
John Beppub332e772000-01-29 12:59:01 +000066
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000067/* Examples of 'standard' nslookup output
68 * $ nslookup yahoo.com
69 * Server: 128.193.0.10
70 * Address: 128.193.0.10#53
Denis Vlasenkof7996f32007-01-11 17:20:00 +000071 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000072 * Non-authoritative answer:
73 * Name: yahoo.com
74 * Address: 216.109.112.135
75 * Name: yahoo.com
76 * Address: 66.94.234.13
77 *
78 * $ nslookup 204.152.191.37
79 * Server: 128.193.4.20
80 * Address: 128.193.4.20#53
Denis Vlasenkof7996f32007-01-11 17:20:00 +000081 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000082 * Non-authoritative answer:
83 * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa.
84 * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org.
Denis Vlasenkof7996f32007-01-11 17:20:00 +000085 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000086 * Authoritative answers can be found from:
87 * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org.
88 * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org.
89 * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org.
90 * ns1.kernel.org internet address = 140.211.167.34
91 * ns2.kernel.org internet address = 204.152.191.4
92 * ns3.kernel.org internet address = 204.152.191.36
93 */
John Beppub332e772000-01-29 12:59:01 +000094
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000095static int print_host(const char *hostname, const char *header)
John Beppub332e772000-01-29 12:59:01 +000096{
Denis Vlasenko42823d52007-02-04 02:39:08 +000097 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
Denis Vlasenko448f0242007-01-22 22:43:05 +000098 * not just one */
Denis Vlasenko448f0242007-01-22 22:43:05 +000099 struct addrinfo *result = NULL;
100 int rc;
101 struct addrinfo hint;
102
103 memset(&hint, 0 , sizeof(hint));
104 /* hint.ai_family = AF_UNSPEC; - zero anyway */
105 /* Needed. Or else we will get each address thrice (or more)
106 * for each possible socket type (tcp,udp,raw...): */
107 hint.ai_socktype = SOCK_STREAM;
108 // hint.ai_flags = AI_CANONNAME;
109 rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result);
110
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100111 if (rc == 0) {
Denis Vlasenko448f0242007-01-22 22:43:05 +0000112 struct addrinfo *cur = result;
113 unsigned cnt = 0;
114
115 printf("%-10s %s\n", header, hostname);
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000116 // puts(cur->ai_canonname); ?
Denis Vlasenko448f0242007-01-22 22:43:05 +0000117 while (cur) {
118 char *dotted, *revhost;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000119 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
120 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000121
122 printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
123 if (revhost) {
124 puts(revhost);
125 if (ENABLE_FEATURE_CLEAN_UP)
126 free(revhost);
127 }
128 if (ENABLE_FEATURE_CLEAN_UP)
129 free(dotted);
130 cur = cur->ai_next;
131 }
132 } else {
133#if ENABLE_VERBOSE_RESOLUTION_ERRORS
Denis Vlasenko5de9e9c2007-01-22 22:46:04 +0000134 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
Denis Vlasenko448f0242007-01-22 22:43:05 +0000135#else
136 bb_error_msg("can't resolve '%s'", hostname);
137#endif
138 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100139 if (ENABLE_FEATURE_CLEAN_UP && result)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000140 freeaddrinfo(result);
141 return (rc != 0);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000142}
143
Denis Vlasenko448f0242007-01-22 22:43:05 +0000144/* lookup the default nameserver and display it */
145static void server_print(void)
146{
147 char *server;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000148 struct sockaddr *sa;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000149
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000150#if ENABLE_FEATURE_IPV6
151 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
152 if (!sa)
153#endif
154 sa = (struct sockaddr*)&_res.nsaddr_list[0];
155 server = xmalloc_sockaddr2dotted_noport(sa);
156
Denis Vlasenko448f0242007-01-22 22:43:05 +0000157 print_host(server, "Server:");
158 if (ENABLE_FEATURE_CLEAN_UP)
159 free(server);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000160 bb_putchar('\n');
John Beppub332e772000-01-29 12:59:01 +0000161}
162
Robert Griebl31a2e202002-07-24 00:56:56 +0000163/* alter the global _res nameserver structure to use
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000164 an explicit dns server instead of what is in /etc/resolv.conf */
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000165static void set_default_dns(const char *server)
Robert Griebl31a2e202002-07-24 00:56:56 +0000166{
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000167 len_and_sockaddr *lsa;
Robert Griebl31a2e202002-07-24 00:56:56 +0000168
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200169 if (!server)
170 return;
171
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000172 /* NB: this works even with, say, "[::1]:5353"! :) */
173 lsa = xhost2sockaddr(server, 53);
174
175 if (lsa->u.sa.sa_family == AF_INET) {
Robert Griebl31a2e202002-07-24 00:56:56 +0000176 _res.nscount = 1;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000177 /* struct copy */
178 _res.nsaddr_list[0] = lsa->u.sin;
Robert Griebl31a2e202002-07-24 00:56:56 +0000179 }
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000180#if ENABLE_FEATURE_IPV6
Denis Vlasenko249d9482008-11-17 15:36:36 +0000181 /* Hoped libc can cope with IPv4 address there too.
182 * No such luck, glibc 2.4 segfaults even with IPv6,
183 * maybe I misunderstand how to make glibc use IPv6 addr?
184 * (uclibc 0.9.31+ should work) */
185 if (lsa->u.sa.sa_family == AF_INET6) {
186 // glibc neither SEGVs nor sends any dgrams with this
187 // (strace shows no socket ops):
188 //_res.nscount = 0;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000189 _res._u._ext.nscount = 1;
190 /* store a pointer to part of malloc'ed lsa */
191 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
192 /* must not free(lsa)! */
Denis Vlasenko249d9482008-11-17 15:36:36 +0000193 }
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000194#endif
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000195}
Robert Griebl31a2e202002-07-24 00:56:56 +0000196
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000197int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198int nslookup_main(int argc, char **argv)
John Beppub332e772000-01-29 12:59:01 +0000199{
Denis Vlasenko448f0242007-01-22 22:43:05 +0000200 /* We allow 1 or 2 arguments.
201 * The first is the name to be looked up and the second is an
202 * optional DNS server with which to do the lookup.
203 * More than 3 arguments is an error to follow the pattern of the
204 * standard nslookup */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000205 if (!argv[1] || argv[1][0] == '-' || argc > 3)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 bb_show_usage();
Denis Vlasenko448f0242007-01-22 22:43:05 +0000207
208 /* initialize DNS structure _res used in printing the default
209 * name server and in the explicit name server option feature. */
210 res_init();
211 /* rfc2133 says this enables IPv6 lookups */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000212 /* (but it also says "may be enabled in /etc/resolv.conf") */
Denis Vlasenko448f0242007-01-22 22:43:05 +0000213 /*_res.options |= RES_USE_INET6;*/
214
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200215 set_default_dns(argv[2]);
Robert Griebl31a2e202002-07-24 00:56:56 +0000216
Eric Andersenfe9888a2001-01-20 21:51:21 +0000217 server_print();
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200218
219 /* getaddrinfo and friends are free to request a resolver
220 * reinitialization. Just in case, set_default_dns() again
221 * after getaddrinfo (in server_print). This reportedly helps
222 * with bug 675 "nslookup does not properly use second argument"
223 * at least on Debian Wheezy and Openwrt AA (eglibc based).
224 */
225 set_default_dns(argv[2]);
226
Denis Vlasenko448f0242007-01-22 22:43:05 +0000227 return print_host(argv[1], "Name:");
John Beppub332e772000-01-29 12:59:01 +0000228}
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200229
230
231#else /****** A version from LEDE / OpenWRT ******/
232
233/*
234 * musl compatible nslookup
235 *
236 * Copyright (C) 2017 Jo-Philipp Wich <jo@mein.io>
237 *
238 * Permission to use, copy, modify, and/or distribute this software for any
239 * purpose with or without fee is hereby granted, provided that the above
240 * copyright notice and this permission notice appear in all copies.
241 *
242 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
243 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
244 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
245 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
246 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
247 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
248 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
249 */
250
251#if 0
252# define dbg(...) fprintf(stderr, __VA_ARGS__)
253#else
254# define dbg(...) ((void)0)
255#endif
256
257struct ns {
258 const char *name;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200259 len_and_sockaddr *lsa;
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100260 //UNUSED: int failures;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200261 int replies;
262};
263
264struct query {
265 const char *name;
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200266 unsigned qlen;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200267// unsigned latency;
268// uint8_t rcode;
269 unsigned char query[512];
270// unsigned char reply[512];
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200271};
272
273static const struct {
Denys Vlasenkoa9801092018-04-15 10:52:11 +0200274 unsigned char type;
275 char name[7];
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200276} qtypes[] = {
277 { ns_t_soa, "SOA" },
278 { ns_t_ns, "NS" },
279 { ns_t_a, "A" },
280#if ENABLE_FEATURE_IPV6
281 { ns_t_aaaa, "AAAA" },
282#endif
283 { ns_t_cname, "CNAME" },
284 { ns_t_mx, "MX" },
285 { ns_t_txt, "TXT" },
286 { ns_t_ptr, "PTR" },
287 { ns_t_any, "ANY" },
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200288};
289
290static const char *const rcodes[] = {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200291 "NOERROR", // 0
292 "FORMERR", // 1
293 "SERVFAIL", // 2
294 "NXDOMAIN", // 3
295 "NOTIMP", // 4
296 "REFUSED", // 5
297 "YXDOMAIN", // 6
298 "YXRRSET", // 7
299 "NXRRSET", // 8
300 "NOTAUTH", // 9
301 "NOTZONE", // 10
302 "11", // 11 not assigned
303 "12", // 12 not assigned
304 "13", // 13 not assigned
305 "14", // 14 not assigned
306 "15", // 15 not assigned
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200307};
308
309#if ENABLE_FEATURE_IPV6
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200310static const char v4_mapped[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xff,0xff };
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200311#endif
312
313struct globals {
314 unsigned default_port;
315 unsigned default_retry;
316 unsigned default_timeout;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200317 unsigned query_count;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200318 unsigned serv_count;
319 struct ns *server;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200320 struct query *query;
Denys Vlasenko94089782018-08-01 19:42:46 +0200321 char *search;
322 smalluint have_search_directive;
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100323 smalluint exitcode;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200324} FIX_ALIASING;
325#define G (*(struct globals*)bb_common_bufsiz1)
326#define INIT_G() do { \
327 setup_common_bufsiz(); \
328 G.default_port = 53; \
329 G.default_retry = 2; \
330 G.default_timeout = 5; \
331} while (0)
332
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200333enum {
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200334 OPT_debug = (1 << 0),
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200335};
336
337static int parse_reply(const unsigned char *msg, size_t len)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200338{
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200339 HEADER *header;
340
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200341 ns_msg handle;
342 ns_rr rr;
343 int i, n, rdlen;
344 const char *format = NULL;
345 char astr[INET6_ADDRSTRLEN], dname[MAXDNAME];
346 const unsigned char *cp;
347
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200348 header = (HEADER *)msg;
349 if (!header->aa)
350 printf("Non-authoritative answer:\n");
351
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200352 if (ns_initparse(msg, len, &handle) != 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200353 //printf("Unable to parse reply: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200354 return -1;
355 }
356
357 for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) {
358 if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200359 //printf("Unable to parse resource record: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200360 return -1;
361 }
362
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200363 rdlen = ns_rr_rdlen(rr);
364
365 switch (ns_rr_type(rr))
366 {
367 case ns_t_a:
368 if (rdlen != 4) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200369 dbg("unexpected A record length %d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200370 return -1;
371 }
372 inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr));
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200373 printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200374 break;
375
376#if ENABLE_FEATURE_IPV6
377 case ns_t_aaaa:
378 if (rdlen != 16) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200379 dbg("unexpected AAAA record length %d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200380 return -1;
381 }
382 inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr));
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200383 /* bind-utils-9.11.3 uses the same format for A and AAAA answers */
384 printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200385 break;
386#endif
387
388 case ns_t_ns:
389 if (!format)
390 format = "%s\tnameserver = %s\n";
391 /* fall through */
392
393 case ns_t_cname:
394 if (!format)
395 format = "%s\tcanonical name = %s\n";
396 /* fall through */
397
398 case ns_t_ptr:
399 if (!format)
400 format = "%s\tname = %s\n";
401 if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200402 ns_rr_rdata(rr), dname, sizeof(dname)) < 0
403 ) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200404 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200405 return -1;
406 }
407 printf(format, ns_rr_name(rr), dname);
408 break;
409
410 case ns_t_mx:
411 if (rdlen < 2) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200412 printf("MX record too short\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200413 return -1;
414 }
415 n = ns_get16(ns_rr_rdata(rr));
416 if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200417 ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0
418 ) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200419 //printf("Cannot uncompress MX domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200420 return -1;
421 }
422 printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
423 break;
424
425 case ns_t_txt:
426 if (rdlen < 1) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200427 //printf("TXT record too short\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200428 return -1;
429 }
430 n = *(unsigned char *)ns_rr_rdata(rr);
431 if (n > 0) {
432 memset(dname, 0, sizeof(dname));
433 memcpy(dname, ns_rr_rdata(rr) + 1, n);
434 printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname);
435 }
436 break;
437
438 case ns_t_soa:
439 if (rdlen < 20) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200440 dbg("SOA record too short:%d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200441 return -1;
442 }
443
444 printf("%s\n", ns_rr_name(rr));
445
446 cp = ns_rr_rdata(rr);
447 n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
448 cp, dname, sizeof(dname));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200449 if (n < 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200450 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200451 return -1;
452 }
453
454 printf("\torigin = %s\n", dname);
455 cp += n;
456
457 n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
458 cp, dname, sizeof(dname));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200459 if (n < 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200460 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200461 return -1;
462 }
463
464 printf("\tmail addr = %s\n", dname);
465 cp += n;
466
467 printf("\tserial = %lu\n", ns_get32(cp));
468 cp += 4;
469
470 printf("\trefresh = %lu\n", ns_get32(cp));
471 cp += 4;
472
473 printf("\tretry = %lu\n", ns_get32(cp));
474 cp += 4;
475
476 printf("\texpire = %lu\n", ns_get32(cp));
477 cp += 4;
478
479 printf("\tminimum = %lu\n", ns_get32(cp));
480 break;
481
482 default:
483 break;
484 }
485 }
486
487 return i;
488}
489
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200490/*
491 * Function logic borrowed & modified from musl libc, res_msend.c
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200492 * G.query_count is always > 0.
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200493 */
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200494static int send_queries(struct ns *ns)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200495{
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200496 unsigned char reply[512];
497 uint8_t rcode;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200498 len_and_sockaddr *local_lsa;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200499 struct pollfd pfd;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200500 int servfail_retry = 0;
501 int n_replies = 0;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200502// int save_idx = 0;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200503 unsigned retry_interval;
504 unsigned timeout = G.default_timeout * 1000;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200505 unsigned tstart, tsent, tcur;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200506
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200507 pfd.events = POLLIN;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200508 pfd.fd = xsocket_type(&local_lsa, ns->lsa->u.sa.sa_family, SOCK_DGRAM);
509 /*
510 * local_lsa has "null" address and port 0 now.
511 * bind() ensures we have a *particular port* selected by kernel
512 * and remembered in fd, thus later recv(fd)
513 * receives only packets sent to this port.
514 */
515 xbind(pfd.fd, &local_lsa->u.sa, local_lsa->len);
516 free(local_lsa);
517 /* Make read/writes know the destination */
518 xconnect(pfd.fd, &ns->lsa->u.sa, ns->lsa->len);
519 ndelay_on(pfd.fd);
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200520
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200521 retry_interval = timeout / G.default_retry;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200522 tstart = tcur = monotonic_ms();
523 goto send;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200524
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200525 while (tcur - tstart < timeout) {
526 int qn;
527 int recvlen;
528
529 if (tcur - tsent >= retry_interval) {
530 send:
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200531 for (qn = 0; qn < G.query_count; qn++) {
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200532 if (G.query[qn].qlen == 0)
533 continue; /* this one was replied already */
534
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200535 if (write(pfd.fd, G.query[qn].query, G.query[qn].qlen) < 0) {
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200536 bb_perror_msg("write to '%s'", ns->name);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200537 n_replies = -1; /* "no go, try next server" */
538 goto ret;
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200539 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200540 dbg("query %u sent\n", qn);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200541 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200542 tsent = tcur;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200543 servfail_retry = 2 * G.query_count;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200544 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200545
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200546 /* Wait for a response, or until time to retry */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200547 if (poll(&pfd, 1, retry_interval - (tcur - tsent)) <= 0)
548 goto next;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200549
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200550 recvlen = read(pfd.fd, reply, sizeof(reply));
551 if (recvlen < 0) {
James Byrne69374872019-07-02 11:35:03 +0200552 bb_simple_perror_msg("read");
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200553 next:
554 tcur = monotonic_ms();
555 continue;
556 }
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200557
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200558 if (ns->replies++ == 0) {
559 printf("Server:\t\t%s\n", ns->name);
560 printf("Address:\t%s\n\n",
561 auto_string(xmalloc_sockaddr2dotted(&ns->lsa->u.sa))
562 );
563 /* In "Address", bind-utils-9.11.3 show port after a hash: "1.2.3.4#53" */
564 /* Should we do the same? */
565 }
566
567 /* Non-identifiable packet */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200568 if (recvlen < 4) {
569 dbg("read is too short:%d\n", recvlen);
570 goto next;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200571 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200572
573 /* Find which query this answer goes with, if any */
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200574// qn = save_idx;
575 qn = 0;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200576 for (;;) {
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200577 if (memcmp(reply, G.query[qn].query, 2) == 0) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200578 dbg("response matches query %u\n", qn);
579 break;
580 }
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200581 if (++qn >= G.query_count) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200582 dbg("response does not match any query\n");
583 goto next;
584 }
585 }
586
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200587 if (G.query[qn].qlen == 0) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200588 dbg("dropped duplicate response to query %u\n", qn);
589 goto next;
590 }
591
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200592 rcode = reply[3] & 0x0f;
593 dbg("query %u rcode:%s\n", qn, rcodes[rcode]);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200594
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200595 /* Retry immediately on SERVFAIL */
596 if (rcode == 2) {
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100597 //UNUSED: ns->failures++;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200598 if (servfail_retry) {
599 servfail_retry--;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200600 write(pfd.fd, G.query[qn].query, G.query[qn].qlen);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200601 dbg("query %u resent\n", qn);
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200602 goto next;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200603 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200604 }
605
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200606 /* Process reply */
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200607 G.query[qn].qlen = 0; /* flag: "reply received" */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200608 tcur = monotonic_ms();
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200609#if 1
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200610 if (option_mask32 & OPT_debug) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200611 printf("Query #%d completed in %ums:\n", qn, tcur - tstart);
612 }
613 if (rcode != 0) {
614 printf("** server can't find %s: %s\n",
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200615 G.query[qn].name, rcodes[rcode]);
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100616 G.exitcode = EXIT_FAILURE;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200617 } else {
Jo-Philipp Wich8c3b5202019-06-27 17:27:28 +0200618 switch (parse_reply(reply, recvlen)) {
619 case -1:
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200620 printf("*** Can't find %s: Parse error\n", G.query[qn].name);
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100621 G.exitcode = EXIT_FAILURE;
Jo-Philipp Wich8c3b5202019-06-27 17:27:28 +0200622 break;
623
624 case 0:
625 printf("*** Can't find %s: No answer\n", G.query[qn].name);
626 break;
Denys Vlasenko6ca8e342019-01-09 13:38:57 +0100627 }
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200628 }
629 bb_putchar('\n');
630 n_replies++;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200631 if (n_replies >= G.query_count)
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200632 goto ret;
633#else
634//used to store replies and process them later
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200635 G.query[qn].latency = tcur - tstart;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200636 n_replies++;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200637 if (qn != save_idx) {
638 /* "wrong" receive buffer, move to correct one */
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200639 memcpy(G.query[qn].reply, G.query[save_idx].reply, recvlen);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200640 continue;
641 }
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200642 /* G.query[0..save_idx] have replies, move to next one, if exists */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200643 for (;;) {
644 save_idx++;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200645 if (save_idx >= G.query_count)
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200646 goto ret; /* all are full: we have all results */
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200647 if (!G.query[save_idx].rlen)
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200648 break; /* this one is empty */
649 }
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200650#endif
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200651 } /* while() */
652
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200653 ret:
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200654 close(pfd.fd);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200655
656 return n_replies;
657}
658
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200659static void add_ns(const char *addr)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200660{
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200661 struct ns *ns;
662 unsigned count;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200663
664 dbg("%s: addr:'%s'\n", __func__, addr);
665
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200666 count = G.serv_count++;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200667
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200668 G.server = xrealloc_vector(G.server, /*8=2^3:*/ 3, count);
669 ns = &G.server[count];
670 ns->name = addr;
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200671 ns->lsa = xhost2sockaddr(addr, G.default_port);
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200672 /*ns->replies = 0; - already is */
673 /*ns->failures = 0; - already is */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200674}
675
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200676static void parse_resolvconf(void)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200677{
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200678 FILE *resolv;
679
680 resolv = fopen("/etc/resolv.conf", "r");
681 if (resolv) {
Denys Vlasenko94089782018-08-01 19:42:46 +0200682 char line[512]; /* "search" is defined to be up to 256 chars */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200683
684 while (fgets(line, sizeof(line), resolv)) {
Denys Vlasenko94089782018-08-01 19:42:46 +0200685 char *p, *arg;
686
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200687 p = strtok(line, " \t\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200688 if (!p)
689 continue;
Denys Vlasenko94089782018-08-01 19:42:46 +0200690 dbg("resolv_key:'%s'\n", p);
691 arg = strtok(NULL, "\n");
692 dbg("resolv_arg:'%s'\n", arg);
693 if (!arg)
694 continue;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200695
Denys Vlasenko94089782018-08-01 19:42:46 +0200696 if (strcmp(p, "domain") == 0) {
697 /* domain DOM */
698 if (!G.have_search_directive)
699 goto set_search;
700 continue;
701 }
702 if (strcmp(p, "search") == 0) {
703 /* search DOM1 DOM2... */
704 G.have_search_directive = 1;
705 set_search:
706 free(G.search);
707 G.search = xstrdup(arg);
708 dbg("search='%s'\n", G.search);
709 continue;
710 }
711
712 if (strcmp(p, "nameserver") != 0)
713 continue;
714
715 /* nameserver DNS */
716 add_ns(xstrdup(arg));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200717 }
718
719 fclose(resolv);
720 }
Denys Vlasenko94089782018-08-01 19:42:46 +0200721
722 if (!G.search) {
723 /* default search domain is domain part of hostname */
724 char *h = safe_gethostname();
725 char *d = strchr(h, '.');
726 if (d) {
727 G.search = d + 1;
728 dbg("search='%s' (from hostname)\n", G.search);
729 }
730 /* else free(h); */
731 }
732
733 /* Cater for case of "domain ." in resolv.conf */
734 if (G.search && LONE_CHAR(G.search, '.'))
735 G.search = NULL;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200736}
737
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200738static void add_query(int type, const char *dname)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200739{
740 struct query *new_q;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200741 unsigned count;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200742 ssize_t qlen;
743
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200744 count = G.query_count++;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200745
Denys Vlasenko94089782018-08-01 19:42:46 +0200746 G.query = xrealloc_vector(G.query, /*4=2^2:*/ 2, count);
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200747 new_q = &G.query[count];
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200748
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200749 dbg("new query#%u type %u for '%s'\n", count, type, dname);
750 new_q->name = dname;
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200751
752 qlen = res_mkquery(QUERY, dname, C_IN, type,
753 /*data:*/ NULL, /*datalen:*/ 0,
754 /*newrr:*/ NULL,
755 new_q->query, sizeof(new_q->query)
756 );
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200757 new_q->qlen = qlen;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200758}
759
Denys Vlasenko94089782018-08-01 19:42:46 +0200760static void add_query_with_search(int type, const char *dname)
761{
762 char *s;
763
764 if (type == T_PTR || !G.search || strchr(dname, '.')) {
765 add_query(type, dname);
766 return;
767 }
768
769 s = G.search;
770 for (;;) {
771 char *fullname, *e;
772
773 e = skip_non_whitespace(s);
774 fullname = xasprintf("%s.%.*s", dname, (int)(e - s), s);
775 add_query(type, fullname);
776 s = skip_whitespace(e);
777 if (!*s)
778 break;
779 }
780}
781
Denys Vlasenkoc7249952018-04-15 20:04:57 +0200782static char *make_ptr(const char *addrstr)
783{
784 unsigned char addr[16];
Denys Vlasenkoc7249952018-04-15 20:04:57 +0200785
786#if ENABLE_FEATURE_IPV6
787 if (inet_pton(AF_INET6, addrstr, addr)) {
788 if (memcmp(addr, v4_mapped, 12) != 0) {
Denys Vlasenko2f7738e2018-06-27 14:44:35 +0200789 int i;
Denys Vlasenkoc7249952018-04-15 20:04:57 +0200790 char resbuf[80];
791 char *ptr = resbuf;
792 for (i = 0; i < 16; i++) {
793 *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf];
794 *ptr++ = '.';
795 *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4];
796 *ptr++ = '.';
797 }
798 strcpy(ptr, "ip6.arpa");
799 return xstrdup(resbuf);
800 }
801 return xasprintf("%u.%u.%u.%u.in-addr.arpa",
802 addr[15], addr[14], addr[13], addr[12]);
803 }
804#endif
805
806 if (inet_pton(AF_INET, addrstr, addr)) {
807 return xasprintf("%u.%u.%u.%u.in-addr.arpa",
808 addr[3], addr[2], addr[1], addr[0]);
809 }
810
811 return NULL;
812}
813
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200814int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
815int nslookup_main(int argc UNUSED_PARAM, char **argv)
816{
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200817 unsigned types;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200818 int rc;
819 int err;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200820
821 INIT_G();
822
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200823 /* manpage: "Options can also be specified on the command line
824 * if they precede the arguments and are prefixed with a hyphen."
825 */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200826 types = 0;
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200827 argv++;
828 for (;;) {
829 const char *options =
830// bind-utils-9.11.3 accept these:
831// class= cl=
832// type= ty= querytype= query= qu= q=
833// domain= do=
834// port= po=
835// timeout= t=
836// retry= ret=
837// ndots=
838// recurse
839// norecurse
840// defname
841// nodefname
842// vc
843// novc
844// debug
845// nodebug
846// d2
847// nod2
848// search
849// nosearch
850// sil
851// fail
852// nofail
853// ver (prints version and exits)
854 "type\0" /* 0 */
855 "querytype\0" /* 1 */
856 "port\0" /* 2 */
857 "retry\0" /* 3 */
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200858 "debug\0" /* 4 */
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200859 "t\0" /* disambiguate with "type": else -t=2 fails */
860 "timeout\0" /* 6 */
861 "";
862 int i;
863 char *arg;
864 char *val;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200865
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200866 if (!*argv)
867 bb_show_usage();
868 if (argv[0][0] != '-')
869 break;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200870
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200871 /* Separate out "=val" part */
872 arg = (*argv++) + 1;
873 val = strchrnul(arg, '=');
874 if (*val)
875 *val++ = '\0';
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200876
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200877 i = index_in_substrings(options, arg);
878 //bb_error_msg("i:%d arg:'%s' val:'%s'", i, arg, val);
879 if (i < 0)
880 bb_show_usage();
881
882 if (i <= 1) {
883 for (i = 0;; i++) {
884 if (i == ARRAY_SIZE(qtypes))
885 bb_error_msg_and_die("invalid query type \"%s\"", val);
Denys Vlasenko4b6091f2018-04-15 12:57:32 +0200886 if (strcasecmp(qtypes[i].name, val) == 0)
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200887 break;
888 }
889 types |= (1 << i);
890 continue;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200891 }
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200892 if (i == 2) {
893 G.default_port = xatou_range(val, 1, 0xffff);
894 }
895 if (i == 3) {
896 G.default_retry = xatou_range(val, 1, INT_MAX);
897 }
898 if (i == 4) {
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200899 option_mask32 |= OPT_debug;
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200900 }
901 if (i > 4) {
902 G.default_timeout = xatou_range(val, 1, INT_MAX / 1000);
903 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200904 }
905
Denys Vlasenko94089782018-08-01 19:42:46 +0200906 /* Use given DNS server if present */
907 if (argv[1]) {
908 if (argv[2])
909 bb_show_usage();
910 add_ns(argv[1]);
911 } else {
912 parse_resolvconf();
913 /* Fall back to localhost if we could not find NS in resolv.conf */
914 if (G.serv_count == 0)
915 add_ns("127.0.0.1");
916 }
917
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200918 if (types == 0) {
919 /* No explicit type given, guess query type.
920 * If we can convert the domain argument into a ptr (means that
921 * inet_pton() could read it) we assume a PTR request, else
922 * we issue A+AAAA queries and switch to an output format
923 * mimicking the one of the traditional nslookup applet.
924 */
925 char *ptr;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200926
Denys Vlasenkoc7249952018-04-15 20:04:57 +0200927 ptr = make_ptr(argv[0]);
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200928 if (ptr) {
Denys Vlasenkoc7249952018-04-15 20:04:57 +0200929 add_query(T_PTR, ptr);
Denys Vlasenkoa9801092018-04-15 10:52:11 +0200930 } else {
Denys Vlasenko94089782018-08-01 19:42:46 +0200931 add_query_with_search(T_A, argv[0]);
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200932#if ENABLE_FEATURE_IPV6
Denys Vlasenko94089782018-08-01 19:42:46 +0200933 add_query_with_search(T_AAAA, argv[0]);
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200934#endif
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200935 }
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200936 } else {
937 int c;
938 for (c = 0; c < ARRAY_SIZE(qtypes); c++) {
939 if (types & (1 << c))
Denys Vlasenko94089782018-08-01 19:42:46 +0200940 add_query_with_search(qtypes[c].type, argv[0]);
Denys Vlasenkod4461ef2018-04-15 12:01:46 +0200941 }
942 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200943
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200944 for (rc = 0; rc < G.serv_count;) {
945 int c;
946
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200947 c = send_queries(&G.server[rc]);
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200948 if (c > 0) {
949 /* more than zero replies received */
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200950#if 0 /* which version does this? */
951 if (option_mask32 & OPT_debug) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200952 printf("Replies:\t%d\n", G.server[rc].replies);
953 printf("Failures:\t%d\n\n", G.server[rc].failures);
954 }
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200955#endif
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200956 break;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200957//FIXME: we "break" even though some queries may still be not answered, and other servers may know them?
958 }
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200959 /* c = 0: timed out waiting for replies */
960 /* c < 0: error (message already printed) */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200961 rc++;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200962 if (rc >= G.serv_count) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200963//
964// NB: bind-utils-9.11.3 behavior (all to stdout, not stderr):
965//
966// $ nslookup gmail.com 8.8.8.8
967// ;; connection timed out; no servers could be reached
968//
Denys Vlasenko6cdc3192018-04-15 12:49:11 +0200969// Using TCP mode:
970// $ nslookup -vc gmail.com 8.8.8.8; echo EXITCODE:$?
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200971// <~10 sec>
972// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
973// <~10 sec>
974// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
975// <~10 sec>
976// ;; connection timed out; no servers could be reached
977// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
978// <empty line>
979// EXITCODE:1
980// $ _
981 printf(";; connection timed out; no servers could be reached\n\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200982 return EXIT_FAILURE;
983 }
984 }
985
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200986 err = 0;
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200987 for (rc = 0; rc < G.query_count; rc++) {
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200988 if (G.query[rc].qlen) {
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200989 printf("*** Can't find %s: No answer\n", G.query[rc].name);
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200990 err = 1;
991 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200992 }
Denys Vlasenko71e4b3f2018-04-15 19:51:42 +0200993 if (err) /* should this affect exicode too? */
994 bb_putchar('\n');
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200995
996 if (ENABLE_FEATURE_CLEAN_UP) {
Denys Vlasenko58e43a42018-04-15 14:10:45 +0200997 free(G.server);
998 free(G.query);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200999 }
1000
Denys Vlasenko6ca8e342019-01-09 13:38:57 +01001001 return G.exitcode;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +02001002}
1003
1004#endif