whois.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * whois - tiny client for the whois directory service
  4. *
  5. * Copyright (c) 2011 Pere Orga <gotrunks@gmail.com>
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. /* TODO
  9. * Add ipv6 support
  10. * Add proxy support
  11. */
  12. //config:config WHOIS
  13. //config: bool "whois (6.3 kb)"
  14. //config: default y
  15. //config: help
  16. //config: whois is a client for the whois directory service
  17. //applet:IF_WHOIS(APPLET(whois, BB_DIR_USR_BIN, BB_SUID_DROP))
  18. //kbuild:lib-$(CONFIG_WHOIS) += whois.o
  19. //usage:#define whois_trivial_usage
  20. //usage: "[-i] [-h SERVER] [-p PORT] NAME..."
  21. //usage:#define whois_full_usage "\n\n"
  22. //usage: "Query WHOIS info about NAME\n"
  23. //usage: "\n -i Show redirect results too"
  24. //usage: "\n -h,-p Server to query"
  25. #include "libbb.h"
  26. enum {
  27. OPT_i = (1 << 0),
  28. };
  29. static char *query(const char *host, int port, const char *domain)
  30. {
  31. int fd;
  32. FILE *fp;
  33. bool success;
  34. char *redir = NULL;
  35. const char *pfx = "";
  36. /* some .io domains reported to have very long strings in whois
  37. * responses, 1k was not enough:
  38. */
  39. char linebuf[2 * 1024];
  40. char *buf = NULL;
  41. unsigned bufpos = 0;
  42. again:
  43. printf("[Querying %s:%d '%s%s']\n", host, port, pfx, domain);
  44. fd = create_and_connect_stream_or_die(host, port);
  45. fdprintf(fd, "%s%s\r\n", pfx, domain);
  46. fp = xfdopen_for_read(fd);
  47. success = 0;
  48. while (fgets(linebuf, sizeof(linebuf)-1, fp)) {
  49. unsigned len;
  50. len = strcspn(linebuf, "\r\n");
  51. linebuf[len++] = '\n';
  52. linebuf[len] = '\0';
  53. buf = xrealloc(buf, bufpos + len + 1);
  54. memcpy(buf + bufpos, linebuf, len);
  55. bufpos += len;
  56. buf[bufpos] = '\0';
  57. if (!redir || !success) {
  58. trim(linebuf);
  59. str_tolower(linebuf);
  60. if (!success) {
  61. success = is_prefixed_with(linebuf, "domain:")
  62. || is_prefixed_with(linebuf, "domain name:");
  63. }
  64. else if (!redir) {
  65. char *p = is_prefixed_with(linebuf, "whois server:");
  66. if (!p)
  67. p = is_prefixed_with(linebuf, "whois:");
  68. if (p)
  69. redir = xstrdup(skip_whitespace(p));
  70. }
  71. }
  72. }
  73. fclose(fp); /* closes fd too */
  74. if (!success && !pfx[0]) {
  75. /*
  76. * Looking at /etc/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.
  82. */
  83. pfx = "domain ";
  84. bufpos = 0;
  85. goto again;
  86. }
  87. /* Success */
  88. if (redir && strcmp(redir, host) == 0) {
  89. /* Redirect to self does not count */
  90. free(redir);
  91. redir = NULL;
  92. }
  93. if (!redir || (option_mask32 & OPT_i)) {
  94. /* Output saved text */
  95. printf("[%s]\n%s", host, buf ? buf : "");
  96. }
  97. free(buf);
  98. return redir;
  99. }
  100. static void recursive_query(const char *host, int port, const char *domain)
  101. {
  102. char *free_me = NULL;
  103. char *redir;
  104. again:
  105. redir = query(host, port, domain);
  106. free(free_me);
  107. if (redir) {
  108. printf("[Redirected to %s]\n", redir);
  109. host = free_me = redir;
  110. port = 43;
  111. goto again;
  112. }
  113. }
  114. /* One of "big" whois implementations has these options:
  115. *
  116. * $ whois --help
  117. * jwhois version 4.0, Copyright (C) 1999-2007 Free Software Foundation, Inc.
  118. * -v, --verbose verbose debug output
  119. * -c FILE, --config=FILE use FILE as configuration file
  120. * -h HOST, --host=HOST explicitly query HOST
  121. * -n, --no-redirect disable content redirection
  122. * -s, --no-whoisservers disable whois-servers.net service support
  123. * -a, --raw disable reformatting of the query
  124. * -i, --display-redirections display all redirects instead of hiding them
  125. * -p PORT, --port=PORT use port number PORT (in conjunction with HOST)
  126. * -r, --rwhois force an rwhois query to be made
  127. * --rwhois-display=DISPLAY sets the display option in rwhois queries
  128. * --rwhois-limit=LIMIT sets the maximum number of matches to return
  129. *
  130. * Example of its output:
  131. * $ whois cnn.com
  132. * [Querying whois.verisign-grs.com]
  133. * [Redirected to whois.corporatedomains.com]
  134. * [Querying whois.corporatedomains.com]
  135. * [whois.corporatedomains.com]
  136. * ...text of the reply...
  137. *
  138. * With -i, reply from each server is printed, after all redirects are done:
  139. * [Querying whois.verisign-grs.com]
  140. * [Redirected to whois.corporatedomains.com]
  141. * [Querying whois.corporatedomains.com]
  142. * [whois.verisign-grs.com]
  143. * ...text of the reply...
  144. * [whois.corporatedomains.com]
  145. * ...text of the reply...
  146. *
  147. * With -a, no "DOMAIN" -> "domain DOMAIN" transformation is attempted.
  148. * With -n, the first reply is shown, redirects are not followed:
  149. * [Querying whois.verisign-grs.com]
  150. * [whois.verisign-grs.com]
  151. * ...text of the reply...
  152. */
  153. int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  154. int whois_main(int argc UNUSED_PARAM, char **argv)
  155. {
  156. int port = 43;
  157. const char *host = "whois.iana.org";
  158. getopt32(argv, "^" "ih:p:+" "\0" "-1", &host, &port);
  159. argv += optind;
  160. do {
  161. recursive_query(host, port, *argv);
  162. }
  163. while (*++argv);
  164. return EXIT_SUCCESS;
  165. }