Annotation of src/usr.bin/whois/whois.c, Revision 1.22
1.22 ! millert 1: /* $OpenBSD: whois.c,v 1.21 2003/01/06 20:47:19 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.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.17 millert 37: static const char copyright[] =
1.1 deraadt 38: "@(#) Copyright (c) 1980, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: #if 0
1.17 millert 44: static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
1.9 millert 45: #else
1.22 ! millert 46: static const char rcsid[] = "$OpenBSD: whois.c,v 1.21 2003/01/06 20:47:19 millert Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/socket.h>
1.17 millert 52:
1.1 deraadt 53: #include <netinet/in.h>
1.5 art 54: #include <arpa/inet.h>
1.1 deraadt 55: #include <netdb.h>
1.17 millert 56:
1.18 millert 57: #include <ctype.h>
1.7 millert 58: #include <err.h>
1.1 deraadt 59: #include <stdio.h>
60: #include <stdlib.h>
61: #include <string.h>
62: #include <unistd.h>
63:
1.7 millert 64: #define NICHOST "whois.crsnic.net"
1.22 ! millert 65: #define INICHOST "whois.networksolutions.com"
1.19 millert 66: #define CNICHOST "whois.corenic.net"
1.7 millert 67: #define DNICHOST "whois.nic.mil"
68: #define GNICHOST "whois.nic.gov"
1.4 deraadt 69: #define ANICHOST "whois.arin.net"
70: #define RNICHOST "whois.ripe.net"
71: #define PNICHOST "whois.apnic.net"
1.7 millert 72: #define RUNICHOST "whois.ripn.net"
1.6 deraadt 73: #define MNICHOST "whois.ra.net"
1.16 fgsch 74: #define LNICHOST "whois.lacnic.net"
1.18 millert 75: #define SNICHOST "whois.6bone.net"
1.22 ! millert 76: #define BNICHOST "whois.registro.br"
1.6 deraadt 77: #define QNICHOST_TAIL ".whois-servers.net"
1.22 ! millert 78:
! 79: #define WHOIS_PORT "whois"
! 80: #define WHOIS_SERVER_ID "Whois Server: "
1.1 deraadt 81:
1.8 millert 82: #define WHOIS_RECURSE 0x01
1.22 ! millert 83: #define WHOIS_QUICK 0x02
! 84:
! 85: const char *port = WHOIS_PORT;
! 86: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
1.8 millert 87:
1.17 millert 88: static __dead void usage(void);
1.22 ! millert 89: static int whois(const char *, const char *, const char *, int);
1.18 millert 90: static char *choose_server(const char *, const char *);
1.1 deraadt 91:
92: int
1.17 millert 93: main(int argc, char **argv)
1.1 deraadt 94: {
1.18 millert 95: int ch, flags, rval;
96: char *host, *name, *country, *server;
1.1 deraadt 97:
1.9 millert 98: #ifdef SOCKS
99: SOCKSinit(argv[0]);
100: #endif
1.18 millert 101: country = host = server = NULL;
102: flags = rval = 0;
1.22 ! millert 103: while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:qQrR6")) != -1)
1.1 deraadt 104: switch((char)ch) {
1.4 deraadt 105: case 'a':
106: host = ANICHOST;
107: break;
1.22 ! millert 108: case 'A':
! 109: host = PNICHOST;
! 110: break;
1.18 millert 111: case 'c':
112: country = optarg;
113: break;
1.4 deraadt 114: case 'd':
115: host = DNICHOST;
116: break;
1.7 millert 117: case 'g':
118: host = GNICHOST;
119: break;
1.1 deraadt 120: case 'h':
121: host = optarg;
122: break;
1.7 millert 123: case 'i':
124: host = INICHOST;
125: break;
1.16 fgsch 126: case 'l':
127: host = LNICHOST;
128: break;
1.6 deraadt 129: case 'm':
130: host = MNICHOST;
131: break;
1.4 deraadt 132: case 'p':
1.22 ! millert 133: port = optarg;
1.4 deraadt 134: break;
1.6 deraadt 135: case 'q':
1.22 ! millert 136: /* deprecated, now the default */
1.8 millert 137: break;
138: case 'Q':
139: flags |= WHOIS_QUICK;
1.6 deraadt 140: break;
1.4 deraadt 141: case 'r':
142: host = RNICHOST;
143: break;
1.7 millert 144: case 'R':
145: host = RUNICHOST;
146: break;
1.18 millert 147: case '6':
148: host = SNICHOST;
149: break;
1.1 deraadt 150: default:
151: usage();
152: }
153: argc -= optind;
154: argv += optind;
155:
1.18 millert 156: if (!argc || (country != NULL && host != NULL))
1.1 deraadt 157: usage();
158:
1.18 millert 159: if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22 ! millert 160: flags |= WHOIS_RECURSE;
1.18 millert 161: for (name = *argv; (name = *argv) != NULL; argv++)
1.22 ! millert 162: rval += whois(name, host ? host : choose_server(name, country),
! 163: port, flags);
1.17 millert 164: exit(rval);
1.8 millert 165: }
166:
1.17 millert 167: static int
1.22 ! millert 168: whois(const char *query, const char *server, const char *port, int flags)
1.8 millert 169: {
170: FILE *sfi, *sfo;
1.12 millert 171: char *buf, *p, *nhost, *nbuf = NULL;
1.8 millert 172: size_t len;
1.22 ! millert 173: int i, s, error;
1.11 itojun 174: const char *reason = NULL;
1.20 millert 175: struct addrinfo hints, *res, *ai;
1.18 millert 176:
177: memset(&hints, 0, sizeof(hints));
178: hints.ai_flags = 0;
179: hints.ai_family = AF_UNSPEC;
180: hints.ai_socktype = SOCK_STREAM;
1.22 ! millert 181: error = getaddrinfo(server, port, &hints, &res);
1.18 millert 182: if (error) {
183: warnx("%s: %s", server, gai_strerror(error));
184: return (1);
185: }
1.6 deraadt 186:
1.20 millert 187: for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
188: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.11 itojun 189: if (s < 0) {
190: reason = "socket";
191: continue;
192: }
1.20 millert 193: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1.11 itojun 194: reason = "connect";
195: close(s);
196: s = -1;
197: continue;
198: }
199: break; /*okay*/
200: }
201: if (s < 0) {
202: if (reason)
1.17 millert 203: warn("%s", reason);
1.11 itojun 204: else
1.17 millert 205: warn("unknown error in connection attempt");
1.18 millert 206: freeaddrinfo(res);
207: return (1);
1.11 itojun 208: }
1.4 deraadt 209:
1.1 deraadt 210: sfi = fdopen(s, "r");
211: sfo = fdopen(s, "w");
1.4 deraadt 212: if (sfi == NULL || sfo == NULL)
1.18 millert 213: err(1, "fdopen");
1.22 ! millert 214: (void)fprintf(sfo, "%s\r\n", query);
1.1 deraadt 215: (void)fflush(sfo);
1.8 millert 216: nhost = NULL;
1.22 ! millert 217: while ((buf = fgetln(sfi, &len)) != NULL) {
! 218: p = buf + len - 1;
! 219: if (isspace((unsigned char)*p)) {
! 220: do
! 221: *p-- = '\0';
! 222: while (p != buf && isspace((unsigned char)*p));
! 223: } else {
1.17 millert 224: if ((nbuf = malloc(len + 1)) == NULL)
225: err(1, "malloc");
1.12 millert 226: memcpy(nbuf, buf, len);
227: nbuf[len] = '\0';
228: buf = nbuf;
229: }
1.22 ! millert 230: (void)puts(buf);
1.8 millert 231:
1.22 ! millert 232: if (nhost != NULL || !(flags & WHOIS_RECURSE))
! 233: continue;
! 234:
! 235: if ((p = strstr(buf, WHOIS_SERVER_ID))) {
! 236: p += sizeof(WHOIS_SERVER_ID) - 1;
1.8 millert 237: if ((len = strcspn(p, " \t\n\r"))) {
238: if ((nhost = malloc(len + 1)) == NULL)
239: err(1, "malloc");
240: memcpy(nhost, p, len);
241: nhost[len] = '\0';
242: }
1.22 ! millert 243: } else if (strcmp(server, ANICHOST) == 0) {
! 244: for (p = buf; *p != '\0'; p++)
! 245: *p = tolower((unsigned char)*p);
! 246: for (i = 0; ip_whois[i] != NULL; i++) {
! 247: if (strstr(buf, ip_whois[i]) != NULL) {
! 248: nhost = strdup(ip_whois[i]);
! 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.22 ! millert 307: "usage: %s [-aAdgilmpQrR6] [-c country-code | -h hostname] "
! 308: "[-p port] name ...\n", __progname);
1.18 millert 309: exit(1);
1.1 deraadt 310: }