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