2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
64 #define ABUSEHOST "whois.abuse.net"
65 #define ANICHOST "whois.arin.net"
66 #define DENICHOST "whois.denic.de"
67 #define DKNICHOST "whois.dk-hostmaster.dk"
68 #define FNICHOST "whois.afrinic.net"
69 #define GNICHOST "whois.nic.gov"
70 #define IANAHOST "whois.iana.org"
71 #define INICHOST "whois.internic.net"
72 #define KNICHOST "whois.krnic.net"
73 #define LNICHOST "whois.lacnic.net"
74 #define MNICHOST "whois.ra.net"
75 #define PDBHOST "whois.peeringdb.com"
76 #define PNICHOST "whois.apnic.net"
77 #define QNICHOST_TAIL ".whois-servers.net"
78 #define RNICHOST "whois.ripe.net"
79 #define VNICHOST "whois.verisign-grs.com"
81 #define DEFAULT_PORT "whois"
83 #define WHOIS_RECURSE 0x01
84 #define WHOIS_QUICK 0x02
85 #define WHOIS_SPAM_ME 0x04
87 #define CHOPSPAM ">>> Last update of WHOIS database:"
89 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
91 #define SCAN(p, end, check) \
97 const char *suffix, *server;
100 { "-ARIN", ANICHOST },
101 { "-NICAT", "at" QNICHOST_TAIL },
102 { "-NORID", "no" QNICHOST_TAIL },
103 { "-RIPE", RNICHOST },
104 /* Nominet's whois server doesn't return referrals to JANET */
105 { ".ac.uk", "ac.uk" QNICHOST_TAIL },
106 { ".gov.uk", "ac.uk" QNICHOST_TAIL },
107 { "", IANAHOST }, /* default */
108 { NULL, NULL } /* safety belt */
111 #define WHOIS_REFERRAL(s) { s, sizeof(s) - 1 }
115 } whois_referral[] = {
116 WHOIS_REFERRAL("whois:"), /* IANA */
117 WHOIS_REFERRAL("Whois Server:"),
118 WHOIS_REFERRAL("Registrar WHOIS Server:"), /* corporatedomains.com */
119 WHOIS_REFERRAL("ReferralServer: whois://"), /* ARIN */
120 WHOIS_REFERRAL("ReferralServer: rwhois://"), /* ARIN */
121 WHOIS_REFERRAL("descr: region. Please query"), /* AfriNIC */
126 * We have a list of patterns for RIRs that assert ignorance rather than
127 * providing referrals. If that happens, we guess that ARIN will be more
128 * helpful. But, before following a referral to an RIR, we check if we have
129 * asked that RIR already, and if so we make another guess.
131 static const char *actually_arin[] = {
132 "netname: ERX-NETBLOCK\n", /* APNIC */
133 "netname: NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK\n",
153 for (i = 0; try_rir[i].host != NULL; i++)
157 static const char *port = DEFAULT_PORT;
159 static const char *choose_server(char *);
160 static struct addrinfo *gethostinfo(const char *, const char *, int);
161 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
162 static void usage(void);
163 static void whois(const char *, const char *, const char *, int);
166 main(int argc, char *argv[])
168 const char *country, *host;
175 country = host = NULL;
177 while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:PQrRS")) != -1) {
222 flags |= WHOIS_QUICK;
228 flags |= WHOIS_RECURSE;
231 flags |= WHOIS_SPAM_ME;
242 if (!argc || (country != NULL && host != NULL))
246 * If no host or country is specified, rely on referrals from IANA.
248 if (host == NULL && country == NULL) {
249 if ((host = getenv("WHOIS_SERVER")) == NULL &&
250 (host = getenv("RA_SERVER")) == NULL) {
251 if (!(flags & WHOIS_QUICK))
252 flags |= WHOIS_RECURSE;
256 if (country != NULL) {
258 s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
259 whois(*argv, qnichost, port, flags);
262 whois(*argv, host != NULL ? host :
263 choose_server(*argv), port, flags);
271 choose_server(char *domain)
273 size_t len = strlen(domain);
276 for (i = 0; whoiswhere[i].suffix != NULL; i++) {
277 size_t suffix_len = strlen(whoiswhere[i].suffix);
278 if (len > suffix_len &&
279 strcasecmp(domain + len - suffix_len,
280 whoiswhere[i].suffix) == 0)
281 return (whoiswhere[i].server);
283 errx(EX_SOFTWARE, "no default whois server");
286 static struct addrinfo *
287 gethostinfo(const char *host, const char *hport, int exit_on_noname)
289 struct addrinfo hints, *res;
292 memset(&hints, 0, sizeof(hints));
293 hints.ai_flags = AI_CANONNAME;
294 hints.ai_family = AF_UNSPEC;
295 hints.ai_socktype = SOCK_STREAM;
297 error = getaddrinfo(host, hport, &hints, &res);
298 if (error && (exit_on_noname || error != EAI_NONAME))
299 err(EX_NOHOST, "%s: %s", host, gai_strerror(error));
304 * Wrapper for asprintf(3) that exits on error.
307 s_asprintf(char **ret, const char *format, ...)
311 va_start(ap, format);
312 if (vasprintf(ret, format, ap) == -1) {
314 err(EX_OSERR, "vasprintf()");
320 connect_to_any_host(struct addrinfo *hostres)
322 struct addrinfo *res;
326 int timeout = 180, s = -1;
328 for (res = hostres, count = 0; res; res = res->ai_next)
330 fds = calloc(count, sizeof(*fds));
332 err(EX_OSERR, "calloc()");
335 * Traverse the result list elements and make non-block
336 * connection attempts.
339 for (res = hostres; res != NULL; res = res->ai_next) {
340 s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
344 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
345 if (errno == EINPROGRESS) {
346 /* Add the socket to poll list */
348 fds[i].events = POLLERR | POLLHUP |
351 * From here until a socket connects, the
352 * socket fd is owned by the fds[] poll array.
362 * Poll only if we have something to poll,
363 * otherwise just go ahead and try next
373 * If we are at the last address, poll until a connection is
374 * established or we failed all connection attempts.
376 if (res->ai_next == NULL)
380 * Poll the watched descriptors for successful connections:
381 * if we still have more untried resolved addresses, poll only
382 * once; otherwise, poll until all descriptors have errors,
383 * which will be considered as ETIMEDOUT later.
388 n = poll(fds, i, timeout);
391 * No event reported in time. Try with a
392 * smaller timeout (but cap at 2-3ms)
393 * after a new host have been added.
401 * errno here can only be EINTR which we would
402 * want to clean up and bail out.
409 * Check for the event(s) we have seen.
411 for (j = 0; j < i; j++) {
412 if (fds[j].fd == -1 || fds[j].events == 0 ||
415 if (fds[j].revents & ~(POLLIN | POLLOUT)) {
421 } else if (fds[j].revents & (POLLIN | POLLOUT)) {
422 /* Connect succeeded. */
430 } while (timeout == INFTIM && count != 0);
433 /* All attempts were failed */
439 /* Close all watched fds except the succeeded one */
440 for (j = 0; j < i; j++)
448 whois(const char *query, const char *hostname, const char *hostport, int flags)
451 struct addrinfo *hostres;
452 char *buf, *host, *nhost, *nport, *p;
456 hostres = gethostinfo(hostname, hostport, 1);
457 s = connect_to_any_host(hostres);
459 err(EX_OSERR, "connect()");
461 /* Restore default blocking behavior. */
462 if ((f = fcntl(s, F_GETFL)) == -1)
463 err(EX_OSERR, "fcntl()");
465 if (fcntl(s, F_SETFL, f) == -1)
466 err(EX_OSERR, "fcntl()");
468 fp = fdopen(s, "r+");
470 err(EX_OSERR, "fdopen()");
472 if (!(flags & WHOIS_SPAM_ME) &&
473 (strcasecmp(hostname, DENICHOST) == 0 ||
474 strcasecmp(hostname, "de" QNICHOST_TAIL) == 0)) {
477 for (q = query; *q != '\0'; q++)
480 fprintf(fp, "-T dn%s %s\r\n", idn ? "" : ",ace", query);
481 } else if (!(flags & WHOIS_SPAM_ME) &&
482 (strcasecmp(hostname, DKNICHOST) == 0 ||
483 strcasecmp(hostname, "dk" QNICHOST_TAIL) == 0))
484 fprintf(fp, "--show-handles %s\r\n", query);
485 else if ((flags & WHOIS_SPAM_ME) ||
486 strchr(query, ' ') != NULL)
487 fprintf(fp, "%s\r\n", query);
488 else if (strcasecmp(hostname, ANICHOST) == 0) {
489 if (strncasecmp(query, "AS", 2) == 0 &&
490 strspn(query+2, "0123456789") == strlen(query+2))
491 fprintf(fp, "+ a %s\r\n", query+2);
493 fprintf(fp, "+ %s\r\n", query);
494 } else if (strcasecmp(hostres->ai_canonname, VNICHOST) == 0)
495 fprintf(fp, "domain %s\r\n", query);
497 fprintf(fp, "%s\r\n", query);
501 if (!(flags & WHOIS_SPAM_ME) &&
502 (strcasecmp(hostname, ANICHOST) == 0 ||
503 strcasecmp(hostname, RNICHOST) == 0)) {
508 while ((buf = fgetln(fp, &len)) != NULL) {
510 if (!(flags & WHOIS_SPAM_ME) &&
511 len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
514 if (comment == 1 && buf[0] == '#')
516 else if (comment == 2) {
517 if (strchr("#%\r\n", buf[0]) != NULL)
523 printf("%.*s", (int)len, buf);
525 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
526 for (i = 0; whois_referral[i].prefix != NULL; i++) {
528 SCAN(p, buf+len, *p == ' ');
529 if (strncasecmp(p, whois_referral[i].prefix,
530 whois_referral[i].len) != 0)
532 p += whois_referral[i].len;
533 SCAN(p, buf+len, *p == ' ');
535 SCAN(p, buf+len, ishost(*p));
539 s_asprintf(&nhost, "%.*s",
540 (int)(p - host), host);
543 s_asprintf(&nport, "%s", port);
548 SCAN(p, buf+len, isdigit(*p));
549 if (p > pstr && (p - pstr) < 6) {
550 s_asprintf(&nport, "%.*s",
551 (int)(p - pstr), pstr);
555 /* Invalid port; don't recurse */
561 for (i = 0; actually_arin[i] != NULL; i++) {
562 if (strncmp(buf, actually_arin[i], len) == 0) {
563 s_asprintf(&nhost, "%s", ANICHOST);
564 s_asprintf(&nport, "%s", port);
570 if (!(flags & WHOIS_SPAM_ME) &&
571 len >= sizeof(CHOPSPAM)-1 &&
572 (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
573 strncasecmp(buf, CHOPSPAM+4, sizeof(CHOPSPAM)-5) == 0)) {
579 freeaddrinfo(hostres);
582 for (i = 0; try_rir[i].host != NULL; i++) {
583 /* Remember visits to RIRs */
584 if (try_rir[i].loop == 0 &&
585 strcasecmp(try_rir[i].host, hostname) == 0)
587 /* Do we need to find an alternative RIR? */
588 if (try_rir[i].loop != 0 && nhost != NULL &&
589 strcasecmp(try_rir[i].host, nhost) == 0) {
598 /* Find a replacement RIR */
599 for (i = 0; try_rir[i].host != NULL; i++) {
600 if (try_rir[i].loop == 0) {
601 s_asprintf(&nhost, "%s", try_rir[i].host);
602 s_asprintf(&nport, "%s", port);
608 /* Ignore self-referrals */
609 if (strcasecmp(hostname, nhost) != 0) {
610 printf("# %s\n\n", nhost);
611 whois(query, nhost, nport, flags);
622 "usage: whois [-aAbfgiIklmPQrRS] [-c country-code | -h hostname] "
623 "[-p port] name ...\n");