xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/whois.c (revision 24bb1048b1197ebc9afe761e4098573aeedfe8c9)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
6  *
7  * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
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 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <limits.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	GNICHOST	"whois.nic.gov"
54 #define	ANICHOST	"whois.arin.net"
55 #define	LNICHOST	"whois.lacnic.net"
56 #define	KNICHOST	"whois.krnic.net"
57 #define	RNICHOST	"whois.ripe.net"
58 #define	PNICHOST	"whois.apnic.net"
59 #define	MNICHOST	"whois.ra.net"
60 #define	QNICHOST_TAIL	".whois-servers.net"
61 #define	BNICHOST	"whois.registro.br"
62 #define	NORIDHOST	"whois.norid.no"
63 #define	IANAHOST	"whois.iana.org"
64 #define	GERMNICHOST	"de.whois-servers.net"
65 #define	FNICHOST	"whois.afrinic.net"
66 #define	DEFAULT_PORT	"whois"
67 #define	WHOIS_SERVER_ID	"Whois Server: "
68 #define	WHOIS_ORG_SERVER_ID	"Registrant Street1:Whois Server:"
69 
70 #define	WHOIS_RECURSE		0x01
71 #define	WHOIS_QUICK		0x02
72 
73 #define	ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
74 
75 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
76     FNICHOST, NULL };
77 const char *port = DEFAULT_PORT;
78 
79 static char *choose_server(char *);
80 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
81 static void s_asprintf(char **ret, const char *format, ...);
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 	country = host = qnichost = NULL;
93 	flags = use_qnichost = 0;
94 	while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:Qr")) != -1) {
95 		switch (ch) {
96 		case 'a':
97 			host = ANICHOST;
98 			break;
99 		case 'A':
100 			host = PNICHOST;
101 			break;
102 		case 'b':
103 			host = ABUSEHOST;
104 			break;
105 		case 'c':
106 			country = optarg;
107 			break;
108 		case 'f':
109 			host = FNICHOST;
110 			break;
111 		case 'g':
112 			host = GNICHOST;
113 			break;
114 		case 'h':
115 			host = optarg;
116 			break;
117 		case 'i':
118 			host = INICHOST;
119 			break;
120 		case 'I':
121 			host = IANAHOST;
122 			break;
123 		case 'k':
124 			host = KNICHOST;
125 			break;
126 		case 'l':
127 			host = LNICHOST;
128 			break;
129 		case 'm':
130 			host = MNICHOST;
131 			break;
132 		case 'p':
133 			port = optarg;
134 			break;
135 		case 'Q':
136 			flags |= WHOIS_QUICK;
137 			break;
138 		case 'r':
139 			host = RNICHOST;
140 			break;
141 		case '?':
142 		default:
143 			usage();
144 			/* NOTREACHED */
145 		}
146 	}
147 	argc -= optind;
148 	argv += optind;
149 
150 	if (!argc || (country != NULL && host != NULL))
151 		usage();
152 
153 	/*
154 	 * If no host or country is specified determine the top level domain
155 	 * from the query.  If the TLD is a number, query ARIN.  Otherwise, use
156 	 * TLD.whois-server.net.  If the domain does not contain '.', fall
157 	 * back to NICHOST.
158 	 */
159 	if (host == NULL && country == NULL) {
160 		use_qnichost = 1;
161 		host = NICHOST;
162 		if (!(flags & WHOIS_QUICK))
163 			flags |= WHOIS_RECURSE;
164 	}
165 	while (argc-- > 0) {
166 		if (country != NULL) {
167 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
168 			whois(*argv, qnichost, flags);
169 		} else if (use_qnichost)
170 			if ((qnichost = choose_server(*argv)) != NULL)
171 				whois(*argv, qnichost, flags);
172 		if (qnichost == NULL)
173 			whois(*argv, host, flags);
174 		free(qnichost);
175 		qnichost = NULL;
176 		argv++;
177 	}
178 
179 	return (0);
180 }
181 
182 /*
183  * This function will remove any trailing periods from domain, after which it
184  * returns a pointer to newly allocated memory containing the whois server to
185  * be queried, or a NULL if the correct server couldn't be determined.  The
186  * caller must remember to free(3) the allocated memory.
187  */
188 static char *
189 choose_server(char *domain)
190 {
191 	char *pos, *retval;
192 
193 	if (strchr(domain, ':')) {
194 		s_asprintf(&retval, "%s", ANICHOST);
195 		return (retval);
196 	}
197 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.'; )
198 		*pos = '\0';
199 	if (*domain == '\0')
200 		errx(EX_USAGE, "can't search for a null string");
201 	if (strlen(domain) > sizeof ("-NORID")-1 &&
202 	    strcasecmp(domain + strlen(domain) - sizeof ("-NORID") + 1,
203 	    "-NORID") == 0) {
204 		s_asprintf(&retval, "%s", NORIDHOST);
205 		return (retval);
206 	}
207 	while (pos > domain && *pos != '.')
208 		--pos;
209 	if (pos <= domain)
210 		return (NULL);
211 	if (isdigit((unsigned char)*++pos))
212 		s_asprintf(&retval, "%s", ANICHOST);
213 	else
214 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
215 	return (retval);
216 }
217 
218 static struct addrinfo *
219 gethostinfo(char const *host, int exit_on_error)
220 {
221 	struct addrinfo hints, *res;
222 	int error;
223 
224 	(void) memset(&hints, 0, sizeof (hints));
225 	hints.ai_flags = 0;
226 	hints.ai_family = AF_UNSPEC;
227 	hints.ai_socktype = SOCK_STREAM;
228 	error = getaddrinfo(host, port, &hints, &res);
229 	if (error) {
230 		warnx("%s: %s", host, gai_strerror(error));
231 		if (exit_on_error)
232 			exit(EX_NOHOST);
233 		return (NULL);
234 	}
235 	return (res);
236 }
237 
238 /*
239  * Wrapper for asprintf(3) that exits on error.
240  */
241 /* PRINTFLIKE2 */
242 static void
243 s_asprintf(char **ret, const char *format, ...)
244 {
245 	va_list ap;
246 
247 	va_start(ap, format);
248 	if (vasprintf(ret, format, ap) == -1) {
249 		va_end(ap);
250 		err(EX_OSERR, "vasprintf()");
251 	}
252 	va_end(ap);
253 }
254 
255 static void
256 whois(const char *query, const char *hostname, int flags)
257 {
258 	FILE *sfi, *sfo;
259 	struct addrinfo *hostres, *res;
260 	/*
261 	 * The variables buf and buflen are static so the buffer for
262 	 * getline() is retained across calls.
263 	 */
264 	static char *buf = NULL;
265 	static size_t buflen = 0;
266 	char *host, *nhost, *p;
267 	int i, s;
268 	ssize_t c, len;
269 
270 	s = -1;
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 		(void) 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 		(void) fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query);
290 	} else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
291 		(void) fprintf(sfo, "--show-handles %s\r\n", query);
292 	} else {
293 		(void) fprintf(sfo, "%s\r\n", query);
294 	}
295 	(void) fflush(sfo);
296 	nhost = NULL;
297 	while ((len = getline(&buf, &buflen, sfi)) != -1) {
298 		while (len > 0 && isspace((unsigned char)buf[len - 1]))
299 			buf[--len] = '\0';
300 		(void) printf("%.*s\n", (int)len, buf);
301 
302 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
303 			host = strnstr(buf, WHOIS_SERVER_ID, len);
304 			if (host != NULL) {
305 				host += sizeof (WHOIS_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 ((host =
315 			    strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
316 				host += sizeof (WHOIS_ORG_SERVER_ID) - 1;
317 				for (p = host; p < buf + len; p++) {
318 					if (!ishost(*p)) {
319 						*p = '\0';
320 						break;
321 					}
322 				}
323 				s_asprintf(&nhost, "%.*s",
324 				    (int)(buf + len - host), host);
325 			} else if (strcmp(hostname, ANICHOST) == 0) {
326 				for (c = 0; c <= len; c++)
327 					buf[c] = tolower((unsigned char)buf[c]);
328 				for (i = 0; ip_whois[i] != NULL; i++) {
329 					if (strnstr(buf, ip_whois[i], len) !=
330 					    NULL) {
331 						s_asprintf(&nhost, "%s",
332 						    ip_whois[i]);
333 						break;
334 					}
335 				}
336 			}
337 		}
338 	}
339 	if (nhost != NULL) {
340 		whois(query, nhost, 0);
341 		free(nhost);
342 	}
343 }
344 
345 static void
346 usage(void)
347 {
348 	(void) fprintf(stderr,
349 	    "usage: whois [-aAbfgiIklmQr] [-c country-code | -h hostname] "
350 	    "[-p port] name ...\n");
351 	exit(EX_USAGE);
352 }
353