Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini nslookup implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu |
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 8 | * Correct default name server display and explicit name server option |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 9 | * added by Ben Zeckel <bzeckel@hmc.edu> June 2001 |
| 10 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 12 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 13 | //config:config NSLOOKUP |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 14 | //config: bool "nslookup (4.5 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 17 | //config: nslookup is a tool to query Internet name servers. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 18 | |
| 19 | //applet:IF_NSLOOKUP(APPLET(nslookup, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_NSLOOKUP) += nslookup.o |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //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 Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 37 | #include <resolv.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 38 | #include "libbb.h" |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 39 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 40 | /* |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 41 | * 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 Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 46 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 48 | /* Examples of 'standard' nslookup output |
| 49 | * $ nslookup yahoo.com |
| 50 | * Server: 128.193.0.10 |
| 51 | * Address: 128.193.0.10#53 |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 52 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 53 | * 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 Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 62 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 63 | * 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 Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 66 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 67 | * 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 Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 76 | static int print_host(const char *hostname, const char *header) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 77 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 78 | /* We can't use xhost2sockaddr() - we want to get ALL addresses, |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 79 | * not just one */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 80 | 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 Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 92 | if (rc == 0) { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 93 | struct addrinfo *cur = result; |
| 94 | unsigned cnt = 0; |
| 95 | |
| 96 | printf("%-10s %s\n", header, hostname); |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 97 | // puts(cur->ai_canonname); ? |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 98 | while (cur) { |
| 99 | char *dotted, *revhost; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 100 | dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr); |
| 101 | revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 102 | |
| 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 Vlasenko | 5de9e9c | 2007-01-22 22:46:04 +0000 | [diff] [blame] | 115 | bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc)); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 116 | #else |
| 117 | bb_error_msg("can't resolve '%s'", hostname); |
| 118 | #endif |
| 119 | } |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 120 | if (ENABLE_FEATURE_CLEAN_UP && result) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 121 | freeaddrinfo(result); |
| 122 | return (rc != 0); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 125 | /* lookup the default nameserver and display it */ |
| 126 | static void server_print(void) |
| 127 | { |
| 128 | char *server; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 129 | struct sockaddr *sa; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 130 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 131 | #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 Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 138 | print_host(server, "Server:"); |
| 139 | if (ENABLE_FEATURE_CLEAN_UP) |
| 140 | free(server); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 141 | bb_putchar('\n'); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 144 | /* alter the global _res nameserver structure to use |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 145 | an explicit dns server instead of what is in /etc/resolv.conf */ |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 146 | static void set_default_dns(const char *server) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 147 | { |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 148 | len_and_sockaddr *lsa; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 149 | |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 150 | if (!server) |
| 151 | return; |
| 152 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 153 | /* NB: this works even with, say, "[::1]:5353"! :) */ |
| 154 | lsa = xhost2sockaddr(server, 53); |
| 155 | |
| 156 | if (lsa->u.sa.sa_family == AF_INET) { |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 157 | _res.nscount = 1; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 158 | /* struct copy */ |
| 159 | _res.nsaddr_list[0] = lsa->u.sin; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 160 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 161 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 162 | /* 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 Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 170 | _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 Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 174 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 175 | #endif |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 176 | } |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 178 | int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 179 | int nslookup_main(int argc, char **argv) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 180 | { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 181 | /* 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 Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 186 | if (!argv[1] || argv[1][0] == '-' || argc > 3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 187 | bb_show_usage(); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 188 | |
| 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 Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 193 | /* (but it also says "may be enabled in /etc/resolv.conf") */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 194 | /*_res.options |= RES_USE_INET6;*/ |
| 195 | |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 196 | set_default_dns(argv[2]); |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 197 | |
Eric Andersen | fe9888a | 2001-01-20 21:51:21 +0000 | [diff] [blame] | 198 | server_print(); |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 199 | |
| 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 Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 208 | return print_host(argv[1], "Name:"); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 209 | } |