Annotation of src/usr.bin/whois/whois.c, Revision 1.60
1.60 ! millert 1: /* $OpenBSD: whois.c,v 1.59 2024/03/05 16:06:32 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.59 millert 48: #define INICHOST "whois.internic.net"
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.52 sthen 61: #define IANAHOST "whois.iana.org"
1.6 deraadt 62: #define QNICHOST_TAIL ".whois-servers.net"
1.22 millert 63:
64: #define WHOIS_PORT "whois"
1.56 sthen 65: #define WHOIS_SERVER_ID "Registrar WHOIS Server:"
1.1 deraadt 66:
1.60 ! millert 67: #define WHOIS_RECURSE 0x01
! 68: #define WHOIS_QUICK 0x02
! 69: #define WHOIS_SPAM_ME 0x04
! 70:
! 71: #define CHOPSPAM ">>> Last update of WHOIS database:"
1.22 millert 72:
1.35 henning 73: const char *port_whois = WHOIS_PORT;
1.41 henning 74: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
75: AFNICHOST, NULL };
1.8 millert 76:
1.34 henning 77: __dead void usage(void);
78: int whois(const char *, const char *, const char *, int);
1.55 millert 79: char *choose_server(const char *, const char *, char **);
1.1 deraadt 80:
81: int
1.27 deraadt 82: main(int argc, char *argv[])
1.1 deraadt 83: {
1.18 millert 84: int ch, flags, rval;
1.55 millert 85: char *host, *name, *country;
1.1 deraadt 86:
1.39 millert 87: country = host = NULL;
1.18 millert 88: flags = rval = 0;
1.60 ! millert 89: while ((ch = getopt(argc, argv, "aAc:dgh:iIlmp:PqQrRS")) != -1)
1.34 henning 90: switch (ch) {
1.4 deraadt 91: case 'a':
92: host = ANICHOST;
93: break;
1.22 millert 94: case 'A':
95: host = PNICHOST;
96: break;
1.18 millert 97: case 'c':
98: country = optarg;
99: break;
1.4 deraadt 100: case 'd':
101: host = DNICHOST;
102: break;
1.7 millert 103: case 'g':
104: host = GNICHOST;
105: break;
1.1 deraadt 106: case 'h':
107: host = optarg;
108: break;
1.7 millert 109: case 'i':
110: host = INICHOST;
111: break;
1.52 sthen 112: case 'I':
113: host = IANAHOST;
114: break;
1.16 fgsch 115: case 'l':
116: host = LNICHOST;
117: break;
1.6 deraadt 118: case 'm':
119: host = MNICHOST;
120: break;
1.4 deraadt 121: case 'p':
1.35 henning 122: port_whois = optarg;
1.4 deraadt 123: break;
1.44 sthen 124: case 'P':
125: host = PDBHOST;
126: break;
1.6 deraadt 127: case 'q':
1.22 millert 128: /* deprecated, now the default */
1.8 millert 129: break;
130: case 'Q':
131: flags |= WHOIS_QUICK;
1.6 deraadt 132: break;
1.4 deraadt 133: case 'r':
134: host = RNICHOST;
135: break;
1.7 millert 136: case 'R':
137: host = RUNICHOST;
1.18 millert 138: break;
1.60 ! millert 139: case 'S':
! 140: flags |= WHOIS_SPAM_ME;
! 141: break;
1.1 deraadt 142: default:
143: usage();
144: }
145: argc -= optind;
146: argv += optind;
147:
1.18 millert 148: if (!argc || (country != NULL && host != NULL))
1.1 deraadt 149: usage();
1.49 deraadt 150:
1.50 deraadt 151: if (pledge("stdio dns inet", NULL) == -1)
152: err(1, "pledge");
1.1 deraadt 153:
1.18 millert 154: if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22 millert 155: flags |= WHOIS_RECURSE;
1.54 millert 156: for (name = *argv; (name = *argv) != NULL; argv++) {
1.55 millert 157: char *tofree = NULL;
158: const char *server =
159: host ? host : choose_server(name, country, &tofree);
1.54 millert 160: rval += whois(name, server, port_whois, flags);
1.55 millert 161: free(tofree);
1.54 millert 162: }
163: return (rval);
1.8 millert 164: }
165:
1.34 henning 166: int
1.22 millert 167: whois(const char *query, const char *server, const char *port, int flags)
1.8 millert 168: {
1.46 millert 169: FILE *fp;
1.32 henning 170: char *buf, *p, *nhost, *nbuf = NULL;
1.8 millert 171: size_t len;
1.22 millert 172: int i, s, error;
1.32 henning 173: const char *reason = NULL, *fmt;
1.20 millert 174: struct addrinfo hints, *res, *ai;
1.18 millert 175:
176: memset(&hints, 0, sizeof(hints));
177: hints.ai_flags = 0;
178: hints.ai_family = AF_UNSPEC;
179: hints.ai_socktype = SOCK_STREAM;
1.22 millert 180: error = getaddrinfo(server, port, &hints, &res);
1.18 millert 181: if (error) {
1.29 deraadt 182: if (error == EAI_SERVICE)
183: warnx("%s: bad port", port);
184: else
185: warnx("%s: %s", server, gai_strerror(error));
1.18 millert 186: return (1);
187: }
1.6 deraadt 188:
1.20 millert 189: for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
190: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.34 henning 191: if (s == -1) {
1.35 henning 192: error = errno;
1.11 itojun 193: reason = "socket";
194: continue;
195: }
1.34 henning 196: if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
1.35 henning 197: error = errno;
1.11 itojun 198: reason = "connect";
199: close(s);
200: s = -1;
201: continue;
202: }
203: break; /*okay*/
204: }
1.58 jca 205: freeaddrinfo(res);
1.34 henning 206: if (s == -1) {
1.35 henning 207: if (reason) {
208: errno = error;
1.24 millert 209: warn("%s: %s", server, reason);
1.35 henning 210: } else
1.17 millert 211: warn("unknown error in connection attempt");
1.18 millert 212: return (1);
1.11 itojun 213: }
1.4 deraadt 214:
1.60 ! millert 215: if (!(flags & WHOIS_SPAM_ME) &&
! 216: (strcmp(server, "whois.denic.de") == 0 ||
! 217: strcmp(server, "de" QNICHOST_TAIL) == 0))
1.36 henning 218: fmt = "-T dn,ace -C ISO-8859-1 %s\r\n";
1.60 ! millert 219: else if (!(flags & WHOIS_SPAM_ME) &&
! 220: (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
! 221: strcmp(server, "dk" QNICHOST_TAIL) == 0))
1.37 mk 222: fmt = "--show-handles %s\r\n";
1.32 henning 223: else
224: fmt = "%s\r\n";
1.31 henning 225:
1.46 millert 226: fp = fdopen(s, "r+");
227: if (fp == NULL)
1.18 millert 228: err(1, "fdopen");
1.46 millert 229: fprintf(fp, fmt, query);
230: fflush(fp);
1.8 millert 231: nhost = NULL;
1.46 millert 232: while ((buf = fgetln(fp, &len)) != NULL) {
1.60 ! millert 233: /* Nominet */
! 234: if (!(flags & WHOIS_SPAM_ME) &&
! 235: len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
! 236: break;
! 237:
1.22 millert 238: p = buf + len - 1;
239: if (isspace((unsigned char)*p)) {
240: do
1.28 fgsch 241: *p = '\0';
242: while (p > buf && isspace((unsigned char)*--p));
1.22 millert 243: } else {
1.17 millert 244: if ((nbuf = malloc(len + 1)) == NULL)
245: err(1, "malloc");
1.12 millert 246: memcpy(nbuf, buf, len);
247: nbuf[len] = '\0';
248: buf = nbuf;
249: }
1.34 henning 250: puts(buf);
1.8 millert 251:
1.60 ! millert 252: if (nhost == NULL && (flags & WHOIS_RECURSE)) {
! 253: if ((p = strstr(buf, WHOIS_SERVER_ID))) {
! 254: p += sizeof(WHOIS_SERVER_ID) - 1;
! 255: while (isblank((unsigned char)*p))
! 256: p++;
! 257: if ((len = strcspn(p, " \t\n\r"))) {
! 258: if ((nhost = malloc(len + 1)) == NULL)
! 259: err(1, "malloc");
! 260: memcpy(nhost, p, len);
! 261: nhost[len] = '\0';
! 262: }
! 263: } else if (strcmp(server, ANICHOST) == 0) {
! 264: for (p = buf; *p != '\0'; p++)
! 265: *p = tolower((unsigned char)*p);
! 266: for (i = 0; ip_whois[i] != NULL; i++) {
! 267: if (strstr(buf, ip_whois[i]) != NULL) {
! 268: nhost = strdup(ip_whois[i]);
! 269: if (nhost == NULL)
! 270: err(1, "strdup");
! 271: break;
! 272: }
1.22 millert 273: }
274: }
1.60 ! millert 275: }
! 276:
! 277: /* Verisign etc. */
! 278: if (!(flags & WHOIS_SPAM_ME) &&
! 279: len >= sizeof(CHOPSPAM)-1 &&
! 280: (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
! 281: strncasecmp(buf, &CHOPSPAM[4], sizeof(CHOPSPAM)-5) == 0)) {
! 282: printf("\n");
! 283: break;
1.8 millert 284: }
285: }
1.46 millert 286: fclose(fp);
1.53 mmcc 287: free(nbuf);
1.8 millert 288:
1.22 millert 289: if (nhost != NULL) {
290: error = whois(query, nhost, port, 0);
291: free(nhost);
1.8 millert 292: }
1.58 jca 293:
1.18 millert 294: return (error);
295: }
296:
297: /*
298: * If no country is specified determine the top level domain from the query.
1.19 millert 299: * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
300: * If the domain does not contain '.', check to see if it is an NSI handle
1.38 henning 301: * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+) or an
1.57 florian 302: * ASN (starts with AS) or IPv6 address (contains ':'). Fall back to
303: * NICHOST for the non-handle and non-IPv6 case.
1.18 millert 304: */
1.34 henning 305: char *
1.55 millert 306: choose_server(const char *name, const char *country, char **tofree)
1.18 millert 307: {
1.54 millert 308: char *server;
1.18 millert 309: const char *qhead;
1.19 millert 310: char *ep;
1.58 jca 311: struct addrinfo hints, *res = NULL;
1.47 sthen 312:
313: memset(&hints, 0, sizeof(hints));
314: hints.ai_flags = 0;
315: hints.ai_family = AF_UNSPEC;
316: hints.ai_socktype = SOCK_STREAM;
1.18 millert 317:
318: if (country != NULL)
319: qhead = country;
1.19 millert 320: else if ((qhead = strrchr(name, '.')) == NULL) {
321: if (*name == '!')
1.22 millert 322: return (INICHOST);
1.19 millert 323: else if ((strncasecmp(name, "COCO-", 5) == 0 ||
324: strncasecmp(name, "COHO-", 5) == 0) &&
325: strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
1.34 henning 326: return (CNICHOST);
1.38 henning 327: else if ((strncasecmp(name, "AS", 2) == 0) &&
328: strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
329: return (MNICHOST);
1.57 florian 330: else if (strchr(name, ':') != NULL) /* IPv6 address */
331: return (ANICHOST);
1.19 millert 332: else
333: return (NICHOST);
1.51 mmcc 334: } else if (isdigit((unsigned char)*(++qhead)))
1.18 millert 335: return (ANICHOST);
1.47 sthen 336:
337: /*
338: * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
339: * (per registry agreement), some older gTLDs also support this...
340: */
1.54 millert 341: if (asprintf(&server, "whois.nic.%s", qhead) == -1)
342: err(1, NULL);
1.47 sthen 343:
344: /* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
345: if ((strlen(qhead) == 2 ||
346: /* and is required for most of the <=2003 TLDs/gTLDs */
1.48 sthen 347: strcasecmp(qhead, "org") == 0 ||
348: strcasecmp(qhead, "com") == 0 ||
349: strcasecmp(qhead, "net") == 0 ||
350: strcasecmp(qhead, "cat") == 0 ||
351: strcasecmp(qhead, "pro") == 0 ||
352: strcasecmp(qhead, "info") == 0 ||
353: strcasecmp(qhead, "aero") == 0 ||
354: strcasecmp(qhead, "jobs") == 0 ||
355: strcasecmp(qhead, "mobi") == 0 ||
356: strcasecmp(qhead, "museum") == 0 ||
1.47 sthen 357: /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
358: getaddrinfo(server, NULL, &hints, &res) != 0)) {
1.54 millert 359: free(server);
360: if (asprintf(&server, "%s%s", qhead, QNICHOST_TAIL) == -1)
361: err(1, NULL);
1.47 sthen 362: }
1.58 jca 363: if (res != NULL)
364: freeaddrinfo(res);
1.47 sthen 365:
1.55 millert 366: *tofree = server;
1.18 millert 367: return (server);
1.1 deraadt 368: }
369:
1.34 henning 370: __dead void
1.17 millert 371: usage(void)
1.1 deraadt 372: {
1.18 millert 373: extern char *__progname;
1.7 millert 374:
1.34 henning 375: fprintf(stderr,
1.52 sthen 376: "usage: %s [-AadgIilmPQRr] [-c country-code | -h host] "
1.22 millert 377: "[-p port] name ...\n", __progname);
1.18 millert 378: exit(1);
1.1 deraadt 379: }