xref: /freebsd/usr.bin/whois/whois.c (revision 63f9a4cb2684a303e3eb2ffed39c03a2e2b28ae0)
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  * 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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
43 #endif /* not lint */
44 #endif
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <netdb.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sysexits.h>
61 #include <unistd.h>
62 
63 #define	ABUSEHOST	"whois.abuse.net"
64 #define	NICHOST		"whois.crsnic.net"
65 #define	INICHOST	"whois.networksolutions.com"
66 #define	DNICHOST	"whois.nic.mil"
67 #define	GNICHOST	"whois.nic.gov"
68 #define	ANICHOST	"whois.arin.net"
69 #define	LNICHOST	"whois.lacnic.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	SNICHOST	"whois.6bone.net"
75 #define	BNICHOST	"whois.registro.br"
76 #define NORIDHOST	"whois.norid.no"
77 #define	IANAHOST	"whois.iana.org"
78 #define GERMNICHOST	"de.whois-servers.net"
79 #define	DEFAULT_PORT	"whois"
80 #define	WHOIS_SERVER_ID	"Whois Server: "
81 #define	WHOIS_ORG_SERVER_ID	"Registrant Street1:Whois Server:"
82 
83 #define WHOIS_RECURSE		0x01
84 #define WHOIS_QUICK		0x02
85 
86 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
87 
88 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
89 const char *port = DEFAULT_PORT;
90 
91 static char *choose_server(char *);
92 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
93 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
94 static void usage(void);
95 static void whois(const char *, const char *, int);
96 
97 int
98 main(int argc, char *argv[])
99 {
100 	const char *country, *host;
101 	char *qnichost;
102 	int ch, flags, use_qnichost;
103 
104 #ifdef	SOCKS
105 	SOCKSinit(argv[0]);
106 #endif
107 
108 	country = host = qnichost = NULL;
109 	flags = use_qnichost = 0;
110 	while ((ch = getopt(argc, argv, "aAbc:dgh:iIlmp:QrR6")) != -1) {
111 		switch (ch) {
112 		case 'a':
113 			host = ANICHOST;
114 			break;
115 		case 'A':
116 			host = PNICHOST;
117 			break;
118 		case 'b':
119 			host = ABUSEHOST;
120 			break;
121 		case 'c':
122 			country = optarg;
123 			break;
124 		case 'd':
125 			host = DNICHOST;
126 			break;
127 		case 'g':
128 			host = GNICHOST;
129 			break;
130 		case 'h':
131 			host = optarg;
132 			break;
133 		case 'i':
134 			host = INICHOST;
135 			break;
136 		case 'I':
137 			host = IANAHOST;
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 		case '6':
159 			host = SNICHOST;
160 			break;
161 		case '?':
162 		default:
163 			usage();
164 			/* NOTREACHED */
165 		}
166 	}
167 	argc -= optind;
168 	argv += optind;
169 
170 	if (!argc || (country != NULL && host != NULL))
171 		usage();
172 
173 	/*
174 	 * If no host or country is specified determine the top level domain
175 	 * from the query.  If the TLD is a number, query ARIN.  Otherwise, use
176 	 * TLD.whois-server.net.  If the domain does not contain '.', fall
177 	 * back to NICHOST.
178 	 */
179 	if (host == NULL && country == NULL) {
180 		use_qnichost = 1;
181 		host = NICHOST;
182 		if (!(flags & WHOIS_QUICK))
183 			flags |= WHOIS_RECURSE;
184 	}
185 	while (argc-- > 0) {
186 		if (country != NULL) {
187 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
188 			whois(*argv, qnichost, flags);
189 		} else if (use_qnichost)
190 			if ((qnichost = choose_server(*argv)) != NULL)
191 				whois(*argv, qnichost, flags);
192 		if (qnichost == NULL)
193 			whois(*argv, host, flags);
194 		free(qnichost);
195 		qnichost = NULL;
196 		argv++;
197 	}
198 	exit(0);
199 }
200 
201 /*
202  * This function will remove any trailing periods from domain, after which it
203  * returns a pointer to newly allocated memory containing the whois server to
204  * be queried, or a NULL if the correct server couldn't be determined.  The
205  * caller must remember to free(3) the allocated memory.
206  */
207 static char *
208 choose_server(char *domain)
209 {
210 	char *pos, *retval;
211 
212 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
213 		*pos = '\0';
214 	if (*domain == '\0')
215 		errx(EX_USAGE, "can't search for a null string");
216 	if (strlen(domain) > sizeof("-NORID")-1 &&
217 	    strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
218 		"-NORID") == 0) {
219 		s_asprintf(&retval, "%s", NORIDHOST);
220 		return (retval);
221 	}
222 	while (pos > domain && *pos != '.')
223 		--pos;
224 	if (pos <= domain)
225 		return (NULL);
226 	if (isdigit((unsigned char)*++pos))
227 		s_asprintf(&retval, "%s", ANICHOST);
228 	else
229 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
230 	return (retval);
231 }
232 
233 static struct addrinfo *
234 gethostinfo(char const *host, int exit_on_error)
235 {
236 	struct addrinfo hints, *res;
237 	int error;
238 
239 	memset(&hints, 0, sizeof(hints));
240 	hints.ai_flags = 0;
241 	hints.ai_family = AF_UNSPEC;
242 	hints.ai_socktype = SOCK_STREAM;
243 	error = getaddrinfo(host, port, &hints, &res);
244 	if (error) {
245 		warnx("%s: %s", host, gai_strerror(error));
246 		if (exit_on_error)
247 			exit(EX_NOHOST);
248 		return (NULL);
249 	}
250 	return (res);
251 }
252 
253 /*
254  * Wrapper for asprintf(3) that exits on error.
255  */
256 static void
257 s_asprintf(char **ret, const char *format, ...)
258 {
259 	va_list ap;
260 
261 	va_start(ap, format);
262 	if (vasprintf(ret, format, ap) == -1) {
263 		va_end(ap);
264 		err(EX_OSERR, "vasprintf()");
265 	}
266 	va_end(ap);
267 }
268 
269 static void
270 whois(const char *query, const char *hostname, int flags)
271 {
272 	FILE *sfi, *sfo;
273 	struct addrinfo *hostres, *res;
274 	char *buf, *host, *nhost, *p;
275 	int i, s;
276 	size_t c, len;
277 
278 	hostres = gethostinfo(hostname, 1);
279 	for (res = hostres; res; res = res->ai_next) {
280 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
281 		if (s < 0)
282 			continue;
283 		if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
284 			break;
285 		close(s);
286 	}
287 	freeaddrinfo(hostres);
288 	if (res == NULL)
289 		err(EX_OSERR, "connect()");
290 
291 	sfi = fdopen(s, "r");
292 	sfo = fdopen(s, "w");
293 	if (sfi == NULL || sfo == NULL)
294 		err(EX_OSERR, "fdopen()");
295 	if (strcmp(hostname, GERMNICHOST) == 0) {
296 		fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query);
297 	} else {
298 		fprintf(sfo, "%s\r\n", query);
299 	}
300 	fflush(sfo);
301 	nhost = NULL;
302 	while ((buf = fgetln(sfi, &len)) != NULL) {
303 		while (len > 0 && isspace((unsigned char)buf[len - 1]))
304 			buf[--len] = '\0';
305 		printf("%.*s\n", (int)len, buf);
306 
307 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
308 			host = strnstr(buf, WHOIS_SERVER_ID, len);
309 			if (host != NULL) {
310 				host += sizeof(WHOIS_SERVER_ID) - 1;
311 				for (p = host; p < buf + len; p++) {
312 					if (!ishost(*p)) {
313 						*p = '\0';
314 						break;
315 					}
316 				}
317 				s_asprintf(&nhost, "%.*s",
318 				     (int)(buf + len - host), host);
319 			} else if ((host =
320 			    strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
321 				host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
322 				for (p = host; p < buf + len; p++) {
323 					if (!ishost(*p)) {
324 						*p = '\0';
325 						break;
326 					}
327 				}
328 				s_asprintf(&nhost, "%.*s",
329 				    (int)(buf + len - host), host);
330 			} else if (strcmp(hostname, ANICHOST) == 0) {
331 				for (c = 0; c <= len; c++)
332 					buf[c] = tolower((int)buf[c]);
333 				for (i = 0; ip_whois[i] != NULL; i++) {
334 					if (strnstr(buf, ip_whois[i], len) !=
335 					    NULL) {
336 						s_asprintf(&nhost, "%s",
337 						    ip_whois[i]);
338 						break;
339 					}
340 				}
341 			}
342 		}
343 	}
344 	if (nhost != NULL) {
345 		whois(query, nhost, 0);
346 		free(nhost);
347 	}
348 }
349 
350 static void
351 usage(void)
352 {
353 	fprintf(stderr,
354 	    "usage: whois [-aAbdgiIlmQrR6] [-c country-code | -h hostname] "
355 	    "[-p port] name ...\n");
356 	exit(EX_USAGE);
357 }
358