whois.c revision 1.35 1 1.35 christos /* $NetBSD: whois.c,v 1.35 2011/08/17 13:57:12 christos Exp $ */
2 1.22 jrf /* $OpenBSD: whois.c,v 1.28 2003/09/18 22:16:15 fgsch Exp $ */
3 1.5 jtc
4 1.1 cgd /*
5 1.22 jrf * Copyright (c) 1980, 1993
6 1.22 jrf * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.21 agc * 3. Neither the name of the University nor the names of its contributors
17 1.1 cgd * may be used to endorse or promote products derived from this software
18 1.1 cgd * without specific prior written permission.
19 1.1 cgd *
20 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 cgd * SUCH DAMAGE.
31 1.1 cgd */
32 1.1 cgd
33 1.23 christos #include <sys/cdefs.h>
34 1.23 christos
35 1.1 cgd #ifndef lint
36 1.33 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
37 1.33 lukem The Regents of the University of California. All rights reserved.");
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.5 jtc #if 0
42 1.22 jrf static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
43 1.6 mrg #else
44 1.35 christos __RCSID("$NetBSD: whois.c,v 1.35 2011/08/17 13:57:12 christos Exp $");
45 1.5 jtc #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #include <sys/types.h>
49 1.1 cgd #include <sys/socket.h>
50 1.22 jrf
51 1.1 cgd #include <netinet/in.h>
52 1.22 jrf #include <arpa/inet.h>
53 1.22 jrf #include <netdb.h>
54 1.22 jrf
55 1.22 jrf #include <ctype.h>
56 1.7 lukem #include <err.h>
57 1.1 cgd #include <stdio.h>
58 1.4 jtc #include <stdlib.h>
59 1.4 jtc #include <string.h>
60 1.4 jtc #include <unistd.h>
61 1.23 christos #include <errno.h>
62 1.9 tron
63 1.31 christos #define ANICHOST "whois.arin.net"
64 1.31 christos #define BNICHOST "whois.registro.br"
65 1.22 jrf #define CNICHOST "whois.corenic.net"
66 1.22 jrf #define DNICHOST "whois.nic.mil"
67 1.31 christos #define FNICHOST "whois.afrinic.net"
68 1.22 jrf #define GNICHOST "whois.nic.gov"
69 1.31 christos #define INICHOST "whois.networksolutions.com"
70 1.31 christos #define LNICHOST "whois.lacnic.net"
71 1.31 christos #define MNICHOST "whois.ra.net"
72 1.31 christos #define NICHOST "whois.crsnic.net"
73 1.31 christos #define PNICHOST "whois.apnic.net"
74 1.31 christos #define QNICHOST_TAIL ".whois-servers.net"
75 1.22 jrf #define RNICHOST "whois.ripe.net"
76 1.22 jrf #define RUNICHOST "whois.ripn.net"
77 1.31 christos #define SNICHOST "whois.6bone.net"
78 1.22 jrf
79 1.22 jrf #define WHOIS_PORT "whois"
80 1.22 jrf #define WHOIS_SERVER_ID "Whois Server:"
81 1.22 jrf
82 1.22 jrf #define WHOIS_RECURSE 0x01
83 1.22 jrf #define WHOIS_QUICK 0x02
84 1.22 jrf
85 1.23 christos static const char *port_whois = WHOIS_PORT;
86 1.23 christos static const char *ip_whois[] =
87 1.31 christos { LNICHOST, RNICHOST, PNICHOST, FNICHOST, BNICHOST, NULL };
88 1.22 jrf
89 1.32 perry static void usage(void) __dead;
90 1.22 jrf static int whois(const char *, const char *, const char *, int);
91 1.34 lukem static const char *choose_server(const char *, const char *);
92 1.9 tron
93 1.22 jrf int
94 1.22 jrf main(int argc, char *argv[])
95 1.22 jrf {
96 1.22 jrf int ch, flags, rval;
97 1.34 lukem const char *host, *name, *country;
98 1.9 tron
99 1.22 jrf #ifdef SOCKS
100 1.22 jrf SOCKSinit(argv[0]);
101 1.9 tron #endif
102 1.29 liamjfoy country = host = NULL;
103 1.22 jrf flags = rval = 0;
104 1.31 christos while ((ch = getopt(argc, argv, "6Aac:dfgh:ilmp:qQRr")) != -1)
105 1.22 jrf switch(ch) {
106 1.22 jrf case 'a':
107 1.22 jrf host = ANICHOST;
108 1.22 jrf break;
109 1.22 jrf case 'A':
110 1.22 jrf host = PNICHOST;
111 1.22 jrf break;
112 1.22 jrf case 'c':
113 1.22 jrf country = optarg;
114 1.22 jrf break;
115 1.22 jrf case 'd':
116 1.22 jrf host = DNICHOST;
117 1.22 jrf break;
118 1.31 christos case 'f':
119 1.31 christos host = FNICHOST;
120 1.31 christos break;
121 1.22 jrf case 'g':
122 1.22 jrf host = GNICHOST;
123 1.22 jrf break;
124 1.22 jrf case 'h':
125 1.22 jrf host = optarg;
126 1.22 jrf break;
127 1.22 jrf case 'i':
128 1.22 jrf host = INICHOST;
129 1.22 jrf break;
130 1.22 jrf case 'l':
131 1.22 jrf host = LNICHOST;
132 1.22 jrf break;
133 1.22 jrf case 'm':
134 1.22 jrf host = MNICHOST;
135 1.22 jrf break;
136 1.22 jrf case 'p':
137 1.23 christos port_whois = optarg;
138 1.22 jrf break;
139 1.22 jrf case 'q':
140 1.22 jrf /* deprecated, now the default */
141 1.22 jrf break;
142 1.22 jrf case 'Q':
143 1.22 jrf flags |= WHOIS_QUICK;
144 1.22 jrf break;
145 1.22 jrf case 'r':
146 1.22 jrf host = RNICHOST;
147 1.22 jrf break;
148 1.22 jrf case 'R':
149 1.22 jrf host = RUNICHOST;
150 1.22 jrf break;
151 1.22 jrf case '6':
152 1.22 jrf host = SNICHOST;
153 1.22 jrf break;
154 1.22 jrf default:
155 1.22 jrf usage();
156 1.22 jrf }
157 1.22 jrf argc -= optind;
158 1.22 jrf argv += optind;
159 1.22 jrf
160 1.22 jrf if (!argc || (country != NULL && host != NULL))
161 1.22 jrf usage();
162 1.22 jrf
163 1.22 jrf if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
164 1.22 jrf flags |= WHOIS_RECURSE;
165 1.22 jrf for (name = *argv; (name = *argv) != NULL; argv++)
166 1.22 jrf rval += whois(name, host ? host : choose_server(name, country),
167 1.23 christos port_whois, flags);
168 1.23 christos return rval;
169 1.9 tron }
170 1.4 jtc
171 1.22 jrf static int
172 1.22 jrf whois(const char *query, const char *server, const char *port, int flags)
173 1.9 tron {
174 1.22 jrf FILE *sfi, *sfo;
175 1.22 jrf char *buf, *p, *nhost, *nbuf = NULL;
176 1.22 jrf size_t len;
177 1.22 jrf int i, s, error;
178 1.27 christos const char *reason = NULL, *fmt;
179 1.22 jrf struct addrinfo hints, *res, *ai;
180 1.22 jrf
181 1.23 christos (void)memset(&hints, 0, sizeof(hints));
182 1.22 jrf hints.ai_flags = 0;
183 1.22 jrf hints.ai_family = AF_UNSPEC;
184 1.22 jrf hints.ai_socktype = SOCK_STREAM;
185 1.22 jrf error = getaddrinfo(server, port, &hints, &res);
186 1.22 jrf if (error) {
187 1.22 jrf warnx("%s: %s", server, gai_strerror(error));
188 1.22 jrf return (1);
189 1.22 jrf }
190 1.9 tron
191 1.22 jrf for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
192 1.22 jrf s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
193 1.22 jrf if (s < 0) {
194 1.23 christos error = errno;
195 1.22 jrf reason = "socket";
196 1.22 jrf continue;
197 1.22 jrf }
198 1.22 jrf if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
199 1.23 christos error = errno;
200 1.22 jrf reason = "connect";
201 1.22 jrf close(s);
202 1.22 jrf s = -1;
203 1.22 jrf continue;
204 1.22 jrf }
205 1.22 jrf break; /*okay*/
206 1.22 jrf }
207 1.22 jrf if (s < 0) {
208 1.23 christos if (reason) {
209 1.23 christos errno = error;
210 1.22 jrf warn("%s: %s", server, reason);
211 1.23 christos } else
212 1.23 christos warnx("Unknown error in connection attempt");
213 1.22 jrf freeaddrinfo(res);
214 1.22 jrf return (1);
215 1.22 jrf }
216 1.9 tron
217 1.27 christos if (strcmp(server, "whois.denic.de") == 0 ||
218 1.27 christos strcmp(server, "de.whois-servers.net") == 0)
219 1.35 christos fmt = "-T dn,ace -C ISO-8859-1";
220 1.27 christos else
221 1.35 christos fmt = "";
222 1.27 christos
223 1.22 jrf sfi = fdopen(s, "r");
224 1.22 jrf sfo = fdopen(s, "w");
225 1.22 jrf if (sfi == NULL || sfo == NULL)
226 1.22 jrf err(1, "fdopen");
227 1.35 christos (void)fprintf(sfo, "%s%s\r\n", fmt, query);
228 1.22 jrf (void)fflush(sfo);
229 1.22 jrf nhost = NULL;
230 1.22 jrf while ((buf = fgetln(sfi, &len)) != NULL) {
231 1.22 jrf p = buf + len - 1;
232 1.22 jrf if (isspace((unsigned char)*p)) {
233 1.22 jrf do
234 1.22 jrf *p = '\0';
235 1.22 jrf while (p > buf && isspace((unsigned char)*--p));
236 1.22 jrf } else {
237 1.22 jrf if ((nbuf = malloc(len + 1)) == NULL)
238 1.22 jrf err(1, "malloc");
239 1.23 christos (void)memcpy(nbuf, buf, len);
240 1.22 jrf nbuf[len] = '\0';
241 1.22 jrf buf = nbuf;
242 1.22 jrf }
243 1.22 jrf (void)puts(buf);
244 1.22 jrf
245 1.22 jrf if (nhost != NULL || !(flags & WHOIS_RECURSE))
246 1.22 jrf continue;
247 1.22 jrf
248 1.22 jrf if ((p = strstr(buf, WHOIS_SERVER_ID))) {
249 1.22 jrf p += sizeof(WHOIS_SERVER_ID) - 1;
250 1.23 christos while (isblank((unsigned char)*p))
251 1.22 jrf p++;
252 1.22 jrf if ((len = strcspn(p, " \t\n\r"))) {
253 1.22 jrf if ((nhost = malloc(len + 1)) == NULL)
254 1.22 jrf err(1, "malloc");
255 1.23 christos (void)memcpy(nhost, p, len);
256 1.22 jrf nhost[len] = '\0';
257 1.22 jrf }
258 1.22 jrf } else if (strcmp(server, ANICHOST) == 0) {
259 1.22 jrf for (p = buf; *p != '\0'; p++)
260 1.22 jrf *p = tolower((unsigned char)*p);
261 1.22 jrf for (i = 0; ip_whois[i] != NULL; i++) {
262 1.22 jrf if (strstr(buf, ip_whois[i]) != NULL) {
263 1.22 jrf nhost = strdup(ip_whois[i]);
264 1.22 jrf if (nhost == NULL)
265 1.22 jrf err(1, "strdup");
266 1.22 jrf break;
267 1.22 jrf }
268 1.22 jrf }
269 1.22 jrf }
270 1.22 jrf }
271 1.22 jrf if (nbuf != NULL)
272 1.22 jrf free(nbuf);
273 1.9 tron
274 1.22 jrf if (nhost != NULL) {
275 1.22 jrf error = whois(query, nhost, port, 0);
276 1.22 jrf free(nhost);
277 1.22 jrf }
278 1.22 jrf freeaddrinfo(res);
279 1.30 christos (void)fclose(sfi);
280 1.30 christos (void)fclose(sfo);
281 1.22 jrf return (error);
282 1.9 tron }
283 1.9 tron
284 1.22 jrf /*
285 1.22 jrf * If no country is specified determine the top level domain from the query.
286 1.22 jrf * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
287 1.22 jrf * If the domain does not contain '.', check to see if it is an NSI handle
288 1.22 jrf * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
289 1.22 jrf * Fall back to NICHOST for the non-handle case.
290 1.22 jrf */
291 1.34 lukem static const char *
292 1.22 jrf choose_server(const char *name, const char *country)
293 1.1 cgd {
294 1.22 jrf static char *server;
295 1.25 itojun char *nserver;
296 1.22 jrf const char *qhead;
297 1.22 jrf char *ep;
298 1.22 jrf size_t len;
299 1.22 jrf
300 1.22 jrf if (country != NULL)
301 1.22 jrf qhead = country;
302 1.22 jrf else if ((qhead = strrchr(name, '.')) == NULL) {
303 1.22 jrf if (*name == '!')
304 1.22 jrf return (INICHOST);
305 1.22 jrf else if ((strncasecmp(name, "COCO-", 5) == 0 ||
306 1.22 jrf strncasecmp(name, "COHO-", 5) == 0) &&
307 1.22 jrf strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
308 1.22 jrf return (CNICHOST);
309 1.22 jrf else
310 1.22 jrf return (NICHOST);
311 1.23 christos } else if (isdigit((unsigned char)*(++qhead)))
312 1.22 jrf return (ANICHOST);
313 1.22 jrf len = strlen(qhead) + sizeof(QNICHOST_TAIL);
314 1.25 itojun if ((nserver = realloc(server, len)) == NULL)
315 1.22 jrf err(1, "realloc");
316 1.25 itojun server = nserver;
317 1.23 christos (void)strlcpy(server, qhead, len);
318 1.23 christos (void)strlcat(server, QNICHOST_TAIL, len);
319 1.22 jrf return (server);
320 1.22 jrf }
321 1.9 tron
322 1.23 christos static void
323 1.22 jrf usage(void)
324 1.1 cgd {
325 1.22 jrf (void)fprintf(stderr,
326 1.26 jmmv "usage: %s [-6AadgilmQRr] [-c country-code | -h hostname] "
327 1.23 christos "[-p port] name ...\n", getprogname());
328 1.22 jrf exit(1);
329 1.1 cgd }
330