xref: /freebsd/usr.bin/whois/whois.c (revision 058543317c5f7a313ee41783f90a2eab7021f40d)
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	NICHOST		"whois.crsnic.net"
64 #define	INICHOST	"whois.networksolutions.com"
65 #define	DNICHOST	"whois.nic.mil"
66 #define	GNICHOST	"whois.nic.gov"
67 #define	ANICHOST	"whois.arin.net"
68 #define	LNICHOST	"whois.lacnic.net"
69 #define	RNICHOST	"whois.ripe.net"
70 #define	PNICHOST	"whois.apnic.net"
71 #define	MNICHOST	"whois.ra.net"
72 #define	QNICHOST_TAIL	".whois-servers.net"
73 #define	SNICHOST	"whois.6bone.net"
74 #define	BNICHOST	"whois.registro.br"
75 #define NORIDHOST	"whois.norid.no"
76 #define	DEFAULT_PORT	"whois"
77 #define	WHOIS_SERVER_ID	"Whois Server: "
78 #define	WHOIS_ORG_SERVER_ID	"Registrant Street1:Whois Server:"
79 
80 #define WHOIS_RECURSE		0x01
81 #define WHOIS_QUICK		0x02
82 
83 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
84 
85 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
86 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, "aAc:dgh:ilmp:QrR6")) != -1) {
108 		switch (ch) {
109 		case 'a':
110 			host = ANICHOST;
111 			break;
112 		case 'A':
113 			host = PNICHOST;
114 			break;
115 		case 'c':
116 			country = optarg;
117 			break;
118 		case 'd':
119 			host = DNICHOST;
120 			break;
121 		case 'g':
122 			host = GNICHOST;
123 			break;
124 		case 'h':
125 			host = optarg;
126 			break;
127 		case 'i':
128 			host = INICHOST;
129 			break;
130 		case 'l':
131 			host = LNICHOST;
132 			break;
133 		case 'm':
134 			host = MNICHOST;
135 			break;
136 		case 'p':
137 			port = optarg;
138 			break;
139 		case 'Q':
140 			flags |= WHOIS_QUICK;
141 			break;
142 		case 'r':
143 			host = RNICHOST;
144 			break;
145 		case 'R':
146 			warnx("-R is deprecated; use '-c ru' instead");
147 			country = "ru";
148 			break;
149 		case '6':
150 			host = SNICHOST;
151 			break;
152 		case '?':
153 		default:
154 			usage();
155 			/* NOTREACHED */
156 		}
157 	}
158 	argc -= optind;
159 	argv += optind;
160 
161 	if (!argc || (country != NULL && host != NULL))
162 		usage();
163 
164 	/*
165 	 * If no host or country is specified determine the top level domain
166 	 * from the query.  If the TLD is a number, query ARIN.  Otherwise, use
167 	 * TLD.whois-server.net.  If the domain does not contain '.', fall
168 	 * back to NICHOST.
169 	 */
170 	if (host == NULL && country == NULL) {
171 		use_qnichost = 1;
172 		host = NICHOST;
173 		if (!(flags & WHOIS_QUICK))
174 			flags |= WHOIS_RECURSE;
175 	}
176 	while (argc-- > 0) {
177 		if (country != NULL) {
178 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
179 			whois(*argv, qnichost, flags);
180 		} else if (use_qnichost)
181 			if ((qnichost = choose_server(*argv)) != NULL)
182 				whois(*argv, qnichost, flags);
183 		if (qnichost == NULL)
184 			whois(*argv, host, flags);
185 		free(qnichost);
186 		qnichost = NULL;
187 		argv++;
188 	}
189 	exit(0);
190 }
191 
192 /*
193  * This function will remove any trailing periods from domain, after which it
194  * returns a pointer to newly allocated memory containing the whois server to
195  * be queried, or a NULL if the correct server couldn't be determined.  The
196  * caller must remember to free(3) the allocated memory.
197  */
198 static char *
199 choose_server(char *domain)
200 {
201 	char *pos, *retval;
202 
203 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
204 		*pos = '\0';
205 	if (*domain == '\0')
206 		errx(EX_USAGE, "can't search for a null string");
207 	if (strlen(domain) > sizeof("-NORID")-1 &&
208 	    strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
209 		"-NORID") == 0) {
210 		s_asprintf(&retval, "%s", NORIDHOST);
211 		return (retval);
212 	}
213 	while (pos > domain && *pos != '.')
214 		--pos;
215 	if (pos <= domain)
216 		return (NULL);
217 	if (isdigit((unsigned char)*++pos))
218 		s_asprintf(&retval, "%s", ANICHOST);
219 	else
220 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
221 	return (retval);
222 }
223 
224 static struct addrinfo *
225 gethostinfo(char const *host, int exit_on_error)
226 {
227 	struct addrinfo hints, *res;
228 	int error;
229 
230 	memset(&hints, 0, sizeof(hints));
231 	hints.ai_flags = 0;
232 	hints.ai_family = AF_UNSPEC;
233 	hints.ai_socktype = SOCK_STREAM;
234 	error = getaddrinfo(host, port, &hints, &res);
235 	if (error) {
236 		warnx("%s: %s", host, gai_strerror(error));
237 		if (exit_on_error)
238 			exit(EX_NOHOST);
239 		return (NULL);
240 	}
241 	return (res);
242 }
243 
244 /*
245  * Wrapper for asprintf(3) that exits on error.
246  */
247 static void
248 s_asprintf(char **ret, const char *format, ...)
249 {
250 	va_list ap;
251 
252 	va_start(ap, format);
253 	if (vasprintf(ret, format, ap) == -1) {
254 		va_end(ap);
255 		err(EX_OSERR, "vasprintf()");
256 	}
257 	va_end(ap);
258 }
259 
260 static void
261 whois(const char *query, const char *hostname, int flags)
262 {
263 	FILE *sfi, *sfo;
264 	struct addrinfo *hostres, *res;
265 	char *buf, *host, *nhost, *p;
266 	int i, s;
267 	size_t c, len;
268 
269 	hostres = gethostinfo(hostname, 1);
270 	for (res = hostres; res; res = res->ai_next) {
271 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
272 		if (s < 0)
273 			continue;
274 		if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
275 			break;
276 		close(s);
277 	}
278 	freeaddrinfo(hostres);
279 	if (res == NULL)
280 		err(EX_OSERR, "connect()");
281 
282 	sfi = fdopen(s, "r");
283 	sfo = fdopen(s, "w");
284 	if (sfi == NULL || sfo == NULL)
285 		err(EX_OSERR, "fdopen()");
286 	fprintf(sfo, "%s\r\n", query);
287 	fflush(sfo);
288 	nhost = NULL;
289 	while ((buf = fgetln(sfi, &len)) != NULL) {
290 		while (len > 0 && isspace((unsigned char)buf[len - 1]))
291 			buf[--len] = '\0';
292 		printf("%.*s\n", (int)len, buf);
293 
294 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
295 			host = strnstr(buf, WHOIS_SERVER_ID, len);
296 			if (host != NULL) {
297 				host += sizeof(WHOIS_SERVER_ID) - 1;
298 				for (p = host; p < buf + len; p++) {
299 					if (!ishost(*p)) {
300 						*p = '\0';
301 						break;
302 					}
303 				}
304 				s_asprintf(&nhost, "%.*s",
305 				     (int)(buf + len - host), host);
306 			} else if ((host =
307 			    strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
308 				host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
309 				for (p = host; p < buf + len; p++) {
310 					if (!ishost(*p)) {
311 						*p = '\0';
312 						break;
313 					}
314 				}
315 				s_asprintf(&nhost, "%.*s",
316 				    (int)(buf + len - host), host);
317 			} else if (strcmp(hostname, ANICHOST) == 0) {
318 				for (c = 0; c <= len; c++)
319 					buf[c] = tolower((int)buf[c]);
320 				for (i = 0; ip_whois[i] != NULL; i++) {
321 					if (strnstr(buf, ip_whois[i], len) !=
322 					    NULL) {
323 						s_asprintf(&nhost, "%s",
324 						    ip_whois[i]);
325 						break;
326 					}
327 				}
328 			}
329 		}
330 	}
331 	if (nhost != NULL) {
332 		whois(query, nhost, 0);
333 		free(nhost);
334 	}
335 }
336 
337 static void
338 usage(void)
339 {
340 	fprintf(stderr,
341 	    "usage: whois [-aAdgilmQrR6] [-c country-code | -h hostname] "
342 	    "[-p port] name ...\n");
343 	exit(EX_USAGE);
344 }
345