xref: /openbsd/usr.bin/whois/whois.c (revision cee5670f)
1 /*      $OpenBSD: whois.c,v 1.64 2024/03/25 19:11:52 op Exp $   */
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #define	NICHOST		"whois.crsnic.net"
48 #define	INICHOST	"whois.internic.net"
49 #define	DNICHOST	"whois.nic.mil"
50 #define	GNICHOST	"whois.nic.gov"
51 #define	ANICHOST	"whois.arin.net"
52 #define	RNICHOST	"whois.ripe.net"
53 #define	PNICHOST	"whois.apnic.net"
54 #define	RUNICHOST	"whois.ripn.net"
55 #define	MNICHOST	"whois.ra.net"
56 #define LNICHOST	"whois.lacnic.net"
57 #define	AFNICHOST	"whois.afrinic.net"
58 #define BNICHOST	"whois.registro.br"
59 #define	PDBHOST		"whois.peeringdb.com"
60 #define	IANAHOST	"whois.iana.org"
61 #define	QNICHOST_TAIL	".whois-servers.net"
62 
63 #define	WHOIS_PORT	"whois"
64 #define	WHOIS_SERVER_ID	"Registrar WHOIS Server:"
65 
66 #define WHOIS_RECURSE	0x01
67 #define WHOIS_QUICK	0x02
68 #define WHOIS_SPAM_ME	0x04
69 
70 #define CHOPSPAM	">>> Last update of WHOIS database:"
71 
72 const char *port_whois = WHOIS_PORT;
73 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
74     AFNICHOST, NULL };
75 
76 __dead void usage(void);
77 int whois(const char *, const char *, const char *, int);
78 char *choose_server(const char *, const char *, char **);
79 
80 int
main(int argc,char * argv[])81 main(int argc, char *argv[])
82 {
83 	int ch, flags, rval;
84 	char *host, *name, *country;
85 
86 	country = host = NULL;
87 	flags = rval = 0;
88 	while ((ch = getopt(argc, argv, "aAc:dgh:iIlmp:PqQrRS")) != -1)
89 		switch (ch) {
90 		case 'a':
91 			host = ANICHOST;
92 			break;
93 		case 'A':
94 			host = PNICHOST;
95 			break;
96 		case 'c':
97 			country = optarg;
98 			break;
99 		case 'd':
100 			host = DNICHOST;
101 			break;
102 		case 'g':
103 			host = GNICHOST;
104 			break;
105 		case 'h':
106 			host = optarg;
107 			break;
108 		case 'i':
109 			host = INICHOST;
110 			break;
111 		case 'I':
112 			host = IANAHOST;
113 			break;
114 		case 'l':
115 			host = LNICHOST;
116 			break;
117 		case 'm':
118 			host = MNICHOST;
119 			break;
120 		case 'p':
121 			port_whois = optarg;
122 			break;
123 		case 'P':
124 			host = PDBHOST;
125 			break;
126 		case 'q':
127 			/* deprecated, now the default */
128 			break;
129 		case 'Q':
130 			flags |= WHOIS_QUICK;
131 			break;
132 		case 'r':
133 			host = RNICHOST;
134 			break;
135 		case 'R':
136 			host = RUNICHOST;
137 			break;
138 		case 'S':
139 			flags |= WHOIS_SPAM_ME;
140 			break;
141 		default:
142 			usage();
143 		}
144 	argc -= optind;
145 	argv += optind;
146 
147 	if (!argc || (country != NULL && host != NULL))
148 		usage();
149 
150 	if (pledge("stdio dns inet", NULL) == -1)
151 		err(1, "pledge");
152 
153 	if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
154 		flags |= WHOIS_RECURSE;
155 	for (name = *argv; (name = *argv) != NULL; argv++) {
156 		char *tofree = NULL;
157 		const char *server =
158 		    host ? host : choose_server(name, country, &tofree);
159 		rval += whois(name, server, port_whois, flags);
160 		free(tofree);
161 	}
162 	return (rval);
163 }
164 
165 int
whois(const char * query,const char * server,const char * port,int flags)166 whois(const char *query, const char *server, const char *port, int flags)
167 {
168 	FILE *fp;
169 	char *p, *nhost, *buf = NULL;
170 	size_t len, bufsize = 0;
171 	int i, s, error;
172 	const char *reason = NULL, *fmt;
173 	struct addrinfo hints, *res, *ai;
174 
175 	memset(&hints, 0, sizeof(hints));
176 	hints.ai_flags = 0;
177 	hints.ai_family = AF_UNSPEC;
178 	hints.ai_socktype = SOCK_STREAM;
179 	error = getaddrinfo(server, port, &hints, &res);
180 	if (error) {
181 		if (error == EAI_SERVICE)
182 			warnx("%s: bad port", port);
183 		else
184 			warnx("%s: %s", server, gai_strerror(error));
185 		return (1);
186 	}
187 
188 	for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
189 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
190 		if (s == -1) {
191 			error = errno;
192 			reason = "socket";
193 			continue;
194 		}
195 		if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
196 			error = errno;
197 			reason = "connect";
198 			close(s);
199 			s = -1;
200 			continue;
201 		}
202 		break;	/*okay*/
203 	}
204 	freeaddrinfo(res);
205 	if (s == -1) {
206 		if (reason) {
207 			errno = error;
208 			warn("%s: %s", server, reason);
209 		} else
210 			warn("unknown error in connection attempt");
211 		return (1);
212 	}
213 
214 	if (!(flags & WHOIS_SPAM_ME) &&
215 	    (strcmp(server, "whois.denic.de") == 0 ||
216 	    strcmp(server, "de" QNICHOST_TAIL) == 0))
217 		fmt = "-T dn,ace -C ISO-8859-1 %s\r\n";
218 	else if (!(flags & WHOIS_SPAM_ME) &&
219 	    (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
220 	    strcmp(server, "dk" QNICHOST_TAIL) == 0))
221 		fmt = "--show-handles %s\r\n";
222 	else
223 		fmt = "%s\r\n";
224 
225 	fp = fdopen(s, "r+");
226 	if (fp == NULL)
227 		err(1, "fdopen");
228 	fprintf(fp, fmt, query);
229 	fflush(fp);
230 	nhost = NULL;
231 	while ((len = getline(&buf, &bufsize, fp)) != (size_t)-1) {
232 		/* Nominet */
233 		if (!(flags & WHOIS_SPAM_ME) &&
234 		    len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
235 			break;
236 
237 		p = buf + len - 1;
238 		while (p > buf && isspace((unsigned char)*p))
239 			*p-- = '\0';
240 		puts(buf);
241 
242 		if (nhost == NULL && (flags & WHOIS_RECURSE)) {
243 			if ((p = strstr(buf, WHOIS_SERVER_ID))) {
244 				p += sizeof(WHOIS_SERVER_ID) - 1;
245 				while (isblank((unsigned char)*p))
246 					p++;
247 				if ((len = strcspn(p, " \t\n\r"))) {
248 					if ((nhost = strndup(p, len)) == NULL)
249 						err(1, "strndup");
250 				}
251 			} else if (strcmp(server, ANICHOST) == 0) {
252 				for (p = buf; *p != '\0'; p++)
253 					*p = tolower((unsigned char)*p);
254 				for (i = 0; ip_whois[i] != NULL; i++) {
255 					if (strstr(buf, ip_whois[i]) != NULL) {
256 						nhost = strdup(ip_whois[i]);
257 						if (nhost == NULL)
258 							err(1, "strdup");
259 						break;
260 					}
261 				}
262 			}
263 		}
264 
265 		/* Verisign etc. */
266 		if (!(flags & WHOIS_SPAM_ME) &&
267 		    (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
268 		     strncasecmp(buf, &CHOPSPAM[4], sizeof(CHOPSPAM)-5) == 0)) {
269 			printf("\n");
270 			break;
271 		}
272 	}
273 	fclose(fp);
274 	free(buf);
275 
276 	if (nhost != NULL) {
277 		error = whois(query, nhost, port, 0);
278 		free(nhost);
279 	}
280 
281 	return (error);
282 }
283 
284 /*
285  * If no country is specified determine the top level domain from the query.
286  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
287  * If the domain does not contain '.', check to see if it is an ASN (starts
288  * with AS) or IPv6 address (contains ':').
289  * Fall back to NICHOST for the non-handle and non-IPv6 case.
290  */
291 char *
choose_server(const char * name,const char * country,char ** tofree)292 choose_server(const char *name, const char *country, char **tofree)
293 {
294 	char *server;
295 	const char *qhead;
296 	char *ep;
297 	struct addrinfo hints, *res = NULL;
298 
299 	memset(&hints, 0, sizeof(hints));
300 	hints.ai_flags = 0;
301 	hints.ai_family = AF_UNSPEC;
302 	hints.ai_socktype = SOCK_STREAM;
303 
304 	if (country != NULL)
305 		qhead = country;
306 	else if ((qhead = strrchr(name, '.')) == NULL) {
307 		if ((strncasecmp(name, "AS", 2) == 0) &&
308 		    strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
309 			return (MNICHOST);
310 		else if (strchr(name, ':') != NULL) /* IPv6 address */
311 			return (ANICHOST);
312 		else
313 			return (NICHOST);
314 	} else if (isdigit((unsigned char)*(++qhead)))
315 		return (ANICHOST);
316 
317 	/*
318 	 * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
319 	 * (per registry agreement), some older gTLDs also support this...
320 	 */
321 	if (asprintf(&server, "whois.nic.%s", qhead) == -1)
322 		err(1, NULL);
323 
324 	/* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
325 	if ((strlen(qhead) == 2 ||
326 	    /* and is required for most of the <=2003 TLDs/gTLDs */
327 	    strcasecmp(qhead, "org") == 0 ||
328 	    strcasecmp(qhead, "com") == 0 ||
329 	    strcasecmp(qhead, "net") == 0 ||
330 	    strcasecmp(qhead, "cat") == 0 ||
331 	    strcasecmp(qhead, "pro") == 0 ||
332 	    strcasecmp(qhead, "info") == 0 ||
333 	    strcasecmp(qhead, "aero") == 0 ||
334 	    strcasecmp(qhead, "jobs") == 0 ||
335 	    strcasecmp(qhead, "mobi") == 0 ||
336 	    strcasecmp(qhead, "museum") == 0 ||
337 	     /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
338 	    getaddrinfo(server, NULL, &hints, &res) != 0)) {
339 		free(server);
340 		if (asprintf(&server, "%s%s", qhead, QNICHOST_TAIL) == -1)
341 			err(1, NULL);
342 	}
343 	if (res != NULL)
344 		freeaddrinfo(res);
345 
346 	*tofree = server;
347 	return (server);
348 }
349 
350 __dead void
usage(void)351 usage(void)
352 {
353 	extern char *__progname;
354 
355 	fprintf(stderr,
356 	    "usage: %s [-AadgIilmPQRrS] [-c country-code | -h host] "
357 		"[-p port] name ...\n", __progname);
358 	exit(1);
359 }
360