xref: /freebsd/usr.bin/whois/whois.c (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
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	DEFAULT_PORT	"whois"
73 #define	WHOIS_SERVER_ID	"Whois Server: "
74 #define	NO_MATCH_ID	"No match for \""
75 
76 #define WHOIS_RECURSE		0x01
77 #define WHOIS_INIC_FALLBACK	0x02
78 #define WHOIS_QUICK		0x04
79 
80 const char *ip_whois[] = { RNICHOST, PNICHOST, NULL };
81 const char *port = DEFAULT_PORT;
82 
83 static char *choose_server(char *);
84 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
85 static void s_asprintf(char **ret, const char *format, ...);
86 static void usage(void);
87 static void whois(char *, struct addrinfo *, int);
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	struct addrinfo *res;
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, "aAc:dgh:imp:QrR6")) != -1) {
104 		switch (ch) {
105 		case 'a':
106 			host = ANICHOST;
107 			break;
108 		case 'A':
109 			host = PNICHOST;
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 'm':
127 			host = MNICHOST;
128 			break;
129 		case 'p':
130 			port = optarg;
131 			break;
132 		case 'Q':
133 			flags |= WHOIS_QUICK;
134 			break;
135 		case 'r':
136 			host = RNICHOST;
137 			break;
138 		case 'R':
139 			warnx("-R is deprecated; use '-c ru' instead");
140 			country = "ru";
141 			break;
142 		case '6':
143 			host = SNICHOST;
144 			break;
145 		case '?':
146 		default:
147 			usage();
148 			/* NOTREACHED */
149 		}
150 	}
151 	argc -= optind;
152 	argv += optind;
153 
154 	if (!argc || (country != NULL && host != NULL))
155 		usage();
156 
157 	/*
158 	 * If no host or country is specified determine the top level domain
159 	 * from the query.  If the TLD is a number, query ARIN.  Otherwise, use
160 	 * TLD.whois-server.net.  If the domain does not contain '.', fall
161 	 * back to NICHOST.
162 	 */
163 	if (host == NULL && country == NULL) {
164 		use_qnichost = 1;
165 		host = NICHOST;
166 		if (!(flags & WHOIS_QUICK))
167 			flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE;
168 	}
169 	while (argc--) {
170 		if (country != NULL) {
171 			s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
172 			res = gethostinfo(qnichost, 1);
173 		} else if (use_qnichost)
174 			if ((qnichost = choose_server(*argv)) != NULL)
175 				res = gethostinfo(qnichost, 1);
176 		if (qnichost == NULL)
177 			res = gethostinfo(host, 1);
178 
179 		free(qnichost);
180 		qnichost = NULL;
181 		whois(*argv++, res, flags);
182 		freeaddrinfo(res);
183 	}
184 	exit(0);
185 }
186 
187 /*
188  * This function will remove any trailing periods from domain, after which it
189  * returns a pointer to newly allocated memory containing the whois server to
190  * be queried, or a NULL if the correct server couldn't be determined.  The
191  * caller must remember to free(3) the allocated memory.
192  */
193 static char *
194 choose_server(char *domain)
195 {
196 	char *pos, *retval;
197 
198 	for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
199 		*pos = '\0';
200 	if (*domain == '\0')
201 		errx(EX_USAGE, "can't search for a null string");
202 	while (pos > domain && *pos != '.')
203 		--pos;
204 	if (pos <= domain)
205 		return (NULL);
206 	if (isdigit((unsigned char)*++pos))
207 		s_asprintf(&retval, "%s", ANICHOST);
208 	else
209 		s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
210 	return (retval);
211 }
212 
213 static struct addrinfo *
214 gethostinfo(char const *host, int exit_on_error)
215 {
216 	struct addrinfo hints, *res;
217 	int error;
218 
219 	memset(&hints, 0, sizeof(hints));
220 	hints.ai_flags = 0;
221 	hints.ai_family = AF_UNSPEC;
222 	hints.ai_socktype = SOCK_STREAM;
223 	error = getaddrinfo(host, port, &hints, &res);
224 	if (error) {
225 		warnx("%s: %s", host, gai_strerror(error));
226 		if (exit_on_error)
227 			exit(EX_NOHOST);
228 		return (NULL);
229 	}
230 	return (res);
231 }
232 
233 /*
234  * Wrapper for asprintf(3) that exits on error.
235  */
236 static void
237 s_asprintf(char **ret, const char *format, ...)
238 {
239 	va_list ap;
240 
241 	va_start(ap, format);
242 	if (vasprintf(ret, format, ap) == -1) {
243 		va_end(ap);
244 		err(EX_OSERR, "vasprintf()");
245 	}
246 	va_end(ap);
247 }
248 
249 static void
250 whois(char *name, struct addrinfo *res, int flags)
251 {
252 	FILE *sfi, *sfo;
253 	struct addrinfo *res2;
254 	char *buf, *nhost, *p;
255 	int i, nomatch, s;
256 	size_t len;
257 
258 	for (; res; res = res->ai_next) {
259 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
260 		if (s < 0)
261 			continue;
262 		if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
263 			break;
264 		close(s);
265 	}
266 	if (res == NULL)
267 		err(EX_OSERR, "connect()");
268 
269 	sfi = fdopen(s, "r");
270 	sfo = fdopen(s, "w");
271 	if (sfi == NULL || sfo == NULL)
272 		err(EX_OSERR, "fdopen()");
273 	fprintf(sfo, "%s\r\n", name);
274 	fflush(sfo);
275 	nhost = NULL;
276 	nomatch = 0;
277 	while ((buf = fgetln(sfi, &len)) != NULL) {
278 		while (len && isspace(buf[len - 1]))
279 			buf[--len] = '\0';
280 
281 		if ((flags & WHOIS_RECURSE) && nhost == NULL) {
282 			p = strstr(buf, WHOIS_SERVER_ID);
283 			if (p != NULL) {
284 				p += sizeof(WHOIS_SERVER_ID) - 1;
285 				if ((len = strcspn(p, " \t\n\r")) != 0) {
286 					s_asprintf(&nhost, "%s", p);
287 				}
288 			} else {
289 				for (i = 0; ip_whois[i] != NULL; i++) {
290 					if (strstr(buf, ip_whois[i]) == NULL)
291 						continue;
292 					s_asprintf(&nhost, "%s", ip_whois[i]);
293 				}
294 			}
295 		}
296 
297 		if ((flags & WHOIS_INIC_FALLBACK) && nhost == NULL &&
298 		    !nomatch && (p = strstr(buf, NO_MATCH_ID)) != NULL) {
299 			p += sizeof(NO_MATCH_ID) - 1;
300 			if ((len = strcspn(p, "\"")) &&
301 			    strncasecmp(name, p, len) == 0 &&
302 			    name[len] == '\0' &&
303 			    strchr(name, '.') == NULL)
304 				nomatch = 1;
305 		}
306 		printf("%s\n", buf);
307 	}
308 
309 	/* Do second lookup as needed. */
310 	if (nomatch && nhost == NULL) {
311 		printf("Looking up %s at %s.\n\n", name, INICHOST);
312 		s_asprintf(&nhost, "%s", INICHOST);
313 	}
314 
315 	if (nhost != NULL) {
316 		if ((res2 = gethostinfo(nhost, 0)) == NULL) {
317 			free(nhost);
318 			return;
319 		}
320 		free(nhost);
321 		whois(name, res2, 0);
322 		freeaddrinfo(res2);
323 	}
324 }
325 
326 static void
327 usage(void)
328 {
329 	fprintf(stderr,
330 	    "usage: whois [-adgimpQrR6] [-c country-code | -h hostname] "
331 	    "[-p port] name ...\n");
332 	exit(EX_USAGE);
333 }
334