1 /* vi: set sw=4 ts=4: */
3 * whois - tiny client for the whois directory service
5 * Copyright (c) 2011 Pere Orga <gotrunks@gmail.com>
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 //config: bool "whois"
17 //config: whois is a client for the whois directory service
19 //applet:IF_WHOIS(APPLET(whois, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_WHOIS) += whois.o
23 //usage:#define whois_trivial_usage
24 //usage: "[-h SERVER] [-p PORT] NAME..."
25 //usage:#define whois_full_usage "\n\n"
26 //usage: "Query WHOIS info about NAME\n"
27 //usage: "\n -h,-p Server to query"
31 static char *query(const char *host, int port, const char *domain)
43 printf("[Querying %s:%d '%s%s']\n", host, port, pfx, domain);
44 fd = create_and_connect_stream_or_die(host, port);
46 fdprintf(fd, "%s%s\r\n", pfx, domain);
47 fp = xfdopen_for_read(fd);
49 while (fgets(linebuf, sizeof(linebuf), fp)) {
50 unsigned len = strcspn(linebuf, "\r\n");
51 linebuf[len++] = '\n';
53 buf = xrealloc(buf, bufpos + len + 1);
54 memcpy(buf + bufpos, linebuf, len);
57 if (!redir || !success) {
61 success = is_prefixed_with(linebuf, "domain:")
62 || is_prefixed_with(linebuf, "domain name:");
65 char *p = is_prefixed_with(linebuf, "whois server:");
67 p = is_prefixed_with(linebuf, "whois:");
69 redir = xstrdup(skip_whitespace(p));
73 fclose(fp); /* closes fd too */
74 if (!success && !pfx[0]) {
76 * Looking at jwhois.conf, some whois servers use
77 * "domain = DOMAIN", "DOMAIN ID <DOMAIN>"
78 * and "domain=DOMAIN_WITHOUT_LAST_COMPONENT"
79 * formats, but those are rare.
80 * (There are a few even more contrived ones.)
81 * We are trying only "domain DOMAIN", the typical one.
89 if (redir && strcmp(redir, host) == 0) {
90 /* Redirect to self does not count */
95 /* Output saved text */
96 printf("[%s]\n", host);
104 static void recursive_query(const char *host, int port, const char *domain)
106 char *free_me = NULL;
109 redir = query(host, port, domain);
112 printf("[Redirected to %s]\n", redir);
113 host = free_me = redir;
119 /* One of "big" whois implementations has these options:
122 * jwhois version 4.0, Copyright (C) 1999-2007 Free Software Foundation, Inc.
123 * -v, --verbose verbose debug output
124 * -c FILE, --config=FILE use FILE as configuration file
125 * -h HOST, --host=HOST explicitly query HOST
126 * -n, --no-redirect disable content redirection
127 * -s, --no-whoisservers disable whois-servers.net service support
128 * -a, --raw disable reformatting of the query
129 * -i, --display-redirections display all redirects instead of hiding them
130 * -p PORT, --port=PORT use port number PORT (in conjunction with HOST)
131 * -r, --rwhois force an rwhois query to be made
132 * --rwhois-display=DISPLAY sets the display option in rwhois queries
133 * --rwhois-limit=LIMIT sets the maximum number of matches to return
135 * Example of its output:
137 * [Querying whois.verisign-grs.com]
138 * [Redirected to whois.corporatedomains.com]
139 * [Querying whois.corporatedomains.com]
140 * [whois.corporatedomains.com]
141 * ...text of the reply...
143 * With -i, reply from each server is printed, after all redirects are done:
144 * [Querying whois.verisign-grs.com]
145 * [Redirected to whois.corporatedomains.com]
146 * [Querying whois.corporatedomains.com]
147 * [whois.verisign-grs.com]
148 * ...text of the reply...
149 * [whois.corporatedomains.com]
150 * ...text of the reply...
152 * With -a, no "DOMAIN" -> "domain DOMAIN" transformation is attempted.
154 * With -n, the first reply is shown, redirects are not followed:
155 * [Querying whois.verisign-grs.com]
156 * [whois.verisign-grs.com]
157 * ...text of the reply...
160 int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
161 int whois_main(int argc UNUSED_PARAM, char **argv)
164 const char *host = "whois.iana.org";
166 opt_complementary = "-1:p+";
167 getopt32(argv, "h:p:", &host, &port);
171 recursive_query(host, port, *argv);