Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 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 | |
| 13 | //config:config WHOIS |
| 14 | //config: bool "whois" |
| 15 | //config: default y |
| 16 | //config: help |
| 17 | //config: whois is a client for the whois directory service |
| 18 | |
| 19 | //applet:IF_WHOIS(APPLET(whois, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_WHOIS) += whois.o |
| 22 | |
| 23 | //usage:#define whois_trivial_usage |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 24 | //usage: "[-i] [-h SERVER] [-p PORT] NAME..." |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 25 | //usage:#define whois_full_usage "\n\n" |
| 26 | //usage: "Query WHOIS info about NAME\n" |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 27 | //usage: "\n -i Show redirect results too" |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 28 | //usage: "\n -h,-p Server to query" |
| 29 | |
| 30 | #include "libbb.h" |
| 31 | |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 32 | enum { |
| 33 | OPT_i = (1 << 0), |
| 34 | }; |
| 35 | |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 36 | static char *query(const char *host, int port, const char *domain) |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 37 | { |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 38 | int fd; |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 39 | FILE *fp; |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 40 | bool success; |
| 41 | char *redir = NULL; |
| 42 | const char *pfx = ""; |
| 43 | char linebuf[1024]; |
| 44 | char *buf = NULL; |
| 45 | unsigned bufpos = 0; |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 46 | |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 47 | again: |
| 48 | printf("[Querying %s:%d '%s%s']\n", host, port, pfx, domain); |
| 49 | fd = create_and_connect_stream_or_die(host, port); |
| 50 | success = 0; |
| 51 | fdprintf(fd, "%s%s\r\n", pfx, domain); |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 52 | fp = xfdopen_for_read(fd); |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 53 | |
| 54 | while (fgets(linebuf, sizeof(linebuf), fp)) { |
| 55 | unsigned len = strcspn(linebuf, "\r\n"); |
| 56 | linebuf[len++] = '\n'; |
| 57 | |
| 58 | buf = xrealloc(buf, bufpos + len + 1); |
| 59 | memcpy(buf + bufpos, linebuf, len); |
| 60 | bufpos += len; |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 61 | buf[bufpos] = '\0'; |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 62 | |
| 63 | if (!redir || !success) { |
| 64 | trim(linebuf); |
| 65 | str_tolower(linebuf); |
| 66 | if (!success) { |
| 67 | success = is_prefixed_with(linebuf, "domain:") |
| 68 | || is_prefixed_with(linebuf, "domain name:"); |
| 69 | } |
| 70 | else if (!redir) { |
| 71 | char *p = is_prefixed_with(linebuf, "whois server:"); |
| 72 | if (!p) |
| 73 | p = is_prefixed_with(linebuf, "whois:"); |
| 74 | if (p) |
| 75 | redir = xstrdup(skip_whitespace(p)); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | fclose(fp); /* closes fd too */ |
| 80 | if (!success && !pfx[0]) { |
| 81 | /* |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 82 | * Looking at /etc/jwhois.conf, some whois servers use |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 83 | * "domain = DOMAIN", "DOMAIN ID <DOMAIN>" |
| 84 | * and "domain=DOMAIN_WITHOUT_LAST_COMPONENT" |
| 85 | * formats, but those are rare. |
| 86 | * (There are a few even more contrived ones.) |
| 87 | * We are trying only "domain DOMAIN", the typical one. |
| 88 | */ |
| 89 | pfx = "domain "; |
| 90 | bufpos = 0; |
| 91 | goto again; |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 94 | /* Success */ |
| 95 | if (redir && strcmp(redir, host) == 0) { |
| 96 | /* Redirect to self does not count */ |
| 97 | free(redir); |
| 98 | redir = NULL; |
| 99 | } |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 100 | if (!redir || (option_mask32 & OPT_i)) { |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 101 | /* Output saved text */ |
Denys Vlasenko | 0844b5a | 2016-07-06 17:16:27 +0200 | [diff] [blame] | 102 | printf("[%s]\n%s", host, buf ? buf : ""); |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 103 | } |
| 104 | free(buf); |
| 105 | return redir; |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 108 | static void recursive_query(const char *host, int port, const char *domain) |
| 109 | { |
| 110 | char *free_me = NULL; |
| 111 | char *redir; |
| 112 | again: |
| 113 | redir = query(host, port, domain); |
| 114 | free(free_me); |
| 115 | if (redir) { |
| 116 | printf("[Redirected to %s]\n", redir); |
| 117 | host = free_me = redir; |
| 118 | port = 43; |
| 119 | goto again; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* One of "big" whois implementations has these options: |
| 124 | * |
| 125 | * $ whois --help |
| 126 | * jwhois version 4.0, Copyright (C) 1999-2007 Free Software Foundation, Inc. |
| 127 | * -v, --verbose verbose debug output |
| 128 | * -c FILE, --config=FILE use FILE as configuration file |
| 129 | * -h HOST, --host=HOST explicitly query HOST |
| 130 | * -n, --no-redirect disable content redirection |
| 131 | * -s, --no-whoisservers disable whois-servers.net service support |
| 132 | * -a, --raw disable reformatting of the query |
| 133 | * -i, --display-redirections display all redirects instead of hiding them |
| 134 | * -p PORT, --port=PORT use port number PORT (in conjunction with HOST) |
| 135 | * -r, --rwhois force an rwhois query to be made |
| 136 | * --rwhois-display=DISPLAY sets the display option in rwhois queries |
| 137 | * --rwhois-limit=LIMIT sets the maximum number of matches to return |
| 138 | * |
| 139 | * Example of its output: |
| 140 | * $ whois cnn.com |
| 141 | * [Querying whois.verisign-grs.com] |
| 142 | * [Redirected to whois.corporatedomains.com] |
| 143 | * [Querying whois.corporatedomains.com] |
| 144 | * [whois.corporatedomains.com] |
| 145 | * ...text of the reply... |
| 146 | * |
| 147 | * With -i, reply from each server is printed, after all redirects are done: |
| 148 | * [Querying whois.verisign-grs.com] |
| 149 | * [Redirected to whois.corporatedomains.com] |
| 150 | * [Querying whois.corporatedomains.com] |
| 151 | * [whois.verisign-grs.com] |
| 152 | * ...text of the reply... |
| 153 | * [whois.corporatedomains.com] |
| 154 | * ...text of the reply... |
| 155 | * |
| 156 | * With -a, no "DOMAIN" -> "domain DOMAIN" transformation is attempted. |
| 157 | |
| 158 | * With -n, the first reply is shown, redirects are not followed: |
| 159 | * [Querying whois.verisign-grs.com] |
| 160 | * [whois.verisign-grs.com] |
| 161 | * ...text of the reply... |
| 162 | */ |
| 163 | |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 164 | int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 165 | int whois_main(int argc UNUSED_PARAM, char **argv) |
| 166 | { |
| 167 | int port = 43; |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 168 | const char *host = "whois.iana.org"; |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 169 | |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 170 | opt_complementary = "-1"; |
| 171 | getopt32(argv, "ih:p:+", &host, &port); |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 172 | argv += optind; |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 173 | |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 174 | do { |
Denys Vlasenko | 1035c92 | 2016-07-06 15:45:41 +0200 | [diff] [blame] | 175 | recursive_query(host, port, *argv); |
Pere Orga | 251962f | 2011-02-27 23:38:52 +0100 | [diff] [blame] | 176 | } |
| 177 | while (*++argv); |
| 178 | |
| 179 | return EXIT_SUCCESS; |
| 180 | } |