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