Annotation of src/usr.bin/whois/whois.c, Revision 1.47
1.47 ! sthen 1: /* $OpenBSD: whois.c,v 1.46 2014/01/03 15:25:18 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: #include <sys/types.h>
33: #include <sys/socket.h>
1.17 millert 34:
1.1 deraadt 35: #include <netinet/in.h>
1.5 art 36: #include <arpa/inet.h>
1.1 deraadt 37: #include <netdb.h>
1.17 millert 38:
1.18 millert 39: #include <ctype.h>
1.7 millert 40: #include <err.h>
1.35 henning 41: #include <errno.h>
1.1 deraadt 42: #include <stdio.h>
43: #include <stdlib.h>
44: #include <string.h>
45: #include <unistd.h>
46:
1.7 millert 47: #define NICHOST "whois.crsnic.net"
1.22 millert 48: #define INICHOST "whois.networksolutions.com"
1.19 millert 49: #define CNICHOST "whois.corenic.net"
1.7 millert 50: #define DNICHOST "whois.nic.mil"
51: #define GNICHOST "whois.nic.gov"
1.4 deraadt 52: #define ANICHOST "whois.arin.net"
53: #define RNICHOST "whois.ripe.net"
54: #define PNICHOST "whois.apnic.net"
1.7 millert 55: #define RUNICHOST "whois.ripn.net"
1.6 deraadt 56: #define MNICHOST "whois.ra.net"
1.16 fgsch 57: #define LNICHOST "whois.lacnic.net"
1.41 henning 58: #define AFNICHOST "whois.afrinic.net"
1.22 millert 59: #define BNICHOST "whois.registro.br"
1.44 sthen 60: #define PDBHOST "whois.peeringdb.com"
1.6 deraadt 61: #define QNICHOST_TAIL ".whois-servers.net"
1.22 millert 62:
63: #define WHOIS_PORT "whois"
1.24 millert 64: #define WHOIS_SERVER_ID "Whois Server:"
1.1 deraadt 65:
1.8 millert 66: #define WHOIS_RECURSE 0x01
1.22 millert 67: #define WHOIS_QUICK 0x02
68:
1.35 henning 69: const char *port_whois = WHOIS_PORT;
1.41 henning 70: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
71: AFNICHOST, NULL };
1.8 millert 72:
1.34 henning 73: __dead void usage(void);
74: int whois(const char *, const char *, const char *, int);
75: char *choose_server(const char *, const char *);
1.1 deraadt 76:
77: int
1.27 deraadt 78: main(int argc, char *argv[])
1.1 deraadt 79: {
1.18 millert 80: int ch, flags, rval;
1.39 millert 81: char *host, *name, *country;
1.1 deraadt 82:
1.39 millert 83: country = host = NULL;
1.18 millert 84: flags = rval = 0;
1.44 sthen 85: while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:PqQrR")) != -1)
1.34 henning 86: switch (ch) {
1.4 deraadt 87: case 'a':
88: host = ANICHOST;
89: break;
1.22 millert 90: case 'A':
91: host = PNICHOST;
92: break;
1.18 millert 93: case 'c':
94: country = optarg;
95: break;
1.4 deraadt 96: case 'd':
97: host = DNICHOST;
98: break;
1.7 millert 99: case 'g':
100: host = GNICHOST;
101: break;
1.1 deraadt 102: case 'h':
103: host = optarg;
104: break;
1.7 millert 105: case 'i':
106: host = INICHOST;
107: break;
1.16 fgsch 108: case 'l':
109: host = LNICHOST;
110: break;
1.6 deraadt 111: case 'm':
112: host = MNICHOST;
113: break;
1.4 deraadt 114: case 'p':
1.35 henning 115: port_whois = optarg;
1.4 deraadt 116: break;
1.44 sthen 117: case 'P':
118: host = PDBHOST;
119: break;
1.6 deraadt 120: case 'q':
1.22 millert 121: /* deprecated, now the default */
1.8 millert 122: break;
123: case 'Q':
124: flags |= WHOIS_QUICK;
1.6 deraadt 125: break;
1.4 deraadt 126: case 'r':
127: host = RNICHOST;
128: break;
1.7 millert 129: case 'R':
130: host = RUNICHOST;
1.18 millert 131: break;
1.1 deraadt 132: default:
133: usage();
134: }
135: argc -= optind;
136: argv += optind;
137:
1.18 millert 138: if (!argc || (country != NULL && host != NULL))
1.1 deraadt 139: usage();
140:
1.18 millert 141: if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22 millert 142: flags |= WHOIS_RECURSE;
1.18 millert 143: for (name = *argv; (name = *argv) != NULL; argv++)
1.22 millert 144: rval += whois(name, host ? host : choose_server(name, country),
1.35 henning 145: port_whois, flags);
1.17 millert 146: exit(rval);
1.8 millert 147: }
148:
1.34 henning 149: int
1.22 millert 150: whois(const char *query, const char *server, const char *port, int flags)
1.8 millert 151: {
1.46 millert 152: FILE *fp;
1.32 henning 153: char *buf, *p, *nhost, *nbuf = NULL;
1.8 millert 154: size_t len;
1.22 millert 155: int i, s, error;
1.32 henning 156: const char *reason = NULL, *fmt;
1.20 millert 157: struct addrinfo hints, *res, *ai;
1.18 millert 158:
159: memset(&hints, 0, sizeof(hints));
160: hints.ai_flags = 0;
161: hints.ai_family = AF_UNSPEC;
162: hints.ai_socktype = SOCK_STREAM;
1.22 millert 163: error = getaddrinfo(server, port, &hints, &res);
1.18 millert 164: if (error) {
1.29 deraadt 165: if (error == EAI_SERVICE)
166: warnx("%s: bad port", port);
167: else
168: warnx("%s: %s", server, gai_strerror(error));
1.18 millert 169: return (1);
170: }
1.6 deraadt 171:
1.20 millert 172: for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
173: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.34 henning 174: if (s == -1) {
1.35 henning 175: error = errno;
1.11 itojun 176: reason = "socket";
177: continue;
178: }
1.34 henning 179: if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
1.35 henning 180: error = errno;
1.11 itojun 181: reason = "connect";
182: close(s);
183: s = -1;
184: continue;
185: }
186: break; /*okay*/
187: }
1.34 henning 188: if (s == -1) {
1.35 henning 189: if (reason) {
190: errno = error;
1.24 millert 191: warn("%s: %s", server, reason);
1.35 henning 192: } else
1.17 millert 193: warn("unknown error in connection attempt");
1.18 millert 194: freeaddrinfo(res);
195: return (1);
1.11 itojun 196: }
1.4 deraadt 197:
1.32 henning 198: if (strcmp(server, "whois.denic.de") == 0 ||
1.37 mk 199: strcmp(server, "de" QNICHOST_TAIL) == 0)
1.36 henning 200: fmt = "-T dn,ace -C ISO-8859-1 %s\r\n";
1.37 mk 201: else if (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
202: strcmp(server, "dk" QNICHOST_TAIL) == 0)
203: fmt = "--show-handles %s\r\n";
1.32 henning 204: else
205: fmt = "%s\r\n";
1.31 henning 206:
1.46 millert 207: fp = fdopen(s, "r+");
208: if (fp == NULL)
1.18 millert 209: err(1, "fdopen");
1.46 millert 210: fprintf(fp, fmt, query);
211: fflush(fp);
1.8 millert 212: nhost = NULL;
1.46 millert 213: while ((buf = fgetln(fp, &len)) != NULL) {
1.22 millert 214: p = buf + len - 1;
215: if (isspace((unsigned char)*p)) {
216: do
1.28 fgsch 217: *p = '\0';
218: while (p > buf && isspace((unsigned char)*--p));
1.22 millert 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.34 henning 226: 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.45 deraadt 233: while (isblank((unsigned char)*p))
1.25 millert 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.46 millert 254: fclose(fp);
1.32 henning 255: if (nbuf != NULL)
256: free(nbuf);
1.8 millert 257:
1.22 millert 258: if (nhost != NULL) {
259: error = whois(query, nhost, port, 0);
260: free(nhost);
1.8 millert 261: }
1.18 millert 262: freeaddrinfo(res);
263: return (error);
264: }
265:
266: /*
267: * If no country is specified determine the top level domain from the query.
1.19 millert 268: * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
269: * If the domain does not contain '.', check to see if it is an NSI handle
1.38 henning 270: * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+) or an
271: * ASN (starts with AS). Fall back to NICHOST for the non-handle case.
1.18 millert 272: */
1.34 henning 273: char *
1.18 millert 274: choose_server(const char *name, const char *country)
275: {
276: static char *server;
277: const char *qhead;
1.35 henning 278: char *nserver;
1.19 millert 279: char *ep;
1.18 millert 280: size_t len;
1.47 ! sthen 281: struct addrinfo hints, *res;
! 282:
! 283: memset(&hints, 0, sizeof(hints));
! 284: hints.ai_flags = 0;
! 285: hints.ai_family = AF_UNSPEC;
! 286: hints.ai_socktype = SOCK_STREAM;
1.18 millert 287:
288: if (country != NULL)
289: qhead = country;
1.19 millert 290: else if ((qhead = strrchr(name, '.')) == NULL) {
291: if (*name == '!')
1.22 millert 292: return (INICHOST);
1.19 millert 293: else if ((strncasecmp(name, "COCO-", 5) == 0 ||
294: strncasecmp(name, "COHO-", 5) == 0) &&
295: strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
1.34 henning 296: return (CNICHOST);
1.38 henning 297: else if ((strncasecmp(name, "AS", 2) == 0) &&
298: strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
299: return (MNICHOST);
1.19 millert 300: else
301: return (NICHOST);
302: } else if (isdigit(*(++qhead)))
1.18 millert 303: return (ANICHOST);
304: len = strlen(qhead) + sizeof(QNICHOST_TAIL);
1.35 henning 305: if ((nserver = realloc(server, len)) == NULL)
1.18 millert 306: err(1, "realloc");
1.35 henning 307: server = nserver;
1.47 ! sthen 308:
! 309: /*
! 310: * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
! 311: * (per registry agreement), some older gTLDs also support this...
! 312: */
! 313: strlcpy(server, "whois.nic.", len);
! 314: strlcat(server, qhead, len);
! 315:
! 316: /* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
! 317: if ((strlen(qhead) == 2 ||
! 318: /* and is required for most of the <=2003 TLDs/gTLDs */
! 319: strncasecmp(qhead, "org", 3) == 0 ||
! 320: strncasecmp(qhead, "com", 3) == 0 ||
! 321: strncasecmp(qhead, "net", 3) == 0 ||
! 322: strncasecmp(qhead, "cat", 3) == 0 ||
! 323: strncasecmp(qhead, "pro", 3) == 0 ||
! 324: strncasecmp(qhead, "info", 4) == 0 ||
! 325: strncasecmp(qhead, "aero", 4) == 0 ||
! 326: strncasecmp(qhead, "jobs", 4) == 0 ||
! 327: strncasecmp(qhead, "mobi", 4) == 0 ||
! 328: strncasecmp(qhead, "museum", 6) == 0 ||
! 329: /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
! 330: getaddrinfo(server, NULL, &hints, &res) != 0)) {
! 331: strlcpy(server, qhead, len);
! 332: strlcat(server, QNICHOST_TAIL, len);
! 333: }
! 334:
1.18 millert 335: return (server);
1.1 deraadt 336: }
337:
1.34 henning 338: __dead void
1.17 millert 339: usage(void)
1.1 deraadt 340: {
1.18 millert 341: extern char *__progname;
1.7 millert 342:
1.34 henning 343: fprintf(stderr,
1.44 sthen 344: "usage: %s [-AadgilmPQRr] [-c country-code | -h host] "
1.22 millert 345: "[-p port] name ...\n", __progname);
1.18 millert 346: exit(1);
1.1 deraadt 347: }