2015-04-25 00:51:44 +00:00
|
|
|
/*-
|
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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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 00:28:18 +00:00
|
|
|
#define DENICHOST "de" QNICHOST_TAIL
|
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-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-01-22 16:43:49 +00:00
|
|
|
{ NULL, 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);
|
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);
|
|
|
|
}
|
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
static void
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(const char *query, const char *hostname, int flags)
|
1999-11-17 19:11:51 +00:00
|
|
|
{
|
2014-01-04 15:51:52 +00:00
|
|
|
FILE *fp;
|
2002-02-03 07:35:32 +00:00
|
|
|
struct addrinfo *hostres, *res;
|
2001-10-12 17:39:36 +00:00
|
|
|
char *buf, *host, *nhost, *p;
|
2016-01-23 00:28:18 +00:00
|
|
|
int s = -1, f;
|
2015-05-14 10:32:05 +00:00
|
|
|
nfds_t i, j;
|
2016-01-22 16:43:49 +00:00
|
|
|
size_t len, count;
|
2015-04-25 00:51:44 +00:00
|
|
|
struct pollfd *fds;
|
|
|
|
int timeout = 180;
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2002-02-03 07:35:32 +00:00
|
|
|
hostres = gethostinfo(hostname, 1);
|
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;
|
|
|
|
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)
|
|
|
|
timeout <<= 1;
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
close(s);
|
|
|
|
fds[j].fd = -1;
|
|
|
|
fds[j].events = 0;
|
|
|
|
count--;
|
|
|
|
continue;
|
|
|
|
} else if (fds[j].revents & (POLLIN | POLLOUT)) {
|
|
|
|
/* Connect succeeded. */
|
|
|
|
s = fds[j].fd;
|
|
|
|
|
|
|
|
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;
|
|
|
|
done:
|
2016-01-23 00:28:18 +00:00
|
|
|
if (s == -1)
|
|
|
|
err(EX_OSERR, "connect()");
|
|
|
|
|
2015-04-25 00:51:44 +00:00
|
|
|
/* Close all watched fds except the succeeded one */
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
if (fds[j].fd != s && fds[j].fd != -1)
|
|
|
|
close(fds[j].fd);
|
|
|
|
free(fds);
|
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) &&
|
|
|
|
strcmp(hostname, DENICHOST) == 0)
|
2015-08-23 08:22:00 +00:00
|
|
|
fprintf(fp, "-T dn,ace -C ISO-8859-1 %s\r\n", query);
|
2016-01-23 00:28:18 +00:00
|
|
|
else if (!(flags & WHOIS_SPAM_ME) &&
|
|
|
|
strcmp(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);
|
|
|
|
else if (strcmp(hostname, ANICHOST) == 0)
|
|
|
|
fprintf(fp, "+ %s\r\n", query);
|
|
|
|
else if (strcmp(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
|
|
|
|
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;
|
|
|
|
|
|
|
|
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));
|
|
|
|
/* avoid loops */
|
|
|
|
if (strncmp(hostname, host, p - host) != 0)
|
|
|
|
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-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);
|
2001-06-22 01:52:37 +00:00
|
|
|
if (nhost != NULL) {
|
2016-01-23 00:28:18 +00:00
|
|
|
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
|
|
|
}
|