1 /* $NetBSD: whois.c,v 1.28 2005/07/22 16:01:52 christos 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 #include <sys/cdefs.h> 34 35 #ifndef lint 36 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"); 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93"; 43 #else 44 __RCSID("$NetBSD: whois.c,v 1.28 2005/07/22 16:01:52 christos Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 51 #include <netinet/in.h> 52 #include <arpa/inet.h> 53 #include <netdb.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <errno.h> 62 63 #define NICHOST "whois.crsnic.net" 64 #define INICHOST "whois.networksolutions.com" 65 #define CNICHOST "whois.corenic.net" 66 #define DNICHOST "whois.nic.mil" 67 #define GNICHOST "whois.nic.gov" 68 #define ANICHOST "whois.arin.net" 69 #define RNICHOST "whois.ripe.net" 70 #define PNICHOST "whois.apnic.net" 71 #define RUNICHOST "whois.ripn.net" 72 #define MNICHOST "whois.ra.net" 73 #define LNICHOST "whois.lacnic.net" 74 #define SNICHOST "whois.6bone.net" 75 #define BNICHOST "whois.registro.br" 76 #define QNICHOST_TAIL ".whois-servers.net" 77 78 #define WHOIS_PORT "whois" 79 #define WHOIS_SERVER_ID "Whois Server:" 80 81 #define WHOIS_RECURSE 0x01 82 #define WHOIS_QUICK 0x02 83 84 static const char *port_whois = WHOIS_PORT; 85 static const char *ip_whois[] = 86 { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL }; 87 88 static void usage(void) __attribute__((__noreturn__)); 89 static int whois(const char *, const char *, const char *, int); 90 static char *choose_server(const char *, const char *); 91 92 int 93 main(int argc, char *argv[]) 94 { 95 int ch, flags, rval; 96 char *host, *name, *country, *server; 97 98 #ifdef SOCKS 99 SOCKSinit(argv[0]); 100 #endif 101 country = host = server = NULL; 102 flags = rval = 0; 103 while ((ch = getopt(argc, argv, "6Aac:dgh:ilmp:qQRr")) != -1) 104 switch(ch) { 105 case 'a': 106 host = ANICHOST; 107 break; 108 case 'A': 109 host = PNICHOST; 110 break; 111 case 'c': 112 country = optarg; 113 break; 114 case 'd': 115 host = DNICHOST; 116 break; 117 case 'g': 118 host = GNICHOST; 119 break; 120 case 'h': 121 host = optarg; 122 break; 123 case 'i': 124 host = INICHOST; 125 break; 126 case 'l': 127 host = LNICHOST; 128 break; 129 case 'm': 130 host = MNICHOST; 131 break; 132 case 'p': 133 port_whois = optarg; 134 break; 135 case 'q': 136 /* deprecated, now the default */ 137 break; 138 case 'Q': 139 flags |= WHOIS_QUICK; 140 break; 141 case 'r': 142 host = RNICHOST; 143 break; 144 case 'R': 145 host = RUNICHOST; 146 break; 147 case '6': 148 host = SNICHOST; 149 break; 150 default: 151 usage(); 152 } 153 argc -= optind; 154 argv += optind; 155 156 if (!argc || (country != NULL && host != NULL)) 157 usage(); 158 159 if (host == NULL && country == NULL && !(flags & WHOIS_QUICK)) 160 flags |= WHOIS_RECURSE; 161 for (name = *argv; (name = *argv) != NULL; argv++) 162 rval += whois(name, host ? host : choose_server(name, country), 163 port_whois, flags); 164 return rval; 165 } 166 167 static int 168 whois(const char *query, const char *server, const char *port, int flags) 169 { 170 FILE *sfi, *sfo; 171 char *buf, *p, *nhost, *nbuf = NULL; 172 size_t len; 173 int i, s, error; 174 const char *reason = NULL, *fmt; 175 struct addrinfo hints, *res, *ai; 176 177 (void)memset(&hints, 0, sizeof(hints)); 178 hints.ai_flags = 0; 179 hints.ai_family = AF_UNSPEC; 180 hints.ai_socktype = SOCK_STREAM; 181 error = getaddrinfo(server, port, &hints, &res); 182 if (error) { 183 warnx("%s: %s", server, gai_strerror(error)); 184 return (1); 185 } 186 187 for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) { 188 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 189 if (s < 0) { 190 error = errno; 191 reason = "socket"; 192 continue; 193 } 194 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 195 error = errno; 196 reason = "connect"; 197 close(s); 198 s = -1; 199 continue; 200 } 201 break; /*okay*/ 202 } 203 if (s < 0) { 204 if (reason) { 205 errno = error; 206 warn("%s: %s", server, reason); 207 } else 208 warnx("Unknown error in connection attempt"); 209 freeaddrinfo(res); 210 return (1); 211 } 212 213 if (strcmp(server, "whois.denic.de") == 0 || 214 strcmp(server, "de.whois-servers.net") == 0) 215 fmt = "-T dn,ace -C ISO-8859-1 %s\r\n"; 216 else 217 fmt = "%s\r\n"; 218 219 sfi = fdopen(s, "r"); 220 sfo = fdopen(s, "w"); 221 if (sfi == NULL || sfo == NULL) 222 err(1, "fdopen"); 223 (void)fprintf(sfo, fmt, query); 224 (void)fflush(sfo); 225 nhost = NULL; 226 while ((buf = fgetln(sfi, &len)) != NULL) { 227 p = buf + len - 1; 228 if (isspace((unsigned char)*p)) { 229 do 230 *p = '\0'; 231 while (p > buf && isspace((unsigned char)*--p)); 232 } else { 233 if ((nbuf = malloc(len + 1)) == NULL) 234 err(1, "malloc"); 235 (void)memcpy(nbuf, buf, len); 236 nbuf[len] = '\0'; 237 buf = nbuf; 238 } 239 (void)puts(buf); 240 241 if (nhost != NULL || !(flags & WHOIS_RECURSE)) 242 continue; 243 244 if ((p = strstr(buf, WHOIS_SERVER_ID))) { 245 p += sizeof(WHOIS_SERVER_ID) - 1; 246 while (isblank((unsigned char)*p)) 247 p++; 248 if ((len = strcspn(p, " \t\n\r"))) { 249 if ((nhost = malloc(len + 1)) == NULL) 250 err(1, "malloc"); 251 (void)memcpy(nhost, p, len); 252 nhost[len] = '\0'; 253 } 254 } else if (strcmp(server, ANICHOST) == 0) { 255 for (p = buf; *p != '\0'; p++) 256 *p = tolower((unsigned char)*p); 257 for (i = 0; ip_whois[i] != NULL; i++) { 258 if (strstr(buf, ip_whois[i]) != NULL) { 259 nhost = strdup(ip_whois[i]); 260 if (nhost == NULL) 261 err(1, "strdup"); 262 break; 263 } 264 } 265 } 266 } 267 if (nbuf != NULL) 268 free(nbuf); 269 270 if (nhost != NULL) { 271 error = whois(query, nhost, port, 0); 272 free(nhost); 273 } 274 freeaddrinfo(res); 275 return (error); 276 } 277 278 /* 279 * If no country is specified determine the top level domain from the query. 280 * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net. 281 * If the domain does not contain '.', check to see if it is an NSI handle 282 * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+). 283 * Fall back to NICHOST for the non-handle case. 284 */ 285 static char * 286 choose_server(const char *name, const char *country) 287 { 288 static char *server; 289 char *nserver; 290 const char *qhead; 291 char *ep; 292 size_t len; 293 294 if (country != NULL) 295 qhead = country; 296 else if ((qhead = strrchr(name, '.')) == NULL) { 297 if (*name == '!') 298 return (INICHOST); 299 else if ((strncasecmp(name, "COCO-", 5) == 0 || 300 strncasecmp(name, "COHO-", 5) == 0) && 301 strtol(name + 5, &ep, 10) > 0 && *ep == '\0') 302 return (CNICHOST); 303 else 304 return (NICHOST); 305 } else if (isdigit((unsigned char)*(++qhead))) 306 return (ANICHOST); 307 len = strlen(qhead) + sizeof(QNICHOST_TAIL); 308 if ((nserver = realloc(server, len)) == NULL) 309 err(1, "realloc"); 310 server = nserver; 311 (void)strlcpy(server, qhead, len); 312 (void)strlcat(server, QNICHOST_TAIL, len); 313 return (server); 314 } 315 316 static void 317 usage(void) 318 { 319 (void)fprintf(stderr, 320 "usage: %s [-6AadgilmQRr] [-c country-code | -h hostname] " 321 "[-p port] name ...\n", getprogname()); 322 exit(1); 323 } 324