blob: d31801e9611daf136964b699cb2edf1290190685 [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 Vlasenkoae178ce2017-07-19 14:32:54 +02004//config: bool "nslookup (4.5 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:
9//config:config NSLOOKUP_BIG
10//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
17//config: depends on 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
24//usage: "[HOST] [SERVER]"
25//usage:#define nslookup_full_usage "\n\n"
26//usage: "Query the nameserver for the IP address of the given HOST\n"
27//usage: "optionally using a specified DNS server"
28//usage:
29//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
45#if !ENABLE_NSLOOKUP_BIG
46
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 Vlasenko0dd3be82018-04-14 14:05:45 +0200260 int failures;
261 int replies;
262};
263
264struct query {
265 const char *name;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200266 unsigned qlen, rlen;
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 {
274 int type;
275 const char *name;
276} 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" },
288 { }
289};
290
291static const char *const rcodes[] = {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200292 "NOERROR", // 0
293 "FORMERR", // 1
294 "SERVFAIL", // 2
295 "NXDOMAIN", // 3
296 "NOTIMP", // 4
297 "REFUSED", // 5
298 "YXDOMAIN", // 6
299 "YXRRSET", // 7
300 "NXRRSET", // 8
301 "NOTAUTH", // 9
302 "NOTZONE", // 10
303 "11", // 11 not assigned
304 "12", // 12 not assigned
305 "13", // 13 not assigned
306 "14", // 14 not assigned
307 "15", // 15 not assigned
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200308};
309
310#if ENABLE_FEATURE_IPV6
311static const char v4_mapped[] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
312#endif
313
314struct globals {
315 unsigned default_port;
316 unsigned default_retry;
317 unsigned default_timeout;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200318 unsigned serv_count;
319 struct ns *server;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200320} FIX_ALIASING;
321#define G (*(struct globals*)bb_common_bufsiz1)
322#define INIT_G() do { \
323 setup_common_bufsiz(); \
324 G.default_port = 53; \
325 G.default_retry = 2; \
326 G.default_timeout = 5; \
327} while (0)
328
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200329enum {
330 OPT_stats = (1 << 4),
331};
332
333static int parse_reply(const unsigned char *msg, size_t len)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200334{
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200335 HEADER *header;
336
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200337 ns_msg handle;
338 ns_rr rr;
339 int i, n, rdlen;
340 const char *format = NULL;
341 char astr[INET6_ADDRSTRLEN], dname[MAXDNAME];
342 const unsigned char *cp;
343
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200344 header = (HEADER *)msg;
345 if (!header->aa)
346 printf("Non-authoritative answer:\n");
347
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200348 if (ns_initparse(msg, len, &handle) != 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200349 //printf("Unable to parse reply: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200350 return -1;
351 }
352
353 for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) {
354 if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200355 //printf("Unable to parse resource record: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200356 return -1;
357 }
358
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200359 rdlen = ns_rr_rdlen(rr);
360
361 switch (ns_rr_type(rr))
362 {
363 case ns_t_a:
364 if (rdlen != 4) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200365 dbg("unexpected A record length %d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200366 return -1;
367 }
368 inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr));
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200369 printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200370 break;
371
372#if ENABLE_FEATURE_IPV6
373 case ns_t_aaaa:
374 if (rdlen != 16) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200375 dbg("unexpected AAAA record length %d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200376 return -1;
377 }
378 inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr));
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200379 /* bind-utils-9.11.3 uses the same format for A and AAAA answers */
380 printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200381 break;
382#endif
383
384 case ns_t_ns:
385 if (!format)
386 format = "%s\tnameserver = %s\n";
387 /* fall through */
388
389 case ns_t_cname:
390 if (!format)
391 format = "%s\tcanonical name = %s\n";
392 /* fall through */
393
394 case ns_t_ptr:
395 if (!format)
396 format = "%s\tname = %s\n";
397 if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200398 ns_rr_rdata(rr), dname, sizeof(dname)) < 0
399 ) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200400 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200401 return -1;
402 }
403 printf(format, ns_rr_name(rr), dname);
404 break;
405
406 case ns_t_mx:
407 if (rdlen < 2) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200408 printf("MX record too short\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200409 return -1;
410 }
411 n = ns_get16(ns_rr_rdata(rr));
412 if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200413 ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0
414 ) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200415 //printf("Cannot uncompress MX domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200416 return -1;
417 }
418 printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
419 break;
420
421 case ns_t_txt:
422 if (rdlen < 1) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200423 //printf("TXT record too short\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200424 return -1;
425 }
426 n = *(unsigned char *)ns_rr_rdata(rr);
427 if (n > 0) {
428 memset(dname, 0, sizeof(dname));
429 memcpy(dname, ns_rr_rdata(rr) + 1, n);
430 printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname);
431 }
432 break;
433
434 case ns_t_soa:
435 if (rdlen < 20) {
Denys Vlasenkocf950cd2018-04-14 23:08:23 +0200436 dbg("SOA record too short:%d\n", rdlen);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200437 return -1;
438 }
439
440 printf("%s\n", ns_rr_name(rr));
441
442 cp = ns_rr_rdata(rr);
443 n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
444 cp, dname, sizeof(dname));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200445 if (n < 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200446 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200447 return -1;
448 }
449
450 printf("\torigin = %s\n", dname);
451 cp += n;
452
453 n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
454 cp, dname, sizeof(dname));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200455 if (n < 0) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200456 //printf("Unable to uncompress domain: %s\n", strerror(errno));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200457 return -1;
458 }
459
460 printf("\tmail addr = %s\n", dname);
461 cp += n;
462
463 printf("\tserial = %lu\n", ns_get32(cp));
464 cp += 4;
465
466 printf("\trefresh = %lu\n", ns_get32(cp));
467 cp += 4;
468
469 printf("\tretry = %lu\n", ns_get32(cp));
470 cp += 4;
471
472 printf("\texpire = %lu\n", ns_get32(cp));
473 cp += 4;
474
475 printf("\tminimum = %lu\n", ns_get32(cp));
476 break;
477
478 default:
479 break;
480 }
481 }
482
483 return i;
484}
485
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200486static char *make_ptr(char resbuf[80], const char *addrstr)
487{
488 unsigned char addr[16];
489 int i;
490
491#if ENABLE_FEATURE_IPV6
492 if (inet_pton(AF_INET6, addrstr, addr)) {
493 if (memcmp(addr, v4_mapped, 12) != 0) {
494 char *ptr = resbuf;
495 for (i = 0; i < 16; i++) {
496 *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf];
497 *ptr++ = '.';
498 *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4];
499 *ptr++ = '.';
500 }
501 strcpy(ptr, "ip6.arpa");
502 }
503 else {
504 sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa",
505 addr[15], addr[14], addr[13], addr[12]);
506 }
507 return resbuf;
508 }
509#endif
510
511 if (inet_pton(AF_INET, addrstr, addr)) {
512 sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa",
513 addr[3], addr[2], addr[1], addr[0]);
514 return resbuf;
515 }
516
517 return NULL;
518}
519
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200520/*
521 * Function logic borrowed & modified from musl libc, res_msend.c
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200522 * n_queries is always > 0.
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200523 */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200524static int send_queries(struct ns *ns, struct query *query, int n_queries)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200525{
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200526 unsigned char reply[512];
527 uint8_t rcode;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200528 len_and_sockaddr *local_lsa;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200529 struct pollfd pfd;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200530 int servfail_retry = 0;
531 int n_replies = 0;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200532// int save_idx = 0;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200533 unsigned retry_interval;
534 unsigned timeout = G.default_timeout * 1000;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200535 unsigned tstart, tsent, tcur;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200536
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200537 pfd.events = POLLIN;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200538 pfd.fd = xsocket_type(&local_lsa, ns->lsa->u.sa.sa_family, SOCK_DGRAM);
539 /*
540 * local_lsa has "null" address and port 0 now.
541 * bind() ensures we have a *particular port* selected by kernel
542 * and remembered in fd, thus later recv(fd)
543 * receives only packets sent to this port.
544 */
545 xbind(pfd.fd, &local_lsa->u.sa, local_lsa->len);
546 free(local_lsa);
547 /* Make read/writes know the destination */
548 xconnect(pfd.fd, &ns->lsa->u.sa, ns->lsa->len);
549 ndelay_on(pfd.fd);
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200550
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200551 retry_interval = timeout / G.default_retry;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200552 tstart = tcur = monotonic_ms();
553 goto send;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200554
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200555 while (tcur - tstart < timeout) {
556 int qn;
557 int recvlen;
558
559 if (tcur - tsent >= retry_interval) {
560 send:
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200561 for (qn = 0; qn < n_queries; qn++) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200562 if (query[qn].rlen)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200563 continue;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200564 if (write(pfd.fd, query[qn].query, query[qn].qlen) < 0) {
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200565 bb_perror_msg("write to '%s'", ns->name);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200566 n_replies = -1; /* "no go, try next server" */
567 goto ret;
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200568 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200569 dbg("query %u sent\n", qn);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200570 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200571 tsent = tcur;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200572 servfail_retry = 2 * n_queries;
573 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200574
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200575 /* Wait for a response, or until time to retry */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200576 if (poll(&pfd, 1, retry_interval - (tcur - tsent)) <= 0)
577 goto next;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200578
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200579 recvlen = read(pfd.fd, reply, sizeof(reply));
580 if (recvlen < 0) {
581 bb_perror_msg("read");
582 next:
583 tcur = monotonic_ms();
584 continue;
585 }
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200586
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200587 if (ns->replies++ == 0) {
588 printf("Server:\t\t%s\n", ns->name);
589 printf("Address:\t%s\n\n",
590 auto_string(xmalloc_sockaddr2dotted(&ns->lsa->u.sa))
591 );
592 /* In "Address", bind-utils-9.11.3 show port after a hash: "1.2.3.4#53" */
593 /* Should we do the same? */
594 }
595
596 /* Non-identifiable packet */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200597 if (recvlen < 4) {
598 dbg("read is too short:%d\n", recvlen);
599 goto next;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200600 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200601
602 /* Find which query this answer goes with, if any */
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200603// qn = save_idx;
604 qn = 0;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200605 for (;;) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200606 if (memcmp(reply, query[qn].query, 2) == 0) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200607 dbg("response matches query %u\n", qn);
608 break;
609 }
610 if (++qn >= n_queries) {
611 dbg("response does not match any query\n");
612 goto next;
613 }
614 }
615
616 if (query[qn].rlen) {
617 dbg("dropped duplicate response to query %u\n", qn);
618 goto next;
619 }
620
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200621 rcode = reply[3] & 0x0f;
622 dbg("query %u rcode:%s\n", qn, rcodes[rcode]);
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200623
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200624 /* Retry immediately on SERVFAIL */
625 if (rcode == 2) {
626 ns->failures++;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200627 if (servfail_retry) {
628 servfail_retry--;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200629 write(pfd.fd, query[qn].query, query[qn].qlen);
630 dbg("query %u resent\n", qn);
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200631 goto next;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200632 }
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200633 }
634
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200635 /* Process reply */
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200636 query[qn].rlen = recvlen;
637 tcur = monotonic_ms();
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200638#if 1
639 if (option_mask32 & OPT_stats) {
640 printf("Query #%d completed in %ums:\n", qn, tcur - tstart);
641 }
642 if (rcode != 0) {
643 printf("** server can't find %s: %s\n",
644 query[qn].name, rcodes[rcode]);
645 } else {
646 if (parse_reply(reply, recvlen) < 0)
647 printf("*** Can't find %s: Parse error\n", query[qn].name);
648 }
649 bb_putchar('\n');
650 n_replies++;
651 if (n_replies >= n_queries)
652 goto ret;
653#else
654//used to store replies and process them later
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200655 query[qn].latency = tcur - tstart;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200656 n_replies++;
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200657 if (qn != save_idx) {
658 /* "wrong" receive buffer, move to correct one */
659 memcpy(query[qn].reply, query[save_idx].reply, recvlen);
660 continue;
661 }
662 /* query[0..save_idx] have replies, move to next one, if exists */
663 for (;;) {
664 save_idx++;
665 if (save_idx >= n_queries)
666 goto ret; /* all are full: we have all results */
667 if (!query[save_idx].rlen)
668 break; /* this one is empty */
669 }
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200670#endif
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200671 } /* while() */
672
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200673 ret:
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200674 close(pfd.fd);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200675
676 return n_replies;
677}
678
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200679static void add_ns(const char *addr)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200680{
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200681 struct ns *ns;
682 unsigned count;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200683
684 dbg("%s: addr:'%s'\n", __func__, addr);
685
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200686 count = G.serv_count++;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200687
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200688 G.server = xrealloc_vector(G.server, /*8=2^3:*/ 3, count);
689 ns = &G.server[count];
690 ns->name = addr;
691 ns->lsa = xhost2sockaddr(addr, 53);
692 /*ns->replies = 0; - already is */
693 /*ns->failures = 0; - already is */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200694}
695
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200696static void parse_resolvconf(void)
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200697{
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200698 FILE *resolv;
699
700 resolv = fopen("/etc/resolv.conf", "r");
701 if (resolv) {
702 char line[128], *p;
703
704 while (fgets(line, sizeof(line), resolv)) {
705 p = strtok(line, " \t\n");
706
707 if (!p || strcmp(p, "nameserver") != 0)
708 continue;
709
710 p = strtok(NULL, " \t\n");
711
712 if (!p)
713 continue;
714
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200715 add_ns(xstrdup(p));
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200716 }
717
718 fclose(resolv);
719 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200720}
721
722static struct query *add_query(struct query **queries, int *n_queries,
723 int type, const char *dname)
724{
725 struct query *new_q;
726 int pos;
727 ssize_t qlen;
728
729 pos = *n_queries;
730 *n_queries = pos + 1;
731 *queries = new_q = xrealloc(*queries, sizeof(**queries) * (pos + 1));
732
733 dbg("new query#%u type %u for '%s'\n", pos, type, dname);
734 new_q += pos;
735 memset(new_q, 0, sizeof(*new_q));
736
737 qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL,
738 new_q->query, sizeof(new_q->query));
739
740 new_q->qlen = qlen;
741 new_q->name = dname;
742
743 return new_q;
744}
745
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200746int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
747int nslookup_main(int argc UNUSED_PARAM, char **argv)
748{
749 struct ns *ns;
750 struct query *queries;
751 llist_t *type_strings;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200752 int n_queries;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200753 unsigned types;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200754 int opts;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200755 int rc;
756 int err;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200757
758 INIT_G();
759
760 type_strings = NULL;
761#if ENABLE_FEATURE_NSLOOKUP_LONG_OPTIONS
762 opts = getopt32long(argv, "^"
763 "+" /* '+': stop at first non-option (why?) */
764 "q:*p:+r:+t:+s"
765 "\0"
766 "-1:q::", /* minimum 1 arg, -q is a list */
767 "type\0" Required_argument "q"
768 "querytype\0" Required_argument "q"
769 "port\0" Required_argument "p"
770 "retry\0" Required_argument "r"
771 "timeout\0" Required_argument "t"
772 "stats\0" No_argument "s",
773 &type_strings, &G.default_port,
774 &G.default_retry, &G.default_timeout
775 );
776#else
777 opts = getopt32(argv, "^"
778 "+" /* '+': stop at first non-option (why?) */
779 "q:*p:+r:+t:+s"
780 "\0"
781 "-1:q::", /* minimum 1 arg, -q is a list */
782 &type_strings, &G.default_port,
783 &G.default_retry, &G.default_timeout
784 );
785#endif
786 if (G.default_port > 65535)
787 bb_error_msg_and_die("invalid server port");
788 if (G.default_retry == 0)
789 bb_error_msg_and_die("invalid retry value");
790 if (G.default_timeout == 0)
791 bb_error_msg_and_die("invalid timeout value");
792
793 types = 0;
794 while (type_strings) {
795 int c;
796 char *ptr, *chr;
797
798 ptr = llist_pop(&type_strings);
799
800 /* skip leading text, e.g. when invoked with -querytype=AAAA */
801 chr = strchr(ptr, '=');
802 if (chr)
803 ptr = chr + 1;
804
805 for (c = 0;; c++) {
806 if (!qtypes[c].name)
807 bb_error_msg_and_die("invalid query type \"%s\"", ptr);
808 if (strcmp(qtypes[c].name, ptr) == 0)
809 break;
810 }
811
812 types |= (1 << c);
813 }
814
815 argv += optind;
816
817 n_queries = 0;
818 queries = NULL;
819 do {
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200820 if (types == 0) {
Denys Vlasenko71e016d2018-04-14 22:53:39 +0200821 /* No explicit type given, guess query type.
822 * If we can convert the domain argument into a ptr (means that
823 * inet_pton() could read it) we assume a PTR request, else
824 * we issue A+AAAA queries and switch to an output format
825 * mimicking the one of the traditional nslookup applet.
826 */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200827 char *ptr;
828 char buf80[80];
829
830 ptr = make_ptr(buf80, *argv);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200831 if (ptr) {
Denys Vlasenko4e73c0f2018-04-14 23:18:34 +0200832 add_query(&queries, &n_queries, T_PTR, xstrdup(ptr));
833 } else {
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200834 add_query(&queries, &n_queries, T_A, *argv);
835#if ENABLE_FEATURE_IPV6
836 add_query(&queries, &n_queries, T_AAAA, *argv);
837#endif
838 }
839 }
840 else {
841 int c;
842 for (c = 0; qtypes[c].name; c++) {
843 if (types & (1 << c))
844 add_query(&queries, &n_queries, qtypes[c].type,
845 *argv);
846 }
847 }
848 argv++;
849 } while (argv[0] && argv[1]);
850
851 /* Use given DNS server if present */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200852 if (argv[0]) {
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200853 add_ns(argv[0]);
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200854 } else {
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200855 parse_resolvconf();
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200856 /* Fall back to localhost if we could not find NS in resolv.conf */
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200857 if (G.serv_count == 0)
858 add_ns("127.0.0.1");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200859 }
860
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200861 for (rc = 0; rc < G.serv_count;) {
862 int c;
863
864 c = send_queries(&G.server[rc], queries, n_queries);
865 if (c > 0) {
866 /* more than zero replies received */
867 if (opts & OPT_stats) {
868 printf("Replies:\t%d\n", G.server[rc].replies);
869 printf("Failures:\t%d\n\n", G.server[rc].failures);
870 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200871 break;
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200872//FIXME: we "break" even though some queries may still be not answered, and other servers may know them?
873 }
Denys Vlasenkodb93b212018-04-14 18:11:35 +0200874 /* c = 0: timed out waiting for replies */
875 /* c < 0: error (message already printed) */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200876 rc++;
Denys Vlasenko55bc8e82018-04-14 17:54:24 +0200877 if (rc >= G.serv_count) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200878//
879// NB: bind-utils-9.11.3 behavior (all to stdout, not stderr):
880//
881// $ nslookup gmail.com 8.8.8.8
882// ;; connection timed out; no servers could be reached
883//
884// $ nslookup -s gmail.com 8.8.8.8; echo EXITCODE:$?
885// <~10 sec>
886// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
887// <~10 sec>
888// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
889// <~10 sec>
890// ;; connection timed out; no servers could be reached
891// ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out.
892// <empty line>
893// EXITCODE:1
894// $ _
895 printf(";; connection timed out; no servers could be reached\n\n");
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200896 return EXIT_FAILURE;
897 }
898 }
899
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200900 err = 0;
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200901 for (rc = 0; rc < n_queries; rc++) {
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200902 if (queries[rc].rlen == 0) {
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200903 printf("*** Can't find %s: No answer\n", queries[rc].name);
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200904 err = 1;
905 }
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200906 }
Denys Vlasenko2cf75b32018-04-15 10:46:44 +0200907 if (err)
908 bb_putchar('\n'); /* should this affect exicode too? */
Denys Vlasenko0dd3be82018-04-14 14:05:45 +0200909
910 if (ENABLE_FEATURE_CLEAN_UP) {
911 free(ns);
912 if (n_queries)
913 free(queries);
914 }
915
916 return EXIT_SUCCESS;
917}
918
919#endif