Annotation of src/usr.bin/whois/whois.c, Revision 1.21
1.21 ! millert 1: /* $OpenBSD: whois.c,v 1.20 2003/01/05 00:27:55 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.21 ! millert 46: static const char rcsid[] = "$OpenBSD: whois.c,v 1.20 2003/01/05 00:27:55 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"
65: #define INICHOST "whois.internic.net"
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.21 ! millert 74: #define NNICHOST "whois.networksolutions.com"
1.16 fgsch 75: #define LNICHOST "whois.lacnic.net"
1.18 millert 76: #define SNICHOST "whois.6bone.net"
1.6 deraadt 77: #define QNICHOST_TAIL ".whois-servers.net"
1.4 deraadt 78: #define WHOIS_PORT 43
1.1 deraadt 79:
1.8 millert 80: #define WHOIS_RECURSE 0x01
81: #define WHOIS_INIC_FALLBACK 0x02
82: #define WHOIS_QUICK 0x04
83:
1.17 millert 84: static __dead void usage(void);
1.18 millert 85: static int whois(const char *, const char *, int);
86: static char *choose_server(const char *, const char *);
1.1 deraadt 87:
88: int
1.17 millert 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;
99: while ((ch = getopt(argc, argv, "ac:dgh:ilmpqQrR6")) != -1)
1.1 deraadt 100: switch((char)ch) {
1.4 deraadt 101: case 'a':
102: host = ANICHOST;
103: break;
1.18 millert 104: case 'c':
105: country = optarg;
106: break;
1.4 deraadt 107: case 'd':
108: host = DNICHOST;
109: break;
1.7 millert 110: case 'g':
111: host = GNICHOST;
112: break;
1.1 deraadt 113: case 'h':
114: host = optarg;
115: break;
1.7 millert 116: case 'i':
117: host = INICHOST;
118: break;
1.16 fgsch 119: case 'l':
120: host = LNICHOST;
121: break;
1.6 deraadt 122: case 'm':
123: host = MNICHOST;
124: break;
1.4 deraadt 125: case 'p':
126: host = PNICHOST;
127: break;
1.6 deraadt 128: case 'q':
1.8 millert 129: /* default */
130: break;
131: case 'Q':
132: flags |= WHOIS_QUICK;
1.6 deraadt 133: break;
1.4 deraadt 134: case 'r':
135: host = RNICHOST;
136: break;
1.7 millert 137: case 'R':
138: host = RUNICHOST;
139: break;
1.18 millert 140: case '6':
141: host = SNICHOST;
142: break;
1.1 deraadt 143: default:
144: usage();
145: }
146: argc -= optind;
147: argv += optind;
148:
1.18 millert 149: if (!argc || (country != NULL && host != NULL))
1.1 deraadt 150: usage();
151:
1.18 millert 152: if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
153: flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE;
154: for (name = *argv; (name = *argv) != NULL; argv++)
155: rval += whois(name, host ? host : choose_server(name, country), flags);
1.17 millert 156: exit(rval);
1.8 millert 157: }
158:
1.17 millert 159: static int
1.18 millert 160: whois(const char *name, const char *server, int flags)
1.8 millert 161: {
162: FILE *sfi, *sfo;
1.12 millert 163: char *buf, *p, *nhost, *nbuf = NULL;
1.8 millert 164: size_t len;
1.18 millert 165: int s, nomatch, error;
1.11 itojun 166: const char *reason = NULL;
1.20 millert 167: struct addrinfo hints, *res, *ai;
1.18 millert 168:
169: memset(&hints, 0, sizeof(hints));
170: hints.ai_flags = 0;
171: hints.ai_family = AF_UNSPEC;
172: hints.ai_socktype = SOCK_STREAM;
173: error = getaddrinfo(server, "whois", &hints, &res);
174: if (error) {
175: warnx("%s: %s", server, gai_strerror(error));
176: return (1);
177: }
1.6 deraadt 178:
1.20 millert 179: for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
180: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.11 itojun 181: if (s < 0) {
182: reason = "socket";
183: continue;
184: }
1.20 millert 185: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1.11 itojun 186: reason = "connect";
187: close(s);
188: s = -1;
189: continue;
190: }
1.4 deraadt 191:
1.11 itojun 192: break; /*okay*/
193: }
194: if (s < 0) {
195: if (reason)
1.17 millert 196: warn("%s", reason);
1.11 itojun 197: else
1.17 millert 198: warn("unknown error in connection attempt");
1.18 millert 199: freeaddrinfo(res);
200: return (1);
1.11 itojun 201: }
1.4 deraadt 202:
1.1 deraadt 203: sfi = fdopen(s, "r");
204: sfo = fdopen(s, "w");
1.4 deraadt 205: if (sfi == NULL || sfo == NULL)
1.18 millert 206: err(1, "fdopen");
1.8 millert 207: (void)fprintf(sfo, "%s\r\n", name);
1.1 deraadt 208: (void)fflush(sfo);
1.8 millert 209: nhost = NULL;
210: nomatch = 0;
211: while ((buf = fgetln(sfi, &len))) {
212: if (buf[len - 2] == '\r')
213: buf[len - 2] = '\0';
1.12 millert 214: else if (buf[len - 1] == '\n')
1.8 millert 215: buf[len - 1] = '\0';
1.12 millert 216: else {
1.17 millert 217: if ((nbuf = malloc(len + 1)) == NULL)
218: err(1, "malloc");
1.12 millert 219: memcpy(nbuf, buf, len);
220: nbuf[len] = '\0';
221: buf = nbuf;
222: }
1.8 millert 223:
1.18 millert 224: if ((flags & WHOIS_RECURSE) && nhost == NULL &&
1.8 millert 225: (p = strstr(buf, "Whois Server: "))) {
226: p += sizeof("Whois Server: ") - 1;
227: if ((len = strcspn(p, " \t\n\r"))) {
228: if ((nhost = malloc(len + 1)) == NULL)
229: err(1, "malloc");
230: memcpy(nhost, p, len);
231: nhost[len] = '\0';
232: }
233: }
1.18 millert 234: if ((flags & WHOIS_INIC_FALLBACK) && nhost == NULL &&
235: !nomatch && (p = strstr(buf, "No match for \""))) {
1.8 millert 236: p += sizeof("No match for \"") - 1;
1.10 millert 237: if ((len = strcspn(p, "\"")) && len == strlen(name) &&
1.8 millert 238: strncasecmp(name, p, len) == 0)
239: nomatch = 1;
240: }
241: (void)puts(buf);
242: }
1.18 millert 243: if (nbuf != NULL)
1.12 millert 244: free(nbuf);
1.8 millert 245:
246: /* Do second lookup as needed */
1.18 millert 247: if (nomatch && nhost == NULL) {
1.8 millert 248: (void)printf("Looking up %s at %s.\n\n", name, INICHOST);
249: nhost = INICHOST;
250: }
251: if (nhost) {
1.18 millert 252: error += whois(name, nhost, 0);
1.8 millert 253: if (!nomatch)
254: free(nhost);
255: }
1.18 millert 256: freeaddrinfo(res);
257: return (error);
258: }
259:
260: /*
261: * If no country is specified determine the top level domain from the query.
1.19 millert 262: * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
263: * If the domain does not contain '.', check to see if it is an NSI handle
264: * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
265: * Fall back to NICHOST for the non-handle case.
1.18 millert 266: */
267: static char *
268: choose_server(const char *name, const char *country)
269: {
270: static char *server;
271: const char *qhead;
1.19 millert 272: char *ep;
1.18 millert 273: size_t len;
274:
275: if (country != NULL)
276: qhead = country;
1.19 millert 277: else if ((qhead = strrchr(name, '.')) == NULL) {
278: if (*name == '!')
1.21 ! millert 279: return (NNICHOST);
1.19 millert 280: else if ((strncasecmp(name, "COCO-", 5) == 0 ||
281: strncasecmp(name, "COHO-", 5) == 0) &&
282: strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
283: return (CNICHOST);
284: else
285: return (NICHOST);
286: } else if (isdigit(*(++qhead)))
1.18 millert 287: return (ANICHOST);
288: len = strlen(qhead) + sizeof(QNICHOST_TAIL);
289: if ((server = realloc(server, len)) == NULL)
290: err(1, "realloc");
291: strlcpy(server, qhead, len);
292: strlcat(server, QNICHOST_TAIL, len);
293: return (server);
1.1 deraadt 294: }
295:
1.17 millert 296: static __dead void
297: usage(void)
1.1 deraadt 298: {
1.18 millert 299: extern char *__progname;
1.7 millert 300:
301: (void)fprintf(stderr,
1.18 millert 302: "usage: %s [-adgilmpqQrR6] [-c country-code | -h hostname] "
303: "name ...\n", __progname);
304: exit(1);
1.1 deraadt 305: }