xref: /freebsd/usr.bin/whois/whois.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
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 <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <ctype.h>
50 #include <err.h>
51 #include <netdb.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58 
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:"
78 
79 #define WHOIS_RECURSE		0x01
80 #define WHOIS_QUICK		0x02
81 
82 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
83 
84 static const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
85 				  FNICHOST, NULL };
86 static const char *port = DEFAULT_PORT;
87 
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);
93 
94 int
95 main(int argc, char *argv[])
96 {
97 	const char *country, *host;
98 	char *qnichost;
99 	int ch, flags, use_qnichost;
100 
101 #ifdef	SOCKS
102 	SOCKSinit(argv[0]);
103 #endif
104 
105 	country = host = qnichost = NULL;
106 	flags = use_qnichost = 0;
107 	while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:QrR6")) != -1) {
108 		switch (ch) {
109 		case 'a':
110 			host = ANICHOST;
111 			break;
112 		case 'A':
113 			host = PNICHOST;
114 			break;
115 		case 'b':
116 			host = ABUSEHOST;
117 			break;
118 		case 'c':
119 			country = optarg;
120 			break;
121 		case 'f':
122 			host = FNICHOST;
123 			break;
124 		case 'g':
125 			host = GNICHOST;
126 			break;
127 		case 'h':
128 			host = optarg;
129 			break;
130 		case 'i':
131 			host = INICHOST;
132 			break;
133 		case 'I':
134 			host = IANAHOST;
135 			break;
136 		case 'k':
137 			host = KNICHOST;
138 			break;
139 		case 'l':
140 			host = LNICHOST;
141 			break;
142 		case 'm':
143 			host = MNICHOST;
144 			break;
145 		case 'p':
146 			port = optarg;
147 			break;
148 		case 'Q':
149 			flags |= WHOIS_QUICK;
150 			break;
151 		case 'r':
152 			host = RNICHOST;
153 			break;
154 		case 'R':
155 			warnx("-R is deprecated; use '-c ru' instead");
156 			country = "ru";
157 			break;
158 		/* Remove in FreeBSD 10 */
159 		case '6':
160 			errx(EX_USAGE,
161 				"-6 is deprecated; use -[aAflr] instead");
162 			break;
163 		case '?':
164 		default:
165 			usage();
166 			/* NOTREACHED */
167 		}
168 	}
169 	argc -= optind;
170 	argv += optind;
171 
172 	if (!argc || (country != NULL && host != NULL))
173 		usage();
174 
175 	/*
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
179 	 * back to NICHOST.
180 	 */
181 	if (host == NULL && country == NULL) {
182 		use_qnichost = 1;
183 		host = NICHOST;
184 		if (!(flags & WHOIS_QUICK))
185 			flags |= WHOIS_RECURSE;
186 	}
187 	while (argc-- > 0) {
188 		if (country != NULL) {
189 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
190 			whois(*argv, qnichost, flags);
191 		} else if (use_qnichost)
192 			if ((qnichost = choose_server(*argv)) != NULL)
193 				whois(*argv, qnichost, flags);
194 		if (qnichost == NULL)
195 			whois(*argv, host, flags);
196 		free(qnichost);
197 		qnichost = NULL;
198 		argv++;
199 	}
200 	exit(0);
201 }
202 
203 /*
204  * This function will remove any trailing periods from domain, after which it
205  * returns a pointer to newly allocated memory containing the whois server to
206  * be queried, or a NULL if the correct server couldn't be determined.  The
207  * caller must remember to free(3) the allocated memory.
208  */
209 static char *
210 choose_server(char *domain)
211 {
212 	char *pos, *retval;
213 
214 	if (strchr(domain, ':')) {
215 		s_asprintf(&retval, "%s", ANICHOST);
216 		return (retval);
217 	}
218 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
219 		*pos = '\0';
220 	if (*domain == '\0')
221 		errx(EX_USAGE, "can't search for a null string");
222 	if (strlen(domain) > sizeof("-NORID")-1 &&
223 	    strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
224 		"-NORID") == 0) {
225 		s_asprintf(&retval, "%s", NORIDHOST);
226 		return (retval);
227 	}
228 	while (pos > domain && *pos != '.')
229 		--pos;
230 	if (pos <= domain)
231 		return (NULL);
232 	if (isdigit((unsigned char)*++pos))
233 		s_asprintf(&retval, "%s", ANICHOST);
234 	else
235 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
236 	return (retval);
237 }
238 
239 static struct addrinfo *
240 gethostinfo(char const *host, int exit_on_error)
241 {
242 	struct addrinfo hints, *res;
243 	int error;
244 
245 	memset(&hints, 0, sizeof(hints));
246 	hints.ai_flags = 0;
247 	hints.ai_family = AF_UNSPEC;
248 	hints.ai_socktype = SOCK_STREAM;
249 	error = getaddrinfo(host, port, &hints, &res);
250 	if (error) {
251 		warnx("%s: %s", host, gai_strerror(error));
252 		if (exit_on_error)
253 			exit(EX_NOHOST);
254 		return (NULL);
255 	}
256 	return (res);
257 }
258 
259 /*
260  * Wrapper for asprintf(3) that exits on error.
261  */
262 static void
263 s_asprintf(char **ret, const char *format, ...)
264 {
265 	va_list ap;
266 
267 	va_start(ap, format);
268 	if (vasprintf(ret, format, ap) == -1) {
269 		va_end(ap);
270 		err(EX_OSERR, "vasprintf()");
271 	}
272 	va_end(ap);
273 }
274 
275 static void
276 whois(const char *query, const char *hostname, int flags)
277 {
278 	FILE *sfi, *sfo;
279 	struct addrinfo *hostres, *res;
280 	char *buf, *host, *nhost, *p;
281 	int i, s;
282 	size_t c, len;
283 
284 	s = -1;
285 	hostres = gethostinfo(hostname, 1);
286 	for (res = hostres; res; res = res->ai_next) {
287 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
288 		if (s < 0)
289 			continue;
290 		if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
291 			break;
292 		close(s);
293 	}
294 	freeaddrinfo(hostres);
295 	if (res == NULL)
296 		err(EX_OSERR, "connect()");
297 
298 	sfi = fdopen(s, "r");
299 	sfo = fdopen(s, "w");
300 	if (sfi == NULL || sfo == NULL)
301 		err(EX_OSERR, "fdopen()");
302 	if (strcmp(hostname, GERMNICHOST) == 0) {
303 		fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query);
304 	} else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
305 		fprintf(sfo, "--show-handles %s\r\n", query);
306 	} else {
307 		fprintf(sfo, "%s\r\n", query);
308 	}
309 	fflush(sfo);
310 	nhost = NULL;
311 	while ((buf = fgetln(sfi, &len)) != NULL) {
312 		while (len > 0 && isspace((unsigned char)buf[len - 1]))
313 			buf[--len] = '\0';
314 		printf("%.*s\n", (int)len, buf);
315 
316 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
317 			host = strnstr(buf, WHOIS_SERVER_ID, len);
318 			if (host != NULL) {
319 				host += sizeof(WHOIS_SERVER_ID) - 1;
320 				for (p = host; p < buf + len; p++) {
321 					if (!ishost(*p)) {
322 						*p = '\0';
323 						break;
324 					}
325 				}
326 				s_asprintf(&nhost, "%.*s",
327 				     (int)(buf + len - host), host);
328 			} else if ((host =
329 			    strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
330 				host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
331 				for (p = host; p < buf + len; p++) {
332 					if (!ishost(*p)) {
333 						*p = '\0';
334 						break;
335 					}
336 				}
337 				s_asprintf(&nhost, "%.*s",
338 				    (int)(buf + len - host), host);
339 			} else if (strcmp(hostname, ANICHOST) == 0) {
340 				for (c = 0; c <= len; c++)
341 					buf[c] = tolower((unsigned char)buf[c]);
342 				for (i = 0; ip_whois[i] != NULL; i++) {
343 					if (strnstr(buf, ip_whois[i], len) !=
344 					    NULL) {
345 						s_asprintf(&nhost, "%s",
346 						    ip_whois[i]);
347 						break;
348 					}
349 				}
350 			}
351 		}
352 	}
353 	if (nhost != NULL) {
354 		whois(query, nhost, 0);
355 		free(nhost);
356 	}
357 }
358 
359 static void
360 usage(void)
361 {
362 	fprintf(stderr,
363 	    "usage: whois [-aAbfgiIklmQrR6] [-c country-code | -h hostname] "
364 	    "[-p port] name ...\n");
365 	exit(EX_USAGE);
366 }
367