2015-04-25 00:51:44 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2002-03-07 10:06:00 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-03-07 10:06:00 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif /* not lint */
|
2002-02-03 07:35:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-04-25 00:51:44 +00:00
|
|
|
#include <sys/poll.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netinet/in.h>
|
1998-06-12 12:55:46 +00:00
|
|
|
#include <arpa/inet.h>
|
2001-05-28 21:37:29 +00:00
|
|
|
#include <ctype.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netdb.h>
|
2001-07-20 16:44:44 +00:00
|
|
|
#include <stdarg.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
1999-11-17 19:11:51 +00:00
|
|
|
#include <stdlib.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <string.h>
|
1998-02-19 19:07:50 +00:00
|
|
|
#include <sysexits.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <unistd.h>
|
2015-04-25 00:51:44 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-06-14 17:30:46 +00:00
|
|
|
#define ABUSEHOST "whois.abuse.net"
|
1998-02-19 19:07:50 +00:00
|
|
|
#define ANICHOST "whois.arin.net"
|
2016-01-23 01:20:58 +00:00
|
|
|
#define DENICHOST "whois.denic.de"
|
|
|
|
#define DKNICHOST "whois.dk-hostmaster.dk"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define FNICHOST "whois.afrinic.net"
|
|
|
|
#define GNICHOST "whois.nic.gov"
|
|
|
|
#define IANAHOST "whois.iana.org"
|
2016-01-23 00:28:18 +00:00
|
|
|
#define INICHOST "whois.internic.net"
|
2004-12-11 13:14:07 +00:00
|
|
|
#define KNICHOST "whois.krnic.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define LNICHOST "whois.lacnic.net"
|
1999-11-17 19:11:51 +00:00
|
|
|
#define MNICHOST "whois.ra.net"
|
2015-05-14 11:37:36 +00:00
|
|
|
#define PDBHOST "whois.peeringdb.com"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define PNICHOST "whois.apnic.net"
|
1999-11-17 19:11:51 +00:00
|
|
|
#define QNICHOST_TAIL ".whois-servers.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define RNICHOST "whois.ripe.net"
|
2016-01-22 16:43:49 +00:00
|
|
|
#define VNICHOST "whois.verisign-grs.com"
|
2015-05-14 10:33:51 +00:00
|
|
|
|
2001-08-05 19:37:12 +00:00
|
|
|
#define DEFAULT_PORT "whois"
|
2015-05-14 10:33:51 +00:00
|
|
|
|
2016-01-23 00:28:18 +00:00
|
|
|
#define WHOIS_RECURSE 0x01
|
|
|
|
#define WHOIS_QUICK 0x02
|
|
|
|
#define WHOIS_SPAM_ME 0x04
|
|
|
|
|
|
|
|
#define CHOPSPAM ">>> Last update of WHOIS database:"
|
2001-10-12 17:39:36 +00:00
|
|
|
|
|
|
|
#define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2016-01-23 00:28:18 +00:00
|
|
|
#define SCAN(p, end, check) \
|
|
|
|
while ((p) < (end)) \
|
|
|
|
if (check) ++(p); \
|
|
|
|
else break
|
|
|
|
|
2015-05-14 10:31:37 +00:00
|
|
|
static struct {
|
|
|
|
const char *suffix, *server;
|
|
|
|
} whoiswhere[] = {
|
|
|
|
/* Various handles */
|
|
|
|
{ "-ARIN", ANICHOST },
|
|
|
|
{ "-NICAT", "at" QNICHOST_TAIL },
|
|
|
|
{ "-NORID", "no" QNICHOST_TAIL },
|
|
|
|
{ "-RIPE", RNICHOST },
|
|
|
|
/* Nominet's whois server doesn't return referrals to JANET */
|
|
|
|
{ ".ac.uk", "ac.uk" QNICHOST_TAIL },
|
2016-05-25 13:09:06 +00:00
|
|
|
{ ".gov.uk", "ac.uk" QNICHOST_TAIL },
|
2016-01-23 00:28:18 +00:00
|
|
|
{ "", IANAHOST }, /* default */
|
|
|
|
{ NULL, NULL } /* safety belt */
|
2015-05-14 10:31:37 +00:00
|
|
|
};
|
|
|
|
|
2016-01-22 16:43:49 +00:00
|
|
|
#define WHOIS_REFERRAL(s) { s, sizeof(s) - 1 }
|
|
|
|
static struct {
|
|
|
|
const char *prefix;
|
|
|
|
size_t len;
|
|
|
|
} whois_referral[] = {
|
2016-01-23 00:28:18 +00:00
|
|
|
WHOIS_REFERRAL("whois:"), /* IANA */
|
|
|
|
WHOIS_REFERRAL("Whois Server:"),
|
|
|
|
WHOIS_REFERRAL("Registrar WHOIS Server:"), /* corporatedomains.com */
|
|
|
|
WHOIS_REFERRAL("ReferralServer: whois://"), /* ARIN */
|
2016-04-08 11:43:22 +00:00
|
|
|
WHOIS_REFERRAL("descr: region. Please query"), /* AfriNIC */
|
2016-01-22 16:43:49 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2016-11-17 15:19:06 +00:00
|
|
|
/*
|
|
|
|
* We have a list of patterns for RIRs that assert ignorance rather than
|
|
|
|
* providing referrals. If that happens, we guess that ARIN will be more
|
|
|
|
* helpful. But, before following a referral to an RIR, we check if we have
|
|
|
|
* asked that RIR already, and if so we make another guess.
|
|
|
|
*/
|
2016-04-08 11:43:22 +00:00
|
|
|
static const char *actually_arin[] = {
|
|
|
|
"netname: ERX-NETBLOCK\n", /* APNIC */
|
|
|
|
"netname: NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK\n",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-11-17 15:19:06 +00:00
|
|
|
static struct {
|
|
|
|
int loop;
|
|
|
|
const char *host;
|
|
|
|
} try_rir[] = {
|
|
|
|
{ 0, ANICHOST },
|
|
|
|
{ 0, RNICHOST },
|
|
|
|
{ 0, PNICHOST },
|
|
|
|
{ 0, FNICHOST },
|
|
|
|
{ 0, LNICHOST },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
reset_rir(void) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; try_rir[i].host != NULL; i++)
|
|
|
|
try_rir[i].loop = 0;
|
|
|
|
}
|
|
|
|
|
2011-11-06 18:50:33 +00:00
|
|
|
static const char *port = DEFAULT_PORT;
|
2001-06-27 23:06:47 +00:00
|
|
|
|
2016-01-23 00:28:18 +00:00
|
|
|
static const char *choose_server(char *);
|
2015-05-14 10:33:01 +00:00
|
|
|
static struct addrinfo *gethostinfo(char const *host, int exitnoname);
|
2002-02-04 02:12:49 +00:00
|
|
|
static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
|
2001-06-22 01:52:37 +00:00
|
|
|
static void usage(void);
|
2002-02-03 07:35:32 +00:00
|
|
|
static void whois(const char *, const char *, int);
|
1997-08-26 11:16:08 +00:00
|
|
|
|
|
|
|
int
|
2001-06-22 01:52:37 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-08-05 19:37:12 +00:00
|
|
|
const char *country, *host;
|
2016-01-23 00:28:18 +00:00
|
|
|
int ch, flags;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1996-04-23 06:01:10 +00:00
|
|
|
#ifdef SOCKS
|
|
|
|
SOCKSinit(argv[0]);
|
|
|
|
#endif
|
|
|
|
|
2016-01-23 00:28:18 +00:00
|
|
|
country = host = NULL;
|
|
|
|
flags = 0;
|
2016-01-22 16:43:49 +00:00
|
|
|
while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:PQrRS")) != -1) {
|
2001-06-22 01:52:37 +00:00
|
|
|
switch (ch) {
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'a':
|
|
|
|
host = ANICHOST;
|
|
|
|
break;
|
2001-08-05 19:37:12 +00:00
|
|
|
case 'A':
|
|
|
|
host = PNICHOST;
|
|
|
|
break;
|
2004-06-14 17:30:46 +00:00
|
|
|
case 'b':
|
|
|
|
host = ABUSEHOST;
|
|
|
|
break;
|
2001-08-05 19:37:12 +00:00
|
|
|
case 'c':
|
|
|
|
country = optarg;
|
2001-10-17 16:28:12 +00:00
|
|
|
break;
|
2006-01-23 13:49:39 +00:00
|
|
|
case 'f':
|
|
|
|
host = FNICHOST;
|
|
|
|
break;
|
1999-02-01 19:22:27 +00:00
|
|
|
case 'g':
|
|
|
|
host = GNICHOST;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'h':
|
|
|
|
host = optarg;
|
|
|
|
break;
|
1999-11-09 09:01:38 +00:00
|
|
|
case 'i':
|
|
|
|
host = INICHOST;
|
|
|
|
break;
|
2004-06-14 14:03:10 +00:00
|
|
|
case 'I':
|
|
|
|
host = IANAHOST;
|
|
|
|
break;
|
2004-12-11 13:14:07 +00:00
|
|
|
case 'k':
|
|
|
|
host = KNICHOST;
|
|
|
|
break;
|
2002-11-10 16:58:56 +00:00
|
|
|
case 'l':
|
|
|
|
host = LNICHOST;
|
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
case 'm':
|
|
|
|
host = MNICHOST;
|
|
|
|
break;
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'p':
|
2001-08-05 19:37:12 +00:00
|
|
|
port = optarg;
|
1998-02-19 19:07:50 +00:00
|
|
|
break;
|
2015-05-14 11:37:36 +00:00
|
|
|
case 'P':
|
|
|
|
host = PDBHOST;
|
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
case 'Q':
|
|
|
|
flags |= WHOIS_QUICK;
|
|
|
|
break;
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'r':
|
|
|
|
host = RNICHOST;
|
|
|
|
break;
|
2016-01-22 16:43:49 +00:00
|
|
|
case 'R':
|
|
|
|
flags |= WHOIS_RECURSE;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
flags |= WHOIS_SPAM_ME;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
2001-06-22 01:52:37 +00:00
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1999-12-06 21:18:32 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2001-08-05 19:37:12 +00:00
|
|
|
if (!argc || (country != NULL && host != NULL))
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
/*
|
2016-01-23 00:28:18 +00:00
|
|
|
* If no host or country is specified, rely on referrals from IANA.
|
1999-11-17 19:11:51 +00:00
|
|
|
*/
|
2001-08-05 19:37:12 +00:00
|
|
|
if (host == NULL && country == NULL) {
|
2015-05-14 10:32:35 +00:00
|
|
|
if ((host = getenv("WHOIS_SERVER")) == NULL &&
|
|
|
|
(host = getenv("RA_SERVER")) == NULL) {
|
2014-06-25 15:39:08 +00:00
|
|
|
if (!(flags & WHOIS_QUICK))
|
|
|
|
flags |= WHOIS_RECURSE;
|
|
|
|
}
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
2002-02-03 07:35:32 +00:00
|
|
|
while (argc-- > 0) {
|
2001-08-05 19:37:12 +00:00
|
|
|
if (country != NULL) {
|
2016-01-23 00:28:18 +00:00
|
|
|
char *qnichost;
|
2001-08-05 19:37:12 +00:00
|
|
|
s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(*argv, qnichost, flags);
|
2016-01-23 00:28:18 +00:00
|
|
|
free(qnichost);
|
|
|
|
} else
|
|
|
|
whois(*argv, host != NULL ? host :
|
|
|
|
choose_server(*argv), flags);
|
2016-11-17 15:19:06 +00:00
|
|
|
reset_rir();
|
2002-02-03 07:35:32 +00:00
|
|
|
argv++;
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:28:18 +00:00
|
|
|
static const char *
|
2001-07-17 20:40:41 +00:00
|
|
|
choose_server(char *domain)
|
|
|
|
{
|
2016-01-23 00:28:18 +00:00
|
|
|
size_t len = strlen(domain);
|
2015-05-14 10:31:37 +00:00
|
|
|
int i;
|
2001-07-17 20:40:41 +00:00
|
|
|
|
2015-05-14 10:31:37 +00:00
|
|
|
for (i = 0; whoiswhere[i].suffix != NULL; i++) {
|
|
|
|
size_t suffix_len = strlen(whoiswhere[i].suffix);
|
2016-01-23 00:28:18 +00:00
|
|
|
if (len > suffix_len &&
|
|
|
|
strcasecmp(domain + len - suffix_len,
|
|
|
|
whoiswhere[i].suffix) == 0)
|
|
|
|
return (whoiswhere[i].server);
|
2015-05-14 10:33:01 +00:00
|
|
|
}
|
2016-01-23 00:28:18 +00:00
|
|
|
errx(EX_SOFTWARE, "no default whois server");
|
2001-07-17 20:40:41 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 16:28:12 +00:00
|
|
|
static struct addrinfo *
|
2015-05-14 10:33:01 +00:00
|
|
|
gethostinfo(char const *host, int exit_on_noname)
|
2001-07-20 16:44:44 +00:00
|
|
|
{
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2016-01-22 16:43:49 +00:00
|
|
|
hints.ai_flags = AI_CANONNAME;
|
2001-07-20 16:44:44 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2015-05-14 10:33:01 +00:00
|
|
|
res = NULL;
|
2001-08-05 19:37:12 +00:00
|
|
|
error = getaddrinfo(host, port, &hints, &res);
|
2015-05-14 10:33:01 +00:00
|
|
|
if (error && (exit_on_noname || error != EAI_NONAME))
|
|
|
|
err(EX_NOHOST, "%s: %s", host, gai_strerror(error));
|
2001-07-20 16:44:44 +00:00
|
|
|
return (res);
|
2001-10-17 16:28:12 +00:00
|
|
|
}
|
2001-07-20 16:44:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper for asprintf(3) that exits on error.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
s_asprintf(char **ret, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
if (vasprintf(ret, format, ap) == -1) {
|
|
|
|
va_end(ap);
|
|
|
|
err(EX_OSERR, "vasprintf()");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:52:06 +00:00
|
|
|
static int
|
|
|
|
connect_to_any_host(struct addrinfo *hostres)
|
1999-11-17 19:11:51 +00:00
|
|
|
{
|
2016-05-11 17:52:06 +00:00
|
|
|
struct addrinfo *res;
|
2015-05-14 10:32:05 +00:00
|
|
|
nfds_t i, j;
|
2016-05-11 17:52:06 +00:00
|
|
|
size_t count;
|
2015-04-25 00:51:44 +00:00
|
|
|
struct pollfd *fds;
|
2016-05-11 17:52:06 +00:00
|
|
|
int timeout = 180, s = -1;
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2015-04-25 00:51:44 +00:00
|
|
|
for (res = hostres, count = 0; res; res = res->ai_next)
|
|
|
|
count++;
|
|
|
|
fds = calloc(count, sizeof(*fds));
|
|
|
|
if (fds == NULL)
|
|
|
|
err(EX_OSERR, "calloc()");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the result list elements and make non-block
|
|
|
|
* connection attempts.
|
|
|
|
*/
|
|
|
|
count = i = 0;
|
|
|
|
for (res = hostres; res != NULL; res = res->ai_next) {
|
|
|
|
s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
|
|
|
|
res->ai_protocol);
|
2001-06-22 01:52:37 +00:00
|
|
|
if (s < 0)
|
2001-06-01 13:43:20 +00:00
|
|
|
continue;
|
2015-04-25 00:51:44 +00:00
|
|
|
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
/* Add the socket to poll list */
|
|
|
|
fds[i].fd = s;
|
|
|
|
fds[i].events = POLLERR | POLLHUP |
|
|
|
|
POLLIN | POLLOUT;
|
2016-05-11 18:03:51 +00:00
|
|
|
/*
|
|
|
|
* From here until a socket connects, the
|
|
|
|
* socket fd is owned by the fds[] poll array.
|
|
|
|
*/
|
|
|
|
s = -1;
|
2015-04-25 00:51:44 +00:00
|
|
|
count++;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll only if we have something to poll,
|
|
|
|
* otherwise just go ahead and try next
|
|
|
|
* address
|
|
|
|
*/
|
|
|
|
if (count == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are at the last address, poll until a connection is
|
|
|
|
* established or we failed all connection attempts.
|
|
|
|
*/
|
|
|
|
if (res->ai_next == NULL)
|
|
|
|
timeout = INFTIM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll the watched descriptors for successful connections:
|
|
|
|
* if we still have more untried resolved addresses, poll only
|
|
|
|
* once; otherwise, poll until all descriptors have errors,
|
|
|
|
* which will be considered as ETIMEDOUT later.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = poll(fds, i, timeout);
|
|
|
|
if (n == 0) {
|
|
|
|
/*
|
|
|
|
* No event reported in time. Try with a
|
|
|
|
* smaller timeout (but cap at 2-3ms)
|
|
|
|
* after a new host have been added.
|
|
|
|
*/
|
|
|
|
if (timeout >= 3)
|
2016-05-11 18:03:51 +00:00
|
|
|
timeout >>= 1;
|
2015-04-25 00:51:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
} else if (n < 0) {
|
|
|
|
/*
|
2016-01-23 00:28:18 +00:00
|
|
|
* errno here can only be EINTR which we would
|
|
|
|
* want to clean up and bail out.
|
2015-04-25 00:51:44 +00:00
|
|
|
*/
|
|
|
|
s = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the event(s) we have seen.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
if (fds[j].fd == -1 || fds[j].events == 0 ||
|
|
|
|
fds[j].revents == 0)
|
|
|
|
continue;
|
|
|
|
if (fds[j].revents & ~(POLLIN | POLLOUT)) {
|
2016-05-11 18:03:51 +00:00
|
|
|
close(fds[j].fd);
|
2015-04-25 00:51:44 +00:00
|
|
|
fds[j].fd = -1;
|
|
|
|
fds[j].events = 0;
|
|
|
|
count--;
|
|
|
|
continue;
|
|
|
|
} else if (fds[j].revents & (POLLIN | POLLOUT)) {
|
|
|
|
/* Connect succeeded. */
|
|
|
|
s = fds[j].fd;
|
2016-05-11 18:03:51 +00:00
|
|
|
fds[j].fd = -1;
|
2015-04-25 00:51:44 +00:00
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} while (timeout == INFTIM && count != 0);
|
1999-12-06 21:18:32 +00:00
|
|
|
}
|
2015-04-25 00:51:44 +00:00
|
|
|
|
|
|
|
/* All attempts were failed */
|
|
|
|
s = -1;
|
|
|
|
if (count == 0)
|
|
|
|
errno = ETIMEDOUT;
|
2016-01-23 00:28:18 +00:00
|
|
|
|
2016-05-11 17:52:06 +00:00
|
|
|
done:
|
2015-04-25 00:51:44 +00:00
|
|
|
/* Close all watched fds except the succeeded one */
|
|
|
|
for (j = 0; j < i; j++)
|
2016-05-11 18:03:51 +00:00
|
|
|
if (fds[j].fd != -1)
|
2015-04-25 00:51:44 +00:00
|
|
|
close(fds[j].fd);
|
|
|
|
free(fds);
|
2016-05-11 17:52:06 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
whois(const char *query, const char *hostname, int flags)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct addrinfo *hostres;
|
|
|
|
char *buf, *host, *nhost, *p;
|
2016-11-17 15:19:06 +00:00
|
|
|
int comment, s, f;
|
2016-05-11 17:52:06 +00:00
|
|
|
size_t len, i;
|
|
|
|
|
|
|
|
hostres = gethostinfo(hostname, 1);
|
|
|
|
s = connect_to_any_host(hostres);
|
|
|
|
if (s == -1)
|
|
|
|
err(EX_OSERR, "connect()");
|
2016-01-23 00:28:18 +00:00
|
|
|
|
|
|
|
/* Restore default blocking behavior. */
|
|
|
|
if ((f = fcntl(s, F_GETFL)) == -1)
|
|
|
|
err(EX_OSERR, "fcntl()");
|
|
|
|
f &= ~O_NONBLOCK;
|
|
|
|
if (fcntl(s, F_SETFL, f) == -1)
|
|
|
|
err(EX_OSERR, "fcntl()");
|
1998-02-19 19:07:50 +00:00
|
|
|
|
2014-01-04 15:51:52 +00:00
|
|
|
fp = fdopen(s, "r+");
|
|
|
|
if (fp == NULL)
|
2001-06-22 01:52:37 +00:00
|
|
|
err(EX_OSERR, "fdopen()");
|
2016-01-23 00:28:18 +00:00
|
|
|
|
|
|
|
if (!(flags & WHOIS_SPAM_ME) &&
|
2016-01-23 01:20:58 +00:00
|
|
|
(strcasecmp(hostname, DENICHOST) == 0 ||
|
|
|
|
strcasecmp(hostname, "de" QNICHOST_TAIL) == 0)) {
|
|
|
|
const char *q;
|
|
|
|
int idn = 0;
|
|
|
|
for (q = query; *q != '\0'; q++)
|
|
|
|
if (!isascii(*q))
|
|
|
|
idn = 1;
|
|
|
|
fprintf(fp, "-T dn%s %s\r\n", idn ? "" : ",ace", query);
|
|
|
|
} else if (!(flags & WHOIS_SPAM_ME) &&
|
|
|
|
(strcasecmp(hostname, DKNICHOST) == 0 ||
|
|
|
|
strcasecmp(hostname, "dk" QNICHOST_TAIL) == 0))
|
2014-01-04 15:51:52 +00:00
|
|
|
fprintf(fp, "--show-handles %s\r\n", query);
|
2016-01-23 00:28:18 +00:00
|
|
|
else if ((flags & WHOIS_SPAM_ME) ||
|
|
|
|
strchr(query, ' ') != NULL)
|
|
|
|
fprintf(fp, "%s\r\n", query);
|
2016-03-31 12:13:01 +00:00
|
|
|
else if (strcasecmp(hostname, ANICHOST) == 0) {
|
|
|
|
if (strncasecmp(query, "AS", 2) == 0 &&
|
|
|
|
strspn(query+2, "0123456789") == strlen(query+2))
|
|
|
|
fprintf(fp, "+ a %s\r\n", query+2);
|
|
|
|
else
|
|
|
|
fprintf(fp, "+ %s\r\n", query);
|
|
|
|
} else if (strcasecmp(hostres->ai_canonname, VNICHOST) == 0)
|
2016-01-22 16:43:49 +00:00
|
|
|
fprintf(fp, "domain %s\r\n", query);
|
2016-01-23 00:28:18 +00:00
|
|
|
else
|
2014-01-04 15:51:52 +00:00
|
|
|
fprintf(fp, "%s\r\n", query);
|
|
|
|
fflush(fp);
|
2016-01-23 00:28:18 +00:00
|
|
|
|
2016-11-17 15:19:06 +00:00
|
|
|
comment = 0;
|
|
|
|
if (!(flags & WHOIS_SPAM_ME) &&
|
|
|
|
(strcasecmp(hostname, ANICHOST) == 0 ||
|
|
|
|
strcasecmp(hostname, RNICHOST) == 0)) {
|
|
|
|
comment = 2;
|
|
|
|
}
|
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
nhost = NULL;
|
2014-01-04 15:51:52 +00:00
|
|
|
while ((buf = fgetln(fp, &len)) != NULL) {
|
2016-01-23 00:28:18 +00:00
|
|
|
/* Nominet */
|
|
|
|
if (!(flags & WHOIS_SPAM_ME) &&
|
|
|
|
len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
|
|
|
|
break;
|
2016-11-17 15:19:06 +00:00
|
|
|
/* RIRs */
|
|
|
|
if (comment == 1 && buf[0] == '#')
|
|
|
|
break;
|
|
|
|
else if (comment == 2) {
|
|
|
|
if (strchr("#%\r\n", buf[0]) != NULL)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
comment = 1;
|
|
|
|
}
|
2016-01-23 00:28:18 +00:00
|
|
|
|
|
|
|
printf("%.*s", (int)len, buf);
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2001-06-27 23:06:47 +00:00
|
|
|
if ((flags & WHOIS_RECURSE) && nhost == NULL) {
|
2016-01-22 16:43:49 +00:00
|
|
|
for (i = 0; whois_referral[i].prefix != NULL; i++) {
|
2016-01-23 00:28:18 +00:00
|
|
|
p = buf;
|
|
|
|
SCAN(p, buf+len, *p == ' ');
|
|
|
|
if (strncasecmp(p, whois_referral[i].prefix,
|
|
|
|
whois_referral[i].len) != 0)
|
2016-01-22 16:43:49 +00:00
|
|
|
continue;
|
2016-01-23 00:28:18 +00:00
|
|
|
p += whois_referral[i].len;
|
|
|
|
SCAN(p, buf+len, *p == ' ');
|
|
|
|
host = p;
|
|
|
|
SCAN(p, buf+len, ishost(*p));
|
2016-11-17 15:19:06 +00:00
|
|
|
if (p > host)
|
2016-01-23 00:28:18 +00:00
|
|
|
s_asprintf(&nhost, "%.*s",
|
|
|
|
(int)(p - host), host);
|
2016-01-22 16:43:49 +00:00
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
2016-04-08 11:43:22 +00:00
|
|
|
for (i = 0; actually_arin[i] != NULL; i++) {
|
|
|
|
if (strncmp(buf, actually_arin[i], len) == 0) {
|
|
|
|
s_asprintf(&nhost, "%s", ANICHOST);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
2016-01-23 00:28:18 +00:00
|
|
|
/* Verisign etc. */
|
|
|
|
if (!(flags & WHOIS_SPAM_ME) &&
|
|
|
|
len >= sizeof(CHOPSPAM)-1 &&
|
|
|
|
(strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
|
|
|
|
strncasecmp(buf, CHOPSPAM+4, sizeof(CHOPSPAM)-5) == 0)) {
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
}
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
2014-01-04 15:51:52 +00:00
|
|
|
fclose(fp);
|
2016-01-23 00:28:18 +00:00
|
|
|
freeaddrinfo(hostres);
|
2016-11-17 15:19:06 +00:00
|
|
|
|
|
|
|
f = 0;
|
|
|
|
for (i = 0; try_rir[i].host != NULL; i++) {
|
|
|
|
/* Remember visits to RIRs */
|
|
|
|
if (try_rir[i].loop == 0 &&
|
|
|
|
strcasecmp(try_rir[i].host, hostname) == 0)
|
|
|
|
try_rir[i].loop = 1;
|
|
|
|
/* Do we need to find an alternative RIR? */
|
|
|
|
if (try_rir[i].loop != 0 && nhost != NULL &&
|
|
|
|
strcasecmp(try_rir[i].host, nhost) == 0) {
|
|
|
|
free(nhost);
|
|
|
|
nhost = NULL;
|
|
|
|
f = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f) {
|
|
|
|
/* Find a replacement RIR */
|
|
|
|
for (i = 0; try_rir[i].host != NULL; i++) {
|
|
|
|
if (try_rir[i].loop == 0) {
|
|
|
|
s_asprintf(&nhost, "%s",
|
|
|
|
try_rir[i].host);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-06-22 01:52:37 +00:00
|
|
|
if (nhost != NULL) {
|
2016-11-17 15:19:06 +00:00
|
|
|
/* Ignore self-referrals */
|
|
|
|
if (strcasecmp(hostname, nhost) != 0) {
|
|
|
|
printf("# %s\n\n", nhost);
|
|
|
|
whois(query, nhost, flags);
|
|
|
|
}
|
2001-06-22 01:52:37 +00:00
|
|
|
free(nhost);
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1997-08-26 11:16:08 +00:00
|
|
|
static void
|
2001-06-22 01:52:37 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-06-22 01:52:37 +00:00
|
|
|
fprintf(stderr,
|
2016-01-22 19:06:43 +00:00
|
|
|
"usage: whois [-aAbfgiIklmPQrRS] [-c country-code | -h hostname] "
|
2001-08-05 19:37:12 +00:00
|
|
|
"[-p port] name ...\n");
|
1998-02-19 19:07:50 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|