2011-02-28 04:08:52 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* whois - tiny client for the whois directory service
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Pere Orga <gotrunks@gmail.com>
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
/* TODO
|
|
|
|
* Add ipv6 support
|
|
|
|
* Add proxy support
|
|
|
|
*/
|
|
|
|
|
|
|
|
//config:config WHOIS
|
|
|
|
//config: bool "whois"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: whois is a client for the whois directory service
|
|
|
|
|
|
|
|
//applet:IF_WHOIS(APPLET(whois, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_WHOIS) += whois.o
|
|
|
|
|
|
|
|
//usage:#define whois_trivial_usage
|
2016-07-06 20:46:27 +05:30
|
|
|
//usage: "[-i] [-h SERVER] [-p PORT] NAME..."
|
2011-02-28 04:08:52 +05:30
|
|
|
//usage:#define whois_full_usage "\n\n"
|
|
|
|
//usage: "Query WHOIS info about NAME\n"
|
2016-07-06 20:46:27 +05:30
|
|
|
//usage: "\n -i Show redirect results too"
|
2011-02-28 04:08:52 +05:30
|
|
|
//usage: "\n -h,-p Server to query"
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2016-07-06 20:46:27 +05:30
|
|
|
enum {
|
|
|
|
OPT_i = (1 << 0),
|
|
|
|
};
|
|
|
|
|
2016-07-06 19:15:41 +05:30
|
|
|
static char *query(const char *host, int port, const char *domain)
|
2011-02-28 04:08:52 +05:30
|
|
|
{
|
2016-07-06 19:15:41 +05:30
|
|
|
int fd;
|
2011-02-28 04:08:52 +05:30
|
|
|
FILE *fp;
|
2016-07-06 19:15:41 +05:30
|
|
|
bool success;
|
|
|
|
char *redir = NULL;
|
|
|
|
const char *pfx = "";
|
|
|
|
char linebuf[1024];
|
|
|
|
char *buf = NULL;
|
|
|
|
unsigned bufpos = 0;
|
2011-02-28 04:08:52 +05:30
|
|
|
|
2016-07-06 19:15:41 +05:30
|
|
|
again:
|
|
|
|
printf("[Querying %s:%d '%s%s']\n", host, port, pfx, domain);
|
|
|
|
fd = create_and_connect_stream_or_die(host, port);
|
|
|
|
success = 0;
|
|
|
|
fdprintf(fd, "%s%s\r\n", pfx, domain);
|
2011-02-28 04:08:52 +05:30
|
|
|
fp = xfdopen_for_read(fd);
|
|
|
|
|
2016-07-06 19:15:41 +05:30
|
|
|
while (fgets(linebuf, sizeof(linebuf), fp)) {
|
|
|
|
unsigned len = strcspn(linebuf, "\r\n");
|
|
|
|
linebuf[len++] = '\n';
|
|
|
|
|
|
|
|
buf = xrealloc(buf, bufpos + len + 1);
|
|
|
|
memcpy(buf + bufpos, linebuf, len);
|
|
|
|
bufpos += len;
|
2016-07-06 20:46:27 +05:30
|
|
|
buf[bufpos] = '\0';
|
2016-07-06 19:15:41 +05:30
|
|
|
|
|
|
|
if (!redir || !success) {
|
|
|
|
trim(linebuf);
|
|
|
|
str_tolower(linebuf);
|
|
|
|
if (!success) {
|
|
|
|
success = is_prefixed_with(linebuf, "domain:")
|
|
|
|
|| is_prefixed_with(linebuf, "domain name:");
|
|
|
|
}
|
|
|
|
else if (!redir) {
|
|
|
|
char *p = is_prefixed_with(linebuf, "whois server:");
|
|
|
|
if (!p)
|
|
|
|
p = is_prefixed_with(linebuf, "whois:");
|
|
|
|
if (p)
|
|
|
|
redir = xstrdup(skip_whitespace(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 04:08:52 +05:30
|
|
|
fclose(fp); /* closes fd too */
|
2016-07-06 19:15:41 +05:30
|
|
|
if (!success && !pfx[0]) {
|
|
|
|
/*
|
2016-07-06 20:46:27 +05:30
|
|
|
* Looking at /etc/jwhois.conf, some whois servers use
|
2016-07-06 19:15:41 +05:30
|
|
|
* "domain = DOMAIN", "DOMAIN ID <DOMAIN>"
|
|
|
|
* and "domain=DOMAIN_WITHOUT_LAST_COMPONENT"
|
|
|
|
* formats, but those are rare.
|
|
|
|
* (There are a few even more contrived ones.)
|
|
|
|
* We are trying only "domain DOMAIN", the typical one.
|
|
|
|
*/
|
|
|
|
pfx = "domain ";
|
|
|
|
bufpos = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success */
|
|
|
|
if (redir && strcmp(redir, host) == 0) {
|
|
|
|
/* Redirect to self does not count */
|
|
|
|
free(redir);
|
|
|
|
redir = NULL;
|
|
|
|
}
|
2016-07-06 20:46:27 +05:30
|
|
|
if (!redir || (option_mask32 & OPT_i)) {
|
2016-07-06 19:15:41 +05:30
|
|
|
/* Output saved text */
|
2016-07-06 20:46:27 +05:30
|
|
|
printf("[%s]\n%s", host, buf ? buf : "");
|
2016-07-06 19:15:41 +05:30
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
return redir;
|
2011-02-28 04:08:52 +05:30
|
|
|
}
|
|
|
|
|
2016-07-06 19:15:41 +05:30
|
|
|
static void recursive_query(const char *host, int port, const char *domain)
|
|
|
|
{
|
|
|
|
char *free_me = NULL;
|
|
|
|
char *redir;
|
|
|
|
again:
|
|
|
|
redir = query(host, port, domain);
|
|
|
|
free(free_me);
|
|
|
|
if (redir) {
|
|
|
|
printf("[Redirected to %s]\n", redir);
|
|
|
|
host = free_me = redir;
|
|
|
|
port = 43;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One of "big" whois implementations has these options:
|
|
|
|
*
|
|
|
|
* $ whois --help
|
|
|
|
* jwhois version 4.0, Copyright (C) 1999-2007 Free Software Foundation, Inc.
|
|
|
|
* -v, --verbose verbose debug output
|
|
|
|
* -c FILE, --config=FILE use FILE as configuration file
|
|
|
|
* -h HOST, --host=HOST explicitly query HOST
|
|
|
|
* -n, --no-redirect disable content redirection
|
|
|
|
* -s, --no-whoisservers disable whois-servers.net service support
|
|
|
|
* -a, --raw disable reformatting of the query
|
|
|
|
* -i, --display-redirections display all redirects instead of hiding them
|
|
|
|
* -p PORT, --port=PORT use port number PORT (in conjunction with HOST)
|
|
|
|
* -r, --rwhois force an rwhois query to be made
|
|
|
|
* --rwhois-display=DISPLAY sets the display option in rwhois queries
|
|
|
|
* --rwhois-limit=LIMIT sets the maximum number of matches to return
|
|
|
|
*
|
|
|
|
* Example of its output:
|
|
|
|
* $ whois cnn.com
|
|
|
|
* [Querying whois.verisign-grs.com]
|
|
|
|
* [Redirected to whois.corporatedomains.com]
|
|
|
|
* [Querying whois.corporatedomains.com]
|
|
|
|
* [whois.corporatedomains.com]
|
|
|
|
* ...text of the reply...
|
|
|
|
*
|
|
|
|
* With -i, reply from each server is printed, after all redirects are done:
|
|
|
|
* [Querying whois.verisign-grs.com]
|
|
|
|
* [Redirected to whois.corporatedomains.com]
|
|
|
|
* [Querying whois.corporatedomains.com]
|
|
|
|
* [whois.verisign-grs.com]
|
|
|
|
* ...text of the reply...
|
|
|
|
* [whois.corporatedomains.com]
|
|
|
|
* ...text of the reply...
|
|
|
|
*
|
|
|
|
* With -a, no "DOMAIN" -> "domain DOMAIN" transformation is attempted.
|
|
|
|
|
|
|
|
* With -n, the first reply is shown, redirects are not followed:
|
|
|
|
* [Querying whois.verisign-grs.com]
|
|
|
|
* [whois.verisign-grs.com]
|
|
|
|
* ...text of the reply...
|
|
|
|
*/
|
|
|
|
|
2011-02-28 04:08:52 +05:30
|
|
|
int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int whois_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int port = 43;
|
2016-07-06 19:15:41 +05:30
|
|
|
const char *host = "whois.iana.org";
|
2011-02-28 04:08:52 +05:30
|
|
|
|
2016-07-07 01:28:02 +05:30
|
|
|
opt_complementary = "-1";
|
|
|
|
getopt32(argv, "ih:p:+", &host, &port);
|
2011-02-28 04:08:52 +05:30
|
|
|
argv += optind;
|
2016-07-06 19:15:41 +05:30
|
|
|
|
2011-02-28 04:08:52 +05:30
|
|
|
do {
|
2016-07-06 19:15:41 +05:30
|
|
|
recursive_query(host, port, *argv);
|
2011-02-28 04:08:52 +05:30
|
|
|
}
|
|
|
|
while (*++argv);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|