2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
59 #define ABUSEHOST "whois.abuse.net"
60 #define NICHOST "whois.crsnic.net"
61 #define INICHOST "whois.networksolutions.com"
62 #define GNICHOST "whois.nic.gov"
63 #define ANICHOST "whois.arin.net"
64 #define LNICHOST "whois.lacnic.net"
65 #define KNICHOST "whois.krnic.net"
66 #define RNICHOST "whois.ripe.net"
67 #define PNICHOST "whois.apnic.net"
68 #define MNICHOST "whois.ra.net"
69 #define QNICHOST_TAIL ".whois-servers.net"
70 #define BNICHOST "whois.registro.br"
71 #define NORIDHOST "whois.norid.no"
72 #define IANAHOST "whois.iana.org"
73 #define GERMNICHOST "de.whois-servers.net"
74 #define FNICHOST "whois.afrinic.net"
75 #define DEFAULT_PORT "whois"
76 #define WHOIS_SERVER_ID "Whois Server: "
77 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
79 #define WHOIS_RECURSE 0x01
80 #define WHOIS_QUICK 0x02
82 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
84 static const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
86 static const char *port = DEFAULT_PORT;
88 static char *choose_server(char *);
89 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
90 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
91 static void usage(void);
92 static void whois(const char *, const char *, int);
95 main(int argc, char *argv[])
97 const char *country, *host;
99 int ch, flags, use_qnichost;
105 country = host = qnichost = NULL;
106 flags = use_qnichost = 0;
107 while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:QrR6")) != -1) {
149 flags |= WHOIS_QUICK;
155 warnx("-R is deprecated; use '-c ru' instead");
158 /* Remove in FreeBSD 10 */
161 "-6 is deprecated; use -[aAflr] instead");
172 if (!argc || (country != NULL && host != NULL))
176 * If no host or country is specified determine the top level domain
177 * from the query. If the TLD is a number, query ARIN. Otherwise, use
178 * TLD.whois-server.net. If the domain does not contain '.', fall
181 if (host == NULL && country == NULL) {
182 if ((host = getenv("RA_SERVER")) == NULL) {
185 if (!(flags & WHOIS_QUICK))
186 flags |= WHOIS_RECURSE;
190 if (country != NULL) {
191 s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
192 whois(*argv, qnichost, flags);
193 } else if (use_qnichost)
194 if ((qnichost = choose_server(*argv)) != NULL)
195 whois(*argv, qnichost, flags);
196 if (qnichost == NULL)
197 whois(*argv, host, flags);
206 * This function will remove any trailing periods from domain, after which it
207 * returns a pointer to newly allocated memory containing the whois server to
208 * be queried, or a NULL if the correct server couldn't be determined. The
209 * caller must remember to free(3) the allocated memory.
212 choose_server(char *domain)
216 if (strchr(domain, ':')) {
217 s_asprintf(&retval, "%s", ANICHOST);
220 for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
223 errx(EX_USAGE, "can't search for a null string");
224 if (strlen(domain) > sizeof("-NORID")-1 &&
225 strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
227 s_asprintf(&retval, "%s", NORIDHOST);
230 while (pos > domain && *pos != '.')
234 if (isdigit((unsigned char)*++pos))
235 s_asprintf(&retval, "%s", ANICHOST);
237 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
241 static struct addrinfo *
242 gethostinfo(char const *host, int exit_on_error)
244 struct addrinfo hints, *res;
247 memset(&hints, 0, sizeof(hints));
249 hints.ai_family = AF_UNSPEC;
250 hints.ai_socktype = SOCK_STREAM;
251 error = getaddrinfo(host, port, &hints, &res);
253 warnx("%s: %s", host, gai_strerror(error));
262 * Wrapper for asprintf(3) that exits on error.
265 s_asprintf(char **ret, const char *format, ...)
269 va_start(ap, format);
270 if (vasprintf(ret, format, ap) == -1) {
272 err(EX_OSERR, "vasprintf()");
278 whois(const char *query, const char *hostname, int flags)
281 struct addrinfo *hostres, *res;
282 char *buf, *host, *nhost, *p;
287 hostres = gethostinfo(hostname, 1);
288 for (res = hostres; res; res = res->ai_next) {
289 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
292 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
296 freeaddrinfo(hostres);
298 err(EX_OSERR, "connect()");
300 fp = fdopen(s, "r+");
302 err(EX_OSERR, "fdopen()");
303 if (strcmp(hostname, GERMNICHOST) == 0) {
304 fprintf(fp, "-T dn,ace -C US-ASCII %s\r\n", query);
305 } else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
306 fprintf(fp, "--show-handles %s\r\n", query);
308 fprintf(fp, "%s\r\n", query);
312 while ((buf = fgetln(fp, &len)) != NULL) {
313 while (len > 0 && isspace((unsigned char)buf[len - 1]))
315 printf("%.*s\n", (int)len, buf);
317 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
318 host = strnstr(buf, WHOIS_SERVER_ID, len);
320 host += sizeof(WHOIS_SERVER_ID) - 1;
321 for (p = host; p < buf + len; p++) {
327 s_asprintf(&nhost, "%.*s",
328 (int)(buf + len - host), host);
330 strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
331 host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
332 for (p = host; p < buf + len; p++) {
338 s_asprintf(&nhost, "%.*s",
339 (int)(buf + len - host), host);
340 } else if (strcmp(hostname, ANICHOST) == 0) {
341 for (c = 0; c <= len; c++)
342 buf[c] = tolower((unsigned char)buf[c]);
343 for (i = 0; ip_whois[i] != NULL; i++) {
344 if (strnstr(buf, ip_whois[i], len) !=
346 s_asprintf(&nhost, "%s",
356 whois(query, nhost, 0);
365 "usage: whois [-aAbfgiIklmQrR6] [-c country-code | -h hostname] "
366 "[-p port] name ...\n");