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