whois.c revision 1.29 1 1.29 liamjfoy /* $NetBSD: whois.c,v 1.29 2006/04/30 20:23:02 liamjfoy 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.6 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
37 1.6 mrg The Regents of the University of California. All rights reserved.\n");
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.29 liamjfoy __RCSID("$NetBSD: whois.c,v 1.29 2006/04/30 20:23:02 liamjfoy 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.22 jrf #define NICHOST "whois.crsnic.net"
64 1.22 jrf #define INICHOST "whois.networksolutions.com"
65 1.22 jrf #define CNICHOST "whois.corenic.net"
66 1.22 jrf #define DNICHOST "whois.nic.mil"
67 1.22 jrf #define GNICHOST "whois.nic.gov"
68 1.22 jrf #define ANICHOST "whois.arin.net"
69 1.22 jrf #define RNICHOST "whois.ripe.net"
70 1.22 jrf #define PNICHOST "whois.apnic.net"
71 1.22 jrf #define RUNICHOST "whois.ripn.net"
72 1.22 jrf #define MNICHOST "whois.ra.net"
73 1.22 jrf #define LNICHOST "whois.lacnic.net"
74 1.22 jrf #define SNICHOST "whois.6bone.net"
75 1.22 jrf #define BNICHOST "whois.registro.br"
76 1.22 jrf #define QNICHOST_TAIL ".whois-servers.net"
77 1.22 jrf
78 1.22 jrf #define WHOIS_PORT "whois"
79 1.22 jrf #define WHOIS_SERVER_ID "Whois Server:"
80 1.22 jrf
81 1.22 jrf #define WHOIS_RECURSE 0x01
82 1.22 jrf #define WHOIS_QUICK 0x02
83 1.22 jrf
84 1.23 christos static const char *port_whois = WHOIS_PORT;
85 1.23 christos static const char *ip_whois[] =
86 1.23 christos { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
87 1.22 jrf
88 1.23 christos static void usage(void) __attribute__((__noreturn__));
89 1.22 jrf static int whois(const char *, const char *, const char *, int);
90 1.22 jrf static char *choose_server(const char *, const char *);
91 1.9 tron
92 1.22 jrf int
93 1.22 jrf main(int argc, char *argv[])
94 1.22 jrf {
95 1.22 jrf int ch, flags, rval;
96 1.29 liamjfoy char *host, *name, *country;
97 1.9 tron
98 1.22 jrf #ifdef SOCKS
99 1.22 jrf SOCKSinit(argv[0]);
100 1.9 tron #endif
101 1.29 liamjfoy country = host = NULL;
102 1.22 jrf flags = rval = 0;
103 1.24 wiz while ((ch = getopt(argc, argv, "6Aac:dgh:ilmp:qQRr")) != -1)
104 1.22 jrf switch(ch) {
105 1.22 jrf case 'a':
106 1.22 jrf host = ANICHOST;
107 1.22 jrf break;
108 1.22 jrf case 'A':
109 1.22 jrf host = PNICHOST;
110 1.22 jrf break;
111 1.22 jrf case 'c':
112 1.22 jrf country = optarg;
113 1.22 jrf break;
114 1.22 jrf case 'd':
115 1.22 jrf host = DNICHOST;
116 1.22 jrf break;
117 1.22 jrf case 'g':
118 1.22 jrf host = GNICHOST;
119 1.22 jrf break;
120 1.22 jrf case 'h':
121 1.22 jrf host = optarg;
122 1.22 jrf break;
123 1.22 jrf case 'i':
124 1.22 jrf host = INICHOST;
125 1.22 jrf break;
126 1.22 jrf case 'l':
127 1.22 jrf host = LNICHOST;
128 1.22 jrf break;
129 1.22 jrf case 'm':
130 1.22 jrf host = MNICHOST;
131 1.22 jrf break;
132 1.22 jrf case 'p':
133 1.23 christos port_whois = optarg;
134 1.22 jrf break;
135 1.22 jrf case 'q':
136 1.22 jrf /* deprecated, now the default */
137 1.22 jrf break;
138 1.22 jrf case 'Q':
139 1.22 jrf flags |= WHOIS_QUICK;
140 1.22 jrf break;
141 1.22 jrf case 'r':
142 1.22 jrf host = RNICHOST;
143 1.22 jrf break;
144 1.22 jrf case 'R':
145 1.22 jrf host = RUNICHOST;
146 1.22 jrf break;
147 1.22 jrf case '6':
148 1.22 jrf host = SNICHOST;
149 1.22 jrf break;
150 1.22 jrf default:
151 1.22 jrf usage();
152 1.22 jrf }
153 1.22 jrf argc -= optind;
154 1.22 jrf argv += optind;
155 1.22 jrf
156 1.22 jrf if (!argc || (country != NULL && host != NULL))
157 1.22 jrf usage();
158 1.22 jrf
159 1.22 jrf if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
160 1.22 jrf flags |= WHOIS_RECURSE;
161 1.22 jrf for (name = *argv; (name = *argv) != NULL; argv++)
162 1.22 jrf rval += whois(name, host ? host : choose_server(name, country),
163 1.23 christos port_whois, flags);
164 1.23 christos return rval;
165 1.9 tron }
166 1.4 jtc
167 1.22 jrf static int
168 1.22 jrf whois(const char *query, const char *server, const char *port, int flags)
169 1.9 tron {
170 1.22 jrf FILE *sfi, *sfo;
171 1.22 jrf char *buf, *p, *nhost, *nbuf = NULL;
172 1.22 jrf size_t len;
173 1.22 jrf int i, s, error;
174 1.27 christos const char *reason = NULL, *fmt;
175 1.22 jrf struct addrinfo hints, *res, *ai;
176 1.22 jrf
177 1.23 christos (void)memset(&hints, 0, sizeof(hints));
178 1.22 jrf hints.ai_flags = 0;
179 1.22 jrf hints.ai_family = AF_UNSPEC;
180 1.22 jrf hints.ai_socktype = SOCK_STREAM;
181 1.22 jrf error = getaddrinfo(server, port, &hints, &res);
182 1.22 jrf if (error) {
183 1.22 jrf warnx("%s: %s", server, gai_strerror(error));
184 1.22 jrf return (1);
185 1.22 jrf }
186 1.9 tron
187 1.22 jrf for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
188 1.22 jrf s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
189 1.22 jrf if (s < 0) {
190 1.23 christos error = errno;
191 1.22 jrf reason = "socket";
192 1.22 jrf continue;
193 1.22 jrf }
194 1.22 jrf if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
195 1.23 christos error = errno;
196 1.22 jrf reason = "connect";
197 1.22 jrf close(s);
198 1.22 jrf s = -1;
199 1.22 jrf continue;
200 1.22 jrf }
201 1.22 jrf break; /*okay*/
202 1.22 jrf }
203 1.22 jrf if (s < 0) {
204 1.23 christos if (reason) {
205 1.23 christos errno = error;
206 1.22 jrf warn("%s: %s", server, reason);
207 1.23 christos } else
208 1.23 christos warnx("Unknown error in connection attempt");
209 1.22 jrf freeaddrinfo(res);
210 1.22 jrf return (1);
211 1.22 jrf }
212 1.9 tron
213 1.27 christos if (strcmp(server, "whois.denic.de") == 0 ||
214 1.27 christos strcmp(server, "de.whois-servers.net") == 0)
215 1.28 christos fmt = "-T dn,ace -C ISO-8859-1 %s\r\n";
216 1.27 christos else
217 1.27 christos fmt = "%s\r\n";
218 1.27 christos
219 1.22 jrf sfi = fdopen(s, "r");
220 1.22 jrf sfo = fdopen(s, "w");
221 1.22 jrf if (sfi == NULL || sfo == NULL)
222 1.22 jrf err(1, "fdopen");
223 1.27 christos (void)fprintf(sfo, fmt, query);
224 1.22 jrf (void)fflush(sfo);
225 1.22 jrf nhost = NULL;
226 1.22 jrf while ((buf = fgetln(sfi, &len)) != NULL) {
227 1.22 jrf p = buf + len - 1;
228 1.22 jrf if (isspace((unsigned char)*p)) {
229 1.22 jrf do
230 1.22 jrf *p = '\0';
231 1.22 jrf while (p > buf && isspace((unsigned char)*--p));
232 1.22 jrf } else {
233 1.22 jrf if ((nbuf = malloc(len + 1)) == NULL)
234 1.22 jrf err(1, "malloc");
235 1.23 christos (void)memcpy(nbuf, buf, len);
236 1.22 jrf nbuf[len] = '\0';
237 1.22 jrf buf = nbuf;
238 1.22 jrf }
239 1.22 jrf (void)puts(buf);
240 1.22 jrf
241 1.22 jrf if (nhost != NULL || !(flags & WHOIS_RECURSE))
242 1.22 jrf continue;
243 1.22 jrf
244 1.22 jrf if ((p = strstr(buf, WHOIS_SERVER_ID))) {
245 1.22 jrf p += sizeof(WHOIS_SERVER_ID) - 1;
246 1.23 christos while (isblank((unsigned char)*p))
247 1.22 jrf p++;
248 1.22 jrf if ((len = strcspn(p, " \t\n\r"))) {
249 1.22 jrf if ((nhost = malloc(len + 1)) == NULL)
250 1.22 jrf err(1, "malloc");
251 1.23 christos (void)memcpy(nhost, p, len);
252 1.22 jrf nhost[len] = '\0';
253 1.22 jrf }
254 1.22 jrf } else if (strcmp(server, ANICHOST) == 0) {
255 1.22 jrf for (p = buf; *p != '\0'; p++)
256 1.22 jrf *p = tolower((unsigned char)*p);
257 1.22 jrf for (i = 0; ip_whois[i] != NULL; i++) {
258 1.22 jrf if (strstr(buf, ip_whois[i]) != NULL) {
259 1.22 jrf nhost = strdup(ip_whois[i]);
260 1.22 jrf if (nhost == NULL)
261 1.22 jrf err(1, "strdup");
262 1.22 jrf break;
263 1.22 jrf }
264 1.22 jrf }
265 1.22 jrf }
266 1.22 jrf }
267 1.22 jrf if (nbuf != NULL)
268 1.22 jrf free(nbuf);
269 1.9 tron
270 1.22 jrf if (nhost != NULL) {
271 1.22 jrf error = whois(query, nhost, port, 0);
272 1.22 jrf free(nhost);
273 1.22 jrf }
274 1.22 jrf freeaddrinfo(res);
275 1.22 jrf return (error);
276 1.9 tron }
277 1.9 tron
278 1.22 jrf /*
279 1.22 jrf * If no country is specified determine the top level domain from the query.
280 1.22 jrf * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
281 1.22 jrf * If the domain does not contain '.', check to see if it is an NSI handle
282 1.22 jrf * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
283 1.22 jrf * Fall back to NICHOST for the non-handle case.
284 1.22 jrf */
285 1.22 jrf static char *
286 1.22 jrf choose_server(const char *name, const char *country)
287 1.1 cgd {
288 1.22 jrf static char *server;
289 1.25 itojun char *nserver;
290 1.22 jrf const char *qhead;
291 1.22 jrf char *ep;
292 1.22 jrf size_t len;
293 1.22 jrf
294 1.22 jrf if (country != NULL)
295 1.22 jrf qhead = country;
296 1.22 jrf else if ((qhead = strrchr(name, '.')) == NULL) {
297 1.22 jrf if (*name == '!')
298 1.22 jrf return (INICHOST);
299 1.22 jrf else if ((strncasecmp(name, "COCO-", 5) == 0 ||
300 1.22 jrf strncasecmp(name, "COHO-", 5) == 0) &&
301 1.22 jrf strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
302 1.22 jrf return (CNICHOST);
303 1.22 jrf else
304 1.22 jrf return (NICHOST);
305 1.23 christos } else if (isdigit((unsigned char)*(++qhead)))
306 1.22 jrf return (ANICHOST);
307 1.22 jrf len = strlen(qhead) + sizeof(QNICHOST_TAIL);
308 1.25 itojun if ((nserver = realloc(server, len)) == NULL)
309 1.22 jrf err(1, "realloc");
310 1.25 itojun server = nserver;
311 1.23 christos (void)strlcpy(server, qhead, len);
312 1.23 christos (void)strlcat(server, QNICHOST_TAIL, len);
313 1.22 jrf return (server);
314 1.22 jrf }
315 1.9 tron
316 1.23 christos static void
317 1.22 jrf usage(void)
318 1.1 cgd {
319 1.22 jrf (void)fprintf(stderr,
320 1.26 jmmv "usage: %s [-6AadgilmQRr] [-c country-code | -h hostname] "
321 1.23 christos "[-p port] name ...\n", getprogname());
322 1.22 jrf exit(1);
323 1.1 cgd }
324