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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
63 #define ABUSEHOST "whois.abuse.net"
64 #define NICHOST "whois.crsnic.net"
65 #define INICHOST "whois.networksolutions.com"
66 #define GNICHOST "whois.nic.gov"
67 #define ANICHOST "whois.arin.net"
68 #define LNICHOST "whois.lacnic.net"
69 #define KNICHOST "whois.krnic.net"
70 #define RNICHOST "whois.ripe.net"
71 #define PNICHOST "whois.apnic.net"
72 #define MNICHOST "whois.ra.net"
73 #define QNICHOST_TAIL ".whois-servers.net"
74 #define BNICHOST "whois.registro.br"
75 #define NORIDHOST "whois.norid.no"
76 #define IANAHOST "whois.iana.org"
77 #define GERMNICHOST "de.whois-servers.net"
78 #define FNICHOST "whois.afrinic.net"
79 #define DEFAULT_PORT "whois"
80 #define WHOIS_SERVER_ID "Whois Server: "
81 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
83 #define WHOIS_RECURSE 0x01
84 #define WHOIS_QUICK 0x02
86 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
88 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
90 const char *port = DEFAULT_PORT;
92 static char *choose_server(char *);
93 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
94 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
95 static void usage(void);
96 static void whois(const char *, const char *, int);
99 main(int argc, char *argv[])
101 const char *country, *host;
103 int ch, flags, use_qnichost;
109 country = host = qnichost = NULL;
110 flags = use_qnichost = 0;
111 while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:QrR6")) != -1) {
153 flags |= WHOIS_QUICK;
159 warnx("-R is deprecated; use '-c ru' instead");
162 /* Remove in FreeBSD 10 */
165 "-6 is deprecated; use -[aAflr] instead");
176 if (!argc || (country != NULL && host != NULL))
180 * If no host or country is specified determine the top level domain
181 * from the query. If the TLD is a number, query ARIN. Otherwise, use
182 * TLD.whois-server.net. If the domain does not contain '.', fall
185 if (host == NULL && country == NULL) {
188 if (!(flags & WHOIS_QUICK))
189 flags |= WHOIS_RECURSE;
192 if (country != NULL) {
193 s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
194 whois(*argv, qnichost, flags);
195 } else if (use_qnichost)
196 if ((qnichost = choose_server(*argv)) != NULL)
197 whois(*argv, qnichost, flags);
198 if (qnichost == NULL)
199 whois(*argv, host, flags);
208 * This function will remove any trailing periods from domain, after which it
209 * returns a pointer to newly allocated memory containing the whois server to
210 * be queried, or a NULL if the correct server couldn't be determined. The
211 * caller must remember to free(3) the allocated memory.
214 choose_server(char *domain)
218 if (strchr(domain, ':')) {
219 s_asprintf(&retval, "%s", ANICHOST);
222 for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
225 errx(EX_USAGE, "can't search for a null string");
226 if (strlen(domain) > sizeof("-NORID")-1 &&
227 strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
229 s_asprintf(&retval, "%s", NORIDHOST);
232 while (pos > domain && *pos != '.')
236 if (isdigit((unsigned char)*++pos))
237 s_asprintf(&retval, "%s", ANICHOST);
239 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
243 static struct addrinfo *
244 gethostinfo(char const *host, int exit_on_error)
246 struct addrinfo hints, *res;
249 memset(&hints, 0, sizeof(hints));
251 hints.ai_family = AF_UNSPEC;
252 hints.ai_socktype = SOCK_STREAM;
253 error = getaddrinfo(host, port, &hints, &res);
255 warnx("%s: %s", host, gai_strerror(error));
264 * Wrapper for asprintf(3) that exits on error.
267 s_asprintf(char **ret, const char *format, ...)
271 va_start(ap, format);
272 if (vasprintf(ret, format, ap) == -1) {
274 err(EX_OSERR, "vasprintf()");
280 whois(const char *query, const char *hostname, int flags)
283 struct addrinfo *hostres, *res;
284 char *buf, *host, *nhost, *p;
289 hostres = gethostinfo(hostname, 1);
290 for (res = hostres; res; res = res->ai_next) {
291 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
294 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
298 freeaddrinfo(hostres);
300 err(EX_OSERR, "connect()");
302 sfi = fdopen(s, "r");
303 sfo = fdopen(s, "w");
304 if (sfi == NULL || sfo == NULL)
305 err(EX_OSERR, "fdopen()");
306 if (strcmp(hostname, GERMNICHOST) == 0) {
307 fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query);
308 } else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
309 fprintf(sfo, "--show-handles %s\r\n", query);
311 fprintf(sfo, "%s\r\n", query);
315 while ((buf = fgetln(sfi, &len)) != NULL) {
316 while (len > 0 && isspace((unsigned char)buf[len - 1]))
318 printf("%.*s\n", (int)len, buf);
320 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
321 host = strnstr(buf, WHOIS_SERVER_ID, len);
323 host += sizeof(WHOIS_SERVER_ID) - 1;
324 for (p = host; p < buf + len; p++) {
330 s_asprintf(&nhost, "%.*s",
331 (int)(buf + len - host), host);
333 strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
334 host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
335 for (p = host; p < buf + len; p++) {
341 s_asprintf(&nhost, "%.*s",
342 (int)(buf + len - host), host);
343 } else if (strcmp(hostname, ANICHOST) == 0) {
344 for (c = 0; c <= len; c++)
345 buf[c] = tolower((unsigned char)buf[c]);
346 for (i = 0; ip_whois[i] != NULL; i++) {
347 if (strnstr(buf, ip_whois[i], len) !=
349 s_asprintf(&nhost, "%s",
358 whois(query, nhost, 0);
367 "usage: whois [-aAbfgiIklmQrR6] [-c country-code | -h hostname] "
368 "[-p port] name ...\n");