From owner-svn-src-all@FreeBSD.ORG Thu May 14 10:31:38 2015 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id F246BEC0; Thu, 14 May 2015 10:31:37 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DF25F1FA7; Thu, 14 May 2015 10:31:37 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t4EAVbmA038135; Thu, 14 May 2015 10:31:37 GMT (envelope-from fanf@FreeBSD.org) Received: (from fanf@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t4EAVbJA038134; Thu, 14 May 2015 10:31:37 GMT (envelope-from fanf@FreeBSD.org) Message-Id: <201505141031.t4EAVbJA038134@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: fanf set sender to fanf@FreeBSD.org using -f From: Tony Finch Date: Thu, 14 May 2015 10:31:37 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r282884 - head/usr.bin/whois X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 May 2015 10:31:38 -0000 Author: fanf Date: Thu May 14 10:31:37 2015 New Revision: 282884 URL: https://svnweb.freebsd.org/changeset/base/282884 Log: whois: special case certain query suffixes This extends the existing support for -NORID handles to include -NICAT, -ARIN, and -RIPE handles. The suffix machinery is also used to work around a problem with lack of referrals from the Nominet server for .uk: names under .ac.uk need to be queried directly at JANET's whois server. Modified: head/usr.bin/whois/whois.c Modified: head/usr.bin/whois/whois.c ============================================================================== --- head/usr.bin/whois/whois.c Thu May 14 10:30:37 2015 (r282883) +++ head/usr.bin/whois/whois.c Thu May 14 10:31:37 2015 (r282884) @@ -71,7 +71,6 @@ __FBSDID("$FreeBSD$"); #define MNICHOST "whois.ra.net" #define QNICHOST_TAIL ".whois-servers.net" #define BNICHOST "whois.registro.br" -#define NORIDHOST "whois.norid.no" #define IANAHOST "whois.iana.org" #define GERMNICHOST "de.whois-servers.net" #define FNICHOST "whois.afrinic.net" @@ -84,6 +83,19 @@ __FBSDID("$FreeBSD$"); #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-') +static struct { + const char *suffix, *server; +} whoiswhere[] = { + /* Various handles */ + { "-ARIN", ANICHOST }, + { "-NICAT", "at" QNICHOST_TAIL }, + { "-NORID", "no" QNICHOST_TAIL }, + { "-RIPE", RNICHOST }, + /* Nominet's whois server doesn't return referrals to JANET */ + { ".ac.uk", "ac.uk" QNICHOST_TAIL }, + { NULL, NULL } +}; + static const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, FNICHOST, NULL }; static const char *port = DEFAULT_PORT; @@ -176,10 +188,8 @@ main(int argc, char *argv[]) usage(); /* - * If no host or country is specified determine the top level domain - * from the query. If the TLD is a number, query ARIN. Otherwise, use - * TLD.whois-server.net. If the domain does not contain '.', fall - * back to NICHOST. + * If no host or country is specified, try to determine the top + * level domain from the query, or fall back to NICHOST. */ if (host == NULL && country == NULL) { if ((host = getenv("RA_SERVER")) == NULL) { @@ -210,25 +220,32 @@ main(int argc, char *argv[]) * returns a pointer to newly allocated memory containing the whois server to * be queried, or a NULL if the correct server couldn't be determined. The * caller must remember to free(3) the allocated memory. + * + * If the domain is an IPv6 address or has a known suffix, that determines + * the server, else if the TLD is a number, query ARIN, else use + * TLD.whois-server.net. Fail if the domain does not contain '.'. */ static char * choose_server(char *domain) { char *pos, *retval; + int i; if (strchr(domain, ':')) { s_asprintf(&retval, "%s", ANICHOST); return (retval); } - for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';) - *pos = '\0'; + for (pos = strchr(domain, '\0'); pos > domain && pos[-1] == '.';) + *--pos = '\0'; if (*domain == '\0') errx(EX_USAGE, "can't search for a null string"); - if (strlen(domain) > sizeof("-NORID")-1 && - strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1, - "-NORID") == 0) { - s_asprintf(&retval, "%s", NORIDHOST); - return (retval); + for (i = 0; whoiswhere[i].suffix != NULL; i++) { + size_t suffix_len = strlen(whoiswhere[i].suffix); + if (domain + suffix_len < pos && + strcasecmp(pos - suffix_len, whoiswhere[i].suffix) == 0) { + s_asprintf(&retval, "%s", whoiswhere[i].server); + return (retval); + } } while (pos > domain && *pos != '.') --pos;