whois.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.6 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. char linebuf[1024];
  37. char *buf = NULL;
  38. unsigned bufpos = 0;
  39. again:
  40. printf("[Querying %s:%d '%s%s']\n", host, port, pfx, domain);
  41. fd = create_and_connect_stream_or_die(host, port);
  42. success = 0;
  43. fdprintf(fd, "%s%s\r\n", pfx, domain);
  44. fp = xfdopen_for_read(fd);
  45. while (fgets(linebuf, sizeof(linebuf), fp)) {
  46. unsigned len = strcspn(linebuf, "\r\n");
  47. linebuf[len++] = '\n';
  48. buf = xrealloc(buf, bufpos + len + 1);
  49. memcpy(buf + bufpos, linebuf, len);
  50. bufpos += len;
  51. buf[bufpos] = '\0';
  52. if (!redir || !success) {
  53. trim(linebuf);
  54. str_tolower(linebuf);
  55. if (!success) {
  56. success = is_prefixed_with(linebuf, "domain:")
  57. || is_prefixed_with(linebuf, "domain name:");
  58. }
  59. else if (!redir) {
  60. char *p = is_prefixed_with(linebuf, "whois server:");
  61. if (!p)
  62. p = is_prefixed_with(linebuf, "whois:");
  63. if (p)
  64. redir = xstrdup(skip_whitespace(p));
  65. }
  66. }
  67. }
  68. fclose(fp); /* closes fd too */
  69. if (!success && !pfx[0]) {
  70. /*
  71. * Looking at /etc/jwhois.conf, some whois servers use
  72. * "domain = DOMAIN", "DOMAIN ID <DOMAIN>"
  73. * and "domain=DOMAIN_WITHOUT_LAST_COMPONENT"
  74. * formats, but those are rare.
  75. * (There are a few even more contrived ones.)
  76. * We are trying only "domain DOMAIN", the typical one.
  77. */
  78. pfx = "domain ";
  79. bufpos = 0;
  80. goto again;
  81. }
  82. /* Success */
  83. if (redir && strcmp(redir, host) == 0) {
  84. /* Redirect to self does not count */
  85. free(redir);
  86. redir = NULL;
  87. }
  88. if (!redir || (option_mask32 & OPT_i)) {
  89. /* Output saved text */
  90. printf("[%s]\n%s", host, buf ? buf : "");
  91. }
  92. free(buf);
  93. return redir;
  94. }
  95. static void recursive_query(const char *host, int port, const char *domain)
  96. {
  97. char *free_me = NULL;
  98. char *redir;
  99. again:
  100. redir = query(host, port, domain);
  101. free(free_me);
  102. if (redir) {
  103. printf("[Redirected to %s]\n", redir);
  104. host = free_me = redir;
  105. port = 43;
  106. goto again;
  107. }
  108. }
  109. /* One of "big" whois implementations has these options:
  110. *
  111. * $ whois --help
  112. * jwhois version 4.0, Copyright (C) 1999-2007 Free Software Foundation, Inc.
  113. * -v, --verbose verbose debug output
  114. * -c FILE, --config=FILE use FILE as configuration file
  115. * -h HOST, --host=HOST explicitly query HOST
  116. * -n, --no-redirect disable content redirection
  117. * -s, --no-whoisservers disable whois-servers.net service support
  118. * -a, --raw disable reformatting of the query
  119. * -i, --display-redirections display all redirects instead of hiding them
  120. * -p PORT, --port=PORT use port number PORT (in conjunction with HOST)
  121. * -r, --rwhois force an rwhois query to be made
  122. * --rwhois-display=DISPLAY sets the display option in rwhois queries
  123. * --rwhois-limit=LIMIT sets the maximum number of matches to return
  124. *
  125. * Example of its output:
  126. * $ whois cnn.com
  127. * [Querying whois.verisign-grs.com]
  128. * [Redirected to whois.corporatedomains.com]
  129. * [Querying whois.corporatedomains.com]
  130. * [whois.corporatedomains.com]
  131. * ...text of the reply...
  132. *
  133. * With -i, reply from each server is printed, after all redirects are done:
  134. * [Querying whois.verisign-grs.com]
  135. * [Redirected to whois.corporatedomains.com]
  136. * [Querying whois.corporatedomains.com]
  137. * [whois.verisign-grs.com]
  138. * ...text of the reply...
  139. * [whois.corporatedomains.com]
  140. * ...text of the reply...
  141. *
  142. * With -a, no "DOMAIN" -> "domain DOMAIN" transformation is attempted.
  143. * With -n, the first reply is shown, redirects are not followed:
  144. * [Querying whois.verisign-grs.com]
  145. * [whois.verisign-grs.com]
  146. * ...text of the reply...
  147. */
  148. int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  149. int whois_main(int argc UNUSED_PARAM, char **argv)
  150. {
  151. int port = 43;
  152. const char *host = "whois.iana.org";
  153. getopt32(argv, "^" "ih:p:+" "\0" "-1", &host, &port);
  154. argv += optind;
  155. do {
  156. recursive_query(host, port, *argv);
  157. }
  158. while (*++argv);
  159. return EXIT_SUCCESS;
  160. }