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