Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.22
      1 /*      $NetBSD: whois.c,v 1.22 2003/10/09 14:21:49 jrf Exp $   */
      2 /*	$OpenBSD: whois.c,v 1.28 2003/09/18 22:16:15 fgsch Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1980, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static const char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     41 #else
     42 __RCSID("$NetBSD: whois.c,v 1.22 2003/10/09 14:21:49 jrf Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/types.h>
     47 #include <sys/socket.h>
     48 
     49 #include <netinet/in.h>
     50 #include <arpa/inet.h>
     51 #include <netdb.h>
     52 
     53 #include <ctype.h>
     54 #include <err.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #define	NICHOST		"whois.crsnic.net"
     61 #define	INICHOST	"whois.networksolutions.com"
     62 #define	CNICHOST	"whois.corenic.net"
     63 #define	DNICHOST	"whois.nic.mil"
     64 #define	GNICHOST	"whois.nic.gov"
     65 #define	ANICHOST	"whois.arin.net"
     66 #define	RNICHOST	"whois.ripe.net"
     67 #define	PNICHOST	"whois.apnic.net"
     68 #define	RUNICHOST	"whois.ripn.net"
     69 #define	MNICHOST	"whois.ra.net"
     70 #define LNICHOST	"whois.lacnic.net"
     71 #define SNICHOST	"whois.6bone.net"
     72 #define BNICHOST	"whois.registro.br"
     73 #define	QNICHOST_TAIL	".whois-servers.net"
     74 
     75 #define	WHOIS_PORT	"whois"
     76 #define	WHOIS_SERVER_ID	"Whois Server:"
     77 
     78 #define WHOIS_RECURSE		0x01
     79 #define WHOIS_QUICK		0x02
     80 
     81 const char *port = WHOIS_PORT;
     82 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
     83 
     84 static __dead void usage(void);
     85 static int whois(const char *, const char *, const char *, int);
     86 static char *choose_server(const char *, const char *);
     87 
     88 int
     89 main(int argc, char *argv[])
     90 {
     91 	int ch, flags, rval;
     92 	char *host, *name, *country, *server;
     93 
     94 #ifdef SOCKS
     95 	SOCKSinit(argv[0]);
     96 #endif
     97 	country = host = server = NULL;
     98 	flags = rval = 0;
     99 	while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:qQrR6")) != -1)
    100 		switch(ch) {
    101 		case 'a':
    102 			host = ANICHOST;
    103 			break;
    104 		case 'A':
    105 			host = PNICHOST;
    106 			break;
    107 		case 'c':
    108 			country = optarg;
    109 			break;
    110 		case 'd':
    111 			host = DNICHOST;
    112 			break;
    113 		case 'g':
    114 			host = GNICHOST;
    115 			break;
    116 		case 'h':
    117 			host = optarg;
    118 			break;
    119 		case 'i':
    120 			host = INICHOST;
    121 			break;
    122 		case 'l':
    123 			host = LNICHOST;
    124 			break;
    125 		case 'm':
    126 			host = MNICHOST;
    127 			break;
    128 		case 'p':
    129 			port = optarg;
    130 			break;
    131 		case 'q':
    132 			/* deprecated, now the default */
    133 			break;
    134 		case 'Q':
    135 			flags |= WHOIS_QUICK;
    136 			break;
    137 		case 'r':
    138 			host = RNICHOST;
    139 			break;
    140 		case 'R':
    141 			host = RUNICHOST;
    142 			break;
    143 		case '6':
    144 			host = SNICHOST;
    145 			break;
    146 		default:
    147 			usage();
    148 		}
    149 	argc -= optind;
    150 	argv += optind;
    151 
    152 	if (!argc || (country != NULL && host != NULL))
    153 		usage();
    154 
    155 	if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
    156 		flags |= WHOIS_RECURSE;
    157 	for (name = *argv; (name = *argv) != NULL; argv++)
    158 		rval += whois(name, host ? host : choose_server(name, country),
    159 		    port, flags);
    160 	exit(rval);
    161 }
    162 
    163 static int
    164 whois(const char *query, const char *server, const char *port, int flags)
    165 {
    166 	FILE *sfi, *sfo;
    167 	char *buf, *p, *nhost, *nbuf = NULL;
    168 	size_t len;
    169 	int i, s, error;
    170 	const char *reason = NULL;
    171 	struct addrinfo hints, *res, *ai;
    172 
    173 	memset(&hints, 0, sizeof(hints));
    174 	hints.ai_flags = 0;
    175 	hints.ai_family = AF_UNSPEC;
    176 	hints.ai_socktype = SOCK_STREAM;
    177 	error = getaddrinfo(server, port, &hints, &res);
    178 	if (error) {
    179 		warnx("%s: %s", server, gai_strerror(error));
    180 		return (1);
    181 	}
    182 
    183 	for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
    184 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    185 		if (s < 0) {
    186 			reason = "socket";
    187 			continue;
    188 		}
    189 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
    190 			reason = "connect";
    191 			close(s);
    192 			s = -1;
    193 			continue;
    194 		}
    195 		break;	/*okay*/
    196 	}
    197 	if (s < 0) {
    198 		if (reason)
    199 			warn("%s: %s", server, reason);
    200 		else
    201 			warn("unknown error in connection attempt");
    202 		freeaddrinfo(res);
    203 		return (1);
    204 	}
    205 
    206 	sfi = fdopen(s, "r");
    207 	sfo = fdopen(s, "w");
    208 	if (sfi == NULL || sfo == NULL)
    209 		err(1, "fdopen");
    210 	(void)fprintf(sfo, "%s\r\n", query);
    211 	(void)fflush(sfo);
    212 	nhost = NULL;
    213 	while ((buf = fgetln(sfi, &len)) != NULL) {
    214 		p = buf + len - 1;
    215 		if (isspace((unsigned char)*p)) {
    216 			do
    217 				*p = '\0';
    218 			while (p > buf && isspace((unsigned char)*--p));
    219 		} else {
    220 			if ((nbuf = malloc(len + 1)) == NULL)
    221 				err(1, "malloc");
    222 			memcpy(nbuf, buf, len);
    223 			nbuf[len] = '\0';
    224 			buf = nbuf;
    225 		}
    226 		(void)puts(buf);
    227 
    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;
    233 			while (isblank(*p))
    234 				p++;
    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 			}
    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]);
    247 					if (nhost == NULL)
    248 						err(1, "strdup");
    249 					break;
    250 				}
    251 			}
    252 		}
    253 	}
    254 	if (nbuf != NULL)
    255 		free(nbuf);
    256 
    257 	if (nhost != NULL) {
    258 		error = whois(query, nhost, port, 0);
    259 		free(nhost);
    260 	}
    261 	freeaddrinfo(res);
    262 	return (error);
    263 }
    264 
    265 /*
    266  * If no country is specified determine the top level domain from the query.
    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.
    271  */
    272 static char *
    273 choose_server(const char *name, const char *country)
    274 {
    275 	static char *server;
    276 	const char *qhead;
    277 	char *ep;
    278 	size_t len;
    279 
    280 	if (country != NULL)
    281 		qhead = country;
    282 	else if ((qhead = strrchr(name, '.')) == NULL) {
    283 		if (*name == '!')
    284 			return (INICHOST);
    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)))
    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);
    299 }
    300 
    301 static __dead void
    302 usage(void)
    303 {
    304 	extern char *__progname;
    305 
    306 	(void)fprintf(stderr,
    307 	    "usage: %s [-aAdgilmQrR6] [-c country-code | -h hostname] "
    308 		"[-p port] name ...\n", __progname);
    309 	exit(1);
    310 }
    311