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