blob: 45c218e6c615947ee010ce4bec6a9fc1694ce05d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppub332e772000-01-29 12:59:01 +00002/*
3 * Mini nslookup implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
John Beppub332e772000-01-29 12:59:01 +00007 *
Eric Andersen39cdf4e2004-01-30 22:40:05 +00008 * Correct default name server display and explicit name server option
Robert Griebl31a2e202002-07-24 00:56:56 +00009 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
10 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppub332e772000-01-29 12:59:01 +000012 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010013//config:config NSLOOKUP
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020014//config: bool "nslookup (4.5 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: nslookup is a tool to query Internet name servers.
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>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
John Beppub332e772000-01-29 12:59:01 +000039
John Beppu50bc1012000-01-30 09:47:16 +000040/*
Denis Vlasenko391ffa12008-11-04 21:44:28 +000041 * I'm only implementing non-interactive mode;
42 * I totally forgot nslookup even had an interactive mode.
43 *
44 * This applet is the only user of res_init(). Without it,
45 * you may avoid pulling in _res global from libc.
John Beppu50bc1012000-01-30 09:47:16 +000046 */
John Beppub332e772000-01-29 12:59:01 +000047
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000048/* Examples of 'standard' nslookup output
49 * $ nslookup yahoo.com
50 * Server: 128.193.0.10
51 * Address: 128.193.0.10#53
Denis Vlasenkof7996f32007-01-11 17:20:00 +000052 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000053 * Non-authoritative answer:
54 * Name: yahoo.com
55 * Address: 216.109.112.135
56 * Name: yahoo.com
57 * Address: 66.94.234.13
58 *
59 * $ nslookup 204.152.191.37
60 * Server: 128.193.4.20
61 * Address: 128.193.4.20#53
Denis Vlasenkof7996f32007-01-11 17:20:00 +000062 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000063 * Non-authoritative answer:
64 * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa.
65 * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org.
Denis Vlasenkof7996f32007-01-11 17:20:00 +000066 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000067 * Authoritative answers can be found from:
68 * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org.
69 * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org.
70 * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org.
71 * ns1.kernel.org internet address = 140.211.167.34
72 * ns2.kernel.org internet address = 204.152.191.4
73 * ns3.kernel.org internet address = 204.152.191.36
74 */
John Beppub332e772000-01-29 12:59:01 +000075
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000076static int print_host(const char *hostname, const char *header)
John Beppub332e772000-01-29 12:59:01 +000077{
Denis Vlasenko42823d52007-02-04 02:39:08 +000078 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
Denis Vlasenko448f0242007-01-22 22:43:05 +000079 * not just one */
Denis Vlasenko448f0242007-01-22 22:43:05 +000080 struct addrinfo *result = NULL;
81 int rc;
82 struct addrinfo hint;
83
84 memset(&hint, 0 , sizeof(hint));
85 /* hint.ai_family = AF_UNSPEC; - zero anyway */
86 /* Needed. Or else we will get each address thrice (or more)
87 * for each possible socket type (tcp,udp,raw...): */
88 hint.ai_socktype = SOCK_STREAM;
89 // hint.ai_flags = AI_CANONNAME;
90 rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result);
91
Vitaly Magerya7f4b7692011-03-22 20:14:26 +010092 if (rc == 0) {
Denis Vlasenko448f0242007-01-22 22:43:05 +000093 struct addrinfo *cur = result;
94 unsigned cnt = 0;
95
96 printf("%-10s %s\n", header, hostname);
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +000097 // puts(cur->ai_canonname); ?
Denis Vlasenko448f0242007-01-22 22:43:05 +000098 while (cur) {
99 char *dotted, *revhost;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000100 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
101 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000102
103 printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
104 if (revhost) {
105 puts(revhost);
106 if (ENABLE_FEATURE_CLEAN_UP)
107 free(revhost);
108 }
109 if (ENABLE_FEATURE_CLEAN_UP)
110 free(dotted);
111 cur = cur->ai_next;
112 }
113 } else {
114#if ENABLE_VERBOSE_RESOLUTION_ERRORS
Denis Vlasenko5de9e9c2007-01-22 22:46:04 +0000115 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
Denis Vlasenko448f0242007-01-22 22:43:05 +0000116#else
117 bb_error_msg("can't resolve '%s'", hostname);
118#endif
119 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100120 if (ENABLE_FEATURE_CLEAN_UP && result)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000121 freeaddrinfo(result);
122 return (rc != 0);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000123}
124
Denis Vlasenko448f0242007-01-22 22:43:05 +0000125/* lookup the default nameserver and display it */
126static void server_print(void)
127{
128 char *server;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000129 struct sockaddr *sa;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000130
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000131#if ENABLE_FEATURE_IPV6
132 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
133 if (!sa)
134#endif
135 sa = (struct sockaddr*)&_res.nsaddr_list[0];
136 server = xmalloc_sockaddr2dotted_noport(sa);
137
Denis Vlasenko448f0242007-01-22 22:43:05 +0000138 print_host(server, "Server:");
139 if (ENABLE_FEATURE_CLEAN_UP)
140 free(server);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000141 bb_putchar('\n');
John Beppub332e772000-01-29 12:59:01 +0000142}
143
Robert Griebl31a2e202002-07-24 00:56:56 +0000144/* alter the global _res nameserver structure to use
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000145 an explicit dns server instead of what is in /etc/resolv.conf */
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000146static void set_default_dns(const char *server)
Robert Griebl31a2e202002-07-24 00:56:56 +0000147{
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000148 len_and_sockaddr *lsa;
Robert Griebl31a2e202002-07-24 00:56:56 +0000149
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200150 if (!server)
151 return;
152
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000153 /* NB: this works even with, say, "[::1]:5353"! :) */
154 lsa = xhost2sockaddr(server, 53);
155
156 if (lsa->u.sa.sa_family == AF_INET) {
Robert Griebl31a2e202002-07-24 00:56:56 +0000157 _res.nscount = 1;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000158 /* struct copy */
159 _res.nsaddr_list[0] = lsa->u.sin;
Robert Griebl31a2e202002-07-24 00:56:56 +0000160 }
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000161#if ENABLE_FEATURE_IPV6
Denis Vlasenko249d9482008-11-17 15:36:36 +0000162 /* Hoped libc can cope with IPv4 address there too.
163 * No such luck, glibc 2.4 segfaults even with IPv6,
164 * maybe I misunderstand how to make glibc use IPv6 addr?
165 * (uclibc 0.9.31+ should work) */
166 if (lsa->u.sa.sa_family == AF_INET6) {
167 // glibc neither SEGVs nor sends any dgrams with this
168 // (strace shows no socket ops):
169 //_res.nscount = 0;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000170 _res._u._ext.nscount = 1;
171 /* store a pointer to part of malloc'ed lsa */
172 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
173 /* must not free(lsa)! */
Denis Vlasenko249d9482008-11-17 15:36:36 +0000174 }
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000175#endif
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000176}
Robert Griebl31a2e202002-07-24 00:56:56 +0000177
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000178int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179int nslookup_main(int argc, char **argv)
John Beppub332e772000-01-29 12:59:01 +0000180{
Denis Vlasenko448f0242007-01-22 22:43:05 +0000181 /* We allow 1 or 2 arguments.
182 * The first is the name to be looked up and the second is an
183 * optional DNS server with which to do the lookup.
184 * More than 3 arguments is an error to follow the pattern of the
185 * standard nslookup */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000186 if (!argv[1] || argv[1][0] == '-' || argc > 3)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000187 bb_show_usage();
Denis Vlasenko448f0242007-01-22 22:43:05 +0000188
189 /* initialize DNS structure _res used in printing the default
190 * name server and in the explicit name server option feature. */
191 res_init();
192 /* rfc2133 says this enables IPv6 lookups */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000193 /* (but it also says "may be enabled in /etc/resolv.conf") */
Denis Vlasenko448f0242007-01-22 22:43:05 +0000194 /*_res.options |= RES_USE_INET6;*/
195
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200196 set_default_dns(argv[2]);
Robert Griebl31a2e202002-07-24 00:56:56 +0000197
Eric Andersenfe9888a2001-01-20 21:51:21 +0000198 server_print();
Denys Vlasenkod66eb902013-06-27 01:09:51 +0200199
200 /* getaddrinfo and friends are free to request a resolver
201 * reinitialization. Just in case, set_default_dns() again
202 * after getaddrinfo (in server_print). This reportedly helps
203 * with bug 675 "nslookup does not properly use second argument"
204 * at least on Debian Wheezy and Openwrt AA (eglibc based).
205 */
206 set_default_dns(argv[2]);
207
Denis Vlasenko448f0242007-01-22 22:43:05 +0000208 return print_host(argv[1], "Name:");
John Beppub332e772000-01-29 12:59:01 +0000209}