Annotation of src/usr.bin/whois/whois.c, Revision 1.27
1.27 ! deraadt 1: /* $OpenBSD: whois.c,v 1.26 2003/06/03 02:56:23 millert Exp $ */
1.1 deraadt 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.
1.26 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
1.17 millert 33: static const char copyright[] =
1.1 deraadt 34: "@(#) Copyright (c) 1980, 1993\n\
35: The Regents of the University of California. All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
39: #if 0
1.17 millert 40: static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
1.9 millert 41: #else
1.27 ! deraadt 42: static const char rcsid[] = "$OpenBSD: whois.c,v 1.26 2003/06/03 02:56:23 millert Exp $";
1.1 deraadt 43: #endif
44: #endif /* not lint */
45:
46: #include <sys/types.h>
47: #include <sys/socket.h>
1.17 millert 48:
1.1 deraadt 49: #include <netinet/in.h>
1.5 art 50: #include <arpa/inet.h>
1.1 deraadt 51: #include <netdb.h>
1.17 millert 52:
1.18 millert 53: #include <ctype.h>
1.7 millert 54: #include <err.h>
1.1 deraadt 55: #include <stdio.h>
56: #include <stdlib.h>
57: #include <string.h>
58: #include <unistd.h>
59:
1.7 millert 60: #define NICHOST "whois.crsnic.net"
1.22 millert 61: #define INICHOST "whois.networksolutions.com"
1.19 millert 62: #define CNICHOST "whois.corenic.net"
1.7 millert 63: #define DNICHOST "whois.nic.mil"
64: #define GNICHOST "whois.nic.gov"
1.4 deraadt 65: #define ANICHOST "whois.arin.net"
66: #define RNICHOST "whois.ripe.net"
67: #define PNICHOST "whois.apnic.net"
1.7 millert 68: #define RUNICHOST "whois.ripn.net"
1.6 deraadt 69: #define MNICHOST "whois.ra.net"
1.16 fgsch 70: #define LNICHOST "whois.lacnic.net"
1.18 millert 71: #define SNICHOST "whois.6bone.net"
1.22 millert 72: #define BNICHOST "whois.registro.br"
1.6 deraadt 73: #define QNICHOST_TAIL ".whois-servers.net"
1.22 millert 74:
75: #define WHOIS_PORT "whois"
1.24 millert 76: #define WHOIS_SERVER_ID "Whois Server:"
1.1 deraadt 77:
1.8 millert 78: #define WHOIS_RECURSE 0x01
1.22 millert 79: #define WHOIS_QUICK 0x02
80:
81: const char *port = WHOIS_PORT;
82: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
1.8 millert 83:
1.17 millert 84: static __dead void usage(void);
1.22 millert 85: static int whois(const char *, const char *, const char *, int);
1.18 millert 86: static char *choose_server(const char *, const char *);
1.1 deraadt 87:
88: int
1.27 ! deraadt 89: main(int argc, char *argv[])
1.1 deraadt 90: {
1.18 millert 91: int ch, flags, rval;
92: char *host, *name, *country, *server;
1.1 deraadt 93:
1.9 millert 94: #ifdef SOCKS
95: SOCKSinit(argv[0]);
96: #endif
1.18 millert 97: country = host = server = NULL;
98: flags = rval = 0;
1.22 millert 99: while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:qQrR6")) != -1)
1.23 millert 100: switch(ch) {
1.4 deraadt 101: case 'a':
102: host = ANICHOST;
103: break;
1.22 millert 104: case 'A':
105: host = PNICHOST;
106: break;
1.18 millert 107: case 'c':
108: country = optarg;
109: break;
1.4 deraadt 110: case 'd':
111: host = DNICHOST;
112: break;
1.7 millert 113: case 'g':
114: host = GNICHOST;
115: break;
1.1 deraadt 116: case 'h':
117: host = optarg;
118: break;
1.7 millert 119: case 'i':
120: host = INICHOST;
121: break;
1.16 fgsch 122: case 'l':
123: host = LNICHOST;
124: break;
1.6 deraadt 125: case 'm':
126: host = MNICHOST;
127: break;
1.4 deraadt 128: case 'p':
1.22 millert 129: port = optarg;
1.4 deraadt 130: break;
1.6 deraadt 131: case 'q':
1.22 millert 132: /* deprecated, now the default */
1.8 millert 133: break;
134: case 'Q':
135: flags |= WHOIS_QUICK;
1.6 deraadt 136: break;
1.4 deraadt 137: case 'r':
138: host = RNICHOST;
139: break;
1.7 millert 140: case 'R':
141: host = RUNICHOST;
142: break;
1.18 millert 143: case '6':
144: host = SNICHOST;
145: break;
1.1 deraadt 146: default:
147: usage();
148: }
149: argc -= optind;
150: argv += optind;
151:
1.18 millert 152: if (!argc || (country != NULL && host != NULL))
1.1 deraadt 153: usage();
154:
1.18 millert 155: if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22 millert 156: flags |= WHOIS_RECURSE;
1.18 millert 157: for (name = *argv; (name = *argv) != NULL; argv++)
1.22 millert 158: rval += whois(name, host ? host : choose_server(name, country),
159: port, flags);
1.17 millert 160: exit(rval);
1.8 millert 161: }
162:
1.17 millert 163: static int
1.22 millert 164: whois(const char *query, const char *server, const char *port, int flags)
1.8 millert 165: {
166: FILE *sfi, *sfo;
1.12 millert 167: char *buf, *p, *nhost, *nbuf = NULL;
1.8 millert 168: size_t len;
1.22 millert 169: int i, s, error;
1.11 itojun 170: const char *reason = NULL;
1.20 millert 171: struct addrinfo hints, *res, *ai;
1.18 millert 172:
173: memset(&hints, 0, sizeof(hints));
174: hints.ai_flags = 0;
175: hints.ai_family = AF_UNSPEC;
176: hints.ai_socktype = SOCK_STREAM;
1.22 millert 177: error = getaddrinfo(server, port, &hints, &res);
1.18 millert 178: if (error) {
179: warnx("%s: %s", server, gai_strerror(error));
180: return (1);
181: }
1.6 deraadt 182:
1.20 millert 183: for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
184: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.11 itojun 185: if (s < 0) {
186: reason = "socket";
187: continue;
188: }
1.20 millert 189: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1.11 itojun 190: reason = "connect";
191: close(s);
192: s = -1;
193: continue;
194: }
195: break; /*okay*/
196: }
197: if (s < 0) {
198: if (reason)
1.24 millert 199: warn("%s: %s", server, reason);
1.11 itojun 200: else
1.17 millert 201: warn("unknown error in connection attempt");
1.18 millert 202: freeaddrinfo(res);
203: return (1);
1.11 itojun 204: }
1.4 deraadt 205:
1.1 deraadt 206: sfi = fdopen(s, "r");
207: sfo = fdopen(s, "w");
1.4 deraadt 208: if (sfi == NULL || sfo == NULL)
1.18 millert 209: err(1, "fdopen");
1.22 millert 210: (void)fprintf(sfo, "%s\r\n", query);
1.1 deraadt 211: (void)fflush(sfo);
1.8 millert 212: nhost = NULL;
1.22 millert 213: while ((buf = fgetln(sfi, &len)) != NULL) {
214: p = buf + len - 1;
215: if (isspace((unsigned char)*p)) {
216: do
217: *p-- = '\0';
218: while (p != buf && isspace((unsigned char)*p));
219: } else {
1.17 millert 220: if ((nbuf = malloc(len + 1)) == NULL)
221: err(1, "malloc");
1.12 millert 222: memcpy(nbuf, buf, len);
223: nbuf[len] = '\0';
224: buf = nbuf;
225: }
1.22 millert 226: (void)puts(buf);
1.8 millert 227:
1.22 millert 228: if (nhost != NULL || !(flags & WHOIS_RECURSE))
229: continue;
230:
231: if ((p = strstr(buf, WHOIS_SERVER_ID))) {
232: p += sizeof(WHOIS_SERVER_ID) - 1;
1.25 millert 233: while (isblank(*p))
234: p++;
1.8 millert 235: if ((len = strcspn(p, " \t\n\r"))) {
236: if ((nhost = malloc(len + 1)) == NULL)
237: err(1, "malloc");
238: memcpy(nhost, p, len);
239: nhost[len] = '\0';
240: }
1.22 millert 241: } else if (strcmp(server, ANICHOST) == 0) {
242: for (p = buf; *p != '\0'; p++)
243: *p = tolower((unsigned char)*p);
244: for (i = 0; ip_whois[i] != NULL; i++) {
245: if (strstr(buf, ip_whois[i]) != NULL) {
246: nhost = strdup(ip_whois[i]);
1.23 millert 247: if (nhost == NULL)
248: err(1, "strdup");
1.22 millert 249: break;
250: }
251: }
1.8 millert 252: }
253: }
1.18 millert 254: if (nbuf != NULL)
1.12 millert 255: free(nbuf);
1.8 millert 256:
1.22 millert 257: if (nhost != NULL) {
258: error = whois(query, nhost, port, 0);
259: free(nhost);
1.8 millert 260: }
1.18 millert 261: freeaddrinfo(res);
262: return (error);
263: }
264:
265: /*
266: * If no country is specified determine the top level domain from the query.
1.19 millert 267: * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
268: * If the domain does not contain '.', check to see if it is an NSI handle
269: * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
270: * Fall back to NICHOST for the non-handle case.
1.18 millert 271: */
272: static char *
273: choose_server(const char *name, const char *country)
274: {
275: static char *server;
276: const char *qhead;
1.19 millert 277: char *ep;
1.18 millert 278: size_t len;
279:
280: if (country != NULL)
281: qhead = country;
1.19 millert 282: else if ((qhead = strrchr(name, '.')) == NULL) {
283: if (*name == '!')
1.22 millert 284: return (INICHOST);
1.19 millert 285: else if ((strncasecmp(name, "COCO-", 5) == 0 ||
286: strncasecmp(name, "COHO-", 5) == 0) &&
287: strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
288: return (CNICHOST);
289: else
290: return (NICHOST);
291: } else if (isdigit(*(++qhead)))
1.18 millert 292: return (ANICHOST);
293: len = strlen(qhead) + sizeof(QNICHOST_TAIL);
294: if ((server = realloc(server, len)) == NULL)
295: err(1, "realloc");
296: strlcpy(server, qhead, len);
297: strlcat(server, QNICHOST_TAIL, len);
298: return (server);
1.1 deraadt 299: }
300:
1.17 millert 301: static __dead void
302: usage(void)
1.1 deraadt 303: {
1.18 millert 304: extern char *__progname;
1.7 millert 305:
306: (void)fprintf(stderr,
1.23 millert 307: "usage: %s [-aAdgilmQrR6] [-c country-code | -h hostname] "
1.22 millert 308: "[-p port] name ...\n", __progname);
1.18 millert 309: exit(1);
1.1 deraadt 310: }