xref: /freebsd/usr.bin/whois/whois.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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 			if (strcasecmp(country, "su") == 0)
179 				country = "ru";
180 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
181 			whois(*argv, qnichost, flags);
182 		} else if (use_qnichost)
183 			if ((qnichost = choose_server(*argv)) != NULL)
184 				whois(*argv, qnichost, flags);
185 		if (qnichost == NULL)
186 			whois(*argv, host, flags);
187 		free(qnichost);
188 		qnichost = NULL;
189 		argv++;
190 	}
191 	exit(0);
192 }
193 
194 /*
195  * This function will remove any trailing periods from domain, after which it
196  * returns a pointer to newly allocated memory containing the whois server to
197  * be queried, or a NULL if the correct server couldn't be determined.  The
198  * caller must remember to free(3) the allocated memory.
199  */
200 static char *
201 choose_server(char *domain)
202 {
203 	char *pos, *retval;
204 
205 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
206 		*pos = '\0';
207 	if (*domain == '\0')
208 		errx(EX_USAGE, "can't search for a null string");
209 	if (strlen(domain) > sizeof("-NORID")-1 &&
210 	    strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
211 		"-NORID") == 0) {
212 		s_asprintf(&retval, "%s", NORIDHOST);
213 		return (retval);
214 	}
215 	while (pos > domain && *pos != '.')
216 		--pos;
217 	if (pos <= domain)
218 		return (NULL);
219 	if (isdigit((unsigned char)*++pos))
220 		s_asprintf(&retval, "%s", ANICHOST);
221 	else {
222 		if (strcasecmp(pos, "su") == 0)
223 			pos = "ru";
224 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
225 	}
226 	return (retval);
227 }
228 
229 static struct addrinfo *
230 gethostinfo(char const *host, int exit_on_error)
231 {
232 	struct addrinfo hints, *res;
233 	int error;
234 
235 	memset(&hints, 0, sizeof(hints));
236 	hints.ai_flags = 0;
237 	hints.ai_family = AF_UNSPEC;
238 	hints.ai_socktype = SOCK_STREAM;
239 	error = getaddrinfo(host, port, &hints, &res);
240 	if (error) {
241 		warnx("%s: %s", host, gai_strerror(error));
242 		if (exit_on_error)
243 			exit(EX_NOHOST);
244 		return (NULL);
245 	}
246 	return (res);
247 }
248 
249 /*
250  * Wrapper for asprintf(3) that exits on error.
251  */
252 static void
253 s_asprintf(char **ret, const char *format, ...)
254 {
255 	va_list ap;
256 
257 	va_start(ap, format);
258 	if (vasprintf(ret, format, ap) == -1) {
259 		va_end(ap);
260 		err(EX_OSERR, "vasprintf()");
261 	}
262 	va_end(ap);
263 }
264 
265 static void
266 whois(const char *query, const char *hostname, int flags)
267 {
268 	FILE *sfi, *sfo;
269 	struct addrinfo *hostres, *res;
270 	char *buf, *host, *nhost, *p;
271 	int i, s;
272 	size_t c, len;
273 
274 	hostres = gethostinfo(hostname, 1);
275 	for (res = hostres; res; res = res->ai_next) {
276 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
277 		if (s < 0)
278 			continue;
279 		if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
280 			break;
281 		close(s);
282 	}
283 	freeaddrinfo(hostres);
284 	if (res == NULL)
285 		err(EX_OSERR, "connect()");
286 
287 	sfi = fdopen(s, "r");
288 	sfo = fdopen(s, "w");
289 	if (sfi == NULL || sfo == NULL)
290 		err(EX_OSERR, "fdopen()");
291 	fprintf(sfo, "%s\r\n", query);
292 	fflush(sfo);
293 	nhost = NULL;
294 	while ((buf = fgetln(sfi, &len)) != NULL) {
295 		while (len > 0 && isspace((unsigned char)buf[len - 1]))
296 			buf[--len] = '\0';
297 		printf("%.*s\n", (int)len, buf);
298 
299 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
300 			host = strnstr(buf, WHOIS_SERVER_ID, len);
301 			if (host != NULL) {
302 				host += sizeof(WHOIS_SERVER_ID) - 1;
303 				for (p = host; p < buf + len; p++) {
304 					if (!ishost(*p)) {
305 						*p = '\0';
306 						break;
307 					}
308 				}
309 				s_asprintf(&nhost, "%.*s",
310 				     (int)(buf + len - host), host);
311 			} else if ((host =
312 			    strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
313 				host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
314 				for (p = host; p < buf + len; p++) {
315 					if (!ishost(*p)) {
316 						*p = '\0';
317 						break;
318 					}
319 				}
320 				s_asprintf(&nhost, "%.*s",
321 				    (int)(buf + len - host), host);
322 			} else if (strcmp(hostname, ANICHOST) == 0) {
323 				for (c = 0; c <= len; c++)
324 					buf[c] = tolower((int)buf[c]);
325 				for (i = 0; ip_whois[i] != NULL; i++) {
326 					if (strnstr(buf, ip_whois[i], len) !=
327 					    NULL) {
328 						s_asprintf(&nhost, "%s",
329 						    ip_whois[i]);
330 						break;
331 					}
332 				}
333 			}
334 		}
335 	}
336 	if (nhost != NULL) {
337 		whois(query, nhost, 0);
338 		free(nhost);
339 	}
340 }
341 
342 static void
343 usage(void)
344 {
345 	fprintf(stderr,
346 	    "usage: whois [-aAdgilmQrR6] [-c country-code | -h hostname] "
347 	    "[-p port] name ...\n");
348 	exit(EX_USAGE);
349 }
350