xref: /freebsd/usr.bin/whois/whois.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35 
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/poll.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <netdb.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 
62 #define	ABUSEHOST	"whois.abuse.net"
63 #define	ANICHOST	"whois.arin.net"
64 #define	DENICHOST	"whois.denic.de"
65 #define	DKNICHOST	"whois.dk-hostmaster.dk"
66 #define	FNICHOST	"whois.afrinic.net"
67 #define	GNICHOST	"whois.nic.gov"
68 #define	IANAHOST	"whois.iana.org"
69 #define	INICHOST	"whois.internic.net"
70 #define	KNICHOST	"whois.krnic.net"
71 #define	LNICHOST	"whois.lacnic.net"
72 #define	MNICHOST	"whois.ra.net"
73 #define	PDBHOST		"whois.peeringdb.com"
74 #define	PNICHOST	"whois.apnic.net"
75 #define	QNICHOST_TAIL	".whois-servers.net"
76 #define	RNICHOST	"whois.ripe.net"
77 #define	VNICHOST	"whois.verisign-grs.com"
78 
79 #define	DEFAULT_PORT	"whois"
80 
81 #define WHOIS_RECURSE	0x01
82 #define WHOIS_QUICK	0x02
83 #define WHOIS_SPAM_ME	0x04
84 
85 #define CHOPSPAM	">>> Last update of WHOIS database:"
86 
87 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
88 
89 #define SCAN(p, end, check)					\
90 	while ((p) < (end))					\
91 		if (check) ++(p);				\
92 		else break
93 
94 static struct {
95 	const char *suffix, *server;
96 } whoiswhere[] = {
97 	/* Various handles */
98 	{ "-ARIN", ANICHOST },
99 	{ "-NICAT", "at" QNICHOST_TAIL },
100 	{ "-NORID", "no" QNICHOST_TAIL },
101 	{ "-RIPE", RNICHOST },
102 	/* Nominet's whois server doesn't return referrals to JANET */
103 	{ ".ac.uk", "ac.uk" QNICHOST_TAIL },
104 	{ ".gov.uk", "ac.uk" QNICHOST_TAIL },
105 	{ "", IANAHOST }, /* default */
106 	{ NULL, NULL } /* safety belt */
107 };
108 
109 #define WHOIS_REFERRAL(s) { s, sizeof(s) - 1 }
110 static struct {
111 	const char *prefix;
112 	size_t len;
113 } whois_referral[] = {
114 	WHOIS_REFERRAL("whois:"), /* IANA */
115 	WHOIS_REFERRAL("Whois Server:"),
116 	WHOIS_REFERRAL("Registrar WHOIS Server:"), /* corporatedomains.com */
117 	WHOIS_REFERRAL("ReferralServer:  whois://"), /* ARIN */
118 	WHOIS_REFERRAL("descr:          region. Please query"), /* AfriNIC */
119 	{ NULL, 0 }
120 };
121 
122 static const char *actually_arin[] = {
123 	"netname:        ERX-NETBLOCK\n", /* APNIC */
124 	"netname:        NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK\n",
125 	NULL
126 };
127 
128 static const char *port = DEFAULT_PORT;
129 
130 static const char *choose_server(char *);
131 static struct addrinfo *gethostinfo(char const *host, int exitnoname);
132 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
133 static void usage(void);
134 static void whois(const char *, const char *, int);
135 
136 int
137 main(int argc, char *argv[])
138 {
139 	const char *country, *host;
140 	int ch, flags;
141 
142 #ifdef	SOCKS
143 	SOCKSinit(argv[0]);
144 #endif
145 
146 	country = host = NULL;
147 	flags = 0;
148 	while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:PQrRS")) != -1) {
149 		switch (ch) {
150 		case 'a':
151 			host = ANICHOST;
152 			break;
153 		case 'A':
154 			host = PNICHOST;
155 			break;
156 		case 'b':
157 			host = ABUSEHOST;
158 			break;
159 		case 'c':
160 			country = optarg;
161 			break;
162 		case 'f':
163 			host = FNICHOST;
164 			break;
165 		case 'g':
166 			host = GNICHOST;
167 			break;
168 		case 'h':
169 			host = optarg;
170 			break;
171 		case 'i':
172 			host = INICHOST;
173 			break;
174 		case 'I':
175 			host = IANAHOST;
176 			break;
177 		case 'k':
178 			host = KNICHOST;
179 			break;
180 		case 'l':
181 			host = LNICHOST;
182 			break;
183 		case 'm':
184 			host = MNICHOST;
185 			break;
186 		case 'p':
187 			port = optarg;
188 			break;
189 		case 'P':
190 			host = PDBHOST;
191 			break;
192 		case 'Q':
193 			flags |= WHOIS_QUICK;
194 			break;
195 		case 'r':
196 			host = RNICHOST;
197 			break;
198 		case 'R':
199 			flags |= WHOIS_RECURSE;
200 			break;
201 		case 'S':
202 			flags |= WHOIS_SPAM_ME;
203 			break;
204 		case '?':
205 		default:
206 			usage();
207 			/* NOTREACHED */
208 		}
209 	}
210 	argc -= optind;
211 	argv += optind;
212 
213 	if (!argc || (country != NULL && host != NULL))
214 		usage();
215 
216 	/*
217 	 * If no host or country is specified, rely on referrals from IANA.
218 	 */
219 	if (host == NULL && country == NULL) {
220 		if ((host = getenv("WHOIS_SERVER")) == NULL &&
221 		    (host = getenv("RA_SERVER")) == NULL) {
222 			if (!(flags & WHOIS_QUICK))
223 				flags |= WHOIS_RECURSE;
224 		}
225 	}
226 	while (argc-- > 0) {
227 		if (country != NULL) {
228 			char *qnichost;
229 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
230 			whois(*argv, qnichost, flags);
231 			free(qnichost);
232 		} else
233 			whois(*argv, host != NULL ? host :
234 			      choose_server(*argv), flags);
235 		argv++;
236 	}
237 	exit(0);
238 }
239 
240 static const char *
241 choose_server(char *domain)
242 {
243 	size_t len = strlen(domain);
244 	int i;
245 
246 	for (i = 0; whoiswhere[i].suffix != NULL; i++) {
247 		size_t suffix_len = strlen(whoiswhere[i].suffix);
248 		if (len > suffix_len &&
249 		    strcasecmp(domain + len - suffix_len,
250 			       whoiswhere[i].suffix) == 0)
251 			return (whoiswhere[i].server);
252 	}
253 	errx(EX_SOFTWARE, "no default whois server");
254 }
255 
256 static struct addrinfo *
257 gethostinfo(char const *host, int exit_on_noname)
258 {
259 	struct addrinfo hints, *res;
260 	int error;
261 
262 	memset(&hints, 0, sizeof(hints));
263 	hints.ai_flags = AI_CANONNAME;
264 	hints.ai_family = AF_UNSPEC;
265 	hints.ai_socktype = SOCK_STREAM;
266 	res = NULL;
267 	error = getaddrinfo(host, port, &hints, &res);
268 	if (error && (exit_on_noname || error != EAI_NONAME))
269 		err(EX_NOHOST, "%s: %s", host, gai_strerror(error));
270 	return (res);
271 }
272 
273 /*
274  * Wrapper for asprintf(3) that exits on error.
275  */
276 static void
277 s_asprintf(char **ret, const char *format, ...)
278 {
279 	va_list ap;
280 
281 	va_start(ap, format);
282 	if (vasprintf(ret, format, ap) == -1) {
283 		va_end(ap);
284 		err(EX_OSERR, "vasprintf()");
285 	}
286 	va_end(ap);
287 }
288 
289 static int
290 connect_to_any_host(struct addrinfo *hostres)
291 {
292 	struct addrinfo *res;
293 	nfds_t i, j;
294 	size_t count;
295 	struct pollfd *fds;
296 	int timeout = 180, s = -1;
297 
298 	for (res = hostres, count = 0; res; res = res->ai_next)
299 		count++;
300 	fds = calloc(count, sizeof(*fds));
301 	if (fds == NULL)
302 		err(EX_OSERR, "calloc()");
303 
304 	/*
305 	 * Traverse the result list elements and make non-block
306 	 * connection attempts.
307 	 */
308 	count = i = 0;
309 	for (res = hostres; res != NULL; res = res->ai_next) {
310 		s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
311 		    res->ai_protocol);
312 		if (s < 0)
313 			continue;
314 		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
315 			if (errno == EINPROGRESS) {
316 				/* Add the socket to poll list */
317 				fds[i].fd = s;
318 				fds[i].events = POLLERR | POLLHUP |
319 						POLLIN | POLLOUT;
320 				/*
321 				 * From here until a socket connects, the
322 				 * socket fd is owned by the fds[] poll array.
323 				 */
324 				s = -1;
325 				count++;
326 				i++;
327 			} else {
328 				close(s);
329 				s = -1;
330 
331 				/*
332 				 * Poll only if we have something to poll,
333 				 * otherwise just go ahead and try next
334 				 * address
335 				 */
336 				if (count == 0)
337 					continue;
338 			}
339 		} else
340 			goto done;
341 
342 		/*
343 		 * If we are at the last address, poll until a connection is
344 		 * established or we failed all connection attempts.
345 		 */
346 		if (res->ai_next == NULL)
347 			timeout = INFTIM;
348 
349 		/*
350 		 * Poll the watched descriptors for successful connections:
351 		 * if we still have more untried resolved addresses, poll only
352 		 * once; otherwise, poll until all descriptors have errors,
353 		 * which will be considered as ETIMEDOUT later.
354 		 */
355 		do {
356 			int n;
357 
358 			n = poll(fds, i, timeout);
359 			if (n == 0) {
360 				/*
361 				 * No event reported in time.  Try with a
362 				 * smaller timeout (but cap at 2-3ms)
363 				 * after a new host have been added.
364 				 */
365 				if (timeout >= 3)
366 					timeout >>= 1;
367 
368 				break;
369 			} else if (n < 0) {
370 				/*
371 				 * errno here can only be EINTR which we would
372 				 * want to clean up and bail out.
373 				 */
374 				s = -1;
375 				goto done;
376 			}
377 
378 			/*
379 			 * Check for the event(s) we have seen.
380 			 */
381 			for (j = 0; j < i; j++) {
382 				if (fds[j].fd == -1 || fds[j].events == 0 ||
383 				    fds[j].revents == 0)
384 					continue;
385 				if (fds[j].revents & ~(POLLIN | POLLOUT)) {
386 					close(fds[j].fd);
387 					fds[j].fd = -1;
388 					fds[j].events = 0;
389 					count--;
390 					continue;
391 				} else if (fds[j].revents & (POLLIN | POLLOUT)) {
392 					/* Connect succeeded. */
393 					s = fds[j].fd;
394 					fds[j].fd = -1;
395 
396 					goto done;
397 				}
398 
399 			}
400 		} while (timeout == INFTIM && count != 0);
401 	}
402 
403 	/* All attempts were failed */
404 	s = -1;
405 	if (count == 0)
406 		errno = ETIMEDOUT;
407 
408 done:
409 	/* Close all watched fds except the succeeded one */
410 	for (j = 0; j < i; j++)
411 		if (fds[j].fd != -1)
412 			close(fds[j].fd);
413 	free(fds);
414 	return (s);
415 }
416 
417 static void
418 whois(const char *query, const char *hostname, int flags)
419 {
420 	FILE *fp;
421 	struct addrinfo *hostres;
422 	char *buf, *host, *nhost, *p;
423 	int s, f;
424 	size_t len, i;
425 
426 	hostres = gethostinfo(hostname, 1);
427 	s = connect_to_any_host(hostres);
428 	if (s == -1)
429 		err(EX_OSERR, "connect()");
430 
431 	/* Restore default blocking behavior.  */
432 	if ((f = fcntl(s, F_GETFL)) == -1)
433 		err(EX_OSERR, "fcntl()");
434 	f &= ~O_NONBLOCK;
435 	if (fcntl(s, F_SETFL, f) == -1)
436 		err(EX_OSERR, "fcntl()");
437 
438 	fp = fdopen(s, "r+");
439 	if (fp == NULL)
440 		err(EX_OSERR, "fdopen()");
441 
442 	if (!(flags & WHOIS_SPAM_ME) &&
443 	    (strcasecmp(hostname, DENICHOST) == 0 ||
444 	     strcasecmp(hostname, "de" QNICHOST_TAIL) == 0)) {
445 		const char *q;
446 		int idn = 0;
447 		for (q = query; *q != '\0'; q++)
448 			if (!isascii(*q))
449 				idn = 1;
450 		fprintf(fp, "-T dn%s %s\r\n", idn ? "" : ",ace", query);
451 	} else if (!(flags & WHOIS_SPAM_ME) &&
452 		   (strcasecmp(hostname, DKNICHOST) == 0 ||
453 		    strcasecmp(hostname, "dk" QNICHOST_TAIL) == 0))
454 		fprintf(fp, "--show-handles %s\r\n", query);
455 	else if ((flags & WHOIS_SPAM_ME) ||
456 		 strchr(query, ' ') != NULL)
457 		fprintf(fp, "%s\r\n", query);
458 	else if (strcasecmp(hostname, ANICHOST) == 0) {
459 		if (strncasecmp(query, "AS", 2) == 0 &&
460 		    strspn(query+2, "0123456789") == strlen(query+2))
461 			fprintf(fp, "+ a %s\r\n", query+2);
462 		else
463 			fprintf(fp, "+ %s\r\n", query);
464 	} else if (strcasecmp(hostres->ai_canonname, VNICHOST) == 0)
465 		fprintf(fp, "domain %s\r\n", query);
466 	else
467 		fprintf(fp, "%s\r\n", query);
468 	fflush(fp);
469 
470 	nhost = NULL;
471 	while ((buf = fgetln(fp, &len)) != NULL) {
472 		/* Nominet */
473 		if (!(flags & WHOIS_SPAM_ME) &&
474 		    len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
475 			break;
476 
477 		printf("%.*s", (int)len, buf);
478 
479 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
480 			for (i = 0; whois_referral[i].prefix != NULL; i++) {
481 				p = buf;
482 				SCAN(p, buf+len, *p == ' ');
483 				if (strncasecmp(p, whois_referral[i].prefix,
484 					           whois_referral[i].len) != 0)
485 					continue;
486 				p += whois_referral[i].len;
487 				SCAN(p, buf+len, *p == ' ');
488 				host = p;
489 				SCAN(p, buf+len, ishost(*p));
490 				/* avoid loops */
491 				if (strncmp(hostname, host, p - host) != 0)
492 					s_asprintf(&nhost, "%.*s",
493 						   (int)(p - host), host);
494 				break;
495 			}
496 			for (i = 0; actually_arin[i] != NULL; i++) {
497 				if (strncmp(buf, actually_arin[i], len) == 0) {
498 					s_asprintf(&nhost, "%s", ANICHOST);
499 					break;
500 				}
501 			}
502 		}
503 		/* Verisign etc. */
504 		if (!(flags & WHOIS_SPAM_ME) &&
505 		    len >= sizeof(CHOPSPAM)-1 &&
506 		    (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
507 		     strncasecmp(buf, CHOPSPAM+4, sizeof(CHOPSPAM)-5) == 0)) {
508 			printf("\n");
509 			break;
510 		}
511 	}
512 	fclose(fp);
513 	freeaddrinfo(hostres);
514 	if (nhost != NULL) {
515 		whois(query, nhost, flags);
516 		free(nhost);
517 	}
518 }
519 
520 static void
521 usage(void)
522 {
523 	fprintf(stderr,
524 	    "usage: whois [-aAbfgiIklmPQrRS] [-c country-code | -h hostname] "
525 	    "[-p port] name ...\n");
526 	exit(EX_USAGE);
527 }
528