Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.36.30.1
      1  1.36.30.1    martin /*      $NetBSD: whois.c,v 1.36.30.1 2020/04/08 14:09:18 martin Exp $    */
      2  1.36.30.1    martin /*      $OpenBSD: whois.c,v 1.58 2018/06/19 11:28:11 jca Exp $   */
      3        1.5       jtc 
      4        1.1       cgd /*
      5       1.22       jrf  * Copyright (c) 1980, 1993
      6       1.22       jrf  *	The Regents of the University of California.  All rights reserved.
      7        1.1       cgd  *
      8        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9        1.1       cgd  * modification, are permitted provided that the following conditions
     10        1.1       cgd  * are met:
     11        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16       1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     17        1.1       cgd  *    may be used to endorse or promote products derived from this software
     18        1.1       cgd  *    without specific prior written permission.
     19        1.1       cgd  *
     20        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.1       cgd  * SUCH DAMAGE.
     31        1.1       cgd  */
     32        1.1       cgd 
     33       1.23  christos #include <sys/cdefs.h>
     34       1.23  christos 
     35        1.1       cgd #ifndef lint
     36       1.33     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     37       1.33     lukem  The Regents of the University of California.  All rights reserved.");
     38        1.1       cgd #endif /* not lint */
     39        1.1       cgd 
     40        1.1       cgd #ifndef lint
     41        1.5       jtc #if 0
     42       1.22       jrf static const char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     43        1.6       mrg #else
     44  1.36.30.1    martin __RCSID("$NetBSD: whois.c,v 1.36.30.1 2020/04/08 14:09:18 martin Exp $");
     45        1.5       jtc #endif
     46        1.1       cgd #endif /* not lint */
     47        1.1       cgd 
     48        1.1       cgd #include <sys/types.h>
     49        1.1       cgd #include <sys/socket.h>
     50       1.22       jrf 
     51        1.1       cgd #include <netinet/in.h>
     52       1.22       jrf #include <arpa/inet.h>
     53       1.22       jrf #include <netdb.h>
     54       1.22       jrf 
     55       1.22       jrf #include <ctype.h>
     56        1.7     lukem #include <err.h>
     57        1.1       cgd #include <stdio.h>
     58        1.4       jtc #include <stdlib.h>
     59        1.4       jtc #include <string.h>
     60        1.4       jtc #include <unistd.h>
     61       1.23  christos #include <errno.h>
     62        1.9      tron 
     63       1.31  christos #define	ANICHOST	"whois.arin.net"
     64       1.31  christos #define	BNICHOST	"whois.registro.br"
     65       1.22       jrf #define	CNICHOST	"whois.corenic.net"
     66       1.22       jrf #define	DNICHOST	"whois.nic.mil"
     67       1.31  christos #define	FNICHOST	"whois.afrinic.net"
     68       1.22       jrf #define	GNICHOST	"whois.nic.gov"
     69  1.36.30.1    martin #define	IANAHOST	"whois.iana.org"
     70       1.31  christos #define	INICHOST	"whois.networksolutions.com"
     71       1.31  christos #define	LNICHOST	"whois.lacnic.net"
     72       1.31  christos #define	MNICHOST	"whois.ra.net"
     73       1.31  christos #define	NICHOST		"whois.crsnic.net"
     74  1.36.30.1    martin #define	PDBHOST		"whois.peeringdb.com"
     75       1.31  christos #define	PNICHOST	"whois.apnic.net"
     76       1.31  christos #define	QNICHOST_TAIL	".whois-servers.net"
     77       1.22       jrf #define	RNICHOST	"whois.ripe.net"
     78       1.22       jrf #define	RUNICHOST	"whois.ripn.net"
     79       1.22       jrf 
     80       1.22       jrf #define	WHOIS_PORT	"whois"
     81  1.36.30.1    martin #define	WHOIS_SERVER_ID	"Registrar WHOIS Server:"
     82       1.22       jrf 
     83       1.22       jrf #define WHOIS_RECURSE		0x01
     84       1.22       jrf #define WHOIS_QUICK		0x02
     85       1.22       jrf 
     86       1.23  christos static const char *port_whois = WHOIS_PORT;
     87       1.23  christos static const char *ip_whois[] =
     88       1.31  christos     { LNICHOST, RNICHOST, PNICHOST, FNICHOST, BNICHOST, NULL };
     89       1.22       jrf 
     90       1.32     perry static void usage(void) __dead;
     91       1.22       jrf static int whois(const char *, const char *, const char *, int);
     92  1.36.30.1    martin static const char *choose_server(const char *, const char *, char **);
     93        1.9      tron 
     94       1.22       jrf int
     95       1.22       jrf main(int argc, char *argv[])
     96       1.22       jrf {
     97       1.22       jrf 	int ch, flags, rval;
     98       1.34     lukem 	const char *host, *name, *country;
     99        1.9      tron 
    100       1.29  liamjfoy 	country = host = NULL;
    101       1.22       jrf 	flags = rval = 0;
    102  1.36.30.1    martin 	while ((ch = getopt(argc, argv, "Aac:dfgh:Iilmp:PqQRr")) != -1)
    103  1.36.30.1    martin 		switch (ch) {
    104       1.22       jrf 		case 'a':
    105       1.22       jrf 			host = ANICHOST;
    106       1.22       jrf 			break;
    107       1.22       jrf 		case 'A':
    108       1.22       jrf 			host = PNICHOST;
    109       1.22       jrf 			break;
    110       1.22       jrf 		case 'c':
    111       1.22       jrf 			country = optarg;
    112       1.22       jrf 			break;
    113       1.22       jrf 		case 'd':
    114       1.22       jrf 			host = DNICHOST;
    115       1.22       jrf 			break;
    116       1.31  christos 		case 'f':
    117       1.31  christos 			host = FNICHOST;
    118       1.31  christos 			break;
    119       1.22       jrf 		case 'g':
    120       1.22       jrf 			host = GNICHOST;
    121       1.22       jrf 			break;
    122       1.22       jrf 		case 'h':
    123       1.22       jrf 			host = optarg;
    124       1.22       jrf 			break;
    125       1.22       jrf 		case 'i':
    126       1.22       jrf 			host = INICHOST;
    127       1.22       jrf 			break;
    128  1.36.30.1    martin 		case 'I':
    129  1.36.30.1    martin 			host = IANAHOST;
    130  1.36.30.1    martin 			break;
    131       1.22       jrf 		case 'l':
    132       1.22       jrf 			host = LNICHOST;
    133       1.22       jrf 			break;
    134       1.22       jrf 		case 'm':
    135       1.22       jrf 			host = MNICHOST;
    136       1.22       jrf 			break;
    137       1.22       jrf 		case 'p':
    138       1.23  christos 			port_whois = optarg;
    139       1.22       jrf 			break;
    140  1.36.30.1    martin 		case 'P':
    141  1.36.30.1    martin 			host = PDBHOST;
    142  1.36.30.1    martin 			break;
    143       1.22       jrf 		case 'q':
    144       1.22       jrf 			/* deprecated, now the default */
    145       1.22       jrf 			break;
    146       1.22       jrf 		case 'Q':
    147       1.22       jrf 			flags |= WHOIS_QUICK;
    148       1.22       jrf 			break;
    149       1.22       jrf 		case 'r':
    150       1.22       jrf 			host = RNICHOST;
    151       1.22       jrf 			break;
    152       1.22       jrf 		case 'R':
    153       1.22       jrf 			host = RUNICHOST;
    154       1.22       jrf 			break;
    155       1.22       jrf 		default:
    156       1.22       jrf 			usage();
    157       1.22       jrf 		}
    158       1.22       jrf 	argc -= optind;
    159       1.22       jrf 	argv += optind;
    160       1.22       jrf 
    161       1.22       jrf 	if (!argc || (country != NULL && host != NULL))
    162       1.22       jrf 		usage();
    163       1.22       jrf 
    164       1.22       jrf 	if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
    165       1.22       jrf 		flags |= WHOIS_RECURSE;
    166  1.36.30.1    martin 	for (name = *argv; (name = *argv) != NULL; argv++) {
    167  1.36.30.1    martin 		char *tofree = NULL;
    168  1.36.30.1    martin 		const char *server =
    169  1.36.30.1    martin 		    host ? host : choose_server(name, country, &tofree);
    170  1.36.30.1    martin 		rval += whois(name, server, port_whois, flags);
    171  1.36.30.1    martin 		free(tofree);
    172  1.36.30.1    martin 	}
    173  1.36.30.1    martin 	return (rval);
    174        1.9      tron }
    175        1.4       jtc 
    176       1.22       jrf static int
    177       1.22       jrf whois(const char *query, const char *server, const char *port, int flags)
    178        1.9      tron {
    179  1.36.30.1    martin 	FILE *fp;
    180       1.22       jrf 	char *buf, *p, *nhost, *nbuf = NULL;
    181       1.22       jrf 	size_t len;
    182       1.22       jrf 	int i, s, error;
    183       1.27  christos 	const char *reason = NULL, *fmt;
    184       1.22       jrf 	struct addrinfo hints, *res, *ai;
    185       1.22       jrf 
    186       1.23  christos 	(void)memset(&hints, 0, sizeof(hints));
    187       1.22       jrf 	hints.ai_flags = 0;
    188       1.22       jrf 	hints.ai_family = AF_UNSPEC;
    189       1.22       jrf 	hints.ai_socktype = SOCK_STREAM;
    190       1.22       jrf 	error = getaddrinfo(server, port, &hints, &res);
    191       1.22       jrf 	if (error) {
    192  1.36.30.1    martin 		if (error == EAI_SERVICE)
    193  1.36.30.1    martin 			warnx("%s: bad port", port);
    194  1.36.30.1    martin 		else
    195  1.36.30.1    martin 			warnx("%s: %s", server, gai_strerror(error));
    196       1.22       jrf 		return (1);
    197       1.22       jrf 	}
    198        1.9      tron 
    199       1.22       jrf 	for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
    200       1.22       jrf 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    201       1.22       jrf 		if (s < 0) {
    202       1.23  christos 			error = errno;
    203       1.22       jrf 			reason = "socket";
    204       1.22       jrf 			continue;
    205       1.22       jrf 		}
    206       1.22       jrf 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
    207       1.23  christos 			error = errno;
    208       1.22       jrf 			reason = "connect";
    209       1.22       jrf 			close(s);
    210       1.22       jrf 			s = -1;
    211       1.22       jrf 			continue;
    212       1.22       jrf 		}
    213       1.22       jrf 		break;	/*okay*/
    214       1.22       jrf 	}
    215  1.36.30.1    martin 	freeaddrinfo(res);
    216       1.22       jrf 	if (s < 0) {
    217       1.23  christos 		if (reason) {
    218       1.23  christos 			errno = error;
    219       1.22       jrf 			warn("%s: %s", server, reason);
    220       1.23  christos 		} else
    221       1.23  christos 			warnx("Unknown error in connection attempt");
    222       1.22       jrf 		return (1);
    223       1.22       jrf 	}
    224        1.9      tron 
    225       1.27  christos 	if (strcmp(server, "whois.denic.de") == 0 ||
    226  1.36.30.1    martin 	    strcmp(server, "de" QNICHOST_TAIL) == 0)
    227       1.36        ws 		fmt = "-T dn,ace -C ISO-8859-1 ";
    228  1.36.30.1    martin 	else if (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
    229  1.36.30.1    martin 	    strcmp(server, "dk" QNICHOST_TAIL) == 0)
    230  1.36.30.1    martin 		fmt = "--show-handles ";
    231       1.27  christos 	else
    232       1.35  christos 		fmt = "";
    233       1.27  christos 
    234  1.36.30.1    martin 	fp = fdopen(s, "r+");
    235  1.36.30.1    martin 	if (fp == NULL)
    236       1.22       jrf 		err(1, "fdopen");
    237  1.36.30.1    martin 	(void)fprintf(fp, "%s%s\r\n", fmt, query);
    238  1.36.30.1    martin 	(void)fflush(fp);
    239       1.22       jrf 	nhost = NULL;
    240  1.36.30.1    martin 	while ((buf = fgetln(fp, &len)) != NULL) {
    241       1.22       jrf 		p = buf + len - 1;
    242       1.22       jrf 		if (isspace((unsigned char)*p)) {
    243       1.22       jrf 			do
    244       1.22       jrf 				*p = '\0';
    245       1.22       jrf 			while (p > buf && isspace((unsigned char)*--p));
    246       1.22       jrf 		} else {
    247       1.22       jrf 			if ((nbuf = malloc(len + 1)) == NULL)
    248       1.22       jrf 				err(1, "malloc");
    249       1.23  christos 			(void)memcpy(nbuf, buf, len);
    250       1.22       jrf 			nbuf[len] = '\0';
    251       1.22       jrf 			buf = nbuf;
    252       1.22       jrf 		}
    253       1.22       jrf 		(void)puts(buf);
    254       1.22       jrf 
    255       1.22       jrf 		if (nhost != NULL || !(flags & WHOIS_RECURSE))
    256       1.22       jrf 			continue;
    257       1.22       jrf 
    258       1.22       jrf 		if ((p = strstr(buf, WHOIS_SERVER_ID))) {
    259       1.22       jrf 			p += sizeof(WHOIS_SERVER_ID) - 1;
    260       1.23  christos 			while (isblank((unsigned char)*p))
    261       1.22       jrf 				p++;
    262       1.22       jrf 			if ((len = strcspn(p, " \t\n\r"))) {
    263       1.22       jrf 				if ((nhost = malloc(len + 1)) == NULL)
    264       1.22       jrf 					err(1, "malloc");
    265       1.23  christos 				(void)memcpy(nhost, p, len);
    266       1.22       jrf 				nhost[len] = '\0';
    267       1.22       jrf 			}
    268       1.22       jrf 		} else if (strcmp(server, ANICHOST) == 0) {
    269       1.22       jrf 			for (p = buf; *p != '\0'; p++)
    270       1.22       jrf 				*p = tolower((unsigned char)*p);
    271       1.22       jrf 			for (i = 0; ip_whois[i] != NULL; i++) {
    272       1.22       jrf 				if (strstr(buf, ip_whois[i]) != NULL) {
    273       1.22       jrf 					nhost = strdup(ip_whois[i]);
    274       1.22       jrf 					if (nhost == NULL)
    275       1.22       jrf 						err(1, "strdup");
    276       1.22       jrf 					break;
    277       1.22       jrf 				}
    278       1.22       jrf 			}
    279       1.22       jrf 		}
    280       1.22       jrf 	}
    281  1.36.30.1    martin 	fclose(fp);
    282  1.36.30.1    martin 	free(nbuf);
    283        1.9      tron 
    284       1.22       jrf 	if (nhost != NULL) {
    285       1.22       jrf 		error = whois(query, nhost, port, 0);
    286       1.22       jrf 		free(nhost);
    287       1.22       jrf 	}
    288  1.36.30.1    martin 
    289       1.22       jrf 	return (error);
    290        1.9      tron }
    291        1.9      tron 
    292       1.22       jrf /*
    293       1.22       jrf  * If no country is specified determine the top level domain from the query.
    294       1.22       jrf  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
    295       1.22       jrf  * If the domain does not contain '.', check to see if it is an NSI handle
    296  1.36.30.1    martin  * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+) or an
    297  1.36.30.1    martin  * ASN (starts with AS) or IPv6 address (contains ':'). Fall back to
    298  1.36.30.1    martin  * NICHOST for the non-handle and non-IPv6 case.
    299       1.22       jrf  */
    300       1.34     lukem static const char *
    301  1.36.30.1    martin choose_server(const char *name, const char *country, char **tofree)
    302        1.1       cgd {
    303       1.22       jrf 	static char *server;
    304       1.22       jrf 	const char *qhead;
    305       1.22       jrf 	char *ep;
    306  1.36.30.1    martin 	struct addrinfo hints, *res = NULL;
    307  1.36.30.1    martin 
    308  1.36.30.1    martin 	(void)memset(&hints, 0, sizeof(hints));
    309  1.36.30.1    martin 	hints.ai_flags = 0;
    310  1.36.30.1    martin 	hints.ai_family = AF_UNSPEC;
    311  1.36.30.1    martin 	hints.ai_socktype = SOCK_STREAM;
    312       1.22       jrf 
    313       1.22       jrf 	if (country != NULL)
    314       1.22       jrf 		qhead = country;
    315       1.22       jrf 	else if ((qhead = strrchr(name, '.')) == NULL) {
    316       1.22       jrf 		if (*name == '!')
    317       1.22       jrf 			return (INICHOST);
    318       1.22       jrf 		else if ((strncasecmp(name, "COCO-", 5) == 0 ||
    319       1.22       jrf 		    strncasecmp(name, "COHO-", 5) == 0) &&
    320       1.22       jrf 		    strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
    321  1.36.30.1    martin 			return (CNICHOST);
    322  1.36.30.1    martin 		else if ((strncasecmp(name, "AS", 2) == 0) &&
    323  1.36.30.1    martin 		    strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
    324  1.36.30.1    martin 			return (MNICHOST);
    325  1.36.30.1    martin 		else if (strchr(name, ':') != NULL) /* IPv6 address */
    326  1.36.30.1    martin 			return (ANICHOST);
    327       1.22       jrf 		else
    328       1.22       jrf 			return (NICHOST);
    329       1.23  christos 	} else if (isdigit((unsigned char)*(++qhead)))
    330       1.22       jrf 		return (ANICHOST);
    331  1.36.30.1    martin 
    332  1.36.30.1    martin 	/*
    333  1.36.30.1    martin 	 * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
    334  1.36.30.1    martin 	 * (per registry agreement), some older gTLDs also support this...
    335  1.36.30.1    martin 	 */
    336  1.36.30.1    martin 	if (asprintf(&server, "whois.nic.%s", qhead) == -1)
    337  1.36.30.1    martin 		err(1, NULL);
    338  1.36.30.1    martin 
    339  1.36.30.1    martin 	/* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
    340  1.36.30.1    martin 	if ((strlen(qhead) == 2 ||
    341  1.36.30.1    martin 	    /* and is required for most of the <=2003 TLDs/gTLDs */
    342  1.36.30.1    martin 	    strcasecmp(qhead, "org") == 0 ||
    343  1.36.30.1    martin 	    strcasecmp(qhead, "com") == 0 ||
    344  1.36.30.1    martin 	    strcasecmp(qhead, "net") == 0 ||
    345  1.36.30.1    martin 	    strcasecmp(qhead, "cat") == 0 ||
    346  1.36.30.1    martin 	    strcasecmp(qhead, "pro") == 0 ||
    347  1.36.30.1    martin 	    strcasecmp(qhead, "info") == 0 ||
    348  1.36.30.1    martin 	    strcasecmp(qhead, "aero") == 0 ||
    349  1.36.30.1    martin 	    strcasecmp(qhead, "jobs") == 0 ||
    350  1.36.30.1    martin 	    strcasecmp(qhead, "mobi") == 0 ||
    351  1.36.30.1    martin 	    strcasecmp(qhead, "museum") == 0 ||
    352  1.36.30.1    martin 	     /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
    353  1.36.30.1    martin 	    getaddrinfo(server, NULL, &hints, &res) != 0)) {
    354  1.36.30.1    martin 		free(server);
    355  1.36.30.1    martin 		if (asprintf(&server, "%s%s", qhead, QNICHOST_TAIL) == -1)
    356  1.36.30.1    martin 			err(1, NULL);
    357  1.36.30.1    martin 	}
    358  1.36.30.1    martin 	if (res != NULL)
    359  1.36.30.1    martin 		freeaddrinfo(res);
    360  1.36.30.1    martin 
    361  1.36.30.1    martin 	*tofree = server;
    362       1.22       jrf 	return (server);
    363       1.22       jrf }
    364        1.9      tron 
    365       1.23  christos static void
    366       1.22       jrf usage(void)
    367        1.1       cgd {
    368       1.22       jrf 	(void)fprintf(stderr,
    369  1.36.30.1    martin 	    "usage: %s [-AadfgIilmPQRr] [-c country-code | -h host] "
    370       1.23  christos 		"[-p port] name ...\n", getprogname());
    371       1.22       jrf 	exit(1);
    372        1.1       cgd }
    373