1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #if 0 41 #ifndef lint 42 static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93"; 43 #endif /* not lint */ 44 #endif 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <netinet/in.h> 52 #include <arpa/inet.h> 53 #include <ctype.h> 54 #include <err.h> 55 #include <netdb.h> 56 #include <stdarg.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <sysexits.h> 61 #include <unistd.h> 62 63 #define ABUSEHOST "whois.abuse.net" 64 #define NICHOST "whois.crsnic.net" 65 #define INICHOST "whois.networksolutions.com" 66 #define DNICHOST "whois.nic.mil" 67 #define GNICHOST "whois.nic.gov" 68 #define ANICHOST "whois.arin.net" 69 #define LNICHOST "whois.lacnic.net" 70 #define KNICHOST "whois.krnic.net" 71 #define RNICHOST "whois.ripe.net" 72 #define PNICHOST "whois.apnic.net" 73 #define MNICHOST "whois.ra.net" 74 #define QNICHOST_TAIL ".whois-servers.net" 75 #define SNICHOST "whois.6bone.net" 76 #define BNICHOST "whois.registro.br" 77 #define NORIDHOST "whois.norid.no" 78 #define IANAHOST "whois.iana.org" 79 #define GERMNICHOST "de.whois-servers.net" 80 #define FNICHOST "whois.afrinic.net" 81 #define DEFAULT_PORT "whois" 82 #define WHOIS_SERVER_ID "Whois Server: " 83 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:" 84 85 #define WHOIS_RECURSE 0x01 86 #define WHOIS_QUICK 0x02 87 88 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-') 89 90 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, 91 FNICHOST, NULL }; 92 const char *port = DEFAULT_PORT; 93 94 static char *choose_server(char *); 95 static struct addrinfo *gethostinfo(char const *host, int exit_on_error); 96 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3); 97 static void usage(void); 98 static void whois(const char *, const char *, int); 99 100 int 101 main(int argc, char *argv[]) 102 { 103 const char *country, *host; 104 char *qnichost; 105 int ch, flags, use_qnichost; 106 107 #ifdef SOCKS 108 SOCKSinit(argv[0]); 109 #endif 110 111 country = host = qnichost = NULL; 112 flags = use_qnichost = 0; 113 while ((ch = getopt(argc, argv, "aAbc:dfgh:iIklmp:QrR6")) != -1) { 114 switch (ch) { 115 case 'a': 116 host = ANICHOST; 117 break; 118 case 'A': 119 host = PNICHOST; 120 break; 121 case 'b': 122 host = ABUSEHOST; 123 break; 124 case 'c': 125 country = optarg; 126 break; 127 case 'd': 128 host = DNICHOST; 129 break; 130 case 'f': 131 host = FNICHOST; 132 break; 133 case 'g': 134 host = GNICHOST; 135 break; 136 case 'h': 137 host = optarg; 138 break; 139 case 'i': 140 host = INICHOST; 141 break; 142 case 'I': 143 host = IANAHOST; 144 break; 145 case 'k': 146 host = KNICHOST; 147 break; 148 case 'l': 149 host = LNICHOST; 150 break; 151 case 'm': 152 host = MNICHOST; 153 break; 154 case 'p': 155 port = optarg; 156 break; 157 case 'Q': 158 flags |= WHOIS_QUICK; 159 break; 160 case 'r': 161 host = RNICHOST; 162 break; 163 case 'R': 164 warnx("-R is deprecated; use '-c ru' instead"); 165 country = "ru"; 166 break; 167 case '6': 168 host = SNICHOST; 169 break; 170 case '?': 171 default: 172 usage(); 173 /* NOTREACHED */ 174 } 175 } 176 argc -= optind; 177 argv += optind; 178 179 if (!argc || (country != NULL && host != NULL)) 180 usage(); 181 182 /* 183 * If no host or country is specified determine the top level domain 184 * from the query. If the TLD is a number, query ARIN. Otherwise, use 185 * TLD.whois-server.net. If the domain does not contain '.', fall 186 * back to NICHOST. 187 */ 188 if (host == NULL && country == NULL) { 189 use_qnichost = 1; 190 host = NICHOST; 191 if (!(flags & WHOIS_QUICK)) 192 flags |= WHOIS_RECURSE; 193 } 194 while (argc-- > 0) { 195 if (country != NULL) { 196 s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL); 197 whois(*argv, qnichost, flags); 198 } else if (use_qnichost) 199 if ((qnichost = choose_server(*argv)) != NULL) 200 whois(*argv, qnichost, flags); 201 if (qnichost == NULL) 202 whois(*argv, host, flags); 203 free(qnichost); 204 qnichost = NULL; 205 argv++; 206 } 207 exit(0); 208 } 209 210 /* 211 * This function will remove any trailing periods from domain, after which it 212 * returns a pointer to newly allocated memory containing the whois server to 213 * be queried, or a NULL if the correct server couldn't be determined. The 214 * caller must remember to free(3) the allocated memory. 215 */ 216 static char * 217 choose_server(char *domain) 218 { 219 char *pos, *retval; 220 221 for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';) 222 *pos = '\0'; 223 if (*domain == '\0') 224 errx(EX_USAGE, "can't search for a null string"); 225 if (strlen(domain) > sizeof("-NORID")-1 && 226 strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1, 227 "-NORID") == 0) { 228 s_asprintf(&retval, "%s", NORIDHOST); 229 return (retval); 230 } 231 while (pos > domain && *pos != '.') 232 --pos; 233 if (pos <= domain) 234 return (NULL); 235 if (isdigit((unsigned char)*++pos)) 236 s_asprintf(&retval, "%s", ANICHOST); 237 else 238 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL); 239 return (retval); 240 } 241 242 static struct addrinfo * 243 gethostinfo(char const *host, int exit_on_error) 244 { 245 struct addrinfo hints, *res; 246 int error; 247 248 memset(&hints, 0, sizeof(hints)); 249 hints.ai_flags = 0; 250 hints.ai_family = AF_UNSPEC; 251 hints.ai_socktype = SOCK_STREAM; 252 error = getaddrinfo(host, port, &hints, &res); 253 if (error) { 254 warnx("%s: %s", host, gai_strerror(error)); 255 if (exit_on_error) 256 exit(EX_NOHOST); 257 return (NULL); 258 } 259 return (res); 260 } 261 262 /* 263 * Wrapper for asprintf(3) that exits on error. 264 */ 265 static void 266 s_asprintf(char **ret, const char *format, ...) 267 { 268 va_list ap; 269 270 va_start(ap, format); 271 if (vasprintf(ret, format, ap) == -1) { 272 va_end(ap); 273 err(EX_OSERR, "vasprintf()"); 274 } 275 va_end(ap); 276 } 277 278 static void 279 whois(const char *query, const char *hostname, int flags) 280 { 281 FILE *sfi, *sfo; 282 struct addrinfo *hostres, *res; 283 char *buf, *host, *nhost, *p; 284 int i, s; 285 size_t c, len; 286 287 s = -1; 288 hostres = gethostinfo(hostname, 1); 289 for (res = hostres; res; res = res->ai_next) { 290 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 291 if (s < 0) 292 continue; 293 if (connect(s, res->ai_addr, res->ai_addrlen) == 0) 294 break; 295 close(s); 296 } 297 freeaddrinfo(hostres); 298 if (res == NULL) 299 err(EX_OSERR, "connect()"); 300 301 sfi = fdopen(s, "r"); 302 sfo = fdopen(s, "w"); 303 if (sfi == NULL || sfo == NULL) 304 err(EX_OSERR, "fdopen()"); 305 if (strcmp(hostname, GERMNICHOST) == 0) { 306 fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query); 307 } else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) { 308 fprintf(sfo, "--show-handles %s\r\n", query); 309 } else { 310 fprintf(sfo, "%s\r\n", query); 311 } 312 fflush(sfo); 313 nhost = NULL; 314 while ((buf = fgetln(sfi, &len)) != NULL) { 315 while (len > 0 && isspace((unsigned char)buf[len - 1])) 316 buf[--len] = '\0'; 317 printf("%.*s\n", (int)len, buf); 318 319 if ((flags & WHOIS_RECURSE) && nhost == NULL) { 320 host = strnstr(buf, WHOIS_SERVER_ID, len); 321 if (host != NULL) { 322 host += sizeof(WHOIS_SERVER_ID) - 1; 323 for (p = host; p < buf + len; p++) { 324 if (!ishost(*p)) { 325 *p = '\0'; 326 break; 327 } 328 } 329 s_asprintf(&nhost, "%.*s", 330 (int)(buf + len - host), host); 331 } else if ((host = 332 strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) { 333 host += sizeof(WHOIS_ORG_SERVER_ID) - 1; 334 for (p = host; p < buf + len; p++) { 335 if (!ishost(*p)) { 336 *p = '\0'; 337 break; 338 } 339 } 340 s_asprintf(&nhost, "%.*s", 341 (int)(buf + len - host), host); 342 } else if (strcmp(hostname, ANICHOST) == 0) { 343 for (c = 0; c <= len; c++) 344 buf[c] = tolower((unsigned char)buf[c]); 345 for (i = 0; ip_whois[i] != NULL; i++) { 346 if (strnstr(buf, ip_whois[i], len) != 347 NULL) { 348 s_asprintf(&nhost, "%s", 349 ip_whois[i]); 350 break; 351 } 352 } 353 } 354 } 355 } 356 if (nhost != NULL) { 357 whois(query, nhost, 0); 358 free(nhost); 359 } 360 } 361 362 static void 363 usage(void) 364 { 365 fprintf(stderr, 366 "usage: whois [-aAbdfgiIklmQrR6] [-c country-code | -h hostname] " 367 "[-p port] name ...\n"); 368 exit(EX_USAGE); 369 } 370